use of org.apache.apex.examples.machinedata.data.ResourceType in project apex-malhar by apache.
the class CalculatorOperator method endWindow.
@Override
public void endWindow() {
if (computePercentile) {
for (MachineKey machineKey : data.rowKeySet()) {
Collections.sort(data.get(machineKey, ResourceType.CPU));
Collections.sort(data.get(machineKey, ResourceType.RAM));
Collections.sort(data.get(machineKey, ResourceType.HDD));
Map<ResourceType, Double> percentileData = Maps.newHashMap();
percentileData.put(ResourceType.CPU, getKthPercentile(data.get(machineKey, ResourceType.CPU)));
percentileData.put(ResourceType.RAM, getKthPercentile(data.get(machineKey, ResourceType.RAM)));
percentileData.put(ResourceType.HDD, getKthPercentile(data.get(machineKey, ResourceType.HDD)));
percentileOutputPort.emit(new KeyValPair<>(machineKey, percentileData));
for (ResourceType resourceType : percentileData.keySet()) {
double percentileValue = percentileData.get(resourceType);
if (percentileValue > percentileThreshold) {
emitAlert(resourceType, machineKey, percentileValue, "Percentile");
}
}
}
}
if (computeSD) {
for (MachineKey machineKey : data.rowKeySet()) {
Map<ResourceType, Double> sdData = Maps.newHashMap();
for (ResourceType resourceType : ResourceType.values()) {
sdData.put(resourceType, getSD(data.get(machineKey, resourceType)));
}
sdOutputPort.emit(new KeyValPair<>(machineKey, sdData));
for (ResourceType resourceType : sdData.keySet()) {
double sdValue = sdData.get(resourceType);
if (sdValue > sdThreshold) {
emitAlert(resourceType, machineKey, sdValue, "SD");
}
}
}
}
if (computeMax) {
for (MachineKey machineKey : data.rowKeySet()) {
Map<ResourceType, Integer> maxData = Maps.newHashMap();
maxData.put(ResourceType.CPU, Collections.max(data.get(machineKey, ResourceType.CPU)));
maxData.put(ResourceType.RAM, Collections.max(data.get(machineKey, ResourceType.RAM)));
maxData.put(ResourceType.HDD, Collections.max(data.get(machineKey, ResourceType.HDD)));
maxOutputPort.emit(new KeyValPair<>(machineKey, maxData));
for (ResourceType resourceType : maxData.keySet()) {
double sdValue = maxData.get(resourceType).doubleValue();
if (sdValue > maxThreshold) {
emitAlert(resourceType, machineKey, sdValue, "Max");
}
}
}
}
data.clear();
}
use of org.apache.apex.examples.machinedata.data.ResourceType in project apex-malhar by apache.
the class CalculatorOperatorTest method testStandarDeviation.
public void testStandarDeviation(CalculatorOperator oper) {
CollectorTestSink sortSink = new CollectorTestSink();
oper.sdOutputPort.setSink(sortSink);
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String timeKey = minuteDateFormat.format(date);
String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
Integer vs = new Integer(1);
MachineKey mk = new MachineKey(timeKey, day, vs, vs, vs, vs, vs, vs, vs);
oper.beginWindow(0);
MachineInfo info = new MachineInfo(mk, 1, 1, 1);
oper.dataPort.process(info);
info.setCpu(2);
oper.dataPort.process(info);
info.setCpu(3);
oper.dataPort.process(info);
oper.endWindow();
Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
for (Object o : sortSink.collectedTuples) {
LOG.debug(o.toString());
KeyValPair<TimeBucketKey, Map<ResourceType, Double>> keyValPair = (KeyValPair<TimeBucketKey, Map<ResourceType, Double>>) o;
Assert.assertEquals("emitted value for 'cpu' was ", getSD(ImmutableList.of(1, 2, 3)), keyValPair.getValue().get(ResourceType.CPU), 0);
Assert.assertEquals("emitted value for 'hdd' was ", getSD(ImmutableList.of(1, 1, 1)), keyValPair.getValue().get(ResourceType.HDD), 0);
Assert.assertEquals("emitted value for 'ram' was ", getSD(ImmutableList.of(1, 1, 1)), keyValPair.getValue().get(ResourceType.RAM), 0);
}
LOG.debug("Done sd testing\n");
}
use of org.apache.apex.examples.machinedata.data.ResourceType in project apex-malhar by apache.
the class CalculatorOperatorTest method testPercentile.
public void testPercentile(CalculatorOperator oper) {
CollectorTestSink sortSink = new CollectorTestSink();
oper.percentileOutputPort.setSink(sortSink);
oper.setKthPercentile(50);
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String timeKey = minuteDateFormat.format(date);
String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
Integer vs = new Integer(1);
MachineKey mk = new MachineKey(timeKey, day, vs, vs, vs, vs, vs, vs, vs);
oper.beginWindow(0);
MachineInfo info = new MachineInfo(mk, 1, 1, 1);
oper.dataPort.process(info);
info.setCpu(2);
oper.dataPort.process(info);
info.setCpu(3);
oper.dataPort.process(info);
oper.endWindow();
Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
for (Object o : sortSink.collectedTuples) {
LOG.debug(o.toString());
KeyValPair<TimeBucketKey, Map<ResourceType, Double>> keyValPair = (KeyValPair<TimeBucketKey, Map<ResourceType, Double>>) o;
Assert.assertEquals("emitted value for 'cpu' was ", 2.0, keyValPair.getValue().get(ResourceType.CPU), 0);
Assert.assertEquals("emitted value for 'hdd' was ", 1.0, keyValPair.getValue().get(ResourceType.HDD), 0);
Assert.assertEquals("emitted value for 'ram' was ", 1.0, keyValPair.getValue().get(ResourceType.RAM), 0);
}
LOG.debug("Done percentile testing\n");
}
use of org.apache.apex.examples.machinedata.data.ResourceType in project apex-malhar by apache.
the class CalculatorOperatorTest method testMax.
public void testMax(CalculatorOperator oper) {
CollectorTestSink sortSink = new CollectorTestSink();
oper.maxOutputPort.setSink(sortSink);
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String timeKey = minuteDateFormat.format(date);
String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
Integer vs = new Integer(1);
MachineKey mk = new MachineKey(timeKey, day, vs, vs, vs, vs, vs, vs, vs);
oper.beginWindow(0);
MachineInfo info = new MachineInfo(mk, 1, 1, 1);
oper.dataPort.process(info);
info.setCpu(2);
oper.dataPort.process(info);
info.setCpu(3);
oper.dataPort.process(info);
oper.endWindow();
Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
for (Object o : sortSink.collectedTuples) {
LOG.debug(o.toString());
KeyValPair<TimeBucketKey, Map<ResourceType, Double>> keyValPair = (KeyValPair<TimeBucketKey, Map<ResourceType, Double>>) o;
Assert.assertEquals("emitted value for 'cpu' was ", 3, keyValPair.getValue().get(ResourceType.CPU), 0);
Assert.assertEquals("emitted value for 'hdd' was ", 1, keyValPair.getValue().get(ResourceType.HDD), 0);
Assert.assertEquals("emitted value for 'ram' was ", 1, keyValPair.getValue().get(ResourceType.RAM), 0);
}
LOG.debug("Done max testing\n");
}
Aggregations