@Test public void testProcessMetrics() throws Exception { Configuration configuration = new Configuration(); TimelineMetricsHolder timelineMetricsHolder = TimelineMetricsHolder.getInstance(); timelineMetricsHolder.extractMetricsForAggregationPublishing(); timelineMetricsHolder.extractMetricsForRawPublishing(); TreeMap<Long, Double> metric1App1Metrics = new TreeMap<>(); metric1App1Metrics.put(1L, 1d); metric1App1Metrics.put(2L, 2d); metric1App1Metrics.put(3L, 3d); timelineMetricsHolder.putMetricsForAggregationPublishing(getTimelineMetricsForAppId("metricName1", "app1", metric1App1Metrics)); TreeMap<Long, Double> metric2App2Metrics = new TreeMap<>(); metric2App2Metrics.put(1L, 4d); metric2App2Metrics.put(2L, 5d); metric2App2Metrics.put(3L, 6d); timelineMetricsHolder.putMetricsForAggregationPublishing(getTimelineMetricsForAppId("metricName2", "app2", metric2App2Metrics)); TreeMap<Long, Double> metric3App3Metrics = new TreeMap<>(); metric3App3Metrics.put(1L, 7d); metric3App3Metrics.put(2L, 8d); metric3App3Metrics.put(3L, 9d); timelineMetricsHolder.putMetricsForAggregationPublishing(getTimelineMetricsForAppId("metricName3", "app3", metric3App3Metrics)); AggregatedMetricsPublisher aggregatedMetricsPublisher = new AggregatedMetricsPublisher(TimelineMetricsHolder.getInstance(), configuration, 60); String aggregatedJson = aggregatedMetricsPublisher.processMetrics(timelineMetricsHolder.extractMetricsForAggregationPublishing()); String expectedMetric1App1 = "{\"timelineMetric\":{\"timestamp\":0,\"metadata\":{},\"metricname\":\"metricName1\",\"appid\":\"app1\",\"starttime\":0,\"metrics\":{}},\"metricAggregate\":{\"sum\":6.0,\"deviation\":0.0,\"max\":3.0,\"min\":1.0,\"numberOfSamples\":3}}"; String expectedMetric2App2 = "{\"timelineMetric\":{\"timestamp\":0,\"metadata\":{},\"metricname\":\"metricName2\",\"appid\":\"app2\",\"starttime\":0,\"metrics\":{}},\"metricAggregate\":{\"sum\":15.0,\"deviation\":0.0,\"max\":6.0,\"min\":4.0,\"numberOfSamples\":3}}"; String expectedMetric3App3 = "{\"timelineMetric\":{\"timestamp\":0,\"metadata\":{},\"metricname\":\"metricName3\",\"appid\":\"app3\",\"starttime\":0,\"metrics\":{}},\"metricAggregate\":{\"sum\":24.0,\"deviation\":0.0,\"max\":9.0,\"min\":7.0,\"numberOfSamples\":3}}"; Assert.assertNotNull(aggregatedJson); Assert.assertTrue(aggregatedJson.contains(expectedMetric1App1)); Assert.assertTrue(aggregatedJson.contains(expectedMetric3App3)); Assert.assertTrue(aggregatedJson.contains(expectedMetric2App2)); }
@Override protected String processMetrics(Map<String, TimelineMetrics> metricForAggregationValues) { HashMap<String, TimelineMetrics> nameToMetricMap = new HashMap<>(); for (TimelineMetrics timelineMetrics : metricForAggregationValues.values()) { for (TimelineMetric timelineMetric : timelineMetrics.getMetrics()) { if (!nameToMetricMap.containsKey(timelineMetric.getMetricName())) { nameToMetricMap.put(timelineMetric.getMetricName(), new TimelineMetrics()); } nameToMetricMap.get(timelineMetric.getMetricName()).addOrMergeTimelineMetric(timelineMetric); } } Set<TimelineMetricWithAggregatedValues> metricAggregateMap = new HashSet<>(); for (TimelineMetrics metrics : nameToMetricMap.values()) { double sum = 0; double max = Integer.MIN_VALUE; double min = Integer.MAX_VALUE; int count = 0; TimelineMetric tmpMetric = new TimelineMetric(metrics.getMetrics().iterator().next()); for (TimelineMetric metric : metrics.getMetrics()) { for (Double value : metric.getMetricValues().values()) { sum+=value; max = Math.max(max, value); min = Math.min(min, value); count++; } if (metric.getStartTime() > tmpMetric.getStartTime()) { tmpMetric.setStartTime(metric.getStartTime()); } } tmpMetric.setMetricValues(new TreeMap<Long, Double>()); metricAggregateMap.add(new TimelineMetricWithAggregatedValues(tmpMetric, new MetricHostAggregate(sum, count, 0d, max, min))); } String json = null; try { json = mapper.writeValueAsString(new AggregationResult(metricAggregateMap, System.currentTimeMillis())); LOG.debug(json); } catch (Exception e) { LOG.error("Failed to convert result into json", e); } return json; }
AggregatedMetricsPublisher extends AbstractMetricPublisher { @Override protected String processMetrics(Map<String, TimelineMetrics> metricForAggregationValues) { HashMap<String, TimelineMetrics> nameToMetricMap = new HashMap<>(); for (TimelineMetrics timelineMetrics : metricForAggregationValues.values()) { for (TimelineMetric timelineMetric : timelineMetrics.getMetrics()) { if (!nameToMetricMap.containsKey(timelineMetric.getMetricName())) { nameToMetricMap.put(timelineMetric.getMetricName(), new TimelineMetrics()); } nameToMetricMap.get(timelineMetric.getMetricName()).addOrMergeTimelineMetric(timelineMetric); } } Set<TimelineMetricWithAggregatedValues> metricAggregateMap = new HashSet<>(); for (TimelineMetrics metrics : nameToMetricMap.values()) { double sum = 0; double max = Integer.MIN_VALUE; double min = Integer.MAX_VALUE; int count = 0; TimelineMetric tmpMetric = new TimelineMetric(metrics.getMetrics().iterator().next()); for (TimelineMetric metric : metrics.getMetrics()) { for (Double value : metric.getMetricValues().values()) { sum+=value; max = Math.max(max, value); min = Math.min(min, value); count++; } if (metric.getStartTime() > tmpMetric.getStartTime()) { tmpMetric.setStartTime(metric.getStartTime()); } } tmpMetric.setMetricValues(new TreeMap<Long, Double>()); metricAggregateMap.add(new TimelineMetricWithAggregatedValues(tmpMetric, new MetricHostAggregate(sum, count, 0d, max, min))); } String json = null; try { json = mapper.writeValueAsString(new AggregationResult(metricAggregateMap, System.currentTimeMillis())); LOG.debug(json); } catch (Exception e) { LOG.error("Failed to convert result into json", e); } return json; } }
AggregatedMetricsPublisher extends AbstractMetricPublisher { @Override protected String processMetrics(Map<String, TimelineMetrics> metricForAggregationValues) { HashMap<String, TimelineMetrics> nameToMetricMap = new HashMap<>(); for (TimelineMetrics timelineMetrics : metricForAggregationValues.values()) { for (TimelineMetric timelineMetric : timelineMetrics.getMetrics()) { if (!nameToMetricMap.containsKey(timelineMetric.getMetricName())) { nameToMetricMap.put(timelineMetric.getMetricName(), new TimelineMetrics()); } nameToMetricMap.get(timelineMetric.getMetricName()).addOrMergeTimelineMetric(timelineMetric); } } Set<TimelineMetricWithAggregatedValues> metricAggregateMap = new HashSet<>(); for (TimelineMetrics metrics : nameToMetricMap.values()) { double sum = 0; double max = Integer.MIN_VALUE; double min = Integer.MAX_VALUE; int count = 0; TimelineMetric tmpMetric = new TimelineMetric(metrics.getMetrics().iterator().next()); for (TimelineMetric metric : metrics.getMetrics()) { for (Double value : metric.getMetricValues().values()) { sum+=value; max = Math.max(max, value); min = Math.min(min, value); count++; } if (metric.getStartTime() > tmpMetric.getStartTime()) { tmpMetric.setStartTime(metric.getStartTime()); } } tmpMetric.setMetricValues(new TreeMap<Long, Double>()); metricAggregateMap.add(new TimelineMetricWithAggregatedValues(tmpMetric, new MetricHostAggregate(sum, count, 0d, max, min))); } String json = null; try { json = mapper.writeValueAsString(new AggregationResult(metricAggregateMap, System.currentTimeMillis())); LOG.debug(json); } catch (Exception e) { LOG.error("Failed to convert result into json", e); } return json; } AggregatedMetricsPublisher(TimelineMetricsHolder timelineMetricsHolder, Configuration configuration, int interval); }
AggregatedMetricsPublisher extends AbstractMetricPublisher { @Override protected String processMetrics(Map<String, TimelineMetrics> metricForAggregationValues) { HashMap<String, TimelineMetrics> nameToMetricMap = new HashMap<>(); for (TimelineMetrics timelineMetrics : metricForAggregationValues.values()) { for (TimelineMetric timelineMetric : timelineMetrics.getMetrics()) { if (!nameToMetricMap.containsKey(timelineMetric.getMetricName())) { nameToMetricMap.put(timelineMetric.getMetricName(), new TimelineMetrics()); } nameToMetricMap.get(timelineMetric.getMetricName()).addOrMergeTimelineMetric(timelineMetric); } } Set<TimelineMetricWithAggregatedValues> metricAggregateMap = new HashSet<>(); for (TimelineMetrics metrics : nameToMetricMap.values()) { double sum = 0; double max = Integer.MIN_VALUE; double min = Integer.MAX_VALUE; int count = 0; TimelineMetric tmpMetric = new TimelineMetric(metrics.getMetrics().iterator().next()); for (TimelineMetric metric : metrics.getMetrics()) { for (Double value : metric.getMetricValues().values()) { sum+=value; max = Math.max(max, value); min = Math.min(min, value); count++; } if (metric.getStartTime() > tmpMetric.getStartTime()) { tmpMetric.setStartTime(metric.getStartTime()); } } tmpMetric.setMetricValues(new TreeMap<Long, Double>()); metricAggregateMap.add(new TimelineMetricWithAggregatedValues(tmpMetric, new MetricHostAggregate(sum, count, 0d, max, min))); } String json = null; try { json = mapper.writeValueAsString(new AggregationResult(metricAggregateMap, System.currentTimeMillis())); LOG.debug(json); } catch (Exception e) { LOG.error("Failed to convert result into json", e); } return json; } AggregatedMetricsPublisher(TimelineMetricsHolder timelineMetricsHolder, Configuration configuration, int interval);  }
AggregatedMetricsPublisher extends AbstractMetricPublisher { @Override protected String processMetrics(Map<String, TimelineMetrics> metricForAggregationValues) { HashMap<String, TimelineMetrics> nameToMetricMap = new HashMap<>(); for (TimelineMetrics timelineMetrics : metricForAggregationValues.values()) { for (TimelineMetric timelineMetric : timelineMetrics.getMetrics()) { if (!nameToMetricMap.containsKey(timelineMetric.getMetricName())) { nameToMetricMap.put(timelineMetric.getMetricName(), new TimelineMetrics()); } nameToMetricMap.get(timelineMetric.getMetricName()).addOrMergeTimelineMetric(timelineMetric); } } Set<TimelineMetricWithAggregatedValues> metricAggregateMap = new HashSet<>(); for (TimelineMetrics metrics : nameToMetricMap.values()) { double sum = 0; double max = Integer.MIN_VALUE; double min = Integer.MAX_VALUE; int count = 0; TimelineMetric tmpMetric = new TimelineMetric(metrics.getMetrics().iterator().next()); for (TimelineMetric metric : metrics.getMetrics()) { for (Double value : metric.getMetricValues().values()) { sum+=value; max = Math.max(max, value); min = Math.min(min, value); count++; } if (metric.getStartTime() > tmpMetric.getStartTime()) { tmpMetric.setStartTime(metric.getStartTime()); } } tmpMetric.setMetricValues(new TreeMap<Long, Double>()); metricAggregateMap.add(new TimelineMetricWithAggregatedValues(tmpMetric, new MetricHostAggregate(sum, count, 0d, max, min))); } String json = null; try { json = mapper.writeValueAsString(new AggregationResult(metricAggregateMap, System.currentTimeMillis())); LOG.debug(json); } catch (Exception e) { LOG.error("Failed to convert result into json", e); } return json; } AggregatedMetricsPublisher(TimelineMetricsHolder timelineMetricsHolder, Configuration configuration, int interval);   }