Search in sources :

Example 11 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)

Example 12 with MutableDouble

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

the class ReviewerRecommender method suggestReviewers.

public List<Account.Id> suggestReviewers(ChangeNotes changeNotes, SuggestReviewers suggestReviewers, ProjectControl projectControl, List<Account.Id> candidateList) throws OrmException {
    String query = suggestReviewers.getQuery();
    double baseWeight = config.getInt("addReviewer", "baseWeight", 1);
    Map<Account.Id, MutableDouble> reviewerScores;
    if (Strings.isNullOrEmpty(query)) {
        reviewerScores = baseRankingForEmptyQuery(baseWeight);
    } else {
        reviewerScores = baseRankingForCandidateList(candidateList, projectControl, baseWeight);
    }
    // Send the query along with a candidate list to all plugins and merge the
    // results. Plugins don't necessarily need to use the candidates list, they
    // can also return non-candidate account ids.
    List<Callable<Set<SuggestedReviewer>>> tasks = new ArrayList<>(reviewerSuggestionPluginMap.plugins().size());
    List<Double> weights = new ArrayList<>(reviewerSuggestionPluginMap.plugins().size());
    for (DynamicMap.Entry<ReviewerSuggestion> plugin : reviewerSuggestionPluginMap) {
        tasks.add(() -> plugin.getProvider().get().suggestReviewers(projectControl.getProject().getNameKey(), changeNotes.getChangeId(), query, reviewerScores.keySet()));
        String pluginWeight = config.getString("addReviewer", plugin.getPluginName() + "-" + plugin.getExportName(), "weight");
        if (Strings.isNullOrEmpty(pluginWeight)) {
            pluginWeight = "1";
        }
        try {
            weights.add(Double.parseDouble(pluginWeight));
        } catch (NumberFormatException e) {
            log.error("Exception while parsing weight for " + plugin.getPluginName() + "-" + plugin.getExportName(), e);
            weights.add(1d);
        }
    }
    try {
        List<Future<Set<SuggestedReviewer>>> futures = workQueue.getDefaultQueue().invokeAll(tasks, PLUGIN_QUERY_TIMEOUT, TimeUnit.MILLISECONDS);
        Iterator<Double> weightIterator = weights.iterator();
        for (Future<Set<SuggestedReviewer>> f : futures) {
            double weight = weightIterator.next();
            for (SuggestedReviewer s : f.get()) {
                if (reviewerScores.containsKey(s.account)) {
                    reviewerScores.get(s.account).add(s.score * weight);
                } else {
                    reviewerScores.put(s.account, new MutableDouble(s.score * weight));
                }
            }
        }
    } catch (ExecutionException | InterruptedException e) {
        log.error("Exception while suggesting reviewers", e);
        return ImmutableList.of();
    }
    if (changeNotes != null) {
        // Remove change owner
        reviewerScores.remove(changeNotes.getChange().getOwner());
        // Remove existing reviewers
        reviewerScores.keySet().removeAll(approvalsUtil.getReviewers(dbProvider.get(), changeNotes).byState(REVIEWER));
    }
    // Sort results
    Stream<Entry<Account.Id, MutableDouble>> sorted = reviewerScores.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
    List<Account.Id> sortedSuggestions = sorted.map(Map.Entry::getKey).collect(toList());
    return sortedSuggestions;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ArrayList(java.util.ArrayList) ReviewerSuggestion(com.google.gerrit.server.change.ReviewerSuggestion) Callable(java.util.concurrent.Callable) Entry(java.util.Map.Entry) ExecutionException(java.util.concurrent.ExecutionException) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) SuggestedReviewer(com.google.gerrit.server.change.SuggestedReviewer) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Example 13 with MutableDouble

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

the class ReviewerRecommender method baseRankingForCandidateList.

private Map<Account.Id, MutableDouble> baseRankingForCandidateList(List<Account.Id> candidates, ProjectControl projectControl, double baseWeight) throws OrmException {
    // Get each reviewer's activity based on number of applied labels
    // (weighted 10d), number of comments (weighted 0.5d) and number of owned
    // changes (weighted 1d).
    Map<Account.Id, MutableDouble> reviewers = new LinkedHashMap<>();
    if (candidates.size() == 0) {
        return reviewers;
    }
    List<Predicate<ChangeData>> predicates = new ArrayList<>();
    for (Account.Id id : candidates) {
        try {
            Predicate<ChangeData> projectQuery = changeQueryBuilder.project(projectControl.getProject().getName());
            // Get all labels for this project and create a compound OR query to
            // fetch all changes where users have applied one of these labels
            List<LabelType> labelTypes = projectControl.getLabelTypes().getLabelTypes();
            List<Predicate<ChangeData>> labelPredicates = new ArrayList<>(labelTypes.size());
            for (LabelType type : labelTypes) {
                labelPredicates.add(changeQueryBuilder.label(type.getName() + ",user=" + id));
            }
            Predicate<ChangeData> reviewerQuery = Predicate.and(projectQuery, Predicate.or(labelPredicates));
            Predicate<ChangeData> ownerQuery = Predicate.and(projectQuery, changeQueryBuilder.owner(id.toString()));
            Predicate<ChangeData> commentedByQuery = Predicate.and(projectQuery, changeQueryBuilder.commentby(id.toString()));
            predicates.add(reviewerQuery);
            predicates.add(ownerQuery);
            predicates.add(commentedByQuery);
            reviewers.put(id, new MutableDouble());
        } catch (QueryParseException e) {
            // Unhandled: If an exception is thrown, we won't increase the
            // candidates's score
            log.error("Exception while suggesting reviewers", e);
        }
    }
    List<List<ChangeData>> result = internalChangeQuery.setLimit(25).setRequestedFields(ImmutableSet.of()).query(predicates);
    Iterator<List<ChangeData>> queryResultIterator = result.iterator();
    Iterator<Account.Id> reviewersIterator = reviewers.keySet().iterator();
    int i = 0;
    Account.Id currentId = null;
    while (queryResultIterator.hasNext()) {
        List<ChangeData> currentResult = queryResultIterator.next();
        if (i % WEIGHTS.length == 0) {
            currentId = reviewersIterator.next();
        }
        reviewers.get(currentId).add(WEIGHTS[i % WEIGHTS.length] * baseWeight * currentResult.size());
        i++;
    }
    return reviewers;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ArrayList(java.util.ArrayList) ChangeData(com.google.gerrit.server.query.change.ChangeData) LinkedHashMap(java.util.LinkedHashMap) Predicate(com.google.gerrit.server.query.Predicate) QueryParseException(com.google.gerrit.server.query.QueryParseException) LabelType(com.google.gerrit.common.data.LabelType) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List)

Example 14 with MutableDouble

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

the class MarginKeyVal 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
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void endWindow() {
    Double val;
    for (Map.Entry<K, MutableDouble> e : denominators.entrySet()) {
        K key = e.getKey();
        MutableDouble nval = numerators.get(key);
        if (nval == null) {
            nval = new MutableDouble(0.0);
        } else {
            // so that all left over keys can be reported
            numerators.remove(key);
        }
        if (percent) {
            val = (1 - nval.doubleValue() / e.getValue().doubleValue()) * 100;
        } else {
            val = 1 - nval.doubleValue() / e.getValue().doubleValue();
        }
        margin.emit(new KeyValPair(key, getValue(val.doubleValue())));
    }
    numerators.clear();
    denominators.clear();
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with MutableDouble

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

the class MarginMap method addTuple.

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

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