Add Daily briefing JSON generation
This commit is contained in:
@@ -4,9 +4,11 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/adapters/weatherapi"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/briefing"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/config"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/forecast"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/report"
|
||||
@@ -49,9 +51,28 @@ type FetchBundleRequest struct {
|
||||
OutputPath string
|
||||
}
|
||||
|
||||
type DailyBriefingRequest struct {
|
||||
Config config.Config
|
||||
Resolved report.Resolved
|
||||
OutputPath string
|
||||
}
|
||||
|
||||
type DailyBriefingResult struct {
|
||||
Package briefing.Package
|
||||
OutputPath string
|
||||
}
|
||||
|
||||
func Generate(ctx context.Context, req GenerateRequest) error {
|
||||
_ = ctx
|
||||
if _, err := ResolveGenerate(req, time.Now()); err != nil {
|
||||
resolved, err := ResolveGenerate(req, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resolved.Definition.ID == report.DailyToday {
|
||||
_, err := GenerateDailyBriefing(ctx, DailyBriefingRequest{
|
||||
Config: req.Config,
|
||||
Resolved: resolved,
|
||||
OutputPath: req.OutputPath,
|
||||
})
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("generate is not implemented")
|
||||
@@ -151,3 +172,53 @@ func FetchAndSaveBundle(ctx context.Context, req FetchBundleRequest) (*forecast.
|
||||
}
|
||||
return bundle, nil
|
||||
}
|
||||
|
||||
func GenerateDailyBriefing(ctx context.Context, req DailyBriefingRequest) (*DailyBriefingResult, error) {
|
||||
bundle, err := FetchBundle(ctx, FetchBundleRequest{Config: req.Config})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pkg, err := BuildDailyBriefing(req, bundle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
outputPath := req.OutputPath
|
||||
if outputPath == "" {
|
||||
outputPath = defaultBriefingPath(req.Config, req.Resolved)
|
||||
}
|
||||
if err := briefing.Save(outputPath, pkg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DailyBriefingResult{Package: pkg, OutputPath: outputPath}, nil
|
||||
}
|
||||
|
||||
func BuildDailyBriefing(req DailyBriefingRequest, bundle *forecast.Bundle) (briefing.Package, error) {
|
||||
location, err := timeutil.LoadLocation(req.Config.WeatherAPI.Timezone)
|
||||
if err != nil {
|
||||
return briefing.Package{}, err
|
||||
}
|
||||
dayparts := make([]forecast.DaypartDefinition, 0, len(req.Config.Dayparts))
|
||||
for _, daypart := range req.Config.Dayparts {
|
||||
dayparts = append(dayparts, forecast.DaypartDefinition{
|
||||
Name: daypart.Name,
|
||||
Start: daypart.Start,
|
||||
End: daypart.End,
|
||||
})
|
||||
}
|
||||
summary, err := forecast.BuildDailySummary(bundle, req.Resolved.ValidPeriod.Start, location, dayparts)
|
||||
if err != nil {
|
||||
return briefing.Package{}, err
|
||||
}
|
||||
return briefing.BuildDaily(briefing.BuildContext{
|
||||
Resolved: req.Resolved,
|
||||
Bundle: bundle,
|
||||
Units: req.Config.WeatherAPI.Units,
|
||||
Timezone: req.Config.WeatherAPI.Timezone,
|
||||
}, summary)
|
||||
}
|
||||
|
||||
func defaultBriefingPath(cfg config.Config, resolved report.Resolved) string {
|
||||
validDate := resolved.ValidPeriod.Start.Format("2006-01-02")
|
||||
filename := validDate + "." + string(resolved.Definition.ID) + ".briefing.json"
|
||||
return filepath.Join(cfg.Workspace.Root, cfg.Workspace.SnapshotsDir, "daily", validDate, filename)
|
||||
}
|
||||
|
||||
@@ -65,6 +65,71 @@ func TestFetchAndSaveBundleRequiresOutputPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateDailyBriefingWritesArtifact(t *testing.T) {
|
||||
server := dailyBundleServer(t)
|
||||
cfg := config.Defaults()
|
||||
cfg.WeatherAPI.BaseURL = server.URL + "/"
|
||||
cfg.WeatherAPI.Timezone = "America/Chicago"
|
||||
resolved, err := ResolveGenerate(GenerateRequest{
|
||||
Config: cfg,
|
||||
Report: ReportDaily,
|
||||
Date: mustParse("2026-05-29T12:00:00-05:00"),
|
||||
}, mustParse("2026-05-29T05:00:00-05:00"))
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveGenerate() error = %v", err)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "daily.briefing.json")
|
||||
|
||||
result, err := GenerateDailyBriefing(context.Background(), DailyBriefingRequest{
|
||||
Config: cfg,
|
||||
Resolved: resolved,
|
||||
OutputPath: path,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateDailyBriefing() error = %v", err)
|
||||
}
|
||||
if result.OutputPath != path {
|
||||
t.Fatalf("OutputPath = %q, want %q", result.OutputPath, path)
|
||||
}
|
||||
if result.Package.Daily == nil {
|
||||
t.Fatal("Daily = nil")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read briefing artifact: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), `"schemaVersion"`) || !strings.Contains(string(data), `"daily"`) {
|
||||
t.Fatalf("briefing artifact missing expected fields:\n%s", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateDailyBriefingDefaultPath(t *testing.T) {
|
||||
server := dailyBundleServer(t)
|
||||
cfg := config.Defaults()
|
||||
cfg.WeatherAPI.BaseURL = server.URL + "/"
|
||||
cfg.WeatherAPI.Timezone = "America/Chicago"
|
||||
cfg.Workspace.Root = t.TempDir()
|
||||
resolved, err := ResolveGenerate(GenerateRequest{
|
||||
Config: cfg,
|
||||
Report: ReportDaily,
|
||||
Date: mustParse("2026-05-29T12:00:00-05:00"),
|
||||
}, mustParse("2026-05-29T05:00:00-05:00"))
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveGenerate() error = %v", err)
|
||||
}
|
||||
|
||||
result, err := GenerateDailyBriefing(context.Background(), DailyBriefingRequest{
|
||||
Config: cfg,
|
||||
Resolved: resolved,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateDailyBriefing() error = %v", err)
|
||||
}
|
||||
if !strings.HasSuffix(result.OutputPath, filepath.Join("snapshots", "daily", "2026-05-29", "2026-05-29.daily_today.briefing.json")) {
|
||||
t.Fatalf("OutputPath = %q, want deterministic daily briefing path", result.OutputPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGenerateMapsCommandToReportDefinition(t *testing.T) {
|
||||
cfg := config.Defaults()
|
||||
cfg.WeatherAPI.Timezone = "America/Chicago"
|
||||
@@ -88,6 +153,30 @@ func TestResolveGenerateMapsCommandToReportDefinition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func dailyBundleServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/observations":
|
||||
_, _ = w.Write([]byte(`{"data":{"timestamp":"2026-05-29T14:00:00Z","conditionCode":3}}`))
|
||||
case "/conditions/current":
|
||||
_, _ = w.Write([]byte(`{"data":{"conditionText":"Clear"}}`))
|
||||
case "/forecast/hourly":
|
||||
_, _ = w.Write([]byte(`{"data":{"locationId":"test-grid","locationName":"Testville","issuedAt":"2026-05-29T10:30:00-05:00","product":"hourly","periods":[{"startTime":"2026-05-29T06:00:00-05:00","endTime":"2026-05-29T07:00:00-05:00","textDescription":"Showers and thunderstorms","temperatureF":66,"probabilityOfPrecipitationPercent":80,"windGustMph":32}]}}`))
|
||||
case "/forecast/narrative":
|
||||
_, _ = w.Write([]byte(`{"data":{"issuedAt":"2026-05-29T10:30:00-05:00","product":"narrative","periods":[{"startTime":"2026-05-29T06:00:00-05:00","endTime":"2026-05-29T18:00:00-05:00","textDescription":"Morning storms, then partly sunny."}]}}`))
|
||||
case "/alerts/active":
|
||||
_, _ = w.Write([]byte(`{"data":{"alerts":[{"event":"Flood Watch","effective":"2026-05-29T05:00:00-05:00","expires":"2026-05-29T09:00:00-05:00"}]}}`))
|
||||
case "/discussion":
|
||||
_, _ = w.Write([]byte(`{"data":{"product":"discussion","issuedAt":"2026-05-29T09:25:00-05:00","keyMessages":["Storms are most likely during the morning."]}}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
return server
|
||||
}
|
||||
|
||||
func TestResolveGenerateStorm(t *testing.T) {
|
||||
cfg := config.Defaults()
|
||||
cfg.WeatherAPI.Timezone = "America/Chicago"
|
||||
|
||||
Reference in New Issue
Block a user