Skip to content

Commit 2c7400b

Browse files
committed
Account for period that overlaps years
1 parent 1f5fd92 commit 2c7400b

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

components/widgets/fires/fires-ranked/selectors.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,40 @@ export const getYears = createSelector([getLatestDate], latest => {
3838
return years;
3939
});
4040

41+
export const getFilterWeeks = createSelector([getLatestDate, getOptionsSelected], (latest, options) => {
42+
const period = options.weeks && options.weeks.value || 4;
43+
const endWeek = moment(latest).isoWeek();
44+
const startWeek = moment(latest).subtract(period, 'weeks').isoWeek();
45+
return {startWeek, endWeek};
46+
});
47+
4148
export const getStatsByAdmin = createSelector(
42-
[getData, getYears, getAdm1, getLocation],
43-
(data, years, adm1, location) => {
49+
[getData, getYears, getAdm1, getLocation, getFilterWeeks],
50+
(data, years, adm1, location, filterWeeks) => {
4451
if (isEmpty(data) || isEmpty(years)) {
4552
return null;
4653
}
4754
let matchKey = 'iso';
4855
if (location.value !== 'global') matchKey = adm1 ? 'adm2' : 'adm1';
4956
const alertsByAdm = groupBy(data, matchKey);
50-
57+
const {startWeek, endWeek} = filterWeeks;
5158
const filteredAlertsByAdmin = Object.entries(alertsByAdm).map(
5259
([adm, adminAlerts]) => {
53-
const countsArray = years.map(year => {
54-
const filteredYear = adminAlerts.filter(el => el.year === year);
55-
return filteredYear.length > 0 ? sumBy(filteredYear, 'count') : 0;
56-
});
57-
60+
let countsArray = [];
61+
if (startWeek < endWeek) {
62+
countsArray = years.map(year => {
63+
const filteredYear = adminAlerts.filter(el => el.year === year);
64+
return filteredYear.length > 0 ? sumBy(filteredYear, 'count') : 0;
65+
});
66+
}
67+
else {
68+
// i.e. the period goes into previous year
69+
countsArray = years.map(year => {
70+
// in the case that the period goes over the year line we need to filter differently.
71+
const filteredYear = adminAlerts.filter(el => (el.year === year && el.week <= endWeek) || (el.year === year-1 && el.week > startWeek));
72+
return filteredYear.length > 0 ? sumBy(filteredYear, 'count') : 0;
73+
});
74+
}
5875
const stdDevCounts = stdDevData(countsArray);
5976
const meanCounts = mean(countsArray);
6077
const currentYearCounts = countsArray[countsArray.length - 1];
@@ -65,7 +82,6 @@ export const getStatsByAdmin = createSelector(
6582
return { id: adm, significance, currentYearCounts };
6683
}
6784
);
68-
6985
return filteredAlertsByAdmin;
7086
}
7187
);

services/analysis-cached.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const SQL_QUERIES = {
3434
fires:
3535
'SELECT {location}, alert__year, alert__week, SUM(alert__count) AS alert__count, confidence__cat FROM data {WHERE} GROUP BY {location}, alert__year, alert__week',
3636
firesGrouped:
37-
'SELECT {location}, alert__year, alert__week, SUM(alert__count) AS alert__count, confidence__cat FROM data {WHERE} AND ({dateFilter}) GROUP BY {location}, alert__year',
37+
'SELECT {location}, alert__year, alert__week, SUM(alert__count) AS alert__count, confidence__cat FROM data {WHERE} AND ({dateFilter}) GROUP BY {location}, alert__year, alert__week',
3838
firesWithin:
3939
'SELECT {location}, alert__week, alert__year, SUM(alert__count) AS alert__count, confidence__cat FROM data {WHERE} AND alert__year >= {alert__year} AND alert__week >= 1 GROUP BY alert__year, alert__week ORDER BY alert__week DESC, alert__year DESC',
4040
nonGlobalDatasets:
@@ -250,11 +250,11 @@ export const getWeeksFilter = ({ weeks, latest }) => {
250250
const yf = d.endYear || '';
251251
const wf = d.endWeek || '';
252252

253-
return `${acc} ${i === 0 ? '' : 'OR '}(alert__year = ${
253+
return `${acc} ${i === 0 ? '' : 'OR '}((alert__year = ${
254254
yi
255-
} AND alert__week > ${wi}) AND (alert__year = ${yf} AND alert__week <= ${
255+
} AND alert__week > ${wi}) ${wi < wf ? 'AND' : 'OR'} (alert__year = ${yf} AND alert__week <= ${
256256
wf
257-
})`;
257+
}))`;
258258
}, '');
259259
};
260260

@@ -736,6 +736,7 @@ export const fetchVIIRSAlertsGrouped = params => {
736736
data: {
737737
data: response.data.data.map(d => ({
738738
...d,
739+
week: parseInt(d.alert__week, 10),
739740
year: parseInt(d.alert__year, 10),
740741
count: d.alert__count,
741742
alerts: d.alert__count

0 commit comments

Comments
 (0)