MetricsResponse { protected StringBuilder generateHtml() { final MetaCache cache = MetaCacheFactory.getCache(conf); TreeSet<Meta> tree = new TreeSet<>(); cache.forEach(m -> tree.add(m)); final StringBuilder b = new StringBuilder(); b.append(DOCTYPE); b.append(HTML_START); b.append(HEAD_START); b.append(META); b.append(TITLE); b.append(HEAD_END); b.append(HEADER_START); b.append(HEADER_CONTENT).append(ignoredTags.toString()).append("</p>\n"); b.append(HEADER_END); b.append(BODY_START); b.append(TABLE_START); b.append(TR_START); b.append(TH_START).append("Metric").append(TH_END); b.append(TH_START).append("Available Tags").append(TH_END); b.append(TR_END); String prevMetric = null; StringBuilder tags = new StringBuilder(); Map<String, Long> tagMap = new HashMap<>(); for (Meta m : tree) { if (prevMetric != null && !m.getMetric().equals(prevMetric)) { tagMap.clear(); b.append(TR_START); b.append(TD_START).append(prevMetric).append(TD_END); b.append(TD_START).append(tags.toString()).append(TD_END); b.append(TR_END); prevMetric = m.getMetric(); tags.delete(0, tags.length()); } if (prevMetric == null) { prevMetric = m.getMetric(); } long numValues = tagMap.getOrDefault(m.getTagKey(), 0l); if (!(this.ignoredTags.contains(m.getTagKey()))) { if (numValues <= conf.getMetaCache().getMaxTagValues()) { tags.append(m.getTagKey()).append("=").append(m.getTagValue()).append(" "); tagMap.put(m.getTagKey(), ++numValues); } } } b.append(TR_START); b.append(TD_START).append(prevMetric).append(TD_END); b.append(TD_START).append(tags.toString()).append(TD_END); b.append(TR_END); tags = null; prevMetric = null; b.append(TABLE_END); b.append(BODY_END); b.append(HTML_END); return b; } MetricsResponse();  MetricsResponse(Configuration conf); TextWebSocketFrame toWebSocketResponse(String acceptHeader); FullHttpResponse toHttpResponse(String acceptHeader);  }
@Test public void testGenerateHtml() throws Exception { Configuration cfg = TestConfiguration.createMinimalConfigurationForTest(); MetaCache cache = MetaCacheFactory.getCache(cfg); cache.add(new Meta("sys.cpu.user", "host", "localhost")); cache.add(new Meta("sys.cpu.user", "instance", "0")); cache.add(new Meta("sys.cpu.idle", "host", "localhost")); cache.add(new Meta("sys.cpu.idle", "instance", "0")); TestMetricsResponse r = new TestMetricsResponse(cfg); String html = r.generateHtml().toString(); Assert.assertTrue(html.contains("<td>sys.cpu.idle</td>")); Assert.assertTrue(html.contains("<td>host=localhost instance=0 </td>")); Assert.assertTrue(html.contains("<td>sys.cpu.user</td>")); Assert.assertTrue(html.contains("<td>host=localhost instance=0 </td>")); }
@Test public void testGenerateHtmlWithIgnoredTags() throws Exception { Configuration cfg = TestConfiguration.createMinimalConfigurationForTest(); cfg.getMetricsReportIgnoredTags().add("instance"); MetaCache cache = MetaCacheFactory.getCache(cfg); cache.add(new Meta("sys.cpu.user", "host", "localhost")); cache.add(new Meta("sys.cpu.user", "instance", "0")); cache.add(new Meta("sys.cpu.idle", "host", "localhost")); cache.add(new Meta("sys.cpu.idle", "instance", "0")); TestMetricsResponse r = new TestMetricsResponse(cfg); String html = r.generateHtml().toString(); Assert.assertTrue(html.contains("<td>sys.cpu.idle</td>")); Assert.assertTrue(html.contains("<td>host=localhost </td>")); Assert.assertTrue(html.contains("<td>sys.cpu.user</td>")); Assert.assertTrue(html.contains("<td>host=localhost </td>")); }