Search in sources :

Example 21 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class AverageAlertingOperator method processTuple.

private void processTuple(KeyValPair<MerchantKey, Long> tuple) {
    MerchantKey merchantKey = tuple.getKey();
    MutableDouble lastSma = lastSMAMap.get(tuple.getKey());
    long txValue = tuple.getValue();
    if (lastSma != null && txValue > lastSma.doubleValue()) {
        double lastSmaValue = lastSma.doubleValue();
        double change = txValue - lastSmaValue;
        if (change > threshold) {
            // generate an alert
            AverageAlertData data = getOutputData(merchantKey, txValue, change, lastSmaValue);
            alerts.add(data);
            // if (userGenerated) {   // if its user generated only the pass it to WebSocket
            if (merchantKey.merchantType == MerchantTransaction.MerchantType.BRICK_AND_MORTAR) {
                avgAlertNotificationPort.emit(getOutputData(data, String.format(brickMortarAlertMsg, txValue, change, lastSmaValue, merchantKey.merchantId, merchantKey.terminalId)));
            } else {
                // its internet based
                avgAlertNotificationPort.emit(getOutputData(data, String.format(internetAlertMsg, txValue, change, lastSmaValue, merchantKey.merchantId)));
            }
        // }
        }
    }
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble)

Example 22 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class AverageAlertingOperator method endWindow.

