@QuartetExtendedPluginValue(intf = IDeeTaskExtractionProcedure.class, key = PercentileExtractionProcedure.PLUGIN_KEY)
public class PercentileExtractionProcedure extends ADeeTaskExtractionProcedure {
private static final long serialVersionUID = -6960840774398324497L;
public static final String PLUGIN_KEY = "PercentileEP";
protected static final String AGGREGATION_RESULT_LIST = "AggregationResultList";
private static final String CALCULATION_ID_PROP = "calculationId";
private static final String CALCULATION_ID_PROP_DEFAULT_VALUE = "CalculationId";
public static final String PERCENTILE_LIST_KEY = "percentileList";
public static final String PERCENTILE_LIST_DEFAULT = "0,1,2.5,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,97.5,99,100";
public static final String PERCENTILE_RESULT_PREFIX = "Percentile_";
private static final String DELIMITER = ",";
private final Properties properties;
private final String[] percentileArray;
private ILevelInfo calculationIdLevelInfo;
public PercentileExtractionProcedure(final Properties properties) {
this.properties = properties;
percentileArray = properties.getProperty(PERCENTILE_LIST_KEY, PERCENTILE_LIST_DEFAULT).split(DELIMITER);
}
@Override
public void init(final List<? extends IHierarchy> hierarchies) throws ActiveViamException {
calculationIdLevelInfo = HierarchiesUtil
.getLevelFromProperty(hierarchies, properties.getProperty(CALCULATION_ID_PROP, CALCULATION_ID_PROP_DEFAULT_VALUE)).getLevelInfo();
}
/**
* aggregatesMap will be modified in place.
*/
@Override
public List<Map<ILocation, Map<String, Object>>> process(final Map<ILocation, Map<String, Object>> aggregatesMap, final IActivePivotContext context) {
final List<Map<ILocation, Map<String, Object>>> result = new ArrayList<>();
// Retrieve the calculationId/Timesteps linked to the location
final String calculationId = (String) LocationUtil.getCoordinate(aggregatesMap.keySet().iterator().next(), calculationIdLevelInfo);
for (final Map.Entry<ILocation, Map<String, Object>> aggregatesEntry : aggregatesMap.entrySet()) {
final ILocation currentLocation = aggregatesEntry.getKey();
final Map<String, Object> inputAggregates = aggregatesEntry.getValue();
if (inputAggregates == null) {
continue;
}
for (final Map.Entry<String, Object> input : inputAggregates.entrySet()) {
// assuming whenever this extraction procedure is used, all measure values are vectors
assert input.getValue() instanceof IVector;
final IVector vector = (IVector) input.getValue();
if (vector == null) {
continue;
}
final Map<String, Double> percentileValues = new LinkedHashMap<>();
for (final String percentile : percentileArray) {
percentileValues.put(PERCENTILE_RESULT_PREFIX + percentile, vector.quantileDouble(Double.parseDouble(percentile) / 100d));
}
final Map<ILocation, Map<String, Object>> locationResult = new HashMap<>();
locationResult.put(currentLocation, percentileValues);
// Add aggregates to the result
result.add(locationResult);
}
}
return result;
}
@Override
public List<AggregateDTO> process(final List<AggregateDTO> aggregates, final IActivePivotContext context) {
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " does not support GetAggregatesQuery.");
}
@Override
public Properties getProperties() {
return properties;
}
@Override
public String getType() {
return PLUGIN_KEY;
}
}