in ambari-metrics-timelineservice/src/main/java/org/apache/ambari/metrics/core/timeline/aggregators/AggregatorUtils.java [73:137]
public static Map<TimelineClusterMetric, Double> sliceFromTimelineMetric(
TimelineMetric timelineMetric, List<Long[]> timeSlices, boolean interpolationEnabled) {
if (timelineMetric.getMetricValues().isEmpty()) {
return null;
}
Map<TimelineClusterMetric, Double> timelineClusterMetricMap =
new HashMap<>();
Long prevTimestamp = -1l;
TimelineClusterMetric prevMetric = null;
int count = 0;
double sum = 0.0;
Map<Long,Double> timeSliceValueMap = new HashMap<>();
for (Map.Entry<Long, Double> metric : timelineMetric.getMetricValues().entrySet()) {
if (metric.getValue() == null) {
continue;
}
Long timestamp = getSliceTimeForMetric(timeSlices, Long.parseLong(metric.getKey().toString()));
if (timestamp != -1) {
// Metric is within desired time range
TimelineClusterMetric clusterMetric = new TimelineClusterMetric(
timelineMetric.getMetricName(),
timelineMetric.getAppId(),
timelineMetric.getInstanceId(),
timestamp);
if (prevTimestamp < 0 || timestamp.equals(prevTimestamp)) {
Double newValue = metric.getValue();
if (newValue > 0.0) {
sum += newValue;
count++;
}
} else {
double metricValue = (count > 0) ? (sum / count) : 0.0;
timelineClusterMetricMap.put(prevMetric, metricValue);
timeSliceValueMap.put(prevMetric.getTimestamp(), metricValue);
sum = metric.getValue();
count = sum > 0.0 ? 1 : 0;
}
prevTimestamp = timestamp;
prevMetric = clusterMetric;
}
}
if (prevTimestamp > 0) {
double metricValue = (count > 0) ? (sum / count) : 0.0;
timelineClusterMetricMap.put(prevMetric, metricValue);
timeSliceValueMap.put(prevTimestamp, metricValue);
}
if (interpolationEnabled) {
Map<Long, Double> interpolatedValues = interpolateMissingPeriods(timelineMetric.getMetricValues(), timeSlices, timeSliceValueMap, timelineMetric.getType());
for (Map.Entry<Long, Double> entry : interpolatedValues.entrySet()) {
TimelineClusterMetric timelineClusterMetric = new TimelineClusterMetric(timelineMetric.getMetricName(), timelineMetric.getAppId(), timelineMetric.getInstanceId(), entry.getKey());
timelineClusterMetricMap.putIfAbsent(timelineClusterMetric, entry.getValue());
}
}
return timelineClusterMetricMap;
}