Add SPC Outlook summaries to the alert digest template
This commit is contained in:
@@ -8,8 +8,8 @@ import (
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/weatherdata"
|
||||
)
|
||||
|
||||
const defaultSPCConvectiveDiscussionMinimumSeverityRank = 3
|
||||
const spcCategoricalOutlookType = "categorical"
|
||||
const defaultSPCConvectiveDiscussionMinimumSeverityRank = defaultSPCRiskDigestMinimumSeverityRank
|
||||
const spcCategoricalOutlookType = defaultSPCRiskDigestOutlookType
|
||||
|
||||
type SPCConvectiveDiscussionModule struct {
|
||||
IncludedBecause string `json:"included_because"`
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package briefing
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/module"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/timeutil"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/weatherdata"
|
||||
)
|
||||
|
||||
const defaultSPCRiskDigestOutlookType = "categorical"
|
||||
const defaultSPCRiskDigestMinimumSeverityRank = 3
|
||||
|
||||
type SPCConvectiveOutlooksModule struct {
|
||||
Checked bool `json:"checked"`
|
||||
AsOf string `json:"as_of,omitempty"`
|
||||
@@ -16,6 +21,7 @@ type SPCConvectiveOutlooksModule struct {
|
||||
LocationName string `json:"location_name,omitempty"`
|
||||
OutlookCount int `json:"outlook_count"`
|
||||
Outlooks []SPCConvectiveOutlookRecord `json:"outlooks,omitempty"`
|
||||
RiskDigest []SPCConvectiveOutlookDigest `json:"risk_digest,omitempty"`
|
||||
}
|
||||
|
||||
type SPCConvectiveOutlookRecord struct {
|
||||
@@ -30,6 +36,13 @@ type SPCConvectiveOutlookRecord struct {
|
||||
ImageURL string `json:"image_url,omitempty"`
|
||||
}
|
||||
|
||||
type SPCConvectiveOutlookDigest struct {
|
||||
LabelText string `json:"label_text,omitempty"`
|
||||
RiskLabel string `json:"risk_label,omitempty"`
|
||||
PeriodBegins string `json:"period_begins,omitempty"`
|
||||
PeriodEnds string `json:"period_ends,omitempty"`
|
||||
}
|
||||
|
||||
func buildSPCConvectiveOutlooksModule(ctx ModuleContext, _ any) (*module.Output, error) {
|
||||
value := SPCConvectiveOutlooksModule{}
|
||||
run := ctx.Collected.SPCConvectiveOutlooks
|
||||
@@ -49,10 +62,23 @@ func buildSPCConvectiveOutlooksModule(ctx ModuleContext, _ any) (*module.Output,
|
||||
}
|
||||
|
||||
value.Outlooks = spcConvectiveOutlookRecords(ctx.Derived.SPCConvectiveOutlooks, ctx.Resolved.ValidPeriod, ctx.Timezone)
|
||||
value.RiskDigest = spcConvectiveOutlookRiskDigest(ctx.Derived.SPCConvectiveOutlooks, ctx.Resolved.ValidPeriod, ctx.Timezone, defaultSPCRiskDigestPolicy())
|
||||
value.OutlookCount = len(value.Outlooks)
|
||||
return &module.Output{ID: module.SPCConvectiveOutlooks, StanzaName: string(module.SPCConvectiveOutlooks), Value: value}, nil
|
||||
}
|
||||
|
||||
type spcRiskDigestPolicy struct {
|
||||
OutlookType string
|
||||
MinimumSeverityRank int
|
||||
}
|
||||
|
||||
func defaultSPCRiskDigestPolicy() spcRiskDigestPolicy {
|
||||
return spcRiskDigestPolicy{
|
||||
OutlookType: defaultSPCRiskDigestOutlookType,
|
||||
MinimumSeverityRank: defaultSPCRiskDigestMinimumSeverityRank,
|
||||
}
|
||||
}
|
||||
|
||||
func spcConvectiveOutlookRecords(outlooks []weatherdata.ConvectiveOutlook, reportPeriod timeutil.Period, timezone string) []SPCConvectiveOutlookRecord {
|
||||
records := make([]SPCConvectiveOutlookRecord, 0, len(outlooks))
|
||||
for _, outlook := range outlooks {
|
||||
@@ -75,6 +101,42 @@ func spcConvectiveOutlookRecords(outlooks []weatherdata.ConvectiveOutlook, repor
|
||||
return records
|
||||
}
|
||||
|
||||
func spcConvectiveOutlookRiskDigest(outlooks []weatherdata.ConvectiveOutlook, reportPeriod timeutil.Period, timezone string, policy spcRiskDigestPolicy) []SPCConvectiveOutlookDigest {
|
||||
records := make([]SPCConvectiveOutlookDigest, 0, len(outlooks))
|
||||
for _, outlook := range outlooks {
|
||||
if outlook.OutlookType != policy.OutlookType {
|
||||
continue
|
||||
}
|
||||
if outlook.SeverityRank == nil || *outlook.SeverityRank < policy.MinimumSeverityRank {
|
||||
continue
|
||||
}
|
||||
if !outlook.ContainsLocation {
|
||||
continue
|
||||
}
|
||||
outlookPeriod := timeutil.Period{Start: outlook.ValidFrom, End: outlook.ValidTo}
|
||||
if !outlookPeriod.IsValid() || !outlookPeriod.Overlaps(reportPeriod) {
|
||||
continue
|
||||
}
|
||||
records = append(records, SPCConvectiveOutlookDigest{
|
||||
LabelText: outlook.LabelText,
|
||||
RiskLabel: spcRiskDigestLabel(outlook.LabelText),
|
||||
PeriodBegins: friendlyPeriodBeginsLabel(outlookPeriod, timezone),
|
||||
PeriodEnds: friendlyPeriodEndsLabel(outlookPeriod, timezone),
|
||||
})
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func spcRiskDigestLabel(labelText string) string {
|
||||
label := strings.TrimSpace(labelText)
|
||||
if label == "" {
|
||||
return ""
|
||||
}
|
||||
runes := []rune(strings.ToLower(label))
|
||||
runes[0] = unicode.ToUpper(runes[0])
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func friendlyOptionalTime(value *time.Time, timezone string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
|
||||
@@ -71,12 +71,19 @@ func TestSPCConvectiveOutlooksModuleBuildsPromptSafeRiskProduct(t *testing.T) {
|
||||
if !got.ContainsLocation || got.ImageURL == "" {
|
||||
t.Fatalf("outlook = %#v, want location flag and image URL", got)
|
||||
}
|
||||
if len(value.RiskDigest) != 1 {
|
||||
t.Fatalf("RiskDigest length = %d, want 1", len(value.RiskDigest))
|
||||
}
|
||||
digest := value.RiskDigest[0]
|
||||
if digest.LabelText != "Slight Risk" || digest.RiskLabel != "Slight risk" || digest.PeriodBegins != "2026-05-29 at 11:00 AM" || digest.PeriodEnds != "2026-05-30 at 7:00 AM" {
|
||||
t.Fatalf("risk digest = %#v, want prompt-facing slight risk record", digest)
|
||||
}
|
||||
data, err := json.Marshal(output.Value)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
text := string(data)
|
||||
for _, field := range []string{"checked", "as_of", "issued_at", "location_id", "location_name", "outlook_count", "outlooks", "period_begins", "period_ends", "contains_location", "image_url"} {
|
||||
for _, field := range []string{"checked", "as_of", "issued_at", "location_id", "location_name", "outlook_count", "outlooks", "risk_digest", "period_begins", "period_ends", "contains_location", "image_url"} {
|
||||
if !strings.Contains(text, field) {
|
||||
t.Fatalf("json = %s, want field %s", text, field)
|
||||
}
|
||||
@@ -88,6 +95,71 @@ func TestSPCConvectiveOutlooksModuleBuildsPromptSafeRiskProduct(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSPCRiskDigestDefaultPolicyConstants(t *testing.T) {
|
||||
if defaultSPCRiskDigestOutlookType != "categorical" {
|
||||
t.Fatalf("defaultSPCRiskDigestOutlookType = %q, want categorical", defaultSPCRiskDigestOutlookType)
|
||||
}
|
||||
if defaultSPCRiskDigestMinimumSeverityRank != 3 {
|
||||
t.Fatalf("defaultSPCRiskDigestMinimumSeverityRank = %d, want 3", defaultSPCRiskDigestMinimumSeverityRank)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSPCConvectiveOutlooksRiskDigestFilters(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
outlook weatherdata.ConvectiveOutlook
|
||||
wantRisk bool
|
||||
}{
|
||||
{
|
||||
name: "categorical slight risk included",
|
||||
outlook: spcRiskDigestTestOutlook("categorical", "Slight Risk", 3, true,
|
||||
"2026-05-29T11:00:00-05:00", "2026-05-30T07:00:00-05:00"),
|
||||
wantRisk: true,
|
||||
},
|
||||
{
|
||||
name: "marginal risk excluded",
|
||||
outlook: spcRiskDigestTestOutlook("categorical", "Marginal Risk", 2, true,
|
||||
"2026-05-29T11:00:00-05:00", "2026-05-30T07:00:00-05:00"),
|
||||
},
|
||||
{
|
||||
name: "non categorical high rank excluded",
|
||||
outlook: spcRiskDigestTestOutlook("wind", "30% Wind Risk", 30, true,
|
||||
"2026-05-29T11:00:00-05:00", "2026-05-30T07:00:00-05:00"),
|
||||
},
|
||||
{
|
||||
name: "non overlapping excluded",
|
||||
outlook: spcRiskDigestTestOutlook("categorical", "Enhanced Risk", 4, true,
|
||||
"2026-05-30T07:00:00-05:00", "2026-05-31T07:00:00-05:00"),
|
||||
},
|
||||
{
|
||||
name: "location miss excluded",
|
||||
outlook: spcRiskDigestTestOutlook("categorical", "Moderate Risk", 5, false,
|
||||
"2026-05-29T11:00:00-05:00", "2026-05-30T07:00:00-05:00"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
registry := MustDefaultModuleRegistry()
|
||||
ctx := testModuleContext()
|
||||
ctx.Collected.SPCConvectiveOutlooks = &weatherdata.ConvectiveOutlookRun{
|
||||
Outlooks: []weatherdata.ConvectiveOutlook{tt.outlook},
|
||||
}
|
||||
ctx.Derived.SPCConvectiveOutlooks = []weatherdata.ConvectiveOutlook{tt.outlook}
|
||||
|
||||
output, err := registry.BuildModule(ctx, module.ConfigItem{ID: module.SPCConvectiveOutlooks})
|
||||
if err != nil {
|
||||
t.Fatalf("BuildModule() error = %v", err)
|
||||
}
|
||||
value := moduleValue[SPCConvectiveOutlooksModule](t, output)
|
||||
gotRisk := len(value.RiskDigest) > 0
|
||||
if gotRisk != tt.wantRisk {
|
||||
t.Fatalf("RiskDigest = %#v, want included=%v", value.RiskDigest, tt.wantRisk)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSPCConvectiveOutlooksModuleSkipsNonOverlappingOutlooks(t *testing.T) {
|
||||
registry := MustDefaultModuleRegistry()
|
||||
ctx := testModuleContext()
|
||||
@@ -117,6 +189,9 @@ func TestSPCConvectiveOutlooksModuleSkipsNonOverlappingOutlooks(t *testing.T) {
|
||||
if value.OutlookCount != 0 || len(value.Outlooks) != 0 {
|
||||
t.Fatalf("value = %#v, want non-overlapping outlook omitted", value)
|
||||
}
|
||||
if len(value.RiskDigest) != 0 {
|
||||
t.Fatalf("RiskDigest = %#v, want non-overlapping outlook omitted", value.RiskDigest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSPCConvectiveOutlooksModuleBuildsCheckedEmptyStanza(t *testing.T) {
|
||||
@@ -167,3 +242,16 @@ func TestSPCConvectiveOutlooksModuleBuildsUncheckedStanzaForMissingSource(t *tes
|
||||
t.Fatalf("missing source value = %#v, want unchecked empty stanza", value)
|
||||
}
|
||||
}
|
||||
|
||||
func spcRiskDigestTestOutlook(outlookType string, labelText string, rank int, containsLocation bool, validFrom string, validTo string) weatherdata.ConvectiveOutlook {
|
||||
return weatherdata.ConvectiveOutlook{
|
||||
ID: labelText,
|
||||
Day: 1,
|
||||
OutlookType: outlookType,
|
||||
LabelText: labelText,
|
||||
SeverityRank: &rank,
|
||||
ValidFrom: mustParseModuleTime(validFrom),
|
||||
ValidTo: mustParseModuleTime(validTo),
|
||||
ContainsLocation: containsLocation,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user