Search in sources :

Example 1 with LoadedFile

use of gov.cms.bfd.model.rif.LoadedFile in project beneficiary-fhir-data by CMSgov.

the class LoadedFilterManager method refreshFilters.

/**
 * Called periodically to build and refresh the filters list from the entityManager.
 *
 * <p>The {@link #lastBatchCreated} and {@link #firstBatchCreated} fields are updated by this
 * call.
 */
@Scheduled(fixedDelay = 1000, initialDelay = 2000)
public void refreshFilters() {
    /*
     * Dev note: the pipeline has a process to trim the files list. Nevertheless, building a set of
     * bloom filters may take a while. This method is expected to be called on it's own thread by
     * the the Spring framework. In addition, it doesn't lock this object until the end of the
     * process, so this filter building process can happen without interfering with serving. Also,
     * this refresh time will be proportional to the number of files which have been loaded in the
     * past refresh period. If no files have been loaded, this refresh should take less than a
     * millisecond.
     */
    try {
        // If new batches are present, then build new filters for the affected files
        final Instant currentLastBatchCreated = fetchLastLoadedBatchCreated().orElse(BEFORE_LAST_UPDATED_FEATURE);
        if (this.lastBatchCreated == null || this.lastBatchCreated.isBefore(currentLastBatchCreated)) {
            LOGGER.info("Refreshing LoadedFile filters with new filters from {} to {}", lastBatchCreated, currentLastBatchCreated);
            List<LoadedTuple> loadedTuples = fetchLoadedTuples(this.lastBatchCreated);
            List<LoadedFileFilter> newFilters = updateFilters(this.filters, loadedTuples, this::fetchLoadedBatches);
            // If batches been trimmed, then remove filters which are no longer present
            final Instant currentFirstBatchUpdate = fetchFirstLoadedBatchCreated().orElse(BEFORE_LAST_UPDATED_FEATURE);
            if (this.firstBatchCreated == null || this.firstBatchCreated.isBefore(currentFirstBatchUpdate)) {
                LOGGER.info("Trimmed LoadedFile filters before {}", currentFirstBatchUpdate);
                List<LoadedFile> loadedFiles = fetchLoadedFiles();
                newFilters = trimFilters(newFilters, loadedFiles);
            }
            LOGGER.info("Updating timestamps. currentFirstBatchUpdate={} currentLastBatchCreated={}", currentFirstBatchUpdate, currentLastBatchCreated);
            set(newFilters, currentFirstBatchUpdate, currentLastBatchCreated);
        }
    } catch (Throwable ex) {
        LOGGER.error("Error found refreshing LoadedFile filters", ex);
    }
}
Also used : Instant(java.time.Instant) LoadedFile(gov.cms.bfd.model.rif.LoadedFile) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with LoadedFile

use of gov.cms.bfd.model.rif.LoadedFile in project beneficiary-fhir-data by CMSgov.

the class RifLoaderIT method trimLoadedFiles.

