Search in sources :

Example 1 with VersionCoordinatesComparator

use of org.haiku.haikudepotserver.support.VersionCoordinatesComparator in project haikudepotserver by haiku.

the class UserRatingServiceImpl method userRatingDerivation.

/**
 * <p>This method will calculate the user rating for the package.  It may not be possible to generate a
 * user rating; in which case, an absent {@link Optional} is returned.</p>
 */
Optional<DerivedUserRating> userRatingDerivation(ObjectContext context, Pkg pkg, Repository repository) {
    Preconditions.checkNotNull(context);
    Preconditions.checkNotNull(pkg);
    // haul all of the pkg versions into memory first.
    // active only
    List<PkgVersion> pkgVersions = PkgVersion.getForPkg(context, pkg, repository, false);
    if (!pkgVersions.isEmpty()) {
        final VersionCoordinatesComparator versionCoordinatesComparator = new VersionCoordinatesComparator();
        // convert the package versions into coordinates and sort those.
        List<VersionCoordinates> versionCoordinates = pkgVersions.stream().map(pv -> pv.toVersionCoordinates().toVersionCoordinatesWithoutPreReleaseOrRevision()).distinct().sorted(versionCoordinatesComparator).collect(Collectors.toList());
        // work back VERSIONS_BACK from the latest in order to find out where to start fishing out user
        // ratings from.
        final VersionCoordinates oldestVersionCoordinates;
        if (versionCoordinates.size() < userRatingDerivationVersionsBack + 1) {
            oldestVersionCoordinates = versionCoordinates.get(0);
        } else {
            oldestVersionCoordinates = versionCoordinates.get(versionCoordinates.size() - (userRatingDerivationVersionsBack + 1));
        }
        // now we need to find all of the package versions that are including this one or newer.
        {
            final VersionCoordinatesComparator mainPartsVersionCoordinatesComparator = new VersionCoordinatesComparator(true);
            pkgVersions = pkgVersions.stream().filter(pv -> mainPartsVersionCoordinatesComparator.compare(pv.toVersionCoordinates(), oldestVersionCoordinates) >= 0).collect(Collectors.toList());
        }
        // only one user rating should taken from each user; the latest one.  Get a list of all of the
        // people who have rated these versions.
        List<Short> ratings = new ArrayList<>();
        List<String> userNicknames = getUserNicknamesWhoHaveRatedPkgVersions(context, pkgVersions);
        for (String nickname : userNicknames) {
            User user = User.getByNickname(context, nickname);
            List<UserRating> userRatingsForUser = new ArrayList<>(UserRating.getByUserAndPkgVersions(context, user, pkgVersions));
            userRatingsForUser.sort((o1, o2) -> ComparisonChain.start().compare(o1.getPkgVersion().toVersionCoordinates(), o2.getPkgVersion().toVersionCoordinates(), versionCoordinatesComparator).compare(o1.getCreateTimestamp(), o2.getCreateTimestamp()).compare(o1.getPkgVersion().getArchitecture().getCode(), o2.getPkgVersion().getArchitecture().getCode()).result());
            if (!userRatingsForUser.isEmpty()) {
                UserRating latestUserRatingForUser = userRatingsForUser.get(userRatingsForUser.size() - 1);
                if (null != latestUserRatingForUser.getRating()) {
                    ratings.add(latestUserRatingForUser.getRating());
                }
            }
        }
        if (ratings.size() >= userRatingsDerivationMinRatings) {
            return Optional.of(new DerivedUserRating(averageAsFloat(ratings), ratings.size()));
        }
    }
    return Optional.empty();
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) Logger(org.slf4j.Logger) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) LoggerFactory(org.slf4j.LoggerFactory) StoppableConsumer(org.haiku.haikudepotserver.support.StoppableConsumer) Instant(java.time.Instant) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) org.haiku.haikudepotserver.dataobjects(org.haiku.haikudepotserver.dataobjects) UserRatingSearchSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingSearchSpecification) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) ChronoUnit(java.time.temporal.ChronoUnit) Service(org.springframework.stereotype.Service) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) VersionCoordinatesComparator(org.haiku.haikudepotserver.support.VersionCoordinatesComparator) Preconditions(com.google.common.base.Preconditions) JobService(org.haiku.haikudepotserver.job.model.JobService) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) UserRatingDerivationJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification) UserRatingService(org.haiku.haikudepotserver.userrating.model.UserRatingService) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) VersionCoordinatesComparator(org.haiku.haikudepotserver.support.VersionCoordinatesComparator)

Example 2 with VersionCoordinatesComparator

use of org.haiku.haikudepotserver.support.VersionCoordinatesComparator in project haikudepotserver by haiku.

the class PkgImportServiceImpl method possiblyReconfigurePersistedPkgVersionToBeLatest.

