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

fix: some axios requests not having a catch #512

Merged
merged 1 commit into from
Jun 29, 2023
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
91 changes: 48 additions & 43 deletions cap-file-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,69 @@ function fetchCapFileAndParse(url, latLong, callback, isExisting = false) {
if (!url || !latLong) return;

// url for a cap file has been given to us, so now we need to see if it's relevant to us
axios.get(url).then((resp) => {
const data = resp.data;
if (!data) return;
axios
.get(url)
.then((resp) => {
const data = resp.data;
if (!data) return;

// convert to js object
const weatherAlertCAP = xmljs.xml2js(data, { compact: true });
if (!weatherAlertCAP) return;
// convert to js object
const weatherAlertCAP = xmljs.xml2js(data, { compact: true });
if (!weatherAlertCAP) return;

// find the alert->info->area and see if any include the city we're tracking
// we're presuming info[0] for the english version
const alert = weatherAlertCAP.alert;
const info_en = alert?.info.find((i) => i.language?._text === "en-CA");
let areas = info_en?.area || [];
// find the alert->info->area and see if any include the city we're tracking
// we're presuming info[0] for the english version
const alert = weatherAlertCAP.alert;
const info_en = alert?.info.find((i) => i.language?._text === "en-CA");
let areas = info_en?.area || [];

if (!Array.isArray(areas)) areas = [areas];
if (!Array.isArray(areas)) areas = [areas];

// loop through areas and see if our tracked city (via lat/long) is included
if (!isExisting) {
const pointToCheck = [latLong.lat, latLong.long];
const isRelevant = areas.some((area) => {
const polygon = area.polygon?._text || "";
const polygonAsArray = convertPolygonStringTo3DArray(polygon);
if (!polygonAsArray) return;
// loop through areas and see if our tracked city (via lat/long) is included
if (!isExisting) {
const pointToCheck = [latLong.lat, latLong.long];
const isRelevant = areas.some((area) => {
const polygon = area.polygon?._text || "";
const polygonAsArray = convertPolygonStringTo3DArray(polygon);
if (!polygonAsArray) return;

return pointInPolygon(pointToCheck, polygonAsArray);
});
return pointInPolygon(pointToCheck, polygonAsArray);
});

// if its not relevant stop, otherwise get the data
if (!isRelevant) return;
}
// if its not relevant stop, otherwise get the data
if (!isRelevant) return;
}

// check we have the required information here
const identifier = alert?.identifier?._text;
if (!identifier) return;
// check we have the required information here
const identifier = alert?.identifier?._text;
if (!identifier) return;

const sent = alert?.sent?._text || "";
const references = alert?.references?._text || "";
const sent = alert?.sent?._text || "";
const references = alert?.references?._text || "";

const expires = info_en?.expires?._text;
if (!expires) return;
const expires = info_en?.expires?._text;
if (!expires) return;

const headline = info_en?.headline?._text;
if (!headline) return;
const headline = info_en?.headline?._text;
if (!headline) return;

const description = info_en?.description?._text;
if (!description) return;
const description = info_en?.description?._text;
if (!description) return;

const severity = info_en?.severity?._text;
if (!severity) return;
const severity = info_en?.severity?._text;
if (!severity) return;

const urgency = info_en?.urgency?._text;
if (!urgency) return;
const urgency = info_en?.urgency?._text;
if (!urgency) return;

console.log(`[CAP PARSER] CAP ${url} has been parsed, passing along to alert monitor`);
console.log(`[CAP PARSER] CAP ${url} has been parsed, passing along to alert monitor`);

if (typeof callback === "function")
callback({ identifier, references, expires, headline, description, severity, urgency, url, sent });
});
if (typeof callback === "function")
callback({ identifier, references, expires, headline, description, severity, urgency, url, sent });
})
.catch(() => {
console.error("[CAP PARSER]", "Failed to fetch CAP file from", url);
});
}

