Suppress superseded alerts in the /alerts/active endpoint
All checks were successful
ci/woodpecker/manual/build-image Pipeline was successful

This commit is contained in:
2026-06-17 06:48:14 -05:00
parent 2a33fe01cf
commit 5a1134b955
4 changed files with 102 additions and 3 deletions

View File

@@ -10,6 +10,8 @@ import (
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
)
const nwsAlertURLPrefix = "https://api.weather.gov/alerts/"
// Repository defines outbound data access used by weatherapi use cases.
type Repository interface {
LatestObservation(ctx context.Context) (*model.WeatherObservation, error)
@@ -77,9 +79,10 @@ func (s *Service) LatestActiveAlertRun(ctx context.Context, activeAt time.Time)
}
out := cloneAlertRun(run)
supersededIDs := collectSupersededAlertIDs(out.Alerts)
alerts := out.Alerts[:0]
for _, alert := range out.Alerts {
if isActiveAlert(alert, activeAt) {
if isActiveAlert(alert, activeAt) && !isSupersededAlert(alert, supersededIDs) {
alerts = append(alerts, alert)
}
}
@@ -181,6 +184,41 @@ func isActiveAlert(alert model.WeatherAlert, activeAt time.Time) bool {
return true
}
func collectSupersededAlertIDs(alerts []model.WeatherAlert) map[string]struct{} {
supersededIDs := make(map[string]struct{})
for _, alert := range alerts {
for _, ref := range alert.References {
id := normalizeAlertID(referenceAlertID(ref))
if id != "" {
supersededIDs[id] = struct{}{}
}
}
}
return supersededIDs
}
func isSupersededAlert(alert model.WeatherAlert, supersededIDs map[string]struct{}) bool {
id := normalizeAlertID(alert.ID)
if id == "" {
return false
}
_, ok := supersededIDs[id]
return ok
}
func referenceAlertID(ref model.AlertReference) string {
if strings.TrimSpace(ref.Identifier) != "" {
return ref.Identifier
}
return ref.ID
}
func normalizeAlertID(value string) string {
value = strings.TrimSpace(value)
value = strings.TrimPrefix(value, nwsAlertURLPrefix)
return value
}
func cloneOutlookRun(run *model.WeatherOutlookRun) *model.WeatherOutlookRun {
out := *run
out.Latitude = copyFloat64(run.Latitude)