Search in sources :

Example 96 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class CostEffectivenessVisualsController method costEffectivenessTenderAmount.

@ApiOperation(value = "Cost effectiveness of Tenders:" + " Displays the total amount of the active tenders that have active awards, " + "grouped by year. Only tenders.status=active" + "are taken into account. The year is calculated from tenderPeriod.startDate")
@RequestMapping(value = "/api/costEffectivenessTenderAmount", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> costEffectivenessTenderAmount(@ModelAttribute @Valid final GroupingFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put("year", new BasicDBObject("$year", ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)));
    addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    project.put(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT, 1);
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put("tenderWithAwards", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList(ref(MongoConstants.FieldNames.AWARDS_STATUS), Award.Status.active.toString())), 1, 0)));
    project.put("tenderWithAwardsValue", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$eq", Arrays.asList(ref(MongoConstants.FieldNames.AWARDS_STATUS), Award.Status.active.toString())), ref(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT), 0)));
    project.putAll(filterProjectMap);
    DBObject group1 = new BasicDBObject();
    group1.put(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF);
    addYearlyMonthlyGroupingOperationFirst(filter, group1);
    group1.put("tenderWithAwards", new BasicDBObject("$max", "$tenderWithAwards"));
    group1.put("tenderWithAwardsValue", new BasicDBObject("$max", "$tenderWithAwardsValue"));
    group1.put("tenderAmount", new BasicDBObject("$first", ref(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT)));
    filterProjectMap.forEach((k, v) -> group1.put(k.replace(".", ""), k.equals("tender.items.classification._id") ? new BasicDBObject("$first", new BasicDBObject("$arrayElemAt", Arrays.asList("$" + k, 0))) : new BasicDBObject("$first", "$" + k)));
    Aggregation agg = Aggregation.newAggregation(match(where(MongoConstants.FieldNames.TENDER_STATUS).is(Tender.Status.active.toString()).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), getMatchDefaultFilterOperation(filter), new CustomUnwindOperation("$awards", true), new CustomProjectionOperation(project), new CustomGroupingOperation(group1), getTopXFilterOperation(filter, getYearlyMonthlyGroupingFields(filter)).sum("tenderWithAwardsValue").as(Keys.TOTAL_TENDER_AMOUNT).count().as(Keys.TOTAL_TENDERS).sum("tenderWithAwards").as(Keys.TOTAL_TENDER_WITH_AWARDS), project(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS).andInclude(Fields.from(Fields.field(Fields.UNDERSCORE_ID, Fields.UNDERSCORE_ID_REF))).and(Keys.TOTAL_TENDER_WITH_AWARDS).divide(Keys.TOTAL_TENDERS).as(Keys.FRACTION_TENDERS_WITH_AWARDS), project(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS, Fields.UNDERSCORE_ID).and(Keys.FRACTION_TENDERS_WITH_AWARDS).multiply(100).as(Keys.PERCENTAGE_TENDERS_WITH_AWARDS), (filter.getGroupByCategory() == null ? transformYearlyGrouping(filter) : project()).andInclude(Keys.TOTAL_TENDER_AMOUNT, Keys.TOTAL_TENDERS, Keys.TOTAL_TENDER_WITH_AWARDS, Keys.PERCENTAGE_TENDERS_WITH_AWARDS), filter.getGroupByCategory() == null ? getSortByYearMonth(filter) : sort(Sort.Direction.DESC, Keys.TOTAL_TENDER_AMOUNT), skip(filter.getSkip()), limit(filter.getPageSize())).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
    return releaseAgg(agg);
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomUnwindOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation) CustomGroupingOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 97 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class FrequentSuppliersTimeIntervalController method frequentSuppliersTimeInterval.

