Skip to content

Commit

Permalink
feat(aggregations): improve party base metrics fields and projections
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Apr 29, 2020
1 parent c22e8aa commit 82a9d0a
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 371 deletions.
578 changes: 389 additions & 189 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"lint": "eslint --fix --ext .js src/ test/ rollup.config.js",
"pretest": "npm run lint",
"posttest": "rimraf test/fixtures/*.csv",
"test": "NODE_ENV=test nyc --reporter=html --reporter=text mocha --exit --timeout=20000 --require @babel/register test/**/*.spec.js",
"test:unit": "NODE_ENV=test npm run pretest && NODE_ENV=test mocha --exit --timeout=20000 --require @babel/register test/unit/**/*.spec.js",
"test:integration": "NODE_ENV=test npm run pretest && NODE_ENV=test mocha --exit --timeout=20000 --require @babel/register test/integration/**/*.spec.js",
"test": "NODE_ENV=test LOGGER_LOG_ENABLED=false DEFAULT_LOCALE=en LOCALES=en,sw nyc --reporter=html --reporter=text mocha --exit --timeout=80000 --require @babel/register test/**/*.spec.js",
"test:unit": "NODE_ENV=test npm run pretest && NODE_ENV=test LOGGER_LOG_ENABLED=false DEFAULT_LOCALE=en LOCALES=en,sw mocha --exit --timeout=80000 --require @babel/register test/unit/**/*.spec.js",
"test:integration": "NODE_ENV=test npm run pretest && NODE_ENV=test LOGGER_LOG_ENABLED=false DEFAULT_LOCALE=en LOCALES=en,sw mocha --exit --timeout=80000 --require @babel/register test/integration/**/*.spec.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"docs": "doxdox 'lib/**/*.js' -p package.json -l markdown -o DOCUMENTATION.md",
"cmt": "git add -A && git-cz",
Expand Down Expand Up @@ -102,7 +102,7 @@
"generate-changelog": "^1.8.0",
"handlebars": "^4.7.6",
"husky": "^4.2.5",
"lint-staged": "^10.1.7",
"lint-staged": "^10.2.0",
"lodash.template": "^4.5.0",
"mocha": "^7.1.2",
"mongoose": ">=5.9.10",
Expand All @@ -117,20 +117,20 @@
"mongoose": ">=5.9.10"
},
"dependencies": {
"@codetanzania/emis-stakeholder": ">=2.5.0",
"@codetanzania/ewea-common": ">=0.8.1",
"@codetanzania/emis-stakeholder": ">=2.6.0",
"@codetanzania/ewea-common": ">=0.9.0",
"@codetanzania/ewea-event": ">=0.9.1",
"@codetanzania/ewea-internals": ">=0.11.0",
"@lykmapipo/common": ">=0.33.0",
"@lykmapipo/env": ">=0.17.3",
"@lykmapipo/express-common": ">=0.18.2",
"@lykmapipo/express-rest-actions": ">=0.8.14",
"@lykmapipo/file": ">=0.1.23",
"@lykmapipo/mongoose-common": ">=0.32.1",
"@lykmapipo/mongoose-common": ">=0.33.0",
"@lykmapipo/mongoose-exportable": ">=0.3.27",
"@lykmapipo/mongoose-sequenceable": ">=0.2.13",
"@lykmapipo/postman": ">=0.18.7",
"@lykmapipo/predefine": ">=1.14.1",
"@lykmapipo/predefine": ">=1.15.0",
"async": ">=3.2.0",
"lodash": ">=4.17.15",
"moment": ">=2.24.0",
Expand Down
10 changes: 10 additions & 0 deletions src/aggregations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Convention

## Aggregation
- Each aggregation should be named after a `model` it derived from
- Each aggregation should expose it base
- Each base aggregation should support initial criteria

## Metric Fields
- All extra metric fields should be added into `metric` object to avoid collision with other aggregation fields
- All extra time fields should be suffixed with `Time` e.g `activeTime`
164 changes: 151 additions & 13 deletions src/aggregations/party.aggregations.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,164 @@
import { Party } from '@codetanzania/emis-stakeholder';

export const PARTY_FLAG_METRIC_FIELDS = {
active: {
$cond: { if: { $not: '$deletedAt' }, then: 1, else: 0 },
// start: constants
// order: base to specific

export const PARTY_TYPE_FOCAL = 'Focal';

// start: extra metric fields
// order: base to specific

/**
* @constant
* @name PARTY_BASE_METRIC_FIELDS
* @description Adds new metric fields to the next stage in the pipeline.
* @type {object}
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.2.0
* @version 0.1.0
*/
export const PARTY_BASE_METRIC_FIELDS = {
metrics: {
focal: {
$cond: { if: { $eq: ['$type', PARTY_TYPE_FOCAL] }, then: 1, else: 0 },
},
agency: {
$cond: { if: { $ne: ['$type', PARTY_TYPE_FOCAL] }, then: 1, else: 0 },
},
active: {
$cond: { if: { $not: '$deletedAt' }, then: 1, else: 0 },
},
},
};

export const PARTY_TIME_METRIC_FIELDS = {
activeTime: { $subtract: [new Date(), '$createdAt'] },
// start: projections
// order: base to specific

/**
* @constant
* @name PARTY_BASE_LEVEL_PROJECTION
* @description Party level fields passed to the next stage in the pipeline.
* @type {object}
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.2.0
* @version 0.1.0
*/
export const PARTY_BASE_LEVEL_PROJECTION = {
_id: 1,
namespace: 1,
name: '$level.strings.name',
abbreviation: '$level.strings.abbreviation',
color: '$level.strings.color',
weight: '$level.numbers.weight',
};

/**
* @constant
* @name PARTY_BASE_AREA_PROJECTION
* @description Party area fields passed to the next stage in the pipeline.
* @type {object}
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.2.0
* @version 0.1.0
*/
export const PARTY_BASE_AREA_PROJECTION = {
_id: 1,
namespace: 1,
name: '$area.strings.name',
abbreviation: '$area.strings.abbreviation',
color: '$area.strings.color',
weight: '$area.numbers.weight', // TODO: ensure from level
};

/**
* @constant
* @name PARTY_BASE_GROUP_PROJECTION
* @description Party group fields passed to the next stage in the pipeline.
* @type {object}
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.2.0
* @version 0.1.0
*/
export const PARTY_BASE_GROUP_PROJECTION = {
_id: 1,
namespace: 1,
name: '$group.strings.name',
abbreviation: '$group.strings.abbreviation',
color: '$group.strings.color',
weight: '$group.numbers.weight',
};

/**
* @constant
* @name PARTY_BASE_ROLE_PROJECTION
* @description Party role fields passed to the next stage in the pipeline.
* @type {object}
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.2.0
* @version 0.1.0
*/
export const PARTY_BASE_ROLE_PROJECTION = {
_id: 1,
namespace: 1,
name: '$role.strings.name',
abbreviation: '$role.strings.abbreviation',
color: '$role.strings.color',
weight: '$role.numbers.weight',
};

/**
* @constant
* @name PARTY_BASE_PROJECTION
* @description Party fields passed to the next stage in the pipeline.
* @type {object}
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.2.0
* @version 0.1.0
*/
export const PARTY_BASE_PROJECTION = {
_id: 1,
type: 1,
createdAt: 1,
updatedAt: 1,
metrics: 1,
level: PARTY_BASE_LEVEL_PROJECTION,
area: PARTY_BASE_AREA_PROJECTION,
group: PARTY_BASE_GROUP_PROJECTION,
role: PARTY_BASE_ROLE_PROJECTION,
};

// start: facets
// order: base, overall to specific
export const PARTY_FACET_OVERVIEW = {};

// start: functions
// order: base to specific

/**
* @function getPartyBaseAggregation
* @name getPartyBaseAggregation
* @description Create base aggregation for `Party` with all fields
* looked up and un-winded for further aggregation operations.
* @param {object} [criteria={}] conditions which will be applied in
* first aggregation stage.
* @returns {object} valid base aggregation for party.
* @description Create base `Party` aggregations with all fields looked up and
* un-winded for further stages in the pipeline.
* @param {object} [criteria={}] conditions which will be applied in first
* aggregation stage.
* @returns {object|Error} valid base aggregation for party or error
*
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.1.0
* @version 0.1.0
* @version 0.2.0
* @static
* @public
* @example
Expand All @@ -36,8 +172,10 @@ export const getPartyBaseAggregation = (criteria = {}) => {
const base = Party.lookup(criteria);

// add extra metric fields
base.addFields(PARTY_FLAG_METRIC_FIELDS);
base.addFields(PARTY_TIME_METRIC_FIELDS);
base.addFields(PARTY_BASE_METRIC_FIELDS);

// add base projection
base.project(PARTY_BASE_PROJECTION);

// return party base aggregation
return base;
Expand Down
16 changes: 0 additions & 16 deletions test/fixtures/parties/agencies.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
const agencies = [
{
type: 'Agency',
name: 'People’s Defence Force',
abbreviation: 'PDF',
locale: 'en',
email: 'tpdf@example.com',
mobile: '255754000001',
},
{
type: 'Agency',
name: 'National Roads Agency',
abbreviation: 'NAROADS',
locale: 'en',
email: 'tanroads@example.com',
mobile: '255754000002',
},
{
type: 'Agency',
name: 'Meteorological Agency',
Expand Down

0 comments on commit 82a9d0a

Please sign in to comment.