2026-01-14 11:18:21 -06:00
|
|
|
|
// FILE: ./internal/sources/nws/observation.go
|
2026-01-13 18:14:21 -06:00
|
|
|
|
package nws
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
|
|
|
|
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
2026-01-15 08:58:56 -06:00
|
|
|
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/common"
|
2026-01-13 18:14:21 -06:00
|
|
|
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// ObservationSource polls an NWS station observation endpoint and emits a RAW observation Event.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Key refactor:
|
|
|
|
|
|
// - Source responsibility: fetch bytes + emit a valid event envelope.
|
|
|
|
|
|
// - Normalizer responsibility: interpret raw JSON + map to canonical domain model.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
//
|
|
|
|
|
|
// This corresponds to URLs like:
|
|
|
|
|
|
//
|
|
|
|
|
|
// https://api.weather.gov/stations/KSTL/observations/latest
|
|
|
|
|
|
type ObservationSource struct {
|
|
|
|
|
|
name string
|
|
|
|
|
|
url string
|
|
|
|
|
|
userAgent string
|
|
|
|
|
|
client *http.Client
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
|
|
|
|
|
|
if strings.TrimSpace(cfg.Name) == "" {
|
|
|
|
|
|
return nil, fmt.Errorf("nws_observation: name is required")
|
|
|
|
|
|
}
|
|
|
|
|
|
if cfg.Params == nil {
|
|
|
|
|
|
return nil, fmt.Errorf("nws_observation %q: params are required (need params.url and params.user_agent)", cfg.Name)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
url, ok := cfg.ParamString("url", "URL")
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil, fmt.Errorf("nws_observation %q: params.url is required", cfg.Name)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil, fmt.Errorf("nws_observation %q: params.user_agent is required", cfg.Name)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &ObservationSource{
|
|
|
|
|
|
name: cfg.Name,
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
userAgent: ua,
|
2026-01-14 11:18:21 -06:00
|
|
|
|
client: &http.Client{
|
|
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
|
|
},
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *ObservationSource) Name() string { return s.name }
|
|
|
|
|
|
|
|
|
|
|
|
// Kind is used for routing/policy.
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// We keep Kind canonical (observation) even for raw events; Schema differentiates raw vs canonical.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Poll fetches NWS "latest observation" and emits exactly one RAW Event.
|
|
|
|
|
|
// The RAW payload is json.RawMessage and Schema is standards.SchemaRawNWSObservationV1.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
2026-01-14 11:18:21 -06:00
|
|
|
|
raw, meta, err := s.fetchRaw(ctx)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Event.ID must be set BEFORE normalization (feedkit requires it).
|
|
|
|
|
|
// Prefer NWS-provided "id" (stable URL). Fallback to a stable-ish computed key.
|
|
|
|
|
|
eventID := strings.TrimSpace(meta.ID)
|
|
|
|
|
|
if eventID == "" {
|
|
|
|
|
|
ts := meta.ParsedTimestamp
|
|
|
|
|
|
if ts.IsZero() {
|
|
|
|
|
|
ts = time.Now().UTC()
|
|
|
|
|
|
}
|
|
|
|
|
|
station := strings.TrimSpace(meta.StationID)
|
|
|
|
|
|
if station == "" {
|
|
|
|
|
|
station = "UNKNOWN"
|
|
|
|
|
|
}
|
|
|
|
|
|
eventID = fmt.Sprintf("nws:observation:%s:%s:%s", s.name, station, ts.UTC().Format(time.RFC3339Nano))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// EffectiveAt is optional; for observations it’s naturally the observation timestamp.
|
2026-01-13 18:14:21 -06:00
|
|
|
|
var effectiveAt *time.Time
|
2026-01-14 11:18:21 -06:00
|
|
|
|
if !meta.ParsedTimestamp.IsZero() {
|
|
|
|
|
|
t := meta.ParsedTimestamp.UTC()
|
2026-01-13 18:14:21 -06:00
|
|
|
|
effectiveAt = &t
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e := event.Event{
|
|
|
|
|
|
ID: eventID,
|
|
|
|
|
|
Kind: s.Kind(),
|
|
|
|
|
|
Source: s.name,
|
|
|
|
|
|
EmittedAt: time.Now().UTC(),
|
|
|
|
|
|
EffectiveAt: effectiveAt,
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// RAW schema (normalizer matches on this).
|
|
|
|
|
|
Schema: standards.SchemaRawNWSObservationV1,
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Raw JSON; normalizer will decode and map to canonical model.WeatherObservation.
|
|
|
|
|
|
Payload: raw,
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := e.Validate(); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return []event.Event{e}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// ---- RAW fetch + minimal metadata decode ----
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// observationMeta is a *minimal* decode of the NWS payload used only to build
|
|
|
|
|
|
// a stable Event.ID and a useful EffectiveAt for the envelope.
|
|
|
|
|
|
type observationMeta struct {
|
|
|
|
|
|
ID string `json:"id"`
|
2026-01-13 18:14:21 -06:00
|
|
|
|
Properties struct {
|
2026-01-14 11:18:21 -06:00
|
|
|
|
StationID string `json:"stationId"`
|
|
|
|
|
|
Timestamp string `json:"timestamp"`
|
2026-01-13 18:14:21 -06:00
|
|
|
|
} `json:"properties"`
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
// Convenience fields populated after decode.
|
|
|
|
|
|
ParsedTimestamp time.Time `json:"-"`
|
|
|
|
|
|
StationID string `json:"-"`
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
func (s *ObservationSource) fetchRaw(ctx context.Context) (json.RawMessage, observationMeta, error) {
|
2026-01-15 08:58:56 -06:00
|
|
|
|
b, err := common.FetchBody(ctx, s.client, s.url, s.userAgent, "application/geo+json, application/json")
|
2026-01-13 18:14:21 -06:00
|
|
|
|
if err != nil {
|
2026-01-15 08:58:56 -06:00
|
|
|
|
return nil, observationMeta{}, fmt.Errorf("nws_observation %q: %w", s.name, err)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
raw := json.RawMessage(b)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
var meta observationMeta
|
|
|
|
|
|
if err := json.Unmarshal(b, &meta); err != nil {
|
|
|
|
|
|
// If metadata decode fails, still return raw; envelope will fall back to computed ID.
|
|
|
|
|
|
return raw, observationMeta{}, nil
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
meta.StationID = strings.TrimSpace(meta.Properties.StationID)
|
2026-01-13 18:14:21 -06:00
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
tsStr := strings.TrimSpace(meta.Properties.Timestamp)
|
|
|
|
|
|
if tsStr != "" {
|
|
|
|
|
|
if t, err := time.Parse(time.RFC3339, tsStr); err == nil {
|
|
|
|
|
|
meta.ParsedTimestamp = t
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 11:18:21 -06:00
|
|
|
|
return raw, meta, nil
|
2026-01-13 18:14:21 -06:00
|
|
|
|
}
|