import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.HasAggregations;
-import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
.collect(MoreCollectors.toList(projectUuids.size()));
}
- public List<PrStatistics> searchBranchStatistics(String projectUuid, List<String> branchUuids) {
- if (branchUuids.isEmpty()) {
- return Collections.emptyList();
- }
-
- SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
- .query(
- boolQuery()
- .must(termsQuery(FIELD_ISSUE_BRANCH_UUID, branchUuids))
- .mustNot(existsQuery(FIELD_ISSUE_RESOLUTION))
- .must(termQuery(FIELD_ISSUE_IS_MAIN_BRANCH, Boolean.toString(false))))
- .size(0)
- .aggregation(AggregationBuilders.terms("branchUuids")
- .field(FIELD_ISSUE_BRANCH_UUID)
- .size(branchUuids.size())
- .subAggregation(AggregationBuilders.terms("types")
- .field(FIELD_ISSUE_TYPE)));
-
- SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType())
- .routing(AuthorizationDoc.idOf(projectUuid));
-
- requestBuilder.source(sourceBuilder);
- SearchResponse response = client.search(requestBuilder);
- return ((ParsedStringTerms) response.getAggregations().get("branchUuids")).getBuckets().stream()
- .map(bucket -> new PrStatistics(bucket.getKeyAsString(),
- ((ParsedStringTerms) bucket.getAggregations().get("types")).getBuckets()
- .stream()
- .collect(uniqueIndex(MultiBucketsAggregation.Bucket::getKeyAsString, MultiBucketsAggregation.Bucket::getDocCount))))
- .collect(MoreCollectors.toList(branchUuids.size()));
- }
-
/**
* @deprecated SansTop25 report is outdated and will be removed in future versions
*/
import com.google.common.collect.Iterators;
import java.util.ArrayList;
import java.util.List;
-import java.util.stream.IntStream;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.TotalHits.Relation;
-import org.assertj.core.groups.Tuple;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
-import static org.assertj.core.api.Assertions.tuple;
-import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
-import static org.sonar.api.rules.RuleType.BUG;
-import static org.sonar.api.rules.RuleType.CODE_SMELL;
-import static org.sonar.api.rules.RuleType.VULNERABILITY;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.db.user.GroupTesting.newGroupDto;
String key = "I" + i;
issues.add(newDoc(key, file));
}
- indexIssues(issues.toArray(new IssueDoc[]{}));
+ indexIssues(issues.toArray(new IssueDoc[] {}));
IssueQuery.Builder query = IssueQuery.builder();
SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(500));
String key = "I" + i;
issues.add(newDoc(key, file));
}
- indexIssues(issues.toArray(new IssueDoc[]{}));
+ indexIssues(issues.toArray(new IssueDoc[] {}));
IssueQuery.Builder query = IssueQuery.builder();
SearchResponse result = underTest.search(query.build(), new SearchOptions().setLimit(500));
ComponentDto project = newPrivateProjectDto();
ComponentDto file = newFileDto(project, null);
List<IssueDoc> issues = new ArrayList<>();
- //we are adding issues in reverse order to see if the sort is actually doing anything
+ // we are adding issues in reverse order to see if the sort is actually doing anything
for (int i = 9; i >= 1; i--) {
String key = "I" + i;
issues.add(newDoc(key, file));
}
- indexIssues(issues.toArray(new IssueDoc[]{}));
+ indexIssues(issues.toArray(new IssueDoc[] {}));
IssueQuery.Builder query = IssueQuery.builder().asc(true);
SearchResponse result = underTest.search(query.sort(IssueQuery.SORT_BY_CREATION_DATE).build(), new SearchOptions());
ComponentDto project = newPrivateProjectDto();
ComponentDto file = newFileDto(project, null);
List<IssueDoc> issues = new ArrayList<>();
- //we are adding issues in reverse order to see if the sort is actually doing anything
+ // we are adding issues in reverse order to see if the sort is actually doing anything
for (int i = 9; i >= 1; i--) {
String key = "I" + i;
issues.add(newDoc(key, file));
}
- indexIssues(issues.toArray(new IssueDoc[]{}));
+ indexIssues(issues.toArray(new IssueDoc[] {}));
IssueQuery.Builder query = IssueQuery.builder().asc(true);
SearchResponse result = underTest.search(query.sort(IssueQuery.SORT_BY_CREATION_DATE).build(), new SearchOptions());
assertThat(underTest.countTags(projectQuery("other"), 10)).isEmpty();
}
- @Test
- public void searchBranchStatistics() {
- ComponentDto project = db.components().insertPublicProject();
- ComponentDto branch1 = db.components().insertProjectBranch(project);
- ComponentDto branch2 = db.components().insertProjectBranch(project);
- ComponentDto branch3 = db.components().insertProjectBranch(project);
- ComponentDto fileOnBranch3 = db.components().insertComponent(newFileDto(branch3));
- indexIssues(newDoc(project),
- newDoc(branch1).setType(BUG).setResolution(null), newDoc(branch1).setType(VULNERABILITY).setResolution(null), newDoc(branch1).setType(CODE_SMELL).setResolution(null),
- newDoc(branch1).setType(CODE_SMELL).setResolution(RESOLUTION_FIXED),
- newDoc(branch3).setType(CODE_SMELL).setResolution(null), newDoc(branch3).setType(CODE_SMELL).setResolution(null),
- newDoc(fileOnBranch3).setType(CODE_SMELL).setResolution(null), newDoc(fileOnBranch3).setType(CODE_SMELL).setResolution(RESOLUTION_FIXED));
-
- List<PrStatistics> prStatistics = underTest.searchBranchStatistics(project.uuid(), asList(branch1.uuid(), branch2.uuid(), branch3.uuid()));
-
- assertThat(prStatistics).extracting(PrStatistics::getBranchUuid, PrStatistics::getBugs, PrStatistics::getVulnerabilities, PrStatistics::getCodeSmells)
- .containsExactlyInAnyOrder(
- tuple(branch1.uuid(), 1L, 1L, 1L),
- tuple(branch3.uuid(), 0L, 0L, 3L));
- }
-
- @Test
- public void searchBranchStatistics_on_many_branches() {
- ComponentDto project = db.components().insertPublicProject();
- List<String> branchUuids = new ArrayList<>();
- List<Tuple> expectedResult = new ArrayList<>();
- IntStream.range(0, 15).forEach(i -> {
- ComponentDto branch = db.components().insertProjectBranch(project);
- addIssues(branch, 1 + i, 2 + i, 3 + i);
- expectedResult.add(tuple(branch.uuid(), 1L + i, 2L + i, 3L + i));
- branchUuids.add(branch.uuid());
- });
-
- List<PrStatistics> prStatistics = underTest.searchBranchStatistics(project.uuid(), branchUuids);
-
- assertThat(prStatistics)
- .extracting(PrStatistics::getBranchUuid, PrStatistics::getBugs, PrStatistics::getVulnerabilities, PrStatistics::getCodeSmells)
- .hasSize(15)
- .containsAll(expectedResult);
- }
-
- @Test
- public void searchBranchStatistics_on_empty_list() {
- ComponentDto project = db.components().insertPublicProject();
-
- assertThat(underTest.searchBranchStatistics(project.uuid(), emptyList())).isEmpty();
- assertThat(underTest.searchBranchStatistics(project.uuid(), singletonList("unknown"))).isEmpty();
- }
-
- private void addIssues(ComponentDto component, int bugs, int vulnerabilities, int codeSmelles) {
- List<IssueDoc> issues = new ArrayList<>();
- IntStream.range(0, bugs).forEach(b -> issues.add(newDoc(component).setType(BUG).setResolution(null)));
- IntStream.range(0, vulnerabilities).forEach(v -> issues.add(newDoc(component).setType(VULNERABILITY).setResolution(null)));
- IntStream.range(0, codeSmelles).forEach(c -> issues.add(newDoc(component).setType(CODE_SMELL).setResolution(null)));
- indexIssues(issues.toArray(new IssueDoc[0]));
- }
-
private IssueQuery projectQuery(String projectUuid) {
return IssueQuery.builder().projectUuids(singletonList(projectUuid)).resolved(false).build();
}