Skip to content

Commit

Permalink
Add mini rule engine for displaying datasets per period
Browse files Browse the repository at this point in the history
  • Loading branch information
Markionium committed Mar 11, 2016
1 parent 07e84b4 commit 5ec5e46
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/main/datasets/datasetgroup-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function dataSetGroupService(d2Api, $q, periodService, Restangular, errorHandler
var service = this;
var dataSetGroups = {};
var dataSetGroupNames = [];
var datasetDisplayRules = [];

this.getGroups = function () {
return dataSetGroups;
Expand Down Expand Up @@ -94,6 +95,10 @@ function dataSetGroupService(d2Api, $q, periodService, Restangular, errorHandler
}
};

this.getDataSetDisplayRules = function () {
return datasetDisplayRules;
};

// Configure the api endpoints we use
d2Api.addEndPoint('systemSettings');
d2Api.addEndPoint('dataSets');
Expand All @@ -102,8 +107,11 @@ function dataSetGroupService(d2Api, $q, periodService, Restangular, errorHandler
d2Api.addEndPoint('categoryCombos');

// Load the dataSetGroups that are available from system settings
d2Api.systemSettings.get('keyApprovalDataSetGroups')
.then(function (resultDataSetGroups) {
$q.all([d2Api.systemSettings.get('keyApprovalsDataSetDisplayRules'), d2Api.systemSettings.get('keyApprovalDataSetGroups')])
.then(function (data) {
datasetDisplayRules = data[0];
var resultDataSetGroups = data[1];

if (!Array.isArray(resultDataSetGroups)) {
return $q.reject('Dataset groups not defined in systemsettings (key: keyApprovalDataSetGroups), see the deployment manual on how to configure the app.');
}
Expand Down
69 changes: 68 additions & 1 deletion src/main/datasets/datasetview-directive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global jQuery */
function datasetViewDirective(AppManifest, $translate) {
function datasetViewDirective(AppManifest, $translate, dataSetGroupService, $log) {
var dataSetReportWrapSelector = '.dataset-report-wrap';

function loadDataSetReport(details, ds, element, scope) {
Expand Down Expand Up @@ -151,6 +151,73 @@ function datasetViewDirective(AppManifest, $translate) {
return result;
});

function appliesToCurrentWorkflow(dataSets) {
return function (rulesDefinition) {
return dataSets.every(function (dataSet) {
return (new RegExp(rulesDefinition.workflow)).test(dataSet.name);
});
};
}

function hasValidRulesDefinition(rulesDefinition) {
if (rulesDefinition &&
rulesDefinition.workflow &&
rulesDefinition.matchPeriodOn.test &&
rulesDefinition.matchPeriodOn.comparator &&
rulesDefinition.matchPeriodOn.value
) {
return true;
}
return false;
}

function buildRulesFromConfig(currentPeriod) {
return dataSetGroupService
.getDataSetDisplayRules()
.filter(hasValidRulesDefinition)
.filter(appliesToCurrentWorkflow(scope.details.dataSetsFilteredByMechanisms))
// Check if rule applies to the current periodType
.filter(function (rulesDefinition) {
var periodTest = new RegExp(rulesDefinition.matchPeriodOn.test);

return periodTest.test(currentPeriod);
})
// Check if the rule applies to the current period
.filter(function (rulesDefinition) {
var fullYear = parseInt(currentPeriod.substring(0, 4), 10);

switch (rulesDefinition.matchPeriodOn.comparator) {
case "gte":
return (fullYear >= rulesDefinition.matchPeriodOn.value)
case "lt":
return (fullYear < rulesDefinition.matchPeriodOn.value)
}

return false;
})
.map(function (ruleDefinition) {
return function (dataSet) {
// We only want dataSets that are in the list of dataSets from the ruleDefinition
return ruleDefinition.dataSets.indexOf(dataSet.id) >= 0;
};
})
}

function applyDataSetDisplayRules(dataSets) {
var rules = buildRulesFromConfig(details.period);

// When rules are available run them
if (rules.length) {
return rules.reduce(function (dataSets, ruleFn) {
return dataSets.filter(ruleFn);
}, dataSets);
}

return dataSets;
}

scope.details.dataSetsFilteredByMechanisms = applyDataSetDisplayRules(scope.details.dataSetsFilteredByMechanisms);

//Move this out
jQuery(dataSetReportWrapSelector).html('');

Expand Down
2 changes: 1 addition & 1 deletion src/main/manifest.webapp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.4.8","name":"Data Approval","description":"Approvals app for PEPFAR","icons":{"48":"img/icons/dataapproval.png"},"developer":{"url":"http://www.dhis2.org","name":"Mark Polak","company":"DHIS2 Core Team","email":"[email protected]"},"launch_path":"index.html?v=0.4.8","default_locale":"en","activities":{"dhis":{"href":"*"}}}
{"version":"0.4.9","name":"Data Approval","description":"Approvals app for PEPFAR","icons":{"48":"img/icons/dataapproval.png"},"developer":{"url":"http://www.dhis2.org","name":"Mark Polak","company":"DHIS2 Core Team","email":"[email protected]"},"launch_path":"index.html?v=0.4.9","default_locale":"en","activities":{"dhis":{"href":"*"}}}

0 comments on commit 5ec5e46

Please sign in to comment.