Fixing hourly
This commit is contained in:
@@ -119,6 +119,7 @@ export async function get24HourForecast(currentSensors) {
|
|||||||
|
|
||||||
const weather = pressureRule(pressure3h, seedPressure);
|
const weather = pressureRule(pressure3h, seedPressure);
|
||||||
|
|
||||||
|
let runningAccumulation = 0;
|
||||||
const slots = Array.from({ length: 24 }, (_, i) => {
|
const slots = Array.from({ length: 24 }, (_, i) => {
|
||||||
const time = new Date(now.getTime() + (i + 1) * 3600 * 1000);
|
const time = new Date(now.getTime() + (i + 1) * 3600 * 1000);
|
||||||
time.setMinutes(0, 0, 0);
|
time.setMinutes(0, 0, 0);
|
||||||
@@ -134,6 +135,7 @@ export async function get24HourForecast(currentSensors) {
|
|||||||
const precipitation_chance = Math.round(weather.precipitation_chance * (1 - i * 0.02) * 100);
|
const precipitation_chance = Math.round(weather.precipitation_chance * (1 - i * 0.02) * 100);
|
||||||
const raining = precipitation_chance > 60 ? 'True' : 'False';
|
const raining = precipitation_chance > 60 ? 'True' : 'False';
|
||||||
const precipitation = estimatePrecipitation(precipitation_chance, clouds);
|
const precipitation = estimatePrecipitation(precipitation_chance, clouds);
|
||||||
|
runningAccumulation = Math.round((runningAccumulation + precipitation) * 100) / 100;
|
||||||
|
|
||||||
const celestial = getCelestialCurrent(coords.latitude, coords.longitude, time);
|
const celestial = getCelestialCurrent(coords.latitude, coords.longitude, time);
|
||||||
const uv_index = estimateUV(celestial.sun_elevation ?? 0, clouds);
|
const uv_index = estimateUV(celestial.sun_elevation ?? 0, clouds);
|
||||||
@@ -157,7 +159,7 @@ export async function get24HourForecast(currentSensors) {
|
|||||||
raining,
|
raining,
|
||||||
precipitation,
|
precipitation,
|
||||||
precipitation_chance,
|
precipitation_chance,
|
||||||
accumulation: 0,
|
accumulation: runningAccumulation,
|
||||||
uv_index,
|
uv_index,
|
||||||
uv_dose: 0,
|
uv_dose: 0,
|
||||||
solar_wm2: 0,
|
solar_wm2: 0,
|
||||||
@@ -229,7 +231,6 @@ function summarise(slots) {
|
|||||||
return {
|
return {
|
||||||
time: slots[0].time,
|
time: slots[0].time,
|
||||||
label: dominant.label,
|
label: dominant.label,
|
||||||
code: dominant.code ?? null,
|
|
||||||
icon: dominant.icon,
|
icon: dominant.icon,
|
||||||
temperature: peak(fin('temperature')),
|
temperature: peak(fin('temperature')),
|
||||||
temperature_max: peak(finAll('temperature')),
|
temperature_max: peak(finAll('temperature')),
|
||||||
@@ -250,7 +251,7 @@ function summarise(slots) {
|
|||||||
raining: peak(fin('precipitation_chance')) > 60 ? 'True' : 'False',
|
raining: peak(fin('precipitation_chance')) > 60 ? 'True' : 'False',
|
||||||
precipitation: avg(fin('precipitation')),
|
precipitation: avg(fin('precipitation')),
|
||||||
precipitation_chance: Math.round(peak(fin('precipitation_chance'))),
|
precipitation_chance: Math.round(peak(fin('precipitation_chance'))),
|
||||||
accumulation: 0,
|
accumulation: finAll('precipitation').reduce((a, b) => a + b, 0),
|
||||||
uv_index: peak(fin('uv_index')),
|
uv_index: peak(fin('uv_index')),
|
||||||
uv_dose: finAll('uv_dose').reduce((a, b) => a + b, 0),
|
uv_dose: finAll('uv_dose').reduce((a, b) => a + b, 0),
|
||||||
solar_wm2: avg(fin('solar_wm2')),
|
solar_wm2: avg(fin('solar_wm2')),
|
||||||
@@ -287,20 +288,20 @@ export async function getForecast() {
|
|||||||
|
|
||||||
export function getWeatherCondition(data) {
|
export function getWeatherCondition(data) {
|
||||||
if(data.lightning_rate > 0 && data.raining === 'True') {
|
if(data.lightning_rate > 0 && data.raining === 'True') {
|
||||||
return {label: 'Thunderstorm', icon: '11d', code: 211};
|
return {label: 'Thunderstorm', icon: '11d'};
|
||||||
}
|
}
|
||||||
if(data.raining === 'True') {
|
if(data.raining === 'True') {
|
||||||
if(data.precipitation > 7.6) return {label: 'Heavy Rain', icon: '10d', code: 502};
|
if(data.precipitation > 7.6) return {label: 'Heavy Rain', icon: '10d'};
|
||||||
if(data.precipitation > 2.5) return {label: 'Moderate Rain', icon: '10d', code: 501};
|
if(data.precipitation > 2.5) return {label: 'Moderate Rain', icon: '10d'};
|
||||||
return {label: 'Light Rain', icon: '09d', code: 500};
|
return {label: 'Light Rain', icon: '09d'};
|
||||||
}
|
}
|
||||||
if(data.visibility < 10) return {label: 'Mist', icon: '50d', code: 701};
|
if(data.visibility < 10) return {label: 'Mist', icon: '50d'};
|
||||||
|
|
||||||
const dayNight = data.daytime ? 'd' : 'n';
|
const dayNight = data.daytime ? 'd' : 'n';
|
||||||
const c = data.clouds > 1 ? data.clouds / 100 : data.clouds;
|
const c = data.clouds > 1 ? data.clouds / 100 : data.clouds;
|
||||||
if(c > 0.85) return {label: 'Overcast Clouds', icon: `04${dayNight}`, code: 804};
|
if(c > 0.85) return {label: 'Overcast Clouds', icon: `04${dayNight}`};
|
||||||
if(c > 0.50) return {label: 'Broken Clouds', icon: `04${dayNight}`, code: 803};
|
if(c > 0.50) return {label: 'Broken Clouds', icon: `04${dayNight}`};
|
||||||
if(c > 0.25) return {label: 'Scattered Clouds', icon: `03${dayNight}`, code: 802};
|
if(c > 0.25) return {label: 'Scattered Clouds', icon: `03${dayNight}`};
|
||||||
if(c > 0.10) return {label: 'Few Clouds', icon: `02${dayNight}`, code: 801};
|
if(c > 0.10) return {label: 'Few Clouds', icon: `02${dayNight}`};
|
||||||
return {label: 'Clear Sky', icon: `01${dayNight}`, code: 800};
|
return {label: 'Clear Sky', icon: `01${dayNight}`};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,9 +44,10 @@ export async function queryHourly(start, end) {
|
|||||||
const results = await query(c, `avg_over_time({__name__!=""}[1h])`, s, e);
|
const results = await query(c, `avg_over_time({__name__!=""}[1h])`, s, e);
|
||||||
results.forEach(r => {
|
results.forEach(r => {
|
||||||
r.values.forEach(([ts, val]) => {
|
r.values.forEach(([ts, val]) => {
|
||||||
|
if(r.metric.__name__.startsWith('system_')) return;
|
||||||
const time = new Date(ts * 1000).toISOString().slice(0, 13) + ':00';
|
const time = new Date(ts * 1000).toISOString().slice(0, 13) + ':00';
|
||||||
if (!byTime[time]) byTime[time] = {time};
|
if (!byTime[time]) byTime[time] = {time};
|
||||||
byTime[time][r.metric.__name__] = parseFloat(val);
|
byTime[time][r.metric.__name__.replace('weather_', '')] = parseFloat(val);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return Object.values(byTime).sort((a, b) => a.time.localeCompare(b.time));
|
return Object.values(byTime).sort((a, b) => a.time.localeCompare(b.time));
|
||||||
@@ -68,7 +69,7 @@ export async function queryDaily(start, end) {
|
|||||||
r.values.forEach(([ts, val]) => {
|
r.values.forEach(([ts, val]) => {
|
||||||
const time = new Date(ts * 1000).toISOString().slice(0, 10);
|
const time = new Date(ts * 1000).toISOString().slice(0, 10);
|
||||||
if (!byTime[time]) byTime[time] = {time};
|
if (!byTime[time]) byTime[time] = {time};
|
||||||
byTime[time][r.metric.__name__] = parseFloat(val);
|
byTime[time][r.metric.__name__.replace('weather_', '')] = parseFloat(val);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user