Search in sources :

Example 1 with Page

use of org.jboss.pnc.dto.response.Page in project pnc by project-ncl.

the class RemoteCollectionTest method shouldIterateOverPages.

@Test
public void shouldIterateOverPages() throws Exception {
    AtomicInteger index = new AtomicInteger();
    Function<PageParameters, Page<Entity>> endpoint = (parameters) -> {
        Collection<Entity> collection = new ArrayList();
        collection.add(new Entity(index.getAndIncrement()));
        collection.add(new Entity(index.getAndIncrement()));
        return new Page<>(parameters.getPageIndex(), parameters.getPageSize(), 3, 6, collection);
    };
    RemoteCollectionConfig config = RemoteCollectionConfig.builder().pageSize(2).build();
    RemoteCollection<Entity> collection = new DefaultRemoteCollection<>(endpoint, config);
    Iterator<Entity> iterator = collection.iterator();
    List<Entity> collected = new ArrayList<>();
    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        System.out.println("Received: " + entity.id);
        collected.add(entity);
        if (collected.size() > 10) {
            throw new Exception("Infinite loop.");
        }
    }
    Assert.assertEquals(6, collected.size());
    Assert.assertEquals(5, collected.get(5).id);
}
Also used : List(java.util.List) Iterator(java.util.Iterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Test(org.junit.Test) PageParameters(org.jboss.pnc.rest.api.parameters.PageParameters) Assert(org.junit.Assert) Function(java.util.function.Function) Page(org.jboss.pnc.dto.response.Page) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Page(org.jboss.pnc.dto.response.Page) PageParameters(org.jboss.pnc.rest.api.parameters.PageParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Test(org.junit.Test)

Example 2 with Page

use of org.jboss.pnc.dto.response.Page in project pnc by project-ncl.

the class TemporaryBuildsCleanerTest method shouldReturnOnlyTopLevelTemporaryBuilds.

@Test
public void shouldReturnOnlyTopLevelTemporaryBuilds() throws Exception {
    // with
    // top level BR1
    BuildRecord br1 = initBuildRecordBuilder().temporaryBuild(true).build();
    br1 = buildRecordRepository.save(br1);
    Artifact art1br1 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br1).build();
    Artifact art2br1 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br1).build();
    artifactRepository.save(art1br1);
    artifactRepository.save(art2br1);
    Set<Artifact> depArtBr2 = new HashSet<>();
    // independent BR2
    BuildRecord br2 = initBuildRecordBuilder().temporaryBuild(true).dependencies(depArtBr2).build();
    br2 = buildRecordRepository.save(br2);
    Artifact art1br2 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br2).build();
    Artifact art2br2 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br2).build();
    artifactRepository.save(art1br2);
    artifactRepository.save(art2br2);
    Set<Artifact> depArtBr3 = new HashSet<>();
    depArtBr3.add(art1br1);
    depArtBr3.add(art1br2);
    // create implicitly dependent BR3 (BR3 is dependent on BR1)
    BuildRecord br3 = initBuildRecordBuilder().temporaryBuild(true).dependencies(depArtBr3).build();
    br3 = buildRecordRepository.save(br3);
    Artifact art1br3 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br3).build();
    Artifact art2br3 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br3).build();
    artifactRepository.save(art1br3);
    artifactRepository.save(art2br3);
    Set<Artifact> depArtBr4 = new HashSet<>();
    depArtBr4.add(art2br2);
    depArtBr4.add(art1br3);
    // create implicitly dependent BR4 (BR4 is dependent on BR2 and BR3)
    BuildRecord br4 = initBuildRecordBuilder().temporaryBuild(true).dependencies(depArtBr4).build();
    br4 = buildRecordRepository.save(br4);
    Artifact art1br4 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br4).build();
    Artifact art2br4 = initArtifactBuilder().artifactQuality(ArtifactQuality.TEMPORARY).buildRecord(br4).build();
    artifactRepository.save(art1br4);
    artifactRepository.save(art2br4);
    BuildRecord finalBr1 = br1;
    Condition<Build> hasBr1 = new Condition<>((build -> build.getId().equals(BuildMapper.idMapper.toDto(finalBr1.getId()))), "Is Br1 with id " + BuildMapper.idMapper.toDto(finalBr1.getId()));
    BuildRecord finalBr2 = br2;
    Condition<Build> hasBr2 = new Condition<>((build -> build.getId().equals(BuildMapper.idMapper.toDto(finalBr2.getId()))), "Is Br2 with id " + BuildMapper.idMapper.toDto(finalBr2.getId()));
    BuildRecord finalBr3 = br3;
    Condition<Build> hasBr3 = new Condition<>((build -> build.getId().equals(BuildMapper.idMapper.toDto(finalBr3.getId()))), "Is Br3 with id " + BuildMapper.idMapper.toDto(finalBr3.getId()));
    BuildRecord finalBr4 = br4;
    Condition<Build> hasBr4 = new Condition<>((build -> build.getId().equals(BuildMapper.idMapper.toDto(finalBr4.getId()))), "Is Br4 with id " + BuildMapper.idMapper.toDto(finalBr4.getId()));
    // when #1
    Page<Build> builds = buildProvider.getAllIndependentTemporaryOlderThanTimestamp(0, 50, null, null, new Date().getTime());
    // then #1
    assertThat(builds.getContent()).doNotHave(hasBr1).doNotHave(hasBr2).doNotHave(hasBr3).haveExactly(1, hasBr4);
    // when #2
    temporaryBuildsCleaner.deleteTemporaryBuild(br4.getId(), "");
    builds = buildProvider.getAllIndependentTemporaryOlderThanTimestamp(0, 50, null, null, new Date().getTime());
    // then #2
    assertThat(builds.getContent()).doNotHave(hasBr1).doNotHave(hasBr2).haveExactly(1, hasBr3).doNotHave(hasBr4);
    // when #3
    temporaryBuildsCleaner.deleteTemporaryBuild(br3.getId(), "");
    builds = buildProvider.getAllIndependentTemporaryOlderThanTimestamp(0, 50, null, null, new Date().getTime());
    // then #3
    assertThat(builds.getContent()).haveExactly(1, hasBr1).haveExactly(1, hasBr2).doNotHave(hasBr3).doNotHave(hasBr4);
}
Also used : Condition(org.assertj.core.api.Condition) BuildConfiguration(org.jboss.pnc.model.BuildConfiguration) Arquillian(org.jboss.arquillian.junit.Arquillian) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BuildConfigurationSetRepository(org.jboss.pnc.spi.datastore.repositories.BuildConfigurationSetRepository) LoggerFactory(org.slf4j.LoggerFactory) BuildConfigurationAuditedRepository(org.jboss.pnc.spi.datastore.repositories.BuildConfigurationAuditedRepository) ArtifactRepository(org.jboss.pnc.spi.datastore.repositories.ArtifactRepository) BuildConfigurationAudited(org.jboss.pnc.model.BuildConfigurationAudited) BuildConfigSetRecordRepository(org.jboss.pnc.spi.datastore.repositories.BuildConfigSetRecordRepository) Assert.fail(org.junit.Assert.fail) Deployments.addBuildExecutorMock(org.jboss.pnc.integration.setup.Deployments.addBuildExecutorMock) Deployments(org.jboss.pnc.integration.setup.Deployments) BuildConfigurationRepository(org.jboss.pnc.spi.datastore.repositories.BuildConfigurationRepository) TargetRepository(org.jboss.pnc.model.TargetRepository) User(org.jboss.pnc.model.User) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Artifact(org.jboss.pnc.model.Artifact) EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) MethodHandles(java.lang.invoke.MethodHandles) BuildProvider(org.jboss.pnc.facade.providers.api.BuildProvider) Datastore(org.jboss.pnc.spi.datastore.Datastore) Set(java.util.Set) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) Instant(java.time.Instant) BuildStatus(org.jboss.pnc.enums.BuildStatus) UserRepository(org.jboss.pnc.spi.datastore.repositories.UserRepository) List(java.util.List) RemoteBuildsCleanerMock(org.jboss.pnc.integration.mock.RemoteBuildsCleanerMock) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) PersistenceException(javax.persistence.PersistenceException) HeuristicMixedException(javax.transaction.HeuristicMixedException) TargetRepositoryRepository(org.jboss.pnc.spi.datastore.repositories.TargetRepositoryRepository) BuildConfigSetRecord(org.jboss.pnc.model.BuildConfigSetRecord) RunWith(org.junit.runner.RunWith) ArtifactQuality(org.jboss.pnc.enums.ArtifactQuality) BuildConfigurationSet(org.jboss.pnc.model.BuildConfigurationSet) BuildMapper(org.jboss.pnc.mapper.api.BuildMapper) BuildRecordRepository(org.jboss.pnc.spi.datastore.repositories.BuildRecordRepository) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Inject(javax.inject.Inject) HashSet(java.util.HashSet) RollbackException(javax.transaction.RollbackException) ProductVersionRepository(org.jboss.pnc.spi.datastore.repositories.ProductVersionRepository) TemporaryBuildsCleaner(org.jboss.pnc.coordinator.maintenance.TemporaryBuildsCleaner) Before(org.junit.Before) ValidationException(org.jboss.pnc.spi.exception.ValidationException) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) UserTransaction(javax.transaction.UserTransaction) Test(org.junit.Test) BuildRecord(org.jboss.pnc.model.BuildRecord) EntityManager(javax.persistence.EntityManager) Page(org.jboss.pnc.dto.response.Page) NotSupportedException(javax.transaction.NotSupportedException) Build(org.jboss.pnc.dto.Build) ChronoUnit(java.time.temporal.ChronoUnit) Assert.assertNull(org.junit.Assert.assertNull) Deployment(org.jboss.arquillian.container.test.api.Deployment) SystemException(javax.transaction.SystemException) Condition(org.assertj.core.api.Condition) Sequence(org.jboss.pnc.common.concurrent.Sequence) Assert.assertEquals(org.junit.Assert.assertEquals) Build(org.jboss.pnc.dto.Build) BuildRecord(org.jboss.pnc.model.BuildRecord) Artifact(org.jboss.pnc.model.Artifact) Date(java.util.Date) HashSet(java.util.HashSet) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Test(org.junit.Test)

