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)))); }
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); }
@CheckForNull private Integer countDistributionTotal(Metric metric, String label) { return underTest.globalStatistics() .getDistributedMetricStats(metric) .getForLabel(label) .map(MetricStatsInt::getTotal) .orElse(null); }
@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(); }); }
@Test public void add_does_not_count_ruleKey_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setRuleKey(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
@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(); }); }
@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(); }
@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(); } }); }); }
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)))); }