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