-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Matched captures statistic #73
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f139545
feat: migration to add tree_id column
pranavkparti 31f4c77
feat: repository methods for matched_captures
pranavkparti dbad755
feat: matched_captures reporting card
pranavkparti 4c2e0e0
feat: matched_captures schema validation update
pranavkparti 97e3d58
feat: update capture_denormalized.spec.js
pranavkparti 63d70c6
fix: eslint errors
pranavkparti f184299
fix: removed table name prefix
pranavkparti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Promise; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function(options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
Promise = options.Promise; | ||
}; | ||
|
||
exports.up = function(db) { | ||
var filePath = path.join(__dirname, 'sqls', '20230724231226-add-tree-id-column-up.sql'); | ||
return new Promise( function( resolve, reject ) { | ||
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ | ||
if (err) return reject(err); | ||
console.log('received data: ' + data); | ||
|
||
resolve(data); | ||
}); | ||
}) | ||
.then(function(data) { | ||
return db.runSql(data); | ||
}); | ||
}; | ||
|
||
exports.down = function(db) { | ||
var filePath = path.join(__dirname, 'sqls', '20230724231226-add-tree-id-column-down.sql'); | ||
return new Promise( function( resolve, reject ) { | ||
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ | ||
if (err) return reject(err); | ||
console.log('received data: ' + data); | ||
|
||
resolve(data); | ||
}); | ||
}) | ||
.then(function(data) { | ||
return db.runSql(data); | ||
}); | ||
}; | ||
|
||
exports._meta = { | ||
"version": 1 | ||
}; |
1 change: 1 addition & 0 deletions
1
database/migrations/sqls/20230724231226-add-tree-id-column-down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE capture_denormalized DROP COLUMN tree_id; |
1 change: 1 addition & 0 deletions
1
database/migrations/sqls/20230724231226-add-tree-id-column-up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE capture_denormalized ADD COLUMN tree_id uuid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ const BaseRepository = require('./BaseRepository'); | |
|
||
class CaptureRepository extends BaseRepository { | ||
constructor(session) { | ||
super('capture_denormalized', session); | ||
this._tableName = 'capture_denormalized'; | ||
super('reporting.capture_denormalized', session); | ||
this._tableName = 'reporting.capture_denormalized'; | ||
this._session = session; | ||
} | ||
|
||
|
@@ -55,6 +55,7 @@ class CaptureRepository extends BaseRepository { | |
const whereBuilder = function (object, builder) { | ||
const result = builder; | ||
const filterObject = { ...object }; | ||
console.log('FILTEROBJ', filterObject) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove the console.log |
||
delete filterObject.card_title; | ||
if (filterObject.capture_created_start_date) { | ||
result.where( | ||
|
@@ -131,7 +132,7 @@ class CaptureRepository extends BaseRepository { | |
'planter_last_name', | ||
'planter_identifier', | ||
) | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.where((builder) => whereBuilder(filter, builder)) | ||
.as('planters'); | ||
}); | ||
|
@@ -147,7 +148,7 @@ class CaptureRepository extends BaseRepository { | |
'planting_organization_name', | ||
'planting_organization_uuid', | ||
) | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.where((builder) => whereBuilder(filter, builder)) | ||
.groupBy( | ||
'planting_organization_uuid', | ||
|
@@ -177,7 +178,7 @@ class CaptureRepository extends BaseRepository { | |
.avg('totalPlanters') | ||
.from(function () { | ||
this.count('* as totalPlanters') | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.where((builder) => whereBuilder(filter, builder)) | ||
.groupBy( | ||
'planter_first_name', | ||
|
@@ -236,7 +237,7 @@ class CaptureRepository extends BaseRepository { | |
`count(*) as count, planting_organization_name, planting_organization_uuid`, | ||
), | ||
) | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.where((builder) => whereBuilder(filter, builder)) | ||
.groupBy( | ||
'planting_organization_uuid', | ||
|
@@ -265,7 +266,7 @@ class CaptureRepository extends BaseRepository { | |
`count(*) as count, planting_organization_name, planting_organization_uuid`, | ||
), | ||
) | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.where((builder) => whereBuilder(filter, builder)) | ||
.groupBy( | ||
'planting_organization_uuid', | ||
|
@@ -287,7 +288,7 @@ class CaptureRepository extends BaseRepository { | |
.avg('totalCatchment') | ||
.from(function () { | ||
this.count('* as totalCatchment') | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.where((builder) => cachmentWhereBuilder(filter, builder)) | ||
.groupBy('catchment') | ||
.as('catchments'); | ||
|
@@ -312,7 +313,7 @@ class CaptureRepository extends BaseRepository { | |
'gender', | ||
) | ||
.where((builder) => whereBuilder(filter, builder)) | ||
.from('capture_denormalized') | ||
.from('reporting.capture_denormalized') | ||
.as('planters'); | ||
}) | ||
.groupBy('gender') | ||
|
@@ -363,6 +364,24 @@ 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)); | ||
|
||
// 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); | ||
|
||
|
||
if (filter?.card_title) { | ||
const { card_title } = filter; | ||
|
||
|
@@ -385,6 +404,7 @@ class CaptureRepository extends BaseRepository { | |
await topUnverifiedCapturesQuery.cache(); | ||
return { topUnverifiedCaptures }; | ||
} | ||
|
||
case 'top_planters': { | ||
const topPlanters = await topPlantersQuery.cache(); | ||
return { topPlanters }; | ||
|
@@ -402,6 +422,10 @@ class CaptureRepository extends BaseRepository { | |
const approvalRates = await approvalRateQuery.cache(); | ||
return { approvalRates }; | ||
} | ||
case 'matched_captures': { | ||
const topMatchedCaptures = await topMatchedCapturesQuery.cache(); | ||
return { topMatchedCaptures }; | ||
} | ||
|
||
default: | ||
break; | ||
|
@@ -429,6 +453,8 @@ class CaptureRepository extends BaseRepository { | |
const topCatchment = await topCatchmentQuery.cache(); | ||
const genderCount = await genderCountQuery.cache(); | ||
const approvalRates = await approvalRateQuery.cache(); | ||
const totalMatchedCaptures = await totalMatchedCapturesQuery.cache(); | ||
const topMatchedCaptures = await topMatchedCapturesQuery.cache(); | ||
|
||
return { | ||
totalGrowers: +totalGrowers[0].totalPlanters, | ||
|
@@ -449,6 +475,8 @@ class CaptureRepository extends BaseRepository { | |
topCatchment, | ||
genderCount, | ||
approvalRates, | ||
totalMatchedCaptures: +totalMatchedCaptures[0].count, | ||
topMatchedCaptures, | ||
}; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should remove the 'reporting.'. In the cloud, the DATABASE_URL is already configured only to access the reporting schema, so this is unnecessary. This might also be the reason for the failed tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, my bad. I thought I committed the code without the
reporting.
prefix.