/
Backend.scala
61 lines (46 loc) · 2.22 KB
/
Backend.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package lib
import java.util.concurrent.TimeUnit
import com.gu.openplatform.contentapi.model.Content
import org.joda.time.{Duration, DateTime}
import akka.actor._
import akka.util.duration._
import akka.dispatch.{Await, Future}
import akka.util.Timeout
import concurrent.ops
import ops._
object Backend {
val system = ActorSystem("liveDashboard")
val listener = system.actorOf(Props[ClickStreamActor], name = "clickStreamListener")
val calculator = system.actorOf(Props[Calculator], name = "calculator")
val searchTerms = system.actorOf(Props[SearchTermActor], name = "searchTermProcessor")
val latestContent = system.actorOf(Props[LatestContentActor], name = "latestContent")
val mqReader = new MqReader(listener :: searchTerms :: Nil)
def start() {
system.scheduler.schedule(1 minute, 1 minute, listener, TruncateClickStream())
system.scheduler.schedule(5 seconds, 5 seconds, listener, SendClickStreamTo(calculator))
system.scheduler.schedule(5 seconds, 10 seconds, latestContent, LatestContentActor.Refresh())
spawn {
mqReader.start()
}
listener ! Event("1.1.1.1", new DateTime(), "/dummy", "GET", 200, Some("http://www.google.com"), "my agent", "geo!")
searchTerms ! Event("1.1.1.1", new DateTime(), "/search?q=dummy&a=b&c=d%2Fj", "GET", 200, Some("http://www.google.com"), "my agent", "geo!")
}
def stop() {
mqReader.stop()
system.shutdown()
}
// So this is a bad way to do this, should use akka Agents instead (which can read
// without sending a message.)
implicit val timeout = Timeout(5 seconds)
def currentStats = Await.result( (calculator ? GetStats()).mapTo[(List[HitReport], ListsOfStuff)], 5 seconds)
def currentLists = currentStats._2
def currentHits = currentStats._1
def liveSearchTermsFuture = (searchTerms ? GetSearchTerms()).mapTo[List[GuSearchTerm]]
def liveSearchTerms = Await.result(liveSearchTermsFuture, timeout.duration)
def last24hoursOfContentFuture = (latestContent ? LatestContentActor.Get()).mapTo[List[Content]]
def last24hoursOfContent = Await.result(last24hoursOfContentFuture, timeout.duration)
def minutesOfData = {
val currentData = currentLists
new Duration(currentData.firstUpdated, currentData.lastUpdated).getStandardMinutes
}
}