Generalize implicit OFFs to implicitly issued events
authorChristos KK Loverdos <loverdos@gmail.com>
Fri, 24 Feb 2012 13:03:13 +0000 (15:03 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Fri, 24 Feb 2012 13:03:13 +0000 (15:03 +0200)
src/main/scala/gr/grnet/aquarium/logic/events/ResourceEvent.scala
src/main/scala/gr/grnet/aquarium/user/UserDataSnapshot.scala
src/main/scala/gr/grnet/aquarium/user/UserState.scala
src/main/scala/gr/grnet/aquarium/user/UserStateComputations.scala
src/main/scala/gr/grnet/aquarium/util/ContextualLogger.scala

index 4de4325..85a5a47 100644 (file)
@@ -273,7 +273,7 @@ object ResourceEvent {
   type ResourceType = String
   type ResourceIdType = String
   type FullResourceType = (ResourceType, ResourceIdType)
-  type FullResourceTypeMap = Map[FullResourceType, ResourceEvent]
+//  type FullResourceTypeMap = Map[FullResourceType, ResourceEvent]
   type FullMutableResourceTypeMap = scala.collection.mutable.Map[FullResourceType, ResourceEvent]
 
   def fromJson(json: String): ResourceEvent = {
index d0b09b4..1f2592f 100644 (file)
@@ -300,46 +300,47 @@ case class LatestResourceEventsWorker(resourceEventsMap: FullMutableResourceType
  * Keeps the implicit OFF events when a billing period ends.
  * This is normally recorded in the [[gr.grnet.aquarium.user.UserState]].
  *
- * @param implicitOFFEventsMap
+ * @param implicitlyIssuedEvents
  * @param snapshotTime
  *
  * @author Christos KK Loverdos <loverdos@gmail.com>
  */
-case class ImplicitOFFResourceEventsSnapshot(implicitOFFEventsMap: FullResourceTypeMap,
-                                             snapshotTime: Long) extends DataSnapshot {
+case class ImplicitlyIssuedResourceEventsSnapshot(implicitlyIssuedEvents: List[ResourceEvent],
+                                                  snapshotTime: Long) extends DataSnapshot {
   /**
    * The gateway to playing with mutable state.
    *
-   * @return A fresh instance of [[gr.grnet.aquarium.user.ImplicitOFFResourceEventsWorker]].
+   * @return A fresh instance of [[gr.grnet.aquarium.user.ImplicitlyIssuedResourceEventsWorker]].
    */
   def toMutableWorker = {
-    ImplicitOFFResourceEventsWorker(scala.collection.mutable.Map(implicitOFFEventsMap.toSeq: _*))
-  }
+    val map = scala.collection.mutable.Map[ResourceEvent.FullResourceType, ResourceEvent]()
+    for(implicitEvent <- implicitlyIssuedEvents) {
+      map(implicitEvent.fullResourceInfo) = implicitEvent
+    }
 
-  def findResourceEvent(resource: String, instanceId: String): Maybe[ResourceEvent] = {
-    findFromMapAsMaybe(implicitOFFEventsMap, (resource, instanceId))
+    ImplicitlyIssuedResourceEventsWorker(map)
   }
 }
 
 /**
- * This is the mutable cousin of [[gr.grnet.aquarium.user.ImplicitOFFResourceEventsSnapshot]].
+ * This is the mutable cousin of [[gr.grnet.aquarium.user.ImplicitlyIssuedResourceEventsSnapshot]].
  *
- * @param implicitOFFEventsMap
+ * @param implicitlyIssuedEventsMap
  *
  * @author Christos KK Loverdos <loverdos@gmail.com>
  */
-case class ImplicitOFFResourceEventsWorker(implicitOFFEventsMap: FullMutableResourceTypeMap) {
+case class ImplicitlyIssuedResourceEventsWorker(implicitlyIssuedEventsMap: FullMutableResourceTypeMap) {
 
   def toImmutableSnapshot(snapshotTime: Long) =
-    ImplicitOFFResourceEventsSnapshot(implicitOFFEventsMap.toMap, snapshotTime)
+    ImplicitlyIssuedResourceEventsSnapshot(implicitlyIssuedEventsMap.values.toList, snapshotTime)
 
   def findAndRemoveResourceEvent(resource: String, instanceId: String): Maybe[ResourceEvent] = {
-    findAndRemoveFromMap(implicitOFFEventsMap, (resource, instanceId))
+    findAndRemoveFromMap(implicitlyIssuedEventsMap, (resource, instanceId))
   }
 
-  def size = implicitOFFEventsMap.size
+  def size = implicitlyIssuedEventsMap.size
 
   def foreach[U](f: ResourceEvent => U): Unit = {
-    implicitOFFEventsMap.valuesIterator.foreach(f)
+    implicitlyIssuedEventsMap.valuesIterator.foreach(f)
   }
 }
\ No newline at end of file
index 80e6bd1..97b8b14 100644 (file)
@@ -87,14 +87,14 @@ case class UserState(
 
     /**
      * If this is a state for a full billing month, then keep here the implicit OFF
-     * resource events.
+     * resource events or any other whose cost policy demands an implicit event at the end of the billing period.
      *
      * The use case is this: A VM may have been started (ON state) before the end of the billing period
      * and ended (OFF state) after the beginning of the next billing period. In order to bill this, we must assume
      * an implicit OFF even right at the end of the billing period and an implicit ON event with the beginning of the
      * next billing period.
      */
-    implicitOFFsSnapshot: ImplicitOFFResourceEventsSnapshot,
+    implicitlyTerminatedSnapshot: ImplicitlyIssuedResourceEventsSnapshot,
 
     /**
      * So far computed wallet entries for the current billing month.
@@ -136,7 +136,7 @@ case class UserState(
       activeStateSnapshot.snapshotTime,
       creditsSnapshot.snapshotTime, agreementsSnapshot.snapshotTime, rolesSnapshot.snapshotTime,
       ownedResourcesSnapshot.snapshotTime,
-      implicitOFFsSnapshot.snapshotTime,
+      implicitlyTerminatedSnapshot.snapshotTime,
       latestResourceEventsSnapshot.snapshotTime
     )
   }
index dc6bc93..08cb0de 100644 (file)
@@ -57,7 +57,7 @@ class UserStateComputations extends Loggable {
       0L,
       false,
       null,
-      ImplicitOFFResourceEventsSnapshot(Map(), now),
+      ImplicitlyIssuedResourceEventsSnapshot(Map(), now),
       Nil, Nil,
       LatestResourceEventsSnapshot(Map(), now),
       0L, 0L,
@@ -77,7 +77,7 @@ class UserStateComputations extends Loggable {
         0L,
         false,
         null,
-        ImplicitOFFResourceEventsSnapshot(Map(), now),
+        ImplicitlyIssuedResourceEventsSnapshot(Map(), now),
         Nil, Nil,
         LatestResourceEventsSnapshot(Map(), now),
         0L, 0L,
@@ -208,9 +208,6 @@ class UserStateComputations extends Loggable {
 
     val previousBillingMonthData = billingMonthInfo.previousMonth
 
-    val previousBillingMonth = previousBillingMonthData.month
-    val yearOfPreviousBillingMonth = previousBillingMonthData.year
-
     val clog = ContextualLogger.fromOther(
       contextualLogger,
       logger,
@@ -255,7 +252,7 @@ class UserStateComputations extends Loggable {
         var _workingUserState = startingUserState
 
         // Prepare the implicit OFF resource events
-        val theImplicitOFFs = _workingUserState.implicitOFFsSnapshot.toMutableWorker
+        val theImplicitOFFs = _workingUserState.implicitlyTerminatedSnapshot.toMutableWorker
         clog.debug("theImplicitOFFs = %s", theImplicitOFFs)
 
         /**
index 786d880..f15aff9 100644 (file)
@@ -56,22 +56,22 @@ import com.ckkloverdos.maybe.{Failed, Just, Maybe}
  [DEBUG] 2012-02-13 12:54:53,663 main -     findUserStateAtEndOfBillingMonth(2011-11)         Computing full month billing
  [DEBUG] 2012-02-13 12:54:53,664 main -                 doFullMonthlyBilling(2011-11)         BEGIN
  [DEBUG] 2012-02-13 12:54:53,664 main -     findUserStateAtEndOfBillingMonth(2011-10)           BEGIN
- [DEBUG] 2012-02-13 12:54:53,667 main -     findUserStateAtEndOfBillingMonth(2011-10)             User did not exist before 2011-11-01 00:00:00.000. Returning UserState(Christos,0,0,false,null,ImplicitOFFResourceEventsSnapshot(Map(),0),List(),List(),LatestResourceEventsSnapshot(Map(),0),0,ActiveStateSnapshot(false,0),CreditSnapshot(0.0,0),AgreementSnapshot(List(Agreement(default,0,-1)),0),RolesSnapshot(List(),0),OwnedResourcesSnapshot(List(),0))
+ [DEBUG] 2012-02-13 12:54:53,667 main -     findUserStateAtEndOfBillingMonth(2011-10)             User did not exist before 2011-11-01 00:00:00.000. Returning UserState(Christos,0,0,false,null,ImplicitlyIssuedResourceEventsSnapshot(Map(),0),List(),List(),LatestResourceEventsSnapshot(Map(),0),0,ActiveStateSnapshot(false,0),CreditSnapshot(0.0,0),AgreementSnapshot(List(Agreement(default,0,-1)),0),RolesSnapshot(List(),0),OwnedResourcesSnapshot(List(),0))
  [DEBUG] 2012-02-13 12:54:53,668 main -     findUserStateAtEndOfBillingMonth(2011-10)           END
  [DEBUG] 2012-02-13 12:54:53,672 main -                 doFullMonthlyBilling(2011-11)           previousResourceEvents = LatestResourceEventsWorker(Map())
- [DEBUG] 2012-02-13 12:54:53,673 main -                 doFullMonthlyBilling(2011-11)           theImplicitOFFs = ImplicitOFFResourceEventsWorker(Map())
+ [DEBUG] 2012-02-13 12:54:53,673 main -                 doFullMonthlyBilling(2011-11)           theImplicitOFFs = ImplicitlyIssuedResourceEventsWorker(Map())
  [DEBUG] 2012-02-13 12:54:53,680 main -                 doFullMonthlyBilling(2011-11)           resourceEventStore = MemStore(Map(UserState -> 0, WalletEntry -> 0, ResourceEvent -> 5, PolicyEntry -> 0, UserEvent -> 0))
  [DEBUG] 2012-02-13 12:54:53,681 main -                 doFullMonthlyBilling(2011-11)           Found 0 resource events, starting processing...
  [DEBUG] 2012-02-13 12:54:53,683 main -                 doFullMonthlyBilling(2011-11)         END
  [DEBUG] 2012-02-13 12:54:53,683 main -     findUserStateAtEndOfBillingMonth(2011-11)       END
  [DEBUG] 2012-02-13 12:54:53,684 main -                 doFullMonthlyBilling(2011-12)       previousResourceEvents = LatestResourceEventsWorker(Map())
- [DEBUG] 2012-02-13 12:54:53,684 main -                 doFullMonthlyBilling(2011-12)       theImplicitOFFs = ImplicitOFFResourceEventsWorker(Map())
+ [DEBUG] 2012-02-13 12:54:53,684 main -                 doFullMonthlyBilling(2011-12)       theImplicitOFFs = ImplicitlyIssuedResourceEventsWorker(Map())
  [DEBUG] 2012-02-13 12:54:53,685 main -                 doFullMonthlyBilling(2011-12)       resourceEventStore = MemStore(Map(UserState -> 0, WalletEntry -> 0, ResourceEvent -> 5, PolicyEntry -> 0, UserEvent -> 0))
  [DEBUG] 2012-02-13 12:54:53,686 main -                 doFullMonthlyBilling(2011-12)       Found 0 resource events, starting processing...
  [DEBUG] 2012-02-13 12:54:53,686 main -                 doFullMonthlyBilling(2011-12)     END
  [DEBUG] 2012-02-13 12:54:53,687 main -     findUserStateAtEndOfBillingMonth(2011-12)   END
  [DEBUG] 2012-02-13 12:54:53,687 main -                 doFullMonthlyBilling(2012-01)   previousResourceEvents = LatestResourceEventsWorker(Map())
- [DEBUG] 2012-02-13 12:54:53,688 main -                 doFullMonthlyBilling(2012-01)   theImplicitOFFs = ImplicitOFFResourceEventsWorker(Map())
+ [DEBUG] 2012-02-13 12:54:53,688 main -                 doFullMonthlyBilling(2012-01)   theImplicitOFFs = ImplicitlyIssuedResourceEventsWorker(Map())
  [DEBUG] 2012-02-13 12:54:53,688 main -                 doFullMonthlyBilling(2012-01)   resourceEventStore = MemStore(Map(UserState -> 0, WalletEntry -> 0, ResourceEvent -> 5, PolicyEntry -> 0, UserEvent -> 0))
  [DEBUG] 2012-02-13 12:54:53,689 main -                 doFullMonthlyBilling(2012-01)   Found 4 resource events, starting processing...
  [DEBUG] 2012-02-13 12:54:53,690 main -                 doFullMonthlyBilling(2012-01)     Processing EVENT(2, [2012-01-01 03:00:00.000], 99.0 [MB/Hr], diskspace::pithos/diskspace/DISK.1, Map(), Christos, pithos)