const convertPolygonStringTo3DArray = (polygonString) => {
Expand Down
95 changes: 50 additions & 45 deletions current-conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,51 +98,56 @@ const fetchCurrentConditions = (url) => {

const { province, location } = currentConditionsLocation;
url = url || `https://dd.weather.gc.ca/citypage_weather/xml/${province}/${location}_e.xml`;
axios.get(url).then((resp) => {
const weather = new Weather(resp.data);
if (!weather) return;

// all: almanac, riseset, location, yesterday
const allWeatherData = weather.all;
if (!allWeatherData) return;

// first step is figure out the condition id
const conditionID = generateConditionsUniqueID(weather.current?.dateTime[1]);

// if we got the same condition id, and we're rejecting in-hour, all we need to do is update the forecast
if (configRejectInHourConditonUpdates && conditionID === previousConditionsID) {
console.log("[CONDITIONS]", "Rejecting in-hour update for conditon ID", conditionID);
return (conditions.forecast = weather.weekly);
}

// store the long/lat for later use
const {
name: { lat, lon },
} = allWeatherData.location;
parseStationLatLong(lat, lon);

// generate some objects so the FE has less work to do once it receives the data
const observedDateObject = generateConditionsObserved(weather.current?.dateTime[1]);
const city = generateObservedCity(allWeatherData.location);
const observedConditions = generateConditions(weather.current);
const sunRiseSet = generateSunriseSet(allWeatherData.riseSet?.dateTime);
const almanac = generateAlmanac(allWeatherData.almanac);
const windchill = generateWindchill(observedConditions);

// place these into the global conditions object we have that can be used elsewhere
conditions.city = city;
conditions.observed = observedDateObject;
conditions.conditions = observedConditions;
conditions.riseSet = sunRiseSet;
conditions.regionalNormals = weather.all.regionalNormals;
conditions.almanac = almanac;
conditions.windchill = windchill;
conditions.conditionID = conditionID;
conditions.forecast = weather.weekly;

// if the conditions ID has updated this means new info is available which means we should fetch more historical data
if (conditions.conditionID !== previousConditionsID) historicalData && historicalData.fetchHistoricalData();
});
axios
.get(url)
.then((resp) => {
const weather = new Weather(resp.data);
if (!weather) return;

// all: almanac, riseset, location, yesterday
const allWeatherData = weather.all;
if (!allWeatherData) return;

// first step is figure out the condition id
const conditionID = generateConditionsUniqueID(weather.current?.dateTime[1]);

// if we got the same condition id, and we're rejecting in-hour, all we need to do is update the forecast
if (configRejectInHourConditonUpdates && conditionID === previousConditionsID) {
console.log("[CONDITIONS]", "Rejecting in-hour update for conditon ID", conditionID);
return (conditions.forecast = weather.weekly);
}

// store the long/lat for later use
const {
name: { lat, lon },
} = allWeatherData.location;
parseStationLatLong(lat, lon);

// generate some objects so the FE has less work to do once it receives the data
const observedDateObject = generateConditionsObserved(weather.current?.dateTime[1]);
const city = generateObservedCity(allWeatherData.location);
const observedConditions = generateConditions(weather.current);
const sunRiseSet = generateSunriseSet(allWeatherData.riseSet?.dateTime);
const almanac = generateAlmanac(allWeatherData.almanac);
const windchill = generateWindchill(observedConditions);

// place these into the global conditions object we have that can be used elsewhere
conditions.city = city;
conditions.observed = observedDateObject;
conditions.conditions = observedConditions;
conditions.riseSet = sunRiseSet;
conditions.regionalNormals = weather.all.regionalNormals;
conditions.almanac = almanac;
conditions.windchill = windchill;
conditions.conditionID = conditionID;
conditions.forecast = weather.weekly;

// if the conditions ID has updated this means new info is available which means we should fetch more historical data
if (conditions.conditionID !== previousConditionsID) historicalData && historicalData.fetchHistoricalData();
})
.catch(() => {
console.error("[CONDITIONS]", "Failed to fetch in-hour update from AMQP push");
});
};

const parseStationLatLong = (lat, long) => {
Expand Down