Changed user event format
[aquarium] / src / main / scala / gr / grnet / aquarium / store / mongodb / MongoDBStore.scala
index 8e84d1f..5cb84d5 100644 (file)
@@ -38,18 +38,21 @@ package gr.grnet.aquarium.store.mongodb
 import gr.grnet.aquarium.util.Loggable
 import com.mongodb.util.JSON
 import gr.grnet.aquarium.user.UserState
+import gr.grnet.aquarium.user.UserState.{JsonNames => UserStateJsonNames}
 import gr.grnet.aquarium.util.displayableObjectInfo
 import gr.grnet.aquarium.util.json.JsonSupport
 import collection.mutable.ListBuffer
 import gr.grnet.aquarium.store._
 import gr.grnet.aquarium.logic.events.ResourceEvent.{JsonNames => ResourceJsonNames}
+import gr.grnet.aquarium.logic.events.UserEvent.{JsonNames => UserEventJsonNames}
 import gr.grnet.aquarium.logic.events.WalletEntry.{JsonNames => WalletJsonNames}
+import gr.grnet.aquarium.logic.events.PolicyEntry.{JsonNames => PolicyJsonNames}
 import java.util.Date
-import com.ckkloverdos.maybe.Maybe
-import gr.grnet.aquarium.logic.events.{UserEvent, WalletEntry, ResourceEvent, AquariumEvent}
-import com.mongodb._
 import gr.grnet.aquarium.logic.accounting.Policy
