PageRenderTime 77ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/net.modelbased.sensapp.service.rrd/src/main/scala/net/modelbased/sensapp/service/rrd/data/RRDBaseRegistry.scala

https://github.com/SINTEF-9012/sensapp
Scala | 177 lines | 101 code | 19 blank | 57 comment | 6 complexity | eaa1694f05ff3442e597045a83824549 MD5 | raw file
  1. /**
  2. * This file is part of SensApp [ http://sensapp.modelbased.net ]
  3. *
  4. * Copyright (C) 2011- SINTEF ICT
  5. * Contact: SINTEF ICT <nicolas.ferry@sintef.no>
  6. *
  7. * Module: net.modelbased.sensapp
  8. *
  9. * SensApp is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * SensApp is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General
  20. * Public License along with SensApp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. */
  23. package net.modelbased.sensapp.service.rrd.data
  24. /**
  25. * This file is part of SensApp [ http://sensapp.modelbased.net ]
  26. *
  27. * Copyright (C) 2012- SINTEF ICT
  28. * Contact: Sebastien Mosser <sebastien.mosser@sintef.no>
  29. *
  30. * Module: net.modelbased.sensapp.service.rrd
  31. *
  32. * SensApp is free software: you can redistribute it and/or modify
  33. * it under the terms of the GNU Lesser General Public License as
  34. * published by the Free Software Foundation, either version 3 of
  35. * the License, or (at your option) any later version.
  36. *
  37. * SensApp is distributed in the hope that it will be useful, but
  38. * WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU Lesser General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU Lesser General
  43. * Public License along with SensApp. If not, see
  44. * <http://www.gnu.org/licenses/>.
  45. */
  46. import cc.spray.json._
  47. import net.modelbased.sensapp.library.datastore._
  48. import java.util.jar.{JarEntry, JarFile}
  49. import java.io._
  50. import java.lang.StringBuilder
  51. import java.text.SimpleDateFormat
  52. import org.rrd4j.core.{Util, RrdDefTemplate, RrdMongoDBBackendFactory, RrdDb}
  53. import net.modelbased.sensapp.library.senml.MeasurementOrParameter
  54. import org.rrd4j.graph._
  55. import java.awt.{Color, Cursor}
  56. import java.awt.image.BufferedImage
  57. //import org.specs2.internal.scalaz.Validation
  58. import java.net.{URLConnection, URLDecoder, URL}
  59. import org.xml.sax.XMLReader
  60. import com.mongodb._
  61. import org.parboiled.support.Var
  62. import com.mongodb.casbah.Imports._
  63. import java.util.ArrayList
  64. import scala.collection.JavaConversions._
  65. /**
  66. * Persistence layer associated to the RRDBase class
  67. *
  68. * @author Sebastien Mosser
  69. */
  70. class RRDBaseRegistry {
  71. val rrd4jDatabaseName = "sensapp_db"
  72. val rrd4jCollectionName = "rrd.databases"
  73. private lazy val rrd4jcollection = {
  74. val conn = MongoConnection()
  75. val db = conn.getDB(rrd4jDatabaseName)
  76. val col = db.getCollection(rrd4jCollectionName)
  77. col
  78. }
  79. private lazy val rrd4jfactory = new RrdMongoDBBackendFactory(rrd4jcollection);
  80. def listRRD4JBases() : ArrayList[String] = {
  81. // Had to query the DB . No method in the RRD4J APIs.
  82. val result = new ArrayList[String]()
  83. val q = MongoDBObject.empty
  84. val fields = MongoDBObject("path" -> 1)
  85. val res = rrd4jcollection.find(q, fields)
  86. res.toArray.foreach{o =>
  87. result.add(o.get("path").toString)
  88. }
  89. return result
  90. }
  91. def deleteRRD4JBase(path : String) = {
  92. // Had to query the DB . No method in the RRD4J APIs.
  93. val query = MongoDBObject("path" -> path)
  94. val rrdObject = rrd4jcollection.findOne(query);
  95. if (rrdObject != null) {
  96. rrd4jcollection.remove(rrdObject)
  97. }
  98. }
  99. def createRRD4JBase(path : String, template_url : String) = {
  100. // TODO: catch the numerous exceptions which could be raised here
  101. val xml = IOUtils.sendGetRequest(template_url, null);
  102. if (xml != null) {
  103. val template = new RrdDefTemplate(xml)
  104. template.setVariable("PATH", path);
  105. val rrddef = template.getRrdDef
  106. rrddef.setPath(path)
  107. val db = new RrdDb(rrddef, rrd4jfactory)
  108. db.close
  109. }
  110. }
  111. def importRRD4JBase(path : String, data_url : String) = {
  112. // TODO: catch the numerous exceptions which could be raised here
  113. val xmlfile = IOUtils.downloadTmpFile(data_url, null)
  114. if (xmlfile != null) {
  115. val db = new RrdDb(path, xmlfile.getAbsolutePath, rrd4jfactory)
  116. db.close
  117. xmlfile.delete
  118. }
  119. }
  120. def getRRD4JBase(path : String, ro:Boolean) : RrdDb = {
  121. val result = new RrdDb(path, ro, rrd4jfactory)
  122. return result
  123. }
  124. def getgetRRD4JBaseDescription(db : RrdDb) : String = {
  125. val result = new StringBuilder();
  126. val dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  127. result.append("Sensapp RRD Database " + db.getPath + " (step = " +db.getRrdDef.getStep+ ")\n")
  128. result.append("Database Estimated Size: " + db.getRrdDef.getEstimatedSize + "\n")
  129. result.append("Number of Data Source: " + db.getDsCount + " [ ")
  130. db.getDsNames.foreach{ n => result.append(n + " ") }
  131. result.append("]\n")
  132. result.append("Number of Archives: " + db.getArcCount + "\n")
  133. result.append("Latest Update: " + dateFormat.format(Util.getCalendar(db.getLastArchiveUpdateTime).getTime) + "\n")
  134. result.append("Latest Values: [ ")
  135. db.getLastDatasourceValues.foreach{ v => result.append(v + " ") }
  136. result.append("]\n")
  137. result.toString
  138. }
  139. def createDefaultGraph(path : String, source: String, start : String, end : String, resolution : String, function : String) : BufferedImage = {
  140. val db = getRRD4JBase(path, true)
  141. val query = RRDRequest(function, start, end, resolution)
  142. val fr = db.createFetchRequest(query.getFunction, query.getStart, query.getEnd, query.getResolution)
  143. val gd = new RrdGraphDef()
  144. gd.comment("Generated by Sensapp")
  145. gd.setWidth(600);
  146. gd.setHeight(340);
  147. gd.setTimeSpan(query.getStart, query.getEnd);
  148. //gd.setTextAntiAliasing(true)
  149. //gd.setAltAutoscale(true)
  150. //gd.setFilename("-") // in memory
  151. gd.datasource(source, fr.fetchData())
  152. gd.line(source, Color.red)
  153. val g = new RrdGraph(gd)
  154. val img = new BufferedImage(g.getRrdGraphInfo.getWidth,g.getRrdGraphInfo.getHeight,BufferedImage.TYPE_INT_RGB)
  155. g.render(img.getGraphics)
  156. img
  157. }
  158. }