From: David ‘Bombe’ Roden Date: Mon, 18 Nov 2019 06:32:17 +0000 (+0100) Subject: 💄 Generalize rendering of histograms X-Git-Tag: v81^2~43 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1b83b0ae65741c6011ab36678b08ec87bc94c758;p=Sone.git 💄 Generalize rendering of histograms --- 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 236848d..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,12 +13,17 @@ 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) { - addHistogram(templateContext, "sone.parsing.duration", "soneParsingDuration") - addHistogram(templateContext, "sone.insert.duration", "soneInsertDuration") + 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 @@ -35,3 +40,14 @@ class MetricsPage @Inject constructor(webInterface: WebInterface, loaders: Loade } } + +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 { + "" + } diff --git a/src/main/resources/templates/metrics.html b/src/main/resources/templates/metrics.html index beaded8..e69ea19 100644 --- a/src/main/resources/templates/metrics.html +++ b/src/main/resources/templates/metrics.html @@ -21,32 +21,21 @@ - - <%= Page.Metrics.SoneInsertDuration.Title|l10n|html> - <% soneInsertDurationCount|html> - <% soneInsertDurationMin|duration scale=="μs"|html> - <% soneInsertDurationMax|duration scale=="μs"|html> - <% soneInsertDurationMean|duration scale=="μs"|html> - <% soneInsertDurationMedian|duration scale=="μs"|html> - <% soneInsertDurationPercentile75|duration scale=="μs"|html> - <% soneInsertDurationPercentile95|duration scale=="μs"|html> - <% soneInsertDurationPercentile98|duration scale=="μs"|html> - <% soneInsertDurationPercentile99|duration scale=="μs"|html> - <% soneInsertDurationPercentile999|duration scale=="μs"|html> - - - <%= Page.Metrics.SoneParsingDuration.Title|l10n|html> - <% soneParsingDurationCount|html> - <% soneParsingDurationMin|duration scale=="μs"|html> - <% soneParsingDurationMax|duration scale=="μs"|html> - <% soneParsingDurationMean|duration scale=="μs"|html> - <% soneParsingDurationMedian|duration scale=="μs"|html> - <% soneParsingDurationPercentile75|duration scale=="μs"|html> - <% soneParsingDurationPercentile95|duration scale=="μs"|html> - <% soneParsingDurationPercentile98|duration scale=="μs"|html> - <% soneParsingDurationPercentile99|duration scale=="μs"|html> - <% soneParsingDurationPercentile999|duration scale=="μs"|html> - + <%foreach histogramKeys histogramKey> + + <%= "Page.Metrics.<%= '<%histogramKey>I18n'|parse>.Title"|parse|l10n|html> + <% "<%histogramKey>Count"|parse|html> + <% "<%histogramKey>Min"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Max"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Mean"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Median"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Percentile75"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Percentile95"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Percentile98"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Percentile99"|parse|duration scale=="μs"|html> + <% "<%histogramKey>Percentile999"|parse|duration scale=="μs"|html> + + <%/foreach> diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/MetricsPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/MetricsPageTest.kt index 269aafe..5a5d588 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/MetricsPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/MetricsPageTest.kt @@ -62,17 +62,26 @@ class MetricsPageTest : WebPageTest() { } @Test - fun `metrics page lists stats about sone parsing durations`() { - createHistogram("sone.parsing.duration") + fun `metrics page auto-converts histogram name`() { + createHistogram("sone.random.duration") page.handleRequest(soneRequest, templateContext) - verifyHistogram("soneParsingDuration") + verifyHistogram("soneRandomDuration") } @Test - fun `metrics page lists stats about sone insert durations`() { - createHistogram("sone.insert.duration") + @Suppress("UNCHECKED_CAST") + fun `metrics page stores histogram keys in template`() { + createHistogram("sone.random.duration2") + createHistogram("sone.random.duration1") page.handleRequest(soneRequest, templateContext) - verifyHistogram("soneInsertDuration") + assertThat(templateContext["histogramKeys"] as Iterable, contains("soneRandomDuration1", "soneRandomDuration2")) + } + + @Test + fun `metrics page stores i18n names for histogram keys`() { + createHistogram("sone.random.duration1") + page.handleRequest(soneRequest, templateContext) + assertThat(templateContext["soneRandomDuration1I18n"] as String, equalTo("SoneRandomDuration1")) } @Test