Search in sources :

Example 1 with PkgScreenshotImportArchiveJobSpecification

use of org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification in project haikudepotserver by haiku.

the class PkgScreenshotImportArchiveJobRunner method run.

@Override
public void run(JobService jobService, PkgScreenshotImportArchiveJobSpecification specification) throws IOException, JobRunnerException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    Preconditions.checkArgument(null != specification.getInputDataGuid(), "missing input data guid on specification");
    Preconditions.checkArgument(null != specification.getImportStrategy(), "missing import strategy on specification");
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    Optional<JobDataWithByteSource> jobDataWithByteSourceOptional = jobService.tryObtainData(specification.getInputDataGuid());
    if (!jobDataWithByteSourceOptional.isPresent()) {
        throw new IllegalStateException("the job data was not able to be found for guid; " + specification.getInputDataGuid());
    }
    if (!serverRuntime.performInTransaction(() -> {
        try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
            Map<String, ScreenshotImportMetadatas> metadatas = new HashMap<>();
            writer.writeNext(new String[] { "path", "pkg-name", "action", "message", "code" });
            // sweep through and collect meta-data about the packages in the tar file.
            LOGGER.info("will collect data about packages' screenshots from the archive", metadatas.size());
            consumeScreenshotArchiveEntries(jobDataWithByteSourceOptional.get().getByteSource(), (ae) -> collectScreenshotMetadataFromArchive(metadatas, ae.getArchiveInputStream(), ae.getArchiveEntry(), ae.getPkgName(), ae.getOrder()));
            LOGGER.info("did collect data about {} packages' screenshots from the archive", metadatas.size());
            LOGGER.info("will collect data about persisted packages' screenshots");
            collectPersistedScreenshotMetadata(metadatas);
            LOGGER.info("did collect data about persisted packages' screenshots");
            if (specification.getImportStrategy() == PkgScreenshotImportArchiveJobSpecification.ImportStrategy.REPLACE) {
                LOGGER.info("will delete persisted screenshots that are absent from the archive");
                int deleted = deletePersistedScreenshotsThatAreNotPresentInArchiveAndReport(writer, metadatas.values());
                LOGGER.info("did delete {} persisted screenshots that are absent from the archive", deleted);
            }
            blendInArtificialOrderings(metadatas.values());
            // sweep through the archive again and load in those screenshots that are not already present.
            // The ordering of the inbound data should be preserved.
            LOGGER.info("will load screenshots from archive", metadatas.size());
            consumeScreenshotArchiveEntries(jobDataWithByteSourceOptional.get().getByteSource(), (ae) -> importScreenshotsFromArchiveAndReport(writer, metadatas.get(ae.getPkgName()), ae.getArchiveInputStream(), ae.getArchiveEntry(), ae.getPkgName(), ae.getOrder()));
            LOGGER.info("did load screenshots from archive", metadatas.size());
            return true;
        } catch (IOException e) {
            LOGGER.error("unable to complete the job", e);
        }
        return false;
    })) {
        throw new JobRunnerException("unable to complete job");
    }
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) PkgScreenshotImage(org.haiku.haikudepotserver.dataobjects.PkgScreenshotImage) PkgScreenshotService(org.haiku.haikudepotserver.pkg.model.PkgScreenshotService) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) HashingInputStream(com.google.common.hash.HashingInputStream) BadPkgScreenshotException(org.haiku.haikudepotserver.pkg.model.BadPkgScreenshotException) Matcher(java.util.regex.Matcher) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) JobService(org.haiku.haikudepotserver.job.model.JobService) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) HashCode(com.google.common.hash.HashCode) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) CSVWriter(com.opencsv.CSVWriter) PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) java.io(java.io) ByteStreams(com.google.common.io.ByteStreams) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) HashFunction(com.google.common.hash.HashFunction) JobRunnerException(org.haiku.haikudepotserver.job.model.JobRunnerException) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) JobRunnerException(org.haiku.haikudepotserver.job.model.JobRunnerException) JobDataWithByteSource(org.haiku.haikudepotserver.job.model.JobDataWithByteSource) CSVWriter(com.opencsv.CSVWriter) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink)

