public void insert(DbSession session, Collection<SnapshotDto> items) { for (SnapshotDto item : items) { insert(session, item); } }
private SnapshotDto getAnalysis(DbSession dbSession, EventDto event) { return dbClient.snapshotDao().selectByUuid(dbSession, event.getAnalysisUuid()) .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' is not found", event.getAnalysisUuid()))); }
private Optional<SnapshotDto> findFirstSnapshot(DbSession session, SnapshotQuery query) { return dbClient.snapshotDao().selectAnalysesByQuery(session, query) .stream() .findFirst(); }
private void deleteEvent(DbSession dbSession, EventDto dbEvent) { if (VERSION.getLabel().equals(dbEvent.getCategory())) { SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, dbEvent.getAnalysisUuid()) .orElseThrow(() -> new IllegalStateException(format("Analysis '%s' not found", dbEvent.getAnalysisUuid()))); checkArgument(!analysis.getLast(), "Cannot delete the version event of last analysis"); analysis.setVersion(null); dbClient.snapshotDao().update(dbSession, analysis); } dbClient.eventDao().delete(dbSession, dbEvent.getUuid()); dbSession.commit(); }
private void validateAnalysisDate(Optional<ComponentDto> baseProject) { if (baseProject.isPresent()) { Optional<SnapshotDto> snapshotDto = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(session, baseProject.get().uuid()); long currentAnalysisDate = analysisMetadataHolder.getAnalysisDate(); Long lastAnalysisDate = snapshotDto.map(SnapshotDto::getCreatedAt).orElse(null); if (lastAnalysisDate != null && currentAnalysisDate <= lastAnalysisDate) { validationMessages.add(format("Date of analysis cannot be older than the date of the last known analysis on this project. Value: \"%s\". " + "Latest analysis: \"%s\". It's only possible to rebuild the past in a chronological order.", formatDateTime(new Date(currentAnalysisDate)), formatDateTime(new Date(lastAnalysisDate)))); } } }
@Test public void select_first_snapshots() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization()); db.getDbClient().componentDao().insert(dbSession, project); db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(project).setCreatedAt(5L), newAnalysis(project).setCreatedAt(2L), newAnalysis(project).setCreatedAt(1L)); dbSession.commit(); Optional<SnapshotDto> dto = underTest.selectOldestSnapshot(dbSession, project.uuid()); assertThat(dto).isNotEmpty(); assertThat(dto.get().getCreatedAt()).isEqualTo(1L); assertThat(underTest.selectOldestSnapshot(dbSession, "blabla")).isEmpty(); }
@Test public void selectLastSnapshotsByRootComponentUuids_returns_snapshots_flagged_as_last() { ComponentDto firstProject = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "PROJECT_UUID_1")); dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(false)); SnapshotDto lastSnapshotOfFirstProject = dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(true)); ComponentDto secondProject = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "PROJECT_UUID_2")); SnapshotDto lastSnapshotOfSecondProject = dbClient.snapshotDao().insert(dbSession, newAnalysis(secondProject).setLast(true)); db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization())); List<SnapshotDto> result = underTest.selectLastAnalysesByRootComponentUuids(dbSession, newArrayList(firstProject.uuid(), secondProject.uuid())); assertThat(result).extracting(SnapshotDto::getId).containsOnly(lastSnapshotOfFirstProject.getId(), lastSnapshotOfSecondProject.getId()); }
private Map<String, SnapshotDto> getSnapshots(DbSession dbSession, SearchProjectsRequest request, List<String> projectUuids) { if (request.getAdditionalFields().contains(ANALYSIS_DATE) || request.getAdditionalFields().contains(LEAK_PERIOD_DATE)) { return dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, projectUuids) .stream() .collect(MoreCollectors.uniqueIndex(SnapshotDto::getComponentUuid)); } return emptyMap(); }
@Test public void select_snapshot_by_query() { db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml"); assertThat(underTest.selectAnalysesByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setIsLast(true))).isNotNull(); assertThat(underTest.selectAnalysisByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isNull(); assertThat(underTest.selectAnalysisByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isNull(); }
private Consumer<EventDto> updateInDb(DbSession dbSession) { return event -> { dbClient.eventDao().update(dbSession, event.getUuid(), event.getName(), event.getDescription()); if (VERSION.getLabel().equals(event.getCategory())) { SnapshotDto analysis = getAnalysis(dbSession, event); analysis.setVersion(event.getName()); dbClient.snapshotDao().update(dbSession, analysis); } dbSession.commit(); }; }
@Override public void execute(ComputationStep.Context context) { try (DbSession dbSession = dbClient.openSession(false)) { Component project = treeRootHolder.getRoot(); dbClient.snapshotDao().switchIsLastFlagAndSetProcessedStatus(dbSession, project.getUuid(), analysisMetadataHolder.getUuid()); dbClient.componentDao().applyBChangesForRootComponentUuid(dbSession, project.getUuid()); dbSession.commit(); } }
@CheckForNull private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) { return dbClient.snapshotDao().selectAnalysisByQuery( dbSession, new SnapshotQuery() .setComponentUuid(rootUuid) .setIsLast(true)); }
private Optional<Period> findOldestAnalysis(DbSession dbSession, String periodMode, String projectUuid) { Optional<Period> period = dbClient.snapshotDao().selectOldestSnapshot(dbSession, projectUuid) .map(dto -> newPeriod(periodMode, null, dto)); ensureNotOnFirstAnalysis(period.isPresent()); return period; }
@Override public void handle(Request request, Response response) throws Exception { String analysisParam = request.mandatoryParam(PARAM_ANALYSIS); try (DbSession dbSession = dbClient.openSession(false)) { SnapshotDto analysis = dbClient.snapshotDao().selectByUuid(dbSession, analysisParam) .orElseThrow(() -> analysisNotFoundException(analysisParam)); if (STATUS_UNPROCESSED.equals(analysis.getStatus())) { throw analysisNotFoundException(analysisParam); } userSession.checkComponentUuidPermission(UserRole.ADMIN, analysis.getComponentUuid()); checkArgument(!analysis.getLast(), "The last analysis '%s' cannot be deleted", analysisParam); analysis.setStatus(STATUS_UNPROCESSED); dbClient.snapshotDao().update(dbSession, analysis); dbSession.commit(); } response.noContent(); }
private ProjectAndSnapshot getProjectThenSnapshot(DbSession dbSession, String projectId, String projectKey) { ComponentDto projectDto = componentFinder.getByUuidOrKey(dbSession, projectId, projectKey, ParamNames.PROJECT_ID_AND_KEY); Optional<SnapshotDto> snapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, projectDto.projectUuid()); return new ProjectAndSnapshot(projectDto, snapshot.orElse(null)); }
@Test public void selectLastSnapshotsByRootComponentUuids_returns_empty_list_if_empty_input() { List<SnapshotDto> result = underTest.selectLastAnalysesByRootComponentUuids(dbSession, emptyList()); assertThat(result).isEmpty(); }
private EventDto insertDbEvent(DbSession dbSession, CreateEventRequest request, SnapshotDto analysis) { EventDto dbEvent = dbClient.eventDao().insert(dbSession, toDbEvent(request, analysis)); if (VERSION.equals(request.getCategory())) { analysis.setVersion(request.getName()); dbClient.snapshotDao().update(dbSession, analysis); } dbSession.commit(); return dbEvent; }
@Test public void switchIsLastFlagAndSetProcessedStatus() { insertAnalysis("P1", "A1", SnapshotDto.STATUS_PROCESSED, true); insertAnalysis("P1", "A2", SnapshotDto.STATUS_UNPROCESSED, false); insertAnalysis("P2", "A3", SnapshotDto.STATUS_PROCESSED, true); db.commit(); underTest.switchIsLastFlagAndSetProcessedStatus(db.getSession(), "P1", "A2"); verifyStatusAndIsLastFlag("A1", SnapshotDto.STATUS_PROCESSED, false); verifyStatusAndIsLastFlag("A2", SnapshotDto.STATUS_PROCESSED, true); // other project is untouched verifyStatusAndIsLastFlag("A3", SnapshotDto.STATUS_PROCESSED, true); }
@Test public void fail_with_ISE_to_select_snapshot_by_query_when_more_than_one_result() { db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml"); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Expected one analysis to be returned, got 6"); underTest.selectAnalysisByQuery(db.getSession(), new SnapshotQuery()); }
@CheckForNull private Period findByFirstAnalysis() { SnapshotDto snapshotDto = dbClient.snapshotDao().selectOldestSnapshot(session, projectUuid); if (snapshotDto == null) { return null; } LOG.debug("Compare to first analysis ({})", formatDate(snapshotDto.getCreatedAt())); return new Period(LEAK_PERIOD_MODE_PREVIOUS_VERSION, null, snapshotDto.getCreatedAt(), snapshotDto.getUuid()); }