Skip to content

Commit

Permalink
feat: cycles filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanoshadjipetrou committed May 22, 2024
1 parent 353dab8 commit 183914d
Show file tree
Hide file tree
Showing 18 changed files with 349 additions and 56 deletions.
8 changes: 8 additions & 0 deletions src/config/mapping/allocations/cumulative-by-cycles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dataPath": "value",
"value": "value",
"cycle": "periodCovered",
"component": "activityArea.name",
"colors": ["#0A2840", "#013E77", "#00B5AE", "#C3EDFD", "#D9D9D9"],
"urlParams": "?$apply=filter(indicatorName eq 'Communicated Allocation - Reference Rate' AND financialDataSet eq 'CommunicatedAllocation_ReferenceRate'<filterString>)/groupby((periodCovered,activityArea/name),aggregate(actualAmount with sum as value))"
}
8 changes: 3 additions & 5 deletions src/config/mapping/allocations/cycles.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"dataPath": "value",
"value": "value",
"cycle": "periodCovered",
"component": "activityArea.name",
"colors": ["#0A2840", "#013E77", "#00B5AE", "#C3EDFD", "#D9D9D9"],
"urlParams": "?$apply=filter(indicatorName eq 'Communicated Allocation - Reference Rate' AND financialDataSet eq 'CommunicatedAllocation_ReferenceRate'<filterString>)/groupby((periodCovered,activityArea/name),aggregate(actualAmount with sum as value))"
"cycleFrom": "periodCovered",
"cycleTo": "",
"urlParams": "?$apply=filter(indicatorName eq 'Communicated Allocation - Reference Rate' AND financialDataSet eq 'CommunicatedAllocation_ReferenceRate')/groupby((periodCovered))&$orderby=periodCovered asc"
}
6 changes: 3 additions & 3 deletions src/config/mapping/budgets/cycles.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dataPath": "value",
"from": "implementationPeriod/periodFrom",
"to": "implementationPeriod/periodTo",
"urlParams": "?$apply=filter(contains(indicatorName, 'reference') AND financialDataSet eq 'GrantBudget_ReferenceRate')/groupby((implementationPeriod/perioFrom,implementationPeriod/periodTo))"
"cycleFrom": "implementationPeriod.periodFrom",
"cycleTo": "implementationPeriod.periodTo",
"urlParams": "?$apply=filter(contains(indicatorName, 'reference') AND financialDataSet eq 'GrantBudget_ReferenceRate')/groupby((implementationPeriod/periodFrom,implementationPeriod/periodTo))&$orderby=implementationPeriod/periodFrom asc"
}
6 changes: 6 additions & 0 deletions src/config/mapping/disbursements/cycles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dataPath": "value",
"cycleFrom": "implementationPeriod.periodFrom",
"cycleTo": "implementationPeriod.periodTo",
"urlParams": "?$apply=filter(contains(indicatorName, 'reference') AND financialDataSet eq 'Disbursement_ReferenceRate')/groupby((implementationPeriod/periodFrom,implementationPeriod/periodTo))&$orderby=implementationPeriod/periodFrom asc"
}
2 changes: 2 additions & 0 deletions src/config/mapping/disbursements/lineChart.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"dataPath": "value",
"count": "@odata.count",
"cycle": "periodFrom",
"value": "value",
"line": "activityArea.name",
"urlParams": "?$apply=filter(contains(indicatorName, 'reference') AND financialDataSet eq 'Disbursement_ReferenceRate'<filterString>)/groupby((periodFrom,activityArea/name),aggregate(actualAmount with sum as value))",
"activitiesCountUrlParams": "?$count=true&$apply=filter(contains(indicatorName, 'reference') AND financialDataSet eq 'Disbursement_ReferenceRate'<filterString>)/groupby((implementationPeriod/grant/code))",
"colors": ["#0A2840", "#013E77", "#00B5AE", "#C3EDFD"]
}
6 changes: 6 additions & 0 deletions src/config/mapping/expenditures/cycles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dataPath": "value",
"cycleFrom": "implementationPeriod.periodFrom",
"cycleTo": "implementationPeriod.periodTo",
"urlParams": "?$apply=filter(indicatorName eq 'Expenditure: Module-Intervention - Reference Rate' AND isLatestReported eq true)/groupby((implementationPeriod/periodFrom,implementationPeriod/periodTo))&$orderby=implementationPeriod/periodFrom asc"
}
6 changes: 6 additions & 0 deletions src/config/mapping/fundingrequests/cycles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dataPath": "value",
"cycleFrom": "implementationPeriod.periodFrom",
"cycleTo": "implementationPeriod.periodTo",
"urlParams": "?$apply=groupby((implementationPeriod/periodFrom,implementationPeriod/periodTo))&$orderby=implementationPeriod/periodFrom asc"
}
58 changes: 29 additions & 29 deletions src/config/mapping/globalsearch/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@
],
"options": []
},
{
"name": "Grant(s)",
"type": "Grant",
"link": "/grant/<code>",
"url": "https://api-gf-api-gf-02.azurewebsites.net/v4/odata/Grants?$select=code,status&$expand=status(select=statusName)&$filter=(<filterStr>)&$orderby=status/statusName asc",
"itemname": "<code>",
"filterFields": [
"geography/code",
"geography/name",
"activityArea/parent/parent/name",
"title",
"code",
"principalRecipient/name",
"principalRecipient/shortName",
"status/statusName"
],
"filterTemplate": "contains(<field>,<value>)",
"order": ["asc", "asc"],
"mappings": [
"value[]",
{
"code": "code",
"type": "status/statusName",
"order": "status/statusName",
"order1": "code"
}
],
"options": []
},
{
"name": "Partner(s)",
"type": "Partner",
Expand Down Expand Up @@ -68,35 +97,6 @@
],
"options": []
},
{
"name": "Grant(s)",
"type": "Grant",
"link": "/grant/<code>",
"url": "https://api-gf-api-gf-02.azurewebsites.net/v4/odata/Grants?$select=code,status&$expand=status(select=statusName)&$filter=(<filterStr>)&$orderby=status/statusName asc",
"itemname": "<code>",
"filterFields": [
"geography/code",
"geography/name",
"activityArea/parent/parent/name",
"title",
"code",
"principalRecipient/name",
"principalRecipient/shortName",
"status/statusName"
],
"filterTemplate": "contains(<field>,<value>)",
"order": ["asc", "asc"],
"mappings": [
"value[]",
{
"code": "code",
"type": "status/statusName",
"order": "status/statusName",
"order1": "code"
}
],
"options": []
},
{
"name": "Result(s)",
"type": "Result",
Expand Down
6 changes: 6 additions & 0 deletions src/config/mapping/pledgescontributions/cycles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dataPath": "value",
"cycleFrom": "periodCovered",
"cycleTo": "",
"urlParams": "?$apply=filter(financialDataSet eq 'Pledges_Contributions')/groupby((periodCovered))&$orderby=periodCovered asc"
}
6 changes: 6 additions & 0 deletions src/config/mapping/results/cycles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dataPath": "value",
"cycleFrom": "resultValueYear",
"cycleTo": "",
"urlParams": "?$apply=filter(programmaticDataset eq 'Annual_Results')/groupby((resultValueYear))&$orderby=resultValueYear asc"
}
56 changes: 48 additions & 8 deletions src/controllers/allocations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import axios, {AxiosResponse} from 'axios';
import _ from 'lodash';
import querystring from 'querystring';
import filtering from '../config/filtering/index.json';
import AllocationCumulativeByCyclesFieldsMapping from '../config/mapping/allocations/cumulative-by-cycles.json';
import AllocationCyclesFieldsMapping from '../config/mapping/allocations/cycles.json';
import AllocationsDrilldownFieldsMapping from '../config/mapping/allocations/drilldown.json';
import AllocationsGeomapFieldsMapping from '../config/mapping/allocations/geomap.json';
Expand Down Expand Up @@ -99,7 +100,7 @@ export class AllocationsController {
async cumulativeByCycles() {
let filterString = filterFinancialIndicators(
this.req.query,
AllocationCyclesFieldsMapping.urlParams,
AllocationCumulativeByCyclesFieldsMapping.urlParams,
);
const url = `${urls.FINANCIAL_INDICATORS}/${filterString}`;

Expand All @@ -108,7 +109,7 @@ export class AllocationsController {
.then((resp: AxiosResponse) => {
const raw = _.get(
resp.data,
AllocationCyclesFieldsMapping.dataPath,
AllocationCumulativeByCyclesFieldsMapping.dataPath,
[],
);

Expand All @@ -122,24 +123,28 @@ export class AllocationsController {

const groupedByCycle = _.groupBy(
raw,
AllocationCyclesFieldsMapping.cycle,
AllocationCumulativeByCyclesFieldsMapping.cycle,
);

const cycles = Object.keys(groupedByCycle);

const groupedByComponent = _.groupBy(
raw,
AllocationCyclesFieldsMapping.component,
AllocationCumulativeByCyclesFieldsMapping.component,
);

_.forEach(groupedByComponent, (component, componentKey) => {
const values: number[] = [];
_.forEach(cycles, cycle => {
const cycleData = _.find(component, {
[AllocationCyclesFieldsMapping.cycle]: cycle,
[AllocationCumulativeByCyclesFieldsMapping.cycle]: cycle,
});
values.push(
_.get(cycleData, AllocationCyclesFieldsMapping.value, 0),
_.get(
cycleData,
AllocationCumulativeByCyclesFieldsMapping.value,
0,
),
);
});

Expand All @@ -148,7 +153,7 @@ export class AllocationsController {
values,
itemStyle: {
color: _.get(
AllocationCyclesFieldsMapping.colors,
AllocationCumulativeByCyclesFieldsMapping.colors,
data.length + 1,
),
},
Expand All @@ -161,7 +166,7 @@ export class AllocationsController {
_.sumBy(data, `values[${index}]`),
),
itemStyle: {
color: _.get(AllocationCyclesFieldsMapping.colors, 0),
color: _.get(AllocationCumulativeByCyclesFieldsMapping.colors, 0),
},
});

Expand Down Expand Up @@ -436,6 +441,41 @@ export class AllocationsController {
return getAllocationsData(url);
}

@get('/allocations/cycles')
@response(200)
async cycles() {
return axios
.get(
`${urls.FINANCIAL_INDICATORS}${AllocationCyclesFieldsMapping.urlParams}`,
)
.then((resp: AxiosResponse) => {
const rawData = _.get(
resp.data,
AllocationCyclesFieldsMapping.dataPath,
[],
);

const data = _.map(rawData, (item, index) => {
const from = _.get(item, AllocationCyclesFieldsMapping.cycleFrom, '');
const to = _.get(item, AllocationCyclesFieldsMapping.cycleTo, '');

let value = from;

if (from && to) {
value = `${from} - ${to}`;
}

return {
name: `Cycle ${index + 1}`,
value,
};
});

return {data};
})
.catch(handleDataApiError);
}

// v2

@get('/allocations')
Expand Down
35 changes: 25 additions & 10 deletions src/controllers/budgets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,34 @@ export class BudgetsController {
@get('/budgets/cycles')
@response(200)
async cycles() {
const url = `${urls.FINANCIAL_INDICATORS}${BudgetsCyclesMapping.urlParams}`;

return axios
.get(url)
.get(`${urls.FINANCIAL_INDICATORS}${BudgetsCyclesMapping.urlParams}`)
.then((resp: AxiosResponse) => {
return _.get(resp.data, BudgetsCyclesMapping.dataPath, []).map(
(item: any) =>
`${_.get(item, BudgetsCyclesMapping.from, '')}-${_.get(
item,
BudgetsCyclesMapping.to,
'',
)}`,
const rawData = _.get(resp.data, BudgetsCyclesMapping.dataPath, []);

const data = _.map(
_.filter(
rawData,
item => _.get(item, BudgetsCyclesMapping.cycleFrom, null) !== null,
),
(item, index) => {
const from = _.get(item, BudgetsCyclesMapping.cycleFrom, '');
const to = _.get(item, BudgetsCyclesMapping.cycleTo, '');

let value = from;

if (from && to) {
value = `${from} - ${to}`;
}

return {
name: `Cycle ${index + 1}`,
value,
};
},
);

return {data};
})
.catch(handleDataApiError);
}
Expand Down
56 changes: 56 additions & 0 deletions src/controllers/disbursements.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import querystring from 'querystring';
import filteringGrants from '../config/filtering/grants.json';
import filtering from '../config/filtering/index.json';
import BarChartFieldsMapping from '../config/mapping/disbursements/barChart.json';
import DisbursementsCyclesMapping from '../config/mapping/disbursements/cycles.json';
import GeomapFieldsMapping from '../config/mapping/disbursements/geomap.json';
import GrantCommittedTimeCycleFieldsMapping from '../config/mapping/disbursements/grantCommittedTimeCycle.json';
import GrantDetailTimeCycleFieldsMapping from '../config/mapping/disbursements/grantDetailTimeCycle.json';
Expand Down Expand Up @@ -270,7 +271,19 @@ export class DisbursementsController {
this.req.query,
LineChartFieldsMapping.urlParams,
);
const filterString2 = filterFinancialIndicators(
this.req.query,
LineChartFieldsMapping.activitiesCountUrlParams,
);
const url = `${urls.FINANCIAL_INDICATORS}/${filterString}`;
const url2 = `${urls.FINANCIAL_INDICATORS}/${filterString2}`;

const activitiesCount = await axios
.get(url2)
.then((resp: AxiosResponse) =>
_.get(resp.data, LineChartFieldsMapping.count, 0),
)
.catch(() => 0);

return axios
.get(url)
Expand All @@ -296,6 +309,7 @@ export class DisbursementsController {
},
})),
xAxisKeys: Object.keys(years),
activitiesCount,
};
})
.catch(handleDataApiError);
Expand Down Expand Up @@ -353,6 +367,48 @@ export class DisbursementsController {
.catch(handleDataApiError);
}

@get('/disbursements/cycles')
@response(200)
async cycles() {
return axios
.get(
`${urls.FINANCIAL_INDICATORS}${DisbursementsCyclesMapping.urlParams}`,
)
.then((resp: AxiosResponse) => {
const rawData = _.get(
resp.data,
DisbursementsCyclesMapping.dataPath,
[],
);

const data = _.map(
_.filter(
rawData,
item =>
_.get(item, DisbursementsCyclesMapping.cycleFrom, null) !== null,
),
(item, index) => {
const from = _.get(item, DisbursementsCyclesMapping.cycleFrom, '');
const to = _.get(item, DisbursementsCyclesMapping.cycleTo, '');

let value = from;

if (from && to) {
value = `${from} - ${to}`;
}

return {
name: `Cycle ${index + 1}`,
value,
};
},
);

return {data};
})
.catch(handleDataApiError);
}

// v2

// Time/Cycle
Expand Down
Loading

0 comments on commit 183914d

Please sign in to comment.