Search in sources :

Example 1 with JobDataWithByteSink

use of org.haiku.haikudepotserver.job.model.JobDataWithByteSink in project haikudepotserver by haiku.

the class PkgIconSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, PkgIconSpreadsheetJobSpecification specification) throws IOException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    final ObjectContext context = serverRuntime.newContext();
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
        final List<PkgIconConfiguration> pkgIconConfigurations = pkgIconService.getInUsePkgIconConfigurations(context);
        {
            List<String> headings = new ArrayList<>();
            headings.add("pkg-name");
            headings.add("repository-codes");
            headings.add("no-icons");
            for (PkgIconConfiguration pkgIconConfiguration : pkgIconConfigurations) {
                StringBuilder heading = new StringBuilder();
                heading.append(pkgIconConfiguration.getMediaType().getCode());
                if (null != pkgIconConfiguration.getSize()) {
                    heading.append("@");
                    heading.append(pkgIconConfiguration.getSize().toString());
                }
                headings.add(heading.toString());
            }
            writer.writeNext(headings.toArray(new String[headings.size()]));
        }
        // stream out the packages.
        long startMs = System.currentTimeMillis();
        LOGGER.info("will produce icon spreadsheet report");
        long count = pkgService.eachPkg(context, false, pkg -> {
            PkgSupplement pkgSupplement = pkg.getPkgSupplement();
            List<String> cells = new ArrayList<>();
            cells.add(pkg.getName());
            cells.add(repositoryService.getRepositoriesForPkg(context, pkg).stream().map(Repository::getCode).collect(Collectors.joining(";")));
            cells.add(pkg.getPkgSupplement().getPkgIcons().isEmpty() ? MARKER : "");
            for (PkgIconConfiguration pkgIconConfiguration : pkgIconConfigurations) {
                cells.add(pkgSupplement.getPkgIcon(pkgIconConfiguration.getMediaType(), pkgIconConfiguration.getSize()).map(pi -> MARKER).orElse(""));
            }
            writer.writeNext(cells.toArray(new String[cells.size()]));
            return true;
        });
        LOGGER.info("did produce icon report for {} packages in {}ms", count, System.currentTimeMillis() - startMs);
    }
}
Also used : PkgIconConfiguration(org.haiku.haikudepotserver.pkg.model.PkgIconConfiguration) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CSVWriter(com.opencsv.CSVWriter) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Repository(org.haiku.haikudepotserver.dataobjects.Repository) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) ObjectContext(org.apache.cayenne.ObjectContext)

Example 2 with JobDataWithByteSink

use of org.haiku.haikudepotserver.job.model.JobDataWithByteSink in project haikudepotserver by haiku.

the class PkgLocalizationCoverageExportSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, PkgLocalizationCoverageExportSpreadsheetJobSpecification specification) throws IOException, JobRunnerException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    final ObjectContext context = serverRuntime.newContext();
    final List<NaturalLanguage> naturalLanguages = getNaturalLanguages(context);
    if (naturalLanguages.isEmpty()) {
        throw new RuntimeException("there appear to be no natural languages in the system");
    }
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
        final String[] cells = new String[1 + naturalLanguages.size()];
        // headers
        {
            int c = 0;
            cells[c++] = "pkg-name";
            for (NaturalLanguage naturalLanguage : naturalLanguages) {
                cells[c++] = naturalLanguage.getCode();
            }
        }
        long startMs = System.currentTimeMillis();
        writer.writeNext(cells);
        // stream out the packages.
        final long expectedTotal = pkgService.totalPkg(context, false);
        final AtomicLong counter = new AtomicLong(0);
        LOGGER.info("will produce package localization report for {} packages", expectedTotal);
        long count = pkgService.eachPkg(context, // allow source only.
        false, pkg -> {
            PkgSupplement pkgSupplement = pkg.getPkgSupplement();
            int c = 0;
            cells[c++] = pkg.getName();
            for (NaturalLanguage naturalLanguage : naturalLanguages) {
                cells[c++] = pkgSupplement.getPkgLocalization(naturalLanguage).map(pl -> MARKER).orElse("");
            }
            writer.writeNext(cells);
            jobService.setJobProgressPercent(specification.getGuid(), (int) ((100 * counter.incrementAndGet()) / expectedTotal));
            // keep going!
            return true;
        });
        LOGGER.info("did produce pkg localization coverage spreadsheet report for {} packages in {}ms", count, System.currentTimeMillis() - startMs);
    }
}
Also used : OutputStream(java.io.OutputStream) CSVWriter(com.opencsv.CSVWriter) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) AtomicLong(java.util.concurrent.atomic.AtomicLong) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) OutputStreamWriter(java.io.OutputStreamWriter) ObjectContext(org.apache.cayenne.ObjectContext)

Example 3 with JobDataWithByteSink

use of org.haiku.haikudepotserver.job.model.JobDataWithByteSink in project haikudepotserver by haiku.

the class PkgProminenceAndUserRatingSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, PkgProminenceAndUserRatingSpreadsheetJobSpecification specification) throws IOException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    final ObjectContext context = serverRuntime.newContext();
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
        writer.writeNext(new String[] { "pkg-name", "repository-code", "prominence-name", "prominence-ordering", "derived-rating", "derived-rating-sample-size" });
        // stream out the packages.
        long startMs = System.currentTimeMillis();
        LOGGER.info("will produce prominence spreadsheet report");
        long count = pkgService.eachPkg(context, false, pkg -> {
            List<PkgProminence> pkgProminences = PkgProminence.findByPkg(context, pkg);
            List<PkgUserRatingAggregate> pkgUserRatingAggregates = PkgUserRatingAggregate.findByPkg(context, pkg);
            List<Repository> repositories = Stream.concat(pkgProminences.stream().map(PkgProminence::getRepository), pkgUserRatingAggregates.stream().map(PkgUserRatingAggregate::getRepository)).distinct().sorted().collect(Collectors.toList());
            if (repositories.isEmpty()) {
                writer.writeNext(new String[] { pkg.getName(), "", "", "", "", "" });
            } else {
                for (Repository repository : repositories) {
                    Optional<PkgProminence> pkgProminenceOptional = pkgProminences.stream().filter(pp -> pp.getRepository().equals(repository)).collect(SingleCollector.optional());
                    Optional<PkgUserRatingAggregate> pkgUserRatingAggregateOptional = pkgUserRatingAggregates.stream().filter(pura -> pura.getRepository().equals(repository)).collect(SingleCollector.optional());
                    writer.writeNext(new String[] { pkg.getName(), repository.getCode(), pkgProminenceOptional.map(p -> p.getProminence().getName()).orElse(""), pkgProminenceOptional.map(p -> p.getProminence().getOrdering().toString()).orElse(""), pkgUserRatingAggregateOptional.map(p -> p.getDerivedRating().toString()).orElse(""), pkgUserRatingAggregateOptional.map(p -> p.getDerivedRatingSampleSize().toString()).orElse("") });
                }
            }
            return true;
        });
        LOGGER.info("did produce prominence spreadsheet report for {} packages in {}ms", count, System.currentTimeMillis() - startMs);
    }
}
Also used : OutputStream(java.io.OutputStream) MediaType(com.google.common.net.MediaType) PkgService(org.haiku.haikudepotserver.pkg.model.PkgService) ObjectContext(org.apache.cayenne.ObjectContext) Logger(org.slf4j.Logger) SingleCollector(org.haiku.haikudepotserver.support.SingleCollector) PkgProminenceAndUserRatingSpreadsheetJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgProminenceAndUserRatingSpreadsheetJobSpecification) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) LoggerFactory(org.slf4j.LoggerFactory) CSVWriter(com.opencsv.CSVWriter) PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.PkgUserRatingAggregate) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) PkgProminence(org.haiku.haikudepotserver.dataobjects.PkgProminence) Stream(java.util.stream.Stream) Repository(org.haiku.haikudepotserver.dataobjects.Repository) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) OutputStreamWriter(java.io.OutputStreamWriter) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) JobService(org.haiku.haikudepotserver.job.model.JobService) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) OutputStream(java.io.OutputStream) PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.PkgUserRatingAggregate) CSVWriter(com.opencsv.CSVWriter) PkgProminence(org.haiku.haikudepotserver.dataobjects.PkgProminence) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) Repository(org.haiku.haikudepotserver.dataobjects.Repository) OutputStreamWriter(java.io.OutputStreamWriter) ObjectContext(org.apache.cayenne.ObjectContext)

Example 4 with JobDataWithByteSink

use of org.haiku.haikudepotserver.job.model.JobDataWithByteSink 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 5 with JobDataWithByteSink

use of org.haiku.haikudepotserver.job.model.JobDataWithByteSink in project haikudepotserver by haiku.

the class PkgScreenshotSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, PkgScreenshotSpreadsheetJobSpecification specification) throws IOException {
    Preconditions.checkArgument(null != jobService);
    Preconditions.checkArgument(null != specification);
    final ObjectContext context = serverRuntime.newContext();
    // this will register the outbound data against the job.
    JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
    try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
        String[] headings = new String[] { "pkg-name", "repository-codes", "screenshot-count", "screenshot-bytes" };
        writer.writeNext(headings);
        String[] cells = new String[4];
        // stream out the packages.
        long startMs = System.currentTimeMillis();
        LOGGER.info("will produce spreadsheet spreadsheet report");
        long count = pkgService.eachPkg(context, false, pkg -> {
            PkgSupplement pkgSupplement = pkg.getPkgSupplement();
            cells[0] = pkg.getName();
            cells[1] = repositoryService.getRepositoriesForPkg(context, pkg).stream().map(Repository::getCode).collect(Collectors.joining(";"));
            cells[2] = Integer.toString(pkgSupplement.getPkgScreenshots().size());
            cells[3] = Integer.toString(pkgSupplement.getPkgScreenshots().stream().mapToInt(_PkgScreenshot::getLength).sum());
            writer.writeNext(cells);
            return true;
        });
        LOGGER.info("did produce spreadsheet report for {} packages in {}ms", count, System.currentTimeMillis() - startMs);
    }
}
Also used : JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Repository(org.haiku.haikudepotserver.dataobjects.Repository) OutputStream(java.io.OutputStream) CSVWriter(com.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

JobDataWithByteSink (org.haiku.haikudepotserver.job.model.JobDataWithByteSink)16 OutputStream (java.io.OutputStream)13 ObjectContext (org.apache.cayenne.ObjectContext)12 CSVWriter (com.opencsv.CSVWriter)11 OutputStreamWriter (java.io.OutputStreamWriter)9 Repository (org.haiku.haikudepotserver.dataobjects.Repository)6 GZIPOutputStream (java.util.zip.GZIPOutputStream)4 PkgSupplement (org.haiku.haikudepotserver.dataobjects.PkgSupplement)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 Preconditions (com.google.common.base.Preconditions)3 MediaType (com.google.common.net.MediaType)3 IOException (java.io.IOException)3 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)3 AbstractJobRunner (org.haiku.haikudepotserver.job.AbstractJobRunner)3 JobRunnerException (org.haiku.haikudepotserver.job.model.JobRunnerException)3 JobService (org.haiku.haikudepotserver.job.model.JobService)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2