From 400a577b7ed44cc29eced6318f6c2f91cc2351c6 Mon Sep 17 00:00:00 2001 From: Kimmo Brunfeldt <1232405+kimmobrunfeldt@users.noreply.github.com> Date: Sat, 10 Dec 2022 11:05:45 +0200 Subject: [PATCH] Don't include forecast data points for today summary if observation exists --- render/src/weather/weather.test.ts | 30 ++++++++++++++++++++++++++---- render/src/weather/weather.ts | 16 +++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/render/src/weather/weather.test.ts b/render/src/weather/weather.test.ts index a7c3c96..6b764b7 100644 --- a/render/src/weather/weather.test.ts +++ b/render/src/weather/weather.test.ts @@ -79,7 +79,29 @@ describe('calculateTodaySummary', () => { // Correct datapoints - // Just at the start of day + // Just at the start of day (same time as observation so this should be ignored) + { + type: 'harmonie', + Temperature: -10000, + Humidity: -10000, + WindSpeedMS: -10000, + WindGust: -10000, + WindDirection: -10000, + Pressure: -10000, + Visibility: -10000, + PrecipitationAmount: -10000, + Precipitation1h: -10000, + DewPoint: -10000, + WeatherSymbol3: -10000, + time: mockTodayDates.startOfLocalDayInUtc, + // Location doesn't matter + location: { + lat: 0, + lon: 0, + }, + }, + + // Beginning of the day { type: 'harmonie', Temperature: 9, @@ -93,7 +115,7 @@ describe('calculateTodaySummary', () => { Precipitation1h: 10, DewPoint: 8.5, WeatherSymbol3: 1, - time: mockTodayDates.startOfLocalDayInUtc, + time: dateFns.addHours(mockTodayDates.startOfLocalDayInUtc, 8), // Location doesn't matter location: { lat: 0, @@ -115,7 +137,7 @@ describe('calculateTodaySummary', () => { Precipitation1h: 0, DewPoint: 8.5, WeatherSymbol3: 31, - time: mockTodayDates.startOfLocalDayInUtc, + time: dateFns.addHours(mockTodayDates.startOfLocalDayInUtc, 12), // Location doesn't matter location: { lat: 0, @@ -178,7 +200,7 @@ describe('calculateTodaySummary', () => { maxTemperature: 11, }, forecast: { - avgTemperature: 10, // Average of 9, 11, and 10 + avgTemperature: 10, // Average of 9, 11, and 10 (first forecast point is not taken into account because observation exists at the same time) minTemperature: 9, maxTemperature: 11, avgWindSpeedMs: 5, // avg of 4, 6, and 5 diff --git a/render/src/weather/weather.ts b/render/src/weather/weather.ts index 2a86e1f..4ad4151 100644 --- a/render/src/weather/weather.ts +++ b/render/src/weather/weather.ts @@ -324,12 +324,22 @@ export function calculateTodaySummaryFromFmiData( startForecastAtHour, timezone ) - const fmiForecastDataToday = fmiData.filter((d) => - isBetweenInclusive(d.time, startOfLocalDayInUtc, endOfLocalDayInUtc) - ) const fmiObservationsDataToday = fmiObservationData.filter((d) => isBetweenInclusive(d.time, startOfLocalDayInUtc, endOfLocalDayInUtc) ) + const fmiForecastDataToday = fmiData.filter((d) => { + const isBetween = isBetweenInclusive( + d.time, + startOfLocalDayInUtc, + endOfLocalDayInUtc + ) + const isInObservations = + fmiObservationsDataToday.findIndex((obs) => + dateFns.isEqual(obs.time, d.time) + ) !== -1 + return isBetween && !isInObservations + }) + const combined = [...fmiForecastDataToday, ...fmiObservationsDataToday] const avgWindSpeedMs = _.mean(fmiForecastDataToday.map((d) => d.WindSpeedMS))