/app/controllers/dos/ui/Logs.scala

https://github.com/delving/dos · Scala · 56 lines · 31 code · 6 blank · 19 comment · 8 complexity · 4a9deddc79a93099637ea8dfe618207a MD5 · raw file

  1. /*
  2. * Copyright 2011 Delving B.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package controllers.dos.ui
  17. import models.dos.Log
  18. import extensions.Extensions
  19. import play.mvc.Controller
  20. import org.bson.types.ObjectId
  21. import play.mvc.results.Result
  22. import com.mongodb.casbah.commons.MongoDBObject
  23. import com.novus.salat.dao.SalatMongoCursor
  24. /**
  25. *
  26. * @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
  27. */
  28. object Logs extends Controller with Extensions {
  29. def list(taskId: ObjectId, lastCount: Int): Result = {
  30. val cursor: SalatMongoCursor[Log] = Log.find(MongoDBObject("task_id" -> taskId)).sort(MongoDBObject("date" -> 1))
  31. val (logs, skipped) = if(lastCount != null && lastCount > 0) {
  32. if(cursor.count - lastCount > 100) {
  33. (cursor.skip(cursor.count - 100), true)
  34. } else {
  35. (cursor.skip(lastCount + 1), false)
  36. }
  37. } else {
  38. if(cursor.count > 100) {
  39. (cursor.skip(cursor.count - 100), true)
  40. } else {
  41. (cursor, false)
  42. }
  43. }
  44. Json(Map("logs" -> logs, "skipped" -> skipped))
  45. }
  46. def view(taskId: ObjectId): Result = {
  47. val cursor: SalatMongoCursor[Log] = Log.find(MongoDBObject("task_id" -> taskId)).sort(MongoDBObject("date" -> 1))
  48. Text(cursor.map(log => log.date + "\t" + log.level.name.toUpperCase + "\t" + log.node + "\t" + log.message).mkString("\n"))
  49. }
  50. }