Search in sources :

Example 16 with JobDataWithByteSink

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

the class UserRatingSpreadsheetJobRunner method run.

@Override
public void run(JobService jobService, UserRatingSpreadsheetJobSpecification 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, ',')) {
        Optional<Pkg> paramPkgOptional = Optional.empty();
        Optional<User> paramUserOptional = Optional.empty();
        Optional<Repository> paramRepositoryOptional = Optional.empty();
        if (!Strings.isNullOrEmpty(specification.getRepositoryCode())) {
            paramRepositoryOptional = Repository.tryGetByCode(context, specification.getRepositoryCode());
            if (!paramRepositoryOptional.isPresent()) {
                throw new IllegalStateException("unable to find the repository; " + specification.getRepositoryCode());
            }
        }
        if (!Strings.isNullOrEmpty(specification.getUserNickname())) {
            paramUserOptional = User.tryGetByNickname(context, specification.getUserNickname());
            if (!paramUserOptional.isPresent()) {
                throw new IllegalStateException("unable to find the user; " + specification.getUserNickname());
            }
        }
        if (!Strings.isNullOrEmpty(specification.getPkgName())) {
            paramPkgOptional = Pkg.tryGetByName(context, specification.getPkgName());
            if (!paramPkgOptional.isPresent()) {
                throw new IllegalStateException("unable to find the package; " + specification.getPkgName());
            }
        }
        writer.writeNext(new String[] { "pkg-name", "repository-code", "architecture-code", "version-coordinates", "user-nickname", "create-timestamp", "modify-timestamp", "rating", "stability-code", "natural-language-code", "comment", "code" });
        // stream out the packages.
        long startMs = System.currentTimeMillis();
        LOGGER.info("will user rating spreadsheet report");
        final DateTimeFormatter dateTimeFormatter = DateTimeHelper.createStandardDateTimeFormat();
        UserRatingSearchSpecification spec = new UserRatingSearchSpecification();
        spec.setPkg(paramPkgOptional.orElse(null));
        spec.setUser(paramUserOptional.orElse(null));
        spec.setRepository(paramRepositoryOptional.orElse(null));
        // TODO; provide a prefetch tree into the user, pkgversion.
        int count = userRatingService.each(context, spec, userRating -> {
            writer.writeNext(new String[] { userRating.getPkgVersion().getPkg().getName(), userRating.getPkgVersion().getRepositorySource().getRepository().getCode(), userRating.getPkgVersion().getArchitecture().getCode(), userRating.getPkgVersion().toVersionCoordinates().toString(), userRating.getUser().getNickname(), dateTimeFormatter.format(Instant.ofEpochMilli(userRating.getCreateTimestamp().getTime())), dateTimeFormatter.format(Instant.ofEpochMilli(userRating.getModifyTimestamp().getTime())), null != userRating.getRating() ? userRating.getRating().toString() : "", null != userRating.getUserRatingStability() ? userRating.getUserRatingStability().getCode() : "", userRating.getNaturalLanguage().getCode(), userRating.getComment(), userRating.getCode() });
            return true;
        });
        LOGGER.info("did produce user rating spreadsheet report for {} user ratings in {}ms", count, System.currentTimeMillis() - startMs);
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) OutputStream(java.io.OutputStream) CSVWriter(com.opencsv.CSVWriter) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) Repository(org.haiku.haikudepotserver.dataobjects.Repository) UserRatingSearchSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingSearchSpecification) OutputStreamWriter(java.io.OutputStreamWriter) ObjectContext(org.apache.cayenne.ObjectContext) DateTimeFormatter(java.time.format.DateTimeFormatter)

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