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();
}
}
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);
}
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();
}
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);
}
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"));
}
Aggregations