Skip to content

Commit

Permalink
fix: cache added to capture card queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Dec 24, 2021
1 parent 96d4d73 commit 6266622
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
37 changes: 35 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"joi": "^17.4.2",
"knex": "^0.95.13",
"loglevel": "^1.6.8",
"node-cache": "^5.1.2",
"pg": "^8.7.1",
"uuid": "^8.2.0"
},
Expand Down
21 changes: 19 additions & 2 deletions server/database/knex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const expect = require('expect-runtime');
const log = require('loglevel');
const NodeCache = require('node-cache');
const dbCache = new NodeCache({ checkperiod: 86400 });

const connection = require('../../config/config').connectionString;

Expand All @@ -19,6 +21,21 @@ if (process.env.DATABASE_SCHEMA) {
}
log.debug(knexConfig.searchPath);

const knex = require('knex')(knexConfig);
const knex = require('knex');

module.exports = knex;
knex.QueryBuilder.extend('cache', async function () {
try {
const cacheKey = this.toString();
const cacheValue = dbCache.get(cacheKey);
if (cacheValue) {
return cacheValue;
}
const data = await this;
dbCache.set(cacheKey, data, 86400);
return data;
} catch (e) {
throw new Error(e);
}
});

module.exports = knex(knexConfig);
40 changes: 25 additions & 15 deletions server/repositories/CaptureRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class CaptureRepository extends BaseRepository {
.from('capture_denormalized')
.where((builder) => whereBuilder(filter, builder))
.as('planters');
});
})
.cache();

// total number of growers per organization
const topGrowersPerOrganizatinoQuery = knex(this._tableName)
Expand All @@ -113,7 +114,8 @@ class CaptureRepository extends BaseRepository {
.groupBy('planting_organization_name', 'planting_organization_uuid')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);
.offset(options.offset)
.cache();

const topPlantersQuery = knex(this._tableName)
.select(
Expand All @@ -123,7 +125,8 @@ class CaptureRepository extends BaseRepository {
.groupBy('planter_first_name', 'planter_last_name', 'planter_identifier')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);
.offset(options.offset)
.cache();

const averageCapturePerPlanterQuery = knex(this._tableName)
.avg('totalPlanters')
Expand All @@ -137,11 +140,13 @@ class CaptureRepository extends BaseRepository {
'planter_identifier',
)
.as('planters');
});
})
.cache();

const totalSpeciesQuery = knex(this._tableName)
.where((builder) => whereBuilder(filter, builder))
.countDistinct('species as totalSpecies');
.countDistinct('species as totalSpecies')
.cache();

const topSpeciesQuery = knex(this._tableName)
.select(knex.raw('species, count(*) as count'))
Expand All @@ -150,33 +155,36 @@ class CaptureRepository extends BaseRepository {
.groupBy('species')
.orderBy('count', 'desc')
.limit(options.limit)
.offset(options.offset);
.offset(options.offset)
.cache();

const totalApprovedCapturesQuery = knex(this._tableName)
.count()
.where((builder) => whereBuilder({ ...filter, approved: true }, builder));
.where((builder) => whereBuilder({ ...filter, approved: true }, builder))
.cache();

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

const totalUnverifiedCapturesQuery = knex(this._tableName)
.count()
.where((builder) =>
whereBuilder({ ...filter, approved: false }, builder),
);
.where((builder) => whereBuilder({ ...filter, approved: false }, builder))
.cache();

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

const averageCapturesPerPlanterPerOrganizationQuery = knex(this._tableName)
.avg('averageCapturesPerPlanters')
Expand All @@ -201,7 +209,8 @@ class CaptureRepository extends BaseRepository {
})
.groupBy('planting_organization_name', 'planting_organization_uuid')
.as('plantersAverage');
});
})
.cache();

const topAverageCapturesPerPlanterPerOrganizationQuery = knex(
this._tableName,
Expand Down Expand Up @@ -231,9 +240,10 @@ class CaptureRepository extends BaseRepository {
.groupBy('planting_organization_name', 'planting_organization_uuid')
.orderBy('averagecapturesperplanters', 'desc')
.limit(options.limit)
.offset(options.offset);
.offset(options.offset)
.cache();

const lastUpdatedQuery = knex(this._tableName).max('created_at');
const lastUpdatedQuery = knex(this._tableName).max('created_at').cache();

if (filter?.card_title) {
const { card_title } = filter;
Expand Down

0 comments on commit 6266622

Please sign in to comment.