Skip to content

Commit 4d4529c

Browse files
authored
Merge pull request #3367 from Vizzuality/feature/glad_summary_tables
GLAD Biodiversity Table Update
2 parents 8c047af + c8b9d09 commit 4d4529c

10 files changed

+104
-69
lines changed

Gemfile.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ GEM
100100
sass-rails (< 5.1)
101101
sprockets (< 4.0)
102102
concurrent-ruby (1.0.5)
103-
crass (1.0.3)
103+
crass (1.0.4)
104104
database_cleaner (1.6.2)
105105
debug_inspector (0.0.3)
106106
diff-lcs (1.3)
@@ -295,7 +295,7 @@ GEM
295295
railties (>= 4.2.0)
296296
thor (>= 0.14, < 2.0)
297297
json (2.1.0)
298-
loofah (2.2.1)
298+
loofah (2.2.2)
299299
crass (~> 1.0.2)
300300
nokogiri (>= 1.5.9)
301301
mail (2.7.0)
@@ -347,8 +347,8 @@ GEM
347347
rails-dom-testing (2.0.3)
348348
activesupport (>= 4.2.0)
349349
nokogiri (>= 1.6)
350-
rails-html-sanitizer (1.0.3)
351-
loofah (~> 2.0)
350+
rails-html-sanitizer (1.0.4)
351+
loofah (~> 2.2, >= 2.2.2)
352352
rails_12factor (0.0.3)
353353
rails_serve_static_assets
354354
rails_stdout_logging

