Created
November 24, 2015 18:13
-
-
Save vitillo/52194c012f2030b06747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._ | |
import Scalaz._ | |
import org.json4s._ | |
import org.json4s.native.JsonMethods._ | |
case class Hang(stack: List[String], histogram: Histogram) | |
case class ThreadHangs(threadName: Option[String], hangs: Map[List[String], Histogram], activity: Histogram) { | |
def this(name: String, hangs: Option[List[Hang]], activity: Histogram) = | |
this(Some(name), hangs.getOrElse(List()).map(t => t.stack -> t.histogram).toMap, activity) | |
def average(): ThreadHangs = ThreadHangs(threadName, hangs.mapValues(_.average), activity.average) | |
} | |
case class Histogram(var values: Map[String, Double], sum: Int, count: Int) { | |
def this(values: Map[String, Double], sum: Int) = this(values, sum, 1) | |
def average(): Histogram = Histogram(values.mapValues(_/count), sum, count) | |
} | |
case class Payload(histograms: Map[String, Histogram], | |
keyedHistograms: Map[String, Map[String, Histogram]], | |
childPayload: ChildPayload, | |
threadHangStats: Map[String, ThreadHangs]) { | |
def average(): Payload = Payload(histograms.mapValues(_.average), | |
keyedHistograms.mapValues(_.mapValues(_.average)), | |
childPayload.average, | |
threadHangStats.mapValues(_.average)) | |
} | |
case class ChildPayload(histograms: Map[String, Histogram], | |
keyedHistograms: Map[String, Map[String, Histogram]]) { | |
def average() = ChildPayload(histograms.mapValues(_.average), keyedHistograms.mapValues(_.mapValues(_.average))) | |
} | |
object Payload{ | |
implicit private val formats = DefaultFormats | |
implicit def doubleMonoid: Monoid[Double] = new Monoid[Double] { | |
def zero = 0.0 | |
def append(a: Double, b: => Double) = a + b | |
} | |
implicit def threadHangStatsMonoid: Monoid[ThreadHangs] = new Monoid[ThreadHangs] { | |
def zero = ThreadHangs(None, Map(), histogramMonoid.zero) | |
def append(a: ThreadHangs, b: => ThreadHangs) = ThreadHangs(a.threadName, | |
a.hangs |+| b.hangs, | |
a.activity |+| b.activity) | |
} | |
implicit def childPayloadMonoid: Monoid[ChildPayload] = new Monoid[ChildPayload] { | |
def zero = ChildPayload(Map(), Map()) | |
def append(a: ChildPayload, b: => ChildPayload) = ChildPayload(a.histograms |+| b.histograms, | |
a.keyedHistograms |+| b.keyedHistograms) | |
} | |
implicit def payloadMonoid: Monoid[Payload] = new Monoid[Payload] { | |
def zero = Payload(Map(), Map(), ChildPayload(Map(), Map()), Map()) | |
def append(a: Payload, b: => Payload) = Payload(a.histograms |+| b.histograms, | |
a.keyedHistograms |+| b.keyedHistograms, | |
a.childPayload |+| b.childPayload, | |
a.threadHangStats |+| b.threadHangStats) | |
} | |
implicit def histogramMonoid: Monoid[Histogram] = new Monoid[Histogram] { | |
def zero = Histogram(Map(), 0, 0) | |
def append(a: Histogram, b: => Histogram) = Histogram(a.values |+| b.values, | |
a.sum |+| b.sum, | |
a.count |+| b.count) | |
} | |
def apply(raw: String): Payload = { | |
val parsed = parse(raw) | |
val h = (parsed \ "payload" \ "histograms").extract[Map[String, Histogram]] | |
val k = (parsed \ "payload" \ "keyedHistograms").extract[Map[String, Map[String, Histogram]]] | |
val c = (parsed \ "payload" \ "childPayloads").extract[List[ChildPayload]].sumr | |
val t = (parsed \ "payload" \ "threadHangStats").extract[List[ThreadHangs]].map(t => t.threadName.get -> t).toMap | |
Payload(h, k, c, t) | |
} | |
} | |
object PayloadSmoother{ | |
def main(args: Array[String]) { | |
val json = scala.io.Source.fromFile("test.json").mkString | |
val payload = Payload(json) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment