3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import java.time.temporal.ChronoUnit;
23 import java.util.Date;
24 import java.util.List;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
27 import org.apache.commons.lang.StringUtils;
28 import org.sonar.api.config.Settings;
29 import org.sonar.api.utils.DateUtils;
30 import org.sonar.api.utils.log.Logger;
31 import org.sonar.api.utils.log.Loggers;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.SnapshotDto;
35 import org.sonar.db.component.SnapshotQuery;
36 import org.sonar.server.computation.task.projectanalysis.period.Period;
38 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD;
39 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
40 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS;
41 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS;
42 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
43 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION;
44 import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
45 import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE;
46 import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.ASC;
47 import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC;
49 public class PeriodResolver {
50 private static final Logger LOG = Loggers.get(PeriodResolver.class);
52 private final DbClient dbClient;
53 private final DbSession session;
54 private final String projectUuid;
55 private final long analysisDate;
57 private final String currentVersion;
59 public PeriodResolver(DbClient dbClient, DbSession session, String projectUuid, long analysisDate, @Nullable String currentVersion) {
60 this.dbClient = dbClient;
61 this.session = session;
62 this.projectUuid = projectUuid;
63 this.analysisDate = analysisDate;
64 this.currentVersion = currentVersion;
68 public Period resolve(Settings settings) {
69 String propertyValue = getPropertyValue(settings);
70 if (StringUtils.isBlank(propertyValue)) {
73 if (propertyValue.equals(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS)) {
74 LOG.warn("Leak period is set to deprecated value '{}'. This value will be removed in next SonarQube LTS, please use another one instead.",
75 LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS);
77 Period period = resolve(propertyValue);
78 if (period == null && StringUtils.isNotBlank(propertyValue)) {
79 LOG.debug("Property " + LEAK_PERIOD + " is not valid: " + propertyValue);
85 private Period resolve(String property) {
86 Integer days = tryToResolveByDays(property);
88 return findByDays(days);
90 Date date = DateUtils.parseDateQuietly(property);
92 return findByDate(date);
94 if (StringUtils.equals(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, property)) {
95 return findByPreviousAnalysis();
97 if (StringUtils.equals(LEAK_PERIOD_MODE_PREVIOUS_VERSION, property)) {
98 return findByPreviousVersion();
100 return findByVersion(property);
103 private Period findByDate(Date date) {
104 SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setCreatedAfter(date.getTime()).setSort(BY_DATE, ASC));
105 if (snapshot == null) {
108 LOG.debug("Compare to date {} (analysis of {})", formatDate(date.getTime()), formatDate(snapshot.getCreatedAt()));
109 return new Period(LEAK_PERIOD_MODE_DATE, DateUtils.formatDate(date), snapshot.getCreatedAt(), snapshot.getUuid());
113 private Period findByDays(int days) {
114 List<SnapshotDto> snapshots = dbClient.snapshotDao().selectAnalysesByQuery(session, createCommonQuery(projectUuid).setCreatedBefore(analysisDate).setSort(BY_DATE, ASC));
115 long targetDate = DateUtils.addDays(new Date(analysisDate), -days).getTime();
116 SnapshotDto snapshot = findNearestSnapshotToTargetDate(snapshots, targetDate);
117 if (snapshot == null) {
120 LOG.debug("Compare over {} days ({}, analysis of {})", String.valueOf(days), formatDate(targetDate), formatDate(snapshot.getCreatedAt()));
121 return new Period(LEAK_PERIOD_MODE_DAYS, String.valueOf(days), snapshot.getCreatedAt(), snapshot.getUuid());
125 private Period findByPreviousAnalysis() {
126 SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setCreatedBefore(analysisDate).setIsLast(true).setSort(BY_DATE, DESC));
127 if (snapshot == null) {
130 LOG.debug("Compare to previous analysis ({})", formatDate(snapshot.getCreatedAt()));
131 return new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, formatDate(snapshot.getCreatedAt()), snapshot.getCreatedAt(), snapshot.getUuid());
135 private Period findByPreviousVersion() {
136 if (currentVersion == null) {
139 List<SnapshotDto> snapshotDtos = dbClient.snapshotDao().selectPreviousVersionSnapshots(session, projectUuid, currentVersion);
140 if (snapshotDtos.isEmpty()) {
141 // If no previous version is found, the first analysis is returned
142 return findByFirstAnalysis();
144 SnapshotDto snapshotDto = snapshotDtos.get(0);
145 LOG.debug("Compare to previous version ({})", formatDate(snapshotDto.getCreatedAt()));
146 return new Period(LEAK_PERIOD_MODE_PREVIOUS_VERSION, snapshotDto.getVersion(), snapshotDto.getCreatedAt(), snapshotDto.getUuid());
150 private Period findByFirstAnalysis() {
151 SnapshotDto snapshotDto = dbClient.snapshotDao().selectOldestSnapshot(session, projectUuid);
152 if (snapshotDto == null) {
155 LOG.debug("Compare to first analysis ({})", formatDate(snapshotDto.getCreatedAt()));
156 return new Period(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, snapshotDto.getCreatedAt(), snapshotDto.getUuid());
160 private Period findByVersion(String version) {
161 SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setVersion(version).setSort(BY_DATE, DESC));
162 if (snapshot == null) {
165 LOG.debug("Compare to version ({}) ({})", version, formatDate(snapshot.getCreatedAt()));
166 return new Period(LEAK_PERIOD_MODE_VERSION, version, snapshot.getCreatedAt(), snapshot.getUuid());
170 private SnapshotDto findFirstSnapshot(DbSession session, SnapshotQuery query) {
171 List<SnapshotDto> snapshots = dbClient.snapshotDao().selectAnalysesByQuery(session, query);
172 if (!snapshots.isEmpty()) {
173 return snapshots.get(0);
179 private static Integer tryToResolveByDays(String property) {
181 return Integer.parseInt(property);
182 } catch (NumberFormatException e) {
183 // Nothing to, it means that the property is not a number of days
189 private static SnapshotDto findNearestSnapshotToTargetDate(List<SnapshotDto> snapshots, Long targetDate) {
190 long bestDistance = Long.MAX_VALUE;
191 SnapshotDto nearest = null;
192 for (SnapshotDto snapshot : snapshots) {
193 long distance = Math.abs(snapshot.getCreatedAt() - targetDate);
194 if (distance <= bestDistance) {
195 bestDistance = distance;
202 private static SnapshotQuery createCommonQuery(String projectUuid) {
203 return new SnapshotQuery().setComponentUuid(projectUuid).setStatus(STATUS_PROCESSED);
206 private static String formatDate(long date) {
207 return DateUtils.formatDate(Date.from(new Date(date).toInstant().truncatedTo(ChronoUnit.SECONDS)));
210 private static String getPropertyValue(Settings settings) {
211 return settings.getString(LEAK_PERIOD);