private Optional<Ce.Task> searchTaskByUuid(DbSession dbSession, Request request) { String textQuery = request.getQ(); if (textQuery == null) { return Optional.empty(); } Optional<CeQueueDto> queue = dbClient.ceQueueDao().selectByUuid(dbSession, textQuery); if (queue.isPresent()) { return Optional.of(formatter.formatQueue(dbSession, queue.get())); } Optional<CeActivityDto> activity = dbClient.ceActivityDao().selectByUuid(dbSession, textQuery); return activity.map(ceActivityDto -> formatter.formatActivity(dbSession, ceActivityDto, null, emptyList())); }
private CeActivityDto insert(String uuid, String type, @Nullable String componentUuid, @Nullable String mainComponentUuid, CeActivityDto.Status status) { CeActivityDto dto = createActivityDto(uuid, type, componentUuid, mainComponentUuid, status); underTest.insert(db.getSession(), dto); return dto; }
@Test public void deleteByUuids() { insert("TASK_1", "REPORT", MAINCOMPONENT_1, SUCCESS); insert("TASK_2", "REPORT", MAINCOMPONENT_1, SUCCESS); insert("TASK_3", "REPORT", MAINCOMPONENT_1, SUCCESS); underTest.deleteByUuids(db.getSession(), ImmutableSet.of("TASK_1", "TASK_3")); assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").isPresent()).isFalse(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_2")).isPresent(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_3").isPresent()).isFalse(); }
@Override public void start() { try (DbSession dbSession = dbClient.openSession(false)) { Date sixMonthsAgo = DateUtils.addDays(new Date(system2.now()), -180); LOGGER.info("Delete the Compute Engine tasks created before {}", sixMonthsAgo.getTime()); Set<String> ceActivityUuids = dbClient.ceActivityDao().selectOlderThan(dbSession, sixMonthsAgo.getTime()) .stream() .map(CeActivityDto::getUuid) .collect(toSet()); dbClient.ceActivityDao().deleteByUuids(dbSession, ceActivityUuids); dbClient.ceTaskCharacteristicsDao().deleteByTaskUuids(dbSession, ceActivityUuids); dbClient.ceTaskInputDao().deleteByUuids(dbSession, ceActivityUuids); Date fourWeeksAgo = DateUtils.addDays(new Date(system2.now()), -28); LOGGER.info("Delete the Scanner contexts tasks created before {}", fourWeeksAgo.getTime()); Set<String> scannerContextUuids = dbClient.ceScannerContextDao().selectOlderThan(dbSession, fourWeeksAgo.getTime()); dbClient.ceScannerContextDao().deleteByUuids( dbSession, concat(ceActivityUuids.stream(), scannerContextUuids.stream()).collect(toSet())); dbSession.commit(); } }
@Test public void test_insert_error_message_only() { CeActivityDto dto = createActivityDto("TASK_1", REPORT, COMPONENT_1, MAINCOMPONENT_1, FAILED); underTest.insert(db.getSession(), dto); Optional<CeActivityDto> saved = underTest.selectByUuid(db.getSession(), "TASK_1"); CeActivityDto read = saved.get(); assertThat(read.getErrorMessage()).isEqualTo(read.getErrorMessage()); assertThat(read.getErrorStacktrace()).isNull(); }
@Test public void selectByQuery_does_not_populate_errorStacktrace_field() { insert("TASK_1", REPORT, MAINCOMPONENT_1, FAILED); underTest.insert(db.getSession(), createActivityDto("TASK_2", REPORT, COMPONENT_1, MAINCOMPONENT_1, FAILED).setErrorStacktrace("some stack")); List<CeActivityDto> dtos = underTest.selectByQuery(db.getSession(), new CeTaskQuery().setMainComponentUuid(MAINCOMPONENT_1), forPage(1).andSize(100)); assertThat(dtos) .hasSize(2) .extracting("errorStacktrace").containsOnly((String) null); }
private List<Ce.Task> loadPastTasks(DbSession dbSession, Request request, CeTaskQuery query) { List<CeActivityDto> dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, forPage(1).andSize(parseInt(request.getPs()))); return formatter.formatActivity(dbSession, dtos); }
public Optional<CeActivityDto> selectLastByMainComponentUuid(DbSession dbSession, String mainComponentUuid) { return Optional.ofNullable(mapper(dbSession).selectLastByMainComponentUuid(mainComponentUuid)); }
@Test public void count_last_by_status_and_main_component_uuid() { insert("TASK_1", CeTaskTypes.REPORT, MAINCOMPONENT_1, SUCCESS); // component 2 insert("TASK_2", CeTaskTypes.REPORT, MAINCOMPONENT_2, SUCCESS); // status failed insert("TASK_3", CeTaskTypes.REPORT, MAINCOMPONENT_1, FAILED); // status canceled insert("TASK_4", CeTaskTypes.REPORT, MAINCOMPONENT_1, CANCELED); insert("TASK_5", CeTaskTypes.REPORT, MAINCOMPONENT_1, SUCCESS); db.commit(); assertThat(underTest.countLastByStatusAndMainComponentUuid(dbSession, SUCCESS, MAINCOMPONENT_1)).isEqualTo(1); assertThat(underTest.countLastByStatusAndMainComponentUuid(dbSession, SUCCESS, null)).isEqualTo(2); }
@Test public void test_insert_error_message_and_stacktrace() { CeActivityDto dto = createActivityDto("TASK_1", REPORT, COMPONENT_1, MAINCOMPONENT_1, FAILED) .setErrorStacktrace("error stack"); underTest.insert(db.getSession(), dto); Optional<CeActivityDto> saved = underTest.selectByUuid(db.getSession(), "TASK_1"); CeActivityDto read = saved.get(); assertThat(read.getErrorMessage()).isEqualTo(dto.getErrorMessage()); assertThat(read.getErrorStacktrace()).isEqualTo(dto.getErrorStacktrace()); assertThat(read.getErrorType()).isNotNull().isEqualTo(dto.getErrorType()); }
private List<String> selectPageOfUuids(Pagination pagination) { return underTest.selectByQuery(db.getSession(), new CeTaskQuery(), pagination).stream() .map(CeActivityToUuid.INSTANCE::apply) .collect(MoreCollectors.toList()); }
public List<CeActivityDto> selectOlderThan(DbSession dbSession, long beforeDate) { return mapper(dbSession).selectOlderThan(beforeDate); }
@Override public void start() { try (DbSession dbSession = dbClient.openSession(false)) { Calendar sixMonthsAgo = Calendar.getInstance(); sixMonthsAgo.setTimeInMillis(system2.now()); sixMonthsAgo.add(Calendar.DATE, -180); LOGGER.info("Delete the Compute Engine tasks created before {}", sixMonthsAgo.getTime()); Set<String> ceActivityUuids = dbClient.ceActivityDao().selectOlderThan(dbSession, sixMonthsAgo.getTimeInMillis()) .stream() .map(CeActivityDto::getUuid) .collect(MoreCollectors.toSet()); dbClient.ceActivityDao().deleteByUuids(dbSession, ceActivityUuids); dbClient.ceScannerContextDao().deleteByUuids(dbSession, ceActivityUuids); dbSession.commit(); } }
private ActivityStatusWsResponse doHandle(Request request) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<ComponentDto> component = searchComponent(dbSession, request); String componentUuid = component.isPresent() ? component.get().uuid() : null; checkPermissions(component); int pendingCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.PENDING, componentUuid); int inProgressCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.IN_PROGRESS, componentUuid); int failingCount = dbClient.ceActivityDao().countLastByStatusAndMainComponentUuid(dbSession, CeActivityDto.Status.FAILED, componentUuid); return ActivityStatusWsResponse.newBuilder() .setPending(pendingCount) .setInProgress(inProgressCount) .setFailing(failingCount) .build(); } }
@Override public void onEnd(CeTask ceTask, CeActivityDto.Status status, @Nullable CeTaskResult taskResult, @Nullable Throwable error) { if (status == CeActivityDto.Status.SUCCESS) { return; } String projectUuid = ceTask.getComponent().map(CeTask.Component::getUuid).orElse(null); if (!CeTaskTypes.REPORT.equals(ceTask.getType()) || projectUuid == null) { return; } if (notificationService.hasProjectSubscribersForTypes(projectUuid, singleton(ReportAnalysisFailureNotification.TYPE))) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto projectDto = dbClient.componentDao().selectOrFailByUuid(dbSession, projectUuid); checkScopeAndQualifier(projectDto); CeActivityDto ceActivityDto = dbClient.ceActivityDao().selectByUuid(dbSession, ceTask.getUuid()) .orElseThrow(() -> new RowNotFoundException(format("CeActivity with uuid '%s' not found", ceTask.getUuid()))); ReportAnalysisFailureNotification taskFailureNotification = buildNotification(ceActivityDto, projectDto, error); notificationService.deliver(taskFailureNotificationSerializer.toNotification(taskFailureNotification)); } } }
private CeActivityDto persist(CeActivityDto activityDto) { db.getDbClient().ceActivityDao().insert(db.getSession(), activityDto); db.commit(); return activityDto; }
@Test public void deleteByUuids_does_nothing_if_uuid_does_not_exist() { insert("TASK_1", "REPORT", MAINCOMPONENT_1, SUCCESS); // must not fail underTest.deleteByUuids(db.getSession(), singleton("TASK_2")); assertThat(underTest.selectByUuid(db.getSession(), "TASK_1")).isPresent(); }
@Test public void selectByQuery_no_results_if_shortcircuited_by_component_uuids() { insert("TASK_1", REPORT, MAINCOMPONENT_1, SUCCESS); CeTaskQuery query = new CeTaskQuery(); query.setMainComponentUuids(Collections.emptyList()); assertThat(underTest.selectByQuery(db.getSession(), query, forPage(1).andSize(1))).isEmpty(); }
public Optional<CeActivityDto> selectByUuid(DbSession dbSession, String uuid) { return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid)); }
@Test public void insert_must_set_relevant_is_last_field() { // only a single task on MAINCOMPONENT_1 -> is_last=true insert("TASK_1", REPORT, MAINCOMPONENT_1, SUCCESS); assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").get().getIsLast()).isTrue(); // only a single task on MAINCOMPONENT_2 -> is_last=true insert("TASK_2", REPORT, MAINCOMPONENT_2, SUCCESS); assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").get().getIsLast()).isTrue(); // two tasks on MAINCOMPONENT_1, the most recent one is TASK_3 insert("TASK_3", REPORT, MAINCOMPONENT_1, FAILED); assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").get().getIsLast()).isFalse(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").get().getIsLast()).isTrue(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_3").get().getIsLast()).isTrue(); // inserting a cancelled task does not change the last task insert("TASK_4", REPORT, MAINCOMPONENT_1, CANCELED); assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").get().getIsLast()).isFalse(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").get().getIsLast()).isTrue(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_3").get().getIsLast()).isTrue(); assertThat(underTest.selectByUuid(db.getSession(), "TASK_4").get().getIsLast()).isFalse(); }