-import gr.grnet.aquarium.logic.accounting.dsl.DSLComplexResource
+import gr.grnet.aquarium.logic.events._
+import com.mongodb._
+import com.ckkloverdos.maybe.{NoVal, Maybe}
+import gr.grnet.aquarium.logic.accounting.dsl.{DSLResource, Timeslot, DSLPolicy, DSLComplexResource}
 
 /**
  * Mongodb implementation of the various aquarium stores.
@@ -62,14 +65,19 @@ class MongoDBStore(
     val database: String,
     val username: String,
     val password: String)
-  extends ResourceEventStore with UserStateStore
-  with WalletEntryStore with UserEventStore
+  extends ResourceEventStore
+  with UserStateStore
+  with WalletEntryStore
+  with UserEventStore
+  with PolicyStore
   with Loggable {
 
-  private[store] lazy val rcEvents      = getCollection(MongoDBStore.RESOURCE_EVENTS_COLLECTION)
-  private[store] lazy val userStates    = getCollection(MongoDBStore.USER_STATES_COLLECTION)
-  private[store] lazy val userEvents    = getCollection(MongoDBStore.USER_EVENTS_COLLECTION)
-  private[store] lazy val walletEntries = getCollection(MongoDBStore.WALLET_ENTRIES_COLLECTION)
+  private[store] lazy val resourceEvents = getCollection(MongoDBStore.RESOURCE_EVENTS_COLLECTION)
+  private[store] lazy val userStates     = getCollection(MongoDBStore.USER_STATES_COLLECTION)
+  private[store] lazy val userEvents     = getCollection(MongoDBStore.USER_EVENTS_COLLECTION)
+  private[store] lazy val walletEntries  = getCollection(MongoDBStore.WALLET_ENTRIES_COLLECTION)
+//  private[store] lazy val policies       = getCollection(MongoDBStore.POLICIES_COLLECTION)
+  private[store] lazy val policyEntries  = getCollection(MongoDBStore.POLICY_ENTRIES_COLLECTION)
 
   private[this] def getCollection(name: String): DBCollection = {
     val db = mongo.getDB(database)
@@ -94,16 +102,16 @@ class MongoDBStore(
 
   //+ResourceEventStore
   def storeResourceEvent(event: ResourceEvent): Maybe[RecordID] =
-    MongoDBStore.storeAquariumEvent(event, rcEvents)
+    MongoDBStore.storeAquariumEvent(event, resourceEvents)
 
   def findResourceEventById(id: String): Maybe[ResourceEvent] =
-    MongoDBStore.findById(id, rcEvents, MongoDBStore.dbObjectToResourceEvent)
+    MongoDBStore.findById(id, resourceEvents, MongoDBStore.dbObjectToResourceEvent)
 
   def findResourceEventsByUserId(userId: String)
                                 (sortWith: Option[(ResourceEvent, ResourceEvent) => Boolean]): List[ResourceEvent] = {
     val query = new BasicDBObject(ResourceJsonNames.userId, userId)
 
-    MongoDBStore.runQuery(query, rcEvents)(MongoDBStore.dbObjectToResourceEvent)(sortWith)
+    MongoDBStore.runQuery(query, resourceEvents)(MongoDBStore.dbObjectToResourceEvent)(sortWith)
   }
 
   def findResourceEventsByUserIdAfterTimestamp(userId: String, timestamp: Long): List[ResourceEvent] = {
@@ -113,14 +121,14 @@ class MongoDBStore(
     
     val sort = new BasicDBObject(ResourceJsonNames.occurredMillis, 1)
 
-    val cursor = rcEvents.find(query).sort(sort)
+    val cursor = resourceEvents.find(query).sort(sort)
 
     try {
       val buffer = new scala.collection.mutable.ListBuffer[ResourceEvent]
       while(cursor.hasNext) {
         buffer += MongoDBStore.dbObjectToResourceEvent(cursor.next())
       }
-      buffer.toList
+      buffer.toList.sortWith(_sortByTimestampAsc)
     } finally {
       cursor.close()
     }
@@ -130,42 +138,68 @@ class MongoDBStore(
                                instid: Option[String], upTo: Long) : List[ResourceEvent] = {
     val query = new BasicDBObject()
     query.put(ResourceJsonNames.userId, userId)
-    query.put(ResourceJsonNames.occurredMillis, new BasicDBObject("$lte", upTo))
+    query.put(ResourceJsonNames.occurredMillis, new BasicDBObject("$lt", upTo))
     query.put(ResourceJsonNames.resource, resName)
 
     instid match {
       case Some(id) =>
         Policy.policy.findResource(resName) match {
           case Some(y) => query.put(ResourceJsonNames.details,
-            new BasicDBObject(y.asInstanceOf[DSLComplexResource].descriminatorField, instid.get))
+            new BasicDBObject(y.descriminatorField, instid.get))
           case None =>
         }
       case None =>
     }
 
     val sort = new BasicDBObject(ResourceJsonNames.occurredMillis, 1)
-    val cursor = rcEvents.find(query).sort(sort)
+    val cursor = resourceEvents.find(query).sort(sort)
 
     try {
       val buffer = new scala.collection.mutable.ListBuffer[ResourceEvent]
       while(cursor.hasNext) {
         buffer += MongoDBStore.dbObjectToResourceEvent(cursor.next())
       }
-      buffer.toList
+      buffer.toList.sortWith(_sortByTimestampAsc)
     } finally {
       cursor.close()
     }
+  }
+
+  def findResourceEventsForReceivedPeriod(userId: String, startTimeMillis: Long, stopTimeMillis: Long): List[ResourceEvent] = {
+    val query = new BasicDBObject()
+    query.put(ResourceJsonNames.userId, userId)
+    query.put(ResourceJsonNames.receivedMillis, new BasicDBObject("$gte", startTimeMillis))
+    query.put(ResourceJsonNames.receivedMillis, new BasicDBObject("$lte", stopTimeMillis))
+
+    // Sort them by increasing order for occurred time
+    val orderBy = new BasicDBObject(ResourceJsonNames.occurredMillis, 1)
+
+    MongoDBStore.runQuery[ResourceEvent](query, resourceEvents, orderBy)(MongoDBStore.dbObjectToResourceEvent)(None)
+  }
+  
+  def countOutOfSyncEventsForBillingPeriod(userId: String, startMillis: Long, stopMillis: Long): Maybe[Long] = {
+    Maybe {
+      // FIXME: Implement
+      0L
+    }
+  }
 
+  def findAllRelevantResourceEventsForBillingPeriod(userId: String,
+                                                    startMillis: Long,
+                                                    stopMillis: Long): List[ResourceEvent] = {
+    // FIXME: Implement
+    Nil
   }
   //-ResourceEventStore
 
-  //+UserStateStore
-  def storeUserState(userState: UserState): Maybe[RecordID] =
+  //+ UserStateStore
+  def storeUserState(userState: UserState): Maybe[RecordID] = {
     MongoDBStore.storeUserState(userState, userStates)
+  }
 
   def findUserStateByUserId(userId: String): Maybe[UserState] = {
     Maybe {
-      val query = new BasicDBObject(ResourceJsonNames.userId, userId)
+      val query = new BasicDBObject(UserStateJsonNames.userId, userId)
       val cursor = userStates find query
 
       try {
@@ -179,11 +213,17 @@ class MongoDBStore(
     }
   }
 
+  def findLatestUserStateForEndOfBillingMonth(userId: String,
+                                              yearOfBillingMonth: Int,
+                                              billingMonth: Int): Maybe[UserState] = {
+    NoVal // FIXME: implement
+  }
+
   def deleteUserState(userId: String) = {
-    val query = new BasicDBObject(ResourceJsonNames.userId, userId)
+    val query = new BasicDBObject(UserStateJsonNames.userId, userId)
     userStates.findAndRemove(query)
   }
-  //-UserStateStore
+  //- UserStateStore
 
   //+WalletEntryStore
   def storeWalletEntry(entry: WalletEntry): Maybe[RecordID] =
@@ -200,16 +240,24 @@ class MongoDBStore(
   def findUserWalletEntriesFromTo(userId: String, from: Date, to: Date) : List[WalletEntry] = {
     val q = new BasicDBObject()
     // TODO: Is this the correct way for an AND query?
-    q.put(ResourceJsonNames.occurredMillis, new BasicDBObject("$gt", from.getTime))
-    q.put(ResourceJsonNames.occurredMillis, new BasicDBObject("$lt", to.getTime))
-    q.put(ResourceJsonNames.userId, userId)
+    q.put(WalletJsonNames.occurredMillis, new BasicDBObject("$gt", from.getTime))
+    q.put(WalletJsonNames.occurredMillis, new BasicDBObject("$lt", to.getTime))
+    q.put(WalletJsonNames.userId, userId)
+
+    MongoDBStore.runQuery[WalletEntry](q, walletEntries)(MongoDBStore.dbObjectToWalletEntry)(Some(_sortByTimestampAsc))
+  }
+
+  def findWalletEntriesAfter(userId: String, from: Date) : List[WalletEntry] = {
+    val q = new BasicDBObject()
+    q.put(WalletJsonNames.occurredMillis, new BasicDBObject("$gt", from.getTime))
+    q.put(WalletJsonNames.userId, userId)
 
     MongoDBStore.runQuery[WalletEntry](q, walletEntries)(MongoDBStore.dbObjectToWalletEntry)(Some(_sortByTimestampAsc))
   }
 
   def findLatestUserWalletEntries(userId: String) = {
     Maybe {
-      val orderBy = new BasicDBObject(ResourceJsonNames.occurredMillis, -1) // -1 is descending order
+      val orderBy = new BasicDBObject(WalletJsonNames.occurredMillis, -1) // -1 is descending order
       val cursor = walletEntries.find().sort(orderBy)
 
       try {
@@ -259,21 +307,47 @@ class MongoDBStore(
 
   //+UserEventStore
   def storeUserEvent(event: UserEvent): Maybe[RecordID] =
-    MongoDBStore.storeAny[UserEvent](event, userEvents, ResourceJsonNames.userId,
-      _.userId, MongoDBStore.jsonSupportToDBObject)
+    MongoDBStore.storeAny[UserEvent](event, userEvents, UserEventJsonNames.userId,
+      _.userID, MongoDBStore.jsonSupportToDBObject)
 
 
   def findUserEventById(id: String): Maybe[UserEvent] =
     MongoDBStore.findById[UserEvent](id, userEvents, MongoDBStore.dbObjectToUserEvent)
 
   def findUserEventsByUserId(userId: String): List[UserEvent] = {
-    val query = new BasicDBObject(ResourceJsonNames.userId, userId)
+    val query = new BasicDBObject(UserEventJsonNames.userId, userId)
     MongoDBStore.runQuery(query, userEvents)(MongoDBStore.dbObjectToUserEvent)(Some(_sortByTimestampAsc))
   }
   //-UserEventStore
+
+  //+PolicyStore
+  def loadPolicyEntriesAfter(after: Long): List[PolicyEntry] = {
+    val query = new BasicDBObject(PolicyEntry.JsonNames.validFrom,
+      new BasicDBObject("$gt", after))
+    MongoDBStore.runQuery(query, policyEntries)(MongoDBStore.dbObjectToPolicyEntry)(Some(_sortByTimestampAsc))
+  }
+
+  def storePolicyEntry(policy: PolicyEntry): Maybe[RecordID] = MongoDBStore.storePolicyEntry(policy, policyEntries)
+
+
+  def updatePolicyEntry(policy: PolicyEntry) = {
+    //Find the entry
+    val query = new BasicDBObject(PolicyEntry.JsonNames.id, policy.id)
+    val policyObject = MongoDBStore.jsonSupportToDBObject(policy)
+    policyEntries.update(query, policyObject, true, false)
+  }
+  
+  def findPolicyEntry(id: String) =
+    MongoDBStore.findById[PolicyEntry](id, policyEntries, MongoDBStore.dbObjectToPolicyEntry)
+
+  //-PolicyStore
 }
 
 object MongoDBStore {
+  object JsonNames {
+    final val _id = "_id"
+  }
+
   /**
    * Collection holding the [[gr.grnet.aquarium.logic.events.ResourceEvent]]s.
    *
@@ -295,7 +369,6 @@ object MongoDBStore {
    */
   final val USER_EVENTS_COLLECTION = "userevents"
 
