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

Fixed GLAD alerts getLatest and added a fetchFiresLatest method, both… #3711

Merged
merged 1 commit into from
Mar 7, 2019
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { fetchFiresAlerts } from 'services/alerts';
import { fetchFiresAlerts, fetchFiresLatest } from 'services/alerts';
import axios from 'axios';

export default ({ params }) =>
fetchFiresAlerts(params).then(alerts => {
const { data } = alerts.data;
return data || {};
});
axios.all([fetchFiresAlerts(params), fetchFiresLatest(params)]).then(
axios.spread((alerts, latest) => {
const { data } = alerts.data;
return { alerts: data, latest } || {};
})
);
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
getChartConfig
} from 'components/widgets/utils/data';

const getAlerts = state => state.data || null;
const getAlerts = state => (state.data && state.data.alerts) || null;
const getLatest = state => (state.data && state.data.latest) || null;
const getColors = state => state.colors || null;
const getActiveData = state => state.settings.activeData || null;
const getWeeks = state => state.settings.weeks || null;
Expand Down Expand Up @@ -87,13 +88,9 @@ export const parseData = createSelector([getDates, getWeeks], (data, weeks) => {
return data.slice(-weeks);
});

export const parseConfig = createSelector([getColors], colors =>
getChartConfig(
colors,
moment()
.subtract(2, 'w')
.format('YYYY-MM-DD')
)
export const parseConfig = createSelector(
[getColors, getLatest],
(colors, latest) => getChartConfig(colors, moment(latest))
);

export const parseSentence = createSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { fetchGladAlerts, fetchGLADLatest } from 'services/alerts';
import axios from 'axios';

export default ({ params }) =>
axios.all([fetchGladAlerts(params), fetchGLADLatest()]).then(
axios.all([fetchGladAlerts(params), fetchGLADLatest(params)]).then(
axios.spread((alerts, latest) => {
let data = {};
if (alerts && alerts.data && latest && latest.data) {
const alertsData = alerts.data.data;
const latestData = latest.data.data;
data = {
alerts: alertsData,
latest: latestData.length && latestData[0].attributes.date
latest:
latestData &&
latestData.attributes &&
latestData.attributes.updatedAt
};
}
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default ({ params }) =>
axios
.all([
fetchGladIntersectionAlerts({ ...params }),
fetchGLADLatest(),
fetchGLADLatest(params),
getMultiRegionExtent({ ...params })
])
.then(
Expand All @@ -18,7 +18,7 @@ export default ({ params }) =>
? {
alerts: data,
extent: areas,
latest: latestData[0].attributes.date
latest: latestData.attributes && latestData.attributes.updatedAt
}
: {};
})
Expand Down
46 changes: 37 additions & 9 deletions app/javascript/services/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ export const fetchFiresAlerts = ({ adm0, adm1, adm2, dataset }) => {
return request.get(url, 3600, 'firesRequest');
};

export const fetchFiresLatest = ({ adm1, adm2 }) => {
let fires_summary_table = FIRES_ISO_DATASET;
if (adm2) {
fires_summary_table = FIRES_ADM2_DATASET;
} else if (adm1) {
fires_summary_table = FIRES_ADM1_DATASET;
}
const url = `${REQUEST_URL}/dataset/${fires_summary_table}`;
return request.get(url, 3600, 'firesRequest').catch(error => {
console.error('Error in firesRequest:', error);
return new Promise(resolve =>
resolve({
data: {
data: {
attributes: { updatedAt: lastFriday },
id: null,
type: 'fires-alerts'
}
}
})
);
});
};

export const fetchViirsAlerts = ({ adm0, adm1, adm2, dates }) => {
const url = `${REQUEST_URL}/viirs-active-fires/${!adm2 ? 'admin/' : ''}${
QUERIES.viirsAlerts
Expand Down Expand Up @@ -112,20 +136,24 @@ export const fetchLatestDate = url =>
);
});

export const fetchGLADLatest = () => {
const url = `${REQUEST_URL}/glad-alerts/latest`;
export const fetchGLADLatest = ({ adm1, adm2 }) => {
let glad_summary_table = GLAD_ISO_DATASET;
if (adm2) {
glad_summary_table = GLAD_ADM2_DATASET;
} else if (adm1) {
glad_summary_table = GLAD_ADM1_DATASET;
}
const url = `${REQUEST_URL}/dataset/${glad_summary_table}`;
return request.get(url, 3600, 'gladRequest').catch(error => {
console.error('Error in gladRequest:', error);
return new Promise(resolve =>
resolve({
data: {
data: [
{
attributes: { date: lastFriday },
id: null,
type: 'glad-alerts'
}
]
data: {
attributes: { updatedAt: lastFriday },
id: null,
type: 'glad-alerts'
}
}
})
);
Expand Down