private void possiblyReconfigurePersistedPkgVersionToBeLatest(ObjectContext objectContext, PkgVersion persistedLatestExistingPkgVersion, PkgVersion persistedPkgVersion) {
    if (null != persistedLatestExistingPkgVersion) {
        VersionCoordinatesComparator versionCoordinatesComparator = new VersionCoordinatesComparator();
        VersionCoordinates persistedPkgVersionCoords = persistedPkgVersion.toVersionCoordinates();
        VersionCoordinates persistedLatestExistingPkgVersionCoords = persistedLatestExistingPkgVersion.toVersionCoordinates();
        int c = versionCoordinatesComparator.compare(persistedPkgVersionCoords, persistedLatestExistingPkgVersionCoords);
        if (c > 0) {
            persistedPkgVersion.setIsLatest(true);
            persistedLatestExistingPkgVersion.setIsLatest(false);
        } else {
            boolean isRealArchitecture = !persistedPkgVersion.getArchitecture().getCode().equals(Architecture.CODE_SOURCE);
            if (0 == c) {
                if (isRealArchitecture) {
                    LOGGER.debug("imported a package version [{}] of [{}] which is the same as the existing [{}]", persistedPkgVersionCoords, persistedPkgVersion.getPkg().getName(), persistedLatestExistingPkgVersionCoords);
                }
            } else {
                if (isRealArchitecture) {
                    // [apl 3.dec.2016]
                    // If the package from the repository is older than the one that is presently marked as latest
                    // then a regression has occurred.  In this case make the imported one be the latest and mark
                    // the later ones as "inactive".
                    List<PkgVersion> pkgVersionsToDeactivate = PkgVersion.getForPkg(objectContext, persistedPkgVersion.getPkg(), persistedPkgVersion.getRepositorySource().getRepository(), false).stream().filter((pv) -> pv.getArchitecture().equals(persistedPkgVersion.getArchitecture())).filter((pv) -> versionCoordinatesComparator.compare(persistedPkgVersionCoords, pv.toVersionCoordinates()) < 0).collect(Collectors.toList());
                    LOGGER.warn("imported a package version {} of {} which is older or the same as the existing {}" + " -- will deactivate {} pkg versions after the imported one and make the" + " imported one as latest", persistedPkgVersionCoords, persistedPkgVersion.getPkg().getName(), persistedLatestExistingPkgVersionCoords, pkgVersionsToDeactivate.size());
                    for (PkgVersion pkgVersionToDeactivate : pkgVersionsToDeactivate) {
                        pkgVersionToDeactivate.setActive(false);
                        pkgVersionToDeactivate.setIsLatest(false);
                        LOGGER.info("deactivated {}", pkgVersionToDeactivate);
                    }
                    persistedPkgVersion.setIsLatest(true);
                }
            }
        }
    } else {
        persistedPkgVersion.setIsLatest(true);
    }
}
Also used : PkgIconService(org.haiku.haikudepotserver.pkg.model.PkgIconService) PkgService(org.haiku.haikudepotserver.pkg.model.PkgService) Iterables(com.google.common.collect.Iterables) ObjectContext(org.apache.cayenne.ObjectContext) URL(java.net.URL) BadPkgIconException(org.haiku.haikudepotserver.pkg.model.BadPkgIconException) LoggerFactory(org.slf4j.LoggerFactory) AttributeContext(org.haiku.pkg.AttributeContext) HpkgHelper(org.haiku.haikudepotserver.support.HpkgHelper) PkgUrlType(org.haiku.haikudepotserver.dataobjects.PkgUrlType) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) ImmutableList(com.google.common.collect.ImmutableList) Service(org.springframework.stereotype.Service) PkgVersionCopyright(org.haiku.haikudepotserver.dataobjects.PkgVersionCopyright) PkgImportService(org.haiku.haikudepotserver.pkg.model.PkgImportService) ByteSource(com.google.common.io.ByteSource) MediaType(org.haiku.haikudepotserver.dataobjects.MediaType) HpkgFileExtractor(org.haiku.pkg.HpkgFileExtractor) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) Architecture(org.haiku.haikudepotserver.dataobjects.Architecture) AttributeId(org.haiku.pkg.model.AttributeId) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ExposureType(org.haiku.haikudepotserver.support.ExposureType) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) URLHelperService(org.haiku.haikudepotserver.support.URLHelperService) List(java.util.List) Stream(java.util.stream.Stream) ObjectId(org.apache.cayenne.ObjectId) PkgVersionLicense(org.haiku.haikudepotserver.dataobjects.PkgVersionLicense) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) VersionCoordinatesComparator(org.haiku.haikudepotserver.support.VersionCoordinatesComparator) PkgSupplement(org.haiku.haikudepotserver.dataobjects.PkgSupplement) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) PkgLocalizationService(org.haiku.haikudepotserver.pkg.model.PkgLocalizationService) PkgVersionUrl(org.haiku.haikudepotserver.dataobjects.PkgVersionUrl) Collections(java.util.Collections) Attribute(org.haiku.pkg.model.Attribute) InputStream(java.io.InputStream) VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) VersionCoordinatesComparator(org.haiku.haikudepotserver.support.VersionCoordinatesComparator) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion)

Aggregations

Preconditions (com.google.common.base.Preconditions)2 Strings (com.google.common.base.Strings)2 Collectors (java.util.stream.Collectors)2 ObjectContext (org.apache.cayenne.ObjectContext)2 VersionCoordinates (org.haiku.haikudepotserver.support.VersionCoordinates)2 VersionCoordinatesComparator (org.haiku.haikudepotserver.support.VersionCoordinatesComparator)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Service (org.springframework.stereotype.Service)2 ComparisonChain (com.google.common.collect.ComparisonChain)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 ByteSource (com.google.common.io.ByteSource)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 URL (java.net.URL)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1