Example 2 with PkgScreenshotImportArchiveJobSpecification

use of org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification in project haikudepotserver by haiku.

the class PkgScreenshotImportArchiveJobRunnerIT method testImport_augment.

/**
 * <p>The package 'pkg1' has screenshots loaded already.</p>
 */
@Test
public void testImport_augment() throws Exception {
    integrationTestSupportService.createStandardTestData();
    // now load in the data to the job's storage system.
    PkgScreenshotImportArchiveJobSpecification spec = new PkgScreenshotImportArchiveJobSpecification();
    spec.setInputDataGuid(jobService.storeSuppliedData("sample-pkgscreenshotimportarchive-supplied.tgz", MediaType.TAR.toString(), getResourceByteSource("sample-pkgscreenshotimportarchive-supplied.tgz")).getGuid());
    spec.setImportStrategy(PkgScreenshotImportArchiveJobSpecification.ImportStrategy.AUGMENT);
    // run the job to import the data
    // ------------------------------------
    String jobGuid = jobService.immediate(spec, false);
    // ------------------------------------
    JobSnapshot snapshot = jobService.tryGetJob(jobGuid).get();
    Assertions.assertThat(snapshot.getStatus()).isEqualTo(JobSnapshot.Status.FINISHED);
    // check that the pkg1 is now loaded-up with screenshots from the tar-ball.
    assertScreenshotHashes(new String[] { "741dcbf503ddf034db0c41cb0fc9e3f82b20a5b6", "199a10b2b498e07e5bde31d816b560b19ed76ca6", "1a7be578f99d815084d32487d0e74626fe489967", "cba98ed83e44c2b07710f78c95568a3b2e3a081e", "fd1616327841ee2be21a16e330c57daa613580fa" });
    // check that the output report is as expected.
    {
        List<String> outputLines = getOutputLines(snapshot);
        // "path","pkg-name","action","message","code"
        // "hscr/pkg1/200.png","pkg1","ADDED","","34e796f2-27ff-4139-a0eb-b3b0e8238e96"
        // "hscr/pkg1/201.png","pkg1","ADDED","","91da40b7-762d-4d43-a0a0-4b4f31e585ae"
        // "hscr/pkg1/202.png","pkg1","INVALID",,""
        // "hscr/pkg1/23.png","pkg1","PRESENT","","2ef86afa-4da3-4f0c-9be6-d2b54ff099cb"
        // "hscr/notexists/3.png","notexists","NOTFOUND","",""
        // compare actual generated with expected.
        Assertions.assertThat(outputLines).hasSize(6);
        Assertions.assertThat(outputLines.get(1)).startsWith("\"hscr/pkg1/200.png\",\"pkg1\",\"ADDED\",\"\",\"");
        Assertions.assertThat(outputLines.get(2)).startsWith("\"hscr/pkg1/201.png\",\"pkg1\",\"ADDED\",\"\",\"");
        Assertions.assertThat(outputLines.get(3)).isEqualTo("\"hscr/pkg1/202.png\",\"pkg1\",\"INVALID\",,\"\"");
        Assertions.assertThat(outputLines.get(4)).startsWith("\"hscr/pkg1/23.png\",\"pkg1\",\"PRESENT\",\"\",\"");
        Assertions.assertThat(outputLines.get(5)).isEqualTo("\"hscr/notexists/3.png\",\"notexists\",\"NOTFOUND\",\"\",\"\"");
    }
}
Also used : PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) List(java.util.List) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with PkgScreenshotImportArchiveJobSpecification

use of org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification in project haikudepotserver by haiku.

the class PkgScreenshotImportArchiveJobRunnerIT method testImport_replace.

/**
 * <p>The package 'pkg1' has screenshots loaded already.</p>
 */
