use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.
the class TotalCancelledTendersByYearController method totalCancelledTendersByYearByRationale.
@ApiOperation(value = "Total Cancelled tenders by year by cancel reason. " + "The tender amount is read from tender.value." + "The tender status has to be 'cancelled'. The year is retrieved from tender.tenderPeriod.startDate." + "The cancellation reason is read from tender.cancellationRationale.")
@RequestMapping(value = "/api/totalCancelledTendersByYearByRationale", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalCancelledTendersByYearByRationale(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
DBObject project = new BasicDBObject();
project.put(Fields.UNDERSCORE_ID, 0);
addYearlyMonthlyProjection(filter, project, "$tender.tenderPeriod.startDate");
project.put("tender.value.amount", 1);
project.put("tender.cancellationRationale", 1);
Aggregation agg = newAggregation(match(where("tender.status").is("cancelled").and("tender.tenderPeriod.startDate").exists(true).andOperator(getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))), new CustomProjectionOperation(project), group(getYearlyMonthlyGroupingFields(filter, "$tender.cancellationRationale")).sum("$tender.value.amount").as(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), getSortByYearMonth(filter));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
List<DBObject> list = results.getMappedResults();
return list;
}
use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.
the class TotalCancelledTendersByYearController method totalCancelledTendersByYear.
@ApiOperation(value = "Total Cancelled tenders by year. The tender amount is read from tender.value." + "The tender status has to be 'cancelled'. The year is retrieved from tender.tenderPeriod.startDate.")
@RequestMapping(value = "/api/totalCancelledTendersByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalCancelledTendersByYear(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
DBObject project = new BasicDBObject();
addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
project.put("tender.value.amount", 1);
Aggregation agg = newAggregation(match(where("tender.status").is("cancelled").and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomOperation(new BasicDBObject("$project", project)), getYearlyMonthlyGroupingOperation(filter).sum("$tender.value.amount").as(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), getSortByYearMonth(filter));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
List<DBObject> list = results.getMappedResults();
return list;
}
use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.
the class VNImportService method getMaxTenderValue.
private BigDecimal getMaxTenderValue() {
Aggregation agg = Aggregation.newAggregation(match(where("tender.value.amount").exists(true)), project().and("tender.value.amount").as("tender.value.amount"), group().max("tender.value.amount").as("maxTenderValue"), project().andInclude("maxTenderValue").andExclude(Fields.UNDERSCORE_ID));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
return results.getMappedResults().size() == 0 ? BigDecimal.ZERO : BigDecimal.valueOf((double) results.getMappedResults().get(0).get("maxTenderValue"));
}
use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.
the class AbstractFlagReleaseSearchController method releaseFlagSearch.
@JsonView(Views.Internal.class)
public List<DBObject> releaseFlagSearch(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
Aggregation agg = newAggregation(match(where("flags.flaggedStats.0").exists(true).and(getFlagProperty()).is(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags.flaggedStats"), match(where(getFlagProperty()).is(true)), project("ocid", "tender.procuringEntity.name", "tender.tenderPeriod", "flags", "tender.title", "tag").and("tender.value").as("tender.value").and("awards.value").as("awards.value").andExclude(Fields.UNDERSCORE_ID), sort(Sort.Direction.DESC, "flags.flaggedStats.count"), skip(filter.getSkip()), limit(filter.getPageSize()));
AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
List<DBObject> list = results.getMappedResults();
return list;
}
use of org.springframework.data.mongodb.core.aggregation.Aggregation in project spring-data-mongodb by spring-projects.
the class MongoTemplate method aggregateStream.
@SuppressWarnings("ConstantConditions")
protected <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType, @Nullable AggregationOperationContext context) {
Assert.hasText(collectionName, "Collection name must not be null or empty!");
Assert.notNull(aggregation, "Aggregation pipeline must not be null!");
Assert.notNull(outputType, "Output type must not be null!");
Assert.isTrue(!aggregation.getOptions().isExplain(), "Can't use explain option with streaming!");
AggregationOperationContext rootContext = context == null ? Aggregation.DEFAULT_CONTEXT : context;
AggregationOptions options = aggregation.getOptions();
List<Document> pipeline = aggregation.toPipeline(rootContext);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Streaming aggregation: {} in collection {}", serializeToJsonSafely(pipeline), collectionName);
}
ReadDocumentCallback<O> readCallback = new ReadDocumentCallback<>(mongoConverter, outputType, collectionName);
return execute(collectionName, (CollectionCallback<CloseableIterator<O>>) collection -> {
AggregateIterable<Document> cursor = collection.aggregate(pipeline, Document.class).allowDiskUse(options.isAllowDiskUse()).useCursor(true);
if (options.getCursorBatchSize() != null) {
cursor = cursor.batchSize(options.getCursorBatchSize());
}
if (options.getCollation().isPresent()) {
cursor = cursor.collation(options.getCollation().map(Collation::toMongoCollation).get());
}
return new CloseableIterableCursorAdapter<>(cursor.iterator(), exceptionTranslator, readCallback);
});
}
Aggregations