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

[FLAG-982] Weekly Fire Alerts wrong date comparison #4735

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 13 additions & 7 deletions components/widgets/fires/fires-alerts/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ export const getData = createSelector(
[getAlerts, getLatest],
(data, latest) => {
if (!data || isEmpty(data)) return null;

const parsedData = data.map((d) => ({
...d,
count: d.alert__count || d.area_ha,
week: parseInt(d.alert__week, 10),
year: parseInt(d.alert__year, 10),
}));

const groupedByYear = groupBy(sortBy(parsedData, ['year', 'week']), 'year');

const hasAlertsByYears = Object.values(groupedByYear).reduce(
(acc, next) => {
const { year } = next[0];
Expand All @@ -57,9 +60,11 @@ export const getData = createSelector(
const dataYears = Object.keys(hasAlertsByYears).filter(
(key) => hasAlertsByYears[key] === true
);

const years = dataYears.map((item) => parseInt(item, 10));
const formattedData = [];
const latestWeek = moment(latest);

const lastWeek = {
isoWeek: latestWeek.isoWeek(),
year: latestWeek.year(),
Expand All @@ -81,6 +86,10 @@ export const getData = createSelector(
}

for (let i = 1; i <= yearLength[year]; i += 1) {
if (Object.keys(yearDataByWeek).length < i) {
return;
}

const yearDataLength = yearDataByWeek[i]
? yearDataByWeek[i].length - 1
: 0;
Expand Down Expand Up @@ -159,27 +168,25 @@ export const parseData = createSelector(
// @TODO: better compare year parsing
const { year: startYear, week: startWeek } = currentData[0];
const yearDifference = maxminYear.max - startYear;

const compareStartYear = compareYear - yearDifference;

const weekFound = !!data.find(
(el) => el.year === compareStartYear && el.week === startWeek
);

const findWeek = weekFound ? startWeek : 1;
const findYear = weekFound ? compareStartYear : compareStartYear + 1;

const findYear = compareStartYear + 1;
const compareStartIndex = data.findIndex(
(el) => el.year === findYear && el.week === findWeek
);

const parsedData = currentData.map((d, i) => {
if (compareYear) {
const parsedCompareYear = compareYear - yearDifference;
const compareWeek = data[compareStartIndex + i];

return {
...d,
compareYear: parsedCompareYear,
compareYear,
compareCount: compareWeek ? compareWeek.count : null,
};
}
Expand All @@ -196,7 +203,6 @@ export const parseBrushedData = createSelector(
if (!data || isEmpty(data)) return null;

const { startIndex, endIndex } = indexes;

const start = startIndex || 0;
const end = endIndex || data.length - 1;

Expand All @@ -222,7 +228,7 @@ export const getLegend = createSelector(
...(compareYear && {
compare: {
label: `${moment(first.date)
.set('year', first.compareYear)
.set('year', first.compareYear - 1) // the last 12 months of the year selected
.format('MMM YYYY')}–${moment(end.date)
.set('year', end.compareYear)
.format('MMM YYYY')}`,
Expand Down
8 changes: 4 additions & 4 deletions components/widgets/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ export const getStatsData = (data, latest) => {

const latestWeek = moment(latest);

const lastYear = data.filter(
const pastTwelveMonths = data.filter(
(item) =>
item.year === latestWeek.year() ||
(item.week >= latestWeek.isoWeek() && item.year >= latestWeek.year() - 1) // 52 weeks ago, including latestWeek
);
(item.week >= latestWeek.isoWeek() && item.year === latestWeek.year() - 1)
); // 52 weeks ago, including latestWeek

const parsedData = lastYear.map((d, i) => {
const parsedData = pastTwelveMonths.map((d, i) => {
const weekMean = (translatedMeans && translatedMeans[i]) || 0;
const stdDev = (translatedStds && translatedStds[i]) || 0;

Expand Down
Loading