From 235a9d1325a666305ba5a7087955992a13c5ea7d Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Fri, 24 Jun 2022 22:04:05 -0400 Subject: nws: update `GetForecastHourly` Add handling to error out when weather data returned by NWS is stale. --- nws/nws.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'nws') diff --git a/nws/nws.go b/nws/nws.go index 0a456d9..c5dfe19 100644 --- a/nws/nws.go +++ b/nws/nws.go @@ -53,7 +53,8 @@ type ForecastPeriod struct { } type ForecastProperties struct { - Periods []ForecastPeriod + GeneratedAt string + Periods []ForecastPeriod } type Forecast struct { @@ -262,6 +263,20 @@ func GetForecastHourly(point *Point) (*Forecast, error) { if len(forecast.Properties.Periods) == 0 { return nil, fmt.Errorf("forecast hourly: periods empty") } + + // Check for staleness. + genAt, tErr := time.Parse(time.RFC3339, forecast.Properties.GeneratedAt) + if tErr != nil { + return nil, fmt.Errorf("forecast hourly: unable to check staleness") + } + if time.Since(genAt).Seconds() > 86400 { + fhCache.Set(point.Properties.ForecastHourly, + []byte{}, time.Now()) // Invalidate cache. + return nil, fmt.Errorf( + "forecast hourly: stale data from weather.gov from %v", + forecast.Properties.GeneratedAt, + ) + } return forecast, nil } -- cgit v1.2.3