Added documentation to CronSpec and TimeslotComputations. Fixed a bug in ContinuousCh...
authorProdromos Gerakios <pgerakios@grnet.gr>
Thu, 30 Aug 2012 14:50:31 +0000 (17:50 +0300)
committerProdromos Gerakios <pgerakios@grnet.gr>
Thu, 30 Aug 2012 14:50:31 +0000 (17:50 +0300)
src/main/scala/gr/grnet/aquarium/actor/service/user/UserActor.scala
src/main/scala/gr/grnet/aquarium/charging/ContinuousChargingBehavior.scala
src/main/scala/gr/grnet/aquarium/charging/bill/BillEntry.scala
src/main/scala/gr/grnet/aquarium/computation/TimeslotComputations.scala
src/main/scala/gr/grnet/aquarium/policy/CronSpec.scala
src/test/scala/gr/grnet/aquarium/BillTest.scala

index f1c48ce..d174389 100644 (file)
@@ -297,6 +297,7 @@ class UserActor extends ReflectiveRoleableActor {
       } else {
       updateAgreementHistoryFrom(imEvent)
       updateLatestIMEventIDFrom(imEvent)
+        //Thread.sleep(3000)
       }
     }
 
index f8eb43a..a8ea14d 100644 (file)
@@ -35,7 +35,7 @@
 
 package gr.grnet.aquarium.charging
 
-import gr.grnet.aquarium.Aquarium
+import gr.grnet.aquarium.{AquariumInternalError, Aquarium}
 import gr.grnet.aquarium.charging.state.{AgreementHistoryModel, WorkingResourcesChargingState, WorkingResourceInstanceChargingState}
 import gr.grnet.aquarium.charging.wallet.WalletEntry
 import gr.grnet.aquarium.computation.BillingMonthInfo
@@ -45,6 +45,7 @@ import gr.grnet.aquarium.policy.{FullPriceTable, ResourceType}
 import gr.grnet.aquarium.util.LogHelpers.Debug
 import scala.collection.mutable
 import gr.grnet.aquarium.event.model.EventModel
+import java.util.Date
 
 /**
  * In practice a resource usage will be charged for the total amount of usage
@@ -141,14 +142,21 @@ final class ContinuousChargingBehavior extends ChargingBehaviorSkeleton(Nil) {
         )
 
         val dummyFirstEventValue = 0.0 // TODO From configuration
+
+        val millis = userAgreements.agreementByTimeslot.headOption match {
+          case None =>
+            throw new AquariumInternalError("No agreement!!!")
+          case Some((_,aggr)) =>
+            val millisAgg = aggr.timeslot.from.getTime
+            val millisMon = billingMonthInfo.monthStartMillis
+            if(millisAgg>millisMon) millisAgg else millisMon
+        }
+
         val dummyFirstEvent = resourceEvent.withDetailsAndValue(
             dummyFirstEventDetails,
             dummyFirstEventValue,
-            billingMonthInfo.monthStartMillis // TODO max(billingMonthInfo.monthStartMillis, userAgreementModel.validFromMillis)
-        )
-
+            millis)
         Debug(logger, "Dummy first event %s", dummyFirstEvent.toDebugString)
-
         dummyFirstEvent
     }
 
index 1133226..eacf99a 100644 (file)
@@ -146,7 +146,8 @@ object AbstractBillEntry {
           case "diskspace" =>
             val action = w.currentResourceEvent.details("action")
             val path = w.currentResourceEvent.details("path")
-            "%s@%s".format(action,path)
+            //"%s@%s".format(action,path)
+            action
           case "vmtime" =>
             w.currentResourceEvent.value.toInt match {
               case 0 => // OFF
@@ -178,14 +179,18 @@ object AbstractBillEntry {
     var sum = 0.0
     //Console.err.println("Wallet entries: " + w.walletEntries)
     val walletEntries = w.walletEntries
+    /*Console.err.println("Wallet entries ")
+    for { i <- walletEntries }
+      Console.err.println("WALLET ENTRY\n%s\nEND WALLET ENTRY".format(i.toJsonString))
+    Console.err.println("End wallet entries")*/
     for { i <- walletEntries} {
       if(t.contains(i.referenceTimeslot) && i.sumOfCreditsToSubtract != 0.0){
-        //Console.err.println("i.sumOfCreditsToSubtract : " + i.sumOfCreditsToSubtract)
+        /*Console.err.println("i.sumOfCreditsToSubtract : " + i.sumOfCreditsToSubtract)*/
         if(i.sumOfCreditsToSubtract > 0.0D) sum += i.sumOfCreditsToSubtract
         ret += toResourceEntry(i)
       } else {
-        //Console.err.println("WALLET ENTERY : " + i + "\n" +
-        //             t + "  does not contain " +  i.referenceTimeslot + "  !!!!")
+        /*Console.err.println("WALLET ENTERY : " + i.toJsonString + "\n" +
+                     t + "  does not contain " +  i.referenceTimeslot + "  !!!!")*/
       }
     }
     (ret.toList,sum)