app/javascript/components/widget/widgets/widget-glad-alerts/widget-glad-alerts-actions.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const getGladAlerts = createThunkAction(
1414
if (!state().widgetGladAlerts.loading) {
1515
dispatch(setGladAlertsLoading({ loading: true, error: false }));
1616
axios
17-
.all([fetchGladAlerts(params), fetchGLADLatest()])
17+
.all([fetchGladAlerts({ ...params }), fetchGLADLatest()])
1818
.then(
1919
axios.spread((alerts, latest) => {
2020
let data = {};
2121
if (alerts && alerts.data && latest && latest.data) {
22-
const alertsData = alerts.data.data;
22+
const alertsData = alerts.data;
2323
const latestData = latest.data.data;
2424
data = {
25-
alerts: alertsData.attributes.value,
25+
alerts: alertsData.data,
2626
latest: latestData[0].attributes.date
2727
};
2828
}

app/javascript/components/widget/widgets/widget-glad-alerts/widget-glad-alerts-selectors.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ export const getData = createSelector(
7878
years.forEach(d => {
7979
const yearDataByWeek = groupBy(groupedByYear[d], 'week');
8080
for (let i = 1; i <= yearLengths[d]; i += 1) {
81-
zeroFilledData.push(
82-
yearDataByWeek[i]
83-
? yearDataByWeek[i][0]
84-
: { count: 0, week: i, year: parseInt(d, 10) }
85-
);
81+
zeroFilledData.push(yearDataByWeek[i][0]);
8682
}
8783
});
8884
return zeroFilledData;

app/javascript/components/widget/widgets/widget-glad-biodiversity/widget-glad-biodiversity-actions.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { createAction } from 'redux-actions';
22
import { createThunkAction } from 'utils/redux';
33
import axios from 'axios';
4-
import moment from 'moment';
54

6-
import { fetchGladIntersectionAlerts } from 'services/alerts';
5+
import { fetchGladIntersectionAlerts, fetchGLADLatest } from 'services/alerts';
76
import { getMultiRegionExtent } from 'services/forest-data';
87

98
const setGladBiodiversityData = createAction('setGladBiodiversityData');
@@ -19,23 +18,22 @@ const getGladBiodiversity = createThunkAction(
1918
axios
2019
.all([
2120
fetchGladIntersectionAlerts({ ...params }),
21+
fetchGLADLatest(),
2222
getMultiRegionExtent({ ...params })
2323
])
2424
.then(
25-
axios.spread((alerts, extent) => {
25+
axios.spread((alerts, latest, extent) => {
2626
const { data } = alerts.data;
27+
const latestData = latest.data.data;
2728
const areas = extent.data.data;
28-
const alertsByDate =
29-
data &&
30-
data.filter(d =>
31-
moment(new Date(d.date)).isAfter(
32-
moment.utc().subtract(53, 'weeks')
33-
)
34-
);
3529
dispatch(
3630
setGladBiodiversityData(
37-
alertsByDate && extent
38-
? { data: alertsByDate, extent: areas }
31+
data && extent && latest
32+
? {
33+
data,
34+
extent: areas,
35+
latest: latestData[0].attributes.date
36+
}
3937
: {}
4038
)
4139
);

app/javascript/components/widget/widgets/widget-glad-biodiversity/widget-glad-biodiversity-selectors.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import moment from 'moment';
88

99
// get list data
1010
const getData = state => state.data || null;
11+
const getLatestDates = state => state.latest || null;
1112
const getExtent = state => state.extent || null;
1213
const getSettings = state => state.settings || null;
1314
const getOptions = state => state.options || null;
@@ -18,13 +19,33 @@ const getLocationNames = state => state.locationNames || null;
1819
const getColors = state => state.colors || null;
1920

2021
export const getSortedData = createSelector(
21-
[getData, getExtent, getSettings, getLocation, getLocationsMeta, getColors],
22-
(data, extent, settings, location, meta, colors) => {
22+
[
23+
getData,
24+
getLatestDates,
25+
getExtent,
26+
getSettings,
27+
getLocation,
28+
getLocationsMeta,
29+
getColors
30+
],
31+
(data, latest, extent, settings, location, meta, colors) => {
2332
if (!data || isEmpty(data) || !meta || isEmpty(meta)) return null;
33+
const latestWeek = moment(latest)
34+
.subtract(1, 'weeks')
35+
.week();
36+
const latestYear = moment(latest)
37+
.subtract(1, 'weeks')
38+
.year();
2439
const alertsByDate = data.filter(d =>
25-
moment(new Date(d.date)).isAfter(
26-
moment.utc().subtract(settings.weeks, 'weeks')
27-
)
40+
moment()
41+
.week(d.week)
42+
.year(d.year)
43+
.isAfter(
44+
moment()
45+
.week(latestWeek)
46+
.year(latestYear)
47+
.subtract(settings.weeks, 'weeks')
48+
)
2849
);
2950
const groupedAlerts = groupBy(
3051
alertsByDate,

app/javascript/components/widget/widgets/widget-glad-biodiversity/widget-glad-biodiversity.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const mapStateToProps = (
2222
const selectorData = {
2323
data: data.data,
2424
extent: data.extent,
25+
latest: data.latest,
2526
settings,
2627
options: settingsConfig.options,
2728
meta: countryData[!payload.region ? 'regions' : 'subRegions'],

app/javascript/components/widget/widgets/widget-glad-ranked/widget-glad-ranked-actions.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { createAction } from 'redux-actions';
22
import { createThunkAction } from 'utils/redux';
33
import axios from 'axios';
4-
import moment from 'moment';
54

6-
import { fetchGladIntersectionAlerts } from 'services/alerts';
5+
import { fetchGladIntersectionAlerts, fetchGLADLatest } from 'services/alerts';
76
import { getMultiRegionExtent } from 'services/forest-data';
87

98
const setGladRankedData = createAction('setGladRankedData');
@@ -14,28 +13,27 @@ const setGladRankedLoading = createAction('setGladRankedLoading');
1413
const getGladRanked = createThunkAction(
1514
'getGladRanked',
1615
params => (dispatch, state) => {
17-
if (!state().widgetGladRanked.loading) {
16+
if (!state().widgetGladBiodiversity.loading) {
1817
dispatch(setGladRankedLoading({ loading: true, error: false }));
1918
axios
2019
.all([
2120
fetchGladIntersectionAlerts({ ...params }),
21+
fetchGLADLatest(),
2222
getMultiRegionExtent({ ...params })
2323
])
2424
.then(
25-
axios.spread((alerts, extent) => {
25+
axios.spread((alerts, latest, extent) => {
2626
const { data } = alerts.data;
27+
const latestData = latest.data.data;
2728
const areas = extent.data.data;
28-
const alertsByDate =
29-
data &&
30-
data.filter(d =>
31-
moment(new Date(d.date)).isAfter(
32-
moment.utc().subtract(53, 'weeks')
33-
)
34-
);
3529
dispatch(
3630
setGladRankedData(
37-
alertsByDate && extent
38-
? { data: alertsByDate, extent: areas }
31+
data && extent && latest
32+
? {
33+
data,
34+
extent: areas,
35+
latest: latestData[0].attributes.date
36+
}
3937
: {}
4038
)
4139
);

app/javascript/components/widget/widgets/widget-glad-ranked/widget-glad-ranked-selectors.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import moment from 'moment';
88

99
// get list data
1010
const getData = state => state.data || null;
11+
const getLatestDates = state => state.latest || null;
1112
const getExtent = state => state.extent || null;
1213
const getSettings = state => state.settings || null;
1314
const getOptions = state => state.options || null;
@@ -18,13 +19,33 @@ const getLocationNames = state => state.locationNames || null;
1819
const getColors = state => state.colors || null;
1920

2021
export const getSortedData = createSelector(
21-
[getData, getExtent, getSettings, getLocation, getLocationsMeta, getColors],
22-
(data, extent, settings, location, meta, colors) => {
22+
[
23+
getData,
24+
getLatestDates,
25+
getExtent,
26+
getSettings,
27+
getLocation,
28+
getLocationsMeta,
29+
getColors
30+
],
31+
(data, latest, extent, settings, location, meta, colors) => {
2332
if (!data || isEmpty(data) || !meta || isEmpty(meta)) return null;
33+
const latestWeek = moment(latest)
34+
.subtract(1, 'weeks')
35+
.week();
36+
const latestYear = moment(latest)
37+
.subtract(1, 'weeks')
38+
.year();
2439
const alertsByDate = data.filter(d =>
25-
moment(new Date(d.date)).isAfter(
26-
moment.utc().subtract(settings.weeks, 'weeks')
27-
)
40+
moment()
41+
.year(d.year)
42+
.week(d.week)
43+
.isAfter(
44+
moment()
45+
.year(latestYear)
46+
.week(latestWeek)
47+
.subtract(settings.weeks, 'weeks')
48+
)
2849
);
2950
const groupedAlerts = groupBy(
3051
alertsByDate,

app/javascript/components/widget/widgets/widget-glad-ranked/widget-glad-ranked.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ const mapStateToProps = (
1717
const { colors, locationNames, settingsConfig, activeIndicator } = ownProps;
1818
const { payload } = location;
1919
const selectorData = {
20-
data: data.data,
21-
extent: data.extent,
20+
...data,
2221
settings,
2322
options: settingsConfig.options,
2423
meta: countryData[!payload.region ? 'regions' : 'subRegions'],

app/javascript/services/alerts.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios from 'axios';
2-
import moment from 'moment';
32

4-
const REQUEST_URL = `${process.env.GFW_API_HOST_PROD}`;
5-
const DATASET = process.env.GLAD_PRECALC_DATASET;
3+
const REQUEST_URL = process.env.GFW_API_HOST_PROD;
4+
const GLAD_ISO_DATASET = process.env.GLAD_ISO_DATASET;
5+
const GLAD_ADM1_DATASET = process.env.GLAD_ADM1_DATASET;
6+
const GLAD_ADM2_DATASET = process.env.GLAD_ADM2_DATASET;
67

78
const QUERIES = {
8-
gladAlerts: '{location}?aggregate_values=true&aggregate_by={period}',
99
gladIntersectionAlerts:
10-
"SELECT iso, adm1, adm2, alerts as count, alert_date as date, area_ha, polyname FROM data WHERE {location} AND alert_date > '{dateBound}' AND polyname = '{polyname}'",
10+
"SELECT iso, adm1, adm2, week, year, alerts as count, area_ha, polyname FROM data WHERE {location} AND polyname = '{polyname}'",
1111
viirsAlerts:
1212
'{location}?group=true&period={period}&thresh=0&geostore={geostore}'
1313
};
@@ -20,26 +20,27 @@ const getLocation = (country, region, subRegion) =>
2020
subRegion ? ` AND adm2 = ${subRegion}` : ''
2121
}`;
2222

23-
export const fetchGladAlerts = ({ country, region, subRegion, period }) => {
24-
const url = `${REQUEST_URL}/glad-alerts/admin/${QUERIES.gladAlerts}`
25-
.replace('{location}', getLocationQuery(country, region, subRegion))
26-
.replace('{period}', period || 'week');
23+
export const fetchGladAlerts = ({ country, region, subRegion }) => {
24+
let glad_summary_table = GLAD_ISO_DATASET;
25+
if (subRegion) {
26+
glad_summary_table = GLAD_ADM2_DATASET;
27+
} else if (region) {
28+
glad_summary_table = GLAD_ADM1_DATASET;
29+
}
30+
const url = `${REQUEST_URL}/query/${glad_summary_table}?sql=${
31+
QUERIES.gladIntersectionAlerts
32+
}`
33+
.replace('{location}', getLocation(country, region, subRegion))
34+
.replace('{polyname}', 'gadm28');
2735
return axios.get(url);
2836
};
2937

3038
export const fetchGladIntersectionAlerts = ({ country, region, indicator }) => {
31-
const url = `${REQUEST_URL}/query/${DATASET}?sql=${
32-
QUERIES.gladIntersectionAlerts
33-
}`
39+
const url = `${REQUEST_URL}/query/${
40+
region ? GLAD_ADM2_DATASET : GLAD_ADM1_DATASET
41+
}?sql=${QUERIES.gladIntersectionAlerts}`
3442
.replace('{location}', getLocation(country, region))
35-
.replace('{polyname}', indicator)
36-
.replace(
37-
'{dateBound}',
38-
moment
39-
.utc()
40-
.subtract(53, 'weeks')
41-
.format('YYYY/MM/DD')
42-
);
43+
.replace('{polyname}', indicator);
4344
return axios.get(url);
4445
};
4546

0 commit comments

Comments
 (0)