Search in sources :

Example 26 with CoverageSearchEvent

use of gov.cms.ab2d.coverage.model.CoverageSearchEvent in project ab2d by CMSgov.

the class CoverageServiceRepository method deleteCurrentSearch.

/**
 * Delete all coverage information related to the results of a single coverage period
 * defined by an offset into the past.
 *
 * An offset of 0 finds the current IN_PROGRESS search event and deletes all coverage information associated
 * with that event.
 *
 * @param period coverage period to remove
 */
public void deleteCurrentSearch(CoveragePeriod period) {
    Optional<CoverageSearchEvent> searchEvent = coverageSearchEventRepo.findByPeriodDesc(period.getId(), 100).stream().filter(event -> event.getNewStatus() == CoverageJobStatus.IN_PROGRESS).findFirst();
    // For performance reasons this is done via jdbc
    if (searchEvent.isPresent()) {
        MapSqlParameterSource parameterSource = new MapSqlParameterSource().addValue("searchEvent", searchEvent.get().getId()).addValue("contract", period.getContractNumber()).addValue("years", YEARS);
        NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);
        template.update(DELETE_SEARCH, parameterSource);
        vacuumCoverage();
    }
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) Trace(com.newrelic.api.agent.Trace) ContractForCoverageDTO(gov.cms.ab2d.coverage.model.ContractForCoverageDTO) ZonedDateTime(java.time.ZonedDateTime) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) StringUtils(org.apache.commons.lang3.StringUtils) CoverageJobStatus(gov.cms.ab2d.coverage.model.CoverageJobStatus) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) FilterOutByDate(gov.cms.ab2d.filter.FilterOutByDate) SQLException(java.sql.SQLException) CoveragePagingRequest(gov.cms.ab2d.coverage.model.CoveragePagingRequest) CoverageCount(gov.cms.ab2d.coverage.model.CoverageCount) ResultSet(java.sql.ResultSet) AB2D_ZONE(gov.cms.ab2d.common.util.DateUtil.AB2D_ZONE) Map(java.util.Map) DataSource(javax.sql.DataSource) Repository(org.springframework.stereotype.Repository) CoverageMembership(gov.cms.ab2d.coverage.model.CoverageMembership) LinkedHashSet(java.util.LinkedHashSet) CoveragePagingResult(gov.cms.ab2d.coverage.model.CoveragePagingResult) CoverageSummary(gov.cms.ab2d.coverage.model.CoverageSummary) PreparedStatement(java.sql.PreparedStatement) CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent) AB2D_EPOCH(gov.cms.ab2d.common.util.DateUtil.AB2D_EPOCH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Collectors.toList(java.util.stream.Collectors.toList) OffsetDateTime(java.time.OffsetDateTime) ChronoUnit(java.time.temporal.ChronoUnit) Identifiers(gov.cms.ab2d.coverage.model.Identifiers) LocalDate(java.time.LocalDate) CoveragePeriod(gov.cms.ab2d.coverage.model.CoveragePeriod) Optional(java.util.Optional) Collections(java.util.Collections) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent)

Example 27 with CoverageSearchEvent

use of gov.cms.ab2d.coverage.model.CoverageSearchEvent in project ab2d by CMSgov.

the class CoverageServiceImpl method updateStatus.

/**
 * Update the status of a {@link CoveragePeriod} in the search workflow.
 * @param period the coverage period to update
 * @param description a description of the update
 * @param status the new status for the coverage period and coverage search
 * @param updateCoveragePeriod whether the coverage period needs to be updated as well
 * @return the update coverage search event
 */
