MetricDisplaySummary summary = new MetricDisplaySummary();
summary.setScheduleId(schedule.getId());
summary.setBeginTimeFrame(begin);
summary.setEndTimeFrame(end);
MeasurementDefinition definition = schedule.getDefinition();
summary.setDefinitionId(definition.getId());
summary.setMetricName(definition.getName());
summary.setLabel(definition.getDisplayName());
summary.setDescription(definition.getDescription());
summary.setMetricSource(schedule.getResource().getName());
summary.setMetricSourceId(schedule.getResource().getId());
switch (definition.getDataType()) {
case MEASUREMENT: {
MeasurementUnits units = definition.getUnits();
summary.setUnits(units.name());
NumericType type = definition.getNumericType();
if (type == null) {
throw new IllegalStateException("NumericType is null, but no traits are expected here");
}
int collectionType;
switch (type) {
case DYNAMIC: {
collectionType = 0;
break;
}
case TRENDSUP: {
collectionType = 2;
break;
}
case TRENDSDOWN: {
collectionType = 3;
break;
}
default: {
throw new IllegalStateException("Unknown measurement type: " + type);
}
}
summary.setCollectionType(collectionType);
if (!narrowed) {
MeasurementAggregate compositeHighLow = dataManager.getMeasurementAggregate(subject, schedule.getId(),
begin, end);
if (compositeHighLow.isEmpty()) {
summary.setValuesPresent(false);
}
Map<String, MetricDisplayValue> metricValues = new HashMap<String, MetricDisplayValue>();
metricValues.put(MetricDisplayConstants.MIN_KEY, new MetricDisplayValue(compositeHighLow.getMin()));
metricValues.put(MetricDisplayConstants.AVERAGE_KEY, new MetricDisplayValue(compositeHighLow.getAvg()));
metricValues.put(MetricDisplayConstants.MAX_KEY, new MetricDisplayValue(compositeHighLow.getMax()));
MeasurementDataNumeric currentNumeric = dataManager.getCurrentNumericForSchedule(schedule.getId());
Double lastValue = (currentNumeric != null) ? currentNumeric.getValue() : Double.NaN;
metricValues.put(MetricDisplayConstants.LAST_KEY, new MetricDisplayValue(lastValue));
summary.setMetrics(metricValues);
}
break;
}
case TRAIT: {
summary.setIsTrait(true);
MeasurementDataTrait trait = dataManager.getCurrentTraitForSchedule(schedule.getId());
if (trait != null) {
summary.setValue(trait.getValue());
summary.setTimestamp(trait.getTimestamp());
} else {
summary.setValue("- nothing found -");
summary.setTimestamp(System.currentTimeMillis());
}
break;
}
case CALLTIME: {
// TODO: ignore?
break;
}
default: {
throw new IllegalStateException("Unsupported metric data type: " + definition.getDataType());
}
}
return summary;
}