Fixed BillEntry: milliseconds are rounded off. BillTest can send ordered/out of order...
[aquarium] / src / main / scala / gr / grnet / aquarium / charging / bill / BillEntry.scala
1 package gr.grnet.aquarium.charging.bill
2
3 import gr.grnet.aquarium.charging.state.WorkingUserState
4 import gr.grnet.aquarium.util.json.JsonSupport
5 import com.ckkloverdos.resource.FileStreamResource
6 import java.io.File
7 import com.ckkloverdos.props.Props
8 import gr.grnet.aquarium.converter.{CompactJsonTextFormat, PrettyJsonTextFormat, StdConverters}
9 import gr.grnet.aquarium.{Aquarium, ResourceLocator, AquariumBuilder}
10 import gr.grnet.aquarium.store.memory.MemStoreProvider
11 import gr.grnet.aquarium.converter.StdConverters._
12 import scala._
13 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
14 import java.util.concurrent.atomic.AtomicLong
15 import java.util.{Date, Calendar, GregorianCalendar}
16 import gr.grnet.aquarium.charging.wallet.WalletEntry
17 import scala.collection.parallel.mutable
18 import scala.collection.mutable.ListBuffer
19 import gr.grnet.aquarium.Aquarium.EnvKeys
20 import gr.grnet.aquarium.charging.Chargeslot
21 import scala.collection.immutable.TreeMap
22 import scala.Some
23 import gr.grnet.aquarium.charging.Chargeslot
24 import gr.grnet.aquarium.util.Lock
25
26
27 /*
28 * Copyright 2011-2012 GRNET S.A. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or
31 * without modification, are permitted provided that the following
32 * conditions are met:
33 *
34 *   1. Redistributions of source code must retain the above
35 *      copyright notice, this list of conditions and the following
36 *      disclaimer.
37 *
38 *   2. Redistributions in binary form must reproduce the above
39 *      copyright notice, this list of conditions and the following
40 *      disclaimer in the documentation and/or other materials
41 *      provided with the distribution.
42 *
43 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
47 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
51 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
53 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 * POSSIBILITY OF SUCH DAMAGE.
55 *
56 * The views and conclusions contained in the software and
57 * documentation are those of the authors and should not be
58 * interpreted as representing official policies, either expressed
59 * or implied, of GRNET S.A.
60 */
61
62
63 /*
64 * @author Prodromos Gerakios <pgerakios@grnet.gr>
65 */
66
67 case class ChargeEntry(val id:String,
68                        val unitPrice:String,
69                        val startTime:String,
70                        val endTime:String,
71                        val ellapsedTime:String,
72                        val credits:String)
73   extends JsonSupport {}
74
75
76 class EventEntry(val eventType : String,
77                  val details   : List[ChargeEntry])
78  extends JsonSupport {}
79
80
81 case class ResourceEntry(val resourceName : String,
82                          val resourceType : String,
83                          val unitName : String,
84                          val totalCredits : String,
85                          val details : List[EventEntry])
86 extends JsonSupport {}
87
88
89 abstract class AbstractBillEntry
90  extends JsonSupport {}
91
92 class BillEntry(val id:String,
93                 val userID : String,
94                 val status : String,
95                 val remainingCredits:String,
96                 val deductedCredits:String,
97                 val startTime:String,
98                 val endTime:String,
99                 val bill:List[ResourceEntry]
100               )
101  extends AbstractBillEntry {}
102
103
104 object AbstractBillEntry {
105
106   private[this] val counter = new AtomicLong(0L)
107   private[this] def nextUIDObject() = counter.getAndIncrement
108
109   /*private[this] def walletTimeslot(i:WalletEntry) : Timeslot = {
110     val cal = new GregorianCalendar
111     cal.set(i.billingYear,i.billingMonth,1)
112     val dstart = cal.getTime
113     val lastDate = cal.getActualMaximum(Calendar.DATE)
114     cal.set(Calendar.DATE, lastDate)
115     val dend = cal.getTime
116    Timeslot(dstart,dend)
117   } */
118
119   private[this] def toChargeEntry(c:Chargeslot) : ChargeEntry = {
120     val unitPrice = c.unitPrice.toString
121     val startTime = c.startMillis.toString
122     val endTime   = c.stopMillis.toString
123     val difTime   = (c.stopMillis - c.startMillis).toString
124     val credits   = c.creditsToSubtract.toString
125     new ChargeEntry(counter.getAndIncrement.toString,unitPrice,
126                     startTime,endTime,difTime,credits)
127   }
128
129   private[this] def toEventEntry(eventType:String,c:Chargeslot) : EventEntry =
130     new EventEntry(eventType,List(toChargeEntry(c)))
131
132
133   private[this] def toResourceEntry(w:WalletEntry) : ResourceEntry = {
134     assert(w.sumOfCreditsToSubtract==0.0 || w.chargslotCount > 0)
135     val rcType =  w.resourceType.name
136     val rcName = rcType match {
137             case "diskspace" =>
138               w.currentResourceEvent.details("path")
139             case _ =>
140               w.currentResourceEvent.instanceID
141         }
142     val rcUnitName = w.resourceType.unit
143     val eventEntry = new ListBuffer[EventEntry]
144     val credits = w.sumOfCreditsToSubtract
145     val eventType = //TODO: This is hardcoded; find a better solution
146         rcType match {
147           case "diskspace" =>
148             val action = w.currentResourceEvent.details("action")
149             val path = w.currentResourceEvent.details("path")
150             //"%s@%s".format(action,path)
151             action
152           case "vmtime" =>
153             w.currentResourceEvent.value.toInt match {
154               case 0 => // OFF
155                   "offOn"
156               case 1 =>  // ON
157                  "onOff"
158               case 2 =>
159                  "destroy"
160               case _ =>
161                  "BUG"
162             }
163           case "addcredits" =>
164             "once"
165         }
166
167     for { c <- w.chargeslots }{
168       if(c.creditsToSubtract != 0.0) {
169         //Console.err.println("c.creditsToSubtract : " + c.creditsToSubtract)
170         eventEntry += toEventEntry(eventType.toString,c)
171         //credits += c.creditsToSubtract
172       }
173     }
174     //Console.err.println("TOTAL resource event credits: " + credits)
175     new ResourceEntry(rcName,rcType,rcUnitName,credits.toString,eventEntry.toList)
176   }
177
178   private[this] def resourceEntriesAt(t:Timeslot,w:WorkingUserState) : (List[ResourceEntry],Double) = {
179     val ret = new ListBuffer[ResourceEntry]
180     var sum = 0.0
181     //Console.err.println("Wallet entries: " + w.walletEntries)
182     val walletEntries = w.walletEntries
183     /*Console.err.println("Wallet entries ")
184     for { i <- walletEntries }
185       Console.err.println("WALLET ENTRY\n%s\nEND WALLET ENTRY".format(i.toJsonString))
186     Console.err.println("End wallet entries")*/
187     for { i <- walletEntries} {
188       if(t.contains(i.referenceTimeslot) && i.sumOfCreditsToSubtract != 0.0){
189         /*Console.err.println("i.sumOfCreditsToSubtract : " + i.sumOfCreditsToSubtract)*/
190         if(i.sumOfCreditsToSubtract > 0.0D) sum += i.sumOfCreditsToSubtract
191         ret += toResourceEntry(i)
192       } else {
193         Console.err.println("IGNORING WALLET ENTRY : " + i.toJsonString + "\n" +
194                      t + "  does not contain " +  i.referenceTimeslot + "  !!!!")
195       }
196     }
197     (ret.toList,sum)
198   }
199
200   private[this] def aggregateResourceEntries(re:List[ResourceEntry]) : List[ResourceEntry] = {
201     def addResourceEntries(a:ResourceEntry,b:ResourceEntry) : ResourceEntry = {
202       assert(a.resourceName == b.resourceName)
203       val totalCredits = (a.totalCredits.toDouble+b.totalCredits.toDouble).toString
204       a.copy(a.resourceName,a.resourceType,a.unitName,totalCredits,a.details ::: b.details)
205     }
206     re.foldLeft(TreeMap[String,ResourceEntry]()){ (map,r1) =>
207       map.get(r1.resourceName) match {
208         case None => map + ((r1.resourceName,r1))
209         case Some(r0) => (map - r0.resourceName) +
210                          ((r0.resourceName, addResourceEntries(r0,r1)))
211       }
212     }.values.toList
213   }
214
215   def fromWorkingUserState(t0:Timeslot,userID:String,w:Option[WorkingUserState]) : AbstractBillEntry = {
216     val t = t0.roundMilliseconds /* we do not care about milliseconds */
217     //Console.err.println("Timeslot: " + t0)
218     //Console.err.println("After rounding timeslot: " + t)
219     val ret = w match {
220       case None =>
221           new BillEntry(counter.getAndIncrement.toString,
222                         userID,"processing",
223                         "0.0",
224                         "0.0",
225                         t.from.getTime.toString,t.to.getTime.toString,
226                         Nil)
227       case Some(w) =>
228         Console.err.println("Working user state: %s".format(w.toJsonString))
229         val (rcEntries,rcEntriesCredits) = resourceEntriesAt(t,w)
230         val resMap = aggregateResourceEntries(rcEntries)
231         new BillEntry(counter.getAndIncrement.toString,
232                       userID,"ok",
233                       w.totalCredits.toString,
234                       rcEntriesCredits.toString,
235                       t.from.getTime.toString,t.to.getTime.toString,
236                       resMap)
237     }
238     //Console.err.println("JSON: " +  ret.toJsonString)
239     ret
240   }
241
242   val jsonSample = "{\n  \"id\":\"2\",\n  \"userID\":\"loverdos@grnet.gr\",\n  \"status\":\"ok\",\n  \"remainingCredits\":\"3130.0000027777783\",\n  \"deductedCredits\":\"5739.9999944444435\",\n  \"startTime\":\"1341090000000\",\n  \"endTime\":\"1343768399999\",\n  \"bill\":[{\n    \"resourceName\":\"diskspace\",\n    \"resourceType\":\"diskspace\",\n    \"unitName\":\"MB/Hr\",\n    \"totalCredits\":\"2869.9999972222217\",\n    \"eventType\":\"object update@/Papers/GOTO_HARMFUL.PDF\",\n\t    \"details\":[\n\t     {\"totalCredits\":\"2869.9999972222217\",\n\t      \"details\":[{\n\t      \"id\":\"0\",\n\t      \"unitPrice\":\"0.01\",\n\t      \"startTime\":\"1342735200000\",\n\t      \"endTime\":\"1343768399999\",\n\t      \"ellapsedTime\":\"1033199999\",\n\t      \"credits\":\"2869.9999972222217\"\n\t    \t}]\n\t    }\n\t  ]\n  },{\n    \"resourceName\":\"diskspace\",\n    \"resourceType\":\"diskspace\",\n    \"unitName\":\"MB/Hr\",\n    \"totalCredits\":\"2869.9999972222217\",\n    \"eventType\":\"object update@/Papers/GOTO_HARMFUL.PDF\",\n    \"details\":[\t     {\"totalCredits\":\"2869.9999972222217\",\n\t      \"details\":[{\n\t      \"id\":\"0\",\n\t      \"unitPrice\":\"0.01\",\n\t      \"startTime\":\"1342735200000\",\n\t      \"endTime\":\"1343768399999\",\n\t      \"ellapsedTime\":\"1033199999\",\n\t      \"credits\":\"2869.9999972222217\"\n\t    \t}]\n\t    }\n\t]\n  }]\n}"
243
244   def main0(args: Array[String]) = {
245      val b : BillEntry = StdConverters.AllConverters.convertEx[BillEntry](CompactJsonTextFormat(jsonSample))
246      val l0 = b.bill
247      val l1 = aggregateResourceEntries(l0)
248
249      Console.err.println("Initial resources: ")
250      for{ i <- l0 } Console.err.println("RESOURCE: " + i.toJsonString)
251     Console.err.println("Aggregate resources: ")
252     for{ a <- l1 } {
253       Console.err.println("RESOURCE:  %s\n  %s\nEND RESOURCE".format(a.resourceName,a.toJsonString))
254     }
255
256     val aggr = new BillEntry(b.id,b.userID,b.status,b.remainingCredits,b.deductedCredits,b.startTime,b.endTime,l1)
257     Console.err.println("Aggregate:\n" + aggr.toJsonString)
258   }
259
260   //
261   def main(args: Array[String]) = {
262     //Console.err.println("JSON: " +  (new BillEntry).toJsonString)
263     val propsfile = new FileStreamResource(new File("aquarium.properties"))
264     var _props: Props = Props(propsfile)(StdConverters.AllConverters).getOr(Props()(StdConverters.AllConverters))
265     val aquarium = new AquariumBuilder(_props, ResourceLocator.DefaultPolicyModel).
266       update(Aquarium.EnvKeys.storeProvider, new MemStoreProvider).
267       update(Aquarium.EnvKeys.eventsStoreFolder,Some(new File(".."))).
268       build()
269     aquarium.start()
270     ()
271   }
272 }