Search in sources :

Example 1 with PeriodStart

use of org.sonar.server.issue.index.IssueQuery.PeriodStart in project sonarqube by SonarSource.

the class IssueQueryTest method build_query.

@Test
public void build_query() {
    RuleDefinitionDto rule = new RuleDefinitionDto().setUuid(Uuids.createFast());
    PeriodStart filterDate = new IssueQuery.PeriodStart(new Date(10_000_000_000L), false);
    IssueQuery query = IssueQuery.builder().issueKeys(newArrayList("ABCDE")).severities(newArrayList(Severity.BLOCKER)).statuses(Lists.newArrayList(Issue.STATUS_RESOLVED)).resolutions(newArrayList(Issue.RESOLUTION_FALSE_POSITIVE)).projectUuids(newArrayList("PROJECT")).componentUuids(newArrayList("org/struts/Action.java")).moduleUuids(newArrayList("org.struts:core")).rules(newArrayList(rule)).assigneeUuids(newArrayList("gargantua")).languages(newArrayList("xoo")).tags(newArrayList("tag1", "tag2")).types(newArrayList("RELIABILITY", "SECURITY")).owaspTop10(newArrayList("a1", "a2")).sansTop25(newArrayList("insecure-interaction", "porous-defenses")).cwe(newArrayList("12", "125")).branchUuid("my_branch").createdAfterByProjectUuids(ImmutableMap.of("PROJECT", filterDate)).assigned(true).createdAfter(new Date()).createdBefore(new Date()).createdAt(new Date()).resolved(true).sort(IssueQuery.SORT_BY_CREATION_DATE).asc(true).build();
    assertThat(query.issueKeys()).containsOnly("ABCDE");
    assertThat(query.severities()).containsOnly(Severity.BLOCKER);
    assertThat(query.statuses()).containsOnly(Issue.STATUS_RESOLVED);
    assertThat(query.resolutions()).containsOnly(Issue.RESOLUTION_FALSE_POSITIVE);
    assertThat(query.projectUuids()).containsOnly("PROJECT");
    assertThat(query.componentUuids()).containsOnly("org/struts/Action.java");
    assertThat(query.moduleUuids()).containsOnly("org.struts:core");
    assertThat(query.assignees()).containsOnly("gargantua");
    assertThat(query.languages()).containsOnly("xoo");
    assertThat(query.tags()).containsOnly("tag1", "tag2");
    assertThat(query.types()).containsOnly("RELIABILITY", "SECURITY");
    assertThat(query.owaspTop10()).containsOnly("a1", "a2");
    assertThat(query.sansTop25()).containsOnly("insecure-interaction", "porous-defenses");
    assertThat(query.cwe()).containsOnly("12", "125");
    assertThat(query.branchUuid()).isEqualTo("my_branch");
    assertThat(query.createdAfterByProjectUuids()).containsOnly(entry("PROJECT", filterDate));
    assertThat(query.assigned()).isTrue();
    assertThat(query.rules()).containsOnly(rule);
    assertThat(query.createdAfter()).isNotNull();
    assertThat(query.createdBefore()).isNotNull();
    assertThat(query.createdAt()).isNotNull();
    assertThat(query.resolved()).isTrue();
    assertThat(query.sort()).isEqualTo(IssueQuery.SORT_BY_CREATION_DATE);
    assertThat(query.asc()).isTrue();
}
Also used : PeriodStart(org.sonar.server.issue.index.IssueQuery.PeriodStart) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) Test(org.junit.Test)

Example 2 with PeriodStart

use of org.sonar.server.issue.index.IssueQuery.PeriodStart in project sonarqube by SonarSource.

the class IssueIndex method getCreatedAtFacet.

