Skip to content

Commit

Permalink
Merge pull request #222 from devgateway/fix/OCE-156-tender-dates-for-…
Browse files Browse the repository at this point in the history
…cost-eff-awards

OCE-156 Make cost effectiveness award endpoint use tender dates
  • Loading branch information
mpostelnicu authored Dec 12, 2016
2 parents d3ceab1 + 6ee2f9c commit 6291a3f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,9 @@
*******************************************************************************/
package org.devgateway.ocds.web.rest.controller;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.skip;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.unwind;
import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.validation.Valid;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import io.swagger.annotations.ApiOperation;
import org.devgateway.ocds.persistence.mongo.Award;
import org.devgateway.ocds.persistence.mongo.Tender;
import org.devgateway.ocds.web.rest.controller.request.GroupingFilterPagingRequest;
Expand All @@ -51,10 +34,24 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import io.swagger.annotations.ApiOperation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.skip;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.unwind;
import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
*
Expand Down Expand Up @@ -84,14 +81,15 @@ public static final class Keys {


@ApiOperation(value = "Cost effectiveness of Awards: Displays the total amount of active awards grouped by year."
+ "The tender entity, for each award, has to have amount value. The year is calculated from awards.date")
+ "The tender entity, for each award, has to have amount value. The year is calculated from "
+ "tender.tenderPeriod.startDate")
@RequestMapping(value = "/api/costEffectivenessAwardAmount",
method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> costEffectivenessAwardAmount(
@ModelAttribute @Valid final YearFilterPagingRequest filter) {

DBObject project = new BasicDBObject();
project.put("year", new BasicDBObject("$year", "$awards.date"));
project.put("year", new BasicDBObject("$year", "$tender.tenderPeriod.startDate"));
project.put("awards.value.amount", 1);
project.put("totalAwardsWithTender", new BasicDBObject("$cond",
Arrays.asList(new BasicDBObject("$gt", Arrays.asList("$tender.tenderPeriod.startDate", null)), 1, 0)));
Expand All @@ -110,10 +108,10 @@ public List<DBObject> costEffectivenessAwardAmount(

Aggregation agg = Aggregation.newAggregation(
match(where("awards").elemMatch(where("status").is(Award.Status.active.toString())).and("awards.date")
.exists(true)),
.exists(true).and("tender.tenderPeriod.startDate").exists(true)),
getMatchDefaultFilterOperation(filter), unwind("$awards"),
match(where("awards.status").is(Award.Status.active.toString()).and("awards.value").exists(true).
andOperator(getYearDefaultFilterCriteria(filter, "awards.date"))),
andOperator(getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))),
new CustomProjectionOperation(project),
group("$year").sum("awardsWithTenderValue").as(Keys.TOTAL_AWARD_AMOUNT).count().as(Keys.TOTAL_AWARDS)
.sum("totalAwardsWithTender").as(Keys.TOTAL_AWARDS_WITH_TENDER),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.devgateway.ocds.web.rest.controller;

import java.math.BigDecimal;
import java.util.List;

import com.mongodb.DBObject;
import org.devgateway.ocds.web.rest.controller.request.GroupingFilterPagingRequest;
import org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.Fields;

import com.mongodb.DBObject;
import java.math.BigDecimal;
import java.util.List;

/**
* @author idobre
Expand All @@ -34,7 +33,7 @@ public void costEffectivenessAwardAmount() throws Exception {
int totalAwardsWithTender = (int) first.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARDS_WITH_TENDER);
double percentageAwardsWithTender = (double) first.
get(CostEffectivenessVisualsController.Keys.PERCENTAGE_AWARDS_WITH_TENDER);
Assert.assertEquals(2015, year);
Assert.assertEquals(2014, year);
Assert.assertEquals(6000000.0, totalAwardAmount, 0);
Assert.assertEquals(1, totalAwards);
Assert.assertEquals(1, totalAwardsWithTender);
Expand All @@ -47,7 +46,7 @@ public void costEffectivenessAwardAmount() throws Exception {
totalAwardsWithTender = (int) second.get(CostEffectivenessVisualsController.Keys.TOTAL_AWARDS_WITH_TENDER);
percentageAwardsWithTender = (double) second.
get(CostEffectivenessVisualsController.Keys.PERCENTAGE_AWARDS_WITH_TENDER);
Assert.assertEquals(2016, year);
Assert.assertEquals(2015, year);
Assert.assertEquals(40000.0, totalAwardAmount, 0);
Assert.assertEquals(1, totalAwards);
Assert.assertEquals(1, totalAwardsWithTender);
Expand Down Expand Up @@ -95,22 +94,22 @@ public void costEffectivenessTenderAwardAmount() throws Exception {
int year = (int) first.get(Fields.UNDERSCORE_ID);
BigDecimal diffTenderAwardAmount = (BigDecimal) first
.get(CostEffectivenessVisualsController.Keys.DIFF_TENDER_AWARD_AMOUNT);
Assert.assertEquals(-5991000, diffTenderAwardAmount.doubleValue(), 0);
Assert.assertEquals(2015, year);
Assert.assertEquals(-5400000, diffTenderAwardAmount.doubleValue(), 0);
Assert.assertEquals(2014, year);

final DBObject second = costEffectivenessTenderAwardAmount.get(1);
year = (int) second.get(Fields.UNDERSCORE_ID);
diffTenderAwardAmount = (BigDecimal) second
.get(CostEffectivenessVisualsController.Keys.DIFF_TENDER_AWARD_AMOUNT);
Assert.assertEquals(2016, year);
Assert.assertEquals(-40000, diffTenderAwardAmount.doubleValue(), 0);
Assert.assertEquals(2015, year);
Assert.assertEquals(-31000, diffTenderAwardAmount.doubleValue(), 0);

final DBObject third = costEffectivenessTenderAwardAmount.get(2);
year = (int) third.get(Fields.UNDERSCORE_ID);
diffTenderAwardAmount = (BigDecimal) third
.get(CostEffectivenessVisualsController.Keys.DIFF_TENDER_AWARD_AMOUNT);
Assert.assertEquals(2014, year);
Assert.assertEquals(600000, diffTenderAwardAmount.doubleValue(), 0);
// final DBObject third = costEffectivenessTenderAwardAmount.get(2);
// year = (int) third.get(Fields.UNDERSCORE_ID);
// diffTenderAwardAmount = (BigDecimal) third
// .get(CostEffectivenessVisualsController.Keys.DIFF_TENDER_AWARD_AMOUNT);
// Assert.assertEquals(2014, year);
// Assert.assertEquals(600000, diffTenderAwardAmount.doubleValue(), 0);
}

}

0 comments on commit 6291a3f

Please sign in to comment.