Search in sources :

Example 1 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project gerrit by GerritCodeReview.

the class ReviewerRecommender method baseRankingForEmptyQuery.

private Map<Account.Id, MutableDouble> baseRankingForEmptyQuery(double baseWeight) throws OrmException {
    // Get the user's last 25 changes, check approvals
    try {
        List<ChangeData> result = internalChangeQuery.setLimit(25).setRequestedFields(ImmutableSet.of(ChangeField.REVIEWER.getName())).query(changeQueryBuilder.owner("self"));
        Map<Account.Id, MutableDouble> suggestions = new HashMap<>();
        for (ChangeData cd : result) {
            for (PatchSetApproval approval : cd.currentApprovals()) {
                Account.Id id = approval.getAccountId();
                if (suggestions.containsKey(id)) {
                    suggestions.get(id).add(baseWeight);
                } else {
                    suggestions.put(id, new MutableDouble(baseWeight));
                }
            }
        }
        return suggestions;
    } catch (QueryParseException e) {
        // Unhandled, because owner:self will never provoke a QueryParseException
        log.error("Exception while suggesting reviewers", e);
        return ImmutableMap.of();
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 2 with MutableDouble

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

the class MultiWindowDimensionAggregation method endWindow.

@Override
public void endWindow() {
    int totalWindowsOccupied = cacheOject.size();
    for (Map.Entry<String, Map<String, KeyValPair<MutableDouble, Integer>>> e : outputMap.entrySet()) {
        for (Map.Entry<String, KeyValPair<MutableDouble, Integer>> dimensionValObj : e.getValue().entrySet()) {
            Map<String, DimensionObject<String>> outputData = new HashMap<String, DimensionObject<String>>();
            KeyValPair<MutableDouble, Integer> keyVal = dimensionValObj.getValue();
            if (operationType == AggregateOperation.SUM) {
                outputData.put(e.getKey(), new DimensionObject<String>(keyVal.getKey(), dimensionValObj.getKey()));
            } else if (operationType == AggregateOperation.AVERAGE) {
                if (keyVal.getValue() != 0) {
                    double totalCount = ((double) (totalWindowsOccupied * applicationWindowSize)) / 1000;
                    outputData.put(e.getKey(), new DimensionObject<String>(new MutableDouble(keyVal.getKey().doubleValue() / totalCount), dimensionValObj.getKey()));
                }
            }
            if (!outputData.isEmpty()) {
                output.emit(outputData);
            }
        }
    }
    currentWindow = (currentWindow + 1) % windowSize;
}
Also used : HashMap(java.util.HashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MutableDouble

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

the class MarginKeyVal method addTuple.

/**
 * Adds the value for each key.
 *
 * @param tuple
 * @param map
 */
public void addTuple(KeyValPair<K, V> tuple, Map<K, MutableDouble> map) {
    K key = tuple.getKey();
    if (!doprocessKey(key) || (tuple.getValue() == null)) {
        return;
    }
    MutableDouble val = map.get(key);
    if (val == null) {
        val = new MutableDouble(0.0);
        map.put(cloneKey(key), val);
    }
    val.add(tuple.getValue().doubleValue());
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble)

Example 4 with MutableDouble

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

the class DimensionOperator method doComputations.

/**
 * Does computations for the given dimension and its value names on the given value key name
 *
 * @param timeBucket time bucket
 * @param dimensionCombinationId id of dimension combination
 * @param dimValueName values of the dimension combination
 * @param valueKeyName name of the value key on which operations are performed
 * @param value value of the value key
 */
private void doComputations(String timeBucket, Integer dimensionCombinationId, String dimValueName, String valueKeyName, Number value) {
    StringBuilder sb = new StringBuilder();
    sb.append(timeBucket).append("|").append(recordType.get(LogstreamUtil.LOG_TYPE)).append("|").append(recordType.get(LogstreamUtil.FILTER)).append("|").append(dimensionCombinationId).append("|").append(valueKeyName);
    // final key format --> timebucket|type|filter|dimId|val
    // eg: m|201311230108|1|4|10|bytes
    String key = sb.toString();
    Map<AggregateOperation, Number> aggregations;
    if (cacheObject.containsKey(key)) {
        Map<String, Map<AggregateOperation, Number>> dimValueNames = cacheObject.get(key);
        if (dimValueNames.containsKey(dimValueName)) {
            aggregations = dimValueNames.get(dimValueName);
        } else {
            aggregations = new HashMap<AggregateOperation, Number>();
            for (AggregateOperation aggregationType : valueOperationTypes.get(valueKeyName)) {
                aggregations.put(aggregationType, new MutableDouble(0));
            }
            dimValueNames.put(dimValueName, aggregations);
        }
    } else {
        Map<String, Map<AggregateOperation, Number>> newDimValueNames = new HashMap<String, Map<AggregateOperation, Number>>();
        aggregations = new HashMap<AggregateOperation, Number>();
        for (AggregateOperation aggregationType : valueOperationTypes.get(valueKeyName)) {
            aggregations.put(aggregationType, new MutableDouble(0));
        }
        newDimValueNames.put(dimValueName, aggregations);
        cacheObject.put(key, newDimValueNames);
    }
    if (aggregations.containsKey(AggregateOperation.SUM)) {
        MutableDouble aggrVal = (MutableDouble) aggregations.get(AggregateOperation.SUM);
        aggrVal.add(value);
    }
    if (aggregations.containsKey(AggregateOperation.COUNT)) {
        MutableDouble aggrVal = (MutableDouble) aggregations.get(AggregateOperation.COUNT);
        aggrVal.add(1);
    }
    if (aggregations.containsKey(AggregateOperation.AVERAGE)) {
        double avgVal = aggregations.get(AggregateOperation.AVERAGE).doubleValue();
        double countVal = aggregations.get(AggregateOperation.COUNT).doubleValue();
        double newAvg = ((avgVal * (countVal - 1)) + value.doubleValue()) / countVal;
        aggregations.put(AggregateOperation.AVERAGE, new MutableDouble(newAvg));
    }
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble) AggregateOperation(com.datatorrent.apps.logstream.LogstreamUtil.AggregateOperation)

Example 5 with MutableDouble

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

the class DimensionOperatorUnifierTest method testOperator.

@Test
@SuppressWarnings("unchecked")
public void testOperator() {
    DimensionOperatorUnifier unifier = new DimensionOperatorUnifier();
    CollectorTestSink sink = new CollectorTestSink();
    unifier.aggregationsOutput.setSink(sink);
    unifier.beginWindow(1);
    Map<String, DimensionObject<String>> tuple1 = new HashMap<String, DimensionObject<String>>();
    tuple1.put("m|201402121900|0|65537|131074|bytes.AVERAGE", new DimensionObject<String>(new MutableDouble(75), "a"));
    tuple1.put("m|201402121900|0|65537|131074|bytes.COUNT", new DimensionObject<String>(new MutableDouble(3.0), "a"));
    tuple1.put("m|201402121900|0|65537|131074|bytes.SUM", new DimensionObject<String>(new MutableDouble(225), "a"));
    Map<String, DimensionObject<String>> tuple2 = new HashMap<String, DimensionObject<String>>();
    tuple2.put("m|201402121900|0|65537|131074|bytes.AVERAGE", new DimensionObject<String>(new MutableDouble(50), "a"));
    tuple2.put("m|201402121900|0|65537|131074|bytes.COUNT", new DimensionObject<String>(new MutableDouble(2.0), "a"));
    tuple2.put("m|201402121900|0|65537|131074|bytes.SUM", new DimensionObject<String>(new MutableDouble(100), "a"));
    Map<String, DimensionObject<String>> tuple3 = new HashMap<String, DimensionObject<String>>();
    tuple3.put("m|201402121900|0|65537|131074|bytes.AVERAGE", new DimensionObject<String>(new MutableDouble(50), "z"));
    tuple3.put("m|201402121900|0|65537|131074|bytes.COUNT", new DimensionObject<String>(new MutableDouble(2.0), "z"));
    tuple3.put("m|201402121900|0|65537|131074|bytes.SUM", new DimensionObject<String>(new MutableDouble(100), "z"));
    Map<String, DimensionObject<String>> tuple4 = new HashMap<String, DimensionObject<String>>();
    tuple4.put("m|201402121900|0|65537|131075|bytes.AVERAGE", new DimensionObject<String>(new MutableDouble(14290.5), "b"));
    tuple4.put("m|201402121900|0|65537|131075|bytes.COUNT", new DimensionObject<String>(new MutableDouble(2.0), "b"));
    tuple4.put("m|201402121900|0|65537|131075|bytes.SUM", new DimensionObject<String>(new MutableDouble(28581.0), "b"));
    Map<String, DimensionObject<String>> tuple5 = new HashMap<String, DimensionObject<String>>();
    tuple5.put("m|201402121900|0|65537|131076|bytes.AVERAGE", new DimensionObject<String>(new MutableDouble(290.75), "c"));
    tuple5.put("m|201402121900|0|65537|131076|bytes.COUNT", new DimensionObject<String>(new MutableDouble(10.0), "c"));
    tuple5.put("m|201402121900|0|65537|131076|bytes.SUM", new DimensionObject<String>(new MutableDouble(8581.0), "c"));
    unifier.process(tuple1);
    unifier.process(tuple2);
    unifier.process(tuple3);
    unifier.process(tuple4);
    unifier.process(tuple5);
    unifier.endWindow();
    @SuppressWarnings("unchecked") List<Map<String, DimensionObject<String>>> tuples = sink.collectedTuples;
    Assert.assertEquals("Tuple Count", 4, tuples.size());
    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.equals("m|201402121900|0|65537|131074|bytes.AVERAGE") && dimObj.getVal().equals("a")) {
                Assert.assertEquals("average for key " + key + " and dimension key " + "a", new MutableDouble(65), dimObj.getCount());
            }
            if (key.equals("m|201402121900|0|65537|131074|bytes.SUM") && dimObj.getVal().equals("z")) {
                Assert.assertEquals("sum for key " + key + " and dimension key " + "z", new MutableDouble(100), dimObj.getCount());
            }
            if (key.equals("m|201402121900|0|65537|131076|bytes.COUNT") && dimObj.getVal().equals("c")) {
                Assert.assertEquals("count for key " + key + " and dimension key " + "c", new MutableDouble(10), dimObj.getCount());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) DimensionObject(org.apache.apex.malhar.lib.logs.DimensionObject) 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