Skip to content
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

SQL Refactor for Global Pages #3383

Merged
merged 2 commits into from
Apr 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions app/javascript/services/forest-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const CARTO_REQUEST_URL = `${process.env.CARTO_API_URL}/sql?q=`;

const SQL_QUERIES = {
extent:
"SELECT SUM({extentYear}) as value, SUM(area_gadm28) as total_area FROM data WHERE {location} AND thresh = {threshold} AND polyname = '{indicator}'",
"SELECT SUM({extentYear}) as value, SUM(area_gadm28) as total_area FROM data WHERE {location} thresh = {threshold} AND polyname = '{indicator}'",
plantationsExtent:
"SELECT SUM(area_poly_aoi) AS plantation_extent, {admin} AS region, {bound} AS label FROM data WHERE {location} AND thresh = 0 AND polyname = 'plantations' GROUP BY {type} ORDER BY plantation_extent DESC",
"SELECT SUM(area_poly_aoi) AS plantation_extent, {admin} AS region, {bound} AS label FROM data WHERE {location} thresh = 0 AND polyname = 'plantations' GROUP BY {type} ORDER BY plantation_extent DESC",
multiRegionExtent:
"SELECT {region} as region, SUM({extentYear}) as extent, SUM(area_gadm28) as total FROM data WHERE {location} AND thresh = {threshold} AND polyname = '{indicator}' GROUP BY {region} ORDER BY {region}",
"SELECT {region} as region, SUM({extentYear}) as extent, SUM(area_gadm28) as total FROM data WHERE {location} thresh = {threshold} AND polyname = '{indicator}' GROUP BY {region} ORDER BY {region}",
rankedExtent:
"SELECT polyname, SUM({extent_year}) as value, SUM(area_gadm28) as total_area, FROM data WHERE polyname='{polyname}' AND thresh={threshold} GROUP BY polyname, iso",
gain:
"SELECT {calc} as value FROM data WHERE {location} AND polyname = '{indicator}' AND thresh = 0",
"SELECT {calc} as value FROM data WHERE {location} polyname = '{indicator}' AND thresh = 0",
gainRanked:
"SELECT {region} as region, SUM(area_gain) AS gain, FROM data WHERE {location} AND polyname = '{polyname}' AND thresh = 0 GROUP BY region",
"SELECT {region} as region, SUM(area_gain) AS gain FROM data WHERE {location} polyname = '{polyname}' AND thresh = 0 GROUP BY region",
gainLocations:
"SELECT {admin} as region, {calc} as gain, FROM data WHERE {location} AND thresh = 0 AND polyname = '{indicator}' {grouping} ",
"SELECT {admin} as region, {calc} as gain FROM data WHERE {location} thresh = 0 AND polyname = '{indicator}' {grouping} ",
loss:
"SELECT polyname, year_data.year as year, SUM(year_data.area_loss) as area, SUM(year_data.emissions) as emissions FROM data WHERE polyname = '{indicator}' AND {location} AND thresh= {threshold} GROUP BY polyname, iso, nested(year_data.year)",
"SELECT polyname, year_data.year as year, SUM(year_data.area_loss) as area, SUM(year_data.emissions) as emissions FROM data WHERE polyname = '{indicator}' AND {location} thresh= {threshold} GROUP BY polyname, iso, nested(year_data.year)",
locations:
"SELECT {location} as region, {extentYear} as extent, {extent} as total FROM data WHERE iso = '{iso}' AND thresh = {threshold} AND polyname = '{indicator}' {grouping}",
locationsLoss:
Expand All @@ -36,16 +36,16 @@ const SQL_QUERIES = {
faoDeforestRank:
'WITH mytable AS (SELECT fao.country as iso, fao.name, fao.deforest * 1000 AS deforest, fao.humdef FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {year} AND deforest is not null), rank AS (SELECT deforest, iso, name from mytable ORDER BY mytable.deforest DESC) SELECT row_number() over () as rank, iso, name, deforest from rank',
faoEcoLive:
'SELECT fao.country, fao.forempl, fao.femempl, fao.usdrev, fao.usdexp, fao.gdpusd2012, fao.totpop1000, fao.year FROM table_7_economics_livelihood as fao WHERE fao.year = 1990 or fao.year = 2000 or fao.year = 2005 or fao.year = 2010 or fao.year = 9999'
'SELECT fao.country, fao.forempl, fao.femempl, fao.usdrev, fao.usdexp, fao.gdpusd2012, fao.totpop1000, fao.year FROM table_7_economics_livelihood as fao WHERE fao.year = 2000 or fao.year = 2005 or fao.year = 2010 or fao.year = 9999'
};

const getExtentYear = year =>
(year === 2000 ? 'area_extent_2000' : 'area_extent');

const getLocationQuery = (country, region, subRegion) =>
`iso = '${country}'${region ? ` AND adm1 = ${region}` : ''}${
subRegion ? ` AND adm2 = ${subRegion}` : ''
}`;
`${country ? `iso = '${country}'` : ''}${
region ? ` AND adm1 = ${region} AND` : ' AND'
}${subRegion ? ` adm2 = ${subRegion} AND` : ''}`;

export const getLocations = ({
country,
Expand Down Expand Up @@ -235,7 +235,7 @@ export const getGainRanked = ({

const location = region
? `iso = '${country}' ${subRegion ? `AND adm1 = ${region}` : ''}`
: '1 = 1';
: '';

const url = `${REQUEST_URL}${SQL_QUERIES.gainRanked}`
.replace('{region}', regionValue)
Expand Down