Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import scala.language.postfixOps
import scala.concurrent._
import scala.concurrent.duration._
import akka.pattern.pipe
import akka.actor._
import akka.cluster.routing._
/**
* Concept and some code lifted from
* https://github.com/jsuereth/intro-to-fp, thanks Josh!
*/
object StatApp extends App {
val system = ActorSystem("StatsApp")
import system.dispatcher
val publisher = system.actorOf(Props(new Publisher()))
/* Explicitly uses the default metrics selector */
val statistics = system.actorOf(Props(new Statistics(publisher))
.withRouter(
ClusterRouterConfig(AdaptiveLoadBalancingRouter(MixMetricsSelector),
ClusterRouterSettings(
totalInstances = 50, routeesPath = "/user/statsWorker",
allowLocalRoutees = true))), name = "statsRouter")
/* stubbed out: imagine this dynamically gets github projects */
val projects: Set[Project] = Set.empty
system.scheduler.schedule(Duration.Zero, 60 seconds) {
println("Sending project")
projects foreach (statistics ! _)
}
}
class Statistics(publisher: ActorRef) extends Actor {
import context.dispatcher
val api = new Api()
def receive = { case p: Project statistics(p) }
def statistics(project: Project): Unit = {
val pullRequests = api pullrequests project
val collaborators = api collaborators project
pullRequests zip collaborators map {
case (prs, cs) ProjectStatistics(project, cs, prs)
}
} pipeTo publisher
}
/* Stubbed out for brevity */
class Publisher extends Actor {
def receive = {
case event: ProjectStatistics
// converts and pushes to Restful consumers
}
}
/* Stubbed out for brevity */
class Api {
implicit val ctx = concurrent.ExecutionContext.Implicits.global
def pullrequests(proj: Project): Future[Set[PullRequest]] =
Future(Set.empty)
def collaborators(proj: Project): Future[Set[Collaborator]] =
Future(Set.empty)
}
case class Project(owner: String, name: String)
case class Collaborator(name: String)
case class PullRequest(project: Project, id: String,
contributor: String)
case class ProjectStatistics(project: Project,
collaborators: Set[Collaborator],
pullreqs: Set[PullRequest])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment