Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scala for time series controller - how can I improve/speed this up?
def fillZeros(startDateStr : String, endDateStr : String, periodSeconds : Int, jsonKey : scala.collection.immutable.Map[String,Any], group : ListBuffer[collection.mutable.Map[String, Any]]): scala.collection.mutable.ListBuffer[scala.collection.mutable.Map[String,Any]] = {
var finalTimeSeries = ListBuffer[collection.mutable.Map[String, Any]]()
var startDate = MutableDateTime.parse(startDateStr)
val endDate = MutableDateTime.parse(endDateStr)
while(startDate.isBefore(endDate)) {
println(startDate.toString + " is before " + endDate.toString)
// If our results have an entry for this timestamp, append it to the final array
val searchResult = group.filter(x => x.get("eventTime") == Some(startDate.toString))
if (searchResult.length > 0) {
finalTimeSeries += collection.mutable.Map("eventTime" -> searchResult.head.get("eventTime"), "active" -> 1 );
}
else {
// Otherwise append an inactive entry
finalTimeSeries += collection.mutable.Map("eventTime" -> startDate.toString, "active" -> 0)
}
startDate.addSeconds(periodSeconds)
}
return finalTimeSeries
}
@GET
@Path("/beaconingSeries")
def getBeaconingSeries(@QueryParam("isoStartTime") isoStartTime : String,
@QueryParam("isoEndTime") isoEndTime : String,
@QueryParam("period") periodInt : Integer) = {
val buf = beaconingActivityDao.getBeaconingSeries(isoStartTime, isoEndTime, periodInt);
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
// Group by the unique fields
val groups = buf.groupBy(x=> Map[String, Any]("periodSeconds" -> x.get("periodSeconds"),
"cIp" -> x.get("cIp"),
"csHost" -> x.get("csHost"),
"requestMethod" -> x.get("requestMethod"),
"userAgent" -> x.get("userAgent")))
// Loop through the groups and build our time series with zeros filled in
var finalGroups = ListBuffer[Map[String, Any]]();
groups.foreach{
case (jsonKey,group) =>
finalGroups += Map("group" -> jsonKey, "timeSeries" -> fillZeros(isoStartTime, isoEndTime, periodInt, jsonKey, group))
}
val groupJson = mapper.writeValueAsString(finalGroups)
Response.ok(groupJson).build()
}
[
{
"group": {
"csHost": "ad.doubleclick.net",
"cIp": "45.14.1.148",
"userAgent": "\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"",
"requestMethod": "GET",
"periodSeconds": 3600
},
"timeSeries": [
{
"active": 0,
"eventTime": "2014-04-08T14:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T15:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T16:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T17:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T18:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T19:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T20:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T21:00:00.000Z"
},
{
"active": 1,
"eventTime": "2014-04-08T22:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T23:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T00:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T01:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T02:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T03:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T04:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T05:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T06:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T07:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T08:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T09:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T10:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T11:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T12:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T13:00:00.000Z"
}
]
},
{
"group": {
"csHost": "stats.surfaid.ihost.com",
"cIp": "45.0.0.95",
"userAgent": "\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02\"",
"requestMethod": "GET",
"periodSeconds": 3600
},
"timeSeries": [
{
"active": 0,
"eventTime": "2014-04-08T14:00:00.000Z"
},
{
"active": 1,
"eventTime": "2014-04-08T15:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T16:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T17:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T18:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T19:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T20:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T21:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T22:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-08T23:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T00:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T01:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T02:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T03:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T04:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T05:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T06:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T07:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T08:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T09:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T10:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T11:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T12:00:00.000Z"
},
{
"active": 0,
"eventTime": "2014-04-09T13:00:00.000Z"
}
]
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.