use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.
the class MarginMap method endWindow.
/**
* Generates tuples for each key and emits them. Only keys that are in the denominator are iterated on
* If the key is only in the numerator, it gets ignored (cannot do divide by 0)
* Clears internal data
*/
@Override
public void endWindow() {
HashMap<K, V> tuples = new HashMap<K, V>();
Double val;
for (Map.Entry<K, MutableDouble> e : denominators.entrySet()) {
MutableDouble nval = numerators.get(e.getKey());
if (nval == null) {
nval = new MutableDouble(0.0);
} else {
// so that all left over keys can be reported
numerators.remove(e.getKey());
}
if (percent) {
val = (1 - nval.doubleValue() / e.getValue().doubleValue()) * 100;
} else {
val = 1 - nval.doubleValue() / e.getValue().doubleValue();
}
tuples.put(e.getKey(), getValue(val.doubleValue()));
}
if (!tuples.isEmpty()) {
margin.emit(tuples);
}
numerators.clear();
denominators.clear();
}
use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.
the class MultiWindowDimensionAggregationTest method testNodeProcessingSchema.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(MultiWindowDimensionAggregation oper) {
oper.setWindowSize(3);
List<int[]> dimensionArrayList = new ArrayList<int[]>();
int[] dimensionArray = { 0, 1 };
int[] dimensionArray_2 = { 0 };
dimensionArrayList.add(dimensionArray);
dimensionArrayList.add(dimensionArray_2);
oper.setDimensionArray(dimensionArrayList);
oper.setTimeBucket("m");
oper.setDimensionKeyVal("0");
oper.setOperationType(AggregateOperation.AVERAGE);
oper.setup(null);
CollectorTestSink sortSink = new CollectorTestSink();
oper.output.setSink(sortSink);
oper.beginWindow(0);
Map<String, Map<String, Number>> data_0 = new HashMap<String, Map<String, Number>>();
Map<String, Number> input_0 = new HashMap<String, Number>();
input_0.put("0", new MutableDouble(9));
input_0.put("1", new MutableDouble(9));
input_0.put("2", new MutableDouble(9));
data_0.put("m|20130823131512|0:abc|1:ff", input_0);
data_0.put("m|20130823131512|0:abc", input_0);
data_0.put("m|20130823131512|0:abc|1:ie", input_0);
Map<String, Number> input_new = new HashMap<String, Number>();
input_new.put("0", new MutableDouble(19));
input_new.put("1", new MutableDouble(19));
input_new.put("2", new MutableDouble(19));
data_0.put("m|20130823131512|0:def|1:ie", input_new);
oper.data.process(data_0);
oper.endWindow();
Map<String, Map<String, Number>> data_1 = new HashMap<String, Map<String, Number>>();
Map<String, Number> input_1 = new HashMap<String, Number>();
oper.beginWindow(1);
input_1.put("0", new MutableDouble(9));
input_1.put("1", new MutableDouble(9));
input_1.put("2", new MutableDouble(9));
data_1.put("m|20130823131513|0:def|1:ff", input_1);
data_1.put("m|20130823131513|0:abc|1:ie", input_1);
oper.data.process(data_1);
oper.endWindow();
Map<String, Map<String, Number>> data_2 = new HashMap<String, Map<String, Number>>();
Map<String, Number> input_2 = new HashMap<String, Number>();
oper.beginWindow(2);
input_2.put("0", new MutableDouble(19));
input_2.put("1", new MutableDouble(19));
input_2.put("2", new MutableDouble(19));
data_2.put("m|20130823131514|0:def|1:ff", input_2);
data_2.put("m|20130823131514|0:abc|1:ie", input_2);
oper.data.process(data_2);
oper.endWindow();
Map<String, Map<String, Number>> data_3 = new HashMap<String, Map<String, Number>>();
Map<String, Number> input_3 = new HashMap<String, Number>();
oper.beginWindow(3);
input_3.put("0", new MutableDouble(19));
input_3.put("1", new MutableDouble(19));
input_3.put("2", new MutableDouble(19));
data_3.put("m|20130823131514|0:def|1:ff", input_3);
data_3.put("m|20130823131514|0:abc|1:ie", input_3);
oper.data.process(data_3);
oper.endWindow();
Assert.assertEquals("number emitted tuples", 16, sortSink.collectedTuples.size());
for (Object o : sortSink.collectedTuples) {
logger.debug(o.toString());
}
logger.debug("Done testing round\n");
}
use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.
the class TopNUniqueSiteStatsTest method testNodeProcessingSchema.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(TopNUnique oper) {
CollectorTestSink sortSink = new CollectorTestSink();
oper.top.setSink(sortSink);
oper.setN(3);
oper.beginWindow(0);
HashMap<String, DimensionObject<String>> input = new HashMap<String, DimensionObject<String>>();
input.put("url", new DimensionObject<String>(new MutableDouble(10), "abc"));
oper.data.process(input);
input.clear();
input.put("url", new DimensionObject<String>(new MutableDouble(1), "def"));
input.put("url1", new DimensionObject<String>(new MutableDouble(1), "def"));
oper.data.process(input);
input.clear();
input.put("url", new DimensionObject<String>(new MutableDouble(101), "ghi"));
input.put("url1", new DimensionObject<String>(new MutableDouble(101), "ghi"));
oper.data.process(input);
input.clear();
input.put("url", new DimensionObject<String>(new MutableDouble(50), "jkl"));
oper.data.process(input);
input.clear();
input.put("url", new DimensionObject<String>(new MutableDouble(50), "jkl"));
input.put("url3", new DimensionObject<String>(new MutableDouble(50), "jkl"));
oper.data.process(input);
oper.endWindow();
Assert.assertEquals("number emitted tuples", 3, sortSink.collectedTuples.size());
for (Object o : sortSink.collectedTuples) {
log.debug(o.toString());
}
log.debug("Done testing round\n");
}
use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.
the class MultiWindowDimensionAggregation method beginWindow.
@Override
public void beginWindow(long arg0) {
Map<String, Map<String, Number>> currentWindowMap = cacheOject.get(currentWindow);
if (currentWindowMap == null) {
currentWindowMap = new HashMap<String, Map<String, Number>>();
} else {
for (Map.Entry<String, Map<String, Number>> tupleEntry : currentWindowMap.entrySet()) {
String tupleKey = tupleEntry.getKey();
Map<String, Number> tupleValue = tupleEntry.getValue();
int currentPattern = 0;
for (Pattern pattern : patternList) {
Matcher matcher = pattern.matcher(tupleKey);
if (matcher.matches()) {
String currentPatternString = dimensionArrayString.get(currentPattern);
Map<String, KeyValPair<MutableDouble, Integer>> currentPatternMap = outputMap.get(currentPatternString);
if (currentPatternMap != null) {
StringBuilder builder = new StringBuilder(matcher.group(2));
for (int i = 1; i < dimensionArray.get(currentPattern).length; i++) {
builder.append("," + matcher.group(i + 2));
}
KeyValPair<MutableDouble, Integer> currentDimensionKeyValPair = currentPatternMap.get(builder.toString());
if (currentDimensionKeyValPair != null) {
currentDimensionKeyValPair.getKey().add(0 - tupleValue.get(dimensionKeyVal).doubleValue());
currentDimensionKeyValPair.setValue(currentDimensionKeyValPair.getValue() - 1);
if (currentDimensionKeyValPair.getKey().doubleValue() == 0.0) {
currentPatternMap.remove(builder.toString());
}
}
}
break;
}
currentPattern++;
}
}
}
currentWindowMap.clear();
if (patternList == null || patternList.isEmpty()) {
setUpPatternList();
}
}
use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.
the class DimensionTimeBucketSumOperatorTest method testDimensionTimeBucket.
@Test
public void testDimensionTimeBucket() throws InterruptedException {
DimensionTimeBucketSumOperator oper = new DimensionTimeBucketSumOperator();
CollectorTestSink sortSink = new CollectorTestSink();
oper.out.setSink(sortSink);
oper.addDimensionKeyName("ipAddr");
oper.addDimensionKeyName("url");
oper.addDimensionKeyName("status");
oper.addDimensionKeyName("agent");
oper.addValueKeyName("bytes");
Set<String> dimensionKey = new HashSet<String>();
dimensionKey.add("ipAddr");
dimensionKey.add("url");
try {
oper.addCombination(dimensionKey);
} catch (NoSuchFieldException e) {
// ignored
}
oper.setTimeBucketFlags(AbstractDimensionTimeBucketOperator.TIMEBUCKET_MINUTE);
oper.setup(null);
oper.beginWindow(0);
oper.in.process(getMap("10.10.1.1", "/movies", "200", "FF", 20));
oper.in.process(getMap("10.10.1.2", "/movies", "200", "FF", 20));
oper.in.process(getMap("10.10.1.2", "/movies", "200", "FF", 20));
oper.endWindow();
Map<String, Map<String, Number>> outputMap = Maps.newHashMap();
Map<String, Number> key1 = Maps.newHashMap();
key1.put("1", new MutableDouble(40.0));
key1.put("0", new MutableDouble(2.0));
outputMap.put("m|197001010000|0:10.10.1.2|1:/movies", key1);
Map<String, Number> key2 = Maps.newHashMap();
key2.put("0", new MutableDouble(1.0));
key2.put("1", new MutableDouble(20.0));
outputMap.put("m|197001010000|0:10.10.1.1|1:/movies", key2);
Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
for (Object o : sortSink.collectedTuples) {
Assert.assertEquals("content of tuple ", outputMap, o);
logger.debug(o.toString());
}
logger.debug("Done testing round\n");
}
Aggregations