PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/dataset/app/models/DataSetEventLog.scala

https://github.com/delving/culture-hub
Scala | 52 lines | 36 code | 12 blank | 4 comment | 0 complexity | 6b78ba726b336c37bcab41e8a8179b6d MD5 | raw file
  1. package models
  2. import org.bson.types.ObjectId
  3. import com.mongodb.casbah.Imports._
  4. import com.novus.salat.dao.SalatDAO
  5. import models.HubMongoContext._
  6. /**
  7. *
  8. * @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
  9. */
  10. case class DataSetEventLog(_id: ObjectId = new ObjectId,
  11. orgId: String,
  12. spec: String,
  13. eventType: String,
  14. payload: Option[String],
  15. userName: Option[String],
  16. systemEvent: Boolean = false,
  17. transientEvent: Boolean = false)
  18. object DataSetEventLog extends MultiModel[DataSetEventLog, DataSetEventLogDAO] {
  19. val connectionName: String = "DataSetEventLog"
  20. def initIndexes(collection: MongoCollection) {
  21. addIndexes(collection, Seq(MongoDBObject("orgId" -> 1)))
  22. addIndexes(collection, Seq(MongoDBObject("transientEvent" -> 1)))
  23. }
  24. def initDAO(collection: MongoCollection, connection: MongoDB)(implicit configuration: OrganizationConfiguration): DataSetEventLogDAO = new DataSetEventLogDAO(collection)
  25. }
  26. class DataSetEventLogDAO(connection: MongoCollection) extends SalatDAO[DataSetEventLog, ObjectId](connection) {
  27. def findRecent = {
  28. val nonTransient = find(MongoDBObject("transientEvent" -> false)).limit(10).sort(MongoDBObject("_id" -> -1)).toList
  29. val transient = find(MongoDBObject("transientEvent" -> true)).limit(40).sort(MongoDBObject("_id" -> -1)).toList
  30. (nonTransient ++ transient).sortBy(_._id.getTime).reverse
  31. }
  32. def removeTransient() {
  33. val recent = find(MongoDBObject("transientEvent" -> true)).limit(10).sort(MongoDBObject("_id" -> -1)).toList.reverse.headOption
  34. recent.map { r =>
  35. remove(MongoDBObject("transientEvent" -> true) ++ ("_id" $lt r._id))
  36. }.getOrElse {
  37. remove(MongoDBObject("transientEvent" -> true))
  38. }
  39. }
  40. }