private CoverageSearchEvent updateStatus(CoveragePeriod period, String description, CoverageJobStatus status, boolean updateCoveragePeriod) {
    CoverageSearchEvent currentStatus = getLastEvent(period);
    logStatusChange(period, description, status);
    CoverageJobStatus oldStatus = currentStatus != null ? currentStatus.getNewStatus() : null;
    CoverageSearchEvent newStatus = new CoverageSearchEvent();
    newStatus.setCoveragePeriod(period);
    newStatus.setOldStatus(oldStatus);
    newStatus.setNewStatus(status);
    newStatus.setDescription(description);
    if (status == CoverageJobStatus.SUCCESSFUL) {
        period.setLastSuccessfulJob(OffsetDateTime.now());
    }
    // For successful, failed, or submitted jobs also update the coverage periods status in the DB
    if (updateCoveragePeriod) {
        period.setStatus(status);
        coveragePeriodRepo.saveAndFlush(period);
    }
    return coverageSearchEventRepo.saveAndFlush(newStatus);
}
Also used : CoverageJobStatus(gov.cms.ab2d.coverage.model.CoverageJobStatus) CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent)

Example 28 with CoverageSearchEvent

use of gov.cms.ab2d.coverage.model.CoverageSearchEvent in project ab2d by CMSgov.

the class CoverageServiceImpl method findEventWithSuccessfulOffset.

/**
 * Find data saved from in progress search
 * @param periodId coverage period id corresponding to contract, year, and month
 * @param successfulOffset number of successful searches in the past to look at [0,n)
 * @return an in progress search event corresponding to the offset if found
 */
public Optional<CoverageSearchEvent> findEventWithSuccessfulOffset(int periodId, int successfulOffset) {
    List<CoverageSearchEvent> events = coverageSearchEventRepo.findByPeriodDesc(periodId, 100);
    int successfulSearches = 0;
    for (CoverageSearchEvent event : events) {
        if (event.getNewStatus() == CoverageJobStatus.SUCCESSFUL) {
            successfulSearches += 1;
        } else if (event.getNewStatus() == CoverageJobStatus.IN_PROGRESS && successfulSearches == successfulOffset) {
            return Optional.of(event);
        }
    }
    return Optional.empty();
}
Also used : CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent)

Example 29 with CoverageSearchEvent

use of gov.cms.ab2d.coverage.model.CoverageSearchEvent in project ab2d by CMSgov.

the class CoverageServiceImpl method searchDiff.

// todo: consider removing now that the CoverageDeltaRepository functionality exists
// We can write alarms using that delta table if we need to.
@Override
@Trace(metricName = "SearchDiff", dispatcher = true)
public CoverageSearchDiff searchDiff(int periodId) {
    CoveragePeriod period = findCoveragePeriod(periodId);
    if (period.getStatus() != CoverageJobStatus.IN_PROGRESS) {
        throw new InvalidJobStateTransition("Cannot diff a currently running search against previous search because results may be added");
    }
    Optional<CoverageSearchEvent> previousSearch = findEventWithSuccessfulOffset(periodId, 1);
    Optional<CoverageSearchEvent> currentSearch = findEventWithSuccessfulOffset(periodId, 0);
    CoverageSearchEvent current = currentSearch.orElseThrow(() -> new RuntimeException("could not find latest in progress search event"));
    int previousCount = 0;
    if (previousSearch.isPresent()) {
        previousCount = coverageServiceRepo.countBySearchEvent(previousSearch.get());
    }
    int currentCount = coverageServiceRepo.countBySearchEvent(current);
    int unchanged = 0;
    if (previousCount > 0) {
        log.info("Calculating the deltas for the search period {}-{}-{}", period.getContractNumber(), period.getMonth(), period.getYear());
        coverageDeltaRepository.trackDeltas(previousSearch.get(), current);
        unchanged = coverageServiceRepo.countIntersection(previousSearch.get(), current);
    }
    return new CoverageSearchDiff(period, previousCount, currentCount, unchanged);
}
Also used : CoverageSearchDiff(gov.cms.ab2d.coverage.model.CoverageSearchDiff) CoveragePeriod(gov.cms.ab2d.coverage.model.CoveragePeriod) CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent) Trace(com.newrelic.api.agent.Trace)

Example 30 with CoverageSearchEvent

use of gov.cms.ab2d.coverage.model.CoverageSearchEvent in project ab2d by CMSgov.

the class CoverageServiceImplTest method completeSearches.

@DisplayName("Coverage period searches can be marked successful")
@Test
void completeSearches() {
    coverageService.submitSearch(period1Jan.getId(), "testing");
    coverageService.submitSearch(period2Jan.getId(), "testing");
    // Cannot start search that has not been submitted
    assertThrows(InvalidJobStateTransition.class, () -> coverageService.completeSearch(period1Feb.getId(), "testing"));
    startSearchAndPullEvent();
    CoverageSearchEvent completed = coverageService.completeSearch(period1Jan.getId(), "testing");
    CoverageSearchEvent completedCopy = coverageSearchEventRepo.findById(completed.getId()).get();
    assertEquals(CoverageJobStatus.IN_PROGRESS, completedCopy.getOldStatus());
    assertEquals(CoverageJobStatus.SUCCESSFUL, completedCopy.getNewStatus());
    // Check that last successful job is updated
    assertNotNull(completedCopy.getCoveragePeriod().getLastSuccessfulJob());
    // Cannot complete job twice
    assertThrows(InvalidJobStateTransition.class, () -> coverageService.completeSearch(period1Jan.getId(), "testing"));
    // Add status changes that should invalidate marking a job completed
    coverageService.submitSearch(period1Jan.getId(), "testing");
    startSearchAndPullEvent();
    CoverageSearchEvent cancel = new CoverageSearchEvent();
    cancel.setCoveragePeriod(period2Jan);
    cancel.setNewStatus(CoverageJobStatus.CANCELLED);
    cancel.setOldStatus(CoverageJobStatus.SUBMITTED);
    cancel.setDescription("testing");
    CoverageSearchEvent failed = new CoverageSearchEvent();
    failed.setCoveragePeriod(period1Jan);
    failed.setNewStatus(CoverageJobStatus.FAILED);
    failed.setOldStatus(CoverageJobStatus.IN_PROGRESS);
    failed.setDescription("testing");
    coverageSearchEventRepo.save(cancel);
    coverageSearchEventRepo.save(failed);
    period1Jan.setStatus(CoverageJobStatus.CANCELLED);
    period2Jan.setStatus(CoverageJobStatus.FAILED);
    coveragePeriodRepo.saveAndFlush(period1Jan);
    coveragePeriodRepo.saveAndFlush(period2Jan);
    assertThrows(InvalidJobStateTransition.class, () -> coverageService.completeSearch(period1Jan.getId(), "testing"));
    assertThrows(InvalidJobStateTransition.class, () -> coverageService.completeSearch(period2Jan.getId(), "testing"));
}
Also used : CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

CoverageSearchEvent (gov.cms.ab2d.coverage.model.CoverageSearchEvent)55 Test (org.junit.jupiter.api.Test)38 DisplayName (org.junit.jupiter.api.DisplayName)37 CoveragePeriod (gov.cms.ab2d.coverage.model.CoveragePeriod)26 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)26 CoverageSearch (gov.cms.ab2d.coverage.model.CoverageSearch)22 Identifiers (gov.cms.ab2d.coverage.model.Identifiers)22 CoverageMapping (gov.cms.ab2d.coverage.model.CoverageMapping)20 LinkedHashSet (java.util.LinkedHashSet)15 CoverageCount (gov.cms.ab2d.coverage.model.CoverageCount)14 List (java.util.List)13 ContractDTO (gov.cms.ab2d.common.dto.ContractDTO)12 ContractForCoverageDTO (gov.cms.ab2d.coverage.model.ContractForCoverageDTO)12 CoverageJobStatus (gov.cms.ab2d.coverage.model.CoverageJobStatus)12 CoveragePagingRequest (gov.cms.ab2d.coverage.model.CoveragePagingRequest)12 CoveragePagingResult (gov.cms.ab2d.coverage.model.CoveragePagingResult)12 CoverageSearchDiff (gov.cms.ab2d.coverage.model.CoverageSearchDiff)12 CoverageSummary (gov.cms.ab2d.coverage.model.CoverageSummary)12 OffsetDateTime (java.time.OffsetDateTime)12 Collections.disjoint (java.util.Collections.disjoint)12