Example 3 with Page

use of org.jboss.pnc.dto.response.Page in project pnc by project-ncl.

the class BuildProviderImpl method getBuilds.

/**
 * Returns the page of builds (running or finished) filtered by given BuildPageInfo and predicate.
 */
private Page<Build> getBuilds(BuildPageInfo pageInfo, java.util.function.Predicate<BuildTask> predicate, Predicate<BuildRecord> dbPredicate) {
    List<Build> runningBuilds = readRunningBuilds(pageInfo, predicate);
    int firstPossibleDBIndex = pageInfo.getPageIndex() * pageInfo.getPageSize() - runningBuilds.size();
    int lastPossibleDBIndex = (pageInfo.getPageIndex() + 1) * pageInfo.getPageSize() - 1;
    int toSkip = min(runningBuilds.size(), pageInfo.getPageIndex() * pageInfo.getPageSize());
    Predicate<BuildRecord>[] predicates = preparePredicates(dbPredicate, pageInfo.getQ(), pageInfo.getBuildConfigName());
    Comparator<Build> comparing = Comparator.comparing(Build::getSubmitTime).reversed();
    if (!StringUtils.isEmpty(pageInfo.getSort())) {
        comparing = rsqlPredicateProducer.getComparator(pageInfo.getSort());
    }
    SortInfo sortInfo = rsqlPredicateProducer.getSortInfo(type, pageInfo.getSort());
    MergeIterator<Build> builds = new MergeIterator(runningBuilds.iterator(), new BuildIterator(firstPossibleDBIndex, lastPossibleDBIndex, pageInfo.getPageSize(), sortInfo, predicates), comparing);
    List<Build> resultList = StreamSupport.stream(Spliterators.spliteratorUnknownSize(builds, Spliterator.ORDERED | Spliterator.SORTED), false).skip(toSkip).limit(pageInfo.getPageSize()).collect(Collectors.toList());
    int hits = repository.count(predicates) + runningBuilds.size();
    return new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize(), (int) Math.ceil((double) hits / pageInfo.getPageSize()), hits, resultList);
}
Also used : BuildRecordPredicates.temporaryBuild(org.jboss.pnc.spi.datastore.predicates.BuildRecordPredicates.temporaryBuild) Build(org.jboss.pnc.dto.Build) Page(org.jboss.pnc.dto.response.Page) Predicate(org.jboss.pnc.spi.datastore.repositories.api.Predicate) SortInfo(org.jboss.pnc.spi.datastore.repositories.api.SortInfo) MergeIterator(org.jboss.pnc.facade.util.MergeIterator)