@Override
public void endWindow() {
    for (AverageAlertData data : alerts) {
        try {
            avgAlertOutputPort.emit(JsonUtils.toJson(data));
        } catch (IOException e) {
            logger.warn("Exception while converting object to JSON", e);
        }
    }
    alerts.clear();
    for (Map.Entry<MerchantKey, MutableDouble> entry : currentSMAMap.entrySet()) {
        MerchantKey key = entry.getKey();
        MutableDouble currentSma = entry.getValue();
        MutableDouble lastSma = lastSMAMap.get(key);
        if (lastSma == null) {
            lastSma = new MutableDouble(currentSma.doubleValue());
            lastSMAMap.put(key, lastSma);
        } else {
            lastSma.setValue(currentSma.getValue());
        }
    }
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class DimensionOperatorTest method testOperator.

@Test
@SuppressWarnings("unchecked")
public void testOperator() {
    DimensionOperator oper = new DimensionOperator();
    LogstreamPropertyRegistry registry = new LogstreamPropertyRegistry();
    registry.bind(LogstreamUtil.LOG_TYPE, "apache");
    registry.bind(LogstreamUtil.FILTER, "ALL");
    oper.setRegistry(registry);
    // user input example::
    // type=apache,timebucket=m,timebucket=h,dimensions=a:b:c,dimensions=b:c,dimensions=b,dimensions=d,values=x.sum:y.sum:y.avg
    oper.addPropertiesFromString(new String[] { "type=apache", "timebucket=s", "dimensions=name", "dimensions=url", "dimensions=name:url", "values=value.sum:value.avg" });
    HashMap<String, Object> inMap1 = new HashMap<String, Object>();
    inMap1.put(LogstreamUtil.LOG_TYPE, registry.getIndex(LogstreamUtil.LOG_TYPE, "apache"));
    inMap1.put(LogstreamUtil.FILTER, registry.getIndex(LogstreamUtil.FILTER, "ALL"));
    inMap1.put("name", "abc");
    inMap1.put("url", "http://www.t.co");
    inMap1.put("value", 25);
    inMap1.put("response", "404");
    HashMap<String, Object> inMap2 = new HashMap<String, Object>();
    inMap2.put(LogstreamUtil.LOG_TYPE, registry.getIndex(LogstreamUtil.LOG_TYPE, "apache"));
    inMap2.put(LogstreamUtil.FILTER, registry.getIndex(LogstreamUtil.FILTER, "ALL"));
    inMap2.put("name", "xyz");
    inMap2.put("url", "http://www.t.co");
    inMap2.put("value", 25);
    inMap2.put("response", "404");
    HashMap<String, Object> inMap3 = new HashMap<String, Object>();
    inMap3.put(LogstreamUtil.LOG_TYPE, registry.getIndex(LogstreamUtil.LOG_TYPE, "apache"));
    inMap3.put(LogstreamUtil.FILTER, registry.getIndex(LogstreamUtil.FILTER, "ALL"));
    inMap3.put("name", "abc");
    inMap3.put("url", "http://www.t.co");
    inMap3.put("value", 25);
    inMap3.put("response", "404");
    HashMap<String, Object> inMap4 = new HashMap<String, Object>();
    inMap4.put(LogstreamUtil.LOG_TYPE, registry.getIndex(LogstreamUtil.LOG_TYPE, "apache"));
    inMap4.put(LogstreamUtil.FILTER, registry.getIndex(LogstreamUtil.FILTER, "ALL"));
    inMap4.put("name", "abc");
    inMap4.put("url", "http://www.t.co");
    inMap4.put("value", 25);
    inMap4.put("response", "404");
    CollectorTestSink mapSink = new CollectorTestSink();
    oper.aggregationsOutput.setSink(mapSink);
    long now = System.currentTimeMillis();
    long currentId = 0L;
    long windowId = (now / 1000) << 32 | currentId;
    oper.beginWindow(windowId);
    oper.in.process(inMap1);
    oper.in.process(inMap2);
    oper.in.process(inMap3);
    oper.in.process(inMap4);
    try {
        Thread.sleep(1000);
    } catch (Throwable ex) {
        DTThrowable.rethrow(ex);
    }
    oper.endWindow();
    currentId++;
    now = System.currentTimeMillis();
    windowId = (now / 1000) << 32 | currentId;
    oper.beginWindow(windowId);
    oper.endWindow();
    @SuppressWarnings("unchecked") List<Map<String, DimensionObject<String>>> tuples = mapSink.collectedTuples;
    for (Map<String, DimensionObject<String>> map : tuples) {
        for (Entry<String, DimensionObject<String>> entry : map.entrySet()) {
            String key = entry.getKey();
            DimensionObject<String> dimObj = entry.getValue();
            if (key.contains("COUNT")) {
                if (dimObj.getVal().equals("xyz")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(1), dimObj.getCount());
                } else if (dimObj.getVal().equals("abc")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(3), dimObj.getCount());
                } else if (dimObj.getVal().equals("abc,http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(3), dimObj.getCount());
                } else if (dimObj.getVal().equals("xyz,http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(1), dimObj.getCount());
                } else if (dimObj.getVal().equals("http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(4), dimObj.getCount());
                } else {
                    Assert.fail("Unexpected dimension object received: " + dimObj + " for key: " + key);
                }
            } else if (key.contains("AVERAGE")) {
                if (dimObj.getVal().equals("xyz")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else if (dimObj.getVal().equals("abc")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else if (dimObj.getVal().equals("abc,http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else if (dimObj.getVal().equals("xyz,http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else if (dimObj.getVal().equals("http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else {
                    Assert.fail("Unexpected dimension object received: " + dimObj + " for key: " + key);
                }
            } else if (key.contains("SUM")) {
                if (dimObj.getVal().equals("xyz")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else if (dimObj.getVal().equals("abc")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(75), dimObj.getCount());
                } else if (dimObj.getVal().equals("abc,http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(75), dimObj.getCount());
                } else if (dimObj.getVal().equals("xyz,http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(25), dimObj.getCount());
                } else if (dimObj.getVal().equals("http://www.t.co")) {
                    Assert.assertEquals("Count for key " + key, new MutableDouble(100), dimObj.getCount());
                } else {
                    Assert.fail("Unexpected dimension object received: " + dimObj + " for key: " + key);
                }
            } else {
                Assert.fail("Unexpected key received: " + key);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) DimensionObject(org.apache.apex.malhar.lib.logs.DimensionObject) DTThrowable(com.datatorrent.netlet.util.DTThrowable) DimensionObject(org.apache.apex.malhar.lib.logs.DimensionObject) LogstreamPropertyRegistry(com.datatorrent.apps.logstream.PropertyRegistry.LogstreamPropertyRegistry) Map(java.util.Map) HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 24 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class DimensionOperatorUnifier method endWindow.

@Override
public void endWindow() {
    Map<String, DimensionObject<String>> outputAggregationsObject;
    for (Entry<String, Map<String, Map<AggregateOperation, Number>>> keys : unifiedCache.entrySet()) {
        String key = keys.getKey();
        Map<String, Map<AggregateOperation, Number>> dimValues = keys.getValue();
        for (Entry<String, Map<AggregateOperation, Number>> dimValue : dimValues.entrySet()) {
            String dimValueName = dimValue.getKey();
            Map<AggregateOperation, Number> operations = dimValue.getValue();
            outputAggregationsObject = new HashMap<String, DimensionObject<String>>();
            for (Entry<AggregateOperation, Number> operation : operations.entrySet()) {
                AggregateOperation aggrOperationType = operation.getKey();
                Number aggr = operation.getValue();
                String outKey = key + "." + aggrOperationType.name();
                DimensionObject<String> outDimObj = new DimensionObject<String>(new MutableDouble(aggr), dimValueName);
                outputAggregationsObject.put(outKey, outDimObj);
            }
            aggregationsOutput.emit(outputAggregationsObject);
        }
    }
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble) DimensionObject(org.apache.apex.malhar.lib.logs.DimensionObject) Map(java.util.Map) HashMap(java.util.HashMap) AggregateOperation(com.datatorrent.apps.logstream.LogstreamUtil.AggregateOperation)

Example 25 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class LogstreamTopNTest method testOperator.

@Test
@SuppressWarnings("unchecked")
public void testOperator() {
    LogstreamTopN oper = new LogstreamTopN();
    LogstreamPropertyRegistry registry = new LogstreamPropertyRegistry();
    registry.bind(LogstreamUtil.LOG_TYPE, "apache");
    registry.bind(LogstreamUtil.FILTER, "default");
    oper.setRegistry(registry);
    oper.setN(5);
    CollectorTestSink mapSink = new CollectorTestSink();
    oper.top.setSink(mapSink);
    oper.beginWindow(0);
    Map<String, DimensionObject<String>> tuple1 = new HashMap<String, DimensionObject<String>>();
    DimensionObject<String> dimObja = new DimensionObject<String>(new MutableDouble(10), "a");
    tuple1.put("m|201402121900|0|65535|131075|val.COUNT", dimObja);
    oper.data.process(tuple1);
    DimensionObject<String> dimObjb = new DimensionObject<String>(new MutableDouble(1), "b");
    tuple1.put("m|201402121900|0|65535|131075|val.COUNT", dimObjb);
    oper.data.process(tuple1);
    DimensionObject<String> dimObjc = new DimensionObject<String>(new MutableDouble(5), "c");
    tuple1.put("m|201402121900|0|65535|131075|val.COUNT", dimObjc);
    oper.data.process(tuple1);
    DimensionObject<String> dimObjd = new DimensionObject<String>(new MutableDouble(2), "d");
    tuple1.put("m|201402121900|0|65535|131075|val.COUNT", dimObjd);
    oper.data.process(tuple1);
    DimensionObject<String> dimObje = new DimensionObject<String>(new MutableDouble(15), "e");
    tuple1.put("m|201402121900|0|65535|131075|val.COUNT", dimObje);
    oper.data.process(tuple1);
    DimensionObject<String> dimObjf = new DimensionObject<String>(new MutableDouble(4), "f");
    tuple1.put("m|201402121900|0|65535|131075|val.COUNT", dimObjf);
    oper.data.process(tuple1);
    oper.endWindow();
    @SuppressWarnings("unchecked") Map<String, List<DimensionObject<String>>> tuples = (Map<String, List<DimensionObject<String>>>) mapSink.collectedTuples.get(0);
    List<DimensionObject<String>> outList = tuples.get("m|201402121900|0|65535|131075|val.COUNT");
    List<DimensionObject<String>> expectedList = new ArrayList<DimensionObject<String>>();
    expectedList.add(dimObje);
    expectedList.add(dimObja);
    expectedList.add(dimObjc);
    expectedList.add(dimObjf);
    expectedList.add(dimObjd);
    Assert.assertEquals("Size", expectedList.size(), outList.size());
    Assert.assertEquals("compare list", expectedList, outList);
}
Also used : HashMap(java.util.HashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ArrayList(java.util.ArrayList) DimensionObject(org.apache.apex.malhar.lib.logs.DimensionObject) List(java.util.List) ArrayList(java.util.ArrayList) LogstreamPropertyRegistry(com.datatorrent.apps.logstream.PropertyRegistry.LogstreamPropertyRegistry) Map(java.util.Map) HashMap(java.util.HashMap) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Aggregations

MutableDouble (org.apache.commons.lang.mutable.MutableDouble)25 HashMap (java.util.HashMap)17 Map (java.util.Map)16 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 DimensionObject (org.apache.apex.malhar.lib.logs.DimensionObject)4 Account (com.google.gerrit.reviewdb.client.Account)3 LinkedHashMap (java.util.LinkedHashMap)3 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)3 AggregateOperation (com.datatorrent.apps.logstream.LogstreamUtil.AggregateOperation)2 LogstreamPropertyRegistry (com.datatorrent.apps.logstream.PropertyRegistry.LogstreamPropertyRegistry)2 QueryParseException (com.google.gerrit.server.query.QueryParseException)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 List (java.util.List)2 UnifierHashMap (org.apache.apex.malhar.lib.util.UnifierHashMap)2 NumberAggregate (com.datatorrent.common.util.NumberAggregate)1 DTThrowable (com.datatorrent.netlet.util.DTThrowable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1