Search in sources :

Example 1 with PkgProminence

use of org.haiku.haikudepotserver.dataobjects.PkgProminence 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)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 MediaType (com.google.common.net.MediaType)1 CSVWriter (com.opencsv.CSVWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)1 PkgProminence (org.haiku.haikudepotserver.dataobjects.PkgProminence)1 PkgUserRatingAggregate (org.haiku.haikudepotserver.dataobjects.PkgUserRatingAggregate)1 Repository (org.haiku.haikudepotserver.dataobjects.Repository)1 AbstractJobRunner (org.haiku.haikudepotserver.job.AbstractJobRunner)1 JobDataWithByteSink (org.haiku.haikudepotserver.job.model.JobDataWithByteSink)1 JobService (org.haiku.haikudepotserver.job.model.JobService)1 PkgProminenceAndUserRatingSpreadsheetJobSpecification (org.haiku.haikudepotserver.pkg.model.PkgProminenceAndUserRatingSpreadsheetJobSpecification)1 PkgService (org.haiku.haikudepotserver.pkg.model.PkgService)1