@ApiOperation(value = "Returns the frequent suppliers of a procuringEntity split by a time interval. " + "The time interval is " + "a parameter and represents the number of days to take as interval, starting with today and going back " + "till the last award date. The awards are grouped by procuringEntity, supplier and the time interval. " + "maxAwards parameter is used to designate what is the maximum number of awards granted to one supplier " + "by the same procuringEntity inside one timeInterval. The default value for maxAwards is 3 (days) and the" + " default value for intervalDays is 365.")
@RequestMapping(value = "/api/frequentSuppliersTimeInterval", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<FrequentSuppliersResponse> frequentSuppliersTimeInterval(@RequestParam(defaultValue = "365", required = false) Integer intervalDays, @RequestParam(defaultValue = "3", required = false) Integer maxAwards, @RequestParam(required = false) Date now) {
    if (now == null) {
        now = new Date();
    }
    DBObject project = new BasicDBObject();
    project.put(MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID, 1);
    project.put(MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID, 1);
    project.put(MongoConstants.FieldNames.AWARDS_DATE, 1);
    project.put(Fields.UNDERSCORE_ID, 0);
    project.put("timeInterval", new BasicDBObject("$ceil", new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$divide", Arrays.asList(new BasicDBObject("$subtract", Arrays.asList(now, ref(MongoConstants.FieldNames.AWARDS_DATE))), DAY_MS)), intervalDays))));
    Aggregation agg = Aggregation.newAggregation(match(where("tender.procuringEntity").exists(true).and("awards.suppliers.0").exists(true).and(MongoConstants.FieldNames.AWARDS_DATE).exists(true)), unwind("awards"), unwind("awards.suppliers"), new CustomProjectionOperation(project), group(Fields.from(Fields.field("procuringEntityId", MongoConstants.FieldNames.TENDER_PROCURING_ENTITY_ID), Fields.field("supplierId", MongoConstants.FieldNames.AWARDS_SUPPLIERS_ID), Fields.field("timeInterval", "timeInterval"))).count().as("count"), project("count").and("identifier").previousOperation(), match(where("count").gt(maxAwards)), sort(Sort.Direction.DESC, "count"));
    return releaseAgg(agg, FrequentSuppliersResponse.class);
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 98 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class FundingByLocationController method qualityFundingByTenderDeliveryLocation.

@ApiOperation("Calculates percentage of releases with tender with at least one specified delivery location," + " that is the array tender.items.deliveryLocation has to have items." + "Filters out stub tenders, therefore tender.tenderPeriod.startDate has to exist.")
@RequestMapping(value = "/api/qualityFundingByTenderDeliveryLocation", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> qualityFundingByTenderDeliveryLocation(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.putAll(filterProjectMap);
    project.put(Fields.UNDERSCORE_ID, "$tender._id");
    project.put("tenderItemsDeliveryLocation", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$tender.items.deliveryLocation", null)), 1, 0)));
    DBObject project1 = new BasicDBObject();
    project1.put(Fields.UNDERSCORE_ID, 0);
    project1.put(Keys.TOTAL_TENDERS_WITH_START_DATE, 1);
    project1.put(Keys.TOTAL_TENDERS_WITH_START_DATE_AND_LOCATION, 1);
    project1.put(Keys.PERCENT_TENDERS_WITH_START_DATE_AND_LOCATION, new BasicDBObject("$multiply", Arrays.asList(new BasicDBObject("$divide", Arrays.asList("$totalTendersWithStartDateAndLocation", "$totalTendersWithStartDate")), 100)));
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("$tender.items"), new CustomProjectionOperation(project), group(Fields.UNDERSCORE_ID_REF).max("tenderItemsDeliveryLocation").as("hasTenderItemsDeliverLocation"), group().count().as("totalTendersWithStartDate").sum("hasTenderItemsDeliverLocation").as(Keys.TOTAL_TENDERS_WITH_START_DATE_AND_LOCATION), new CustomProjectionOperation(project1));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 99 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class FundingByLocationController method fundingByTenderDeliveryLocation.

@ApiOperation(value = "Total estimated funding (tender.value) grouped by " + "tender.items.deliveryLocation and also grouped by year." + " The endpoint also returns the count of tenders for each location. " + "It responds to all filters. The year is calculated based on tender.tenderPeriod.startDate")
@RequestMapping(value = "/api/fundingByTenderDeliveryLocation", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> fundingByTenderDeliveryLocation(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put("tender.items.deliveryLocation", 1);
    project.put(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT, 1);
    addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
    Aggregation agg = newAggregation(match(where("tender").exists(true).and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), unwind("$tender.items"), unwind("$tender.items.deliveryLocation"), project(getYearlyMonthlyGroupingFields(filter)).and(MongoConstants.FieldNames.TENDER_VALUE_AMOUNT).as("tenderAmount").and("tender.items.deliveryLocation").as("deliveryLocation"), match(where("deliveryLocation.geometry.coordinates.0").exists(true)), group(getYearlyMonthlyGroupingFields(filter, Keys.ITEMS_DELIVERY_LOCATION)).sum("tenderAmount").as(Keys.TOTAL_TENDERS_AMOUNT).count().as(Keys.TENDERS_COUNT), getSortByYearMonth(filter));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 100 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project oc-explorer by devgateway.

the class PercentageAmountAwardedController method percentTendersCancelled.

@ApiOperation("Calculate percentage of awards awarded to a list of suppliers vs total awards. Filters by all" + " filters. Careful using supplierId filter here!" + " It has a different signification than for other endpoints.")
@RequestMapping(value = "/api/percentageAmountAwarded", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> percentTendersCancelled(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    Assert.notEmpty(filter.getProcuringEntityId(), "Must provide at least one procuringEntity!");
    Assert.notEmpty(filter.getSupplierId(), "Must provide at least one supplierId!");
    Aggregation agg = newAggregation(getMatchYearDefaultFilterOperation(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE), match(where("tender.procuringEntity").exists(true).and("awards.suppliers.0").exists(true).andOperator(getProcuringEntityIdCriteria(filter))), unwind("awards"), match(where(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString())), facet().and(match(getSupplierIdCriteria(filter.awardFiltering())), group().sum(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).as("sum")).as("totalAwardedToSuppliers").and(group().sum(MongoConstants.FieldNames.AWARDS_VALUE_AMOUNT).as("sum")).as("totalAwarded"), unwind("totalAwardedToSuppliers"), unwind("totalAwarded"), new CustomProjectionOperation(new BasicDBObject("percentage", getPercentageMongoOp("totalAwardedToSuppliers.sum", "totalAwarded.sum")).append("totalAwardedToSuppliers.sum", 1).append("totalAwarded.sum", 1)));
    return releaseAgg(agg);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)113 DBObject (com.mongodb.DBObject)79 ApiOperation (io.swagger.annotations.ApiOperation)79 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)79 BasicDBObject (com.mongodb.BasicDBObject)73 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)73 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)55 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)22 Test (org.junit.Test)20 CustomOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation)16 Document (org.bson.Document)14 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)5 JsonView (com.fasterxml.jackson.annotation.JsonView)4 Criteria (org.springframework.data.mongodb.core.query.Criteria)4 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)3 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 List (java.util.List)2 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)2 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2