Skip to content

Instantly share code, notes, and snippets.

@rjurney
Created August 23, 2014 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjurney/e0fa39bc26e5a68c6b1b to your computer and use it in GitHub Desktop.
Save rjurney/e0fa39bc26e5a68c6b1b to your computer and use it in GitHub Desktop.
Fun with Scala
@GET
@Path("/groupHttpTimeSeries")
def getTimeSeriesForAllGroups(@QueryParam("startTime") startDateStr : String,
@QueryParam("endTime") endDateStr : String,
@QueryParam("type") typeStr : String,
@QueryParam("period") periodInt : Int) = {
val buf = httpTimeSeriesDao.getTimeSeriesForAllGroups(startDateStr, endDateStr, typeStr, periodInt) // Group by the unique fields
val timeEntries = buf.map(x => scala.collection.mutable.Map[String,Any](
"type" -> x.get("type"),
"groupField" -> x.get("groupField"),
"periodSeconds" -> x.get("periodSeconds"),
"dateTime" -> x.get("dateTime"),
"bitsInPerSecond" -> scala.math.BigDecimal(x.get("bitsInPerSecond").get.asInstanceOf[Double]).setScale(4, scala.math.BigDecimal.RoundingMode.HALF_UP).toDouble,
"bitsOutPerSecond" ->scala.math.BigDecimal(x.get("bitsOutPerSecond").get.asInstanceOf[Double]).setScale(4, scala.math.BigDecimal.RoundingMode.HALF_UP).toDouble,
"connectionsPerSecond" -> scala.math.BigDecimal(x.get("connectionsPerSecond").get.asInstanceOf[Double]).setScale(4, scala.math.BigDecimal.RoundingMode.HALF_UP).toDouble
))
val groups = timeEntries.groupBy(x => scala.collection.immutable.Map[String, Option[Any]](
"type" -> x.get("type"),
"groupField" -> x.get("groupField"),
"periodSeconds" -> x.get("periodSeconds")
))
var finalGroups = ListBuffer[Map[String, Any]]()
groups.foreach {
case(jsonKey,group) =>
finalGroups += Map("count" -> timeEntries.length, "group" -> jsonKey, "timeSeries" -> group.map(x => Option[Any]( scala.collection.mutable.Map[String,Any](
"dateTime" -> x.get("dateTime"),
"bitsInPerSecond" -> x.get("bitsInPerSecond"),
"bitsOutPerSecond" -> x.get("bitsOutPerSecond"),
"connectionsPerSecond" -> x.get("connectionsPerSecond")
))))
}
val a = mapper.writeValueAsString(finalGroups)
Response.ok(a).build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment