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());
}
}
}
}
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;
}
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;
}
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();
}
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());
}
}
Aggregations