summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-06-24 22:04:05 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-06-24 22:04:05 -0400
commit235a9d1325a666305ba5a7087955992a13c5ea7d (patch)
tree31abfb0bcd72d288c1c7e882fcc8ffaf09e0303a
parent36f7c002d65398c3d94c8804c0616623def87f8d (diff)
nws: update `GetForecastHourly`
Add handling to error out when weather data returned by NWS is stale.
-rw-r--r--nws/nws.go17
1 files changed, 16 insertions, 1 deletions
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
}