org.squeryl.logging.BarChartRenderer.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeryl_2.12.0-RC1 Show documentation
Show all versions of squeryl_2.12.0-RC1 Show documentation
A Scala ORM and DSL for talking with Databases using minimum verbosity and maximum type safety
The newest version!
package org.squeryl.logging
import xml.Unparsed
import java.io.{FileOutputStream, PrintStream}
import org.squeryl.InternalFieldMapper._
object BarChartRenderer {
class Stat(val title: String, val xAxisLabel: String, val lines: Iterable[StatLine], measureFromLike: StatLine => String) {
def queryLabelsJSArray =
lines.map(sl => "'" + sl.statement.definitionOrCallSite + "'").mkString("[",",","]")
def measuresJSArray =
lines.map(measureFromLike(_)).mkString("[",",","]")
}
def generateStatSummary(staticHtmlFile: java.io.File, n: Int) = {
val page =
BarChartRenderer.page(
new Stat(
"Top "+n+" statements with longest avg",
"avg time",
StatsSchema.topRankingStatements(n, Measure.AvgExecTime),
sl => sl.avgExecTime.toString),
new Stat(
"Top "+n+" most called statements",
"invocation count",
StatsSchema.topRankingStatements(n, Measure.InvocationCount),
sl => sl.invocationCount.toString),
new Stat(
"Top "+n+" statements incurring most cummulative execution time",
"cummulative execution time",
StatsSchema.topRankingStatements(n, Measure.CumulativeExecutionTime),
sl => sl.cumulativeExecutionTime.toString),
new Stat(
"Top "+n+" statements with highest avg row count",
"avg row count",
StatsSchema.topRankingStatements(n, Measure.AvgResultSetSize),
sl => sl.avgRowCount.toString)
)
val ps = new PrintStream(new FileOutputStream(staticHtmlFile))
ps.print(page)
ps.close
}
val drawFunc = """
function drawBarGraph(divId, chartTitle, statType, queryClasses, measure) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Z');
data.addColumn('number', statType);
data.addRows(queryClasses.length);
for (var j = 0; j < queryClasses.length; ++j) {
data.setValue(j, 0, queryClasses[j].toString());
data.setValue(j, 1, measure[j]);
}
var v = new google.visualization.BarChart(document.getElementById(divId))
v.draw(data,
{title: chartTitle,
width:600, height:400,
vAxis: {title: "Queries"},
hAxis: {title: statType}
}
);
}
"""
def funcCalls(stats: Seq[Stat]) = {
val sb = new java.lang.StringBuilder
var i = 0
for(s <- stats) {
i += 1
sb.append("drawBarGraph('chart")
sb.append(i)
sb.append("','")
sb.append(s.title)
sb.append("','")
sb.append(s.xAxisLabel)
sb.append("',")
sb.append(s.queryLabelsJSArray)
sb.append(",")
sb.append(s.measuresJSArray)
sb.append(");\n")
}
sb.toString
}
def page(stats: Stat*) = """
Performance profile of Squeryl queries
"""
}