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();
}
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);
}
}
Aggregations