Example 4 with Page

use of org.jboss.pnc.dto.response.Page in project pnc by project-ncl.

the class BuildProviderImpl method getByAttribute.

public Page<Build> getByAttribute(BuildPageInfo buildPageInfo, Map<String, String> attributeConstraints) {
    Set<Predicate<BuildRecord>> predicates = new HashSet<>();
    for (Map.Entry<String, String> entry : attributeConstraints.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (key.startsWith("!")) {
            predicates.add(withoutAttribute(key.substring(1)));
        } else {
            predicates.add(withAttribute(key, value));
        }
    }
    Predicate<BuildRecord> queryPredicate = rsqlPredicateProducer.getCriteriaPredicate(BuildRecord.class, buildPageInfo.getQ());
    predicates.add(queryPredicate);
    Predicate<BuildRecord>[] predicatesArray = predicates.toArray(new Predicate[predicates.size()]);
    PageInfo pageInfo = toPageInfo(buildPageInfo);
    SortInfo sortInfo = rsqlPredicateProducer.getSortInfo(type, buildPageInfo.getSort());
    List<BuildRecord> resultList = ((BuildRecordRepository) BuildProviderImpl.this.repository).queryWithPredicatesUsingCursor(pageInfo, sortInfo, predicatesArray);
    int hits = repository.count(predicatesArray);
    return new Page<>(buildPageInfo.getPageIndex(), buildPageInfo.getPageSize(), hits, resultList.stream().map(b -> mapper.toDTO(b)).collect(Collectors.toList()));
}
Also used : Page(org.jboss.pnc.dto.response.Page) BuildRecord(org.jboss.pnc.model.BuildRecord) Predicate(org.jboss.pnc.spi.datastore.repositories.api.Predicate) SortInfo(org.jboss.pnc.spi.datastore.repositories.api.SortInfo) PageInfo(org.jboss.pnc.spi.datastore.repositories.api.PageInfo) BuildPageInfo(org.jboss.pnc.facade.providers.api.BuildPageInfo) DefaultPageInfo(org.jboss.pnc.spi.datastore.repositories.api.impl.DefaultPageInfo) Map(java.util.Map) BuildRecordRepository(org.jboss.pnc.spi.datastore.repositories.BuildRecordRepository) HashSet(java.util.HashSet)