@Test
public void trimLoadedFiles() {
    PipelineTestUtils.get().doTestWithDb((dataSource, entityManager) -> {
        // Setup a loaded file with an old date
        loadSample(Arrays.asList(StaticRifResourceGroup.SAMPLE_A.getResources()));
        final List<LoadedFile> loadedFiles = PipelineTestUtils.get().findLoadedFiles(entityManager);
        final EntityTransaction txn = entityManager.getTransaction();
        txn.begin();
        LoadedFile oldFile = loadedFiles.get(loadedFiles.size() - 1);
        oldFile.setCreated(Instant.now().minus(101, ChronoUnit.DAYS));
        txn.commit();
        // Look at the files now
        final List<LoadedFile> beforeFiles = PipelineTestUtils.get().findLoadedFiles(entityManager);
        final Instant oldDate = Instant.now().minus(99, ChronoUnit.DAYS);
        assertTrue(beforeFiles.stream().anyMatch(file -> file.getCreated().isBefore(oldDate)), "Expect to have old files");
        // Load another set that will cause the old file to be trimmed
        loadSample(Arrays.asList(StaticRifResourceGroup.SAMPLE_U.getResources()));
        // Verify that old file was trimmed
        final List<LoadedFile> afterFiles = PipelineTestUtils.get().findLoadedFiles(entityManager);
        assertFalse(afterFiles.stream().anyMatch(file -> file.getCreated().isBefore(oldDate)), "Expect to not have old files");
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RifFileType(gov.cms.bfd.model.rif.RifFileType) StaticRifResource(gov.cms.bfd.model.rif.samples.StaticRifResource) CSVRecord(org.apache.commons.csv.CSVRecord) LoggerFactory(org.slf4j.LoggerFactory) IdHasher(gov.cms.bfd.pipeline.sharedutils.IdHasher) Disabled(org.junit.jupiter.api.Disabled) RifFilesProcessor(gov.cms.bfd.pipeline.ccw.rif.extract.RifFilesProcessor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) CSVFormat(org.apache.commons.csv.CSVFormat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertionFailedError(org.opentest4j.AssertionFailedError) CarrierClaimLine(gov.cms.bfd.model.rif.CarrierClaimLine) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) BeneficiaryHistory(gov.cms.bfd.model.rif.BeneficiaryHistory) Path(java.nio.file.Path) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(java.util.function.Predicate) RifFileRecords(gov.cms.bfd.model.rif.RifFileRecords) RifFile(gov.cms.bfd.model.rif.RifFile) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) BeneficiaryMonthly(gov.cms.bfd.model.rif.BeneficiaryMonthly) TestInfo(org.junit.jupiter.api.TestInfo) RifFilesEvent(gov.cms.bfd.model.rif.RifFilesEvent) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) LoadedBatch(gov.cms.bfd.model.rif.LoadedBatch) List(java.util.List) BeneficiaryHistory_(gov.cms.bfd.model.rif.BeneficiaryHistory_) Stream(java.util.stream.Stream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CarrierClaim(gov.cms.bfd.model.rif.CarrierClaim) LocalDate(java.time.LocalDate) Slf4jReporter(com.codahale.metrics.Slf4jReporter) LoadedFile(gov.cms.bfd.model.rif.LoadedFile) Optional(java.util.Optional) RifParsingUtils(gov.cms.bfd.model.rif.parse.RifParsingUtils) BeneficiaryColumn(gov.cms.bfd.model.rif.BeneficiaryColumn) RifRecordEvent(gov.cms.bfd.model.rif.RifRecordEvent) CSVPrinter(org.apache.commons.csv.CSVPrinter) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) SkippedRifRecord(gov.cms.bfd.model.rif.SkippedRifRecord) LocalRifFile(gov.cms.bfd.pipeline.ccw.rif.extract.LocalRifFile) Function(java.util.function.Function) PipelineTestUtils(gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils) StreamSupport(java.util.stream.StreamSupport) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) StaticRifResourceGroup(gov.cms.bfd.model.rif.samples.StaticRifResourceGroup) Root(javax.persistence.criteria.Root) RifFileEvent(gov.cms.bfd.model.rif.RifFileEvent) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Month(java.time.Month) FileWriter(java.io.FileWriter) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) EntityTransaction(javax.persistence.EntityTransaction) EntityTransaction(javax.persistence.EntityTransaction) LoadedFile(gov.cms.bfd.model.rif.LoadedFile) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 3 with LoadedFile

use of gov.cms.bfd.model.rif.LoadedFile in project beneficiary-fhir-data by CMSgov.

the class RifLoader method insertLoadedFile.

/**
 * Insert the LoadedFile into the database
 *
 * @param fileEvent to base this new LoadedFile
 * @param errorHandler to call if something bad happens
 * @return the loadedFileId of the new LoadedFile record
 */
private long insertLoadedFile(RifFileEvent fileEvent, Consumer<Throwable> errorHandler) {
    if (fileEvent == null || fileEvent.getFile().getFileType() == null) {
        throw new IllegalArgumentException();
    }
    final LoadedFile loadedFile = new LoadedFile();
    loadedFile.setRifType(fileEvent.getFile().getFileType().toString());
    loadedFile.setCreated(Instant.now());
    try {
        EntityManager em = appState.getEntityManagerFactory().createEntityManager();
        EntityTransaction txn = null;
        try {
            // Insert the passed in loaded file
            txn = em.getTransaction();
            txn.begin();
            em.persist(loadedFile);
            txn.commit();
            LOGGER.info("Inserting LoadedFile {} of type {} created at {}", loadedFile.getLoadedFileId(), loadedFile.getRifType(), loadedFile.getCreated());
            return loadedFile.getLoadedFileId();
        } finally {
            if (em != null && em.isOpen()) {
                if (txn != null && txn.isActive()) {
                    txn.rollback();
                }
                em.close();
            }
        }
    } catch (Exception ex) {
        errorHandler.accept(ex);
        return -1;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) LoadedFile(gov.cms.bfd.model.rif.LoadedFile) UncheckedIOException(java.io.UncheckedIOException) SQLException(java.sql.SQLException) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) IOException(java.io.IOException)