private Optional<AggregationBuilder> getCreatedAtFacet(IssueQuery query, TopAggregationHelper aggregationHelper, AllFilters allFilters) {
    long startTime;
    boolean startInclusive;
    PeriodStart createdAfter = query.createdAfter();
    if (createdAfter == null) {
        OptionalLong minDate = getMinCreatedAt(allFilters);
        if (!minDate.isPresent()) {
            return Optional.empty();
        }
        startTime = minDate.getAsLong();
        startInclusive = true;
    } else {
        startTime = createdAfter.date().getTime();
        startInclusive = createdAfter.inclusive();
    }
    Date createdBefore = query.createdBefore();
    long endTime = createdBefore == null ? system.now() : createdBefore.getTime();
    Duration timeSpan = new Duration(startTime, endTime);
    DateHistogramInterval bucketSize = computeDateHistogramBucketSize(timeSpan);
    FilterAggregationBuilder topAggregation = aggregationHelper.buildTopAggregation(CREATED_AT.getName(), CREATED_AT.getTopAggregationDef(), NO_EXTRA_FILTER, t -> {
        AggregationBuilder dateHistogram = AggregationBuilders.dateHistogram(CREATED_AT.getName()).field(CREATED_AT.getFieldName()).dateHistogramInterval(bucketSize).minDocCount(0L).format(DateUtils.DATETIME_FORMAT).timeZone(Optional.ofNullable(query.timeZone()).orElse(system.getDefaultTimeZone().toZoneId())).extendedBounds(new LongBounds(startInclusive ? startTime : (startTime + 1), endTime - 1L));
        addEffortAggregationIfNeeded(query, dateHistogram);
        t.subAggregation(dateHistogram);
    });
    return Optional.of(topAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SumAggregationBuilder(org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) PeriodStart(org.sonar.server.issue.index.IssueQuery.PeriodStart) OptionalLong(java.util.OptionalLong) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) Duration(org.joda.time.Duration) Date(java.util.Date) LongBounds(org.elasticsearch.search.aggregations.bucket.histogram.LongBounds)

Example 3 with PeriodStart

use of org.sonar.server.issue.index.IssueQuery.PeriodStart in project sonarqube by SonarSource.

the class IssueIndex method addDatesFilter.

private void addDatesFilter(AllFilters filters, IssueQuery query) {
    PeriodStart createdAfter = query.createdAfter();
    Date createdBefore = query.createdBefore();
    validateCreationDateBounds(createdBefore, createdAfter != null ? createdAfter.date() : null);
    if (createdAfter != null) {
        filters.addFilter("__createdAfter", CREATED_AT.getFilterScope(), QueryBuilders.rangeQuery(FIELD_ISSUE_FUNC_CREATED_AT).from(BaseDoc.dateToEpochSeconds(createdAfter.date()), createdAfter.inclusive()));
    }
    if (createdBefore != null) {
        filters.addFilter("__createdBefore", CREATED_AT.getFilterScope(), QueryBuilders.rangeQuery(FIELD_ISSUE_FUNC_CREATED_AT).lt(BaseDoc.dateToEpochSeconds(createdBefore)));
    }
    Date createdAt = query.createdAt();
    if (createdAt != null) {
        filters.addFilter("__createdAt", CREATED_AT.getFilterScope(), termQuery(FIELD_ISSUE_FUNC_CREATED_AT, BaseDoc.dateToEpochSeconds(createdAt)));
    }
}
Also used : PeriodStart(org.sonar.server.issue.index.IssueQuery.PeriodStart) Date(java.util.Date)

Example 4 with PeriodStart

use of org.sonar.server.issue.index.IssueQuery.PeriodStart in project sonarqube by SonarSource.

the class IssueIndex method addCreatedAfterByProjectsFilter.

private static void addCreatedAfterByProjectsFilter(AllFilters allFilters, IssueQuery query) {
    Map<String, PeriodStart> createdAfterByProjectUuids = query.createdAfterByProjectUuids();
    BoolQueryBuilder boolQueryBuilder = boolQuery();
    createdAfterByProjectUuids.forEach((projectOrProjectBranchUuid, createdAfterDate) -> boolQueryBuilder.should(boolQuery().filter(termQuery(FIELD_ISSUE_BRANCH_UUID, projectOrProjectBranchUuid)).filter(rangeQuery(FIELD_ISSUE_FUNC_CREATED_AT).from(BaseDoc.dateToEpochSeconds(createdAfterDate.date()), createdAfterDate.inclusive()))));
    allFilters.addFilter("__created_after_by_project_uuids", new SimpleFieldFilterScope("createdAfterByProjectUuids"), boolQueryBuilder);
}
Also used : SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) PeriodStart(org.sonar.server.issue.index.IssueQuery.PeriodStart)

Aggregations

PeriodStart (org.sonar.server.issue.index.IssueQuery.PeriodStart)4 Date (java.util.Date)3 OptionalLong (java.util.OptionalLong)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)1 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)1 DateHistogramInterval (org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval)1 LongBounds (org.elasticsearch.search.aggregations.bucket.histogram.LongBounds)1 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 SumAggregationBuilder (org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder)1 Duration (org.joda.time.Duration)1 Test (org.junit.Test)1 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)1 SimpleFieldFilterScope (org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope)1