Search in sources :

Example 1 with ReviewerSuggestion

use of com.google.gerrit.server.change.ReviewerSuggestion 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)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)1 Account (com.google.gerrit.reviewdb.client.Account)1 ReviewerSuggestion (com.google.gerrit.server.change.ReviewerSuggestion)1 SuggestedReviewer (com.google.gerrit.server.change.SuggestedReviewer)1 ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 MutableDouble (org.apache.commons.lang.mutable.MutableDouble)1