💄 Generalize rendering of histograms
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 18 Nov 2019 06:32:17 +0000 (07:32 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 18 Nov 2019 06:45:02 +0000 (07:45 +0100)
src/main/kotlin/net/pterodactylus/sone/web/pages/MetricsPage.kt
src/main/resources/templates/metrics.html
src/test/kotlin/net/pterodactylus/sone/web/pages/MetricsPageTest.kt

index 236848d..655c912 100644 (file)
@@ -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 {
+                       ""
+               }
index beaded8..e69ea19 100644 (file)
                        </tr>
                </thead>
                <tbody>
-                       <tr>
-                               <td><%= Page.Metrics.SoneInsertDuration.Title|l10n|html></td>
-                               <td class="numeric"><% soneInsertDurationCount|html></td>
-                               <td class="numeric"><% soneInsertDurationMin|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationMax|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationMean|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationMedian|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationPercentile75|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationPercentile95|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationPercentile98|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationPercentile99|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneInsertDurationPercentile999|duration scale=="μs"|html></td>
-                       </tr>
-                       <tr>
-                               <td><%= Page.Metrics.SoneParsingDuration.Title|l10n|html></td>
-                               <td class="numeric"><% soneParsingDurationCount|html></td>
-                               <td class="numeric"><% soneParsingDurationMin|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationMax|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationMean|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationMedian|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationPercentile75|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationPercentile95|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationPercentile98|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationPercentile99|duration scale=="μs"|html></td>
-                               <td class="numeric"><% soneParsingDurationPercentile999|duration scale=="μs"|html></td>
-                       </tr>
+                       <%foreach histogramKeys histogramKey>
+                               <tr>
+                                       <td><%= "Page.Metrics.<%= '<%histogramKey>I18n'|parse>.Title"|parse|l10n|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Count"|parse|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Min"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Max"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Mean"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Median"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Percentile75"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Percentile95"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Percentile98"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Percentile99"|parse|duration scale=="μs"|html></td>
+                                       <td class="numeric"><% "<%histogramKey>Percentile999"|parse|duration scale=="μs"|html></td>
+                               </tr>
+                       <%/foreach>
                </tbody>
        </table>
 
index 269aafe..5a5d588 100644 (file)
@@ -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<String>, 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