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); }
DistributedMetricStatsInt increment(String label, boolean onLeak) { this.globalStats.increment(onLeak); statsPerLabel.computeIfAbsent(label, l -> new MetricStatsInt()).increment(onLeak); return this; }
public int getOnLeak() { return globalStats.getOnLeak(); }
public int getTotal() { return globalStats.getTotal(); }
public int getOffLeak() { return globalStats.getOffLeak(); }
private void setAssigneesStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) { Metric metric = Metric.ASSIGNEE; List<Map.Entry<String, MetricStatsInt>> entries = fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak); Set<String> assigneeUuids = entries.stream().map(Map.Entry::getKey).filter(Objects::nonNull).collect(toSet()); Map<String, UserDto> userDtoByUuid = dbClient.userDao().selectByUuids(dbSession, assigneeUuids).stream().collect(toMap(UserDto::getUuid, u -> u)); int i = 1; for (Map.Entry<String, MetricStatsInt> assigneeStats : entries) { String assigneeUuid = assigneeStats.getKey(); UserDto user = userDtoByUuid.get(assigneeUuid); String name = user == null ? assigneeUuid : user.getName(); setFieldValue(metric + DOT + i + LABEL, name); setFieldValue(metric + DOT + i + COUNT, String.valueOf(assigneeStats.getValue().getOnLeak())); i++; } }
public int getTotal() { return globalStats.getTotal(); }
@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 setTagsStatistics(NewIssuesStatistics.Stats stats) { Metric metric = Metric.TAG; int i = 1; for (Map.Entry<String, MetricStatsInt> tagStats : fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak)) { setFieldValue(metric + DOT + i + COUNT, String.valueOf(tagStats.getValue().getOnLeak())); setFieldValue(metric + DOT + i + LABEL, tagStats.getKey()); i++; } }
DistributedMetricStatsInt increment(String label, boolean onLeak) { this.globalStats.increment(onLeak); statsPerLabel.computeIfAbsent(label, l -> new MetricStatsInt()).increment(onLeak); return this; }
@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(); } }); }); }
private void setComponentsStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) { Metric metric = Metric.COMPONENT; int i = 1; List<Map.Entry<String, MetricStatsInt>> fiveBiggest = fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak); Set<String> componentUuids = fiveBiggest .stream() .map(Map.Entry::getKey) .collect(MoreCollectors.toSet(fiveBiggest.size())); Map<String, ComponentDto> componentDtosByUuid = dbClient.componentDao().selectByUuids(dbSession, componentUuids) .stream() .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); for (Map.Entry<String, MetricStatsInt> componentStats : fiveBiggest) { String uuid = componentStats.getKey(); String componentName = Optional.ofNullable(componentDtosByUuid.get(uuid)) .map(ComponentDto::name) .orElseThrow(() -> new RowNotFoundException(String.format("Component with uuid '%s' not found", uuid))); setFieldValue(metric + DOT + i + LABEL, componentName); setFieldValue(metric + DOT + i + COUNT, String.valueOf(componentStats.getValue().getOnLeak())); i++; } }
private void setRuleStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) { Metric metric = Metric.RULE; List<Map.Entry<String, MetricStatsInt>> fiveBiggest = fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak); Set<RuleKey> ruleKeys = fiveBiggest .stream() .map(Map.Entry::getKey) .map(RuleKey::parse) .collect(MoreCollectors.toSet(fiveBiggest.size())); Map<String, RuleDefinitionDto> ruleByRuleKey = dbClient.ruleDao().selectDefinitionByKeys(dbSession, ruleKeys) .stream() .collect(MoreCollectors.uniqueIndex(s -> s.getKey().toString())); int i = 1; for (Map.Entry<String, MetricStatsInt> ruleStats : fiveBiggest) { String ruleKey = ruleStats.getKey(); RuleDefinitionDto rule = Optional.ofNullable(ruleByRuleKey.get(ruleKey)) .orElseThrow(() -> new RowNotFoundException(String.format("Rule with key '%s' does not exist", ruleKey))); String name = rule.getName() + " (" + rule.getLanguage() + ")"; setFieldValue(metric + DOT + i + LABEL, name); setFieldValue(metric + DOT + i + COUNT, String.valueOf(ruleStats.getValue().getOnLeak())); i++; } }
public int getOnLeak() { return globalStats.getOnLeak(); }
private void setTagsStatistics(NewIssuesStatistics.Stats stats) { Metric metric = Metric.TAG; int i = 1; for (Map.Entry<String, MetricStatsInt> tagStats : fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak)) { setFieldValue(metric + DOT + i + COUNT, String.valueOf(tagStats.getValue().getOnLeak())); setFieldValue(metric + DOT + i + LABEL, tagStats.getKey()); i++; } }
private void setAssigneesStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) { Metric metric = Metric.ASSIGNEE; List<Map.Entry<String, MetricStatsInt>> entries = fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak); Set<String> assigneeUuids = entries.stream().map(Map.Entry::getKey).filter(Objects::nonNull).collect(toSet()); Map<String, UserDto> userDtoByUuid = dbClient.userDao().selectByUuids(dbSession, assigneeUuids).stream().collect(toMap(UserDto::getUuid, u -> u)); int i = 1; for (Map.Entry<String, MetricStatsInt> assigneeStats : entries) { String assigneeUuid = assigneeStats.getKey(); UserDto user = userDtoByUuid.get(assigneeUuid); String name = user == null ? assigneeUuid : user.getName(); setFieldValue(metric + DOT + i + LABEL, name); setFieldValue(metric + DOT + i + COUNT, String.valueOf(assigneeStats.getValue().getOnLeak())); i++; } }
private void setComponentsStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) { Metric metric = Metric.COMPONENT; int i = 1; List<Map.Entry<String, MetricStatsInt>> fiveBiggest = fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak); Set<String> componentUuids = fiveBiggest .stream() .map(Map.Entry::getKey) .collect(MoreCollectors.toSet(fiveBiggest.size())); Map<String, ComponentDto> componentDtosByUuid = dbClient.componentDao().selectByUuids(dbSession, componentUuids) .stream() .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); for (Map.Entry<String, MetricStatsInt> componentStats : fiveBiggest) { String uuid = componentStats.getKey(); String componentName = Optional.ofNullable(componentDtosByUuid.get(uuid)) .map(ComponentDto::name) .orElseThrow(() -> new RowNotFoundException(String.format("Component with uuid '%s' not found", uuid))); setFieldValue(metric + DOT + i + LABEL, componentName); setFieldValue(metric + DOT + i + COUNT, String.valueOf(componentStats.getValue().getOnLeak())); i++; } }
private void setRuleStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) { Metric metric = Metric.RULE; List<Map.Entry<String, MetricStatsInt>> fiveBiggest = fiveBiggest(stats.getDistributedMetricStats(metric), MetricStatsInt::getOnLeak); Set<RuleKey> ruleKeys = fiveBiggest .stream() .map(Map.Entry::getKey) .map(RuleKey::parse) .collect(MoreCollectors.toSet(fiveBiggest.size())); Map<String, RuleDefinitionDto> ruleByRuleKey = dbClient.ruleDao().selectDefinitionByKeys(dbSession, ruleKeys) .stream() .collect(MoreCollectors.uniqueIndex(s -> s.getKey().toString())); int i = 1; for (Map.Entry<String, MetricStatsInt> ruleStats : fiveBiggest) { String ruleKey = ruleStats.getKey(); RuleDefinitionDto rule = Optional.ofNullable(ruleByRuleKey.get(ruleKey)) .orElseThrow(() -> new RowNotFoundException(String.format("Rule with key '%s' does not exist", ruleKey))); String name = rule.getName() + " (" + rule.getLanguage() + ")"; setFieldValue(metric + DOT + i + LABEL, name); setFieldValue(metric + DOT + i + COUNT, String.valueOf(ruleStats.getValue().getOnLeak())); i++; } }