@@ -218,6 +223,7 @@ object AbstractBillEntry {
       case Some(w) =>
         val (rcEntries,rcEntriesCredits) = resourceEntriesAt(t,w)
         val resMap = aggregateResourceEntries(rcEntries)
+        Console.err.println("Working user state: %s".format(w.toString))
         new BillEntry(counter.getAndIncrement.toString,
                       userID,"ok",
                       w.totalCredits.toString,
index f962a70..7f6bfd2 100644 (file)
@@ -180,7 +180,7 @@ object TimeslotComputations extends Loggable {
 
     if(a.isEmpty) return b
     if(b.isEmpty) return a
-
+    if(a.head.from != b.head.from)
     assert(a.head.from == b.head.from)
 
     if(a.head.endsAfter(b.head)) {
index 13e8333..2af9fd1 100644 (file)
@@ -42,7 +42,11 @@ import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
  * @author Prodromos Gerakios <pgerakios@grnet.gr>
  */
 
-
+/*
+ *  Format:
+ *  minutes hours day-of-month Month Day-of-Week (we do not specify seconds)
+ *  see: http://quartz-scheduler.org/api/2.0.0/org/quartz/CronExpression.html
+ * */
 case class CronSpec(cronSpec: String) {
 
   private val cronExpr = {
index ae21303..8d607be 100644 (file)
@@ -74,7 +74,7 @@ object BillTest extends Loggable {
       exec("mongo aquarium --eval db.resevents.remove();db.imevents.remove();db.policies.remove();db.userstates.remove()",
            Console.err.println(_))
       new AquariumBuilder(props, ResourceLocator.DefaultPolicyModel).
-      update(Aquarium.EnvKeys.storeProvider, new MemStoreProvider).
+      //update(Aquarium.EnvKeys.storeProvider, new MemStoreProvider).
       update(Aquarium.EnvKeys.eventsStoreFolder,Some(new File(".."))).
       build()
   }
@@ -136,7 +136,7 @@ object BillTest extends Loggable {
     val eventType = "addcredits"
     new StdIMEvent(id,occurredMillis,receivedMillis,userID,
                    clientID,isActive,role,eventVersion,eventType,
-                   Map()).toJsonString
+                   Map("credits" -> amount.toString)).toJsonString
   }
 
   private [this] def makePithos(date:DATE,uid:UID,path:String,
@@ -225,11 +225,11 @@ object BillTest extends Loggable {
     val creationDate = "15/00/00/03/08/2012"
     /* ADD CREDITS */
     val addCreditsDate = "18/15/00/05/08/2012"
-    val creditsToAdd = 5000
+    val creditsToAdd = 6000
     /* Pithos STUFF */
     val pithosPath = "/Papers/GOTO_HARMFUL.PDF"
 
-    val pithosDate1 = "08/30/00/05/08/2012"
+    val pithosDate1 = "20/30/00/05/08/2012"
     val pithosAction1 = "update"
     val pithosValue1 = 2000
 
@@ -238,14 +238,25 @@ object BillTest extends Loggable {
     val pithosAction2 = "update"
     val pithosValue2 = 4000
 
+
+    val pithosDate3 = "08/05/00/20/08/2012"
+    val pithosAction3 = "update"
+    val pithosValue3 = 100
+
     val id =
       sendCreate(creationDate)
+      //Thread.sleep(5000)
       sendAddCredits(addCreditsDate,id,creditsToAdd)
+      //Thread.sleep(5000)
       sendPithos(pithosDate1,id,pithosPath,pithosValue1,pithosAction1)
+      //Thread.sleep(5000)
       sendPithos(pithosDate2,id,pithosPath,pithosValue2,pithosAction2)
+      //
+      sendPithos(pithosDate3,id,pithosPath,pithosValue3,pithosAction3)
+
 
     Console.err.println("Waiting for stuff to be processed")
-    Thread.sleep(2000)
+    Thread.sleep(5000)
 
     var resp = ""
     var count = 0
@@ -263,6 +274,7 @@ object BillTest extends Loggable {
   def runTestCase(f: => JSON) = {
     var json = ""
     aquarium.start
+    Thread.sleep(2000)
     try{
       json = f
     }  catch{
@@ -270,6 +282,7 @@ object BillTest extends Loggable {
         e.printStackTrace
     }
     aquarium.stop
+    Thread.sleep(1000)
     Console.err.println("Response : " + json )
   }