Skip to content

Commit 8e3542b

Browse files
Update local_forecasts.js
1 parent 2eceaa9 commit 8e3542b

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

local_forecasts.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -875,22 +875,36 @@ function readCompressedJsonAndAddBanners(fileUrl, selectedSource) {
875875
$(".pollutant-banner-o").append(skeleton);
876876
}
877877

878-
// Try loading hourly snapshot first
878+
// Try loading hourly snapshot first - attempt current hour and fallback to previous hours
879879
const now = new Date();
880-
const snapshotPath = `precomputed/hourly_forecasts/${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}.json`;
880+
const attemptHourlyLoad = async () => {
881+
// Try current hour and up to 6 hours back
882+
for (let hoursBack = 0; hoursBack <= 6; hoursBack++) {
883+
const attemptDate = new Date(now);
884+
attemptDate.setHours(attemptDate.getHours() - hoursBack);
885+
886+
const snapshotPath = `precomputed/hourly_forecasts/${attemptDate.getFullYear()}-${String(attemptDate.getMonth() + 1).padStart(2, '0')}-${String(attemptDate.getDate()).padStart(2, '0')}_${String(attemptDate.getHours()).padStart(2, '0')}.json`;
887+
888+
try {
889+
const response = await fetch(snapshotPath);
890+
if (response.ok) {
891+
const hourlySnapshot = await response.json();
892+
console.log(`Loaded hourly forecast from ${hoursBack} hours ago`);
893+
processHourlySnapshot(hourlySnapshot, filteredIndices);
894+
return;
895+
}
896+
} catch (error) {
897+
// Try next hour back
898+
continue;
899+
}
900+
}
901+
902+
// If no hourly forecasts found, fall back to individual sites
903+
console.log('No recent hourly forecasts available, loading individual sites...');
904+
loadIndividualSites(filteredIndices);
905+
};
881906

882-
fetch(snapshotPath)
883-
.then(response => {
884-
if (!response.ok) throw new Error('Hourly snapshot not found');
885-
return response.json();
886-
})
887-
.then(hourlySnapshot => {
888-
processHourlySnapshot(hourlySnapshot, filteredIndices);
889-
})
890-
.catch(error => {
891-
console.log('Hourly snapshot not available, falling back to individual files:', error);
892-
loadIndividualSites(filteredIndices);
893-
});
907+
attemptHourlyLoad();
894908
})
895909
.catch(error => {
896910
hideLoadingDiv();
@@ -945,22 +959,36 @@ function readCompressedJsonAndAddBannersOptimized(fileUrl, selectedSource) {
945959
$(".pollutant-banner-o").append(skeleton);
946960
}
947961

948-
// Try loading hourly snapshot first
962+
// Try loading hourly snapshot first - attempt current hour and fallback to previous hours
949963
const now = new Date();
950-
const snapshotPath = `precomputed/hourly_forecasts/${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}.json`;
964+
const attemptHourlyLoadOptimized = async () => {
965+
// Try current hour and up to 6 hours back
966+
for (let hoursBack = 0; hoursBack <= 6; hoursBack++) {
967+
const attemptDate = new Date(now);
968+
attemptDate.setHours(attemptDate.getHours() - hoursBack);
969+
970+
const snapshotPath = `precomputed/hourly_forecasts/${attemptDate.getFullYear()}-${String(attemptDate.getMonth() + 1).padStart(2, '0')}-${String(attemptDate.getDate()).padStart(2, '0')}_${String(attemptDate.getHours()).padStart(2, '0')}.json`;
971+
972+
try {
973+
const response = await fetch(snapshotPath);
974+
if (response.ok) {
975+
const hourlySnapshot = await response.json();
976+
console.log(`Loaded hourly forecast from ${hoursBack} hours ago`);
977+
processHourlySnapshotOptimized(hourlySnapshot, filteredIndices);
978+
return;
979+
}
980+
} catch (error) {
981+
// Try next hour back
982+
continue;
983+
}
984+
}
985+
986+
// If no hourly forecasts found, fall back to individual sites
987+
console.log('No recent hourly forecasts available, loading individual sites...');
988+
loadIndividualSitesOptimized(filteredIndices);
989+
};
951990

952-
fetch(snapshotPath)
953-
.then(response => {
954-
if (!response.ok) throw new Error('Hourly snapshot not found');
955-
return response.json();
956-
})
957-
.then(hourlySnapshot => {
958-
processHourlySnapshotOptimized(hourlySnapshot, filteredIndices);
959-
})
960-
.catch(error => {
961-
console.log('Hourly snapshot not available, falling back to individual files:', error);
962-
loadIndividualSitesOptimized(filteredIndices);
963-
});
991+
attemptHourlyLoadOptimized();
964992
})
965993
.catch(error => {
966994
hideLoadingDiv();

0 commit comments

Comments
 (0)