Skip to content

Instantly share code, notes, and snippets.

@helena
Last active December 13, 2015 21:39
Show Gist options
  • Save helena/4979130 to your computer and use it in GitHub Desktop.
Save helena/4979130 to your computer and use it in GitHub Desktop.
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