private void setRuleTypeStatistics(NewIssuesStatistics.Stats stats) { DistributedMetricStatsInt distributedMetricStats = stats.getDistributedMetricStats(RULE_TYPE); setFieldValue(RULE_TYPE + COUNT, String.valueOf(distributedMetricStats.getOnLeak())); Arrays.stream(RuleType.values()) .forEach(ruleType -> setFieldValue( RULE_TYPE + DOT + ruleType + COUNT, String.valueOf(distributedMetricStats.getForLabel(ruleType.name()).map(MetricStatsInt::getOnLeak).orElse(0)))); }
public boolean hasIssues() { return getDistributedMetricStats(RULE_TYPE).getTotal() > 0; }
public Stats(Predicate<DefaultIssue> onLeakPredicate) { this.onLeakPredicate = onLeakPredicate; for (Metric metric : Metric.values()) { if (metric.isComputedByDistribution()) { distributions.put(metric, new DistributedMetricStatsInt()); } } }
@Test public void add_counts_issue_per_assignee_on_leak_globally_and_per_assignee() { List<String> assignees = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); assignees.stream() .map(assignee -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(assignee).setNew(true)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.ASSIGNEE); assignees.forEach(assignee -> assertStats(globalDistribution, assignee, 1, 0, 1)); assignees.forEach(assignee -> { NewIssuesStatistics.Stats stats = underTest.getAssigneesStatistics().get(assignee); DistributedMetricStatsInt assigneeStats = stats.getDistributedMetricStats(Metric.ASSIGNEE); assertThat(assigneeStats.getOnLeak()).isEqualTo(1); assertThat(assigneeStats.getTotal()).isEqualTo(1); assignees.forEach(s -> { Optional<MetricStatsInt> forLabelOpts = assigneeStats.getForLabel(s); if (s.equals(assignee)) { assertThat(forLabelOpts.isPresent()).isTrue(); MetricStatsInt forLabel = forLabelOpts.get(); assertThat(forLabel.getOnLeak()).isEqualTo(1); assertThat(forLabel.getTotal()).isEqualTo(1); } else { assertThat(forLabelOpts.isPresent()).isFalse(); } }); }); }
@Test public void add_does_not_assignee_if_empty_neither_globally_nor_per_assignee() { underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(null).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.ASSIGNEE); assertThat(globalDistribution.getTotal()).isEqualTo(0); assertThat(globalDistribution.getForLabel(null).isPresent()).isFalse(); assertThat(underTest.getAssigneesStatistics()).isEmpty(); }
public boolean hasIssuesOnLeak() { return getDistributedMetricStats(RULE_TYPE).getOnLeak() > 0; }
private void assertStats(DistributedMetricStatsInt distribution, String label, int onLeak, int offLeak, int total) { Optional<MetricStatsInt> statsOption = distribution.getForLabel(label); assertThat(statsOption.isPresent()).describedAs("distribution for label %s not found", label).isTrue(); MetricStatsInt stats = statsOption.get(); assertThat(stats.getOnLeak()).isEqualTo(onLeak); assertThat(stats.getTotal()).isEqualTo(total); }
private static List<Map.Entry<String, MetricStatsInt>> fiveBiggest(DistributedMetricStatsInt distributedMetricStatsInt, ToIntFunction<MetricStatsInt> biggerCriteria) { Comparator<Map.Entry<String, MetricStatsInt>> comparator = Comparator.comparingInt(a -> biggerCriteria.applyAsInt(a.getValue())); return distributedMetricStatsInt.getForLabels() .entrySet() .stream() .sorted(comparator.reversed()) .limit(5) .collect(MoreCollectors.toList(5)); }
public void add(DefaultIssue issue) { boolean isOnLeak = onLeakPredicate.test(issue); distributions.get(RULE_TYPE).increment(issue.type().name(), isOnLeak); String componentUuid = issue.componentUuid(); if (componentUuid != null) { distributions.get(COMPONENT).increment(componentUuid, isOnLeak); } RuleKey ruleKey = issue.ruleKey(); if (ruleKey != null) { distributions.get(RULE).increment(ruleKey.toString(), isOnLeak); } String assigneeUuid = issue.assignee(); if (assigneeUuid != null) { distributions.get(ASSIGNEE).increment(assigneeUuid, isOnLeak); } for (String tag : issue.tags()) { distributions.get(TAG).increment(tag, isOnLeak); } Duration effort = issue.effort(); if (effort != null) { effortStats.add(effort.toMinutes(), isOnLeak); } }
public boolean hasIssuesOffLeak() { return getDistributedMetricStats(RULE_TYPE).getOffLeak() > 0; }
@Test public void add_counts_issue_per_assignee_off_leak_globally_and_per_assignee() { List<String> assignees = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); assignees.stream() .map(assignee -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(assignee).setNew(false)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.ASSIGNEE); assignees.forEach(assignee -> assertStats(globalDistribution, assignee, 0, 1, 1)); assignees.forEach(assignee -> { NewIssuesStatistics.Stats stats = underTest.getAssigneesStatistics().get(assignee); DistributedMetricStatsInt assigneeStats = stats.getDistributedMetricStats(Metric.ASSIGNEE); assertThat(assigneeStats.getOnLeak()).isEqualTo(0); assertThat(assigneeStats.getTotal()).isEqualTo(1); assignees.forEach(s -> { Optional<MetricStatsInt> forLabelOpts = assigneeStats.getForLabel(s); if (s.equals(assignee)) { assertThat(forLabelOpts.isPresent()).isTrue(); MetricStatsInt forLabel = forLabelOpts.get(); assertThat(forLabel.getOnLeak()).isEqualTo(0); assertThat(forLabel.getTotal()).isEqualTo(1); } else { assertThat(forLabelOpts.isPresent()).isFalse(); } }); }); }
@Test public void add_does_not_count_component_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setComponentUuid(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.COMPONENT); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
public NewIssuesNotification setStatistics(String projectName, NewIssuesStatistics.Stats stats) { setDefaultMessage(stats.getDistributedMetricStats(RULE_TYPE).getOnLeak() + " new issues on " + projectName + ".\n"); try (DbSession dbSession = dbClient.openSession(false)) { setRuleTypeStatistics(stats); setAssigneesStatistics(dbSession, stats); setTagsStatistics(stats); setComponentsStatistics(dbSession, stats); setRuleStatistics(dbSession, stats); } return this; }
@CheckForNull private Integer countDistributionTotal(Metric metric, String label) { return underTest.globalStatistics() .getDistributedMetricStats(metric) .getForLabel(label) .map(MetricStatsInt::getTotal) .orElse(null); }
private static List<Map.Entry<String, MetricStatsInt>> fiveBiggest(DistributedMetricStatsInt distributedMetricStatsInt, ToIntFunction<MetricStatsInt> biggerCriteria) { Comparator<Map.Entry<String, MetricStatsInt>> comparator = Comparator.comparingInt(a -> biggerCriteria.applyAsInt(a.getValue())); return distributedMetricStatsInt.getForLabels() .entrySet() .stream() .sorted(comparator.reversed()) .limit(5) .collect(MoreCollectors.toList(5)); }
public void add(DefaultIssue issue) { boolean isOnLeak = onLeakPredicate.test(issue); distributions.get(RULE_TYPE).increment(issue.type().name(), isOnLeak); String componentUuid = issue.componentUuid(); if (componentUuid != null) { distributions.get(COMPONENT).increment(componentUuid, isOnLeak); } RuleKey ruleKey = issue.ruleKey(); if (ruleKey != null) { distributions.get(RULE).increment(ruleKey.toString(), isOnLeak); } String assigneeUuid = issue.assignee(); if (assigneeUuid != null) { distributions.get(ASSIGNEE).increment(assigneeUuid, isOnLeak); } for (String tag : issue.tags()) { distributions.get(TAG).increment(tag, isOnLeak); } Duration effort = issue.effort(); if (effort != null) { effortStats.add(effort.toMinutes(), isOnLeak); } }
@Test public void add_does_not_count_tags_if_empty_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(Collections.emptyList()).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
private void setRuleTypeStatistics(NewIssuesStatistics.Stats stats) { DistributedMetricStatsInt distributedMetricStats = stats.getDistributedMetricStats(RULE_TYPE); setFieldValue(RULE_TYPE + COUNT, String.valueOf(distributedMetricStats.getOnLeak())); Arrays.stream(RuleType.values()) .forEach(ruleType -> setFieldValue( RULE_TYPE + DOT + ruleType + COUNT, String.valueOf(distributedMetricStats.getForLabel(ruleType.name()).map(MetricStatsInt::getOnLeak).orElse(0)))); }
public boolean hasIssuesOnLeak() { return getDistributedMetricStats(RULE_TYPE).getOnLeak() > 0; }
public boolean hasIssues() { return getDistributedMetricStats(RULE_TYPE).getTotal() > 0; }