mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-23 12:09:30 -07:00
Improve SPC Outlook data fetching and error handling
- Switch to safeJson() and safePromiseAll() for centralized error handling - Only set failed state if enabled
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
// display spc outlook in a bar graph
|
// display spc outlook in a bar graph
|
||||||
|
|
||||||
import STATUS from './status.mjs';
|
import STATUS from './status.mjs';
|
||||||
import { json } from './utils/fetch.mjs';
|
import { safeJson, safePromiseAll } from './utils/fetch.mjs';
|
||||||
import { DateTime } from '../vendor/auto/luxon.mjs';
|
import { DateTime } from '../vendor/auto/luxon.mjs';
|
||||||
import WeatherDisplay from './weatherdisplay.mjs';
|
import WeatherDisplay from './weatherdisplay.mjs';
|
||||||
import { registerDisplay } from './navigation.mjs';
|
import { registerDisplay } from './navigation.mjs';
|
||||||
import testPolygon from './utils/polygon.mjs';
|
import testPolygon from './utils/polygon.mjs';
|
||||||
|
import { debugFlag } from './utils/debug.mjs';
|
||||||
|
|
||||||
// list of interesting files ordered [0] = today, [1] = tomorrow...
|
// list of interesting files ordered [0] = today, [1] = tomorrow...
|
||||||
const urlPattern = (day) => `https://www.spc.noaa.gov/products/outlook/day${day}otlk_cat.nolyr.geojson`;
|
const urlPattern = (day) => `https://www.spc.noaa.gov/products/outlook/day${day}otlk_cat.nolyr.geojson`;
|
||||||
@@ -18,8 +19,10 @@ const testAllPoints = (point, data) => {
|
|||||||
data.forEach((day, index) => {
|
data.forEach((day, index) => {
|
||||||
// initialize the result
|
// initialize the result
|
||||||
result[index] = false;
|
result[index] = false;
|
||||||
// if there's no data (file didn't load), exit early
|
// ensure day exists and has features array
|
||||||
if (day === undefined) return;
|
if (!day || !day.features || !Array.isArray(day.features)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// loop through each category
|
// loop through each category
|
||||||
day.features.forEach((feature) => {
|
day.features.forEach((feature) => {
|
||||||
if (!feature.geometry.coordinates) return;
|
if (!feature.geometry.coordinates) return;
|
||||||
@@ -46,7 +49,7 @@ class SpcOutlook extends WeatherDisplay {
|
|||||||
// don't display on progress/navigation screen
|
// don't display on progress/navigation screen
|
||||||
this.showOnProgress = false;
|
this.showOnProgress = false;
|
||||||
|
|
||||||
// calculate file names
|
// calculate file names, one for each day
|
||||||
this.files = [null, null, null].map((v, i) => urlPattern(i + 1));
|
this.files = [null, null, null].map((v, i) => urlPattern(i + 1));
|
||||||
|
|
||||||
// set timings
|
// set timings
|
||||||
@@ -56,27 +59,43 @@ class SpcOutlook extends WeatherDisplay {
|
|||||||
async getData(weatherParameters, refresh) {
|
async getData(weatherParameters, refresh) {
|
||||||
if (!super.getData(weatherParameters, refresh)) return;
|
if (!super.getData(weatherParameters, refresh)) return;
|
||||||
|
|
||||||
// initial data does not need to be reloaded on a location change, only during silent refresh
|
// SPC outlook data does not need to be reloaded on a location change, only during silent refresh
|
||||||
if (!this.initialData || refresh) {
|
if (!this.rawOutlookData || refresh) {
|
||||||
try {
|
try {
|
||||||
// get the three categorical files to get started
|
// get the data for today, tomorrow, and the day after
|
||||||
const filePromises = await Promise.allSettled(this.files.map((file) => json(file)));
|
const filePromises = this.files.map((file) => safeJson(file, {
|
||||||
// store the data, promise will always be fulfilled
|
retryCount: 1, // Retry one time
|
||||||
this.initialData = filePromises.map((outlookDay) => outlookDay.value);
|
timeout: 10000, // 10 second timeout for SPC outlook data
|
||||||
} catch (error) {
|
}));
|
||||||
console.error('Unable to get spc outlook');
|
// wait for all the data to be fetched; always returns an array of (potentially null) results
|
||||||
console.error(error.status, error.responseJSON);
|
this.rawOutlookData = await safePromiseAll(filePromises);
|
||||||
// if there's no previous data, fail
|
|
||||||
if (!this.initialData) {
|
// Filter out null results (like failed requests) and ensure the response has GeoJSON-looking data
|
||||||
this.setStatus(STATUS.failed);
|
this.rawOutlookData = this.rawOutlookData.filter((value) => value && value.features);
|
||||||
|
|
||||||
|
if (this.rawOutlookData.length === 0) {
|
||||||
|
if (debugFlag('verbose-failures')) {
|
||||||
|
console.warn('SPC Outlook has zero days of data');
|
||||||
|
}
|
||||||
|
if (this.isEnabled) this.setStatus(STATUS.failed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.rawOutlookData.length < this.files.length) {
|
||||||
|
if (debugFlag('verbose-failures')) {
|
||||||
|
console.warn(`SPC Outlook only loaded ${this.rawOutlookData.length} of ${this.files.length} days successfully`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Unexpected error getting SPC Outlook data: ${error.message}`);
|
||||||
|
if (this.isEnabled) this.setStatus(STATUS.failed);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// do the initial parsing of the data
|
// parse the data
|
||||||
this.data = testAllPoints([weatherParameters.longitude, weatherParameters.latitude], this.initialData);
|
this.data = testAllPoints([weatherParameters.longitude, weatherParameters.latitude], this.rawOutlookData);
|
||||||
|
|
||||||
// if all the data returns false the there's nothing to do, skip this screen
|
// check if there's a "risk" for any of the three days, otherwise skip the SPC Outlook screen
|
||||||
if (this.data.reduce((prev, cur) => prev || !!cur, false)) {
|
if (this.data.reduce((prev, cur) => prev || !!cur, false)) {
|
||||||
this.timing.totalScreens = 1;
|
this.timing.totalScreens = 1;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user