-
   /**
    * Collection holding [[gr.grnet.aquarium.logic.events.WalletEntry]].
    *
@@ -303,6 +376,16 @@ object MongoDBStore {
    */
   final val WALLET_ENTRIES_COLLECTION = "wallets"
 
+  /**
+   * Collection holding [[gr.grnet.aquarium.logic.accounting.dsl.DSLPolicy]].
+   */
+//  final val POLICIES_COLLECTION = "policies"
+
+  /**
+   * Collection holding [[gr.grnet.aquarium.logic.events.PolicyEntry]].
+   */
+  final val POLICY_ENTRIES_COLLECTION = "policyEntries"
+
   /* TODO: Some of the following methods rely on JSON (de-)serialization).
   * A method based on proper object serialization would be much faster.
   */
@@ -322,6 +405,10 @@ object MongoDBStore {
     UserEvent.fromJson(JSON.serialize(dbObj))
   }
 
+  def dbObjectToPolicyEntry(dbObj: DBObject): PolicyEntry = {
+    PolicyEntry.fromJson(JSON.serialize(dbObj))
+  }
+
   def findById[A >: Null <: AquariumEvent](id: String, collection: DBCollection, deserializer: (DBObject) => A) : Maybe[A] = Maybe {
     val query = new BasicDBObject(ResourceJsonNames.id, id)
     val cursor = collection find query
@@ -372,30 +459,35 @@ object MongoDBStore {
   def storeUserState(userState: UserState, collection: DBCollection): Maybe[RecordID] = {
     storeAny[UserState](userState, collection, ResourceJsonNames.userId, _.userId, MongoDBStore.jsonSupportToDBObject)
   }
+  
+  def storePolicyEntry(policyEntry: PolicyEntry, collection: DBCollection): Maybe[RecordID] = {
+    storeAny[PolicyEntry](policyEntry, collection, PolicyJsonNames.id, _.id, MongoDBStore.jsonSupportToDBObject)
+  }
 
   def storeAny[A](any: A,
                   collection: DBCollection,
                   idName: String,
                   idValueProvider: (A) => String,
-                  serializer: (A) => DBObject) : Maybe[RecordID] = Maybe {
-    // Store
-    val dbObj = serializer apply any
-    val writeResult = collection insert dbObj
-    writeResult.getLastError().throwOnError()
-
-    // Get back to retrieve unique id
-    val cursor = collection.find(new BasicDBObject(idName, idValueProvider(any)))
+                  serializer: (A) => DBObject) : Maybe[RecordID] = {
+    import com.ckkloverdos.maybe.effect
 
-    try {
-      // TODO: better way to get _id?
-      if(cursor.hasNext)
-        RecordID(cursor.next().get(ResourceJsonNames._id).toString)
-      else
-        throw new StoreException("Could not store %s to %s".format(any, collection))
-    } finally {
-      cursor.close()
+    Maybe {
+      val dbObj = serializer apply any
+      val writeResult = collection insert dbObj
+      writeResult.getLastError().throwOnError()
+
+      // Get back to retrieve unique id
+      val cursor = collection.find(new BasicDBObject(idName, idValueProvider(any)))
+      cursor
+    } flatMap { cursor ⇒
+      effect {
+        if(cursor.hasNext)
+          RecordID(cursor.next().get(JsonNames._id).toString)
+        else
+          throw new StoreException("Could not store %s to %s".format(any, collection))
+      } {} { cursor.close() }
     }
- }
+  }
 
   def jsonSupportToDBObject(any: JsonSupport): DBObject = {
     JSON.parse(any.toJson) match {