Example 4 with LoadedFile

use of gov.cms.bfd.model.rif.LoadedFile in project beneficiary-fhir-data by CMSgov.

the class LoadedFilterManager method fetchLoadedTuples.

/**
 * Fetch the tuple of (loadedFileId, LoadedFile.created, max(LoadedBatch.created))
 *
 * @param after limits the query to include batches created after this timestamp
 * @return tuples that meet the after criteria or an empty list
 */
private List<LoadedTuple> fetchLoadedTuples(Instant after) {
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<LoadedTuple> query = cb.createQuery(LoadedTuple.class);
    final Root<LoadedFile> f = query.from(LoadedFile.class);
    Join<LoadedFile, LoadedBatch> b = f.join("batches");
    query = query.select(cb.construct(LoadedTuple.class, f.get("loadedFileId"), f.get("created"), cb.max(b.get("created"))));
    if (after != null) {
        query = query.where(cb.greaterThan(b.get("created"), after));
    }
    query = query.groupBy(f.get("loadedFileId"), f.get("created")).orderBy(cb.desc(f.get("created")));
    return entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LoadedFile(gov.cms.bfd.model.rif.LoadedFile) LoadedBatch(gov.cms.bfd.model.rif.LoadedBatch)

Example 5 with LoadedFile

use of gov.cms.bfd.model.rif.LoadedFile in project beneficiary-fhir-data by CMSgov.

the class RifLoaderIT method buildSyntheticLoadedFiles.

@Disabled
@Test
public void buildSyntheticLoadedFiles() {
    PipelineTestUtils.get().doTestWithDb((dataSource, entityManager) -> {
        loadSample(Arrays.asList(StaticRifResourceGroup.SYNTHETIC_DATA.getResources()));
        // Verify that a loaded files exsits
        final List<LoadedFile> loadedFiles = PipelineTestUtils.get().findLoadedFiles(entityManager);
        assertTrue(loadedFiles.size() > 0, "Expected to have at least one file");
        final LoadedFile file = loadedFiles.get(0);
        final List<LoadedBatch> batches = loadBatches(entityManager, file.getLoadedFileId());
        assertTrue(batches.size() > 0);
    });
}
Also used : LoadedFile(gov.cms.bfd.model.rif.LoadedFile) LoadedBatch(gov.cms.bfd.model.rif.LoadedBatch) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

LoadedFile (gov.cms.bfd.model.rif.LoadedFile)7 LoadedBatch (gov.cms.bfd.model.rif.LoadedBatch)4 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Instant (java.time.Instant)2 EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 Disabled (org.junit.jupiter.api.Disabled)2 Slf4jReporter (com.codahale.metrics.Slf4jReporter)1 Beneficiary (gov.cms.bfd.model.rif.Beneficiary)1 BeneficiaryColumn (gov.cms.bfd.model.rif.BeneficiaryColumn)1 BeneficiaryHistory (gov.cms.bfd.model.rif.BeneficiaryHistory)1 BeneficiaryHistory_ (gov.cms.bfd.model.rif.BeneficiaryHistory_)1 BeneficiaryMonthly (gov.cms.bfd.model.rif.BeneficiaryMonthly)1 CarrierClaim (gov.cms.bfd.model.rif.CarrierClaim)1 CarrierClaimLine (gov.cms.bfd.model.rif.CarrierClaimLine)1 RifFile (gov.cms.bfd.model.rif.RifFile)1 RifFileEvent (gov.cms.bfd.model.rif.RifFileEvent)1 RifFileRecords (gov.cms.bfd.model.rif.RifFileRecords)1