use of org.sonar.db.purge.PurgeableAnalysisDto in project sonarqube by SonarSource.
the class Interval method group.
static List<Interval> group(List<PurgeableAnalysisDto> snapshots, Date start, Date end, int calendarField) {
List<Interval> intervals = Lists.newArrayList();
GregorianCalendar calendar = new GregorianCalendar();
int lastYear = -1;
int lastFieldValue = -1;
Interval currentInterval = null;
for (PurgeableAnalysisDto snapshot : snapshots) {
if (!DateUtils.isSameDay(start, snapshot.getDate()) && snapshot.getDate().after(start) && (snapshot.getDate().before(end) || DateUtils.isSameDay(end, snapshot.getDate()))) {
calendar.setTime(snapshot.getDate());
int currentFieldValue = calendar.get(calendarField);
int currentYear = calendar.get(Calendar.YEAR);
if (lastYear != currentYear || lastFieldValue != currentFieldValue) {
currentInterval = new Interval();
intervals.add(currentInterval);
}
lastFieldValue = currentFieldValue;
lastYear = currentYear;
if (currentInterval != null) {
currentInterval.add(snapshot);
}
}
}
return intervals;
}
use of org.sonar.db.purge.PurgeableAnalysisDto in project sonarqube by SonarSource.
the class KeepOneFilter method appendSnapshotsToDelete.
private void appendSnapshotsToDelete(Interval interval, List<PurgeableAnalysisDto> toDelete) {
if (interval.count() > 1) {
List<PurgeableAnalysisDto> deletables = Lists.newArrayList();
List<PurgeableAnalysisDto> toKeep = Lists.newArrayList();
for (PurgeableAnalysisDto snapshot : interval.get()) {
if (isDeletable(snapshot)) {
deletables.add(snapshot);
} else {
toKeep.add(snapshot);
}
}
if (!toKeep.isEmpty()) {
toDelete.addAll(deletables);
} else if (deletables.size() > 1) {
// keep last snapshot
toDelete.addAll(deletables.subList(0, deletables.size() - 1));
}
}
}
use of org.sonar.db.purge.PurgeableAnalysisDto in project sonarqube by SonarSource.
the class DefaultPeriodCleanerTest method doClean.
@Test
public void doClean() {
PurgeDao dao = mock(PurgeDao.class);
DbSession session = mock(DbSession.class);
when(dao.selectPurgeableAnalyses("uuid_123", session)).thenReturn(Arrays.asList(new PurgeableAnalysisDto().setAnalysisId(999).setAnalysisUuid("u999").setDate(System2.INSTANCE.now()), new PurgeableAnalysisDto().setAnalysisId(456).setAnalysisUuid("u456").setDate(System2.INSTANCE.now())));
Filter filter1 = newFirstSnapshotInListFilter();
Filter filter2 = newFirstSnapshotInListFilter();
PurgeProfiler profiler = new PurgeProfiler();
DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, profiler);
cleaner.doClean("uuid_123", Arrays.asList(filter1, filter2), session);
InOrder inOrder = Mockito.inOrder(dao, filter1, filter2);
inOrder.verify(filter1).log();
inOrder.verify(dao, times(1)).deleteAnalyses(eq(session), eq(profiler), eq(ImmutableList.of(new IdUuidPair(999, "u999"))));
inOrder.verify(filter2).log();
inOrder.verify(dao, times(1)).deleteAnalyses(eq(session), eq(profiler), eq(ImmutableList.of(new IdUuidPair(456, "u456"))));
inOrder.verifyNoMoreInteractions();
}
use of org.sonar.db.purge.PurgeableAnalysisDto in project sonarqube by SonarSource.
the class IntervalTest method calendarField.
static int calendarField(Interval interval, int field) {
if (interval.count() == 0) {
return -1;
}
PurgeableAnalysisDto first = interval.get().iterator().next();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(first.getDate());
return cal.get(field);
}
Aggregations