X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FMetricsPage.kt;h=655c91282ea5e5ece3586485f720b5e8f03999c9;hb=1b83b0ae65741c6011ab36678b08ec87bc94c758;hp=041e75535f30b89764d64240a5c1ee10931140dc;hpb=6562eedae6d6b25ecfb2f662a827db85f7026d50;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/MetricsPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/MetricsPage.kt index 041e755..655c912 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/MetricsPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/MetricsPage.kt @@ -13,18 +13,41 @@ import javax.inject.* class MetricsPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, private val metricsRegistry: MetricRegistry) : SoneTemplatePage(webInterface, loaders, templateRenderer, "Page.Metrics.Title") { override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { - metricsRegistry.histogram("sone.parsing.duration").snapshot.also { snapshot -> - templateContext["soneParsingDurationCount"] = snapshot.size() - templateContext["soneParsingDurationMin"] = snapshot.min - templateContext["soneParsingDurationMax"] = snapshot.max - templateContext["soneParsingDurationMedian"] = snapshot.median - templateContext["soneParsingDurationMean"] = snapshot.mean - templateContext["soneParsingDurationPercentile75"] = snapshot.get75thPercentile() - templateContext["soneParsingDurationPercentile95"] = snapshot.get95thPercentile() - templateContext["soneParsingDurationPercentile98"] = snapshot.get98thPercentile() - templateContext["soneParsingDurationPercentile99"] = snapshot.get99thPercentile() - templateContext["soneParsingDurationPercentile999"] = snapshot.get999thPercentile() + metricsRegistry.histograms + .mapKeys { it.key to it.key.toI18n() } + .onEach { addHistogram(templateContext, it.key.first, it.key.second) } + .keys + .map(Pair<*, String>::second) + .let { templateContext["histogramKeys"] = it } + } + + private fun addHistogram(templateContext: TemplateContext, metricName: String, variablePrefix: String) { + metricsRegistry.histogram(metricName).also { histogram -> + templateContext["${variablePrefix}I18n"] = variablePrefix.capitalizeFirst() + templateContext["${variablePrefix}Count"] = histogram.count + histogram.snapshot.also { snapshot -> + templateContext["${variablePrefix}Min"] = snapshot.min + templateContext["${variablePrefix}Max"] = snapshot.max + templateContext["${variablePrefix}Median"] = snapshot.median + templateContext["${variablePrefix}Mean"] = snapshot.mean + templateContext["${variablePrefix}Percentile75"] = snapshot.get75thPercentile() + templateContext["${variablePrefix}Percentile95"] = snapshot.get95thPercentile() + templateContext["${variablePrefix}Percentile98"] = snapshot.get98thPercentile() + templateContext["${variablePrefix}Percentile99"] = snapshot.get99thPercentile() + templateContext["${variablePrefix}Percentile999"] = snapshot.get999thPercentile() + } } } } + +private fun String.toI18n() = split(".") + .mapIndexed { index, s -> if (index > 0) s.capitalizeFirst() else s } + .joinToString("") + +private fun String.capitalizeFirst() = + get(0).toUpperCase() + if (length > 0) { + substring(1) + } else { + "" + }