Example 5 with Page

use of org.jboss.pnc.dto.response.Page in project pnc by project-ncl.

the class BuildProviderImpl method getAllBuildRecordInsightsNewerThanTimestamp.

@Override
public Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(int pageIndex, int pageSize, Date lastupdatetime) {
    logger.debug("Executing getAllBuildRecordInsightsNewerThanTimestamp with parameters pageIndex: {}, pageSize: {}, lastupdatetime: {}", pageIndex, pageSize, lastupdatetime);
    int count = buildRecordRepository.countAllBuildRecordInsightsNewerThanTimestamp(lastupdatetime);
    logger.debug("BuildRecordInsightsCount: {}", count);
    int totalPages = (int) Math.ceil((count) / (double) pageSize);
    logger.debug("TotalPages of BuildRecordInsightsCount: {}", totalPages);
    List<BuildRecordInsights> content = new ArrayList<BuildRecordInsights>();
    if (count > 0) {
        int offset = pageIndex * pageSize;
        logger.debug("offset: {}", offset);
        List<Object[]> rawBuildInsights = buildRecordRepository.getAllBuildRecordInsightsNewerThanTimestamp(lastupdatetime, pageSize, offset);
        for (Object[] rawBuildInsight : rawBuildInsights) {
            Long buildRecordId = ((BigInteger) rawBuildInsight[0]).longValue();
            String buildContentId = (String) rawBuildInsight[1];
            buildContentId = StringUtils.isEmpty(buildContentId) ? LongBase32IdConverter.toString(buildRecordId) : buildContentId;
            Date submitTime = (Date) rawBuildInsight[2];
            Date startTime = (Date) rawBuildInsight[3];
            Date endTime = (Date) rawBuildInsight[4];
            Date lastTime = (Date) rawBuildInsight[5];
            Integer submitYear = (Integer) rawBuildInsight[6];
            Integer submitMonth = (Integer) rawBuildInsight[7];
            Integer submitQuarter = (Integer) rawBuildInsight[8];
            String status = (String) rawBuildInsight[9];
            Boolean temporaryBuild = (Boolean) rawBuildInsight[10];
            Boolean autoAlign = (Boolean) rawBuildInsight[11];
            Boolean brewPullActive = (Boolean) rawBuildInsight[12];
            String buildType = (String) rawBuildInsight[13];
            String executionRootName = (String) rawBuildInsight[14];
            String executionRootVersion = (String) rawBuildInsight[15];
            Integer userId = (Integer) rawBuildInsight[16];
            String username = (String) rawBuildInsight[17];
            Integer buildConfigurationId = (Integer) rawBuildInsight[18];
            Integer buildConfigurationRev = (Integer) rawBuildInsight[19];
            String buildConfigurationName = (String) rawBuildInsight[20];
            Integer buildConfigSetRecordId = (Integer) rawBuildInsight[21];
            Integer productMilestoneId = (Integer) rawBuildInsight[22];
            String productMilestoneVersion = (String) rawBuildInsight[23];
            Integer projectId = (Integer) rawBuildInsight[24];
            String projectName = (String) rawBuildInsight[25];
            Integer productVersionId = (Integer) rawBuildInsight[26];
            String productVersion = (String) rawBuildInsight[27];
            Integer productId = (Integer) rawBuildInsight[28];
            String productName = (String) rawBuildInsight[29];
            BuildRecordInsights buildRecordInsights = BuildRecordInsights.builder().buildId(buildRecordId).buildContentId(buildContentId).submitTime(TimeUtils.toInstant(submitTime)).startTime(TimeUtils.toInstant(startTime)).endTime(TimeUtils.toInstant(endTime)).lastUpdateTime(TimeUtils.toInstant(lastTime)).submitYear(submitYear).submitMonth(submitMonth).submitQuarter(submitQuarter).status(status).temporarybuild(temporaryBuild).autoalign(autoAlign).brewpullactive(brewPullActive).buildType(buildType).executionRootName(executionRootName).executionRootVersion(executionRootVersion).userId(userId).username(username).buildConfigurationId(buildConfigurationId).buildConfigurationRev(buildConfigurationRev).buildConfigurationName(buildConfigurationName).buildConfigSetRecordId(buildConfigSetRecordId).productMilestoneId(productMilestoneId).productMilestoneVersion(productMilestoneVersion).projectId(projectId).projectName(projectName).productVersionId(productVersionId).productVersionVersion(productVersion).productId(productId).productName(productName).build();
            content.add(buildRecordInsights);
        }
    }
    return new Page<>(pageIndex, pageSize, totalPages, count, content);
}
Also used : ArrayList(java.util.ArrayList) Page(org.jboss.pnc.dto.response.Page) Date(java.util.Date) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) BuildRecordInsights(org.jboss.pnc.dto.insights.BuildRecordInsights)

Aggregations

Page (org.jboss.pnc.dto.response.Page)11 Date (java.util.Date)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Predicate (org.jboss.pnc.spi.datastore.repositories.api.Predicate)4 SortInfo (org.jboss.pnc.spi.datastore.repositories.api.SortInfo)4 Test (org.junit.Test)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 BuildRecord (org.jboss.pnc.model.BuildRecord)3 Instant (java.time.Instant)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Inject (javax.inject.Inject)2 EntityManager (javax.persistence.EntityManager)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Condition (org.assertj.core.api.Condition)2