-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FLAG-943] Tropical Tree Cover analysis malfunctioning #4707
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
601c39a
fix(ttc): add tree cover density validation to getWHEREQuery method
willian-viana abc47eb
fix(ttc): refactor getWHEREQuery
willian-viana d471332
chore(analysis-cached): move getWhereQuery and dependencies to a diff…
willian-viana 93ab2b5
chore(jest): add absolute path config
willian-viana 3d29ded
chore(jest): add getWhereQuery test for tree cover density widget ins…
willian-viana fcb2b14
chore(jest): add getWhereQuery general test
willian-viana c860d12
chore(jest): add a non expected example
willian-viana 84b776d
chore(jest): improve readability
willian-viana 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,41 @@ | ||
import { getWHEREQuery } from '../get-where-query'; | ||
|
||
describe('getWHEREQuery', () => { | ||
it('should return an string with each parameter separated by AND', () => { | ||
const params = { | ||
type: 'country', | ||
adm0: 'BRA', | ||
locationType: 'country', | ||
extentYear: 2000, | ||
thresh: 30, | ||
threshold: 30, | ||
forestType: 'plantations', | ||
dataset: 'annual', | ||
}; | ||
const query = getWHEREQuery(params); | ||
const expected = | ||
"WHERE iso = 'BRA' AND umd_tree_cover_density_2000__threshold = 30 AND gfw_planted_forests__type IS NOT NULL "; | ||
|
||
expect(query).toEqual(expected); | ||
}); | ||
|
||
// Tree Cover Density has a default threshold | ||
it('should not return threshold for Tree Cover Density', () => { | ||
const params = { | ||
type: 'country', | ||
adm0: 'PER', | ||
locationType: 'country', | ||
extentYear: 2020, | ||
thresh: '40', | ||
threshold: 40, // passing threshold as parameter from tropical tree cover layer | ||
dataset: 'treeCoverDensity', | ||
}; | ||
|
||
const query = getWHEREQuery(params); | ||
const expected = "WHERE iso = 'PER' "; | ||
const notExpected = 'AND umd_tree_cover_density_2000__threshold = 40 '; | ||
|
||
expect(query).toEqual(expected); | ||
expect(query).not.toEqual(notExpected); | ||
}); | ||
}); |
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,103 @@ | ||
import ALLOWED_PARAMS from 'utils/get-where-query-allowed-params'; | ||
import { translateParameterKey } from 'utils/get-where-query-translation'; | ||
|
||
import forestTypes from 'data/forest-types'; | ||
import landCategories from 'data/land-categories'; | ||
|
||
const isNumber = (value) => !!(typeof value === 'number' || !isNaN(value)); | ||
|
||
// build {where} statement for query | ||
export const getWHEREQuery = (params = {}) => { | ||
const { type, dataset } = params || {}; | ||
|
||
const allFilterOptions = forestTypes.concat(landCategories); | ||
const allowedParams = ALLOWED_PARAMS[params.dataset || 'annual']; | ||
const isTreeCoverDensity = dataset === 'treeCoverDensity'; | ||
const comparisonString = ' = '; | ||
|
||
let paramString = 'WHERE '; | ||
let paramKeys = Object.keys(params).filter((parameterName) => { | ||
return ( | ||
(params[parameterName] || parameterName === 'threshold') && | ||
allowedParams.includes(parameterName) | ||
); | ||
}); | ||
|
||
if (!paramKeys?.length) { | ||
return ''; | ||
} | ||
|
||
/* | ||
* Removing threshold from Tree Cover Density request | ||
* Tree Cover Density has a default threshold >=10 | ||
*/ | ||
if (isTreeCoverDensity && paramKeys.includes('threshold')) { | ||
paramKeys = paramKeys.filter((item) => item !== 'threshold'); | ||
} | ||
|
||
paramKeys.forEach((parameter, index) => { | ||
const isLastParameter = paramKeys.length - 1 === index; | ||
const hasFilterOption = ['forestType', 'landCategory'].includes(parameter); | ||
const value = hasFilterOption ? 1 : params[parameter]; | ||
const filterOption = allFilterOptions.find( | ||
(pname) => pname.value === params[parameter] | ||
); | ||
|
||
const tableKey = | ||
filterOption && | ||
(filterOption.tableKey || filterOption.tableKeys[dataset || 'annual']); | ||
let isNumericValue = isNumber(value); | ||
|
||
const paramKey = translateParameterKey(parameter, params); | ||
|
||
if (parameter === 'adm0' && type === 'wdpa') { | ||
isNumericValue = false; | ||
} | ||
|
||
if (dataset === 'net_change') { | ||
isNumericValue = false; | ||
} | ||
|
||
const hasPrefixIs__ = hasFilterOption && tableKey.includes('is__'); | ||
let WHERE = ''; | ||
|
||
if (hasFilterOption) { | ||
if (hasPrefixIs__) { | ||
WHERE = `${WHERE}${tableKey} = 'true'`; | ||
} | ||
|
||
if (!hasPrefixIs__) { | ||
WHERE = `${WHERE}${tableKey} IS NOT NULL`; | ||
} | ||
|
||
if ( | ||
filterOption && | ||
!hasPrefixIs__ && | ||
filterOption.default && | ||
filterOption.categories | ||
) { | ||
WHERE = `${WHERE} AND ${tableKey} ${filterOption.comparison || '='} ${ | ||
filterOption?.dataType === 'keyword' | ||
? `'${filterOption?.default}'` | ||
: `${filterOption?.default}` | ||
}`; | ||
} | ||
} | ||
|
||
if (!hasFilterOption) { | ||
WHERE = `${WHERE}${paramKey}${comparisonString}${ | ||
isNumericValue ? value : `'${value}'` | ||
}`; | ||
} | ||
|
||
if (isLastParameter) { | ||
WHERE = `${WHERE} `; | ||
} else { | ||
WHERE = `${WHERE} AND `; | ||
} | ||
|
||
paramString = paramString.concat(WHERE); | ||
}); | ||
|
||
return paramString; | ||
}; |
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,50 @@ | ||
const ALLOWED_PARAMS = { | ||
annual: ['adm0', 'adm1', 'adm2', 'threshold', 'forestType', 'landCategory'], | ||
treeCoverDensity: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'threshold', | ||
'forestType', | ||
'landCategory', | ||
], | ||
integrated_alerts: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'forestType', | ||
'landCategory', | ||
'is__confirmed_alert', | ||
], | ||
glad: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'forestType', | ||
'landCategory', | ||
'is__confirmed_alert', | ||
], | ||
viirs: ['adm0', 'adm1', 'adm2', 'forestType', 'landCategory', 'confidence'], | ||
modis: ['adm0', 'adm1', 'adm2', 'forestType', 'landCategory', 'confidence'], | ||
modis_burned_area: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'threshold', | ||
'forestType', | ||
'landCategory', | ||
'confidence', | ||
], | ||
net_change: [ | ||
'adm0', | ||
'adm1', | ||
'adm2', | ||
'threshold', | ||
'forestType', | ||
'landCategory', | ||
'confidence', | ||
], | ||
tropicalTreeCover: ['adm0', 'adm1', 'adm2', 'threshold', 'forestType'], | ||
}; | ||
|
||
export default ALLOWED_PARAMS; |
Oops, something went wrong.
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.
🎉