- Adopt an opinionated Event.ID policy across sources: - use upstream-provided ID when available - otherwise derive a stable ID from Source:EffectiveAt (RFC3339Nano, UTC) - fall back to Source:EmittedAt when EffectiveAt is unavailable - Add common/id helper to centralize ID selection logic and keep sources consistent - Simplify common event construction by collapsing SingleRawEventAt/SingleRawEvent into a single explicit SingleRawEvent helper (emittedAt passed in) - Update NWS/Open-Meteo/OpenWeather observation sources to: - compute EffectiveAt first - generate IDs via the shared helper - build envelopes via the unified SingleRawEvent helper - Improve determinism and dedupe-friendliness without changing schemas or payloads
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
// FILE: ./internal/sources/openweather/observation.go
|
|
package openweather
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/common"
|
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/standards"
|
|
)
|
|
|
|
type ObservationSource struct {
|
|
http *common.HTTPSource
|
|
}
|
|
|
|
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
|
|
const driver = "openweather_observation"
|
|
|
|
hs, err := common.NewHTTPSource(driver, cfg, "application/json")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := requireMetricUnits(hs.URL); err != nil {
|
|
return nil, fmt.Errorf("%s %q: %w", hs.Driver, hs.Name, err)
|
|
}
|
|
|
|
return &ObservationSource{http: hs}, nil
|
|
}
|
|
|
|
func (s *ObservationSource) Name() string { return s.http.Name }
|
|
|
|
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
|
|
|
|
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
|
if err := requireMetricUnits(s.http.URL); err != nil {
|
|
return nil, fmt.Errorf("%s %q: %w", s.http.Driver, s.http.Name, err)
|
|
}
|
|
|
|
raw, meta, err := s.fetchRaw(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var effectiveAt *time.Time
|
|
if !meta.ParsedTimestamp.IsZero() {
|
|
t := meta.ParsedTimestamp.UTC()
|
|
effectiveAt = &t
|
|
}
|
|
|
|
emittedAt := time.Now().UTC()
|
|
eventID := common.ChooseEventID("", s.http.Name, effectiveAt, emittedAt)
|
|
|
|
return common.SingleRawEvent(
|
|
s.Kind(),
|
|
s.http.Name,
|
|
standards.SchemaRawOpenWeatherCurrentV1,
|
|
eventID,
|
|
emittedAt,
|
|
effectiveAt,
|
|
raw,
|
|
)
|
|
}
|
|
|
|
// ---- RAW fetch + minimal metadata decode ----
|
|
|
|
type openWeatherMeta struct {
|
|
Dt int64 `json:"dt"` // unix seconds, UTC
|
|
|
|
ParsedTimestamp time.Time `json:"-"`
|
|
}
|
|
|
|
func (s *ObservationSource) fetchRaw(ctx context.Context) (json.RawMessage, openWeatherMeta, error) {
|
|
raw, err := s.http.FetchJSON(ctx)
|
|
if err != nil {
|
|
return nil, openWeatherMeta{}, err
|
|
}
|
|
|
|
var meta openWeatherMeta
|
|
if err := json.Unmarshal(raw, &meta); err != nil {
|
|
return raw, openWeatherMeta{}, nil
|
|
}
|
|
|
|
if meta.Dt > 0 {
|
|
meta.ParsedTimestamp = time.Unix(meta.Dt, 0).UTC()
|
|
}
|
|
|
|
return raw, meta, nil
|
|
}
|
|
|
|
func requireMetricUnits(rawURL string) error {
|
|
u, err := url.Parse(strings.TrimSpace(rawURL))
|
|
if err != nil {
|
|
return fmt.Errorf("invalid url %q: %w", rawURL, err)
|
|
}
|
|
|
|
units := strings.ToLower(strings.TrimSpace(u.Query().Get("units")))
|
|
if units != "metric" {
|
|
if units == "" {
|
|
units = "(missing; defaults to standard)"
|
|
}
|
|
return fmt.Errorf("url must include units=metric (got units=%s)", units)
|
|
}
|
|
|
|
return nil
|
|
}
|