Filter active alerts in app service
This commit is contained in:
@@ -68,6 +68,26 @@ func (s *Service) LatestAlertRun(ctx context.Context) (*model.WeatherAlertRun, e
|
||||
return s.repo.LatestAlertRun(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) LatestActiveAlertRun(ctx context.Context, activeAt time.Time) (*model.WeatherAlertRun, error) {
|
||||
run, err := s.repo.LatestAlertRun(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if run == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
out := cloneAlertRun(run)
|
||||
alerts := out.Alerts[:0]
|
||||
for _, alert := range out.Alerts {
|
||||
if isActiveAlert(alert, activeAt) {
|
||||
alerts = append(alerts, alert)
|
||||
}
|
||||
}
|
||||
out.Alerts = alerts
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Service) LatestConvectiveOutlook(ctx context.Context, filter OutlookFilter) (*model.WeatherOutlookRun, error) {
|
||||
run, err := s.repo.LatestConvectiveOutlookRun(ctx)
|
||||
if err != nil {
|
||||
@@ -113,6 +133,53 @@ func normalizeOutlookType(value string) string {
|
||||
return strings.ToLower(strings.TrimSpace(value))
|
||||
}
|
||||
|
||||
func cloneAlertRun(run *model.WeatherAlertRun) *model.WeatherAlertRun {
|
||||
out := *run
|
||||
out.Latitude = copyFloat64(run.Latitude)
|
||||
out.Longitude = copyFloat64(run.Longitude)
|
||||
if run.Alerts != nil {
|
||||
out.Alerts = make([]model.WeatherAlert, len(run.Alerts))
|
||||
for i := range run.Alerts {
|
||||
out.Alerts[i] = cloneAlert(run.Alerts[i])
|
||||
}
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
func cloneAlert(alert model.WeatherAlert) model.WeatherAlert {
|
||||
out := alert
|
||||
out.Sent = copyTime(alert.Sent)
|
||||
out.Effective = copyTime(alert.Effective)
|
||||
out.Onset = copyTime(alert.Onset)
|
||||
out.Expires = copyTime(alert.Expires)
|
||||
if alert.References != nil {
|
||||
out.References = make([]model.AlertReference, len(alert.References))
|
||||
for i := range alert.References {
|
||||
out.References[i] = cloneAlertReference(alert.References[i])
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneAlertReference(ref model.AlertReference) model.AlertReference {
|
||||
out := ref
|
||||
out.Sent = copyTime(ref.Sent)
|
||||
return out
|
||||
}
|
||||
|
||||
func isActiveAlert(alert model.WeatherAlert, activeAt time.Time) bool {
|
||||
if strings.EqualFold(strings.TrimSpace(alert.MessageType), "Cancel") {
|
||||
return false
|
||||
}
|
||||
if alert.Effective != nil && activeAt.Before(*alert.Effective) {
|
||||
return false
|
||||
}
|
||||
if alert.Expires != nil && !activeAt.Before(*alert.Expires) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func cloneOutlookRun(run *model.WeatherOutlookRun) *model.WeatherOutlookRun {
|
||||
out := *run
|
||||
out.Latitude = copyFloat64(run.Latitude)
|
||||
|
||||
Reference in New Issue
Block a user