diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-06-24 22:04:05 -0400 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-06-24 22:04:05 -0400 |
commit | 235a9d1325a666305ba5a7087955992a13c5ea7d (patch) | |
tree | 31abfb0bcd72d288c1c7e882fcc8ffaf09e0303a | |
parent | 36f7c002d65398c3d94c8804c0616623def87f8d (diff) |
nws: update `GetForecastHourly`
Add handling to error out when weather data returned by NWS is stale.
-rw-r--r-- | nws/nws.go | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -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 } |