@Test
public void testImport_replace() throws Exception {
    integrationTestSupportService.createStandardTestData();
    // now load in the data to the job's storage system.
    PkgScreenshotImportArchiveJobSpecification spec = new PkgScreenshotImportArchiveJobSpecification();
    spec.setInputDataGuid(jobService.storeSuppliedData("sample-pkgscreenshotimportarchive-supplied.tgz", MediaType.TAR.toString(), getResourceByteSource("sample-pkgscreenshotimportarchive-supplied.tgz")).getGuid());
    spec.setImportStrategy(PkgScreenshotImportArchiveJobSpecification.ImportStrategy.REPLACE);
    // run the job to import the data
    // ------------------------------------
    String jobGuid = jobService.immediate(spec, false);
    // ------------------------------------
    JobSnapshot snapshot = jobService.tryGetJob(jobGuid).get();
    Assertions.assertThat(snapshot.getStatus()).isEqualTo(JobSnapshot.Status.FINISHED);
    // check that the pkg1 is now loaded-up with screenshots from the tar-ball.
    assertScreenshotHashes(new String[] { "741dcbf503ddf034db0c41cb0fc9e3f82b20a5b6", "cba98ed83e44c2b07710f78c95568a3b2e3a081e", "fd1616327841ee2be21a16e330c57daa613580fa" });
    // check that the output report is as expected.
    {
        List<String> outputLines = getOutputLines(snapshot);
        // "path","pkg-name","action","message","code"
        // "","pkg1","REMOVED","","cc0bbb79-7cc1-45b2-83a0-c53b77d3d228"
        // "","pkg1","REMOVED","","52433d05-9583-4419-9033-ea6e59b0e171"
        // "hscr/pkg1/200.png","pkg1","ADDED","","b1731520-575a-4cca-8a31-afbfb561d184"
        // "hscr/pkg1/201.png","pkg1","ADDED","","7a203288-1c9e-4fa6-b055-1be62b5db2fe"
        // "hscr/pkg1/202.png","pkg1","INVALID",,""
        // "hscr/pkg1/23.png","pkg1","PRESENT","","eb5e9cf5-ca10-4f43-80c4-3ea51cde5132"
        // "hscr/notexists/3.png","notexists","NOTFOUND","",""
        // compare actual generated with expected.
        Assertions.assertThat(outputLines.size()).isEqualTo(8);
        Assertions.assertThat(outputLines.get(1)).startsWith("\"\",\"pkg1\",\"REMOVED\",\"\",\"");
        Assertions.assertThat(outputLines.get(2)).startsWith("\"\",\"pkg1\",\"REMOVED\",\"\",\"");
        Assertions.assertThat(outputLines.get(3)).startsWith("\"hscr/pkg1/200.png\",\"pkg1\",\"ADDED\",\"\",\"");
        Assertions.assertThat(outputLines.get(4)).startsWith("\"hscr/pkg1/201.png\",\"pkg1\",\"ADDED\",\"\",\"");
        Assertions.assertThat(outputLines.get(5)).isEqualTo("\"hscr/pkg1/202.png\",\"pkg1\",\"INVALID\",,\"\"");
        Assertions.assertThat(outputLines.get(6)).startsWith("\"hscr/pkg1/23.png\",\"pkg1\",\"PRESENT\",\"\",\"");
        Assertions.assertThat(outputLines.get(7)).isEqualTo("\"hscr/notexists/3.png\",\"notexists\",\"NOTFOUND\",\"\",\"\"");
    }
}
Also used : PkgScreenshotImportArchiveJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) List(java.util.List) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

PkgScreenshotImportArchiveJobSpecification (org.haiku.haikudepotserver.pkg.model.PkgScreenshotImportArchiveJobSpecification)3 List (java.util.List)2 AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)2 JobSnapshot (org.haiku.haikudepotserver.job.model.JobSnapshot)2 Test (org.junit.jupiter.api.Test)2 Preconditions (com.google.common.base.Preconditions)1 HashCode (com.google.common.hash.HashCode)1 HashFunction (com.google.common.hash.HashFunction)1 Hashing (com.google.common.hash.Hashing)1 HashingInputStream (com.google.common.hash.HashingInputStream)1 ByteSource (com.google.common.io.ByteSource)1 ByteStreams (com.google.common.io.ByteStreams)1 MediaType (com.google.common.net.MediaType)1 CSVWriter (com.opencsv.CSVWriter)1 java.io (java.io)1 java.util (java.util)1 Consumer (java.util.function.Consumer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 GZIPInputStream (java.util.zip.GZIPInputStream)1