Skip to content

Commit

Permalink
improvement(case-aggregations): add occupations facet
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMaruchu committed Jun 5, 2020
1 parent b47cd22 commit c7b8452
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/aggregations/case.aggregations.js
Expand Up @@ -153,7 +153,6 @@ export const CASE_FACET_GENDER = {
* @since 0.8.0
* @version 0.1.0
*/

export const CASE_FACET_AGE_GROUPS = {
ageGroups: [
{
Expand Down Expand Up @@ -183,6 +182,31 @@ export const CASE_FACET_AGE_GROUPS = {
],
};

/**
* @constant
* @name CASE_FACET_OCCUPATIONS
* @description Cases victim occupations facet
*
* @author Benson Maruchu<benmaruchu@gmail.com>
* @license MIT
* @since 0.8.0
* @version 0.1.0
*/
export const CASE_FACET_OCCUPATIONS = {
occupations: [
{
$group: {
_id: '$victim.occupation._id',
total: { $sum: 1 },
namespace: { $first: '$victim.occupation.namespace' },
name: { $first: '$victim.occupation.strings.name' },
weight: { $first: '$victim.occupation.numbers.weight' },
color: { $first: '$victim.occupation.strings.color' },
},
},
],
};

// start: aggregations
// order: base to specific

Expand Down Expand Up @@ -261,6 +285,7 @@ export const getEventCaseAnalysis = (criteria, done) => {
const facets = {
...CASE_FACET_GENDER,
...CASE_FACET_AGE_GROUPS,
...CASE_FACET_OCCUPATIONS,
};

base.facet(facets);
Expand All @@ -269,7 +294,7 @@ export const getEventCaseAnalysis = (criteria, done) => {

// Normalize data
const normalize = (result, next) => {
const { gender, ageGroups } = safeMergeObjects(...result);
const { gender, ageGroups, occupations } = safeMergeObjects(...result);

// add upper boundary for returned age groups
const normalizedResultsAgeGroups = map(ageGroups, (group) => ({
Expand All @@ -290,6 +315,7 @@ export const getEventCaseAnalysis = (criteria, done) => {
overall: {
gender,
ageGroups: normalizedAgeGroups,
occupations,
},
});

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/cases.js
Expand Up @@ -88,6 +88,13 @@ const cases = [
},
model: 'Predefine',
},
'victim.nationality': {
match: {
namespace: 'PartyNationality',
'strings.name.en': 'Tanzanian',
},
model: 'Predefine',
},
'victim.area': {
match: { namespace: 'AdministrativeArea', 'strings.name.en': 'Temeke' },
model: 'Predefine',
Expand Down
1 change: 1 addition & 0 deletions test/integration/case.aggregations.spec.js
Expand Up @@ -21,6 +21,7 @@ describe('EventCase Aggregations', () => {
expect(report.data).to.exist.and.be.an('object');
expect(report.data.overall.gender).to.exist.and.be.an('array');
expect(report.data.overall.ageGroups).to.exist.and.be.an('array');
expect(report.data.overall.occupations).to.exist.and.be.an('array');
expect(report.data.overall.ageGroups).to.have.lengthOf(21);
done(error, report);
});
Expand Down

0 comments on commit c7b8452

Please sign in to comment.