Skip to content

Commit

Permalink
feat: add surviving freetown cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Aug 22, 2023
1 parent 0e9590a commit 6d88f04
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 20 deletions.
7 changes: 5 additions & 2 deletions __tests__/integration/capture_denormalized.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ describe('capture_denormalized', () => {
'total',
'unverified_captures',
]);
expect(res.body.matched_captures).to.have.keys(['total', 'matched_captures']);
expect(res.body.matched_captures).to.have.keys([
'total',
'matched_captures',
]);
expect(res.body.top_planters).to.have.keys([
'average',
'top_planters',
Expand Down Expand Up @@ -429,7 +432,7 @@ describe('capture_denormalized', () => {
.end(function (err, res) {
if (err) return done(err);
expect(res.body.message).to.eql(
'"card_title" must be one of [planters, species, captures, unverified_captures, matched_captures, top_planters, trees_per_planters, catchments, gender_details, approval_rates]',
'"card_title" must be one of [planters, species, captures, unverified_captures, matched_captures, top_planters, trees_per_planters, catchments, gender_details, approval_rates,surviving_trees,surviving_catchments,surviving_species]',
);
return done();
});
Expand Down
3 changes: 3 additions & 0 deletions server/handlers/captureHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const captureStatisticsGetCardQuerySchema = Joi.object({
'catchments',
'gender_details',
'approval_rates',
'surviving_trees',
'surviving_catchments',
'surviving_species',
)
.required(),
}).unknown(false);
Expand Down
58 changes: 51 additions & 7 deletions server/models/Capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class Capture {
approvalRates,
totalMatchedCaptures = undefined,
topMatchedCaptures = [],
totalSurvivingTrees,
survivingTrees = [],
averageSurvivingCatchments,
survivingCatchments = [],
totalSurvivingSpecies,
survivingSpecies = [],
}) {
const planters = {
total: totalGrowers,
Expand Down Expand Up @@ -174,13 +180,36 @@ class Capture {

const matched_captures = {
total: totalMatchedCaptures,
matched_captures: topMatchedCaptures.map(({ planting_organization_name, count }) => {
return {
name: planting_organization_name,
number: count,
}
})
}
matched_captures: topMatchedCaptures.map(
({ planting_organization_name, count }) => {
return {
name: planting_organization_name,
number: count,
};
},
),
};

const surviving_catchments = {
average: Math.round(averageSurvivingCatchments),
catchments: survivingCatchments.map(({ catchment, count }) => {
return { name: catchment, number: count };
}),
};

const surviving_trees = {
total: totalSurvivingTrees,
trees: survivingTrees.map(({ planting_organization_name, count }) => {
return { name: planting_organization_name, number: count };
}),
};

const surviving_species = {
total: totalSurvivingSpecies,
species: survivingSpecies.map(({ species: s, count }) => {
return { name: s, number: count };
}),
};

return {
planters,
Expand All @@ -192,6 +221,9 @@ class Capture {
trees_per_planters,
last_updated_at,
catchments,
surviving_catchments,
surviving_trees,
surviving_species,
gender_details: { total: totalGrowers, gender_details },
approval_rates: { approval_rates: approvalRates },
};
Expand All @@ -218,6 +250,12 @@ class Capture {
approvalRates,
totalMatchedCaptures,
topMatchedCaptures,
totalSurvivingTrees,
survivingTrees,
averageSurvivingCatchments,
survivingCatchments,
totalSurvivingSpecies,
survivingSpecies,
} = await this._captureRepository.getStatistics(filter);

return this.constructor.generateFormattedResponse({
Expand All @@ -240,6 +278,12 @@ class Capture {
approvalRates,
totalMatchedCaptures,
topMatchedCaptures,
totalSurvivingTrees,
survivingTrees,
averageSurvivingCatchments,
survivingCatchments,
totalSurvivingSpecies,
survivingSpecies,
});
}

Expand Down
112 changes: 101 additions & 11 deletions server/repositories/CaptureRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,23 +363,87 @@ class CaptureRepository extends BaseRepository {
.limit(options.limit)
.offset(options.offset);


// total number of matched captures
const totalMatchedCapturesQuery = knex(this._tableName)
.count()
.whereNotNull('tree_id')
.where((builder) => whereBuilder({ ...filter, approved: true }, builder));
.count()
.whereNotNull('tree_id')
.where((builder) => whereBuilder({ ...filter, approved: true }, builder));

// top matched captures (by organization)
const topMatchedCapturesQuery = knex(this._tableName)
.select(knex.raw('planting_organization_name, count(*) as count'))
.whereNotNull('tree_id')
.where((builder) => whereBuilder({ ...filter, approved: true }, builder))
.groupBy('planting_organization_uuid', 'planting_organization_name')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);
.select(knex.raw('planting_organization_name, count(*) as count'))
.whereNotNull('tree_id')
.where((builder) => whereBuilder({ ...filter, approved: true }, builder))
.groupBy('planting_organization_uuid', 'planting_organization_name')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);

// FREETOWN SURVIVING TREES' CARDS
const freetownSurvivingTreesFilter = {
tree_organization_uuid: '058148b8-9606-4c61-9dea-7c7174faf234',
};
const totalSurvivingTreesQuery = knex(this._tableName)
.count()
.where((builder) =>
whereBuilder({ ...filter, ...freetownSurvivingTreesFilter }, builder),
);

const survivingTreesQuery = knex(this._tableName)
.select(knex.raw('planting_organization_name, count(*) as count'))
.where((builder) =>
whereBuilder({ ...filter, ...freetownSurvivingTreesFilter }, builder),
)
.groupBy('planting_organization_uuid', 'planting_organization_name')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);

const averageSurvivingCatchmentsQuery = knex(this._tableName)
.avg('totalCatchment')
.from(function () {
this.count('* as totalCatchment')
.from('capture_denormalized')
.where((builder) =>
cachmentWhereBuilder(
{ ...filter, ...freetownSurvivingTreesFilter },
builder,
),
)
.groupBy('catchment')
.as('catchments');
});

const survivingCatchmentsQuery = knex(this._tableName)
.select(knex.raw('catchment, count(*) as count'))
.where((builder) =>
cachmentWhereBuilder(
{ ...filter, ...freetownSurvivingTreesFilter },
builder,
),
)
.whereNotNull('catchment')
.groupBy('catchment')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);

const totalSurvivingSpeciesQuery = knex(this._tableName)
.where((builder) =>
whereBuilder({ ...filter, ...freetownSurvivingTreesFilter }, builder),
)
.countDistinct('species as totalSpecies');

const survivingSpeciesQuery = knex(this._tableName)
.select(knex.raw('species, count(*) as count'))
.where((builder) =>
whereBuilder({ ...filter, ...freetownSurvivingTreesFilter }, builder),
)
.whereNotNull('species')
.groupBy('species')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);

if (filter?.card_title) {
const { card_title } = filter;
Expand Down Expand Up @@ -425,6 +489,19 @@ class CaptureRepository extends BaseRepository {
const topMatchedCaptures = await topMatchedCapturesQuery.cache();
return { topMatchedCaptures };
}
// FREETOWN SURVIVING TREES
case 'surviving_trees': {
const survivingTrees = await survivingTreesQuery.cache();
return { survivingTrees };
}
case 'surviving_catchments': {
const survivingCatchments = await survivingCatchmentsQuery.cache();
return { survivingCatchments };
}
case 'surviving_species': {
const survivingSpecies = await survivingSpeciesQuery.cache();
return { survivingSpecies };
}

default:
break;
Expand Down Expand Up @@ -454,6 +531,13 @@ class CaptureRepository extends BaseRepository {
const approvalRates = await approvalRateQuery.cache();
const totalMatchedCaptures = await totalMatchedCapturesQuery.cache();
const topMatchedCaptures = await topMatchedCapturesQuery.cache();
const totalSurvivingTrees = await totalSurvivingTreesQuery.cache();
const survivingTrees = await survivingTreesQuery.cache();
const averageSurvivingCatchments =
await averageSurvivingCatchmentsQuery.cache();
const survivingCatchments = await survivingCatchmentsQuery.cache();
const totalSurvivingSpecies = await totalSurvivingSpeciesQuery.cache();
const survivingSpecies = await survivingSpeciesQuery.cache();

return {
totalGrowers: +totalGrowers[0].totalPlanters,
Expand All @@ -476,6 +560,12 @@ class CaptureRepository extends BaseRepository {
approvalRates,
totalMatchedCaptures: +totalMatchedCaptures[0].count,
topMatchedCaptures,
totalSurvivingTrees: +totalSurvivingTrees[0].count,
survivingTrees,
averageSurvivingCatchments: +averageSurvivingCatchments[0].count,
survivingCatchments,
totalSurvivingSpecies: +totalSurvivingSpecies[0].count,
survivingSpecies,
};
}
}
Expand Down

0 comments on commit 6d88f04

Please sign in to comment.