Compare commits
23 Commits
v0.9.1
...
a990da957b
| Author | SHA1 | Date | |
|---|---|---|---|
| a990da957b | |||
| a4cd63ca4e | |||
| fba519cab0 | |||
| da8ff81692 | |||
| f91a185f9d | |||
| e966276c40 | |||
| 1e2db468ea | |||
| cefd4dfc7c | |||
| 42c646c328 | |||
| b2c429983c | |||
| 0f20d1e4cb | |||
| 979d754d18 | |||
| 002f9d0ba6 | |||
| ec115ba152 | |||
| 47176520bb | |||
| 9e27a431a1 | |||
| f0605781a2 | |||
| abb8f218ec | |||
| 4ac4e401ed | |||
| 77bd59cb87 | |||
| bb5abf798b | |||
| fd820fd964 | |||
| cca873cafb |
4
AGENTS.md
Normal file
4
AGENTS.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Please carefully review the documents in `docs/policy` before making any changes to this repository.
|
||||||
|
- `architecture.md` provides the canonical high-level architecture policy for this repository.
|
||||||
|
- `development.md` provides more granular development policy for this repository.
|
||||||
|
- `documentation.md` provides the canonical documentation policy for this repository.
|
||||||
339
API.md
339
API.md
@@ -1,339 +0,0 @@
|
|||||||
# weatherfeeder API (Wire Contract)
|
|
||||||
|
|
||||||
This document defines the stable, consumer-facing JSON contract emitted by weatherfeeder sinks.
|
|
||||||
|
|
||||||
weatherfeeder emits **events** encoded as JSON. Each event has:
|
|
||||||
- an **envelope** (metadata + schema identifier), and
|
|
||||||
- a **payload** whose shape is determined by `schema`.
|
|
||||||
|
|
||||||
Downstream consumers should:
|
|
||||||
1. parse the event envelope,
|
|
||||||
2. switch on `schema`, then
|
|
||||||
3. decode `payload` into the matching schema.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Event envelope
|
|
||||||
|
|
||||||
All events are JSON objects with these fields:
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `id` | string | yes | Stable event identifier. Treat as opaque. |
|
|
||||||
| `schema` | string | yes | Schema identifier (e.g. `weather.observation.v1`). |
|
|
||||||
| `source` | string | yes | Provider/source identifier (stable within configuration). |
|
|
||||||
| `effectiveAt` | string (timestamp) | yes | RFC3339Nano timestamp indicating when this event is effective. |
|
|
||||||
| `payload` | object | yes | Schema-specific payload (see below). |
|
|
||||||
|
|
||||||
### Timestamp format
|
|
||||||
|
|
||||||
All timestamps are encoded as JSON strings using Go’s `time.Time` JSON encoding (RFC3339Nano).
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
- `"2026-01-17T14:27:00Z"`
|
|
||||||
- `"2026-01-17T08:27:00-06:00"`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Canonical schemas
|
|
||||||
|
|
||||||
weatherfeeder emits four canonical domain schemas:
|
|
||||||
|
|
||||||
- `weather.observation.v1`
|
|
||||||
- `weather.forecast.v1`
|
|
||||||
- `weather.forecast_discussion.v1`
|
|
||||||
- `weather.alert.v1`
|
|
||||||
|
|
||||||
Each payload is described below using the JSON field names as the contract.
|
|
||||||
|
|
||||||
### Raw upstream schemas
|
|
||||||
|
|
||||||
weatherfeeder sources also emit provider-specific raw schemas before normalization.
|
|
||||||
For this feature, the raw source schema is:
|
|
||||||
|
|
||||||
- `raw.nws.forecast_discussion.v1`
|
|
||||||
- payload type: string
|
|
||||||
- payload contents: exact fetched HTML response body
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Shared Conventions
|
|
||||||
|
|
||||||
- Timestamps are JSON strings in RFC3339Nano format.
|
|
||||||
- Optional fields are omitted when unknown (`omitempty` behavior).
|
|
||||||
- Numeric measurements are normalized to metric units:
|
|
||||||
- `*C` = Celsius
|
|
||||||
- `*Kmh` = kilometers/hour
|
|
||||||
- `*Pa` = Pascals
|
|
||||||
- `*Meters` = meters
|
|
||||||
- `*Mm` = millimeters
|
|
||||||
- `*Percent` = percent (0-100)
|
|
||||||
- `conditionCode` is a WMO weather interpretation code (`int`).
|
|
||||||
- Unknown/unmappable is `-1`.
|
|
||||||
- Downstream consumers should treat unknown codes as “unknown conditions” rather than failing decoding.
|
|
||||||
- For readability and stability, weatherfeeder rounds floating-point values in canonical payloads to
|
|
||||||
**4 digits after the decimal** during normalization.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Schema: `weather.observation.v1`
|
|
||||||
|
|
||||||
Payload type: `WeatherObservation`
|
|
||||||
|
|
||||||
A `WeatherObservation` represents a point-in-time observation for a station/location.
|
|
||||||
|
|
||||||
### Fields
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `stationId` | string | no | Provider station/location identifier |
|
|
||||||
| `stationName` | string | no | Human station name |
|
|
||||||
| `timestamp` | timestamp string | yes | Observation timestamp |
|
|
||||||
| `conditionCode` | int | yes | WMO code (`-1` unknown) |
|
|
||||||
| `isDay` | bool | no | Day/night hint |
|
|
||||||
| `textDescription` | string | no | Human-facing short description |
|
|
||||||
| `temperatureC` | number | no | Celsius |
|
|
||||||
| `dewpointC` | number | no | Celsius |
|
|
||||||
| `windDirectionDegrees` | number | no | Degrees |
|
|
||||||
| `windSpeedKmh` | number | no | km/h |
|
|
||||||
| `windGustKmh` | number | no | km/h |
|
|
||||||
| `barometricPressurePa` | number | no | Pascals |
|
|
||||||
| `visibilityMeters` | number | no | Meters |
|
|
||||||
| `relativeHumidityPercent` | number | no | Percent |
|
|
||||||
| `apparentTemperatureC` | number | no | Celsius |
|
|
||||||
| `presentWeather` | array | no | Provider-specific structured weather fragments |
|
|
||||||
|
|
||||||
### Nested: `presentWeather[]`
|
|
||||||
|
|
||||||
Each `presentWeather[]` element:
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `raw` | object | no | Provider-specific JSON object |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Schema: `weather.forecast.v1`
|
|
||||||
|
|
||||||
Payload type: `WeatherForecastRun`
|
|
||||||
|
|
||||||
A `WeatherForecastRun` is a single issued forecast snapshot for a location and a specific product
|
|
||||||
(hourly / narrative / daily). The run contains an ordered list of forecast periods.
|
|
||||||
|
|
||||||
### `product` values
|
|
||||||
|
|
||||||
`product` is one of:
|
|
||||||
|
|
||||||
- `"hourly"`
|
|
||||||
- `"narrative"`
|
|
||||||
- `"daily"`
|
|
||||||
|
|
||||||
### Fields
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `locationId` | string | no | Provider location identifier |
|
|
||||||
| `locationName` | string | no | Human name, if available |
|
|
||||||
| `issuedAt` | string (timestamp) | yes | When this run was generated/issued |
|
|
||||||
| `updatedAt` | string (timestamp) | no | Optional later update time |
|
|
||||||
| `product` | string | yes | One of `hourly`, `narrative`, `daily` |
|
|
||||||
| `latitude` | number | no | Degrees |
|
|
||||||
| `longitude` | number | no | Degrees |
|
|
||||||
| `elevationMeters` | number | no | meters |
|
|
||||||
| `periods` | array | yes | Chronological forecast periods |
|
|
||||||
|
|
||||||
### Nested: `periods[]` (`WeatherForecastPeriod`)
|
|
||||||
|
|
||||||
A `WeatherForecastPeriod` is valid for `[startTime, endTime)`.
|
|
||||||
|
|
||||||
| Field | Type | Required | Units / Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `startTime` | string (timestamp) | yes | Period start |
|
|
||||||
| `endTime` | string (timestamp) | yes | Period end |
|
|
||||||
| `name` | string | no | Human label (often empty for hourly) |
|
|
||||||
| `isDay` | bool | no | Day/night hint |
|
|
||||||
| `conditionCode` | int | yes | WMO code (`-1` for unknown) |
|
|
||||||
| `textDescription` | string | no | Human-facing short phrase |
|
|
||||||
| `temperatureC` | number | no | °C |
|
|
||||||
| `temperatureCMin` | number | no | °C (aggregated products) |
|
|
||||||
| `temperatureCMax` | number | no | °C (aggregated products) |
|
|
||||||
| `dewpointC` | number | no | °C |
|
|
||||||
| `relativeHumidityPercent` | number | no | percent |
|
|
||||||
| `windDirectionDegrees` | number | no | degrees |
|
|
||||||
| `windSpeedKmh` | number | no | km/h |
|
|
||||||
| `windGustKmh` | number | no | km/h |
|
|
||||||
| `barometricPressurePa` | number | no | Pa |
|
|
||||||
| `visibilityMeters` | number | no | meters |
|
|
||||||
| `apparentTemperatureC` | number | no | °C |
|
|
||||||
| `cloudCoverPercent` | number | no | percent |
|
|
||||||
| `probabilityOfPrecipitationPercent` | number | no | percent |
|
|
||||||
| `precipitationAmountMm` | number | no | mm (liquid equivalent) |
|
|
||||||
| `snowfallDepthMm` | number | no | mm |
|
|
||||||
| `uvIndex` | number | no | unitless index |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Schema: `weather.alert.v1`
|
|
||||||
|
|
||||||
Payload type: `WeatherAlertRun`
|
|
||||||
|
|
||||||
A `WeatherAlertRun` is a snapshot of *active* alerts for a location as-of a point in time.
|
|
||||||
A run may contain zero, one, or many alerts.
|
|
||||||
|
|
||||||
### Fields
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `locationId` | string | no | Provider location identifier |
|
|
||||||
| `locationName` | string | no | Human name, if available |
|
|
||||||
| `asOf` | string (timestamp) | yes | When the provider asserted this snapshot is current |
|
|
||||||
| `latitude` | number | no | Degrees |
|
|
||||||
| `longitude` | number | no | Degrees |
|
|
||||||
| `alerts` | array | yes | Active alerts (order provider-dependent) |
|
|
||||||
|
|
||||||
### Nested: `alerts[]` (`WeatherAlert`)
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `id` | string | yes | Provider-stable identifier (often a URL/URI) |
|
|
||||||
| `event` | string | no | Classification/event label |
|
|
||||||
| `headline` | string | no | Alert headline |
|
|
||||||
| `severity` | string | no | Example: Extreme/Severe/Moderate/Minor/Unknown |
|
|
||||||
| `urgency` | string | no | Example: Immediate/Expected/Future/Past/Unknown |
|
|
||||||
| `certainty` | string | no | Example: Observed/Likely/Possible/Unlikely/Unknown |
|
|
||||||
| `status` | string | no | Example: Actual/Exercise/Test/System/Unknown |
|
|
||||||
| `messageType` | string | no | Example: Alert/Update/Cancel |
|
|
||||||
| `category` | string | no | Example: Met/Geo/Safety/Rescue/Fire/Health/Env/Transport/Infra/CBRNE/Other |
|
|
||||||
| `response` | string | no | Example: Shelter/Evacuate/Prepare/Execute/Avoid/Monitor/Assess/AllClear/None |
|
|
||||||
| `response` | string | no | e.g. Shelter/Evacuate/Prepare/... |
|
|
||||||
| `description` | string | no | Narrative |
|
|
||||||
| `instruction` | string | no | What to do |
|
|
||||||
| `sent` | string (timestamp) | no | Provider-dependent |
|
|
||||||
| `effective` | string (timestamp) | no | Provider-dependent |
|
|
||||||
| `onset` | string (timestamp) | no | Provider-dependent |
|
|
||||||
| `expires` | string (timestamp) | no | Provider-dependent |
|
|
||||||
| `areaDescription` | string | no | Often a provider string |
|
|
||||||
| `senderName` | string | no | Provenance |
|
|
||||||
| `references` | array | no | Related alert references |
|
|
||||||
|
|
||||||
### Nested: `references[]` (`AlertReference`)
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `id` | string | no | Provider reference ID/URI |
|
|
||||||
| `identifier` | string | no | Provider identifier string, if distinct |
|
|
||||||
| `sender` | string | no | Sender |
|
|
||||||
| `sent` | string (timestamp) | no | Timestamp |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Schema: `weather.forecast_discussion.v1`
|
|
||||||
|
|
||||||
Payload type: `WeatherForecastDiscussion`
|
|
||||||
|
|
||||||
A `WeatherForecastDiscussion` is an issued narrative bulletin for an NWS office.
|
|
||||||
It is distinct from `weather.forecast.v1`, which is period-based.
|
|
||||||
|
|
||||||
### Fields
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `officeId` | string | no | NWS office identifier, e.g. `LSX` |
|
|
||||||
| `officeName` | string | no | Human office name |
|
|
||||||
| `product` | string | yes | Currently `afd` |
|
|
||||||
| `issuedAt` | string (timestamp) | yes | Bulletin issue time |
|
|
||||||
| `updatedAt` | string (timestamp) | no | Optional page/update timestamp |
|
|
||||||
| `keyMessages` | array | no | Ordered key-message bullet list |
|
|
||||||
| `shortTerm` | object | no | Short-term discussion section |
|
|
||||||
| `longTerm` | object | no | Long-term discussion section |
|
|
||||||
|
|
||||||
### Nested: `shortTerm` / `longTerm`
|
|
||||||
|
|
||||||
| Field | Type | Required | Notes |
|
|
||||||
|---|---:|:---:|---|
|
|
||||||
| `qualifier` | string | no | Header qualifier such as `(Through Late Sunday Night)` |
|
|
||||||
| `issuedAt` | string (timestamp) | no | Optional section-local issue time |
|
|
||||||
| `text` | string | no | Paragraph-preserved prose text |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Compatibility rules
|
|
||||||
|
|
||||||
- Consumers **must** ignore unknown fields.
|
|
||||||
- Producers (weatherfeeder) prefer **additive changes** within a schema version.
|
|
||||||
- Renames/removals/semantic breaks normally require a **schema version bump** (`weather.*.v2`); pre-1.0 projects may choose in-place changes.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
### Observation event (`weather.observation.v1`)
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"id": "nws:KSTL:2026-01-17T14:00:00Z",
|
|
||||||
"schema": "weather.observation.v1",
|
|
||||||
"source": "nws_observation",
|
|
||||||
"effectiveAt": "2026-01-17T14:00:00Z",
|
|
||||||
"payload": {
|
|
||||||
"stationId": "KSTL",
|
|
||||||
"timestamp": "2026-01-17T14:00:00Z",
|
|
||||||
"conditionCode": 1,
|
|
||||||
"textDescription": "Mainly Sunny",
|
|
||||||
"temperatureC": 3.25,
|
|
||||||
"windSpeedKmh": 18.5
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Forecast event (`weather.forecast.v1`)
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"id": "openmeteo:38.63,-90.20:2026-01-17T13:00:00Z",
|
|
||||||
"schema": "weather.forecast.v1",
|
|
||||||
"source": "openmeteo_forecast",
|
|
||||||
"effectiveAt": "2026-01-17T13:00:00Z",
|
|
||||||
"payload": {
|
|
||||||
"locationName": "St. Louis, MO",
|
|
||||||
"issuedAt": "2026-01-17T13:00:00Z",
|
|
||||||
"product": "hourly",
|
|
||||||
"latitude": 38.63,
|
|
||||||
"longitude": -90.2,
|
|
||||||
"periods": [
|
|
||||||
{
|
|
||||||
"startTime": "2026-01-17T14:00:00Z",
|
|
||||||
"endTime": "2026-01-17T15:00:00Z",
|
|
||||||
"conditionCode": 2,
|
|
||||||
"textDescription": "Partly Cloudy",
|
|
||||||
"temperatureC": 3.5,
|
|
||||||
"probabilityOfPrecipitationPercent": 10
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Alert event (`weather.alert.v1`)
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"id": "nws:alerts:2026-01-17T14:10:00Z",
|
|
||||||
"schema": "weather.alert.v1",
|
|
||||||
"source": "nws_alerts",
|
|
||||||
"effectiveAt": "2026-01-17T14:10:00Z",
|
|
||||||
"payload": {
|
|
||||||
"asOf": "2026-01-17T14:05:00Z",
|
|
||||||
"alerts": [
|
|
||||||
{
|
|
||||||
"id": "https://api.weather.gov/alerts/abc123",
|
|
||||||
"event": "Winter Weather Advisory",
|
|
||||||
"headline": "Winter Weather Advisory issued January 17 at 8:05AM CST",
|
|
||||||
"severity": "Moderate",
|
|
||||||
"description": "Mixed precipitation expected...",
|
|
||||||
"expires": "2026-01-18T06:00:00Z"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
57
README.md
57
README.md
@@ -1,35 +1,40 @@
|
|||||||
# weatherfeeder
|
# weatherfeeder
|
||||||
|
|
||||||
weatherfeeder is a small daemon that polls weather observations, forecasts, and alerts from multiple upstream
|
`weatherfeeder` is a config-driven daemon that polls weather providers, normalizes
|
||||||
providers, normalizes them into a provider-independent format, and emits them to a sink.
|
provider-specific responses into canonical weather events, and routes those
|
||||||
|
events to configured sinks.
|
||||||
|
|
||||||
Today, the only implemented sink is `stdout`, which prints JSON-encoded events.
|
It currently supports NWS observations, alerts, hourly forecasts, narrative
|
||||||
|
forecasts, forecast discussions, and weather stories; SPC Day 1-3 convective
|
||||||
|
outlooks; Open-Meteo observations and hourly forecasts; and OpenWeather
|
||||||
|
observations. Implemented sinks are stdout, NATS, and Postgres.
|
||||||
|
|
||||||
## What weatherfeeder emits
|
## Quickstart
|
||||||
|
|
||||||
weatherfeeder emits **feed events** encoded as JSON. Each event includes a schema identifier and a payload.
|
Run the checked-in sample config:
|
||||||
Downstream consumers should key off the `schema` value and decode the `payload` accordingly.
|
|
||||||
|
|
||||||
Canonical domain schemas emitted after normalization:
|
```sh
|
||||||
|
cd cmd/weatherfeeder
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
|
||||||
- `weather.observation.v1` → `WeatherObservation`
|
The sample config at `cmd/weatherfeeder/config.yml` is load-tested and can be
|
||||||
- `weather.forecast.v1` → `WeatherForecastRun`
|
used as a starting point. The executable always reads `config.yml` from its
|
||||||
- `weather.forecast_discussion.v1` → `WeatherForecastDiscussion`
|
current working directory.
|
||||||
- `weather.alert.v1` → `WeatherAlertRun`
|
|
||||||
|
|
||||||
For the complete wire contract (event envelope + payload schemas, fields, units, and compatibility rules), see:
|
## Documentation
|
||||||
|
|
||||||
- **API.md**
|
- [CLI reference](docs/cli.md)
|
||||||
|
- [Configuration reference](docs/config.md)
|
||||||
## Upstream providers (current MVP)
|
- [Operations guide](docs/operations.md)
|
||||||
|
- [Troubleshooting guide](docs/troubleshooting.md)
|
||||||
- NWS: observations, hourly forecasts, narrative forecasts, forecast discussions, alerts
|
- [Example configs](examples/)
|
||||||
- Open-Meteo: observations, hourly forecasts
|
- [Event wire contract](docs/integrations/events.md)
|
||||||
- OpenWeather: observations
|
- [Postgres table contract](docs/integrations/postgres.md)
|
||||||
|
- [NWS integration notes](docs/integrations/nws.md)
|
||||||
## Versioning & compatibility
|
- [SPC integration notes](docs/integrations/spc.md)
|
||||||
|
- [Open-Meteo integration notes](docs/integrations/openmeteo.md)
|
||||||
The JSON field names on canonical payload types are treated as part of the wire contract.
|
- [OpenWeather integration notes](docs/integrations/openweather.md)
|
||||||
Additive changes are preferred. Renames/removals require a schema version bump.
|
- [Architecture policy](docs/policy/architecture.md)
|
||||||
|
- [Development policy](docs/policy/development.md)
|
||||||
See **API.md** for details.
|
- [Documentation policy](docs/policy/documentation.md)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ sources:
|
|||||||
# driver: openweather_observation
|
# driver: openweather_observation
|
||||||
# every: 10m
|
# every: 10m
|
||||||
# params:
|
# params:
|
||||||
# url: "https://api.openweathermap.org/data/2.5/weather?lat=38.6239&lon=-90.3571&appid=c954f2566cb7ccb56b43737b52e88fc6&units=metric"
|
# url: "https://api.openweathermap.org/data/2.5/weather?lat=38.6239&lon=-90.3571&units=metric"
|
||||||
# user_agent: "HomeOps (eric@maximumdirect.net)"
|
# user_agent: "HomeOps (eric@maximumdirect.net)"
|
||||||
|
|
||||||
# - name: NWSObservationKSUS
|
# - name: NWSObservationKSUS
|
||||||
@@ -72,6 +72,15 @@ sources:
|
|||||||
url: "https://forecast.weather.gov/product.php?site=LSX&issuedby=LSX&product=AFD&format=TXT&version=1&glossary=0"
|
url: "https://forecast.weather.gov/product.php?site=LSX&issuedby=LSX&product=AFD&format=TXT&version=1&glossary=0"
|
||||||
user_agent: "HomeOps (eric@maximumdirect.net)"
|
user_agent: "HomeOps (eric@maximumdirect.net)"
|
||||||
|
|
||||||
|
- name: NWSWeatherStoriesSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["weather_story"]
|
||||||
|
driver: nws_weatherstories
|
||||||
|
every: 30m
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/offices/LSX/weatherstories"
|
||||||
|
user_agent: "HomeOps (eric@maximumdirect.net)"
|
||||||
|
|
||||||
- name: OpenMeteoHourlyForecastSTL
|
- name: OpenMeteoHourlyForecastSTL
|
||||||
mode: poll
|
mode: poll
|
||||||
kinds: ["forecast"]
|
kinds: ["forecast"]
|
||||||
@@ -90,6 +99,18 @@ sources:
|
|||||||
url: "https://api.weather.gov/alerts?point=38.6239,-90.3571&limit=20"
|
url: "https://api.weather.gov/alerts?point=38.6239,-90.3571&limit=20"
|
||||||
user_agent: "HomeOps (eric@maximumdirect.net)"
|
user_agent: "HomeOps (eric@maximumdirect.net)"
|
||||||
|
|
||||||
|
- name: SPCConvectiveOutlookSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["outlook"]
|
||||||
|
driver: spc_convective_outlook
|
||||||
|
every: 30m
|
||||||
|
params:
|
||||||
|
latitude: 38.6239
|
||||||
|
longitude: -90.3571
|
||||||
|
location_id: "stl"
|
||||||
|
location_name: "St. Louis, MO"
|
||||||
|
user_agent: "HomeOps (eric@maximumdirect.net)"
|
||||||
|
|
||||||
sinks:
|
sinks:
|
||||||
- name: stdout
|
- name: stdout
|
||||||
driver: stdout
|
driver: stdout
|
||||||
@@ -106,7 +127,7 @@ sinks:
|
|||||||
# params:
|
# params:
|
||||||
# uri: postgres://weatherdb:5432/weatherdb?sslmode=disable
|
# uri: postgres://weatherdb:5432/weatherdb?sslmode=disable
|
||||||
# username: weatherdb
|
# username: weatherdb
|
||||||
# password: weatherdb
|
# password: <database_password>
|
||||||
# prune: 3d
|
# prune: 3d
|
||||||
# # Prunes rows older than now-3d on each write transaction.
|
# # Prunes rows older than now-3d on each write transaction.
|
||||||
|
|
||||||
@@ -117,13 +138,13 @@ sinks:
|
|||||||
|
|
||||||
routes:
|
routes:
|
||||||
- sink: stdout
|
- sink: stdout
|
||||||
kinds: ["observation", "forecast", "forecast_discussion", "alert"]
|
kinds: ["observation", "forecast", "forecast_discussion", "weather_story", "alert", "outlook"]
|
||||||
|
|
||||||
- sink: nats_weatherfeeder
|
- sink: nats_weatherfeeder
|
||||||
kinds: ["observation", "forecast", "forecast_discussion", "alert"]
|
kinds: ["observation", "forecast", "forecast_discussion", "weather_story", "alert", "outlook"]
|
||||||
|
|
||||||
# - sink: pg_weatherfeeder
|
# - sink: pg_weatherfeeder
|
||||||
# kinds: ["observation", "forecast", "forecast_discussion", "alert"]
|
# kinds: ["observation", "forecast", "forecast_discussion", "weather_story", "alert", "outlook"]
|
||||||
|
|
||||||
# - sink: logfile
|
# - sink: logfile
|
||||||
# kinds: ["observation", "alert", "forecast", "forecast_discussion"]
|
# kinds: ["observation", "alert", "forecast", "forecast_discussion", "weather_story", "outlook"]
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -82,6 +84,33 @@ func TestExampleConfigSourcesBuildSchedulerJobs(t *testing.T) {
|
|||||||
t.Fatalf("config.Load(config.yml) unexpected error: %v", err)
|
t.Fatalf("config.Load(config.yml) unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assertConfigSourcesBuildSchedulerJobs(t, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaintainedConfigExamplesLoad(t *testing.T) {
|
||||||
|
paths, err := filepath.Glob("../../examples/*.yml")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("filepath.Glob examples: %v", err)
|
||||||
|
}
|
||||||
|
sort.Strings(paths)
|
||||||
|
if len(paths) == 0 {
|
||||||
|
t.Fatalf("expected maintained config examples")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range paths {
|
||||||
|
t.Run(filepath.Base(path), func(t *testing.T) {
|
||||||
|
cfg, err := config.Load(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("config.Load(%s) unexpected error: %v", path, err)
|
||||||
|
}
|
||||||
|
assertConfigSourcesBuildSchedulerJobs(t, cfg)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertConfigSourcesBuildSchedulerJobs(t *testing.T, cfg *config.Config) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
reg := fksources.NewRegistry()
|
reg := fksources.NewRegistry()
|
||||||
wfsources.RegisterBuiltins(reg)
|
wfsources.RegisterBuiltins(reg)
|
||||||
|
|
||||||
@@ -91,6 +120,10 @@ func TestExampleConfigSourcesBuildSchedulerJobs(t *testing.T) {
|
|||||||
t.Fatalf("BuildInput(sources[%d]) error = %v", i, err)
|
t.Fatalf("BuildInput(sources[%d]) error = %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := fksources.ValidateExpectedKinds(sc, in); err != nil {
|
||||||
|
t.Fatalf("ValidateExpectedKinds(sources[%d]) error = %v", i, err)
|
||||||
|
}
|
||||||
|
|
||||||
job, err := fkscheduler.JobFromSourceConfig(in, sc)
|
job, err := fkscheduler.JobFromSourceConfig(in, sc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("JobFromSourceConfig(sources[%d]) error = %v", i, err)
|
t.Fatalf("JobFromSourceConfig(sources[%d]) error = %v", i, err)
|
||||||
|
|||||||
72
docs/cli.md
Normal file
72
docs/cli.md
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
# CLI Reference
|
||||||
|
|
||||||
|
## Shortest Useful Command
|
||||||
|
|
||||||
|
Run `weatherfeeder` from a directory containing `config.yml`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cd cmd/weatherfeeder
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
|
||||||
|
When using a built binary:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./weatherfeeder
|
||||||
|
```
|
||||||
|
|
||||||
|
## Command Overview
|
||||||
|
|
||||||
|
`weatherfeeder` starts a long-running polling daemon. On startup it:
|
||||||
|
|
||||||
|
1. reads `config.yml` from the current working directory;
|
||||||
|
2. builds configured sources, sinks, and routes;
|
||||||
|
3. starts polling sources on their configured intervals;
|
||||||
|
4. normalizes and deduplicates events;
|
||||||
|
5. dispatches matching events to configured sinks.
|
||||||
|
|
||||||
|
The command logs startup, runtime, and shutdown messages to stderr using the Go
|
||||||
|
standard logger.
|
||||||
|
|
||||||
|
## Flags
|
||||||
|
|
||||||
|
There are currently no CLI flags, subcommands, or environment-variable based
|
||||||
|
configuration controls.
|
||||||
|
|
||||||
|
The config path is fixed at `config.yml` relative to the process current working
|
||||||
|
directory. To run with a different config, change the working directory or place
|
||||||
|
the desired file at that path.
|
||||||
|
|
||||||
|
## Common Workflows
|
||||||
|
|
||||||
|
Run the checked-in sample config:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cd cmd/weatherfeeder
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
|
||||||
|
Maintained copyable configs are available under [`examples/`](../examples/).
|
||||||
|
|
||||||
|
Build and run a local binary:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go build -o weatherfeeder ./cmd/weatherfeeder
|
||||||
|
cp cmd/weatherfeeder/config.yml .
|
||||||
|
./weatherfeeder
|
||||||
|
```
|
||||||
|
|
||||||
|
Run in the project container image with a mounted config:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
docker run --rm -v "$PWD/config.yml:/weatherfeeder/config.yml:ro" weatherfeeder
|
||||||
|
```
|
||||||
|
|
||||||
|
The Docker image sets `/weatherfeeder` as the working directory, so the mounted
|
||||||
|
file must appear at `/weatherfeeder/config.yml`.
|
||||||
|
|
||||||
|
## Shutdown
|
||||||
|
|
||||||
|
Stop the daemon with `Ctrl-C` or `SIGTERM`. The process uses context-aware
|
||||||
|
shutdown for scheduler, dispatcher, processors, sources, and sinks, then logs
|
||||||
|
`shutdown complete`.
|
||||||
273
docs/config.md
Normal file
273
docs/config.md
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
# Configuration Reference
|
||||||
|
|
||||||
|
## Config File
|
||||||
|
|
||||||
|
`weatherfeeder` reads exactly one YAML file named `config.yml` from the current
|
||||||
|
working directory. There is no config path flag and no search path.
|
||||||
|
|
||||||
|
YAML decoding is strict for config struct fields: misspelled fields such as
|
||||||
|
`sources[].drviver` fail startup. Driver-specific `params` maps are validated by
|
||||||
|
the source or sink constructor that consumes them.
|
||||||
|
|
||||||
|
The top-level file contains:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sources:
|
||||||
|
- name: NWSObservationKSTL
|
||||||
|
mode: poll
|
||||||
|
driver: nws_observation
|
||||||
|
every: 10m
|
||||||
|
kinds: ["observation"]
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/stations/KSTL/observations/latest"
|
||||||
|
user_agent: "Example weatherfeeder operator (ops@example.com)"
|
||||||
|
|
||||||
|
sinks:
|
||||||
|
- name: stdout
|
||||||
|
driver: stdout
|
||||||
|
params: {}
|
||||||
|
|
||||||
|
routes:
|
||||||
|
- sink: stdout
|
||||||
|
kinds: ["observation"]
|
||||||
|
```
|
||||||
|
|
||||||
|
`sources` and `sinks` must each contain at least one entry. `routes` is optional.
|
||||||
|
When `routes` is omitted, every configured sink receives every event kind.
|
||||||
|
Maintained copyable configs are available under [`examples/`](../examples/).
|
||||||
|
|
||||||
|
## Production-Oriented Shape
|
||||||
|
|
||||||
|
A typical deployment uses multiple polling sources and sends the same canonical
|
||||||
|
event stream to a broker or database:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sources:
|
||||||
|
- name: NWSAlertsLocal
|
||||||
|
mode: poll
|
||||||
|
driver: nws_alerts
|
||||||
|
every: 1m
|
||||||
|
kinds: ["alert"]
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/alerts?point=38.6239,-90.3571&limit=20"
|
||||||
|
user_agent: "Example weatherfeeder operator (ops@example.com)"
|
||||||
|
|
||||||
|
- name: SPCConvectiveOutlookLocal
|
||||||
|
mode: poll
|
||||||
|
driver: spc_convective_outlook
|
||||||
|
every: 30m
|
||||||
|
kinds: ["outlook"]
|
||||||
|
params:
|
||||||
|
latitude: 38.6239
|
||||||
|
longitude: -90.3571
|
||||||
|
location_id: "local"
|
||||||
|
location_name: "Configured point"
|
||||||
|
user_agent: "Example weatherfeeder operator (ops@example.com)"
|
||||||
|
|
||||||
|
sinks:
|
||||||
|
- name: nats_weather
|
||||||
|
driver: nats
|
||||||
|
params:
|
||||||
|
url: nats://nats:4222
|
||||||
|
subject: weatherfeeder
|
||||||
|
|
||||||
|
- name: pg_weather
|
||||||
|
driver: postgres
|
||||||
|
params:
|
||||||
|
uri: postgres://weatherdb:5432/weatherdb?sslmode=disable
|
||||||
|
username: weatherdb
|
||||||
|
password: <database_password>
|
||||||
|
prune: 3d
|
||||||
|
|
||||||
|
routes:
|
||||||
|
- sink: nats_weather
|
||||||
|
kinds: ["observation", "forecast", "forecast_discussion", "weather_story", "alert", "outlook"]
|
||||||
|
|
||||||
|
- sink: pg_weather
|
||||||
|
kinds: ["observation", "forecast", "forecast_discussion", "weather_story", "alert", "outlook"]
|
||||||
|
```
|
||||||
|
|
||||||
|
Do not commit real API keys, database passwords, or personal contact addresses in
|
||||||
|
copyable configs.
|
||||||
|
|
||||||
|
## Top-Level Fields
|
||||||
|
|
||||||
|
| Field | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `sources` | yes | List of configured input sources. |
|
||||||
|
| `sinks` | yes | List of configured output sinks. |
|
||||||
|
| `routes` | no | List of sink routing rules. If omitted, all sinks receive all kinds. |
|
||||||
|
|
||||||
|
## Source Fields
|
||||||
|
|
||||||
|
| Field | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `name` | yes | Unique source name. Used as the event source identifier. |
|
||||||
|
| `driver` | yes | Source driver name. |
|
||||||
|
| `mode` | no | `poll`, `stream`, or omitted for auto. Current weatherfeeder drivers are polling drivers. |
|
||||||
|
| `every` | yes | Poll interval for current weatherfeeder source drivers. |
|
||||||
|
| `kinds` | no | Expected event kinds. If present, startup verifies they match the source driver. |
|
||||||
|
| `params` | driver-specific | Driver parameters. See the source-specific sections below. |
|
||||||
|
|
||||||
|
Current event kinds are `observation`, `forecast`, `forecast_discussion`,
|
||||||
|
`weather_story`, `alert`, and `outlook`.
|
||||||
|
|
||||||
|
## Source Drivers
|
||||||
|
|
||||||
|
| Driver | Kind | Upstream product |
|
||||||
|
|---|---|---|
|
||||||
|
| `nws_observation` | `observation` | NWS station latest observation. |
|
||||||
|
| `nws_alerts` | `alert` | NWS alerts collection. |
|
||||||
|
| `nws_forecast_hourly` | `forecast` | NWS hourly gridpoint forecast. |
|
||||||
|
| `nws_forecast_narrative` | `forecast` | NWS narrative gridpoint forecast. |
|
||||||
|
| `nws_forecast_discussion` | `forecast_discussion` | NWS forecast discussion HTML product. |
|
||||||
|
| `nws_weatherstories` | `weather_story` | NWS office weather stories. |
|
||||||
|
| `openmeteo_observation` | `observation` | Open-Meteo current conditions. |
|
||||||
|
| `openmeteo_forecast` | `forecast` | Open-Meteo hourly forecast. |
|
||||||
|
| `openweather_observation` | `observation` | OpenWeather current weather. |
|
||||||
|
| `spc_convective_outlook` | `outlook` | SPC Day 1-3 convective outlooks. |
|
||||||
|
|
||||||
|
## HTTP Source Params
|
||||||
|
|
||||||
|
Most source drivers use the shared HTTP polling helper.
|
||||||
|
|
||||||
|
| Param | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `url` | yes | Full upstream request URL. `URL` is also accepted by the helper. |
|
||||||
|
| `user_agent` | yes | User-Agent sent to the upstream provider. `userAgent` is also accepted by the helper. |
|
||||||
|
| `conditional` | no | Boolean. Defaults to `true`; enables ETag and Last-Modified conditional requests. |
|
||||||
|
| `http_timeout` | no | Positive duration for the HTTP client timeout. |
|
||||||
|
| `http_response_body_limit_bytes` | no | Positive integer response body limit in bytes. |
|
||||||
|
|
||||||
|
When `conditional` is enabled and the upstream returns `304 Not Modified`, the
|
||||||
|
source emits no events for that poll.
|
||||||
|
|
||||||
|
OpenWeather observation URLs must include `units=metric`. Startup fails if the
|
||||||
|
URL omits it or sets another unit system.
|
||||||
|
|
||||||
|
## SPC Convective Outlook Params
|
||||||
|
|
||||||
|
`spc_convective_outlook` fetches the twelve required Day 1-3 GeoJSON outlook
|
||||||
|
products and the three required Day 1-3 print pages as one atomic bundle.
|
||||||
|
|
||||||
|
| Param | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `latitude` | yes | Location latitude in decimal degrees. |
|
||||||
|
| `longitude` | yes | Location longitude in decimal degrees. |
|
||||||
|
| `user_agent` | yes | User-Agent sent to SPC. `userAgent` is also accepted. |
|
||||||
|
| `location_id` | no | Operator-defined location identifier copied into canonical outlook runs. |
|
||||||
|
| `location_name` | no | Human location label copied into canonical outlook runs. |
|
||||||
|
| `geojson_urls` | no | Map of product key to override URL. Used for tests and upstream URL changes. |
|
||||||
|
| `discussion_urls` | no | Map of day key to override print-page URL. Used for tests and upstream URL changes. |
|
||||||
|
| `rss_url` | no | Optional RSS URL. RSS is not fetched unless this is configured. |
|
||||||
|
| `http_timeout` | no | Positive duration for the HTTP client timeout. |
|
||||||
|
| `http_response_body_limit_bytes` | no | Positive integer response body limit in bytes. |
|
||||||
|
|
||||||
|
GeoJSON product keys are `day1_categorical`, `day1_tornado`, `day1_hail`,
|
||||||
|
`day1_wind`, `day2_categorical`, `day2_tornado`, `day2_hail`, `day2_wind`,
|
||||||
|
`day3_categorical`, `day3_tornado`, `day3_hail`, and `day3_wind`.
|
||||||
|
Discussion keys are `day1`, `day2`, and `day3`.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sources:
|
||||||
|
- name: SPCConvectiveOutlookSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["outlook"]
|
||||||
|
driver: spc_convective_outlook
|
||||||
|
every: 30m
|
||||||
|
params:
|
||||||
|
latitude: 38.6239
|
||||||
|
longitude: -90.3571
|
||||||
|
location_id: "stl"
|
||||||
|
location_name: "St. Louis, MO"
|
||||||
|
user_agent: "Example weatherfeeder operator (ops@example.com)"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sink Fields
|
||||||
|
|
||||||
|
| Field | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `name` | yes | Unique sink name. Routes refer to this value. |
|
||||||
|
| `driver` | yes | Sink driver name. |
|
||||||
|
| `params` | driver-specific | Sink parameters. |
|
||||||
|
|
||||||
|
## Sink Drivers
|
||||||
|
|
||||||
|
### `stdout`
|
||||||
|
|
||||||
|
Prints each event as JSON to stdout.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sinks:
|
||||||
|
- name: stdout
|
||||||
|
driver: stdout
|
||||||
|
params: {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `nats`
|
||||||
|
|
||||||
|
Publishes each event as JSON to a NATS subject.
|
||||||
|
|
||||||
|
| Param | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `url` | yes | NATS server URL, such as `nats://localhost:4222`. |
|
||||||
|
| `subject` | yes | Subject to publish events to. |
|
||||||
|
|
||||||
|
### `postgres`
|
||||||
|
|
||||||
|
Writes supported canonical weather events to Postgres using weatherfeeder's
|
||||||
|
registered schema mapping. The table contract is documented in
|
||||||
|
[Postgres integration](integrations/postgres.md).
|
||||||
|
|
||||||
|
| Param | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `uri` | yes | PostgreSQL connection URI. |
|
||||||
|
| `username` | yes | Database username. |
|
||||||
|
| `password` | yes | Database password. |
|
||||||
|
| `prune` | no | Retention window. If set, rows older than the window are pruned on each write transaction. |
|
||||||
|
|
||||||
|
`prune` accepts Go duration strings such as `72h`, plus day and week suffixes
|
||||||
|
such as `3d` and `2w`.
|
||||||
|
|
||||||
|
## Routes
|
||||||
|
|
||||||
|
Routes connect event kinds to sinks:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
routes:
|
||||||
|
- sink: stdout
|
||||||
|
kinds: ["observation", "alert"]
|
||||||
|
```
|
||||||
|
|
||||||
|
| Field | Required | Description |
|
||||||
|
|---|:---:|---|
|
||||||
|
| `sink` | yes | Name of a configured sink. |
|
||||||
|
| `kinds` | no | Event kinds to send to that sink. Omit or use an empty list to match all kinds. |
|
||||||
|
|
||||||
|
Route `kinds` values are trimmed and lowercased by the dispatcher. Blank entries
|
||||||
|
are rejected.
|
||||||
|
|
||||||
|
## Duration Formats
|
||||||
|
|
||||||
|
Top-level source `every` accepts:
|
||||||
|
|
||||||
|
- Go duration strings such as `30s`, `10m`, or `1h`;
|
||||||
|
- integer values, interpreted as minutes;
|
||||||
|
- numeric strings such as `"15"`, also interpreted as minutes.
|
||||||
|
|
||||||
|
HTTP param durations such as `http_timeout` accept Go duration strings. Numeric
|
||||||
|
values and numeric strings are interpreted as seconds.
|
||||||
|
|
||||||
|
Postgres `prune` must be a string duration.
|
||||||
|
|
||||||
|
## Secrets
|
||||||
|
|
||||||
|
The config file is read directly from disk and has no built-in secret expansion.
|
||||||
|
Keep real credentials out of repository-tracked configs. Use deployment tooling
|
||||||
|
to render `config.yml` with the needed secret values before starting the daemon.
|
||||||
|
|
||||||
|
## Maintained Examples
|
||||||
|
|
||||||
|
- [Minimal stdout config](../examples/config.minimal.yml)
|
||||||
|
- [NATS publishing config](../examples/config.nats.yml)
|
||||||
|
- [Postgres persistence config](../examples/config.postgres.yml)
|
||||||
289
docs/integrations/events.md
Normal file
289
docs/integrations/events.md
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
# Event Wire Contract
|
||||||
|
|
||||||
|
This document is the canonical JSON contract for events emitted by
|
||||||
|
`weatherfeeder` JSON sinks, including stdout and NATS. Postgres stores the same
|
||||||
|
event envelope fields in parent table columns; see
|
||||||
|
[Postgres integration](postgres.md).
|
||||||
|
|
||||||
|
Downstream consumers should read the envelope, switch on `schema`, and decode
|
||||||
|
`payload` according to that schema.
|
||||||
|
|
||||||
|
## Envelope
|
||||||
|
|
||||||
|
Every emitted event is a JSON object with these fields:
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `id` | string | yes | Stable event identifier. Treat as opaque. |
|
||||||
|
| `kind` | string | yes | Routing kind, such as `observation` or `alert`. |
|
||||||
|
| `source` | string | yes | Configured source name. |
|
||||||
|
| `emitted_at` | timestamp | yes | When the daemon emitted the event. |
|
||||||
|
| `effective_at` | timestamp | no | Timestamp the payload is about, when known. |
|
||||||
|
| `schema` | string | no | Schema identifier. Weatherfeeder sources and normalizers set this. |
|
||||||
|
| `payload` | object, array, string, or scalar | yes | Schema-specific payload. |
|
||||||
|
|
||||||
|
Timestamps are JSON strings using Go `time.Time` JSON encoding, which is
|
||||||
|
RFC3339Nano-compatible. Weatherfeeder normalizers use UTC timestamps for
|
||||||
|
canonical payloads.
|
||||||
|
|
||||||
|
## Kinds And Schemas
|
||||||
|
|
||||||
|
Canonical schemas emitted after normalization:
|
||||||
|
|
||||||
|
| Kind | Schema | Payload |
|
||||||
|
|---|---|---|
|
||||||
|
| `observation` | `weather.observation.v1` | `WeatherObservation` |
|
||||||
|
| `forecast` | `weather.forecast.v1` | `WeatherForecastRun` |
|
||||||
|
| `forecast_discussion` | `weather.forecast_discussion.v1` | `WeatherForecastDiscussion` |
|
||||||
|
| `weather_story` | `weather.weather_story.v1` | `WeatherStoryRun` |
|
||||||
|
| `alert` | `weather.alert.v1` | `WeatherAlertRun` |
|
||||||
|
| `outlook` | `weather.outlook.v1` | `WeatherOutlookRun` |
|
||||||
|
|
||||||
|
Raw upstream schemas emitted by current sources:
|
||||||
|
|
||||||
|
| Kind | Schema | Payload |
|
||||||
|
|---|---|---|
|
||||||
|
| `observation` | `raw.nws.observation.v1` | NWS observation JSON |
|
||||||
|
| `observation` | `raw.openmeteo.current.v1` | Open-Meteo current JSON |
|
||||||
|
| `observation` | `raw.openweather.current.v1` | OpenWeather current JSON |
|
||||||
|
| `forecast` | `raw.nws.hourly.forecast.v1` | NWS hourly forecast JSON |
|
||||||
|
| `forecast` | `raw.nws.narrative.forecast.v1` | NWS narrative forecast JSON |
|
||||||
|
| `forecast_discussion` | `raw.nws.forecast_discussion.v1` | NWS forecast discussion HTML string |
|
||||||
|
| `weather_story` | `raw.nws.weatherstories.v1` | NWS weather stories JSON |
|
||||||
|
| `forecast` | `raw.openmeteo.hourly.forecast.v1` | Open-Meteo hourly forecast JSON |
|
||||||
|
| `alert` | `raw.nws.alerts.v1` | NWS alerts JSON |
|
||||||
|
| `outlook` | `raw.spc.convective_outlook.v1` | SPC convective outlook raw bundle |
|
||||||
|
|
||||||
|
`standards.SchemaRawOpenWeatherHourlyForecastV1` exists in code, but no current
|
||||||
|
registered source emits it.
|
||||||
|
|
||||||
|
## Shared Conventions
|
||||||
|
|
||||||
|
- Canonical numeric measurements use metric units.
|
||||||
|
- Floating-point values in canonical payloads are rounded to 4 digits after the
|
||||||
|
decimal point during normalization.
|
||||||
|
- Optional fields use JSON `omitempty`; absent fields should be treated as
|
||||||
|
unknown.
|
||||||
|
- `conditionCode` is a WMO weather interpretation code. Unknown observation
|
||||||
|
conditions use `-1`. Forecast period `conditionCode` is optional.
|
||||||
|
- Additive fields are compatible within a schema version. Removing, renaming, or
|
||||||
|
changing the meaning of a field requires a new schema identifier.
|
||||||
|
|
||||||
|
## `weather.observation.v1`
|
||||||
|
|
||||||
|
Payload type: `WeatherObservation`.
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `stationId` | string | no | Provider station/location identifier. |
|
||||||
|
| `stationName` | string | no | Human station name. |
|
||||||
|
| `timestamp` | timestamp | yes | Observation timestamp. |
|
||||||
|
| `conditionCode` | integer | yes | WMO code; `-1` means unknown. |
|
||||||
|
| `isDay` | boolean | no | Day/night hint. |
|
||||||
|
| `textDescription` | string | no | Short human description. |
|
||||||
|
| `temperatureC` | number | no | Celsius. |
|
||||||
|
| `dewpointC` | number | no | Celsius. |
|
||||||
|
| `windDirectionDegrees` | number | no | Degrees. |
|
||||||
|
| `windSpeedKmh` | number | no | Kilometers per hour. |
|
||||||
|
| `windGustKmh` | number | no | Kilometers per hour. |
|
||||||
|
| `barometricPressurePa` | number | no | Pascals. |
|
||||||
|
| `visibilityMeters` | number | no | Meters. |
|
||||||
|
| `relativeHumidityPercent` | number | no | Percent from 0 to 100. |
|
||||||
|
| `apparentTemperatureC` | number | no | Celsius. |
|
||||||
|
| `presentWeather` | array | no | Provider-specific present weather fragments. |
|
||||||
|
|
||||||
|
`presentWeather[]` entries contain optional `raw` objects.
|
||||||
|
|
||||||
|
## `weather.forecast.v1`
|
||||||
|
|
||||||
|
Payload type: `WeatherForecastRun`.
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `locationId` | string | no | Provider location identifier. |
|
||||||
|
| `locationName` | string | no | Human location name. |
|
||||||
|
| `issuedAt` | timestamp | yes | When the forecast run was generated or issued. |
|
||||||
|
| `updatedAt` | timestamp | no | Subsequent provider update time. |
|
||||||
|
| `product` | string | yes | Current emitted values are `hourly` and `narrative`. |
|
||||||
|
| `latitude` | number | no | Degrees. |
|
||||||
|
| `longitude` | number | no | Degrees. |
|
||||||
|
| `elevationMeters` | number | no | Meters. |
|
||||||
|
| `periods` | array | yes | Ordered forecast periods. |
|
||||||
|
|
||||||
|
`periods[]` entries:
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `startTime` | timestamp | yes | Period start. |
|
||||||
|
| `endTime` | timestamp | yes | Period end. |
|
||||||
|
| `name` | string | no | Human label. |
|
||||||
|
| `isDay` | boolean | no | Day/night hint. |
|
||||||
|
| `conditionCode` | integer | no | WMO code when applicable. |
|
||||||
|
| `textDescription` | string | no | Human summary. |
|
||||||
|
| `temperatureC` | number | no | Celsius. |
|
||||||
|
| `temperatureCMin` | number | no | Celsius. |
|
||||||
|
| `temperatureCMax` | number | no | Celsius. |
|
||||||
|
| `dewpointC` | number | no | Celsius. |
|
||||||
|
| `relativeHumidityPercent` | number | no | Percent from 0 to 100. |
|
||||||
|
| `windDirectionDegrees` | number | no | Degrees. |
|
||||||
|
| `windSpeedKmh` | number | no | Kilometers per hour. |
|
||||||
|
| `windGustKmh` | number | no | Kilometers per hour. |
|
||||||
|
| `barometricPressurePa` | number | no | Pascals. |
|
||||||
|
| `visibilityMeters` | number | no | Meters. |
|
||||||
|
| `apparentTemperatureC` | number | no | Celsius. |
|
||||||
|
| `cloudCoverPercent` | number | no | Percent from 0 to 100. |
|
||||||
|
| `probabilityOfPrecipitationPercent` | number | no | Percent from 0 to 100. |
|
||||||
|
| `precipitationAmountMm` | number | no | Liquid-equivalent millimeters. |
|
||||||
|
| `snowfallDepthMm` | number | no | Millimeters. |
|
||||||
|
| `uvIndex` | number | no | Unitless index. |
|
||||||
|
|
||||||
|
## `weather.forecast_discussion.v1`
|
||||||
|
|
||||||
|
Payload type: `WeatherForecastDiscussion`.
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `officeId` | string | no | NWS office identifier. |
|
||||||
|
| `officeName` | string | no | Office name. |
|
||||||
|
| `product` | string | yes | Current value is `afd`. |
|
||||||
|
| `issuedAt` | timestamp | yes | Bulletin issue time. |
|
||||||
|
| `updatedAt` | timestamp | no | Subsequent update time. |
|
||||||
|
| `keyMessages` | array of strings | no | Extracted key messages. |
|
||||||
|
| `shortTerm` | object | no | Short-term section. |
|
||||||
|
| `longTerm` | object | no | Long-term section. |
|
||||||
|
|
||||||
|
`shortTerm` and `longTerm` sections contain optional `qualifier`, `issuedAt`,
|
||||||
|
and `text` fields.
|
||||||
|
|
||||||
|
## `weather.weather_story.v1`
|
||||||
|
|
||||||
|
Payload type: `WeatherStoryRun`.
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `officeId` | string | no | NWS office identifier. |
|
||||||
|
| `asOf` | timestamp | yes | Snapshot time. |
|
||||||
|
| `stories` | array | yes | Ordered story cards. |
|
||||||
|
|
||||||
|
`stories[]` entries:
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `officeId` | string | no | Office identifier. |
|
||||||
|
| `startTime` | timestamp | yes | Story start. |
|
||||||
|
| `endTime` | timestamp | yes | Story end. |
|
||||||
|
| `updatedAt` | timestamp | yes | Story update time. |
|
||||||
|
| `title` | string | no | Story title. |
|
||||||
|
| `description` | string | no | Story description. |
|
||||||
|
| `altText` | string | no | Image alternate text. |
|
||||||
|
| `priority` | boolean | yes | Provider priority flag. |
|
||||||
|
| `order` | integer | yes | Provider display order. |
|
||||||
|
| `downloadUrl` | string | no | Story image URL. |
|
||||||
|
|
||||||
|
## `weather.alert.v1`
|
||||||
|
|
||||||
|
Payload type: `WeatherAlertRun`.
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `locationId` | string | no | Provider location identifier. |
|
||||||
|
| `locationName` | string | no | Human location name. |
|
||||||
|
| `asOf` | timestamp | yes | Snapshot time. |
|
||||||
|
| `latitude` | number | no | Degrees. |
|
||||||
|
| `longitude` | number | no | Degrees. |
|
||||||
|
| `alerts` | array | yes | Active alerts. |
|
||||||
|
|
||||||
|
`alerts[]` entries:
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `id` | string | yes | Provider-stable alert identifier. |
|
||||||
|
| `event` | string | no | Alert event label. |
|
||||||
|
| `headline` | string | no | Alert headline. |
|
||||||
|
| `severity` | string | no | Provider severity. |
|
||||||
|
| `urgency` | string | no | Provider urgency. |
|
||||||
|
| `certainty` | string | no | Provider certainty. |
|
||||||
|
| `status` | string | no | Alert status. |
|
||||||
|
| `messageType` | string | no | Alert message type. |
|
||||||
|
| `category` | string | no | Alert category. |
|
||||||
|
| `response` | string | no | Recommended response. |
|
||||||
|
| `description` | string | no | Alert description. |
|
||||||
|
| `instruction` | string | no | Alert instruction. |
|
||||||
|
| `sent` | timestamp | no | Provider sent time. |
|
||||||
|
| `effective` | timestamp | no | Effective time. |
|
||||||
|
| `onset` | timestamp | no | Onset time. |
|
||||||
|
| `expires` | timestamp | no | Expiration time. |
|
||||||
|
| `areaDescription` | string | no | Affected area description. |
|
||||||
|
| `senderName` | string | no | Provider sender name. |
|
||||||
|
| `references` | array | no | Related alerts. |
|
||||||
|
|
||||||
|
`references[]` entries contain optional `id`, `identifier`, `sender`, and
|
||||||
|
`sent` fields.
|
||||||
|
|
||||||
|
## `weather.outlook.v1`
|
||||||
|
|
||||||
|
Payload type: `WeatherOutlookRun`.
|
||||||
|
|
||||||
|
The current producer is the SPC convective outlook normalizer. It emits Day 1-3
|
||||||
|
convective outlook polygons for categorical, tornado, hail, and wind products.
|
||||||
|
All timestamps are UTC.
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `locationId` | string | no | Operator-configured location identifier. |
|
||||||
|
| `locationName` | string | no | Operator-configured location label. |
|
||||||
|
| `latitude` | number | no | Configured point latitude in decimal degrees. |
|
||||||
|
| `longitude` | number | no | Configured point longitude in decimal degrees. |
|
||||||
|
| `asOf` | timestamp | yes | Snapshot time. For SPC, this is the latest outlook issue time when available. |
|
||||||
|
| `issuedAt` | timestamp | no | Latest issue time across outlook features when any feature exists. |
|
||||||
|
| `outlooks` | array | yes | Ordered outlook polygons. |
|
||||||
|
|
||||||
|
`outlooks[]` entries:
|
||||||
|
|
||||||
|
| Field | Type | Required | Notes |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `id` | string | yes | Deterministic weatherfeeder outlook identifier. |
|
||||||
|
| `provider` | string | yes | Current value is `spc`. |
|
||||||
|
| `product` | string | yes | Current value is `convective`. |
|
||||||
|
| `day` | integer | yes | SPC outlook day, currently `1`, `2`, or `3`. |
|
||||||
|
| `outlookType` | string | yes | `categorical`, `tornado`, `hail`, or `wind`. |
|
||||||
|
| `label` | string | yes | SPC outlook label such as `SLGT` or `15`. |
|
||||||
|
| `labelText` | string | no | Human label text from SPC, when present. |
|
||||||
|
| `severityRank` | integer | no | SPC `DN` value, when present. |
|
||||||
|
| `validFrom` | timestamp | yes | Valid period start. |
|
||||||
|
| `validTo` | timestamp | yes | Valid period end. |
|
||||||
|
| `issuedAt` | timestamp | yes | Feature issue time. |
|
||||||
|
| `expiresAt` | timestamp | yes | Expiration time; currently equal to `validTo`. |
|
||||||
|
| `forecaster` | string | no | SPC forecaster text, when present. |
|
||||||
|
| `headline` | string | no | Matching Day 1-3 print-page product title. |
|
||||||
|
| `summary` | string | no | Text from the print-page `...SUMMARY...` section. |
|
||||||
|
| `discussion` | string | no | Cleaned full print-page product text. |
|
||||||
|
| `sourceUrl` | string | no | GeoJSON product URL for this outlook feature. |
|
||||||
|
| `imageUrl` | string | no | Reserved for provider image URLs; currently empty. |
|
||||||
|
| `containsLocation` | boolean | yes | Whether the configured point is inside or on the boundary of the polygon. |
|
||||||
|
| `geometry` | object | yes | Compact GeoJSON `Polygon` or `MultiPolygon` geometry. |
|
||||||
|
|
||||||
|
`geometry` preserves the SPC feature geometry as compact GeoJSON using
|
||||||
|
`[longitude, latitude]` coordinate order. `containsLocation` is computed with
|
||||||
|
that geometry and the configured source `latitude`/`longitude`; boundary points
|
||||||
|
count as contained. All outlook polygons are emitted, including polygons that do
|
||||||
|
not contain the configured point.
|
||||||
|
|
||||||
|
## Compact Example
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "NWSObservationKSTL:2026-06-10T12:00:00Z",
|
||||||
|
"kind": "observation",
|
||||||
|
"source": "NWSObservationKSTL",
|
||||||
|
"emitted_at": "2026-06-10T12:00:05Z",
|
||||||
|
"effective_at": "2026-06-10T12:00:00Z",
|
||||||
|
"schema": "weather.observation.v1",
|
||||||
|
"payload": {
|
||||||
|
"stationId": "KSTL",
|
||||||
|
"timestamp": "2026-06-10T12:00:00Z",
|
||||||
|
"conditionCode": 0,
|
||||||
|
"temperatureC": 22.5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
119
docs/integrations/nws.md
Normal file
119
docs/integrations/nws.md
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
# NWS Integration Notes
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document describes the NWS products that `weatherfeeder` currently polls
|
||||||
|
and normalizes. It is for developers and operators maintaining NWS source URLs,
|
||||||
|
normalizers, fixtures, and tests.
|
||||||
|
|
||||||
|
General config syntax belongs in [configuration](../config.md). Emitted JSON
|
||||||
|
events are documented in [event wire contract](events.md).
|
||||||
|
|
||||||
|
## Implemented Drivers
|
||||||
|
|
||||||
|
| Driver | Kind | Raw schema | Canonical schema |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `nws_observation` | `observation` | `raw.nws.observation.v1` | `weather.observation.v1` |
|
||||||
|
| `nws_alerts` | `alert` | `raw.nws.alerts.v1` | `weather.alert.v1` |
|
||||||
|
| `nws_forecast_hourly` | `forecast` | `raw.nws.hourly.forecast.v1` | `weather.forecast.v1` |
|
||||||
|
| `nws_forecast_narrative` | `forecast` | `raw.nws.narrative.forecast.v1` | `weather.forecast.v1` |
|
||||||
|
| `nws_forecast_discussion` | `forecast_discussion` | `raw.nws.forecast_discussion.v1` | `weather.forecast_discussion.v1` |
|
||||||
|
| `nws_weatherstories` | `weather_story` | `raw.nws.weatherstories.v1` | `weather.weather_story.v1` |
|
||||||
|
|
||||||
|
## Config Requirements
|
||||||
|
|
||||||
|
All NWS drivers require HTTP source params:
|
||||||
|
|
||||||
|
- `url`
|
||||||
|
- `user_agent`
|
||||||
|
|
||||||
|
The shared HTTP helper also accepts `conditional`, `http_timeout`, and
|
||||||
|
`http_response_body_limit_bytes`. Conditional requests are enabled by default;
|
||||||
|
an upstream `304 Not Modified` response emits no event for that poll.
|
||||||
|
|
||||||
|
NWS expects a descriptive `User-Agent`. Do not use anonymous or placeholder
|
||||||
|
contact values in production configs.
|
||||||
|
|
||||||
|
## Upstream Shapes Used
|
||||||
|
|
||||||
|
`nws_observation` expects the latest station observation GeoJSON shape. The
|
||||||
|
normalizer uses fields under `properties` such as `stationId`, `stationName`,
|
||||||
|
`timestamp`, `textDescription`, measured values, `presentWeather`, and
|
||||||
|
`cloudLayers`, plus point geometry for day/night inference.
|
||||||
|
|
||||||
|
`nws_alerts` expects an alerts FeatureCollection. The normalizer uses the
|
||||||
|
collection `updated` timestamp, `title`, each feature ID, alert classification
|
||||||
|
fields, narrative fields, timing fields, sender fields, and references.
|
||||||
|
|
||||||
|
`nws_forecast_hourly` and `nws_forecast_narrative` expect gridpoint forecast
|
||||||
|
GeoJSON with `properties.generatedAt`, `properties.updateTime`, elevation,
|
||||||
|
polygon geometry, and ordered `periods`.
|
||||||
|
|
||||||
|
`nws_forecast_discussion` expects an HTML page containing the discussion text in
|
||||||
|
a `<pre>` block. The provider helper extracts office identity, product, issue
|
||||||
|
time, update time, key messages, and short/long term sections.
|
||||||
|
|
||||||
|
`nws_weatherstories` expects a JSON response with a `stories` array. The
|
||||||
|
normalizer uses office ID, start/end/update times, title, description, alt text,
|
||||||
|
priority, order, and download URL.
|
||||||
|
|
||||||
|
## Accept Headers
|
||||||
|
|
||||||
|
NWS JSON sources request:
|
||||||
|
|
||||||
|
```text
|
||||||
|
application/geo+json, application/json
|
||||||
|
```
|
||||||
|
|
||||||
|
The forecast discussion source requests:
|
||||||
|
|
||||||
|
```text
|
||||||
|
text/html, application/xhtml+xml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Effective Time
|
||||||
|
|
||||||
|
Source events set `effective_at` from the best metadata available:
|
||||||
|
|
||||||
|
- observations: `properties.timestamp`;
|
||||||
|
- alerts: collection `updated`, otherwise latest per-alert timestamp;
|
||||||
|
- hourly and narrative forecasts: `properties.generatedAt`, otherwise update
|
||||||
|
time;
|
||||||
|
- forecast discussions: parsed issue time;
|
||||||
|
- weather stories: latest story update time, otherwise latest story start time.
|
||||||
|
|
||||||
|
Normalizers use canonical payload time as the normalized event effective time.
|
||||||
|
Alerts and weather stories fall back to the incoming event envelope when the
|
||||||
|
payload does not provide a better snapshot time.
|
||||||
|
|
||||||
|
## Mapping Notes
|
||||||
|
|
||||||
|
Observations preserve raw `presentWeather` fragments and infer WMO condition
|
||||||
|
codes from METAR phenomena, provider text, and cloud-layer fallback. Sea-level
|
||||||
|
pressure is preferred over barometric pressure when present.
|
||||||
|
|
||||||
|
Hourly forecasts infer WMO condition codes from `shortForecast` and icon tokens.
|
||||||
|
Narrative forecasts preserve text but intentionally leave period condition codes
|
||||||
|
unset. Forecast temperatures are converted to Celsius when NWS supplies
|
||||||
|
Fahrenheit, and wind speed strings are converted to kilometers per hour.
|
||||||
|
|
||||||
|
Alert timing fields are parsed best-effort. Invalid per-alert timestamps are
|
||||||
|
left unset rather than failing the whole alert run. Missing alert IDs are
|
||||||
|
synthesized from the run snapshot time and array position.
|
||||||
|
|
||||||
|
Forecast discussion parsing requires an issue time. Weather story entries require
|
||||||
|
start time, end time, and update time.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Constructor validation failures stop daemon startup. Polling failures are
|
||||||
|
returned to the scheduler. JSON sources still emit raw payloads when only
|
||||||
|
minimal metadata decoding fails. Forecast discussion polling fails if the HTML
|
||||||
|
cannot be parsed enough to determine the issue time.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/sources/nws/*_test.go`
|
||||||
|
- `internal/normalizers/nws/*_test.go`
|
||||||
|
- `internal/providers/nws/*_test.go`
|
||||||
|
- fixtures under `internal/providers/nws/testdata`
|
||||||
101
docs/integrations/openmeteo.md
Normal file
101
docs/integrations/openmeteo.md
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
# Open-Meteo Integration Notes
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document describes the Open-Meteo API usage currently implemented by
|
||||||
|
`weatherfeeder`. It is for developers and operators maintaining Open-Meteo
|
||||||
|
source URLs, normalizers, fixtures, and tests.
|
||||||
|
|
||||||
|
General config syntax belongs in [configuration](../config.md). Emitted JSON
|
||||||
|
events are documented in [event wire contract](events.md).
|
||||||
|
|
||||||
|
## Implemented Drivers
|
||||||
|
|
||||||
|
| Driver | Kind | Raw schema | Canonical schema |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `openmeteo_observation` | `observation` | `raw.openmeteo.current.v1` | `weather.observation.v1` |
|
||||||
|
| `openmeteo_forecast` | `forecast` | `raw.openmeteo.hourly.forecast.v1` | `weather.forecast.v1` |
|
||||||
|
|
||||||
|
## Config Requirements
|
||||||
|
|
||||||
|
Both drivers require HTTP source params:
|
||||||
|
|
||||||
|
- `url`
|
||||||
|
- `user_agent`
|
||||||
|
|
||||||
|
The shared HTTP helper also accepts `conditional`, `http_timeout`, and
|
||||||
|
`http_response_body_limit_bytes`. Conditional requests are enabled by default;
|
||||||
|
an upstream `304 Not Modified` response emits no event for that poll.
|
||||||
|
|
||||||
|
## Upstream Shapes Used
|
||||||
|
|
||||||
|
`openmeteo_observation` expects a JSON response with top-level location/timezone
|
||||||
|
metadata and a `current` object. The normalizer uses:
|
||||||
|
|
||||||
|
- `latitude`, `longitude`, `timezone`, `utc_offset_seconds`;
|
||||||
|
- `current.time`;
|
||||||
|
- current temperature, apparent temperature, relative humidity, weather code,
|
||||||
|
wind speed/direction/gusts, pressure, and `is_day`.
|
||||||
|
|
||||||
|
`openmeteo_forecast` expects top-level location/timezone metadata and an
|
||||||
|
array-oriented `hourly` object. The normalizer uses:
|
||||||
|
|
||||||
|
- `hourly.time`;
|
||||||
|
- hourly temperature, apparent temperature, dew point, relative humidity,
|
||||||
|
precipitation probability, precipitation amount, snowfall, weather code,
|
||||||
|
pressure, wind speed/direction/gusts, `is_day`, cloud cover, visibility, and
|
||||||
|
UV index.
|
||||||
|
|
||||||
|
Open-Meteo field presence is allowed to vary. Missing optional arrays produce
|
||||||
|
nil canonical fields for the affected periods.
|
||||||
|
|
||||||
|
## Accept Header
|
||||||
|
|
||||||
|
Open-Meteo sources request:
|
||||||
|
|
||||||
|
```text
|
||||||
|
application/json
|
||||||
|
```
|
||||||
|
|
||||||
|
## Time Handling
|
||||||
|
|
||||||
|
Open-Meteo timestamps often omit an explicit offset. The provider helper parses
|
||||||
|
times by using the returned `timezone` or `utc_offset_seconds` when needed.
|
||||||
|
|
||||||
|
Observation source events set `effective_at` from `current.time` when it can be
|
||||||
|
parsed. Hourly forecast source events prefer `current.time`, then the first
|
||||||
|
non-empty `hourly.time` entry.
|
||||||
|
|
||||||
|
The hourly forecast normalizer sets canonical `issuedAt` from the incoming event
|
||||||
|
`emitted_at` when present, otherwise from the first hourly period start.
|
||||||
|
Normalized forecast `effective_at` matches `issuedAt`.
|
||||||
|
|
||||||
|
## Mapping Notes
|
||||||
|
|
||||||
|
Open-Meteo is not a station feed. Weatherfeeder synthesizes canonical
|
||||||
|
station/location IDs from latitude and longitude when both are available.
|
||||||
|
|
||||||
|
Open-Meteo weather codes are WMO codes and are treated as authoritative.
|
||||||
|
Canonical text is derived from the WMO code and day/night hint.
|
||||||
|
|
||||||
|
Wind speed and gust fields are treated as kilometers per hour. Pressure values
|
||||||
|
are treated as hPa and converted to Pa. Snowfall values are treated as
|
||||||
|
centimeters and converted to millimeters.
|
||||||
|
|
||||||
|
Hourly forecast period end time is the next period start. The last period uses
|
||||||
|
the previous interval length, or one hour when there is no previous interval.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Constructor validation failures stop daemon startup. Polling failures are
|
||||||
|
returned to the scheduler. Metadata decoding failures in sources still allow raw
|
||||||
|
payload emission when the HTTP response itself succeeded.
|
||||||
|
|
||||||
|
Normalization fails when required time data is missing or invalid, such as an
|
||||||
|
empty `hourly.time` array for hourly forecasts.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/sources/openmeteo/source_test.go`
|
||||||
|
- `internal/normalizers/openmeteo/*_test.go`
|
||||||
|
- `internal/providers/openmeteo/*_test.go`
|
||||||
101
docs/integrations/openweather.md
Normal file
101
docs/integrations/openweather.md
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
# OpenWeather Integration Notes
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document describes the OpenWeather current-weather usage implemented by
|
||||||
|
`weatherfeeder`. It is for developers and operators maintaining OpenWeather
|
||||||
|
source URLs, normalizers, fixtures, and tests.
|
||||||
|
|
||||||
|
General config syntax belongs in [configuration](../config.md). Emitted JSON
|
||||||
|
events are documented in [event wire contract](events.md).
|
||||||
|
|
||||||
|
## Implemented Driver
|
||||||
|
|
||||||
|
| Driver | Kind | Raw schema | Canonical schema |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `openweather_observation` | `observation` | `raw.openweather.current.v1` | `weather.observation.v1` |
|
||||||
|
|
||||||
|
Only current-weather observation polling is registered for OpenWeather.
|
||||||
|
|
||||||
|
## Config Requirements
|
||||||
|
|
||||||
|
The driver requires HTTP source params:
|
||||||
|
|
||||||
|
- `url`
|
||||||
|
- `user_agent`
|
||||||
|
|
||||||
|
The shared HTTP helper also accepts `conditional`, `http_timeout`, and
|
||||||
|
`http_response_body_limit_bytes`. Conditional requests are enabled by default;
|
||||||
|
an upstream `304 Not Modified` response emits no event for that poll.
|
||||||
|
|
||||||
|
The configured URL must include:
|
||||||
|
|
||||||
|
```text
|
||||||
|
units=metric
|
||||||
|
```
|
||||||
|
|
||||||
|
Startup fails if `units` is omitted or set to another value. Keep OpenWeather
|
||||||
|
API keys out of committed configs. Use local config management or deployment
|
||||||
|
secrets for the `appid` query parameter.
|
||||||
|
|
||||||
|
## Upstream Shape Used
|
||||||
|
|
||||||
|
The source emits the full current-weather JSON payload as a raw event. The
|
||||||
|
normalizer uses:
|
||||||
|
|
||||||
|
- `coord.lat`, `coord.lon`;
|
||||||
|
- primary `weather[0]` condition ID, description, and icon;
|
||||||
|
- `main.temp`, `main.feels_like`, `main.pressure`, `main.humidity`, and
|
||||||
|
optional `main.sea_level`;
|
||||||
|
- `visibility`;
|
||||||
|
- `wind.speed`, `wind.deg`, and `wind.gust`;
|
||||||
|
- `dt`;
|
||||||
|
- `sys.sunrise` and `sys.sunset`;
|
||||||
|
- `id` and `name`.
|
||||||
|
|
||||||
|
## Accept Header
|
||||||
|
|
||||||
|
OpenWeather sources request:
|
||||||
|
|
||||||
|
```text
|
||||||
|
application/json
|
||||||
|
```
|
||||||
|
|
||||||
|
## Time Handling
|
||||||
|
|
||||||
|
Source events set `effective_at` from `dt` when it is present and positive.
|
||||||
|
The normalizer also uses `dt` as the canonical observation timestamp and
|
||||||
|
normalized effective time.
|
||||||
|
|
||||||
|
## Mapping Notes
|
||||||
|
|
||||||
|
Metric units are required so canonical unit conversion is deterministic:
|
||||||
|
|
||||||
|
- `main.temp` and `main.feels_like` are treated as Celsius;
|
||||||
|
- `wind.speed` and `wind.gust` are treated as meters per second and converted to
|
||||||
|
kilometers per hour;
|
||||||
|
- pressure values are treated as hPa and converted to Pa.
|
||||||
|
|
||||||
|
The primary condition is `weather[0]`. OpenWeather condition IDs are mapped into
|
||||||
|
the canonical WMO code vocabulary. The human text description is preserved from
|
||||||
|
the provider description.
|
||||||
|
|
||||||
|
Day/night is inferred from the OpenWeather icon suffix when available, otherwise
|
||||||
|
from sunrise and sunset bounds.
|
||||||
|
|
||||||
|
The station ID uses the OpenWeather city ID when present. If no city ID is
|
||||||
|
present, weatherfeeder synthesizes an ID from coordinates. The station name uses
|
||||||
|
the provider `name`, falling back to `OpenWeatherMap` when blank.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Constructor validation failures stop daemon startup. Polling also re-checks the
|
||||||
|
metric-unit requirement before fetching. HTTP failures are returned to the
|
||||||
|
scheduler. Metadata decoding failures in the source still allow raw payload
|
||||||
|
emission when the HTTP response itself succeeded.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/sources/openweather/source_test.go`
|
||||||
|
- `internal/normalizers/openweather/*_test.go`
|
||||||
|
- `internal/providers/openweather/*_test.go`
|
||||||
476
docs/integrations/postgres.md
Normal file
476
docs/integrations/postgres.md
Normal file
@@ -0,0 +1,476 @@
|
|||||||
|
# Postgres Integration
|
||||||
|
|
||||||
|
This document is the canonical table contract for the optional `postgres` sink.
|
||||||
|
It describes the schema created and written by weatherfeeder through feedkit's
|
||||||
|
Postgres sink.
|
||||||
|
|
||||||
|
Configure the sink as described in [configuration](../config.md#postgres).
|
||||||
|
|
||||||
|
## Initialization And Writes
|
||||||
|
|
||||||
|
At startup, each configured Postgres sink opens the database and runs
|
||||||
|
`CREATE TABLE IF NOT EXISTS` for every weatherfeeder table, followed by
|
||||||
|
`CREATE INDEX IF NOT EXISTS` for every configured index.
|
||||||
|
|
||||||
|
This initialization creates missing tables and indexes only. It does not alter
|
||||||
|
existing tables, migrate column definitions, drop old objects, or backfill data.
|
||||||
|
Schema changes require operator-managed database migration.
|
||||||
|
|
||||||
|
Events are mapped only for canonical weather schemas:
|
||||||
|
|
||||||
|
- `weather.observation.v1`
|
||||||
|
- `weather.forecast.v1`
|
||||||
|
- `weather.forecast_discussion.v1`
|
||||||
|
- `weather.weather_story.v1`
|
||||||
|
- `weather.alert.v1`
|
||||||
|
- `weather.outlook.v1`
|
||||||
|
|
||||||
|
Unsupported schemas produce no writes for this sink. Mapped events are inserted
|
||||||
|
transactionally. Inserts use ordinary `INSERT`; duplicate primary keys fail the
|
||||||
|
write.
|
||||||
|
|
||||||
|
## Shared Envelope Columns
|
||||||
|
|
||||||
|
Parent tables store the feed event envelope:
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
|
||||||
|
## Table Overview
|
||||||
|
|
||||||
|
| Table | Primary key | Prune column |
|
||||||
|
|---|---|---|
|
||||||
|
| `observations` | `event_id` | `observed_at` |
|
||||||
|
| `observation_present_weather` | `event_id`, `weather_index` | `observed_at` |
|
||||||
|
| `forecasts` | `event_id` | `issued_at` |
|
||||||
|
| `forecast_periods` | `run_event_id`, `period_index` | `issued_at` |
|
||||||
|
| `forecast_discussions` | `event_id` | `issued_at` |
|
||||||
|
| `forecast_discussion_key_messages` | `run_event_id`, `message_index` | `issued_at` |
|
||||||
|
| `weather_story_runs` | `event_id` | `as_of` |
|
||||||
|
| `weather_stories` | `run_event_id`, `story_index` | `as_of` |
|
||||||
|
| `alert_runs` | `event_id` | `as_of` |
|
||||||
|
| `alerts` | `run_event_id`, `alert_index` | `as_of` |
|
||||||
|
| `alert_references` | `run_event_id`, `alert_index`, `reference_index` | `as_of` |
|
||||||
|
| `outlook_runs` | `event_id` | `as_of` |
|
||||||
|
| `outlooks` | `run_event_id`, `outlook_index` | `as_of` |
|
||||||
|
|
||||||
|
## Table Contract
|
||||||
|
|
||||||
|
### `observations`
|
||||||
|
|
||||||
|
Primary key: `event_id`
|
||||||
|
|
||||||
|
Prune column: `observed_at`
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_obs_station_observed_at` on `station_id`, `observed_at`
|
||||||
|
- `idx_wf_obs_observed_at` on `observed_at`
|
||||||
|
- `idx_wf_obs_condition_code` on `condition_code`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
| `station_id` | `TEXT` | yes | `payload.stationId` |
|
||||||
|
| `station_name` | `TEXT` | yes | `payload.stationName` |
|
||||||
|
| `observed_at` | `TIMESTAMPTZ` | no | `payload.timestamp` |
|
||||||
|
| `condition_code` | `INTEGER` | no | `payload.conditionCode` |
|
||||||
|
| `is_day` | `BOOLEAN` | yes | `payload.isDay` |
|
||||||
|
| `text_description` | `TEXT` | yes | `payload.textDescription` |
|
||||||
|
| `temperature_c` | `DOUBLE PRECISION` | yes | `payload.temperatureC` |
|
||||||
|
| `dewpoint_c` | `DOUBLE PRECISION` | yes | `payload.dewpointC` |
|
||||||
|
| `wind_direction_degrees` | `DOUBLE PRECISION` | yes | `payload.windDirectionDegrees` |
|
||||||
|
| `wind_speed_kmh` | `DOUBLE PRECISION` | yes | `payload.windSpeedKmh` |
|
||||||
|
| `wind_gust_kmh` | `DOUBLE PRECISION` | yes | `payload.windGustKmh` |
|
||||||
|
| `barometric_pressure_pa` | `DOUBLE PRECISION` | yes | `payload.barometricPressurePa` |
|
||||||
|
| `visibility_meters` | `DOUBLE PRECISION` | yes | `payload.visibilityMeters` |
|
||||||
|
| `relative_humidity_percent` | `DOUBLE PRECISION` | yes | `payload.relativeHumidityPercent` |
|
||||||
|
| `apparent_temperature_c` | `DOUBLE PRECISION` | yes | `payload.apparentTemperatureC` |
|
||||||
|
|
||||||
|
### `observation_present_weather`
|
||||||
|
|
||||||
|
Primary key: `event_id`, `weather_index`
|
||||||
|
|
||||||
|
Prune column: `observed_at`
|
||||||
|
|
||||||
|
Foreign key: `event_id` references `observations(event_id)` with cascade delete.
|
||||||
|
|
||||||
|
Index: `idx_wf_obs_present_observed_at` on `observed_at`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT REFERENCES observations(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `weather_index` | `INTEGER` | no | `payload.presentWeather[]` index. |
|
||||||
|
| `observed_at` | `TIMESTAMPTZ` | no | `payload.timestamp` |
|
||||||
|
| `raw_text` | `TEXT` | yes | Compact JSON text from `payload.presentWeather[].raw` |
|
||||||
|
|
||||||
|
### `forecasts`
|
||||||
|
|
||||||
|
Primary key: `event_id`
|
||||||
|
|
||||||
|
Prune column: `issued_at`
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_fc_location_product_issued_at` on `location_id`, `product`, `issued_at`
|
||||||
|
- `idx_wf_fc_issued_at` on `issued_at`
|
||||||
|
- `idx_wf_fc_product_issued_at` on `product`, `issued_at`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
| `location_id` | `TEXT` | yes | `payload.locationId` |
|
||||||
|
| `location_name` | `TEXT` | yes | `payload.locationName` |
|
||||||
|
| `issued_at` | `TIMESTAMPTZ` | no | `payload.issuedAt` |
|
||||||
|
| `updated_at` | `TIMESTAMPTZ` | yes | `payload.updatedAt` |
|
||||||
|
| `product` | `TEXT` | no | `payload.product` |
|
||||||
|
| `latitude` | `DOUBLE PRECISION` | yes | `payload.latitude` |
|
||||||
|
| `longitude` | `DOUBLE PRECISION` | yes | `payload.longitude` |
|
||||||
|
| `elevation_meters` | `DOUBLE PRECISION` | yes | `payload.elevationMeters` |
|
||||||
|
| `period_count` | `INTEGER` | no | `len(payload.periods)` |
|
||||||
|
|
||||||
|
### `forecast_periods`
|
||||||
|
|
||||||
|
Primary key: `run_event_id`, `period_index`
|
||||||
|
|
||||||
|
Prune column: `issued_at`
|
||||||
|
|
||||||
|
Foreign key: `run_event_id` references `forecasts(event_id)` with cascade delete.
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_fc_period_start_time` on `start_time`
|
||||||
|
- `idx_wf_fc_period_end_time` on `end_time`
|
||||||
|
- `idx_wf_fc_period_run_start` on `run_event_id`, `start_time`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `run_event_id` | `TEXT REFERENCES forecasts(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `period_index` | `INTEGER` | no | `payload.periods[]` index. |
|
||||||
|
| `issued_at` | `TIMESTAMPTZ` | no | Parent `payload.issuedAt` |
|
||||||
|
| `start_time` | `TIMESTAMPTZ` | no | `payload.periods[].startTime` |
|
||||||
|
| `end_time` | `TIMESTAMPTZ` | no | `payload.periods[].endTime` |
|
||||||
|
| `name` | `TEXT` | yes | `payload.periods[].name` |
|
||||||
|
| `is_day` | `BOOLEAN` | yes | `payload.periods[].isDay` |
|
||||||
|
| `condition_code` | `INTEGER` | yes | `payload.periods[].conditionCode` |
|
||||||
|
| `text_description` | `TEXT` | yes | `payload.periods[].textDescription` |
|
||||||
|
| `temperature_c` | `DOUBLE PRECISION` | yes | `payload.periods[].temperatureC` |
|
||||||
|
| `temperature_c_min` | `DOUBLE PRECISION` | yes | `payload.periods[].temperatureCMin` |
|
||||||
|
| `temperature_c_max` | `DOUBLE PRECISION` | yes | `payload.periods[].temperatureCMax` |
|
||||||
|
| `dewpoint_c` | `DOUBLE PRECISION` | yes | `payload.periods[].dewpointC` |
|
||||||
|
| `relative_humidity_percent` | `DOUBLE PRECISION` | yes | `payload.periods[].relativeHumidityPercent` |
|
||||||
|
| `wind_direction_degrees` | `DOUBLE PRECISION` | yes | `payload.periods[].windDirectionDegrees` |
|
||||||
|
| `wind_speed_kmh` | `DOUBLE PRECISION` | yes | `payload.periods[].windSpeedKmh` |
|
||||||
|
| `wind_gust_kmh` | `DOUBLE PRECISION` | yes | `payload.periods[].windGustKmh` |
|
||||||
|
| `barometric_pressure_pa` | `DOUBLE PRECISION` | yes | `payload.periods[].barometricPressurePa` |
|
||||||
|
| `visibility_meters` | `DOUBLE PRECISION` | yes | `payload.periods[].visibilityMeters` |
|
||||||
|
| `apparent_temperature_c` | `DOUBLE PRECISION` | yes | `payload.periods[].apparentTemperatureC` |
|
||||||
|
| `cloud_cover_percent` | `DOUBLE PRECISION` | yes | `payload.periods[].cloudCoverPercent` |
|
||||||
|
| `probability_of_precipitation_percent` | `DOUBLE PRECISION` | yes | `payload.periods[].probabilityOfPrecipitationPercent` |
|
||||||
|
| `precipitation_amount_mm` | `DOUBLE PRECISION` | yes | `payload.periods[].precipitationAmountMm` |
|
||||||
|
| `snowfall_depth_mm` | `DOUBLE PRECISION` | yes | `payload.periods[].snowfallDepthMm` |
|
||||||
|
| `uv_index` | `DOUBLE PRECISION` | yes | `payload.periods[].uvIndex` |
|
||||||
|
|
||||||
|
### `forecast_discussions`
|
||||||
|
|
||||||
|
Primary key: `event_id`
|
||||||
|
|
||||||
|
Prune column: `issued_at`
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_discussion_office_product_issued_at` on `office_id`, `product`, `issued_at`
|
||||||
|
- `idx_wf_discussion_issued_at` on `issued_at`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
| `office_id` | `TEXT` | yes | `payload.officeId` |
|
||||||
|
| `office_name` | `TEXT` | yes | `payload.officeName` |
|
||||||
|
| `issued_at` | `TIMESTAMPTZ` | no | `payload.issuedAt` |
|
||||||
|
| `updated_at` | `TIMESTAMPTZ` | yes | `payload.updatedAt` |
|
||||||
|
| `product` | `TEXT` | no | `payload.product` |
|
||||||
|
| `short_term_qualifier` | `TEXT` | yes | `payload.shortTerm.qualifier` |
|
||||||
|
| `short_term_issued_at` | `TIMESTAMPTZ` | yes | `payload.shortTerm.issuedAt` |
|
||||||
|
| `short_term_text` | `TEXT` | yes | `payload.shortTerm.text` |
|
||||||
|
| `long_term_qualifier` | `TEXT` | yes | `payload.longTerm.qualifier` |
|
||||||
|
| `long_term_issued_at` | `TIMESTAMPTZ` | yes | `payload.longTerm.issuedAt` |
|
||||||
|
| `long_term_text` | `TEXT` | yes | `payload.longTerm.text` |
|
||||||
|
| `key_message_count` | `INTEGER` | no | `len(payload.keyMessages)` |
|
||||||
|
|
||||||
|
### `forecast_discussion_key_messages`
|
||||||
|
|
||||||
|
Primary key: `run_event_id`, `message_index`
|
||||||
|
|
||||||
|
Prune column: `issued_at`
|
||||||
|
|
||||||
|
Foreign key: `run_event_id` references `forecast_discussions(event_id)` with
|
||||||
|
cascade delete.
|
||||||
|
|
||||||
|
Index: `idx_wf_discussion_message_issued_at` on `issued_at`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `run_event_id` | `TEXT REFERENCES forecast_discussions(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `message_index` | `INTEGER` | no | `payload.keyMessages[]` index. |
|
||||||
|
| `issued_at` | `TIMESTAMPTZ` | no | Parent `payload.issuedAt` |
|
||||||
|
| `message_text` | `TEXT` | yes | `payload.keyMessages[]` value |
|
||||||
|
|
||||||
|
### `weather_story_runs`
|
||||||
|
|
||||||
|
Primary key: `event_id`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_story_run_office_as_of` on `office_id`, `as_of`
|
||||||
|
- `idx_wf_story_run_as_of` on `as_of`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
| `office_id` | `TEXT` | yes | `payload.officeId` |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | `payload.asOf` |
|
||||||
|
| `story_count` | `INTEGER` | no | `len(payload.stories)` |
|
||||||
|
|
||||||
|
### `weather_stories`
|
||||||
|
|
||||||
|
Primary key: `run_event_id`, `story_index`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Foreign key: `run_event_id` references `weather_story_runs(event_id)` with
|
||||||
|
cascade delete.
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_stories_start_time` on `start_time`
|
||||||
|
- `idx_wf_stories_end_time` on `end_time`
|
||||||
|
- `idx_wf_stories_updated_at` on `updated_at`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `run_event_id` | `TEXT REFERENCES weather_story_runs(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `story_index` | `INTEGER` | no | `payload.stories[]` index. |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | Parent `payload.asOf` |
|
||||||
|
| `office_id` | `TEXT` | yes | `payload.stories[].officeId` |
|
||||||
|
| `start_time` | `TIMESTAMPTZ` | no | `payload.stories[].startTime` |
|
||||||
|
| `end_time` | `TIMESTAMPTZ` | no | `payload.stories[].endTime` |
|
||||||
|
| `updated_at` | `TIMESTAMPTZ` | no | `payload.stories[].updatedAt` |
|
||||||
|
| `title` | `TEXT` | yes | `payload.stories[].title` |
|
||||||
|
| `description` | `TEXT` | yes | `payload.stories[].description` |
|
||||||
|
| `alt_text` | `TEXT` | yes | `payload.stories[].altText` |
|
||||||
|
| `priority` | `BOOLEAN` | no | `payload.stories[].priority` |
|
||||||
|
| `story_order` | `INTEGER` | no | `payload.stories[].order` |
|
||||||
|
| `download_url` | `TEXT` | yes | `payload.stories[].downloadUrl` |
|
||||||
|
|
||||||
|
### `alert_runs`
|
||||||
|
|
||||||
|
Primary key: `event_id`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_alert_run_location_as_of` on `location_id`, `as_of`
|
||||||
|
- `idx_wf_alert_run_as_of` on `as_of`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
| `location_id` | `TEXT` | yes | `payload.locationId` |
|
||||||
|
| `location_name` | `TEXT` | yes | `payload.locationName` |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | `payload.asOf` |
|
||||||
|
| `latitude` | `DOUBLE PRECISION` | yes | `payload.latitude` |
|
||||||
|
| `longitude` | `DOUBLE PRECISION` | yes | `payload.longitude` |
|
||||||
|
| `alert_count` | `INTEGER` | no | `len(payload.alerts)` |
|
||||||
|
|
||||||
|
### `alerts`
|
||||||
|
|
||||||
|
Primary key: `run_event_id`, `alert_index`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Foreign key: `run_event_id` references `alert_runs(event_id)` with cascade
|
||||||
|
delete.
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_alerts_alert_id` on `alert_id`
|
||||||
|
- `idx_wf_alerts_severity_expires` on `severity`, `expires`
|
||||||
|
- `idx_wf_alerts_as_of` on `as_of`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `run_event_id` | `TEXT REFERENCES alert_runs(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `alert_index` | `INTEGER` | no | `payload.alerts[]` index. |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | Parent `payload.asOf` |
|
||||||
|
| `alert_id` | `TEXT` | no | `payload.alerts[].id` |
|
||||||
|
| `event` | `TEXT` | yes | `payload.alerts[].event` |
|
||||||
|
| `headline` | `TEXT` | yes | `payload.alerts[].headline` |
|
||||||
|
| `severity` | `TEXT` | yes | `payload.alerts[].severity` |
|
||||||
|
| `urgency` | `TEXT` | yes | `payload.alerts[].urgency` |
|
||||||
|
| `certainty` | `TEXT` | yes | `payload.alerts[].certainty` |
|
||||||
|
| `status` | `TEXT` | yes | `payload.alerts[].status` |
|
||||||
|
| `message_type` | `TEXT` | yes | `payload.alerts[].messageType` |
|
||||||
|
| `category` | `TEXT` | yes | `payload.alerts[].category` |
|
||||||
|
| `response` | `TEXT` | yes | `payload.alerts[].response` |
|
||||||
|
| `description` | `TEXT` | yes | `payload.alerts[].description` |
|
||||||
|
| `instruction` | `TEXT` | yes | `payload.alerts[].instruction` |
|
||||||
|
| `sent` | `TIMESTAMPTZ` | yes | `payload.alerts[].sent` |
|
||||||
|
| `effective` | `TIMESTAMPTZ` | yes | `payload.alerts[].effective` |
|
||||||
|
| `onset` | `TIMESTAMPTZ` | yes | `payload.alerts[].onset` |
|
||||||
|
| `expires` | `TIMESTAMPTZ` | yes | `payload.alerts[].expires` |
|
||||||
|
| `area_description` | `TEXT` | yes | `payload.alerts[].areaDescription` |
|
||||||
|
| `sender_name` | `TEXT` | yes | `payload.alerts[].senderName` |
|
||||||
|
| `reference_count` | `INTEGER` | no | `len(payload.alerts[].references)` |
|
||||||
|
|
||||||
|
### `alert_references`
|
||||||
|
|
||||||
|
Primary key: `run_event_id`, `alert_index`, `reference_index`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Foreign key: `run_event_id` references `alert_runs(event_id)` with cascade
|
||||||
|
delete.
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_alert_refs_as_of` on `as_of`
|
||||||
|
- `idx_wf_alert_refs_sent` on `sent`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `run_event_id` | `TEXT REFERENCES alert_runs(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `alert_index` | `INTEGER` | no | Parent alert index. |
|
||||||
|
| `reference_index` | `INTEGER` | no | `payload.alerts[].references[]` index. |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | Parent `payload.asOf` |
|
||||||
|
| `id` | `TEXT` | yes | `payload.alerts[].references[].id` |
|
||||||
|
| `identifier` | `TEXT` | yes | `payload.alerts[].references[].identifier` |
|
||||||
|
| `sender` | `TEXT` | yes | `payload.alerts[].references[].sender` |
|
||||||
|
| `sent` | `TIMESTAMPTZ` | yes | `payload.alerts[].references[].sent` |
|
||||||
|
|
||||||
|
### `outlook_runs`
|
||||||
|
|
||||||
|
Primary key: `event_id`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_outlook_run_location_as_of` on `location_id`, `as_of`
|
||||||
|
- `idx_wf_outlook_run_as_of` on `as_of`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `event_id` | `TEXT` | no | `event.id` |
|
||||||
|
| `event_kind` | `TEXT` | no | `event.kind` |
|
||||||
|
| `event_source` | `TEXT` | no | `event.source` |
|
||||||
|
| `event_schema` | `TEXT` | no | `event.schema` |
|
||||||
|
| `event_emitted_at` | `TIMESTAMPTZ` | no | `event.emitted_at` |
|
||||||
|
| `event_effective_at` | `TIMESTAMPTZ` | yes | `event.effective_at` |
|
||||||
|
| `location_id` | `TEXT` | yes | `payload.locationId` |
|
||||||
|
| `location_name` | `TEXT` | yes | `payload.locationName` |
|
||||||
|
| `latitude` | `DOUBLE PRECISION` | yes | `payload.latitude` |
|
||||||
|
| `longitude` | `DOUBLE PRECISION` | yes | `payload.longitude` |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | `payload.asOf` |
|
||||||
|
| `issued_at` | `TIMESTAMPTZ` | yes | `payload.issuedAt` |
|
||||||
|
| `outlook_count` | `INTEGER` | no | `len(payload.outlooks)` |
|
||||||
|
|
||||||
|
### `outlooks`
|
||||||
|
|
||||||
|
Primary key: `run_event_id`, `outlook_index`
|
||||||
|
|
||||||
|
Prune column: `as_of`
|
||||||
|
|
||||||
|
Foreign key: `run_event_id` references `outlook_runs(event_id)` with cascade
|
||||||
|
delete.
|
||||||
|
|
||||||
|
Indexes:
|
||||||
|
|
||||||
|
- `idx_wf_outlooks_contains_valid` on `contains_location`, `valid_from`, `valid_to`
|
||||||
|
- `idx_wf_outlooks_day_type_label` on `day`, `outlook_type`, `label`
|
||||||
|
- `idx_wf_outlooks_valid` on `valid_from`, `valid_to`
|
||||||
|
|
||||||
|
| Column | Type | Null | Source |
|
||||||
|
|---|---|:---:|---|
|
||||||
|
| `run_event_id` | `TEXT REFERENCES outlook_runs(event_id) ON DELETE CASCADE` | no | Parent event ID. |
|
||||||
|
| `outlook_index` | `INTEGER` | no | `payload.outlooks[]` index. |
|
||||||
|
| `as_of` | `TIMESTAMPTZ` | no | Parent `payload.asOf` |
|
||||||
|
| `outlook_id` | `TEXT` | no | `payload.outlooks[].id` |
|
||||||
|
| `provider` | `TEXT` | no | `payload.outlooks[].provider` |
|
||||||
|
| `product` | `TEXT` | no | `payload.outlooks[].product` |
|
||||||
|
| `day` | `INTEGER` | no | `payload.outlooks[].day` |
|
||||||
|
| `outlook_type` | `TEXT` | no | `payload.outlooks[].outlookType` |
|
||||||
|
| `label` | `TEXT` | no | `payload.outlooks[].label` |
|
||||||
|
| `label_text` | `TEXT` | yes | `payload.outlooks[].labelText` |
|
||||||
|
| `severity_rank` | `INTEGER` | yes | `payload.outlooks[].severityRank` |
|
||||||
|
| `valid_from` | `TIMESTAMPTZ` | no | `payload.outlooks[].validFrom` |
|
||||||
|
| `valid_to` | `TIMESTAMPTZ` | no | `payload.outlooks[].validTo` |
|
||||||
|
| `issued_at` | `TIMESTAMPTZ` | no | `payload.outlooks[].issuedAt` |
|
||||||
|
| `expires_at` | `TIMESTAMPTZ` | no | `payload.outlooks[].expiresAt` |
|
||||||
|
| `forecaster` | `TEXT` | yes | `payload.outlooks[].forecaster` |
|
||||||
|
| `headline` | `TEXT` | yes | `payload.outlooks[].headline` |
|
||||||
|
| `summary` | `TEXT` | yes | `payload.outlooks[].summary` |
|
||||||
|
| `discussion` | `TEXT` | yes | `payload.outlooks[].discussion` |
|
||||||
|
| `source_url` | `TEXT` | yes | `payload.outlooks[].sourceUrl` |
|
||||||
|
| `image_url` | `TEXT` | yes | `payload.outlooks[].imageUrl` |
|
||||||
|
| `contains_location` | `BOOLEAN` | no | `payload.outlooks[].containsLocation` |
|
||||||
|
| `geometry_json` | `TEXT` | no | Compact JSON from `payload.outlooks[].geometry` |
|
||||||
|
|
||||||
|
## Retention
|
||||||
|
|
||||||
|
When sink param `prune` is set, every successful write transaction deletes rows
|
||||||
|
older than `now - prune` from every table using that table's prune column.
|
||||||
|
|
||||||
|
The sink also exposes manual prune helpers in code, but the `weatherfeeder`
|
||||||
|
binary does not provide CLI commands for them.
|
||||||
|
|
||||||
|
## Reconstructing Canonical Payloads
|
||||||
|
|
||||||
|
- `WeatherObservation`: read `observations`, then join
|
||||||
|
`observation_present_weather` by `event_id` ordered by `weather_index`.
|
||||||
|
- `WeatherForecastRun`: read `forecasts`, then join `forecast_periods` by
|
||||||
|
`run_event_id` ordered by `period_index`.
|
||||||
|
- `WeatherForecastDiscussion`: read `forecast_discussions`, then join
|
||||||
|
`forecast_discussion_key_messages` by `run_event_id` ordered by
|
||||||
|
`message_index`.
|
||||||
|
- `WeatherStoryRun`: read `weather_story_runs`, then join `weather_stories` by
|
||||||
|
`run_event_id` ordered by `story_index`.
|
||||||
|
- `WeatherAlertRun`: read `alert_runs`, join `alerts` by `run_event_id` ordered
|
||||||
|
by `alert_index`, then join `alert_references` by `run_event_id` and
|
||||||
|
`alert_index` ordered by `reference_index`.
|
||||||
|
- `WeatherOutlookRun`: read `outlook_runs`, then join `outlooks` by
|
||||||
|
`run_event_id` ordered by `outlook_index`.
|
||||||
134
docs/integrations/spc.md
Normal file
134
docs/integrations/spc.md
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
# SPC Integration Notes
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document describes the Storm Prediction Center convective outlook usage
|
||||||
|
implemented by `weatherfeeder`. It is for developers and operators maintaining
|
||||||
|
SPC source configuration, provider helpers, fixtures, and tests.
|
||||||
|
|
||||||
|
General config syntax belongs in [configuration](../config.md). Emitted JSON
|
||||||
|
events are documented in [event wire contract](events.md).
|
||||||
|
|
||||||
|
## Implemented Driver
|
||||||
|
|
||||||
|
| Driver | Kind | Raw schema | Canonical schema |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `spc_convective_outlook` | `outlook` | `raw.spc.convective_outlook.v1` | `weather.outlook.v1` |
|
||||||
|
|
||||||
|
## Config Requirements
|
||||||
|
|
||||||
|
The driver requires:
|
||||||
|
|
||||||
|
- `latitude`
|
||||||
|
- `longitude`
|
||||||
|
- `user_agent`
|
||||||
|
|
||||||
|
Optional params are:
|
||||||
|
|
||||||
|
- `location_id`
|
||||||
|
- `location_name`
|
||||||
|
- `geojson_urls`
|
||||||
|
- `discussion_urls`
|
||||||
|
- `rss_url`
|
||||||
|
- `http_timeout`
|
||||||
|
- `http_response_body_limit_bytes`
|
||||||
|
|
||||||
|
RSS is not fetched unless `rss_url` is configured. URL override maps are intended
|
||||||
|
for tests and upstream URL changes; the default driver configuration owns the
|
||||||
|
current Day 1-3 SPC product URLs.
|
||||||
|
|
||||||
|
## Upstream Products Used
|
||||||
|
|
||||||
|
The source fetches twelve required GeoJSON products every poll:
|
||||||
|
|
||||||
|
- Day 1 categorical, tornado, hail, and wind
|
||||||
|
- Day 2 categorical, tornado, hail, and wind
|
||||||
|
- Day 3 categorical, tornado, hail, and wind
|
||||||
|
|
||||||
|
It also fetches three required print pages:
|
||||||
|
|
||||||
|
- Day 1 convective outlook print page
|
||||||
|
- Day 2 convective outlook print page
|
||||||
|
- Day 3 convective outlook print page
|
||||||
|
|
||||||
|
GeoJSON products are authoritative for outlook polygons, valid windows, issue
|
||||||
|
times, labels, and severity rank. Print pages are authoritative for headline,
|
||||||
|
summary, and discussion text.
|
||||||
|
|
||||||
|
## Accept Headers
|
||||||
|
|
||||||
|
GeoJSON requests use:
|
||||||
|
|
||||||
|
```text
|
||||||
|
application/geo+json, application/json
|
||||||
|
```
|
||||||
|
|
||||||
|
Print-page requests use:
|
||||||
|
|
||||||
|
```text
|
||||||
|
text/html, application/xhtml+xml
|
||||||
|
```
|
||||||
|
|
||||||
|
RSS requests, when configured, use:
|
||||||
|
|
||||||
|
```text
|
||||||
|
application/rss+xml, application/xml, text/xml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Polling And Raw Events
|
||||||
|
|
||||||
|
The source polls all required GeoJSON and print-page products as one bundle. If
|
||||||
|
any required request fails or returns a non-2xx response, the poll returns an
|
||||||
|
error and emits no partial event.
|
||||||
|
|
||||||
|
The raw payload contains fetched bodies plus configured location metadata and
|
||||||
|
per-product metadata. The source parses only the timestamp metadata needed for
|
||||||
|
event effective time selection; canonical mapping belongs to the normalizer.
|
||||||
|
|
||||||
|
The source emits no event when a complete fetched bundle is unchanged from the
|
||||||
|
previous successful poll. It does this with a source-local hash of the fetched
|
||||||
|
document bodies.
|
||||||
|
|
||||||
|
## Time Handling
|
||||||
|
|
||||||
|
Raw source `effective_at` prefers:
|
||||||
|
|
||||||
|
1. the latest valid GeoJSON `ISSUE_ISO`;
|
||||||
|
2. the latest print-page `Updated:` timestamp;
|
||||||
|
3. RSS `lastBuildDate` when RSS was fetched and parseable;
|
||||||
|
4. fetch time.
|
||||||
|
|
||||||
|
The normalizer sets canonical `asOf` and normalized event `effective_at` from
|
||||||
|
the latest valid outlook feature `issuedAt`, with fallback to print-page update
|
||||||
|
time and then the incoming event envelope.
|
||||||
|
|
||||||
|
## Mapping Notes
|
||||||
|
|
||||||
|
Each GeoJSON feature becomes one canonical outlook. Products are ordered by day,
|
||||||
|
then categorical, tornado, hail, and wind. Feature order is preserved within
|
||||||
|
each product.
|
||||||
|
|
||||||
|
The normalizer computes `containsLocation` with the configured latitude and
|
||||||
|
longitude against compact GeoJSON `Polygon` or `MultiPolygon` geometry.
|
||||||
|
Coordinates use GeoJSON order, `[longitude, latitude]`, and boundary points
|
||||||
|
count as contained.
|
||||||
|
|
||||||
|
All outlook polygons are preserved, including polygons that do not contain the
|
||||||
|
configured point. Matching print-page headline, summary, and discussion text is
|
||||||
|
attached to every outlook for the same day.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Constructor validation failures stop daemon startup. Polling failures are
|
||||||
|
returned to the scheduler and emit no event for that poll.
|
||||||
|
|
||||||
|
Normalization fails when required GeoJSON timestamps, labels, geometry, or
|
||||||
|
configured coordinates are missing or invalid. Print-page extraction failures
|
||||||
|
also fail normalization because print pages are required inputs.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/providers/spc/*_test.go`
|
||||||
|
- `internal/sources/spc/*_test.go`
|
||||||
|
- `internal/normalizers/spc/*_test.go`
|
||||||
|
- fixtures under `internal/providers/spc/testdata`
|
||||||
101
docs/internal/normalizers.md
Normal file
101
docs/internal/normalizers.md
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
# Normalizer Internals
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
Normalizers convert raw provider events into canonical weather events. They are
|
||||||
|
weather-domain mapping code and should stay independent of runtime wiring,
|
||||||
|
source polling, and sink persistence.
|
||||||
|
|
||||||
|
Detailed package conventions live in `internal/normalizers/doc.go`.
|
||||||
|
|
||||||
|
## Inputs And Outputs
|
||||||
|
|
||||||
|
Inputs are raw feed events whose schemas identify provider payload shape.
|
||||||
|
Outputs are canonical feed events using `model` payloads and `weather.*`
|
||||||
|
schemas.
|
||||||
|
|
||||||
|
Current mappings:
|
||||||
|
|
||||||
|
| Raw schema | Canonical schema |
|
||||||
|
| --- | --- |
|
||||||
|
| `raw.nws.observation.v1` | `weather.observation.v1` |
|
||||||
|
| `raw.openmeteo.current.v1` | `weather.observation.v1` |
|
||||||
|
| `raw.openweather.current.v1` | `weather.observation.v1` |
|
||||||
|
| `raw.nws.hourly.forecast.v1` | `weather.forecast.v1` |
|
||||||
|
| `raw.nws.narrative.forecast.v1` | `weather.forecast.v1` |
|
||||||
|
| `raw.openmeteo.hourly.forecast.v1` | `weather.forecast.v1` |
|
||||||
|
| `raw.nws.forecast_discussion.v1` | `weather.forecast_discussion.v1` |
|
||||||
|
| `raw.nws.weatherstories.v1` | `weather.weather_story.v1` |
|
||||||
|
| `raw.nws.alerts.v1` | `weather.alert.v1` |
|
||||||
|
| `raw.spc.convective_outlook.v1` | `weather.outlook.v1` |
|
||||||
|
|
||||||
|
## Boundaries
|
||||||
|
|
||||||
|
- Normalizers match by `Event.Schema`.
|
||||||
|
- Normalizers decode raw payloads into provider structs.
|
||||||
|
- Normalizers map provider data into canonical `model` payloads.
|
||||||
|
- Normalizers do not fetch network data, read config, route events, or write
|
||||||
|
sinks.
|
||||||
|
- Shared cross-provider behavior belongs in `internal/normalizers/common`.
|
||||||
|
- Provider-specific helper logic shared with sources belongs in
|
||||||
|
`internal/providers/<provider>`.
|
||||||
|
|
||||||
|
## Config Fields Used
|
||||||
|
|
||||||
|
Normalizers do not read config. They operate only on incoming events.
|
||||||
|
|
||||||
|
## External Adapters Used
|
||||||
|
|
||||||
|
Runtime composition creates feedkit's normalize processor with
|
||||||
|
`RequireMatch=false`. Events without a matching normalizer pass through
|
||||||
|
unchanged.
|
||||||
|
|
||||||
|
Weatherfeeder registers normalizers in a stable order:
|
||||||
|
|
||||||
|
1. NWS
|
||||||
|
2. Open-Meteo
|
||||||
|
3. OpenWeather
|
||||||
|
4. SPC
|
||||||
|
|
||||||
|
The current normalizers avoid ambiguous matches by using schema equality.
|
||||||
|
|
||||||
|
The SPC outlook normalizer decodes the raw multi-document bundle, maps each
|
||||||
|
GeoJSON feature to a canonical outlook, and enriches all outlooks for a day with
|
||||||
|
the matching print-page headline, summary, and discussion. It preserves compact
|
||||||
|
GeoJSON feature geometry and computes `containsLocation` with
|
||||||
|
`internal/geo.ContainsPoint` using the source-configured point. Boundary points
|
||||||
|
count as contained, and all polygons are preserved whether or not they contain
|
||||||
|
the point.
|
||||||
|
|
||||||
|
## State
|
||||||
|
|
||||||
|
Normalizers should be stateless. Shared helpers should be deterministic and free
|
||||||
|
of I/O.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Malformed required raw payload data should produce contextual errors from the
|
||||||
|
owning normalizer. Successful normalization validates the output event before it
|
||||||
|
continues through the pipeline.
|
||||||
|
|
||||||
|
`internal/normalizers/common.Finalize` preserves the input event envelope except
|
||||||
|
for schema, payload, and effective time. It also rounds canonical float values
|
||||||
|
to four digits after the decimal point.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/normalizers/builtins_test.go`
|
||||||
|
- provider normalizer tests under `internal/normalizers/nws`
|
||||||
|
- provider normalizer tests under `internal/normalizers/openmeteo`
|
||||||
|
- provider normalizer tests under `internal/normalizers/openweather`
|
||||||
|
- provider normalizer tests under `internal/normalizers/spc`
|
||||||
|
- common helper tests under `internal/normalizers/common`
|
||||||
|
|
||||||
|
## Invariants
|
||||||
|
|
||||||
|
- Match by schema constants from `standards`.
|
||||||
|
- Preserve the event envelope except for intentional canonical changes.
|
||||||
|
- Produce canonical payload structs from `model`.
|
||||||
|
- Validate normalized events before returning them.
|
||||||
|
- Keep normalizers independent of sources, sinks, config loading, and runtime
|
||||||
|
composition.
|
||||||
124
docs/internal/postgres-sink.md
Normal file
124
docs/internal/postgres-sink.md
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
# Postgres Sink Internals
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
`internal/sinks/postgres` defines weatherfeeder's canonical-event-to-Postgres
|
||||||
|
mapping. It supplies a schema definition and mapper to feedkit's generic
|
||||||
|
Postgres sink.
|
||||||
|
|
||||||
|
The consumer-facing table contract is
|
||||||
|
[`docs/integrations/postgres.md`](../integrations/postgres.md). This document
|
||||||
|
describes the internal ownership boundary.
|
||||||
|
|
||||||
|
## Inputs And Outputs
|
||||||
|
|
||||||
|
Inputs are canonical feed events. The mapper currently handles these schemas:
|
||||||
|
|
||||||
|
- `weather.observation.v1`
|
||||||
|
- `weather.forecast.v1`
|
||||||
|
- `weather.forecast_discussion.v1`
|
||||||
|
- `weather.weather_story.v1`
|
||||||
|
- `weather.alert.v1`
|
||||||
|
- `weather.outlook.v1`
|
||||||
|
|
||||||
|
Outputs are feedkit `PostgresWrite` values for weatherfeeder-owned tables.
|
||||||
|
Unsupported schemas produce no writes and no error.
|
||||||
|
|
||||||
|
## Boundaries
|
||||||
|
|
||||||
|
- Weatherfeeder owns table definitions in `schema.go`.
|
||||||
|
- Weatherfeeder owns canonical payload mapping in `map.go`.
|
||||||
|
- Feedkit owns database opening, table and index creation, transactions,
|
||||||
|
inserts, context-aware consumption, and prune execution.
|
||||||
|
- Postgres mapping consumes canonical events only. It should not understand raw
|
||||||
|
provider schemas.
|
||||||
|
|
||||||
|
## Config Fields Used
|
||||||
|
|
||||||
|
Weatherfeeder registers the `postgres` sink by passing `PostgresSchema()` to
|
||||||
|
feedkit. Feedkit parses sink params:
|
||||||
|
|
||||||
|
- `uri`
|
||||||
|
- `username`
|
||||||
|
- `password`
|
||||||
|
- `prune`, optional duration
|
||||||
|
|
||||||
|
Weatherfeeder-owned mapper code does not read config directly.
|
||||||
|
|
||||||
|
## External Adapters Used
|
||||||
|
|
||||||
|
The runtime registers the sink with:
|
||||||
|
|
||||||
|
```go
|
||||||
|
sinkReg.Register("postgres", fksinks.PostgresFactory(wfpgsink.PostgresSchema()))
|
||||||
|
```
|
||||||
|
|
||||||
|
Feedkit validates events at the sink boundary, calls the weatherfeeder mapper,
|
||||||
|
validates writes against the compiled schema, inserts rows in a transaction, and
|
||||||
|
optionally prunes rows older than the configured window.
|
||||||
|
|
||||||
|
## State
|
||||||
|
|
||||||
|
The mapper is stateless. Durable state is stored in Postgres through feedkit's
|
||||||
|
sink implementation.
|
||||||
|
|
||||||
|
## Mapping Rules
|
||||||
|
|
||||||
|
Parent rows preserve event envelope fields where the table supports them:
|
||||||
|
|
||||||
|
- `event_id`
|
||||||
|
- `event_kind`
|
||||||
|
- `event_source`
|
||||||
|
- `event_schema`
|
||||||
|
- `event_emitted_at`
|
||||||
|
- `event_effective_at`
|
||||||
|
|
||||||
|
Child rows use positional indexes to preserve canonical array order:
|
||||||
|
|
||||||
|
- `weather_index`
|
||||||
|
- `period_index`
|
||||||
|
- `message_index`
|
||||||
|
- `story_index`
|
||||||
|
- `alert_index`
|
||||||
|
- `reference_index`
|
||||||
|
- `outlook_index`
|
||||||
|
|
||||||
|
Required canonical fields are validated before writes are returned:
|
||||||
|
|
||||||
|
- observations require `timestamp`;
|
||||||
|
- forecasts require `issuedAt` and `product`, and each period requires
|
||||||
|
`startTime` and `endTime`;
|
||||||
|
- forecast discussions require `issuedAt` and `product`;
|
||||||
|
- weather story runs require `asOf`, and each story requires `startTime`,
|
||||||
|
`endTime`, and `updatedAt`;
|
||||||
|
- alert runs require `asOf`, and each alert requires `id`;
|
||||||
|
- outlook runs require `asOf`, and each outlook requires `id`, `provider`,
|
||||||
|
`product`, `day`, `outlookType`, `label`, `validFrom`, `validTo`, `issuedAt`,
|
||||||
|
`expiresAt`, and `geometry`.
|
||||||
|
|
||||||
|
Nullable canonical values are converted to SQL nulls by mapper helpers.
|
||||||
|
Observation present-weather raw values and outlook geometry values are stored as
|
||||||
|
compact JSON text.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Payload decode failures, missing required fields, invalid compact JSON values,
|
||||||
|
or schema/write mismatches return errors to feedkit's sink. Feedkit rolls back
|
||||||
|
the transaction when a write fails.
|
||||||
|
|
||||||
|
Unsupported canonical schemas are ignored by this mapper so other routed events
|
||||||
|
can use different sinks without Postgres-specific failures.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/sinks/postgres/schema_test.go`
|
||||||
|
- `internal/sinks/postgres/map_test.go`
|
||||||
|
- feedkit Postgres sink tests when changing generic sink behavior assumptions
|
||||||
|
|
||||||
|
## Invariants
|
||||||
|
|
||||||
|
- Persist only canonical schemas.
|
||||||
|
- Preserve event envelope fields in parent rows.
|
||||||
|
- Preserve array order with child positional indexes.
|
||||||
|
- Validate required fields before writing.
|
||||||
|
- Keep table-contract docs synchronized with schema and mapper changes.
|
||||||
110
docs/internal/runtime.md
Normal file
110
docs/internal/runtime.md
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
# Runtime Internals
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
`cmd/weatherfeeder` wires the daemon together. It owns process setup and runtime
|
||||||
|
composition; provider mapping, source fetching details, and sink persistence
|
||||||
|
rules stay in their owning packages.
|
||||||
|
|
||||||
|
## Inputs And Outputs
|
||||||
|
|
||||||
|
The executable reads `config.yml` from the current working directory through
|
||||||
|
feedkit config loading. It builds configured sources, scheduler jobs, sinks, and
|
||||||
|
routes, then runs source polling and sink dispatch until shutdown.
|
||||||
|
|
||||||
|
Inputs are configured source polls. Outputs are feed events delivered to the
|
||||||
|
configured sinks.
|
||||||
|
|
||||||
|
## Runtime Flow
|
||||||
|
|
||||||
|
The implemented flow is:
|
||||||
|
|
||||||
|
1. load `config.yml`;
|
||||||
|
2. register weatherfeeder source drivers;
|
||||||
|
3. register feedkit built-in sinks and the weatherfeeder Postgres sink;
|
||||||
|
4. build source inputs and scheduler jobs;
|
||||||
|
5. validate configured expected kinds against source-advertised kinds;
|
||||||
|
6. build sinks and compile routes;
|
||||||
|
7. run the processor chain `normalize`, then `dedupe`;
|
||||||
|
8. run the scheduler and dispatcher concurrently;
|
||||||
|
9. shut down on signal or fatal scheduler/dispatcher error.
|
||||||
|
|
||||||
|
The in-process event channel is buffered to 256 events. The dedupe processor is
|
||||||
|
bounded by `dedupeMaxEntries`, currently 2048.
|
||||||
|
|
||||||
|
## Boundaries
|
||||||
|
|
||||||
|
- Runtime composition belongs in `cmd/weatherfeeder/main.go`.
|
||||||
|
- Source driver behavior belongs under `internal/sources`.
|
||||||
|
- Normalizer behavior belongs under `internal/normalizers`.
|
||||||
|
- Canonical payloads and schema strings belong in `model` and `standards`.
|
||||||
|
- Postgres mapping belongs under `internal/sinks/postgres`.
|
||||||
|
|
||||||
|
`cmd/weatherfeeder` should stay thin and should not contain provider parsing,
|
||||||
|
canonical mapping, or table-mapping rules.
|
||||||
|
|
||||||
|
## Config Fields Used
|
||||||
|
|
||||||
|
Runtime wiring consumes the feedkit top-level config sections:
|
||||||
|
|
||||||
|
- `sources`: source driver selection, source name, mode, cadence, expected kinds,
|
||||||
|
and driver params;
|
||||||
|
- `sinks`: sink driver selection, sink name, and sink params;
|
||||||
|
- `routes`: event-kind routing to named sinks.
|
||||||
|
|
||||||
|
The executable does not expose CLI flags or config path discovery.
|
||||||
|
|
||||||
|
## External Adapters Used
|
||||||
|
|
||||||
|
Runtime composition uses feedkit for:
|
||||||
|
|
||||||
|
- config loading;
|
||||||
|
- source registry and expected-kind validation;
|
||||||
|
- scheduler job construction;
|
||||||
|
- processor registry and chain execution;
|
||||||
|
- normalization and dedupe processors;
|
||||||
|
- sink registry and built-in sinks;
|
||||||
|
- route compilation and dispatch.
|
||||||
|
|
||||||
|
Weatherfeeder registers its own source drivers and its Postgres schema mapper.
|
||||||
|
|
||||||
|
## State
|
||||||
|
|
||||||
|
Weatherfeeder-owned runtime state is in process:
|
||||||
|
|
||||||
|
- event channel contents;
|
||||||
|
- the bounded dedupe key set;
|
||||||
|
- source instances and their in-memory unchanged-content state;
|
||||||
|
- scheduler and dispatcher goroutines.
|
||||||
|
|
||||||
|
There is no weatherfeeder-owned durable scheduler state, checkpoint, replay log,
|
||||||
|
or resume marker. Durable persistence is owned by configured external sinks.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Startup failures are fatal and include context such as config index, source name,
|
||||||
|
sink name, driver name, or the operation that failed.
|
||||||
|
|
||||||
|
At runtime, scheduler and dispatcher errors are sent to a shared error channel.
|
||||||
|
Context cancellation and deadline errors are treated as normal shutdown. Any
|
||||||
|
other scheduler or dispatcher error is logged as fatal and cancels the process
|
||||||
|
context.
|
||||||
|
|
||||||
|
The daemon handles `os.Interrupt` and `SIGTERM` with `signal.NotifyContext`.
|
||||||
|
After both runtime goroutines return, it logs `shutdown complete`.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `cmd/weatherfeeder/main_test.go`
|
||||||
|
- source registry tests under `internal/sources`
|
||||||
|
- normalizer registration tests under `internal/normalizers`
|
||||||
|
- feedkit scheduler, processor, dispatch, and sink tests when changing runtime
|
||||||
|
infrastructure usage
|
||||||
|
|
||||||
|
## Invariants
|
||||||
|
|
||||||
|
- Keep normalization before dedupe.
|
||||||
|
- Keep queue sizes and dedupe bounds explicit.
|
||||||
|
- Preserve context-aware shutdown.
|
||||||
|
- Keep runtime wiring separate from domain mapping and persistence rules.
|
||||||
|
- Keep startup validation failures loud and contextual.
|
||||||
122
docs/internal/sources.md
Normal file
122
docs/internal/sources.md
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
# Source Internals
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
Source packages poll upstream weather providers and emit raw feed events. They
|
||||||
|
are adapters, not canonical mappers.
|
||||||
|
|
||||||
|
Sources should decode only the metadata needed for event identity, effective
|
||||||
|
time, and routing policy. Full provider payload interpretation belongs in
|
||||||
|
normalizers.
|
||||||
|
|
||||||
|
## Inputs And Outputs
|
||||||
|
|
||||||
|
Inputs are feedkit `config.SourceConfig` values and upstream HTTP responses.
|
||||||
|
Outputs are feed events whose payloads are raw provider JSON and whose schemas
|
||||||
|
come from `standards`.
|
||||||
|
|
||||||
|
Current drivers:
|
||||||
|
|
||||||
|
| Driver | Kind | Raw schema |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `nws_observation` | `observation` | `raw.nws.observation.v1` |
|
||||||
|
| `nws_alerts` | `alert` | `raw.nws.alerts.v1` |
|
||||||
|
| `nws_forecast_hourly` | `forecast` | `raw.nws.hourly.forecast.v1` |
|
||||||
|
| `nws_forecast_narrative` | `forecast` | `raw.nws.narrative.forecast.v1` |
|
||||||
|
| `nws_forecast_discussion` | `forecast_discussion` | `raw.nws.forecast_discussion.v1` |
|
||||||
|
| `nws_weatherstories` | `weather_story` | `raw.nws.weatherstories.v1` |
|
||||||
|
| `openmeteo_observation` | `observation` | `raw.openmeteo.current.v1` |
|
||||||
|
| `openmeteo_forecast` | `forecast` | `raw.openmeteo.hourly.forecast.v1` |
|
||||||
|
| `openweather_observation` | `observation` | `raw.openweather.current.v1` |
|
||||||
|
| `spc_convective_outlook` | `outlook` | `raw.spc.convective_outlook.v1` |
|
||||||
|
|
||||||
|
## Boundaries
|
||||||
|
|
||||||
|
- Source constructors validate source-specific params.
|
||||||
|
- Sources use feedkit HTTP helpers for HTTP polling.
|
||||||
|
- Sources emit raw events and should not build canonical `model` payloads.
|
||||||
|
- Provider helper packages under `internal/providers/<provider>` hold shared
|
||||||
|
parsing and validation helpers.
|
||||||
|
- Registration is centralized in `internal/sources/builtins.go`.
|
||||||
|
|
||||||
|
## Config Fields Used
|
||||||
|
|
||||||
|
Most source drivers use feedkit `HTTPSource`.
|
||||||
|
|
||||||
|
Required params:
|
||||||
|
|
||||||
|
- `url`
|
||||||
|
- `user_agent`
|
||||||
|
|
||||||
|
Optional params:
|
||||||
|
|
||||||
|
- `conditional`, default `true`;
|
||||||
|
- `http_timeout`;
|
||||||
|
- `http_response_body_limit_bytes`.
|
||||||
|
|
||||||
|
OpenWeather observation sources additionally require the configured URL to use
|
||||||
|
metric units. This is enforced by `internal/providers/openweather`.
|
||||||
|
|
||||||
|
The SPC convective outlook source is a multi-document poll source rather than a
|
||||||
|
single-URL `HTTPSource`. It requires `latitude`, `longitude`, and `user_agent`;
|
||||||
|
accepts optional `location_id`, `location_name`, `geojson_urls`,
|
||||||
|
`discussion_urls`, and `rss_url`; and supports `http_timeout` and
|
||||||
|
`http_response_body_limit_bytes`.
|
||||||
|
|
||||||
|
Source-level `kinds`, when configured, are validated against the source's
|
||||||
|
advertised `Kinds()`.
|
||||||
|
|
||||||
|
## External Adapters Used
|
||||||
|
|
||||||
|
Most sources use feedkit's HTTP helper for:
|
||||||
|
|
||||||
|
- request construction;
|
||||||
|
- `User-Agent` and `Accept` headers;
|
||||||
|
- optional conditional GET validators;
|
||||||
|
- response body size limits;
|
||||||
|
- JSON raw-message fetches.
|
||||||
|
|
||||||
|
NWS helpers parse NWS timestamps. Open-Meteo helpers parse provider-local times
|
||||||
|
with timezone or UTC-offset data. OpenWeather helpers enforce metric-unit URLs.
|
||||||
|
SPC helpers define Day 1-3 product metadata, parse GeoJSON timestamps, extract
|
||||||
|
cleaned print-page discussion text, and parse optional RSS metadata.
|
||||||
|
|
||||||
|
## State
|
||||||
|
|
||||||
|
HTTP conditional validators are held in each single-document HTTP source
|
||||||
|
instance. They are not persisted across process restarts. The SPC outlook source
|
||||||
|
keeps only a source-local hash of the most recent complete required product
|
||||||
|
bundle and emits no event when a later complete bundle is unchanged.
|
||||||
|
|
||||||
|
## Failure Behavior
|
||||||
|
|
||||||
|
Constructor failures are returned during startup and stop the daemon. Polling
|
||||||
|
failures are returned to the scheduler.
|
||||||
|
|
||||||
|
If a source cannot decode minimal metadata from an otherwise successful upstream
|
||||||
|
response, it still emits the raw event when possible. The event then falls back
|
||||||
|
to default ID/effective-time behavior from feedkit source helpers.
|
||||||
|
|
||||||
|
Unchanged conditional responses return no events and no error.
|
||||||
|
|
||||||
|
The SPC outlook source fetches all configured Day 1-3 GeoJSON products and print
|
||||||
|
pages atomically. If any required GeoJSON or print-page request fails, the poll
|
||||||
|
returns an error and emits no partial bundle. RSS is fetched only when `rss_url`
|
||||||
|
is configured.
|
||||||
|
|
||||||
|
## Tests To Inspect
|
||||||
|
|
||||||
|
- `internal/sources/builtins_test.go`
|
||||||
|
- provider source tests under `internal/sources/nws`
|
||||||
|
- provider source tests under `internal/sources/openmeteo`
|
||||||
|
- provider source tests under `internal/sources/openweather`
|
||||||
|
- provider source tests under `internal/sources/spc`
|
||||||
|
- provider helper tests under `internal/providers`
|
||||||
|
|
||||||
|
## Invariants
|
||||||
|
|
||||||
|
- Emit raw provider schemas from `standards`.
|
||||||
|
- Keep provider-to-canonical mapping out of sources.
|
||||||
|
- Keep HTTP behavior context-aware.
|
||||||
|
- Keep driver registration explicit and stable.
|
||||||
|
- Keep source tests independent of live upstream services.
|
||||||
163
docs/operations.md
Normal file
163
docs/operations.md
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
# Operations
|
||||||
|
|
||||||
|
This document describes how to run and observe the `weatherfeeder` daemon in its
|
||||||
|
current form. For configuration syntax, see [configuration](config.md). For the
|
||||||
|
CLI surface, see [CLI reference](cli.md).
|
||||||
|
|
||||||
|
## Normal Workflow
|
||||||
|
|
||||||
|
1. Prepare `config.yml` in the process working directory.
|
||||||
|
2. Start the daemon with `./weatherfeeder` or `go run .` from
|
||||||
|
`cmd/weatherfeeder`.
|
||||||
|
3. Watch stderr logs for startup or runtime errors.
|
||||||
|
4. Consume events from the configured sinks.
|
||||||
|
5. Stop the process with `Ctrl-C` or `SIGTERM`.
|
||||||
|
|
||||||
|
The daemon has no admin subcommands and no runtime reload command. Change the
|
||||||
|
config file and restart the process to apply configuration changes.
|
||||||
|
|
||||||
|
Maintained copyable configs are available under [`examples/`](../examples/).
|
||||||
|
|
||||||
|
## Runtime Lifecycle
|
||||||
|
|
||||||
|
On startup, `weatherfeeder`:
|
||||||
|
|
||||||
|
1. loads `config.yml` from the current working directory;
|
||||||
|
2. registers built-in source drivers;
|
||||||
|
3. registers stdout, NATS, and weatherfeeder Postgres sink drivers;
|
||||||
|
4. builds sources and validates configured `kinds` against source metadata;
|
||||||
|
5. builds sinks and compiles routes;
|
||||||
|
6. starts the scheduler and dispatcher;
|
||||||
|
7. processes events through normalization, then in-memory dedupe;
|
||||||
|
8. routes processed events to configured sinks.
|
||||||
|
|
||||||
|
Startup errors are fatal and terminate the process. Runtime poll, pipeline, and
|
||||||
|
sink errors are logged and the process continues unless the scheduler or
|
||||||
|
dispatcher returns a fatal error.
|
||||||
|
|
||||||
|
## Logs
|
||||||
|
|
||||||
|
The process uses the Go standard logger with date, time, and microseconds. Logs
|
||||||
|
go to stderr.
|
||||||
|
|
||||||
|
Common log prefixes:
|
||||||
|
|
||||||
|
| Prefix | Meaning |
|
||||||
|
|---|---|
|
||||||
|
| `config load failed` | `config.yml` could not be read, parsed, or validated. |
|
||||||
|
| `build source failed` | A source driver or its params are invalid. |
|
||||||
|
| `source expected kinds validation failed` | Configured source `kinds` do not match the source driver. |
|
||||||
|
| `build sink failed` | A sink driver or its params are invalid, or a sink could not initialize. |
|
||||||
|
| `compile routes failed` | Routes reference invalid sinks or kinds. |
|
||||||
|
| `scheduler: poll failed` | A source poll failed; the source will be polled again on its next interval. |
|
||||||
|
| `dispatcher: pipeline error` | Normalization or dedupe failed for one event. |
|
||||||
|
| `dispatch: sink ... failed consuming event` | A sink failed to consume one event. |
|
||||||
|
| `shutdown complete` | Scheduler and dispatcher have exited. |
|
||||||
|
|
||||||
|
## Scheduling And Polling
|
||||||
|
|
||||||
|
Current weatherfeeder sources are polling sources. Each source uses its
|
||||||
|
configured `every` interval. The scheduler applies jitter before the first poll
|
||||||
|
and before each interval tick. If no jitter is configured in code, feedkit uses
|
||||||
|
`min(every/10, 30s)`, capped at half the interval.
|
||||||
|
|
||||||
|
Poll failures are logged and do not stop the daemon. A failed poll emits no
|
||||||
|
events for that source until a subsequent poll succeeds.
|
||||||
|
|
||||||
|
## Unchanged Upstream Content
|
||||||
|
|
||||||
|
Most sources use feedkit's single-document HTTP polling helper. By default,
|
||||||
|
`params.conditional` is `true` for those sources, so the helper keeps ETag and
|
||||||
|
Last-Modified validators in memory for each source instance.
|
||||||
|
|
||||||
|
If the upstream returns `304 Not Modified`, the source emits no events for that
|
||||||
|
poll. Validator state is in memory only; restarting the process starts with no
|
||||||
|
cached validators.
|
||||||
|
|
||||||
|
The SPC convective outlook source polls multiple required documents as one
|
||||||
|
bundle. It emits no event when a later complete bundle has the same fetched
|
||||||
|
document bodies as the previous successful poll.
|
||||||
|
|
||||||
|
## Processing And Dedupe
|
||||||
|
|
||||||
|
Every event passes through normalization first and dedupe second.
|
||||||
|
|
||||||
|
Normalizers match raw source schemas and produce canonical `weather.*.v1`
|
||||||
|
payloads. If an event has no matching normalizer, the normalize processor passes
|
||||||
|
it through unchanged.
|
||||||
|
|
||||||
|
Dedupe keys by event ID and stores a bounded in-memory set of 2048 recent IDs.
|
||||||
|
Duplicate IDs are dropped. Dedupe state is not persisted, so a restart starts
|
||||||
|
with an empty dedupe set.
|
||||||
|
|
||||||
|
## Routing And Sink Fanout
|
||||||
|
|
||||||
|
Routes choose sinks by event kind. If `routes` is omitted, every sink receives
|
||||||
|
every event kind. If a route omits `kinds`, that route also matches all kinds.
|
||||||
|
|
||||||
|
The dispatcher creates one queue and one worker goroutine per sink. The default
|
||||||
|
per-sink queue size is 64. `weatherfeeder` does not currently expose config
|
||||||
|
fields for sink queue size, enqueue timeout, or consume timeout.
|
||||||
|
|
||||||
|
Sink errors are logged per event. A sink failure does not stop other sinks from
|
||||||
|
receiving the same event.
|
||||||
|
|
||||||
|
## Sink Behavior
|
||||||
|
|
||||||
|
### stdout
|
||||||
|
|
||||||
|
The stdout sink validates each event and prints one JSON object per line to
|
||||||
|
stdout. This is useful for local inspection and log forwarding.
|
||||||
|
|
||||||
|
### NATS
|
||||||
|
|
||||||
|
The NATS sink connects lazily on the first event, reuses the connection while it
|
||||||
|
is open, and publishes each event as JSON to the configured subject. Connection,
|
||||||
|
marshal, and publish failures are logged by the dispatch worker.
|
||||||
|
|
||||||
|
### Postgres
|
||||||
|
|
||||||
|
The Postgres sink opens the database during startup. It creates missing tables
|
||||||
|
and indexes with `CREATE TABLE IF NOT EXISTS` and `CREATE INDEX IF NOT EXISTS`.
|
||||||
|
It does not modify existing table definitions.
|
||||||
|
|
||||||
|
Each mapped canonical event is written in one transaction. If `params.prune` is
|
||||||
|
set, the sink deletes rows older than `now - prune` from every weatherfeeder
|
||||||
|
table in that same transaction. See the
|
||||||
|
[Postgres table contract](integrations/postgres.md).
|
||||||
|
|
||||||
|
## State And Recovery
|
||||||
|
|
||||||
|
`weatherfeeder` keeps only runtime state in process memory:
|
||||||
|
|
||||||
|
- scheduler goroutines and timers;
|
||||||
|
- HTTP conditional request validators;
|
||||||
|
- source-local unchanged-content state;
|
||||||
|
- event channel buffers;
|
||||||
|
- per-sink fanout queues;
|
||||||
|
- the dedupe ID set.
|
||||||
|
|
||||||
|
Durable state is external sink state: NATS broker state outside this process and
|
||||||
|
Postgres tables managed by the configured database.
|
||||||
|
|
||||||
|
There is no internal checkpoint, replay log, or resume marker. To recover from a
|
||||||
|
process failure, fix the underlying issue and restart the daemon from a working
|
||||||
|
directory containing the desired `config.yml`.
|
||||||
|
|
||||||
|
## Shutdown
|
||||||
|
|
||||||
|
`weatherfeeder` listens for `os.Interrupt` and `SIGTERM`. On shutdown, the
|
||||||
|
shared context is canceled. Scheduler jobs stop polling, dispatch workers stop,
|
||||||
|
and the process logs `shutdown complete`.
|
||||||
|
|
||||||
|
Queued sink work may be dropped when shutdown context cancellation reaches the
|
||||||
|
fanout workers. Use external sink durability, such as Postgres or broker
|
||||||
|
retention, for durable downstream state.
|
||||||
|
|
||||||
|
## Caveats
|
||||||
|
|
||||||
|
- There is no health-check endpoint.
|
||||||
|
- There is no runtime config reload.
|
||||||
|
- There are no built-in metrics.
|
||||||
|
- Source conditional request state and dedupe state are reset by restart.
|
||||||
|
- Existing Postgres schemas are not migrated automatically.
|
||||||
186
docs/policy/architecture.md
Normal file
186
docs/policy/architecture.md
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
# Architecture Policy
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document defines `weatherfeeder`'s development architecture and invariants for maintainers and LLM coding agents. It describes how the implemented system is built and how subsequent changes should preserve its boundaries.
|
||||||
|
|
||||||
|
This is an inward-facing policy document. User-facing wire contracts belong in
|
||||||
|
[`docs/integrations/events.md`](../integrations/events.md), and roadmap items
|
||||||
|
belong under [`docs/roadmap/`](../roadmap/).
|
||||||
|
|
||||||
|
## Project Shape
|
||||||
|
|
||||||
|
`weatherfeeder` is a config-driven Go daemon that polls upstream weather providers, emits feed events, normalizes provider-specific raw payloads into canonical weather payloads, and routes those events to configured sinks.
|
||||||
|
|
||||||
|
The implemented runtime flow is:
|
||||||
|
|
||||||
|
1. `cmd/weatherfeeder` loads `config.yml` from the working directory.
|
||||||
|
2. Source drivers are built through the source registry.
|
||||||
|
3. Feedkit scheduler jobs poll sources and publish raw events onto an in-process event channel.
|
||||||
|
4. A pipeline runs normalization, then in-memory dedupe.
|
||||||
|
5. The dispatcher routes processed events to configured sinks by event kind.
|
||||||
|
6. Sinks consume events independently through feedkit fanout workers.
|
||||||
|
|
||||||
|
Canonical payload structs live in `model`. Schema identifiers and cross-provider wire conventions live in `standards`. Source adapters live under `internal/sources`. Normalizers live under `internal/normalizers`. Provider-specific parsing helpers shared by sources and normalizers live under `internal/providers`. Sink-specific persistence mapping lives under `internal/sinks`.
|
||||||
|
|
||||||
|
## Core Design Principles
|
||||||
|
|
||||||
|
- Hexagonal boundaries: provider APIs, config loading, scheduling, dispatch, and sinks are external mechanisms around the weather domain model and normalization logic.
|
||||||
|
- Raw-to-canonical flow: sources should fetch and envelope raw provider payloads; normalizers should own provider-to-canonical mapping.
|
||||||
|
- Schema-based routing: normalizers match on event schema, not source name or event kind.
|
||||||
|
- Composable registries: source drivers, normalizers, processors, and sinks are assembled explicitly through registries.
|
||||||
|
- Bounded concurrency: scheduling and sink fanout are concurrent, but the application should keep queues, goroutine ownership, logging, and cancellation behavior visible.
|
||||||
|
- Standard-library-first: use the Go standard library unless a narrow dependency materially improves maintainability or interoperability.
|
||||||
|
- Current-behavior docs: outside roadmap files, document only implemented behavior.
|
||||||
|
|
||||||
|
## Architectural Boundaries
|
||||||
|
|
||||||
|
Core/domain logic:
|
||||||
|
|
||||||
|
- `model` defines canonical payload structs and JSON field names.
|
||||||
|
- `standards` defines schema strings, versioning conventions, WMO constants, and shared wire policy.
|
||||||
|
- Normalizer mapping code is domain logic and should stay independent of CLI setup, filesystem paths, sink details, and runtime orchestration.
|
||||||
|
|
||||||
|
Adapters:
|
||||||
|
|
||||||
|
- Source adapters under `internal/sources/<provider>` fetch upstream data and produce raw feed events.
|
||||||
|
- Sink adapters under `internal/sinks/<sink>` map canonical events to external systems.
|
||||||
|
- Provider helper packages under `internal/providers/<provider>` contain pure parsing or provider-specific helper logic shared by sources and normalizers.
|
||||||
|
|
||||||
|
Runtime composition:
|
||||||
|
|
||||||
|
- `cmd/weatherfeeder/main.go` owns process wiring: config load, registry setup, scheduler jobs, processor chain, dispatcher, signal cancellation, and logging.
|
||||||
|
- It should remain thin. Do not move provider mapping or sink persistence rules into `cmd/weatherfeeder`.
|
||||||
|
|
||||||
|
Tests and examples:
|
||||||
|
|
||||||
|
- The sample `cmd/weatherfeeder/config.yml` is executable test input and is load-tested.
|
||||||
|
- Tests should keep exercising package contracts directly rather than relying only on full-daemon execution.
|
||||||
|
|
||||||
|
## Modules Or Processing Steps
|
||||||
|
|
||||||
|
The implemented processing steps are source polling, normalization, dedupe, and sink dispatch.
|
||||||
|
|
||||||
|
Source contract:
|
||||||
|
|
||||||
|
- Build from `feedkit/config.SourceConfig`.
|
||||||
|
- Validate required driver params inside the source constructor.
|
||||||
|
- Advertise emitted event kinds through `Kinds()` when possible.
|
||||||
|
- Emit raw schemas from `standards`.
|
||||||
|
- Decode only minimal metadata needed for event identity and effective time; leave full provider decoding to normalizers.
|
||||||
|
- Respect `context.Context` during network work.
|
||||||
|
|
||||||
|
Normalizer contract:
|
||||||
|
|
||||||
|
- One normalizer type per normalizer file.
|
||||||
|
- Match by `Event.Schema`.
|
||||||
|
- Decode raw payloads into provider structs.
|
||||||
|
- Map to canonical `model` payloads.
|
||||||
|
- Preserve the incoming event envelope except for intentional schema, payload, and effective-time changes.
|
||||||
|
- Use shared helpers in `internal/normalizers/common` for cross-provider behavior.
|
||||||
|
- Follow the detailed normalizer conventions in `internal/normalizers/doc.go`.
|
||||||
|
|
||||||
|
Sink contract:
|
||||||
|
|
||||||
|
- Consume canonical schemas, not provider raw schemas.
|
||||||
|
- Keep sink mapping isolated from normalizers and sources.
|
||||||
|
- Preserve event envelope fields in durable storage where the sink schema supports it.
|
||||||
|
- Validate required canonical fields before writing.
|
||||||
|
|
||||||
|
## State, Inputs, and Outputs
|
||||||
|
|
||||||
|
Inputs are configured source polls. The daemon currently uses feedkit's YAML config model with sources, sinks, and routes.
|
||||||
|
|
||||||
|
Outputs are feed events sent to configured sinks. Implemented sink support comes from feedkit built-ins plus weatherfeeder's Postgres schema mapping. The sample config includes stdout and NATS routes; Postgres is configured as an optional commented example.
|
||||||
|
|
||||||
|
The daemon's own state is in-process:
|
||||||
|
|
||||||
|
- the event channel buffers events during runtime;
|
||||||
|
- the dedupe processor stores a bounded in-memory key set;
|
||||||
|
- source instances may keep HTTP conditional request state through feedkit HTTP source helpers;
|
||||||
|
- scheduler and dispatcher state is not persisted by `weatherfeeder`.
|
||||||
|
|
||||||
|
Durable persistence is an external sink concern. The Postgres table contract is
|
||||||
|
documented in [`docs/integrations/postgres.md`](../integrations/postgres.md);
|
||||||
|
the consumer-facing event contract is documented in
|
||||||
|
[`docs/integrations/events.md`](../integrations/events.md).
|
||||||
|
|
||||||
|
## Configuration and CLI Boundaries
|
||||||
|
|
||||||
|
The implemented executable reads `config.yml` from the current working directory. It does not currently expose CLI flags or config path discovery.
|
||||||
|
|
||||||
|
Configuration shape is owned by feedkit's config package:
|
||||||
|
|
||||||
|
- `sources` define named source drivers, mode, poll cadence, expected kinds, and driver params.
|
||||||
|
- `sinks` define named sink drivers and sink params.
|
||||||
|
- `routes` connect event kinds to sinks.
|
||||||
|
|
||||||
|
Weatherfeeder-specific config policy belongs in source and sink constructors, registry setup, and tests. Do not spread config parsing through domain model or normalizer packages.
|
||||||
|
|
||||||
|
[`docs/config.md`](../config.md) and [`docs/cli.md`](../cli.md) are the
|
||||||
|
canonical user/operator references. This policy should stay architectural and
|
||||||
|
avoid duplicating those references.
|
||||||
|
|
||||||
|
## Errors, Logging, and Diagnostics
|
||||||
|
|
||||||
|
`cmd/weatherfeeder` uses the standard library `log` package with timestamps and microseconds.
|
||||||
|
|
||||||
|
Startup errors are fatal and include config index, source/sink name, driver name, or operation context where available. Runtime scheduler and dispatcher errors are reported through logs; context cancellation and deadline errors are treated as shutdown conditions.
|
||||||
|
|
||||||
|
Normalizers and sink mappers should wrap errors with operation and payload context, for example decode, parse, map, scan, or required-field context. Avoid logging or returning whole upstream payloads by default.
|
||||||
|
|
||||||
|
The daemon handles `os.Interrupt` and `SIGTERM` through `signal.NotifyContext`. Sources, sinks, scheduler jobs, dispatcher, and processors should respect `context.Context`.
|
||||||
|
|
||||||
|
## Testing Expectations
|
||||||
|
|
||||||
|
When changing behavior, inspect or add focused tests in the owning package.
|
||||||
|
|
||||||
|
Expected coverage by change type:
|
||||||
|
|
||||||
|
- Source drivers: constructor behavior, advertised kinds, poll event schema/kind, effective-time policy, unchanged responses, and malformed metadata handling.
|
||||||
|
- Normalizers: schema matching, canonical schema output, key field mapping, effective time, malformed required fields, and wire-shape regressions.
|
||||||
|
- Provider helpers: parsing edge cases and fixtures.
|
||||||
|
- Runtime wiring: config loading, source registry build, scheduler job creation, processor ordering, pass-through behavior, and dedupe behavior.
|
||||||
|
- Postgres sink: schema shape, mapper writes, required-field validation, nullable handling, and compact JSON behavior.
|
||||||
|
- Documentation-sensitive examples: keep sample config loadable.
|
||||||
|
|
||||||
|
Use local test servers and fixtures rather than real upstream services. Full-package tests should remain fast and deterministic.
|
||||||
|
|
||||||
|
## Dependency Policy
|
||||||
|
|
||||||
|
Prefer the Go standard library for parsing, HTTP handling, time handling, logging, and tests where reasonable.
|
||||||
|
|
||||||
|
Existing broad runtime composition is delegated to `feedkit`, which provides config, sources, scheduler, processors, dispatch, and sinks. Keep weatherfeeder-specific code from depending directly on low-level external clients when feedkit or a small adapter can contain that dependency.
|
||||||
|
|
||||||
|
Third-party dependencies should be narrow, justified, and preferably de facto standard for their purpose. YAML parsing through feedkit is an acceptable example. Do not add dependencies for small conveniences, and do not let dependency-specific types leak across package boundaries unless that dependency is the package's explicit contract.
|
||||||
|
|
||||||
|
## Documentation Expectations
|
||||||
|
|
||||||
|
Documentation must follow [`docs/policy/documentation.md`](documentation.md).
|
||||||
|
|
||||||
|
Rules for architecture-related docs:
|
||||||
|
|
||||||
|
- Current-behavior docs must describe implemented behavior only.
|
||||||
|
- Roadmap or speculative work belongs only under `docs/roadmap/`.
|
||||||
|
- Prefer links to canonical docs over repeated reference material.
|
||||||
|
- Update docs in the same change when modifying schemas, config behavior, runtime behavior, adapters, or persistence contracts.
|
||||||
|
|
||||||
|
## Architectural Invariants
|
||||||
|
|
||||||
|
- Keep `cmd/weatherfeeder` as composition code, not domain logic.
|
||||||
|
- Keep source fetching separate from normalization.
|
||||||
|
- Keep normalizers matched by schema constants from `standards`.
|
||||||
|
- Keep canonical payload structs in `model` and treat JSON tags as wire contract.
|
||||||
|
- Keep provider-specific helpers under `internal/providers/<provider>` when shared by sources and normalizers.
|
||||||
|
- Keep cross-provider normalizer helpers pure and deterministic.
|
||||||
|
- Keep sink persistence mapping isolated under `internal/sinks/<sink>`.
|
||||||
|
- Preserve explicit registry-based extension points for sources and normalizers.
|
||||||
|
- Preserve context-aware shutdown and bounded in-process queues.
|
||||||
|
- Avoid broad dependencies without clear architectural value.
|
||||||
|
|
||||||
|
## Non-Goals
|
||||||
|
|
||||||
|
- `weatherfeeder` is not an HTTP API. API serving belongs to separate consumers such as `weatherapi`.
|
||||||
|
- `weatherfeeder` does not own long-term durable state except through configured external sinks.
|
||||||
|
- `weatherfeeder` does not provide a general plugin runtime; new built-in providers and sinks are registered in code.
|
||||||
|
- Architecture policy is not a CLI, config, or wire-contract reference.
|
||||||
202
docs/policy/development.md
Normal file
202
docs/policy/development.md
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
# Development Policy
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document describes how to change `weatherfeeder` safely. It is for
|
||||||
|
maintainers and coding agents working in the repository.
|
||||||
|
|
||||||
|
Use this alongside the [architecture policy](architecture.md). User-facing CLI,
|
||||||
|
configuration, operations, and wire-contract details belong in their canonical
|
||||||
|
docs, not here.
|
||||||
|
|
||||||
|
## Repository Layout
|
||||||
|
|
||||||
|
- `cmd/weatherfeeder/`: executable wiring, sample `config.yml`, and runtime
|
||||||
|
composition tests.
|
||||||
|
- `model/`: canonical weather payload structs. JSON tags are part of the wire
|
||||||
|
contract.
|
||||||
|
- `standards/`: schema strings, versioning conventions, WMO constants, and
|
||||||
|
shared wire-format policy.
|
||||||
|
- `internal/sources/`: source adapters that poll upstream providers and emit raw
|
||||||
|
feed events.
|
||||||
|
- `internal/normalizers/`: raw-to-canonical event transforms.
|
||||||
|
- `internal/providers/`: pure provider helper code shared by sources and
|
||||||
|
normalizers.
|
||||||
|
- `internal/sinks/postgres/`: weatherfeeder-owned Postgres schema and canonical
|
||||||
|
event mapper.
|
||||||
|
- `docs/`: current behavior, policies, integration contracts, and roadmap files.
|
||||||
|
- `examples/`: maintained, copyable configuration examples.
|
||||||
|
|
||||||
|
## Build And Test
|
||||||
|
|
||||||
|
Run the full test suite before committing behavior or documentation changes that
|
||||||
|
depend on code behavior:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go test ./...
|
||||||
|
```
|
||||||
|
|
||||||
|
Use narrower commands while iterating:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
go test ./cmd/weatherfeeder
|
||||||
|
go test ./internal/sources/...
|
||||||
|
go test ./internal/normalizers/...
|
||||||
|
go test ./internal/sinks/postgres
|
||||||
|
```
|
||||||
|
|
||||||
|
Format Go code before committing:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
gofmt -w <changed-go-files>
|
||||||
|
```
|
||||||
|
|
||||||
|
Do not require live upstream weather services, NATS, or Postgres for unit tests.
|
||||||
|
Use fixtures, local test servers, and package-level tests.
|
||||||
|
|
||||||
|
## Coding Conventions
|
||||||
|
|
||||||
|
- Keep `cmd/weatherfeeder` focused on composition: config load, registry setup,
|
||||||
|
scheduler jobs, processor chain, dispatch, signal handling, and logging.
|
||||||
|
- Keep source fetching separate from normalizer mapping.
|
||||||
|
- Match normalizers by schema constants from `standards`, not source names.
|
||||||
|
- Keep provider-specific helper code under `internal/providers/<provider>` when
|
||||||
|
both sources and normalizers use it.
|
||||||
|
- Keep cross-provider normalizer helpers pure and deterministic under
|
||||||
|
`internal/normalizers/common`.
|
||||||
|
- Keep sink persistence mapping isolated under `internal/sinks/<sink>`.
|
||||||
|
- Wrap errors with operation context, but do not include whole upstream payloads
|
||||||
|
in errors or logs by default.
|
||||||
|
- Prefer explicit registries and small package-level constructors over hidden
|
||||||
|
global behavior.
|
||||||
|
|
||||||
|
## Dependency Policy
|
||||||
|
|
||||||
|
Prefer the Go standard library unless a dependency materially improves
|
||||||
|
maintainability or interoperability.
|
||||||
|
|
||||||
|
`feedkit` owns generic daemon infrastructure for config, HTTP source helpers,
|
||||||
|
scheduling, processors, dispatch, and sinks. Weatherfeeder code should contain
|
||||||
|
weather-domain behavior and narrow adapter logic rather than duplicating feedkit
|
||||||
|
infrastructure.
|
||||||
|
|
||||||
|
Do not add broad dependencies for small conveniences. Do not let
|
||||||
|
dependency-specific types leak across package boundaries unless that dependency
|
||||||
|
is the package contract.
|
||||||
|
|
||||||
|
## Adding Config Fields
|
||||||
|
|
||||||
|
Generic config shape is owned by feedkit. Weatherfeeder-specific config behavior
|
||||||
|
belongs in source or sink constructors, registry setup, and tests.
|
||||||
|
|
||||||
|
When adding config behavior:
|
||||||
|
|
||||||
|
- validate required params at the adapter boundary;
|
||||||
|
- keep secrets in environment variables or placeholders, not committed values;
|
||||||
|
- update [configuration docs](../config.md);
|
||||||
|
- update maintained examples when the change affects normal operation;
|
||||||
|
- add or update config-load tests for example files when practical.
|
||||||
|
|
||||||
|
## Adding CLI Flags
|
||||||
|
|
||||||
|
The executable currently reads `config.yml` from the current working directory.
|
||||||
|
If CLI flags are added:
|
||||||
|
|
||||||
|
- keep parsing in `cmd/weatherfeeder`;
|
||||||
|
- avoid moving config policy into domain packages;
|
||||||
|
- update [CLI docs](../cli.md);
|
||||||
|
- update tests that exercise command behavior.
|
||||||
|
|
||||||
|
## Adding A Source Driver
|
||||||
|
|
||||||
|
Source drivers should fetch upstream data and emit raw events with minimal
|
||||||
|
metadata decoding.
|
||||||
|
|
||||||
|
Checklist:
|
||||||
|
|
||||||
|
- implement the driver under `internal/sources/<provider>`;
|
||||||
|
- build from `config.SourceConfig`;
|
||||||
|
- validate required params in the constructor;
|
||||||
|
- use feedkit HTTP helpers for HTTP polling when applicable;
|
||||||
|
- emit raw schema constants from `standards`;
|
||||||
|
- advertise emitted kinds through `Kinds()`;
|
||||||
|
- decode only metadata needed for event ID and effective time;
|
||||||
|
- register the driver in `internal/sources/builtins.go`;
|
||||||
|
- add constructor, kind, and polling tests;
|
||||||
|
- update config docs and examples when operators need new configuration;
|
||||||
|
- add provider integration notes when the provider contract needs maintenance
|
||||||
|
context.
|
||||||
|
|
||||||
|
## Adding A Normalizer
|
||||||
|
|
||||||
|
Normalizers own provider-to-canonical mapping.
|
||||||
|
|
||||||
|
Checklist:
|
||||||
|
|
||||||
|
- add one normalizer type per normalizer file;
|
||||||
|
- match using `Event.Schema`;
|
||||||
|
- decode raw payloads into provider structs;
|
||||||
|
- map to canonical `model` payloads;
|
||||||
|
- use `internal/normalizers/common.Finalize` so envelope handling and float
|
||||||
|
rounding stay consistent;
|
||||||
|
- preserve input envelope fields except schema, payload, and effective time;
|
||||||
|
- register through the provider package and `internal/normalizers/builtins.go`;
|
||||||
|
- add tests for schema matching, key payload fields, effective time, malformed
|
||||||
|
required data, and output validation.
|
||||||
|
|
||||||
|
## Adding Canonical Models Or Schemas
|
||||||
|
|
||||||
|
Canonical event changes affect multiple contracts.
|
||||||
|
|
||||||
|
Checklist:
|
||||||
|
|
||||||
|
- update payload structs in `model`;
|
||||||
|
- add or update schema constants in `standards`;
|
||||||
|
- update [event wire contract docs](../integrations/events.md);
|
||||||
|
- update normalizers that produce the schema;
|
||||||
|
- update Postgres mapping if the schema is persisted;
|
||||||
|
- add tests for wire shape and mapper behavior.
|
||||||
|
|
||||||
|
## Adding Postgres Mapping
|
||||||
|
|
||||||
|
Weatherfeeder owns the canonical-event-to-table mapping. Feedkit owns the
|
||||||
|
generic Postgres sink mechanics.
|
||||||
|
|
||||||
|
Checklist:
|
||||||
|
|
||||||
|
- update `internal/sinks/postgres/schema.go`;
|
||||||
|
- update `internal/sinks/postgres/map.go`;
|
||||||
|
- preserve event envelope columns in parent rows when the table supports them;
|
||||||
|
- validate required canonical fields before writing;
|
||||||
|
- use positional indexes for child rows that represent arrays;
|
||||||
|
- update mapper and schema tests;
|
||||||
|
- update [Postgres integration docs](../integrations/postgres.md) when the table
|
||||||
|
contract changes.
|
||||||
|
|
||||||
|
## Examples And Documentation
|
||||||
|
|
||||||
|
Documentation must follow the [documentation policy](documentation.md).
|
||||||
|
|
||||||
|
When behavior changes, update the canonical docs in the same change:
|
||||||
|
|
||||||
|
- config shape: `docs/config.md`;
|
||||||
|
- CLI behavior: `docs/cli.md`;
|
||||||
|
- operations and recovery: `docs/operations.md`;
|
||||||
|
- troubleshooting: `docs/troubleshooting.md`;
|
||||||
|
- external contracts: `docs/integrations/`;
|
||||||
|
- internal component behavior: `docs/internal/`;
|
||||||
|
- copyable configs: `examples/`.
|
||||||
|
|
||||||
|
Keep roadmap content under `docs/roadmap/`. Current-behavior docs must describe
|
||||||
|
implemented behavior only.
|
||||||
|
|
||||||
|
## Review Checklist
|
||||||
|
|
||||||
|
Before committing:
|
||||||
|
|
||||||
|
- run focused tests for changed packages;
|
||||||
|
- run `go test ./...` for broad behavior or documentation changes tied to code;
|
||||||
|
- verify maintained examples still load when examples or config docs changed;
|
||||||
|
- check links in changed docs;
|
||||||
|
- search for stale paths, unsupported features, and secret-like values;
|
||||||
|
- keep unrelated refactors out of the change.
|
||||||
356
docs/policy/documentation.md
Normal file
356
docs/policy/documentation.md
Normal file
@@ -0,0 +1,356 @@
|
|||||||
|
# Go Project Documentation Policy
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
Project documentation must help four audiences:
|
||||||
|
|
||||||
|
1. users who need to run the application;
|
||||||
|
2. administrators/operators who need to configure and operate it;
|
||||||
|
3. developers who need to understand and change it safely;
|
||||||
|
4. LLM coding agents that need clear scope, boundaries, and invariants.
|
||||||
|
|
||||||
|
Docs should be accurate, concise, task-oriented, and organized by audience. Prefer links to canonical docs over repetition.
|
||||||
|
|
||||||
|
## Core Rules
|
||||||
|
|
||||||
|
### 1. Keep docs concise
|
||||||
|
|
||||||
|
Each document should cover a defined scope and only the essentials for that scope.
|
||||||
|
|
||||||
|
Avoid:
|
||||||
|
- long background explanations;
|
||||||
|
- repeated reference material;
|
||||||
|
- implementation detail in user-facing docs;
|
||||||
|
- aspirational language outside roadmap docs;
|
||||||
|
- verbose examples where one minimal example is clearer.
|
||||||
|
|
||||||
|
### 2. Document only implemented behavior outside roadmap files
|
||||||
|
|
||||||
|
Unimplemented, planned, aspirational, experimental, or future work may be described only under:
|
||||||
|
|
||||||
|
- `docs/roadmap/`
|
||||||
|
|
||||||
|
No other documentation file, including `README.md`, should describe code, features, modules, stages, commands, config fields, or behaviors that do not currently exist.
|
||||||
|
|
||||||
|
If a feature is partial, non-roadmap docs may describe only the implemented portion and its current boundary.
|
||||||
|
|
||||||
|
### 3. Use canonical homes
|
||||||
|
|
||||||
|
Each type of information should have one canonical location.
|
||||||
|
|
||||||
|
Canonical homes:
|
||||||
|
|
||||||
|
- project purpose and quickstart: `README.md`
|
||||||
|
- development principles: `docs/policy/architecture.md`
|
||||||
|
- configuration reference: `docs/config.md`
|
||||||
|
- CLI reference: `docs/cli.md`
|
||||||
|
- operations and recovery: `docs/operations.md`
|
||||||
|
- troubleshooting: `docs/troubleshooting.md`
|
||||||
|
- implemented internals: `docs/internal/`
|
||||||
|
- future work: `docs/roadmap/`
|
||||||
|
- contributor workflow: `docs/policy/development.md`
|
||||||
|
- copyable examples: `examples/`
|
||||||
|
|
||||||
|
Other files should summarize briefly and link to the canonical source.
|
||||||
|
|
||||||
|
### 4. Keep examples real
|
||||||
|
|
||||||
|
Examples should be valid, maintained, and free of secrets.
|
||||||
|
|
||||||
|
Where practical:
|
||||||
|
- example configs should load successfully;
|
||||||
|
- example commands should match real CLI syntax;
|
||||||
|
- important examples should be covered by tests.
|
||||||
|
|
||||||
|
## Documentation Profiles
|
||||||
|
|
||||||
|
All projects require:
|
||||||
|
|
||||||
|
- `README.md`
|
||||||
|
- `docs/policy/architecture.md`
|
||||||
|
|
||||||
|
Additional docs depend on the project.
|
||||||
|
|
||||||
|
### Small library
|
||||||
|
|
||||||
|
Recommended:
|
||||||
|
- `docs/policy/development.md`, if contributor conventions are non-obvious
|
||||||
|
|
||||||
|
### Simple CLI
|
||||||
|
|
||||||
|
Required:
|
||||||
|
- `docs/cli.md`
|
||||||
|
|
||||||
|
Recommended:
|
||||||
|
- `docs/policy/development.md`
|
||||||
|
|
||||||
|
### Config-driven CLI
|
||||||
|
|
||||||
|
Required:
|
||||||
|
- `docs/cli.md`
|
||||||
|
- `docs/config.md`
|
||||||
|
|
||||||
|
Recommended:
|
||||||
|
- `examples/`
|
||||||
|
- `docs/policy/development.md`
|
||||||
|
|
||||||
|
### Stateful or operator-facing application
|
||||||
|
|
||||||
|
Required:
|
||||||
|
- `docs/cli.md`, if CLI-based
|
||||||
|
- `docs/config.md`, if config-driven
|
||||||
|
- `docs/operations.md`
|
||||||
|
|
||||||
|
Recommended:
|
||||||
|
- `docs/troubleshooting.md`
|
||||||
|
- `examples/`
|
||||||
|
- `docs/policy/development.md`
|
||||||
|
|
||||||
|
### Modular, staged, service-oriented, or orchestration application
|
||||||
|
|
||||||
|
Required:
|
||||||
|
- `docs/cli.md`, if CLI-based
|
||||||
|
- `docs/config.md`, if config-driven
|
||||||
|
- `docs/operations.md`
|
||||||
|
- `docs/internal/`
|
||||||
|
- `docs/policy/development.md`
|
||||||
|
|
||||||
|
Recommended:
|
||||||
|
- `docs/troubleshooting.md`
|
||||||
|
- validated examples under `examples/`
|
||||||
|
|
||||||
|
## Required Documents
|
||||||
|
|
||||||
|
### README.md
|
||||||
|
|
||||||
|
**Audience:** users, administrators, operators
|
||||||
|
|
||||||
|
The README is the outward-facing project orientation page.
|
||||||
|
|
||||||
|
It should include, in order:
|
||||||
|
|
||||||
|
1. concise description;
|
||||||
|
2. elevator pitch;
|
||||||
|
3. shortest useful command or usage example;
|
||||||
|
4. links to targeted docs.
|
||||||
|
|
||||||
|
The README should be short. It is not a manual.
|
||||||
|
|
||||||
|
The “shortest useful command” means the simplest command that performs the project’s core use case. (It does not mean `app --help`.)
|
||||||
|
|
||||||
|
### docs/policy/architecture.md
|
||||||
|
|
||||||
|
**Audience:** developers, LLM coding agents
|
||||||
|
|
||||||
|
`docs/policy/architecture.md` is required for every project.
|
||||||
|
|
||||||
|
It is an inward-facing development policy document. It should describe how the project is intended to be built and changed.
|
||||||
|
|
||||||
|
It should include:
|
||||||
|
|
||||||
|
- project shape;
|
||||||
|
- core design principles;
|
||||||
|
- package and boundary philosophy;
|
||||||
|
- state/persistence philosophy, if applicable;
|
||||||
|
- external integration philosophy, if applicable;
|
||||||
|
- error-handling and logging principles;
|
||||||
|
- testing expectations;
|
||||||
|
- documentation expectations;
|
||||||
|
- architectural invariants;
|
||||||
|
- explicit non-goals, if useful.
|
||||||
|
|
||||||
|
For small projects, this file may be brief. It may simply state that the project is intentionally narrow, monolithic, and dependency-light.
|
||||||
|
|
||||||
|
### docs/policy/development.md
|
||||||
|
|
||||||
|
**Audience:** developers, LLM coding agents
|
||||||
|
|
||||||
|
Required for projects maintained by humans and LLM coding agents.
|
||||||
|
|
||||||
|
It should include:
|
||||||
|
|
||||||
|
- repository layout;
|
||||||
|
- build/test commands;
|
||||||
|
- coding conventions;
|
||||||
|
- dependency policy;
|
||||||
|
- how to add config fields;
|
||||||
|
- how to add CLI flags;
|
||||||
|
- how to add stages/modules/adapters, if applicable;
|
||||||
|
- how to update examples;
|
||||||
|
- documentation update expectations.
|
||||||
|
|
||||||
|
### docs/config.md
|
||||||
|
|
||||||
|
**Audience:** administrators, operators, advanced users
|
||||||
|
|
||||||
|
Required for applications with configuration files.
|
||||||
|
|
||||||
|
It should include, in order:
|
||||||
|
|
||||||
|
1. config file locations and discovery precedence;
|
||||||
|
2. minimal working config;
|
||||||
|
3. production-oriented config;
|
||||||
|
4. full configuration reference;
|
||||||
|
5. secrets handling, if applicable;
|
||||||
|
6. links to maintained examples.
|
||||||
|
|
||||||
|
The full configuration reference should be canonical.
|
||||||
|
|
||||||
|
### docs/cli.md
|
||||||
|
|
||||||
|
**Audience:** users, administrators, operators
|
||||||
|
|
||||||
|
Required for CLI applications.
|
||||||
|
|
||||||
|
It should include, in order:
|
||||||
|
|
||||||
|
1. shortest useful command;
|
||||||
|
2. command overview;
|
||||||
|
3. complete flag reference;
|
||||||
|
4. common workflows;
|
||||||
|
5. diagnostic or recovery commands, if applicable.
|
||||||
|
|
||||||
|
Explain when commands are useful, not just their syntax.
|
||||||
|
|
||||||
|
### docs/operations.md
|
||||||
|
|
||||||
|
**Audience:** administrators, operators
|
||||||
|
|
||||||
|
Required for applications that maintain state, support resume behavior, run multiple stages, write durable artifacts, use remote storage, or require recovery procedures.
|
||||||
|
|
||||||
|
It should cover:
|
||||||
|
|
||||||
|
- normal workflow;
|
||||||
|
- filesystem layout;
|
||||||
|
- remote storage layout, if applicable;
|
||||||
|
- logs and manifests;
|
||||||
|
- resume/retry behavior;
|
||||||
|
- cleanup behavior;
|
||||||
|
- archive/backup behavior;
|
||||||
|
- safe recovery procedures;
|
||||||
|
- operational caveats.
|
||||||
|
|
||||||
|
### docs/troubleshooting.md
|
||||||
|
|
||||||
|
**Audience:** administrators, operators
|
||||||
|
|
||||||
|
Recommended once recurring failure modes exist.
|
||||||
|
|
||||||
|
Each entry should include:
|
||||||
|
|
||||||
|
- symptom;
|
||||||
|
- likely cause;
|
||||||
|
- diagnostic command or inspection step;
|
||||||
|
- safe fix;
|
||||||
|
- relevant links.
|
||||||
|
|
||||||
|
### docs/internal/
|
||||||
|
|
||||||
|
**Audience:** developers, LLM coding agents
|
||||||
|
|
||||||
|
Required for modular, staged, service-oriented, or orchestration projects.
|
||||||
|
|
||||||
|
This directory describes implemented internal components. It is not the roadmap.
|
||||||
|
|
||||||
|
Use one file per major component where useful.
|
||||||
|
|
||||||
|
Each component doc should include:
|
||||||
|
|
||||||
|
1. purpose;
|
||||||
|
2. inputs and outputs;
|
||||||
|
3. boundaries;
|
||||||
|
4. config fields used;
|
||||||
|
5. external adapters used;
|
||||||
|
6. state or manifest behavior, if applicable;
|
||||||
|
7. skip/resume behavior, if applicable;
|
||||||
|
8. failure behavior;
|
||||||
|
9. tests to inspect before changing;
|
||||||
|
10. architectural invariants.
|
||||||
|
|
||||||
|
### docs/roadmap/
|
||||||
|
|
||||||
|
**Audience:** maintainers, developers, LLM coding agents
|
||||||
|
|
||||||
|
This is the only place for planned, future, aspirational, experimental, or unimplemented work.
|
||||||
|
|
||||||
|
Roadmap docs should clearly distinguish:
|
||||||
|
|
||||||
|
- proposed work;
|
||||||
|
- accepted plans;
|
||||||
|
- deferred ideas;
|
||||||
|
- rejected ideas;
|
||||||
|
- implementation prompts or task breakdowns, if useful.
|
||||||
|
|
||||||
|
Roadmap docs should not be confused with current behavior.
|
||||||
|
|
||||||
|
### docs/integrations/
|
||||||
|
|
||||||
|
**Audience:** developers, LLM coding agents
|
||||||
|
|
||||||
|
Required for projects that depend on external CLIs, APIs, services, protocols, or file formats where the integration contract is important to maintain.
|
||||||
|
|
||||||
|
This directory contains concise, versioned reference notes for external integration contracts. It should document only the parts of the external system that this project actually uses.
|
||||||
|
|
||||||
|
Use one file per integration where useful.
|
||||||
|
|
||||||
|
## Examples Directory
|
||||||
|
|
||||||
|
Projects with non-trivial configuration or workflows should include `examples/`.
|
||||||
|
|
||||||
|
Useful examples include:
|
||||||
|
|
||||||
|
- minimal working config;
|
||||||
|
- production-oriented config;
|
||||||
|
- full annotated config;
|
||||||
|
- local development config;
|
||||||
|
- remote/object-storage config;
|
||||||
|
- minimal session/input file.
|
||||||
|
|
||||||
|
Examples should be valid, maintained, tested when practical, and linked from relevant docs.
|
||||||
|
|
||||||
|
## Security and Privacy
|
||||||
|
|
||||||
|
Docs and examples must not include:
|
||||||
|
|
||||||
|
- real API keys;
|
||||||
|
- tokens;
|
||||||
|
- passwords;
|
||||||
|
- private keys;
|
||||||
|
- private environment dumps;
|
||||||
|
- sensitive user data;
|
||||||
|
- raw private transcripts;
|
||||||
|
- private infrastructure details unless intentionally public.
|
||||||
|
|
||||||
|
Document secret-handling mechanisms, not actual secret values.
|
||||||
|
|
||||||
|
## Maintenance Rules
|
||||||
|
|
||||||
|
When docs change, verify the affected behavior.
|
||||||
|
|
||||||
|
Where practical:
|
||||||
|
|
||||||
|
- load example config files in tests;
|
||||||
|
- test CLI examples or command parser behavior;
|
||||||
|
- validate documented flags against real flags;
|
||||||
|
- remove stale references;
|
||||||
|
- update links after renames;
|
||||||
|
- keep roadmap content out of non-roadmap docs.
|
||||||
|
|
||||||
|
If documentation and code disagree, fix the documentation and/or open a roadmap item; do not leave aspirational behavior in current-behavior docs.
|
||||||
|
|
||||||
|
Documentation is complete only when it matches the current code.
|
||||||
|
|
||||||
|
## Documentation Change Checklist
|
||||||
|
|
||||||
|
Before merging documentation changes, verify:
|
||||||
|
|
||||||
|
- README is concise and orientation-focused.
|
||||||
|
- `docs/policy/architecture.md` describes development principles.
|
||||||
|
- Future work appears only under `docs/roadmap/`.
|
||||||
|
- User-facing docs avoid unnecessary internals.
|
||||||
|
- Developer-facing docs preserve boundaries and invariants.
|
||||||
|
- Config examples match the schema.
|
||||||
|
- CLI examples match real commands and flags.
|
||||||
|
- Defaults appear in the canonical config reference.
|
||||||
|
- No secrets or private data are included.
|
||||||
|
- Links are accurate.
|
||||||
60
docs/roadmap/future.md
Normal file
60
docs/roadmap/future.md
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# Future Work
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
This document is the catch-all roadmap for planned, deferred, aspirational, experimental, or unimplemented weatherfeeder work. Current behavior belongs in the canonical docs outside `docs/roadmap/`.
|
||||||
|
|
||||||
|
## SPC Convective Outlook Follow-Ups
|
||||||
|
|
||||||
|
### Weatherapi Outlook Endpoints
|
||||||
|
|
||||||
|
Expose persisted SPC convective outlooks through `weatherapi` after the weatherfeeder storage contract is stable.
|
||||||
|
|
||||||
|
Likely endpoints:
|
||||||
|
|
||||||
|
- `GET /outlooks/convective`
|
||||||
|
- `GET /outlooks/convective/active`
|
||||||
|
- `GET /outlooks/convective/location`
|
||||||
|
|
||||||
|
Recommended behavior:
|
||||||
|
|
||||||
|
- Return the latest outlook run by default.
|
||||||
|
- Support active outlook filtering by current time and `containsLocation=true`.
|
||||||
|
- Consider optional query filters for `day`, `outlookType`, and `containsLocation`.
|
||||||
|
- Preserve canonical outlook geometry for downstream display and audit use.
|
||||||
|
|
||||||
|
### SPC Day 4-8 Outlooks
|
||||||
|
|
||||||
|
Add SPC Day 4-8 convective outlook support as a schema-compatible extension only after Day 1-3 operation is proven.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- Day 4-8 products have different semantics from Day 1-3 categorical/tornado/hail/wind products.
|
||||||
|
- Avoid forcing Day 4-8 assumptions into the current Day 1-3 model until the source shapes and consumer needs are reviewed.
|
||||||
|
- Prefer reusing `weather.outlook.v1` if the fields remain accurate; otherwise write a separate roadmap before changing the canonical contract.
|
||||||
|
|
||||||
|
### Degraded SPC Bundle Mode
|
||||||
|
|
||||||
|
Evaluate whether the SPC source should support degraded partial bundles when one required upstream product fails.
|
||||||
|
|
||||||
|
Current behavior should remain atomic: if a required GeoJSON or print-page fetch fails, emit no event for that poll.
|
||||||
|
|
||||||
|
Future degraded mode would need a clear contract for:
|
||||||
|
|
||||||
|
- distinguishing "no risk polygon" from "product missing";
|
||||||
|
- exposing per-product fetch errors without leaking raw provider internals into canonical events;
|
||||||
|
- deciding whether downstream sinks and APIs should store or serve partial snapshots.
|
||||||
|
|
||||||
|
### Richer SPC Page Assets And Tables
|
||||||
|
|
||||||
|
Evaluate whether to parse additional SPC print-page metadata beyond the current discussion text.
|
||||||
|
|
||||||
|
Possible additions:
|
||||||
|
|
||||||
|
- archive GeoJSON/shapefile/KML links;
|
||||||
|
- image URLs;
|
||||||
|
- page risk tables;
|
||||||
|
- city tables;
|
||||||
|
- richer discussion section metadata.
|
||||||
|
|
||||||
|
Keep GeoJSON products authoritative for polygons, validity windows, and point matching unless a future roadmap explicitly changes that contract.
|
||||||
251
docs/troubleshooting.md
Normal file
251
docs/troubleshooting.md
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
# Troubleshooting
|
||||||
|
|
||||||
|
Use this guide with [configuration](config.md) and [operations](operations.md).
|
||||||
|
Messages are emitted through the standard logger on stderr.
|
||||||
|
|
||||||
|
## `config load failed: ... read "config.yml"`
|
||||||
|
|
||||||
|
Symptom: startup exits before building sources or sinks.
|
||||||
|
|
||||||
|
Likely cause: the process working directory does not contain `config.yml`, or
|
||||||
|
the runtime user cannot read it.
|
||||||
|
|
||||||
|
Diagnostic: run `pwd` in the same working directory used by the process, then
|
||||||
|
check `ls -l config.yml`.
|
||||||
|
|
||||||
|
Safe fix: place the intended config at `./config.yml`, change the working
|
||||||
|
directory, or mount the file at `/weatherfeeder/config.yml` when using the
|
||||||
|
provided container image.
|
||||||
|
|
||||||
|
## `config load failed: ... parse YAML`
|
||||||
|
|
||||||
|
Symptom: startup exits with a YAML parse error or an unknown field error.
|
||||||
|
|
||||||
|
Likely cause: invalid YAML syntax, multiple YAML documents, or a misspelled
|
||||||
|
config struct field.
|
||||||
|
|
||||||
|
Diagnostic: inspect the line and field in the error. Feedkit uses strict YAML
|
||||||
|
field decoding for config struct fields.
|
||||||
|
|
||||||
|
Safe fix: correct the YAML and compare the shape with
|
||||||
|
[configuration](config.md). Driver-specific `params` keys are validated by their
|
||||||
|
source or sink constructors.
|
||||||
|
|
||||||
|
## `config validation failed`
|
||||||
|
|
||||||
|
Symptom: startup exits and prints one or more validation messages.
|
||||||
|
|
||||||
|
Likely cause: missing `sources` or `sinks`, blank names, duplicate source or sink
|
||||||
|
names, invalid `mode`, missing `every` for a polling source with `mode: poll`, or
|
||||||
|
a route that references an unknown sink.
|
||||||
|
|
||||||
|
Diagnostic: read every bullet under `config validation failed`; the loader sorts
|
||||||
|
these messages so multiple issues can be fixed in one edit.
|
||||||
|
|
||||||
|
Safe fix: update the top-level config fields as documented in
|
||||||
|
[configuration](config.md).
|
||||||
|
|
||||||
|
## `unknown source driver`
|
||||||
|
|
||||||
|
Symptom: startup exits with `build source failed`.
|
||||||
|
|
||||||
|
Likely cause: `sources[].driver` does not match a registered weatherfeeder
|
||||||
|
source driver.
|
||||||
|
|
||||||
|
Diagnostic: compare the configured driver with the source driver table in
|
||||||
|
[configuration](config.md#source-drivers).
|
||||||
|
|
||||||
|
Safe fix: correct the driver name. Current drivers include `nws_observation`,
|
||||||
|
`nws_alerts`, `nws_forecast_hourly`, `nws_forecast_narrative`,
|
||||||
|
`nws_forecast_discussion`, `nws_weatherstories`, `openmeteo_observation`,
|
||||||
|
`openmeteo_forecast`, `openweather_observation`, and
|
||||||
|
`spc_convective_outlook`.
|
||||||
|
|
||||||
|
## `unknown sink driver`
|
||||||
|
|
||||||
|
Symptom: startup exits with `build sink failed`.
|
||||||
|
|
||||||
|
Likely cause: `sinks[].driver` is not registered.
|
||||||
|
|
||||||
|
Diagnostic: compare the configured driver with the sink driver table in
|
||||||
|
[configuration](config.md#sink-drivers).
|
||||||
|
|
||||||
|
Safe fix: use `stdout`, `nats`, or `postgres`.
|
||||||
|
|
||||||
|
## `source expected kinds validation failed`
|
||||||
|
|
||||||
|
Symptom: startup exits after building a source.
|
||||||
|
|
||||||
|
Likely cause: `sources[].kinds` declares a kind the source does not emit.
|
||||||
|
|
||||||
|
Diagnostic: compare the configured `kinds` list with the source driver kind in
|
||||||
|
[configuration](config.md#source-drivers).
|
||||||
|
|
||||||
|
Safe fix: remove `kinds` or set it to the kind emitted by that driver.
|
||||||
|
|
||||||
|
## `params.url is required` Or `params.user_agent is required`
|
||||||
|
|
||||||
|
Symptom: startup exits with `build source failed`.
|
||||||
|
|
||||||
|
Likely cause: a source is missing required HTTP params, or the values are blank
|
||||||
|
or not strings.
|
||||||
|
|
||||||
|
Diagnostic: inspect the named source in the error and check its `params`.
|
||||||
|
|
||||||
|
Safe fix: add non-empty `url` and `user_agent` values. See
|
||||||
|
[HTTP source params](config.md#http-source-params).
|
||||||
|
|
||||||
|
SPC convective outlook sources do not use `params.url`; they require
|
||||||
|
`latitude`, `longitude`, and `user_agent`. See
|
||||||
|
[SPC convective outlook params](config.md#spc-convective-outlook-params).
|
||||||
|
|
||||||
|
## `params.latitude is required` Or `params.longitude is required`
|
||||||
|
|
||||||
|
Symptom: startup exits for an `spc_convective_outlook` source.
|
||||||
|
|
||||||
|
Likely cause: the source is missing one of the configured point coordinates.
|
||||||
|
|
||||||
|
Diagnostic: inspect the named SPC source in the error and check its `params`.
|
||||||
|
|
||||||
|
Safe fix: add numeric `latitude` and `longitude` values in decimal degrees.
|
||||||
|
|
||||||
|
## `url must include units=metric`
|
||||||
|
|
||||||
|
Symptom: startup exits for an `openweather_observation` source.
|
||||||
|
|
||||||
|
Likely cause: the OpenWeather URL omits `units=metric` or sets another unit
|
||||||
|
system.
|
||||||
|
|
||||||
|
Diagnostic: inspect the query string in `params.url`.
|
||||||
|
|
||||||
|
Safe fix: add `units=metric` to the OpenWeather current-weather URL. Keep API
|
||||||
|
keys out of committed configs.
|
||||||
|
|
||||||
|
## `source ... sources[].every must be > 0 for polling sources`
|
||||||
|
|
||||||
|
Symptom: startup exits while building scheduler jobs.
|
||||||
|
|
||||||
|
Likely cause: a current weatherfeeder polling source has no usable `every`
|
||||||
|
interval.
|
||||||
|
|
||||||
|
Diagnostic: inspect the named `sources[]` entry and check `every`.
|
||||||
|
|
||||||
|
Safe fix: set a positive duration such as `1m`, `10m`, or `1h`.
|
||||||
|
|
||||||
|
## `build sink failed ... params.url is required`
|
||||||
|
|
||||||
|
Symptom: startup exits while building a NATS sink.
|
||||||
|
|
||||||
|
Likely cause: the NATS sink is missing `params.url`, or the value is blank or
|
||||||
|
not a string.
|
||||||
|
|
||||||
|
Diagnostic: inspect the named sink in the error and check its `params`.
|
||||||
|
|
||||||
|
Safe fix: set a NATS URL such as `nats://localhost:4222`.
|
||||||
|
|
||||||
|
## `build sink failed ... params.subject is required`
|
||||||
|
|
||||||
|
Symptom: startup exits while building a NATS sink.
|
||||||
|
|
||||||
|
Likely cause: the NATS sink is missing `params.subject`, or the value is blank
|
||||||
|
or not a string.
|
||||||
|
|
||||||
|
Diagnostic: inspect the named sink in the error and check its `params`.
|
||||||
|
|
||||||
|
Safe fix: set a non-empty subject such as `weatherfeeder`.
|
||||||
|
|
||||||
|
## `dispatch: sink ... failed consuming event ... NATS sink: connect`
|
||||||
|
|
||||||
|
Symptom: the daemon starts, but NATS events are not published.
|
||||||
|
|
||||||
|
Likely cause: the NATS server URL is unreachable, the server is not accepting
|
||||||
|
connections, or the configured URL is wrong for the runtime network.
|
||||||
|
|
||||||
|
Diagnostic: from the same runtime environment, check that the host and port in
|
||||||
|
`sinks[].params.url` are reachable.
|
||||||
|
|
||||||
|
Safe fix: correct the NATS URL or restore broker connectivity. Other configured
|
||||||
|
sinks continue receiving events.
|
||||||
|
|
||||||
|
## `postgres sink ... open db`
|
||||||
|
|
||||||
|
Symptom: startup exits while building a Postgres sink.
|
||||||
|
|
||||||
|
Likely cause: the database URI, username, password, network path, or database
|
||||||
|
availability is wrong.
|
||||||
|
|
||||||
|
Diagnostic: inspect `sinks[].params.uri`, `username`, and `password`; verify
|
||||||
|
that the same runtime environment can reach the database.
|
||||||
|
|
||||||
|
Safe fix: correct the credentials or URI, restore database connectivity, then
|
||||||
|
restart the daemon.
|
||||||
|
|
||||||
|
## `postgres sink ... ensure table` Or `ensure index`
|
||||||
|
|
||||||
|
Symptom: startup exits during Postgres initialization.
|
||||||
|
|
||||||
|
Likely cause: the database user cannot create required tables or indexes, an
|
||||||
|
existing object conflicts with weatherfeeder's expected table contract, or the
|
||||||
|
database is unavailable during initialization.
|
||||||
|
|
||||||
|
Diagnostic: inspect the named table or index in the error and compare existing
|
||||||
|
database objects with the [Postgres table contract](integrations/postgres.md).
|
||||||
|
|
||||||
|
Safe fix: grant the needed database privileges, create a compatible schema, or
|
||||||
|
perform an operator-managed migration before restarting.
|
||||||
|
|
||||||
|
## `postgres sink: insert into ...`
|
||||||
|
|
||||||
|
Symptom: the daemon starts, but Postgres writes for some events fail.
|
||||||
|
|
||||||
|
Likely cause: a duplicate primary key, incompatible existing table definition,
|
||||||
|
database constraint error, or connection failure during a write transaction.
|
||||||
|
|
||||||
|
Diagnostic: inspect the table name and database error in the log. Compare the
|
||||||
|
table with [Postgres integration](integrations/postgres.md).
|
||||||
|
|
||||||
|
Safe fix: repair the database schema or address the duplicate/connection issue.
|
||||||
|
Other configured sinks continue receiving events.
|
||||||
|
|
||||||
|
## No Events Appear On A Sink
|
||||||
|
|
||||||
|
Symptom: the daemon is running but the expected sink receives no events.
|
||||||
|
|
||||||
|
Likely cause: the route does not match the event kind, the source has not
|
||||||
|
emitted changed content, or the sink is failing per event.
|
||||||
|
|
||||||
|
Diagnostic: check `routes`, source `kinds`, and logs for `scheduler: poll
|
||||||
|
failed`, `dispatcher: pipeline error`, or `dispatch: sink ... failed consuming
|
||||||
|
event`.
|
||||||
|
|
||||||
|
Safe fix: correct the route or source configuration. If the source uses
|
||||||
|
conditional HTTP and the upstream has not changed, no event is emitted for a
|
||||||
|
`304 Not Modified` response; wait for changed upstream content or temporarily
|
||||||
|
set `params.conditional: false` for diagnosis.
|
||||||
|
|
||||||
|
## `scheduler: poll failed`
|
||||||
|
|
||||||
|
Symptom: one source logs poll failures while the daemon keeps running.
|
||||||
|
|
||||||
|
Likely cause: upstream HTTP error, bad URL, timeout, response body limit, or
|
||||||
|
provider response shape that the source cannot parse.
|
||||||
|
|
||||||
|
Diagnostic: inspect the source name in the log and review its HTTP params.
|
||||||
|
|
||||||
|
Safe fix: correct the URL, user agent, timeout, or body limit. The next
|
||||||
|
scheduled poll will retry.
|
||||||
|
|
||||||
|
## `dispatcher: pipeline error`
|
||||||
|
|
||||||
|
Symptom: source polling succeeds, but one event is dropped before sinks.
|
||||||
|
|
||||||
|
Likely cause: a normalizer could not decode or map the raw payload, or dedupe
|
||||||
|
received an invalid event ID.
|
||||||
|
|
||||||
|
Diagnostic: inspect the error text and the source/schema that produced the
|
||||||
|
event. Review the [event wire contract](integrations/events.md) for expected
|
||||||
|
canonical fields.
|
||||||
|
|
||||||
|
Safe fix: correct source configuration if it points to the wrong upstream
|
||||||
|
product. If the upstream payload changed shape, update the relevant normalizer
|
||||||
|
and tests.
|
||||||
19
examples/config.minimal.yml
Normal file
19
examples/config.minimal.yml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
sources:
|
||||||
|
- name: NWSObservationKSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["observation"]
|
||||||
|
driver: nws_observation
|
||||||
|
every: 10m
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/stations/KSTL/observations/latest"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
sinks:
|
||||||
|
- name: stdout
|
||||||
|
driver: stdout
|
||||||
|
params: {}
|
||||||
|
|
||||||
|
routes:
|
||||||
|
- sink: stdout
|
||||||
|
kinds: ["observation"]
|
||||||
42
examples/config.nats.yml
Normal file
42
examples/config.nats.yml
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
---
|
||||||
|
sources:
|
||||||
|
- name: NWSObservationKSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["observation"]
|
||||||
|
driver: nws_observation
|
||||||
|
every: 10m
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/stations/KSTL/observations/latest"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
- name: NWSAlertsSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["alert"]
|
||||||
|
driver: nws_alerts
|
||||||
|
every: 1m
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/alerts?point=38.6239,-90.3571&limit=20"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
- name: SPCConvectiveOutlookSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["outlook"]
|
||||||
|
driver: spc_convective_outlook
|
||||||
|
every: 30m
|
||||||
|
params:
|
||||||
|
latitude: 38.6239
|
||||||
|
longitude: -90.3571
|
||||||
|
location_id: "stl"
|
||||||
|
location_name: "St. Louis, MO"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
sinks:
|
||||||
|
- name: nats_weather
|
||||||
|
driver: nats
|
||||||
|
params:
|
||||||
|
url: nats://localhost:4222
|
||||||
|
subject: weatherfeeder.events
|
||||||
|
|
||||||
|
routes:
|
||||||
|
- sink: nats_weather
|
||||||
|
kinds: ["observation", "alert", "outlook"]
|
||||||
44
examples/config.postgres.yml
Normal file
44
examples/config.postgres.yml
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
---
|
||||||
|
sources:
|
||||||
|
- name: NWSObservationKSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["observation"]
|
||||||
|
driver: nws_observation
|
||||||
|
every: 10m
|
||||||
|
params:
|
||||||
|
url: "https://api.weather.gov/stations/KSTL/observations/latest"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
- name: OpenMeteoHourlyForecastSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["forecast"]
|
||||||
|
driver: openmeteo_forecast
|
||||||
|
every: 1h
|
||||||
|
params:
|
||||||
|
url: "https://api.open-meteo.com/v1/forecast?latitude=38.6239&longitude=-90.3571&hourly=temperature_2m,relative_humidity_2m,dew_point_2m,apparent_temperature,precipitation_probability,precipitation,snowfall,weather_code,surface_pressure,wind_speed_10m,wind_direction_10m&forecast_days=3"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
- name: SPCConvectiveOutlookSTL
|
||||||
|
mode: poll
|
||||||
|
kinds: ["outlook"]
|
||||||
|
driver: spc_convective_outlook
|
||||||
|
every: 30m
|
||||||
|
params:
|
||||||
|
latitude: 38.6239
|
||||||
|
longitude: -90.3571
|
||||||
|
location_id: "stl"
|
||||||
|
location_name: "St. Louis, MO"
|
||||||
|
user_agent: "weatherfeeder example (operator@example.com)"
|
||||||
|
|
||||||
|
sinks:
|
||||||
|
- name: pg_weather
|
||||||
|
driver: postgres
|
||||||
|
params:
|
||||||
|
uri: "postgres://postgres.example.invalid:5432/weatherfeeder?sslmode=disable"
|
||||||
|
username: <database_username>
|
||||||
|
password: <database_password>
|
||||||
|
prune: 3d
|
||||||
|
|
||||||
|
routes:
|
||||||
|
- sink: pg_weather
|
||||||
|
kinds: ["observation", "forecast", "outlook"]
|
||||||
107
internal/geo/geojson.go
Normal file
107
internal/geo/geojson.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type geometry struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Coordinates json.RawMessage `json:"coordinates"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContainsPoint reports whether a GeoJSON Polygon or MultiPolygon contains p.
|
||||||
|
// GeoJSON coordinate order is [longitude, latitude].
|
||||||
|
func ContainsPoint(raw []byte, p Point) (bool, error) {
|
||||||
|
if len(raw) == 0 {
|
||||||
|
return false, fmt.Errorf("geojson geometry is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
var geom geometry
|
||||||
|
if err := json.Unmarshal(raw, &geom); err != nil {
|
||||||
|
return false, fmt.Errorf("decode geojson geometry: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch geom.Type {
|
||||||
|
case "Polygon":
|
||||||
|
polygon, err := decodePolygon(geom.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("decode polygon: %w", err)
|
||||||
|
}
|
||||||
|
return polygonContainsPoint(polygon, p), nil
|
||||||
|
case "MultiPolygon":
|
||||||
|
multiPolygon, err := decodeMultiPolygon(geom.Coordinates)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("decode multipolygon: %w", err)
|
||||||
|
}
|
||||||
|
for _, polygon := range multiPolygon {
|
||||||
|
if polygonContainsPoint(polygon, p) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
case "":
|
||||||
|
return false, fmt.Errorf("geojson geometry type is required")
|
||||||
|
default:
|
||||||
|
return false, fmt.Errorf("unsupported geojson geometry type %q", geom.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodePolygon(raw json.RawMessage) (Polygon, error) {
|
||||||
|
var coords [][][]float64
|
||||||
|
if err := json.Unmarshal(raw, &coords); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return polygonFromCoordinates(coords)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeMultiPolygon(raw json.RawMessage) ([]Polygon, error) {
|
||||||
|
var coords [][][][]float64
|
||||||
|
if err := json.Unmarshal(raw, &coords); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(coords) == 0 {
|
||||||
|
return nil, fmt.Errorf("multipolygon has no polygons")
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make([]Polygon, 0, len(coords))
|
||||||
|
for i, polygonCoords := range coords {
|
||||||
|
polygon, err := polygonFromCoordinates(polygonCoords)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("polygons[%d]: %w", i, err)
|
||||||
|
}
|
||||||
|
out = append(out, polygon)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func polygonFromCoordinates(coords [][][]float64) (Polygon, error) {
|
||||||
|
if len(coords) == 0 {
|
||||||
|
return nil, fmt.Errorf("polygon has no rings")
|
||||||
|
}
|
||||||
|
|
||||||
|
polygon := make(Polygon, 0, len(coords))
|
||||||
|
for i, ringCoords := range coords {
|
||||||
|
ring, err := ringFromCoordinates(ringCoords)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("rings[%d]: %w", i, err)
|
||||||
|
}
|
||||||
|
polygon = append(polygon, ring)
|
||||||
|
}
|
||||||
|
return polygon, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ringFromCoordinates(coords [][]float64) (Ring, error) {
|
||||||
|
if len(coords) == 0 {
|
||||||
|
return nil, fmt.Errorf("ring has no points")
|
||||||
|
}
|
||||||
|
|
||||||
|
ring := make(Ring, 0, len(coords))
|
||||||
|
for i, pair := range coords {
|
||||||
|
if len(pair) < 2 {
|
||||||
|
return nil, fmt.Errorf("points[%d] has %d values, need longitude and latitude", i, len(pair))
|
||||||
|
}
|
||||||
|
ring = append(ring, Point{Longitude: pair[0], Latitude: pair[1]})
|
||||||
|
}
|
||||||
|
return ring, nil
|
||||||
|
}
|
||||||
105
internal/geo/point.go
Normal file
105
internal/geo/point.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
const epsilon = 1e-9
|
||||||
|
|
||||||
|
// Point is a geographic coordinate in decimal degrees.
|
||||||
|
type Point struct {
|
||||||
|
Longitude float64
|
||||||
|
Latitude float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ring is one GeoJSON linear ring.
|
||||||
|
type Ring []Point
|
||||||
|
|
||||||
|
// Polygon is a GeoJSON polygon. The first ring is the exterior ring; subsequent
|
||||||
|
// rings are holes.
|
||||||
|
type Polygon []Ring
|
||||||
|
|
||||||
|
func polygonContainsPoint(polygon Polygon, p Point) bool {
|
||||||
|
if len(polygon) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if pointOnRing(polygon[0], p) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !ringContainsPoint(polygon[0], p) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, hole := range polygon[1:] {
|
||||||
|
if pointOnRing(hole, p) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if ringContainsPoint(hole, p) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ringContainsPoint(ring Ring, p Point) bool {
|
||||||
|
inside := false
|
||||||
|
n := len(ring)
|
||||||
|
if n == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, j := 0, n-1; i < n; j, i = i, i+1 {
|
||||||
|
a := ring[j]
|
||||||
|
b := ring[i]
|
||||||
|
if pointOnSegment(p, a, b) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
intersects := (a.Latitude > p.Latitude) != (b.Latitude > p.Latitude)
|
||||||
|
if intersects {
|
||||||
|
x := (b.Longitude-a.Longitude)*(p.Latitude-a.Latitude)/(b.Latitude-a.Latitude) + a.Longitude
|
||||||
|
if almostEqual(x, p.Longitude) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if x > p.Longitude {
|
||||||
|
inside = !inside
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inside
|
||||||
|
}
|
||||||
|
|
||||||
|
func pointOnRing(ring Ring, p Point) bool {
|
||||||
|
n := len(ring)
|
||||||
|
if n == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i, j := 0, n-1; i < n; j, i = i, i+1 {
|
||||||
|
if pointOnSegment(p, ring[j], ring[i]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func pointOnSegment(p, a, b Point) bool {
|
||||||
|
cross := (p.Latitude-a.Latitude)*(b.Longitude-a.Longitude) - (p.Longitude-a.Longitude)*(b.Latitude-a.Latitude)
|
||||||
|
if math.Abs(cross) > epsilon {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
minLon, maxLon := minMax(a.Longitude, b.Longitude)
|
||||||
|
minLat, maxLat := minMax(a.Latitude, b.Latitude)
|
||||||
|
return p.Longitude >= minLon-epsilon &&
|
||||||
|
p.Longitude <= maxLon+epsilon &&
|
||||||
|
p.Latitude >= minLat-epsilon &&
|
||||||
|
p.Latitude <= maxLat+epsilon
|
||||||
|
}
|
||||||
|
|
||||||
|
func minMax(a, b float64) (float64, float64) {
|
||||||
|
if a < b {
|
||||||
|
return a, b
|
||||||
|
}
|
||||||
|
return b, a
|
||||||
|
}
|
||||||
|
|
||||||
|
func almostEqual(a, b float64) bool {
|
||||||
|
return math.Abs(a-b) <= epsilon
|
||||||
|
}
|
||||||
181
internal/geo/point_test.go
Normal file
181
internal/geo/point_test.go
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const squarePolygon = `{
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [[
|
||||||
|
[-91.0, 38.0],
|
||||||
|
[-90.0, 38.0],
|
||||||
|
[-90.0, 39.0],
|
||||||
|
[-91.0, 39.0],
|
||||||
|
[-91.0, 38.0]
|
||||||
|
]]
|
||||||
|
}`
|
||||||
|
|
||||||
|
func TestContainsPointInsideSimplePolygon(t *testing.T) {
|
||||||
|
got, err := ContainsPoint([]byte(squarePolygon), Point{Longitude: -90.5, Latitude: 38.5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if !got {
|
||||||
|
t.Fatalf("ContainsPoint() = false, want true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointAcceptsRawMessage(t *testing.T) {
|
||||||
|
got, err := ContainsPoint(json.RawMessage(squarePolygon), Point{Longitude: -90.5, Latitude: 38.5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if !got {
|
||||||
|
t.Fatalf("ContainsPoint() = false, want true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointOutsideSimplePolygon(t *testing.T) {
|
||||||
|
got, err := ContainsPoint([]byte(squarePolygon), Point{Longitude: -89.5, Latitude: 38.5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if got {
|
||||||
|
t.Fatalf("ContainsPoint() = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointOnBoundary(t *testing.T) {
|
||||||
|
got, err := ContainsPoint([]byte(squarePolygon), Point{Longitude: -91.0, Latitude: 38.5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if !got {
|
||||||
|
t.Fatalf("ContainsPoint() = false, want true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointInHoleReturnsFalse(t *testing.T) {
|
||||||
|
const polygonWithHole = `{
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [
|
||||||
|
[[0,0],[10,0],[10,10],[0,10],[0,0]],
|
||||||
|
[[4,4],[6,4],[6,6],[4,6],[4,4]]
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
|
||||||
|
got, err := ContainsPoint([]byte(polygonWithHole), Point{Longitude: 5, Latitude: 5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if got {
|
||||||
|
t.Fatalf("ContainsPoint() = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointOnHoleBoundaryReturnsTrue(t *testing.T) {
|
||||||
|
const polygonWithHole = `{
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [
|
||||||
|
[[0,0],[10,0],[10,10],[0,10],[0,0]],
|
||||||
|
[[4,4],[6,4],[6,6],[4,6],[4,4]]
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
|
||||||
|
got, err := ContainsPoint([]byte(polygonWithHole), Point{Longitude: 4, Latitude: 5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if !got {
|
||||||
|
t.Fatalf("ContainsPoint() = false, want true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointInsideOneMultiPolygonMember(t *testing.T) {
|
||||||
|
const multiPolygon = `{
|
||||||
|
"type": "MultiPolygon",
|
||||||
|
"coordinates": [
|
||||||
|
[[[0,0],[1,0],[1,1],[0,1],[0,0]]],
|
||||||
|
[[[10,10],[12,10],[12,12],[10,12],[10,10]]]
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
|
||||||
|
got, err := ContainsPoint([]byte(multiPolygon), Point{Longitude: 11, Latitude: 11})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if !got {
|
||||||
|
t.Fatalf("ContainsPoint() = false, want true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointUsesLongitudeLatitudeOrder(t *testing.T) {
|
||||||
|
const narrowPolygon = `{
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [[
|
||||||
|
[-91.0, 38.0],
|
||||||
|
[-90.0, 38.0],
|
||||||
|
[-90.0, 39.0],
|
||||||
|
[-91.0, 39.0],
|
||||||
|
[-91.0, 38.0]
|
||||||
|
]]
|
||||||
|
}`
|
||||||
|
|
||||||
|
got, err := ContainsPoint([]byte(narrowPolygon), Point{Longitude: -90.5, Latitude: 38.5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = %v", err)
|
||||||
|
}
|
||||||
|
if !got {
|
||||||
|
t.Fatalf("ContainsPoint() = false, want true")
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err = ContainsPoint([]byte(narrowPolygon), Point{Longitude: 38.5, Latitude: -90.5})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ContainsPoint() reversed error = %v", err)
|
||||||
|
}
|
||||||
|
if got {
|
||||||
|
t.Fatalf("ContainsPoint() with reversed coordinate values = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointUnsupportedGeometryError(t *testing.T) {
|
||||||
|
_, err := ContainsPoint([]byte(`{"type":"Point","coordinates":[-90,38]}`), Point{Longitude: -90, Latitude: 38})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), `unsupported geojson geometry type "Point"`) {
|
||||||
|
t.Fatalf("ContainsPoint() error = %q", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointInvalidJSONError(t *testing.T) {
|
||||||
|
_, err := ContainsPoint([]byte(`{"type":"Polygon"`), Point{Longitude: -90, Latitude: 38})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "decode geojson geometry") {
|
||||||
|
t.Fatalf("ContainsPoint() error = %q", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointMalformedCoordinatesError(t *testing.T) {
|
||||||
|
_, err := ContainsPoint([]byte(`{"type":"Polygon","coordinates":[[[1]]]}`), Point{Longitude: 1, Latitude: 1})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "need longitude and latitude") {
|
||||||
|
t.Fatalf("ContainsPoint() error = %q", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContainsPointEmptyRingError(t *testing.T) {
|
||||||
|
_, err := ContainsPoint([]byte(`{"type":"Polygon","coordinates":[[]]}`), Point{Longitude: 1, Latitude: 1})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("ContainsPoint() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "ring has no points") {
|
||||||
|
t.Fatalf("ContainsPoint() error = %q", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,12 +7,14 @@ import (
|
|||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/nws"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/nws"
|
||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openmeteo"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openmeteo"
|
||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openweather"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openweather"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/spc"
|
||||||
)
|
)
|
||||||
|
|
||||||
var builtinRegistrations = []func([]fknormalize.Normalizer) []fknormalize.Normalizer{
|
var builtinRegistrations = []func([]fknormalize.Normalizer) []fknormalize.Normalizer{
|
||||||
nws.Register,
|
nws.Register,
|
||||||
openmeteo.Register,
|
openmeteo.Register,
|
||||||
openweather.Register,
|
openweather.Register,
|
||||||
|
spc.Register,
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterBuiltins registers all normalizers shipped with this binary.
|
// RegisterBuiltins registers all normalizers shipped with this binary.
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/nws"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/nws"
|
||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openmeteo"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openmeteo"
|
||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openweather"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/openweather"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/spc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRegisterBuiltinsOrder(t *testing.T) {
|
func TestRegisterBuiltinsOrder(t *testing.T) {
|
||||||
@@ -20,10 +21,12 @@ func TestRegisterBuiltinsOrder(t *testing.T) {
|
|||||||
nws.ObservationNormalizer{},
|
nws.ObservationNormalizer{},
|
||||||
nws.ForecastNormalizer{},
|
nws.ForecastNormalizer{},
|
||||||
nws.ForecastDiscussionNormalizer{},
|
nws.ForecastDiscussionNormalizer{},
|
||||||
|
nws.WeatherStoriesNormalizer{},
|
||||||
nws.AlertsNormalizer{},
|
nws.AlertsNormalizer{},
|
||||||
openmeteo.ObservationNormalizer{},
|
openmeteo.ObservationNormalizer{},
|
||||||
openmeteo.ForecastNormalizer{},
|
openmeteo.ForecastNormalizer{},
|
||||||
openweather.ObservationNormalizer{},
|
openweather.ObservationNormalizer{},
|
||||||
|
spc.ConvectiveOutlookNormalizer{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(got) != len(want) {
|
if len(got) != len(want) {
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Finalize builds the output event envelope by copying the input and applying the
|
// Finalize builds the output event envelope by copying the input and applying the
|
||||||
// canonical schema/payload, plus (optionally) EffectiveAt.
|
// canonical schema/payload, plus an optional effective time.
|
||||||
//
|
//
|
||||||
// Important behavior:
|
// Important behavior:
|
||||||
// - ID/Kind/Source/EmittedAt are preserved by copying the input event.
|
// - ID/Kind/Source/EmittedAt are preserved by copying the input event.
|
||||||
// - EffectiveAt is only overwritten when effectiveAt is non-zero.
|
// - EffectiveAt is only overwritten when the supplied effective time is non-zero.
|
||||||
// If effectiveAt is zero, any existing in.EffectiveAt is preserved.
|
// If the supplied time is zero, any existing in.EffectiveAt is preserved.
|
||||||
// - Payload floats are rounded to a stable wire-friendly precision (see round.go).
|
// - Payload floats are rounded to a stable wire-friendly precision (see round.go).
|
||||||
func Finalize(in event.Event, outSchema string, outPayload any, effectiveAt time.Time) (*event.Event, error) {
|
func Finalize(in event.Event, outSchema string, outPayload any, effectiveAt time.Time) (*event.Event, error) {
|
||||||
// Enforce stable numeric presentation for weather payloads before delegating to feedkit's
|
// Enforce stable numeric presentation for weather payloads before delegating to feedkit's
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
// - sources emit raw JSON payloads (typically json.RawMessage)
|
// - sources emit raw JSON payloads (typically json.RawMessage)
|
||||||
// - normalizers decode into provider structs
|
// - normalizers decode into provider structs
|
||||||
//
|
//
|
||||||
// Errors include a small amount of stage context ("extract payload", "decode raw payload").
|
// Errors include a small amount of operation context ("extract payload", "decode raw payload").
|
||||||
// Callers typically wrap these with a provider/kind label.
|
// Callers typically wrap these with a provider/kind label.
|
||||||
func DecodeJSONPayload[T any](in event.Event) (T, error) {
|
func DecodeJSONPayload[T any](in event.Event) (T, error) {
|
||||||
return fknormalize.DecodeJSONPayload[T](in)
|
return fknormalize.DecodeJSONPayload[T](in)
|
||||||
@@ -24,15 +24,15 @@ func DecodeJSONPayload[T any](in event.Event) (T, error) {
|
|||||||
// NormalizeJSON is a convenience wrapper for the common JSON-normalizer pattern:
|
// NormalizeJSON is a convenience wrapper for the common JSON-normalizer pattern:
|
||||||
//
|
//
|
||||||
// 1. Decode raw JSON payload into provider struct T
|
// 1. Decode raw JSON payload into provider struct T
|
||||||
// 2. Map T into canonical payload P (plus an EffectiveAt timestamp)
|
// 2. Map T into canonical payload P (plus an effective time)
|
||||||
// 3. Finalize the event envelope (schema/payload/effectiveAt) + Validate
|
// 3. Finalize the event envelope (schema/payload/effective time) + Validate
|
||||||
//
|
//
|
||||||
// label should be short and specific, e.g. "openweather observation".
|
// label should be short and specific, e.g. "openweather observation".
|
||||||
// outSchema should be the canonical schema constant.
|
// outSchema should be the canonical schema constant.
|
||||||
// build should contain ONLY provider/domain mapping logic.
|
// build should contain ONLY provider/domain mapping logic.
|
||||||
//
|
//
|
||||||
// Error policy:
|
// Error policy:
|
||||||
// - NormalizeJSON wraps ALL failures with consistent context: "<label> normalize: <stage>: ..."
|
// - NormalizeJSON wraps ALL failures with consistent context: "<label> normalize: <operation>: ..."
|
||||||
// - build() should return specific errors without repeating the label prefix.
|
// - build() should return specific errors without repeating the label prefix.
|
||||||
func NormalizeJSON[T any, P any](
|
func NormalizeJSON[T any, P any](
|
||||||
in event.Event,
|
in event.Event,
|
||||||
|
|||||||
@@ -70,7 +70,7 @@
|
|||||||
//
|
//
|
||||||
// weather.<kind>.vN
|
// weather.<kind>.vN
|
||||||
//
|
//
|
||||||
// weatherfeeder centralizes schema strings in internal/standards/schema.go.
|
// weatherfeeder centralizes schema strings in standards/schema.go.
|
||||||
// Always use those constants (do not inline schema strings).
|
// Always use those constants (do not inline schema strings).
|
||||||
//
|
//
|
||||||
// Example mappings:
|
// Example mappings:
|
||||||
@@ -101,8 +101,8 @@
|
|||||||
// Every normalizer type must have a doc comment that states:
|
// Every normalizer type must have a doc comment that states:
|
||||||
//
|
//
|
||||||
// - what it converts (e.g., “OpenWeather current -> WeatherObservation”)
|
// - what it converts (e.g., “OpenWeather current -> WeatherObservation”)
|
||||||
// - which raw schema it matches (constant identifier from internal/standards)
|
// - which raw schema it matches (constant identifier from standards)
|
||||||
// - which canonical schema it produces (constant identifier from internal/standards)
|
// - which canonical schema it produces (constant identifier from standards)
|
||||||
// - any special caveats (units, day/night inference, missing fields, etc.)
|
// - any special caveats (units, day/night inference, missing fields, etc.)
|
||||||
//
|
//
|
||||||
// Including literal schema string values is optional,
|
// Including literal schema string values is optional,
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import (
|
|||||||
// 2. Alert timing fields are best-effort parsed; invalid timestamps do not fail the
|
// 2. Alert timing fields are best-effort parsed; invalid timestamps do not fail the
|
||||||
// entire normalization (they are left nil).
|
// entire normalization (they are left nil).
|
||||||
// 3. Some fields are intentionally passed through as strings (severity/urgency/etc.)
|
// 3. Some fields are intentionally passed through as strings (severity/urgency/etc.)
|
||||||
// since canonical vocabularies may evolve later.
|
// because the canonical model currently preserves provider vocabulary there.
|
||||||
type AlertsNormalizer struct{}
|
type AlertsNormalizer struct{}
|
||||||
|
|
||||||
func (AlertsNormalizer) Match(e event.Event) bool {
|
func (AlertsNormalizer) Match(e event.Event) bool {
|
||||||
@@ -37,7 +37,7 @@ func (AlertsNormalizer) Match(e event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (AlertsNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
func (AlertsNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
|
_ = ctx // normalization is pure/CPU; keep signature aligned with Normalizer.
|
||||||
|
|
||||||
// If we can't derive AsOf from the payload, fall back to the existing event envelope.
|
// If we can't derive AsOf from the payload, fall back to the existing event envelope.
|
||||||
fallbackAsOf := in.EmittedAt.UTC()
|
fallbackAsOf := in.EmittedAt.UTC()
|
||||||
|
|||||||
@@ -23,10 +23,11 @@ import (
|
|||||||
// builders by raw schema.
|
// builders by raw schema.
|
||||||
//
|
//
|
||||||
// Caveats / policy:
|
// Caveats / policy:
|
||||||
// 1. NWS forecast periods do not include METAR presentWeather phenomena, so ConditionCode
|
// 1. Hourly NWS forecast periods do not include METAR presentWeather phenomena, so
|
||||||
// is inferred from period.shortForecast (with a conservative icon-based fallback).
|
// ConditionCode is inferred from period.shortForecast (with a conservative icon fallback).
|
||||||
// 2. Temperature is converted to °C when NWS supplies °F.
|
// 2. Narrative NWS periods intentionally leave ConditionCode unset.
|
||||||
// 3. WindSpeed is parsed from strings like "9 mph" / "10 to 15 mph" and converted to km/h.
|
// 3. Temperature is converted to °C when NWS supplies °F.
|
||||||
|
// 4. WindSpeed is parsed from strings like "9 mph" / "10 to 15 mph" and converted to km/h.
|
||||||
type ForecastNormalizer struct{}
|
type ForecastNormalizer struct{}
|
||||||
|
|
||||||
func (ForecastNormalizer) Match(e event.Event) bool {
|
func (ForecastNormalizer) Match(e event.Event) bool {
|
||||||
@@ -41,7 +42,7 @@ func (ForecastNormalizer) Match(e event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ForecastNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
func (ForecastNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
|
_ = ctx // normalization is pure/CPU; keep signature aligned with Normalizer.
|
||||||
|
|
||||||
return normalizeForecastEventBySchema(in)
|
return normalizeForecastEventBySchema(in)
|
||||||
}
|
}
|
||||||
@@ -224,6 +225,7 @@ func mapHourlyForecastPeriod(idx int, p nwsHourlyForecastPeriod) (model.WeatherF
|
|||||||
// Infer WMO from shortForecast (and fall back to icon token).
|
// Infer WMO from shortForecast (and fall back to icon token).
|
||||||
providerDesc := strings.TrimSpace(p.ShortForecast)
|
providerDesc := strings.TrimSpace(p.ShortForecast)
|
||||||
wmo := wmoFromNWSForecast(providerDesc, p.Icon, tempC)
|
wmo := wmoFromNWSForecast(providerDesc, p.Icon, tempC)
|
||||||
|
wmoPtr := wmoCodePtr(wmo)
|
||||||
|
|
||||||
return model.WeatherForecastPeriod{
|
return model.WeatherForecastPeriod{
|
||||||
StartTime: start,
|
StartTime: start,
|
||||||
@@ -232,7 +234,7 @@ func mapHourlyForecastPeriod(idx int, p nwsHourlyForecastPeriod) (model.WeatherF
|
|||||||
Name: strings.TrimSpace(p.Name),
|
Name: strings.TrimSpace(p.Name),
|
||||||
IsDay: isDay,
|
IsDay: isDay,
|
||||||
|
|
||||||
ConditionCode: wmo,
|
ConditionCode: wmoPtr,
|
||||||
|
|
||||||
// For forecasts, keep provider short forecast text as the human-facing description.
|
// For forecasts, keep provider short forecast text as the human-facing description.
|
||||||
TextDescription: providerDesc,
|
TextDescription: providerDesc,
|
||||||
@@ -264,9 +266,7 @@ func mapNarrativeForecastPeriod(idx int, p nwsNarrativeForecastPeriod) (model.We
|
|||||||
|
|
||||||
tempC := tempCFromNWS(p.Temperature, p.TemperatureUnit)
|
tempC := tempCFromNWS(p.Temperature, p.TemperatureUnit)
|
||||||
|
|
||||||
// Infer WMO from shortForecast (and fall back to icon token).
|
|
||||||
shortForecast := strings.TrimSpace(p.ShortForecast)
|
shortForecast := strings.TrimSpace(p.ShortForecast)
|
||||||
wmo := wmoFromNWSForecast(shortForecast, p.Icon, tempC)
|
|
||||||
|
|
||||||
textDescription := strings.TrimSpace(p.DetailedForecast)
|
textDescription := strings.TrimSpace(p.DetailedForecast)
|
||||||
if textDescription == "" {
|
if textDescription == "" {
|
||||||
@@ -280,7 +280,7 @@ func mapNarrativeForecastPeriod(idx int, p nwsNarrativeForecastPeriod) (model.We
|
|||||||
Name: strings.TrimSpace(p.Name),
|
Name: strings.TrimSpace(p.Name),
|
||||||
IsDay: isDay,
|
IsDay: isDay,
|
||||||
|
|
||||||
ConditionCode: wmo,
|
ConditionCode: nil,
|
||||||
|
|
||||||
TextDescription: textDescription,
|
TextDescription: textDescription,
|
||||||
|
|
||||||
@@ -292,3 +292,8 @@ func mapNarrativeForecastPeriod(idx int, p nwsNarrativeForecastPeriod) (model.We
|
|||||||
ProbabilityOfPrecipitationPercent: p.ProbabilityOfPrecipitation.Value,
|
ProbabilityOfPrecipitationPercent: p.ProbabilityOfPrecipitation.Value,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wmoCodePtr(code model.WMOCode) *model.WMOCode {
|
||||||
|
out := code
|
||||||
|
return &out
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ func TestBuildHourlyForecastUsesShortForecastAsTextDescription(t *testing.T) {
|
|||||||
if got, want := run.Periods[0].TextDescription, "Mostly Cloudy"; got != want {
|
if got, want := run.Periods[0].TextDescription, "Mostly Cloudy"; got != want {
|
||||||
t.Fatalf("TextDescription = %q, want %q", got, want)
|
t.Fatalf("TextDescription = %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
|
if run.Periods[0].ConditionCode == nil {
|
||||||
|
t.Fatalf("ConditionCode is nil, want inferred hourly WMO code")
|
||||||
|
}
|
||||||
|
|
||||||
wantIssued := time.Date(2026, 3, 16, 18, 0, 0, 0, time.UTC)
|
wantIssued := time.Date(2026, 3, 16, 18, 0, 0, 0, time.UTC)
|
||||||
if !run.IssuedAt.Equal(wantIssued) {
|
if !run.IssuedAt.Equal(wantIssued) {
|
||||||
@@ -261,6 +264,9 @@ func TestBuildNarrativeForecastMapsExpectedFields(t *testing.T) {
|
|||||||
if p.ProbabilityOfPrecipitationPercent == nil || *p.ProbabilityOfPrecipitationPercent != 20 {
|
if p.ProbabilityOfPrecipitationPercent == nil || *p.ProbabilityOfPrecipitationPercent != 20 {
|
||||||
t.Fatalf("ProbabilityOfPrecipitationPercent = %v, want 20", p.ProbabilityOfPrecipitationPercent)
|
t.Fatalf("ProbabilityOfPrecipitationPercent = %v, want 20", p.ProbabilityOfPrecipitationPercent)
|
||||||
}
|
}
|
||||||
|
if p.ConditionCode != nil {
|
||||||
|
t.Fatalf("ConditionCode = %v, want nil for narrative period", p.ConditionCode)
|
||||||
|
}
|
||||||
|
|
||||||
wantIssued := time.Date(2026, 3, 27, 15, 17, 1, 0, time.UTC)
|
wantIssued := time.Date(2026, 3, 27, 15, 17, 1, 0, time.UTC)
|
||||||
if !run.IssuedAt.Equal(wantIssued) {
|
if !run.IssuedAt.Equal(wantIssued) {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func (ObservationNormalizer) Match(e event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
|
_ = ctx // normalization is pure/CPU; keep signature aligned with Normalizer.
|
||||||
|
|
||||||
return normcommon.NormalizeJSON(
|
return normcommon.NormalizeJSON(
|
||||||
in,
|
in,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ var builtins = []fknormalize.Normalizer{
|
|||||||
ObservationNormalizer{},
|
ObservationNormalizer{},
|
||||||
ForecastNormalizer{},
|
ForecastNormalizer{},
|
||||||
ForecastDiscussionNormalizer{},
|
ForecastDiscussionNormalizer{},
|
||||||
|
WeatherStoriesNormalizer{},
|
||||||
AlertsNormalizer{},
|
AlertsNormalizer{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -262,6 +262,25 @@ type nwsAlertProperties struct {
|
|||||||
References json.RawMessage `json:"references"`
|
References json.RawMessage `json:"references"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nwsWeatherStoriesResponse is a minimal representation of the NWS /weatherstories
|
||||||
|
// payload needed for mapping into model.WeatherStoryRun.
|
||||||
|
type nwsWeatherStoriesResponse struct {
|
||||||
|
Stories []nwsWeatherStory `json:"stories"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nwsWeatherStory struct {
|
||||||
|
OfficeID string `json:"officeId"`
|
||||||
|
StartTime string `json:"startTime"`
|
||||||
|
EndTime string `json:"endTime"`
|
||||||
|
UpdateTime string `json:"updateTime"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
AltText string `json:"altText"`
|
||||||
|
Priority bool `json:"priority"`
|
||||||
|
Order int `json:"order"`
|
||||||
|
Download string `json:"download"`
|
||||||
|
}
|
||||||
|
|
||||||
type nwsAlertReference struct {
|
type nwsAlertReference struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Identifier string `json:"identifier"`
|
Identifier string `json:"identifier"`
|
||||||
|
|||||||
107
internal/normalizers/nws/weatherstories.go
Normal file
107
internal/normalizers/nws/weatherstories.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package nws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
normcommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/common"
|
||||||
|
nwscommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/nws"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WeatherStoriesNormalizer converts:
|
||||||
|
//
|
||||||
|
// standards.SchemaRawNWSWeatherStoriesV1 -> standards.SchemaWeatherStoryV1
|
||||||
|
//
|
||||||
|
// It maps the NWS /weatherstories JSON response into a canonical story snapshot.
|
||||||
|
type WeatherStoriesNormalizer struct{}
|
||||||
|
|
||||||
|
func (WeatherStoriesNormalizer) Match(e event.Event) bool {
|
||||||
|
return strings.TrimSpace(e.Schema) == standards.SchemaRawNWSWeatherStoriesV1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (WeatherStoriesNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
|
_ = ctx
|
||||||
|
|
||||||
|
fallbackAsOf := in.EmittedAt.UTC()
|
||||||
|
if in.EffectiveAt != nil && !in.EffectiveAt.IsZero() {
|
||||||
|
fallbackAsOf = in.EffectiveAt.UTC()
|
||||||
|
}
|
||||||
|
|
||||||
|
return normcommon.NormalizeJSON(
|
||||||
|
in,
|
||||||
|
"nws weatherstories",
|
||||||
|
standards.SchemaWeatherStoryV1,
|
||||||
|
func(parsed nwsWeatherStoriesResponse) (model.WeatherStoryRun, time.Time, error) {
|
||||||
|
return buildWeatherStories(parsed, fallbackAsOf)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildWeatherStories(parsed nwsWeatherStoriesResponse, fallbackAsOf time.Time) (model.WeatherStoryRun, time.Time, error) {
|
||||||
|
stories := make([]model.WeatherStory, 0, len(parsed.Stories))
|
||||||
|
|
||||||
|
var officeID string
|
||||||
|
var asOf time.Time
|
||||||
|
for i, raw := range parsed.Stories {
|
||||||
|
startTime, err := parseRequiredNWSTime(raw.StartTime, fmt.Sprintf("stories[%d].startTime", i))
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherStoryRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
endTime, err := parseRequiredNWSTime(raw.EndTime, fmt.Sprintf("stories[%d].endTime", i))
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherStoryRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
updatedAt, err := parseRequiredNWSTime(raw.UpdateTime, fmt.Sprintf("stories[%d].updateTime", i))
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherStoryRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
storyOfficeID := strings.TrimSpace(raw.OfficeID)
|
||||||
|
if officeID == "" && storyOfficeID != "" {
|
||||||
|
officeID = storyOfficeID
|
||||||
|
}
|
||||||
|
if asOf.IsZero() || updatedAt.After(asOf) {
|
||||||
|
asOf = updatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
stories = append(stories, model.WeatherStory{
|
||||||
|
OfficeID: storyOfficeID,
|
||||||
|
StartTime: startTime,
|
||||||
|
EndTime: endTime,
|
||||||
|
UpdatedAt: updatedAt,
|
||||||
|
Title: strings.TrimSpace(raw.Title),
|
||||||
|
Description: strings.TrimSpace(raw.Description),
|
||||||
|
AltText: strings.TrimSpace(raw.AltText),
|
||||||
|
Priority: raw.Priority,
|
||||||
|
Order: raw.Order,
|
||||||
|
DownloadURL: strings.TrimSpace(raw.Download),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if asOf.IsZero() {
|
||||||
|
asOf = fallbackAsOf.UTC()
|
||||||
|
}
|
||||||
|
|
||||||
|
run := model.WeatherStoryRun{
|
||||||
|
OfficeID: officeID,
|
||||||
|
AsOf: asOf,
|
||||||
|
Stories: stories,
|
||||||
|
}
|
||||||
|
return run, asOf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRequiredNWSTime(raw, field string) (time.Time, error) {
|
||||||
|
if strings.TrimSpace(raw) == "" {
|
||||||
|
return time.Time{}, fmt.Errorf("%s is required", field)
|
||||||
|
}
|
||||||
|
t, err := nwscommon.ParseTime(raw)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, fmt.Errorf("%s: %w", field, err)
|
||||||
|
}
|
||||||
|
return t.UTC(), nil
|
||||||
|
}
|
||||||
146
internal/normalizers/nws/weatherstories_test.go
Normal file
146
internal/normalizers/nws/weatherstories_test.go
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
package nws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWeatherStoriesNormalizerProducesCanonicalSchemaAndMapsSample(t *testing.T) {
|
||||||
|
out, err := (WeatherStoriesNormalizer{}).Normalize(nil, weatherStoriesRawEvent(weatherStoriesSamplePayload()))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
if out == nil {
|
||||||
|
t.Fatalf("Normalize() returned nil output")
|
||||||
|
}
|
||||||
|
if out.Schema != standards.SchemaWeatherStoryV1 {
|
||||||
|
t.Fatalf("Schema = %q, want %q", out.Schema, standards.SchemaWeatherStoryV1)
|
||||||
|
}
|
||||||
|
if out.Kind != event.Kind("weather_story") {
|
||||||
|
t.Fatalf("Kind = %q, want weather_story", out.Kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, ok := out.Payload.(model.WeatherStoryRun)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Payload type = %T, want model.WeatherStoryRun", out.Payload)
|
||||||
|
}
|
||||||
|
if payload.OfficeID != "LSX" {
|
||||||
|
t.Fatalf("OfficeID = %q, want LSX", payload.OfficeID)
|
||||||
|
}
|
||||||
|
wantAsOf := time.Date(2026, 5, 30, 9, 0, 34, 0, time.UTC)
|
||||||
|
if !payload.AsOf.Equal(wantAsOf) {
|
||||||
|
t.Fatalf("AsOf = %s, want %s", payload.AsOf, wantAsOf)
|
||||||
|
}
|
||||||
|
if out.EffectiveAt == nil || !out.EffectiveAt.Equal(wantAsOf) {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want %s", out.EffectiveAt, wantAsOf)
|
||||||
|
}
|
||||||
|
if len(payload.Stories) != 1 {
|
||||||
|
t.Fatalf("Stories len = %d, want 1", len(payload.Stories))
|
||||||
|
}
|
||||||
|
story := payload.Stories[0]
|
||||||
|
if story.Title != "Several Chances for Rain Through Monday" {
|
||||||
|
t.Fatalf("Title = %q", story.Title)
|
||||||
|
}
|
||||||
|
if story.Description != "A stagnant weather pattern." {
|
||||||
|
t.Fatalf("Description = %q", story.Description)
|
||||||
|
}
|
||||||
|
if story.AltText != "This slide shows the forecast." {
|
||||||
|
t.Fatalf("AltText = %q", story.AltText)
|
||||||
|
}
|
||||||
|
if story.Priority {
|
||||||
|
t.Fatalf("Priority = true, want false")
|
||||||
|
}
|
||||||
|
if story.Order != 1 {
|
||||||
|
t.Fatalf("Order = %d, want 1", story.Order)
|
||||||
|
}
|
||||||
|
if story.DownloadURL != "https://api.weather.gov/offices/LSX/weatherstories/download/3228e499-2aae-45a8-9ff9-1c060311026f" {
|
||||||
|
t.Fatalf("DownloadURL = %q", story.DownloadURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherStoriesNormalizerEmptyStoriesUsesFallbackAsOf(t *testing.T) {
|
||||||
|
effectiveAt := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
|
||||||
|
in := weatherStoriesRawEvent(`{"stories":[]}`)
|
||||||
|
in.EffectiveAt = &effectiveAt
|
||||||
|
|
||||||
|
out, err := (WeatherStoriesNormalizer{}).Normalize(nil, in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
payload, ok := out.Payload.(model.WeatherStoryRun)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Payload type = %T, want model.WeatherStoryRun", out.Payload)
|
||||||
|
}
|
||||||
|
if !payload.AsOf.Equal(effectiveAt) {
|
||||||
|
t.Fatalf("AsOf = %s, want fallback %s", payload.AsOf, effectiveAt)
|
||||||
|
}
|
||||||
|
if payload.Stories == nil {
|
||||||
|
t.Fatalf("Stories = nil, want empty slice")
|
||||||
|
}
|
||||||
|
if len(payload.Stories) != 0 {
|
||||||
|
t.Fatalf("Stories len = %d, want 0", len(payload.Stories))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherStoriesNormalizerRejectsInvalidRequiredStoryTime(t *testing.T) {
|
||||||
|
_, err := (WeatherStoriesNormalizer{}).Normalize(nil, weatherStoriesRawEvent(`{
|
||||||
|
"stories": [{
|
||||||
|
"officeId": "LSX",
|
||||||
|
"startTime": "bad",
|
||||||
|
"endTime": "2026-05-31T11:00:00+00:00",
|
||||||
|
"updateTime": "2026-05-30T09:00:34+00:00"
|
||||||
|
}]
|
||||||
|
}`))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Normalize() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "stories[0].startTime") {
|
||||||
|
t.Fatalf("error = %q, want field context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherStoriesNormalizerMatch(t *testing.T) {
|
||||||
|
n := WeatherStoriesNormalizer{}
|
||||||
|
if !n.Match(event.Event{Schema: standards.SchemaRawNWSWeatherStoriesV1}) {
|
||||||
|
t.Fatalf("Match(raw weatherstories) = false, want true")
|
||||||
|
}
|
||||||
|
if n.Match(event.Event{Schema: standards.SchemaRawNWSAlertsV1}) {
|
||||||
|
t.Fatalf("Match(raw alerts) = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherStoriesRawEvent(payload string) event.Event {
|
||||||
|
return event.Event{
|
||||||
|
ID: "evt-weatherstories-1",
|
||||||
|
Kind: event.Kind("weather_story"),
|
||||||
|
Source: "nws-weatherstories-test",
|
||||||
|
EmittedAt: time.Date(2026, 5, 30, 9, 5, 0, 0, time.UTC),
|
||||||
|
Schema: standards.SchemaRawNWSWeatherStoriesV1,
|
||||||
|
Payload: json.RawMessage(payload),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherStoriesSamplePayload() string {
|
||||||
|
return `{
|
||||||
|
"stories": [
|
||||||
|
{
|
||||||
|
"officeId": " LSX ",
|
||||||
|
"startTime": "2026-05-30T08:46:00+00:00",
|
||||||
|
"endTime": "2026-05-31T11:00:00+00:00",
|
||||||
|
"updateTime": "2026-05-30T09:00:34+00:00",
|
||||||
|
"title": " Several Chances for Rain Through Monday ",
|
||||||
|
"description": " A stagnant weather pattern. ",
|
||||||
|
"altText": " This slide shows the forecast. ",
|
||||||
|
"priority": false,
|
||||||
|
"order": 1,
|
||||||
|
"download": " https://api.weather.gov/offices/LSX/weatherstories/download/3228e499-2aae-45a8-9ff9-1c060311026f "
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
}
|
||||||
@@ -33,7 +33,7 @@ func (ForecastNormalizer) Match(e event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ForecastNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
func (ForecastNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
|
_ = ctx // normalization is pure/CPU; keep signature aligned with Normalizer.
|
||||||
|
|
||||||
// If present, prefer the existing event EmittedAt as IssuedAt.
|
// If present, prefer the existing event EmittedAt as IssuedAt.
|
||||||
var fallbackIssued time.Time
|
var fallbackIssued time.Time
|
||||||
@@ -98,6 +98,7 @@ func buildForecast(parsed omForecastResponse, fallbackIssued time.Time) (model.W
|
|||||||
}
|
}
|
||||||
|
|
||||||
wmo := wmoAt(parsed.Hourly.WeatherCode, i)
|
wmo := wmoAt(parsed.Hourly.WeatherCode, i)
|
||||||
|
wmoPtr := wmoCodePtr(wmo)
|
||||||
canonicalText := standards.WMOText(wmo, isDay)
|
canonicalText := standards.WMOText(wmo, isDay)
|
||||||
|
|
||||||
period := model.WeatherForecastPeriod{
|
period := model.WeatherForecastPeriod{
|
||||||
@@ -107,7 +108,7 @@ func buildForecast(parsed omForecastResponse, fallbackIssued time.Time) (model.W
|
|||||||
Name: "",
|
Name: "",
|
||||||
IsDay: isDay,
|
IsDay: isDay,
|
||||||
|
|
||||||
ConditionCode: wmo,
|
ConditionCode: wmoPtr,
|
||||||
TextDescription: canonicalText,
|
TextDescription: canonicalText,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,3 +238,8 @@ func wmoAt(vals []*int, idx int) model.WMOCode {
|
|||||||
}
|
}
|
||||||
return model.WMOUnknown
|
return model.WMOUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wmoCodePtr(code model.WMOCode) *model.WMOCode {
|
||||||
|
out := code
|
||||||
|
return &out
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ func TestBuildForecastUsesCanonicalTextDescription(t *testing.T) {
|
|||||||
if got := run.Periods[0].TextDescription; got != expectedText {
|
if got := run.Periods[0].TextDescription; got != expectedText {
|
||||||
t.Fatalf("TextDescription = %q, want %q", got, expectedText)
|
t.Fatalf("TextDescription = %q, want %q", got, expectedText)
|
||||||
}
|
}
|
||||||
|
if run.Periods[0].ConditionCode == nil {
|
||||||
|
t.Fatalf("ConditionCode is nil, want mapped WMO code")
|
||||||
|
}
|
||||||
|
|
||||||
wantIssued := time.Date(2026, 3, 16, 19, 0, 0, 0, time.UTC)
|
wantIssued := time.Date(2026, 3, 16, 19, 0, 0, 0, time.UTC)
|
||||||
if !run.IssuedAt.Equal(wantIssued) {
|
if !run.IssuedAt.Equal(wantIssued) {
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func (ObservationNormalizer) Match(e event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
|
_ = ctx // normalization is pure/CPU; keep signature aligned with Normalizer.
|
||||||
|
|
||||||
return normcommon.NormalizeJSON(
|
return normcommon.NormalizeJSON(
|
||||||
in,
|
in,
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ import (
|
|||||||
normcommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/common"
|
normcommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This file holds provider-specific helpers that are shared across multiple
|
// This file holds provider-specific helpers for OpenWeather normalizers.
|
||||||
// OpenWeather normalizers (observations today; forecasts/alerts later).
|
|
||||||
// Keeping these out of observation.go helps preserve the "one normalizer per file"
|
// Keeping these out of observation.go helps preserve the "one normalizer per file"
|
||||||
// convention while avoiding duplication.
|
// convention while avoiding duplication.
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func (ObservationNormalizer) Match(e event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
func (ObservationNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
_ = ctx // normalization is pure/CPU; keep ctx for future expensive steps
|
_ = ctx // normalization is pure/CPU; keep signature aligned with Normalizer.
|
||||||
|
|
||||||
return normcommon.NormalizeJSON(
|
return normcommon.NormalizeJSON(
|
||||||
in,
|
in,
|
||||||
|
|||||||
313
internal/normalizers/spc/convective_outlook.go
Normal file
313
internal/normalizers/spc/convective_outlook.go
Normal file
@@ -0,0 +1,313 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/geo"
|
||||||
|
normcommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/normalizers/common"
|
||||||
|
spcprovider "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/spc"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
providerSPC = "spc"
|
||||||
|
productConvective = "convective"
|
||||||
|
outlookNormalizer = "spc convective outlook"
|
||||||
|
outlookKind = "outlook"
|
||||||
|
outlookTypeUnknown = 99
|
||||||
|
)
|
||||||
|
|
||||||
|
var idTokenRE = regexp.MustCompile(`[^a-z0-9]+`)
|
||||||
|
|
||||||
|
// ConvectiveOutlookNormalizer converts:
|
||||||
|
//
|
||||||
|
// standards.SchemaRawSPCConvectiveOutlookV1 -> standards.SchemaWeatherOutlookV1
|
||||||
|
//
|
||||||
|
// It maps SPC GeoJSON outlook features into canonical outlook polygons and
|
||||||
|
// enriches each day with the matching required print-page discussion.
|
||||||
|
type ConvectiveOutlookNormalizer struct{}
|
||||||
|
|
||||||
|
func (ConvectiveOutlookNormalizer) Match(e event.Event) bool {
|
||||||
|
return strings.TrimSpace(e.Schema) == standards.SchemaRawSPCConvectiveOutlookV1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ConvectiveOutlookNormalizer) Normalize(ctx context.Context, in event.Event) (*event.Event, error) {
|
||||||
|
_ = ctx
|
||||||
|
|
||||||
|
fallbackAsOf := in.EmittedAt.UTC()
|
||||||
|
if in.EffectiveAt != nil && !in.EffectiveAt.IsZero() {
|
||||||
|
fallbackAsOf = in.EffectiveAt.UTC()
|
||||||
|
}
|
||||||
|
|
||||||
|
return normcommon.NormalizeJSON(
|
||||||
|
in,
|
||||||
|
outlookNormalizer,
|
||||||
|
standards.SchemaWeatherOutlookV1,
|
||||||
|
func(parsed spcprovider.RawConvectiveOutlookBundle) (model.WeatherOutlookRun, time.Time, error) {
|
||||||
|
return buildConvectiveOutlook(parsed, fallbackAsOf)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildConvectiveOutlook(bundle spcprovider.RawConvectiveOutlookBundle, fallbackAsOf time.Time) (model.WeatherOutlookRun, time.Time, error) {
|
||||||
|
if err := validateCoordinates(bundle.Latitude, bundle.Longitude); err != nil {
|
||||||
|
return model.WeatherOutlookRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
discussions, latestDiscussionUpdated, err := parseDiscussions(bundle.Discussions)
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlookRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
products := orderedProducts(bundle.Products)
|
||||||
|
point := geo.Point{Latitude: bundle.Latitude, Longitude: bundle.Longitude}
|
||||||
|
outlooks := make([]model.WeatherOutlook, 0)
|
||||||
|
var latestIssue time.Time
|
||||||
|
|
||||||
|
for _, product := range products {
|
||||||
|
if err := validateProductMetadata(product); err != nil {
|
||||||
|
return model.WeatherOutlookRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
discussion, ok := discussions[product.Day]
|
||||||
|
if !ok {
|
||||||
|
return model.WeatherOutlookRun{}, time.Time{}, fmt.Errorf("product %s: discussion for day %d is required", product.Key, product.Day)
|
||||||
|
}
|
||||||
|
|
||||||
|
collection, err := spcprovider.DecodeGeoJSON(product.Body)
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlookRun{}, time.Time{}, fmt.Errorf("product %s: %w", product.Key, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, feature := range collection.Features {
|
||||||
|
outlook, err := mapFeature(product, feature, i, point, discussion)
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlookRun{}, time.Time{}, err
|
||||||
|
}
|
||||||
|
if latestIssue.IsZero() || outlook.IssuedAt.After(latestIssue) {
|
||||||
|
latestIssue = outlook.IssuedAt
|
||||||
|
}
|
||||||
|
outlooks = append(outlooks, outlook)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
asOf := latestIssue
|
||||||
|
if asOf.IsZero() {
|
||||||
|
asOf = latestDiscussionUpdated
|
||||||
|
}
|
||||||
|
if asOf.IsZero() {
|
||||||
|
asOf = fallbackAsOf.UTC()
|
||||||
|
}
|
||||||
|
|
||||||
|
var issuedAt *time.Time
|
||||||
|
if !latestIssue.IsZero() {
|
||||||
|
t := latestIssue.UTC()
|
||||||
|
issuedAt = &t
|
||||||
|
}
|
||||||
|
|
||||||
|
lat := bundle.Latitude
|
||||||
|
lon := bundle.Longitude
|
||||||
|
run := model.WeatherOutlookRun{
|
||||||
|
LocationID: strings.TrimSpace(bundle.LocationID),
|
||||||
|
LocationName: strings.TrimSpace(bundle.LocationName),
|
||||||
|
Latitude: &lat,
|
||||||
|
Longitude: &lon,
|
||||||
|
AsOf: asOf.UTC(),
|
||||||
|
IssuedAt: issuedAt,
|
||||||
|
Outlooks: outlooks,
|
||||||
|
}
|
||||||
|
return run, run.AsOf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type parsedDiscussion struct {
|
||||||
|
Headline string
|
||||||
|
Summary string
|
||||||
|
Discussion string
|
||||||
|
UpdatedAt *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseDiscussions(pages []spcprovider.RawDiscussionPage) (map[int]parsedDiscussion, time.Time, error) {
|
||||||
|
out := map[int]parsedDiscussion{}
|
||||||
|
var latestUpdated time.Time
|
||||||
|
for _, page := range pages {
|
||||||
|
parsed, err := spcprovider.ParseDiscussionHTML(page.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, time.Time{}, fmt.Errorf("discussion %s: %w", page.Key, err)
|
||||||
|
}
|
||||||
|
day := page.Day
|
||||||
|
if day == 0 {
|
||||||
|
if meta, ok := spcprovider.DiscussionProductByKey(page.Key); ok {
|
||||||
|
day = meta.Day
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if day < 1 || day > 3 {
|
||||||
|
return nil, time.Time{}, fmt.Errorf("discussion %s: day must be 1, 2, or 3, got %d", page.Key, page.Day)
|
||||||
|
}
|
||||||
|
disc := parsedDiscussion{
|
||||||
|
Headline: strings.TrimSpace(parsed.Headline),
|
||||||
|
Summary: strings.TrimSpace(parsed.Summary),
|
||||||
|
Discussion: strings.TrimSpace(parsed.Discussion),
|
||||||
|
UpdatedAt: parsed.UpdatedAt,
|
||||||
|
}
|
||||||
|
out[day] = disc
|
||||||
|
if parsed.UpdatedAt != nil && (latestUpdated.IsZero() || parsed.UpdatedAt.After(latestUpdated)) {
|
||||||
|
latestUpdated = parsed.UpdatedAt.UTC()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, latestUpdated, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func orderedProducts(products []spcprovider.RawOutlookProduct) []spcprovider.RawOutlookProduct {
|
||||||
|
out := make([]spcprovider.RawOutlookProduct, len(products))
|
||||||
|
copy(out, products)
|
||||||
|
sort.SliceStable(out, func(i, j int) bool {
|
||||||
|
if out[i].Day != out[j].Day {
|
||||||
|
return out[i].Day < out[j].Day
|
||||||
|
}
|
||||||
|
left := outlookTypeOrder(out[i].OutlookType)
|
||||||
|
right := outlookTypeOrder(out[j].OutlookType)
|
||||||
|
if left != right {
|
||||||
|
return left < right
|
||||||
|
}
|
||||||
|
return out[i].Key < out[j].Key
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func outlookTypeOrder(outlookType string) int {
|
||||||
|
switch strings.TrimSpace(outlookType) {
|
||||||
|
case spcprovider.OutlookTypeCategorical:
|
||||||
|
return 0
|
||||||
|
case spcprovider.OutlookTypeTornado:
|
||||||
|
return 1
|
||||||
|
case spcprovider.OutlookTypeHail:
|
||||||
|
return 2
|
||||||
|
case spcprovider.OutlookTypeWind:
|
||||||
|
return 3
|
||||||
|
default:
|
||||||
|
return outlookTypeUnknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateProductMetadata(product spcprovider.RawOutlookProduct) error {
|
||||||
|
if product.Day < 1 || product.Day > 3 {
|
||||||
|
return fmt.Errorf("product %s: day must be 1, 2, or 3, got %d", product.Key, product.Day)
|
||||||
|
}
|
||||||
|
switch strings.TrimSpace(product.OutlookType) {
|
||||||
|
case spcprovider.OutlookTypeCategorical, spcprovider.OutlookTypeTornado, spcprovider.OutlookTypeHail, spcprovider.OutlookTypeWind:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("product %s: unsupported outlook type %q", product.Key, product.OutlookType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapFeature(product spcprovider.RawOutlookProduct, feature spcprovider.GeoJSONFeature, index int, point geo.Point, discussion parsedDiscussion) (model.WeatherOutlook, error) {
|
||||||
|
fieldPrefix := fmt.Sprintf("product %s feature %d", product.Key, index)
|
||||||
|
props := feature.Properties
|
||||||
|
|
||||||
|
validFrom, err := parseRequiredSPCTime(props.ValidISO, fieldPrefix+".VALID_ISO")
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlook{}, err
|
||||||
|
}
|
||||||
|
validTo, err := parseRequiredSPCTime(props.ExpireISO, fieldPrefix+".EXPIRE_ISO")
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlook{}, err
|
||||||
|
}
|
||||||
|
issuedAt, err := parseRequiredSPCTime(props.IssueISO, fieldPrefix+".ISSUE_ISO")
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlook{}, err
|
||||||
|
}
|
||||||
|
label := strings.TrimSpace(props.Label)
|
||||||
|
if label == "" {
|
||||||
|
return model.WeatherOutlook{}, fmt.Errorf("%s.LABEL is required", fieldPrefix)
|
||||||
|
}
|
||||||
|
if len(feature.Geometry) == 0 {
|
||||||
|
return model.WeatherOutlook{}, fmt.Errorf("%s.geometry is required", fieldPrefix)
|
||||||
|
}
|
||||||
|
containsLocation, err := geo.ContainsPoint(feature.Geometry, point)
|
||||||
|
if err != nil {
|
||||||
|
return model.WeatherOutlook{}, fmt.Errorf("%s.geometry: %w", fieldPrefix, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
geometry := make(json.RawMessage, len(feature.Geometry))
|
||||||
|
copy(geometry, feature.Geometry)
|
||||||
|
|
||||||
|
return model.WeatherOutlook{
|
||||||
|
ID: outlookID(product.Day, product.OutlookType, label, issuedAt, validFrom, index),
|
||||||
|
Provider: providerSPC,
|
||||||
|
Product: productConvective,
|
||||||
|
Day: product.Day,
|
||||||
|
OutlookType: strings.TrimSpace(product.OutlookType),
|
||||||
|
Label: label,
|
||||||
|
LabelText: strings.TrimSpace(props.Label2),
|
||||||
|
SeverityRank: props.DN,
|
||||||
|
ValidFrom: validFrom,
|
||||||
|
ValidTo: validTo,
|
||||||
|
IssuedAt: issuedAt,
|
||||||
|
ExpiresAt: validTo,
|
||||||
|
Forecaster: strings.TrimSpace(props.Forecaster),
|
||||||
|
Headline: discussion.Headline,
|
||||||
|
Summary: discussion.Summary,
|
||||||
|
Discussion: discussion.Discussion,
|
||||||
|
SourceURL: strings.TrimSpace(product.URL),
|
||||||
|
ImageURL: "",
|
||||||
|
ContainsLocation: containsLocation,
|
||||||
|
Geometry: geometry,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRequiredSPCTime(value, field string) (time.Time, error) {
|
||||||
|
if strings.TrimSpace(value) == "" {
|
||||||
|
return time.Time{}, fmt.Errorf("%s is required", field)
|
||||||
|
}
|
||||||
|
t, err := spcprovider.ParseISOTimestamp(value)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, fmt.Errorf("%s: %w", field, err)
|
||||||
|
}
|
||||||
|
return t.UTC(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func outlookID(day int, outlookType, label string, issuedAt time.Time, validFrom time.Time, index int) string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"spc-convective-day%d-%s-%s-%s-%s-%d",
|
||||||
|
day,
|
||||||
|
safeIDToken(outlookType),
|
||||||
|
safeIDToken(label),
|
||||||
|
issuedAt.UTC().Format(time.RFC3339),
|
||||||
|
validFrom.UTC().Format(time.RFC3339),
|
||||||
|
index,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func safeIDToken(value string) string {
|
||||||
|
value = strings.ToLower(strings.TrimSpace(value))
|
||||||
|
value = idTokenRE.ReplaceAllString(value, "-")
|
||||||
|
value = strings.Trim(value, "-")
|
||||||
|
if value == "" {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateCoordinates(latitude, longitude float64) error {
|
||||||
|
switch {
|
||||||
|
case math.IsNaN(latitude) || math.IsInf(latitude, 0):
|
||||||
|
return fmt.Errorf("latitude must be finite")
|
||||||
|
case math.IsNaN(longitude) || math.IsInf(longitude, 0):
|
||||||
|
return fmt.Errorf("longitude must be finite")
|
||||||
|
case latitude < -90 || latitude > 90:
|
||||||
|
return fmt.Errorf("latitude must be between -90 and 90, got %v", latitude)
|
||||||
|
case longitude < -180 || longitude > 180:
|
||||||
|
return fmt.Errorf("longitude must be between -180 and 180, got %v", longitude)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
357
internal/normalizers/spc/convective_outlook_test.go
Normal file
357
internal/normalizers/spc/convective_outlook_test.go
Normal file
@@ -0,0 +1,357 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
spcprovider "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/spc"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/model"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerMatch(t *testing.T) {
|
||||||
|
n := ConvectiveOutlookNormalizer{}
|
||||||
|
if !n.Match(event.Event{Schema: standards.SchemaRawSPCConvectiveOutlookV1}) {
|
||||||
|
t.Fatalf("Match(raw SPC outlook) = false, want true")
|
||||||
|
}
|
||||||
|
if n.Match(event.Event{Schema: standards.SchemaRawNWSAlertsV1}) {
|
||||||
|
t.Fatalf("Match(raw NWS alerts) = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerProducesCanonicalSchemaAndMapsSample(t *testing.T) {
|
||||||
|
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, spcBundle(t, 38.5, -90.5)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
if out.Schema != standards.SchemaWeatherOutlookV1 {
|
||||||
|
t.Fatalf("Schema = %q, want %q", out.Schema, standards.SchemaWeatherOutlookV1)
|
||||||
|
}
|
||||||
|
if out.Kind != event.Kind("outlook") {
|
||||||
|
t.Fatalf("Kind = %q, want outlook", out.Kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
run, ok := out.Payload.(model.WeatherOutlookRun)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Payload type = %T, want model.WeatherOutlookRun", out.Payload)
|
||||||
|
}
|
||||||
|
wantAsOf := time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC)
|
||||||
|
if !run.AsOf.Equal(wantAsOf) {
|
||||||
|
t.Fatalf("AsOf = %s, want %s", run.AsOf, wantAsOf)
|
||||||
|
}
|
||||||
|
if run.IssuedAt == nil || !run.IssuedAt.Equal(wantAsOf) {
|
||||||
|
t.Fatalf("IssuedAt = %v, want %s", run.IssuedAt, wantAsOf)
|
||||||
|
}
|
||||||
|
if out.EffectiveAt == nil || !out.EffectiveAt.Equal(wantAsOf) {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want %s", out.EffectiveAt, wantAsOf)
|
||||||
|
}
|
||||||
|
if run.LocationID != "stl" || run.LocationName != "St. Louis, MO" {
|
||||||
|
t.Fatalf("location metadata = %q/%q", run.LocationID, run.LocationName)
|
||||||
|
}
|
||||||
|
if run.Latitude == nil || *run.Latitude != 38.5 || run.Longitude == nil || *run.Longitude != -90.5 {
|
||||||
|
t.Fatalf("coordinates = %v,%v", run.Latitude, run.Longitude)
|
||||||
|
}
|
||||||
|
if len(run.Outlooks) != 12 {
|
||||||
|
t.Fatalf("Outlooks length = %d, want 12", len(run.Outlooks))
|
||||||
|
}
|
||||||
|
|
||||||
|
got := run.Outlooks[0]
|
||||||
|
if got.Provider != "spc" || got.Product != "convective" {
|
||||||
|
t.Fatalf("provider/product = %q/%q", got.Provider, got.Product)
|
||||||
|
}
|
||||||
|
if got.Day != 1 || got.OutlookType != spcprovider.OutlookTypeCategorical {
|
||||||
|
t.Fatalf("day/type = %d/%q", got.Day, got.OutlookType)
|
||||||
|
}
|
||||||
|
if got.Label != "SLGT" || got.LabelText != "Slight Risk" {
|
||||||
|
t.Fatalf("label fields = %q/%q", got.Label, got.LabelText)
|
||||||
|
}
|
||||||
|
if got.SeverityRank == nil || *got.SeverityRank != 3 {
|
||||||
|
t.Fatalf("SeverityRank = %v, want 3", got.SeverityRank)
|
||||||
|
}
|
||||||
|
assertTime(t, "ValidFrom", got.ValidFrom, 2026, 6, 11, 13, 0, 0)
|
||||||
|
assertTime(t, "ValidTo", got.ValidTo, 2026, 6, 12, 12, 0, 0)
|
||||||
|
assertTime(t, "IssuedAt", got.IssuedAt, 2026, 6, 11, 12, 34, 56)
|
||||||
|
assertTime(t, "ExpiresAt", got.ExpiresAt, 2026, 6, 12, 12, 0, 0)
|
||||||
|
if got.Forecaster != "SMITH" {
|
||||||
|
t.Fatalf("Forecaster = %q, want SMITH", got.Forecaster)
|
||||||
|
}
|
||||||
|
if got.SourceURL != "https://example.invalid/day1_categorical.geojson" {
|
||||||
|
t.Fatalf("SourceURL = %q", got.SourceURL)
|
||||||
|
}
|
||||||
|
wantGeometry := `{"type":"Polygon","coordinates":[[[-91.0,38.0],[-90.0,38.0],[-90.0,39.0],[-91.0,39.0],[-91.0,38.0]]]}`
|
||||||
|
if string(got.Geometry) != wantGeometry {
|
||||||
|
t.Fatalf("Geometry = %s, want %s", got.Geometry, wantGeometry)
|
||||||
|
}
|
||||||
|
if !got.ContainsLocation {
|
||||||
|
t.Fatalf("ContainsLocation = false, want true")
|
||||||
|
}
|
||||||
|
if got.Headline != "Day 1 Convective Outlook" {
|
||||||
|
t.Fatalf("Headline = %q", got.Headline)
|
||||||
|
}
|
||||||
|
if !strings.Contains(got.Summary, "central Plains") {
|
||||||
|
t.Fatalf("Summary = %q", got.Summary)
|
||||||
|
}
|
||||||
|
if !strings.Contains(got.Discussion, "...DISCUSSION...") {
|
||||||
|
t.Fatalf("Discussion missing product text: %q", got.Discussion)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(got.Discussion, "SPC AC 111234") {
|
||||||
|
t.Fatalf("Discussion = %q, want SPC product code prefix", got.Discussion)
|
||||||
|
}
|
||||||
|
if got.ID != "spc-convective-day1-categorical-slgt-2026-06-11T12:34:56Z-2026-06-11T13:00:00Z-0" {
|
||||||
|
t.Fatalf("ID = %q", got.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerOrdersProductsByDayAndType(t *testing.T) {
|
||||||
|
bundle := spcBundle(t, 0, 0)
|
||||||
|
for i, j := 0, len(bundle.Products)-1; i < j; i, j = i+1, j-1 {
|
||||||
|
bundle.Products[i], bundle.Products[j] = bundle.Products[j], bundle.Products[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, bundle))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
run := out.Payload.(model.WeatherOutlookRun)
|
||||||
|
got := []string{
|
||||||
|
run.Outlooks[0].OutlookType,
|
||||||
|
run.Outlooks[1].OutlookType,
|
||||||
|
run.Outlooks[2].OutlookType,
|
||||||
|
run.Outlooks[3].OutlookType,
|
||||||
|
}
|
||||||
|
want := []string{
|
||||||
|
spcprovider.OutlookTypeCategorical,
|
||||||
|
spcprovider.OutlookTypeTornado,
|
||||||
|
spcprovider.OutlookTypeHail,
|
||||||
|
spcprovider.OutlookTypeWind,
|
||||||
|
}
|
||||||
|
for i := range want {
|
||||||
|
if got[i] != want[i] || run.Outlooks[i].Day != 1 {
|
||||||
|
t.Fatalf("outlook[%d] = day %d type %q, want day 1 type %q", i, run.Outlooks[i].Day, got[i], want[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerMapsProbabilisticOutlookTypes(t *testing.T) {
|
||||||
|
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, spcBundle(t, 0, 0)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
run := out.Payload.(model.WeatherOutlookRun)
|
||||||
|
for _, outlookType := range []string{
|
||||||
|
spcprovider.OutlookTypeTornado,
|
||||||
|
spcprovider.OutlookTypeHail,
|
||||||
|
spcprovider.OutlookTypeWind,
|
||||||
|
} {
|
||||||
|
if findOutlook(run.Outlooks, 1, outlookType) == nil {
|
||||||
|
t.Fatalf("missing day 1 outlook type %q", outlookType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerContainsLocationFalseOutsidePolygon(t *testing.T) {
|
||||||
|
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, spcBundle(t, 0, 0)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
run := out.Payload.(model.WeatherOutlookRun)
|
||||||
|
if run.Outlooks[0].ContainsLocation {
|
||||||
|
t.Fatalf("ContainsLocation = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerPreservesCorrectionMarker(t *testing.T) {
|
||||||
|
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, spcBundle(t, 0, 0)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
run := out.Payload.(model.WeatherOutlookRun)
|
||||||
|
got := findOutlook(run.Outlooks, 2, spcprovider.OutlookTypeTornado)
|
||||||
|
if got == nil {
|
||||||
|
t.Fatalf("missing day 2 tornado outlook")
|
||||||
|
}
|
||||||
|
if !strings.Contains(got.Headline, "CORR 1") {
|
||||||
|
t.Fatalf("Headline = %q, want correction marker", got.Headline)
|
||||||
|
}
|
||||||
|
if !strings.Contains(got.Discussion, "CORR 1") {
|
||||||
|
t.Fatalf("Discussion = %q, want correction marker", got.Discussion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerMissingRSSNormalizes(t *testing.T) {
|
||||||
|
bundle := spcBundle(t, 0, 0)
|
||||||
|
bundle.RSS = nil
|
||||||
|
if _, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, bundle)); err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerInvalidRequiredTimestampFailsWithContext(t *testing.T) {
|
||||||
|
bundle := spcBundle(t, 0, 0)
|
||||||
|
bundle.Products[0].Body = json.RawMessage(strings.Replace(
|
||||||
|
string(bundle.Products[0].Body),
|
||||||
|
`"ISSUE_ISO": "2026-06-11T12:34:56Z"`,
|
||||||
|
`"ISSUE_ISO": "bad"`,
|
||||||
|
1,
|
||||||
|
))
|
||||||
|
|
||||||
|
_, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, bundle))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Normalize() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "product day1_categorical feature 0.ISSUE_ISO") {
|
||||||
|
t.Fatalf("error = %q, want product and feature context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerInvalidGeometryFailsWithContext(t *testing.T) {
|
||||||
|
bundle := spcBundle(t, 0, 0)
|
||||||
|
bundle.Products[0].Body = json.RawMessage(strings.Replace(
|
||||||
|
string(bundle.Products[0].Body),
|
||||||
|
`"geometry": {`,
|
||||||
|
`"geometry": {"type":"LineString","coordinates":[[-91,38],[-90,39]]}, "oldGeometry": {`,
|
||||||
|
1,
|
||||||
|
))
|
||||||
|
|
||||||
|
_, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, bundle))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Normalize() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "product day1_categorical feature 0.geometry") {
|
||||||
|
t.Fatalf("error = %q, want product and feature context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerRejectsMissingLabel(t *testing.T) {
|
||||||
|
bundle := spcBundle(t, 0, 0)
|
||||||
|
bundle.Products[0].Body = json.RawMessage(strings.Replace(
|
||||||
|
string(bundle.Products[0].Body),
|
||||||
|
`"LABEL": "SLGT"`,
|
||||||
|
`"LABEL": ""`,
|
||||||
|
1,
|
||||||
|
))
|
||||||
|
|
||||||
|
_, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, bundle))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Normalize() error = nil, want error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "product day1_categorical feature 0.LABEL") {
|
||||||
|
t.Fatalf("error = %q, want label context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookNormalizerOutputJSONShape(t *testing.T) {
|
||||||
|
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, spcRawEvent(t, spcBundle(t, 38.5, -90.5)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
|
}
|
||||||
|
raw, err := json.Marshal(out.Payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Marshal(payload) error = %v", err)
|
||||||
|
}
|
||||||
|
got := string(raw)
|
||||||
|
for _, want := range []string{`"asOf"`, `"outlooks"`, `"containsLocation"`, `"geometry"`} {
|
||||||
|
if !strings.Contains(got, want) {
|
||||||
|
t.Fatalf("payload JSON missing %s: %s", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, unwanted := range []string{`"products"`, `"discussions"`, `"fetchedAt"`, `"body"`} {
|
||||||
|
if strings.Contains(got, unwanted) {
|
||||||
|
t.Fatalf("payload JSON exposed raw key %s: %s", unwanted, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func spcRawEvent(t *testing.T, bundle spcprovider.RawConvectiveOutlookBundle) event.Event {
|
||||||
|
t.Helper()
|
||||||
|
raw, err := json.Marshal(bundle)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Marshal(bundle) error = %v", err)
|
||||||
|
}
|
||||||
|
effectiveAt := time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC)
|
||||||
|
return event.Event{
|
||||||
|
ID: "evt-spc-outlook-1",
|
||||||
|
Kind: event.Kind("outlook"),
|
||||||
|
Source: "spc-test",
|
||||||
|
EmittedAt: time.Date(2026, 6, 11, 20, 5, 0, 0, time.UTC),
|
||||||
|
EffectiveAt: &effectiveAt,
|
||||||
|
Schema: standards.SchemaRawSPCConvectiveOutlookV1,
|
||||||
|
Payload: json.RawMessage(raw),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func spcBundle(t *testing.T, latitude, longitude float64) spcprovider.RawConvectiveOutlookBundle {
|
||||||
|
t.Helper()
|
||||||
|
fetchedAt := time.Date(2026, 6, 11, 20, 0, 0, 0, time.UTC)
|
||||||
|
products := make([]spcprovider.RawOutlookProduct, 0, len(spcprovider.GeoJSONProducts()))
|
||||||
|
for _, product := range spcprovider.GeoJSONProducts() {
|
||||||
|
products = append(products, spcprovider.RawOutlookProduct{
|
||||||
|
Key: product.Key,
|
||||||
|
Day: product.Day,
|
||||||
|
OutlookType: product.OutlookType,
|
||||||
|
URL: "https://example.invalid/" + product.Key + ".geojson",
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Body: json.RawMessage(geoJSONFixtureForProduct(t, product.Key)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return spcprovider.RawConvectiveOutlookBundle{
|
||||||
|
LocationID: "stl",
|
||||||
|
LocationName: "St. Louis, MO",
|
||||||
|
Latitude: latitude,
|
||||||
|
Longitude: longitude,
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Products: products,
|
||||||
|
Discussions: []spcprovider.RawDiscussionPage{
|
||||||
|
{Key: "day1", Day: 1, URL: "https://example.invalid/day1.html", FetchedAt: fetchedAt, Body: string(readSPCTestFixture(t, "day1_prt.html"))},
|
||||||
|
{Key: "day2", Day: 2, URL: "https://example.invalid/day2.html", FetchedAt: fetchedAt, Body: string(readSPCTestFixture(t, "day2_prt_corr.html"))},
|
||||||
|
{Key: "day3", Day: 3, URL: "https://example.invalid/day3.html", FetchedAt: fetchedAt, Body: string(readSPCTestFixture(t, "day3_prt.html"))},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func geoJSONFixtureForProduct(t *testing.T, key string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(key, "day1_"):
|
||||||
|
return readSPCTestFixture(t, "day1_cat.geojson")
|
||||||
|
case strings.HasPrefix(key, "day2_"):
|
||||||
|
return readSPCTestFixture(t, "day2_torn.geojson")
|
||||||
|
case strings.HasPrefix(key, "day3_"):
|
||||||
|
return readSPCTestFixture(t, "day3_wind.geojson")
|
||||||
|
default:
|
||||||
|
t.Fatalf("unknown product key %q", key)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readSPCTestFixture(t *testing.T, name string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
path := filepath.Join("..", "..", "providers", "spc", "testdata", name)
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read fixture %s: %v", path, err)
|
||||||
|
}
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
|
||||||
|
func findOutlook(outlooks []model.WeatherOutlook, day int, outlookType string) *model.WeatherOutlook {
|
||||||
|
for i := range outlooks {
|
||||||
|
if outlooks[i].Day == day && outlooks[i].OutlookType == outlookType {
|
||||||
|
return &outlooks[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertTime(t *testing.T, name string, got time.Time, year int, month time.Month, day int, hour int, minute int, second int) {
|
||||||
|
t.Helper()
|
||||||
|
want := time.Date(year, month, day, hour, minute, second, 0, time.UTC)
|
||||||
|
if !got.Equal(want) {
|
||||||
|
t.Fatalf("%s = %s, want %s", name, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
14
internal/normalizers/spc/register.go
Normal file
14
internal/normalizers/spc/register.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
fknormalize "gitea.maximumdirect.net/ejr/feedkit/processors/normalize"
|
||||||
|
)
|
||||||
|
|
||||||
|
var builtins = []fknormalize.Normalizer{
|
||||||
|
ConvectiveOutlookNormalizer{},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register appends SPC normalizers in stable order.
|
||||||
|
func Register(in []fknormalize.Normalizer) []fknormalize.Normalizer {
|
||||||
|
return append(in, builtins...)
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// Package openweather contains provider-specific helper code for OpenWeather used by
|
// Package openweather contains provider-specific helper code for OpenWeather
|
||||||
// both sources and normalizers.
|
// used by sources and normalizers.
|
||||||
//
|
//
|
||||||
// Rules:
|
// Rules:
|
||||||
// - No network I/O here.
|
// - No network I/O here.
|
||||||
|
|||||||
188
internal/providers/spc/discussion.go
Normal file
188
internal/providers/spc/discussion.go
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"html"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
scriptBlockRE = regexp.MustCompile(`(?is)<script\b[^>]*>.*?</script>`)
|
||||||
|
preBlockRE = regexp.MustCompile(`(?is)<pre\b[^>]*>(.*?)</pre>`)
|
||||||
|
tagRE = regexp.MustCompile(`(?is)<[^>]+>`)
|
||||||
|
updatedRE = regexp.MustCompile(`(?im)^\s*Updated:\s*(.+?)\s*$`)
|
||||||
|
pageUpdatedRE = regexp.MustCompile(`(?i)\bUpdated:\s*((?:\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)|(?:[A-Z][a-z]{2}\s+[A-Z][a-z]{2}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+UTC\s+\d{4})|(?:\d{4}\s+UTC\s+[A-Z][a-z]{2}\s+[A-Z][a-z]{2}\s+\d{1,2}\s+\d{4})|(?:\d{4}Z\s+[A-Z][a-z]{2}\s+[A-Z][a-z]{2}\s+\d{1,2}\s+\d{4}))`)
|
||||||
|
productCodeRE = regexp.MustCompile(`(?i)^SPC\s+AC\s+\d+\s*$`)
|
||||||
|
sectionRE = regexp.MustCompile(`^\s*\.\.\.[A-Z0-9 /-]+\.{3}\s*$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// DiscussionText contains parsed text from an SPC print page.
|
||||||
|
type DiscussionText struct {
|
||||||
|
ProductTitle string
|
||||||
|
Headline string
|
||||||
|
Summary string
|
||||||
|
Discussion string
|
||||||
|
UpdatedAt *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtractProductText extracts and cleans the first useful preformatted SPC
|
||||||
|
// product text block from a print page.
|
||||||
|
func ExtractProductText(rawHTML string) (string, error) {
|
||||||
|
matches := preBlockRE.FindAllStringSubmatch(rawHTML, -1)
|
||||||
|
for _, match := range matches {
|
||||||
|
if len(match) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
text := cleanHTMLText(match[1])
|
||||||
|
if strings.TrimSpace(text) != "" {
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("no useful pre block found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDiscussionHTML extracts SPC product text and page-level metadata from a
|
||||||
|
// print-page HTML document.
|
||||||
|
func ParseDiscussionHTML(rawHTML string) (DiscussionText, error) {
|
||||||
|
text, err := ExtractProductText(rawHTML)
|
||||||
|
if err != nil {
|
||||||
|
return DiscussionText{}, err
|
||||||
|
}
|
||||||
|
parsed := ParseDiscussionText(text)
|
||||||
|
if updatedAt := ParsePageUpdatedTimestamp(rawHTML); updatedAt != nil {
|
||||||
|
parsed.UpdatedAt = updatedAt
|
||||||
|
}
|
||||||
|
return parsed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDiscussionText extracts common SPC narrative metadata from cleaned
|
||||||
|
// product text.
|
||||||
|
func ParseDiscussionText(text string) DiscussionText {
|
||||||
|
text = trimBlankLines(normalizeNewlines(text))
|
||||||
|
title := ParseProductTitle(text)
|
||||||
|
return DiscussionText{
|
||||||
|
ProductTitle: title,
|
||||||
|
Headline: title,
|
||||||
|
Summary: ExtractSummary(text),
|
||||||
|
Discussion: text,
|
||||||
|
UpdatedAt: ParseUpdatedTimestamp(text),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParsePageUpdatedTimestamp parses the page-level Updated row from an SPC print
|
||||||
|
// page. SPC currently places this outside the product <pre> block.
|
||||||
|
func ParsePageUpdatedTimestamp(rawHTML string) *time.Time {
|
||||||
|
text := cleanHTMLText(rawHTML)
|
||||||
|
text = strings.ReplaceAll(text, "\u00a0", " ")
|
||||||
|
text = strings.Join(strings.Fields(text), " ")
|
||||||
|
match := pageUpdatedRE.FindStringSubmatch(text)
|
||||||
|
if len(match) != 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return parseUpdatedValue(match[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseUpdatedTimestamp parses an SPC print-page Updated line when present.
|
||||||
|
func ParseUpdatedTimestamp(text string) *time.Time {
|
||||||
|
match := updatedRE.FindStringSubmatch(normalizeNewlines(text))
|
||||||
|
if len(match) != 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return parseUpdatedValue(match[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseProductTitle returns the first non-empty product line from cleaned text.
|
||||||
|
func ParseProductTitle(text string) string {
|
||||||
|
for _, line := range strings.Split(normalizeNewlines(text), "\n") {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if line == "" || strings.HasPrefix(line, "Updated:") || productCodeRE.MatchString(line) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return line
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseHeadline returns the human-facing headline from cleaned text.
|
||||||
|
func ParseHeadline(text string) string {
|
||||||
|
return ParseProductTitle(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtractSummary returns text under the ...SUMMARY... section through the next
|
||||||
|
// SPC section heading.
|
||||||
|
func ExtractSummary(text string) string {
|
||||||
|
lines := strings.Split(normalizeNewlines(text), "\n")
|
||||||
|
start := -1
|
||||||
|
for i, line := range lines {
|
||||||
|
if strings.EqualFold(strings.TrimSpace(line), "...SUMMARY...") {
|
||||||
|
start = i + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if start < 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var out []string
|
||||||
|
for _, line := range lines[start:] {
|
||||||
|
if sectionRE.MatchString(line) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
out = append(out, line)
|
||||||
|
}
|
||||||
|
return trimBlankLines(strings.Join(out, "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanHTMLText(raw string) string {
|
||||||
|
raw = scriptBlockRE.ReplaceAllString(raw, "")
|
||||||
|
raw = tagRE.ReplaceAllString(raw, "")
|
||||||
|
raw = html.UnescapeString(raw)
|
||||||
|
raw = normalizeNewlines(raw)
|
||||||
|
return trimBlankLines(raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeNewlines(text string) string {
|
||||||
|
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||||||
|
text = strings.ReplaceAll(text, "\r", "\n")
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimBlankLines(text string) string {
|
||||||
|
lines := strings.Split(normalizeNewlines(text), "\n")
|
||||||
|
start := 0
|
||||||
|
for start < len(lines) && strings.TrimSpace(lines[start]) == "" {
|
||||||
|
start++
|
||||||
|
}
|
||||||
|
end := len(lines)
|
||||||
|
for end > start && strings.TrimSpace(lines[end-1]) == "" {
|
||||||
|
end--
|
||||||
|
}
|
||||||
|
return strings.Join(lines[start:end], "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseUpdatedValue(value string) *time.Time {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
if value == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if t := parseOptionalISOTimestamp(value); t != nil {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
for _, layout := range []string{
|
||||||
|
"Mon Jan 2 15:04:05 UTC 2006",
|
||||||
|
"1504 UTC Mon Jan 2 2006",
|
||||||
|
"1504Z Mon Jan 2 2006",
|
||||||
|
"3:04 PM UTC Mon Jan 2 2006",
|
||||||
|
time.RFC1123,
|
||||||
|
time.RFC1123Z,
|
||||||
|
} {
|
||||||
|
t, err := time.Parse(layout, value)
|
||||||
|
if err == nil {
|
||||||
|
tt := t.UTC()
|
||||||
|
return &tt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
109
internal/providers/spc/discussion_test.go
Normal file
109
internal/providers/spc/discussion_test.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExtractProductTextCleansPreBlock(t *testing.T) {
|
||||||
|
raw := string(readTestFile(t, "day1_prt.html"))
|
||||||
|
|
||||||
|
got, err := ExtractProductText(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ExtractProductText() error = %v", err)
|
||||||
|
}
|
||||||
|
if strings.Contains(got, "<script") || strings.Contains(got, "<pre") {
|
||||||
|
t.Fatalf("ExtractProductText() retained HTML: %q", got)
|
||||||
|
}
|
||||||
|
if strings.Contains(got, "ignore me") {
|
||||||
|
t.Fatalf("ExtractProductText() retained script content: %q", got)
|
||||||
|
}
|
||||||
|
if !strings.Contains(got, "Day 1 Convective Outlook") {
|
||||||
|
t.Fatalf("ExtractProductText() missing headline: %q", got)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(got, "SPC AC 111234") {
|
||||||
|
t.Fatalf("ExtractProductText() = %q, want product code prefix", got)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(got, "\n") || strings.HasSuffix(got, "\n") {
|
||||||
|
t.Fatalf("ExtractProductText() retained surrounding blank lines: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseDiscussionHTMLExtractsHeadlineSummaryAndUpdated(t *testing.T) {
|
||||||
|
got, err := ParseDiscussionHTML(string(readTestFile(t, "day1_prt.html")))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseDiscussionHTML() error = %v", err)
|
||||||
|
}
|
||||||
|
if got.ProductTitle != "Day 1 Convective Outlook" {
|
||||||
|
t.Fatalf("ProductTitle = %q", got.ProductTitle)
|
||||||
|
}
|
||||||
|
if got.Headline != "Day 1 Convective Outlook" {
|
||||||
|
t.Fatalf("Headline = %q", got.Headline)
|
||||||
|
}
|
||||||
|
wantSummary := "Severe thunderstorms are possible across parts of the central Plains\nand mid Mississippi Valley this afternoon and evening."
|
||||||
|
if got.Summary != wantSummary {
|
||||||
|
t.Fatalf("Summary = %q, want %q", got.Summary, wantSummary)
|
||||||
|
}
|
||||||
|
if !strings.Contains(got.Discussion, "...DISCUSSION...") {
|
||||||
|
t.Fatalf("Discussion missing full text: %q", got.Discussion)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(got.Discussion, "SPC AC 111234") {
|
||||||
|
t.Fatalf("Discussion = %q, want product code prefix", got.Discussion)
|
||||||
|
}
|
||||||
|
wantUpdated := time.Date(2026, 6, 11, 12, 45, 0, 0, time.UTC)
|
||||||
|
if got.UpdatedAt == nil || !got.UpdatedAt.Equal(wantUpdated) {
|
||||||
|
t.Fatalf("UpdatedAt = %v, want %s", got.UpdatedAt, wantUpdated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseProductTitleSkipsSPCProductCode(t *testing.T) {
|
||||||
|
got := ParseProductTitle("SPC AC 101959\nDay 1 Convective Outlook\nNWS Storm Prediction Center Norman OK")
|
||||||
|
if got != "Day 1 Convective Outlook" {
|
||||||
|
t.Fatalf("ParseProductTitle() = %q, want Day 1 Convective Outlook", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseDiscussionTextPreservesCorrectionMarker(t *testing.T) {
|
||||||
|
got, err := ParseDiscussionHTML(string(readTestFile(t, "day2_prt_corr.html")))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseDiscussionHTML() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(got.Headline, "CORR 1") {
|
||||||
|
t.Fatalf("Headline = %q, want correction marker", got.Headline)
|
||||||
|
}
|
||||||
|
if !strings.Contains(got.Discussion, "CORR 1") {
|
||||||
|
t.Fatalf("Discussion = %q, want correction marker", got.Discussion)
|
||||||
|
}
|
||||||
|
wantUpdated := time.Date(2026, 6, 11, 17, 30, 0, 0, time.UTC)
|
||||||
|
if got.UpdatedAt == nil || !got.UpdatedAt.Equal(wantUpdated) {
|
||||||
|
t.Fatalf("UpdatedAt = %v, want %s", got.UpdatedAt, wantUpdated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseUpdatedTimestampReturnsNilWhenAbsent(t *testing.T) {
|
||||||
|
text, err := ExtractProductText(string(readTestFile(t, "day2_prt_corr.html")))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ExtractProductText() error = %v", err)
|
||||||
|
}
|
||||||
|
if got := ParseUpdatedTimestamp(text); got != nil {
|
||||||
|
t.Fatalf("ParseUpdatedTimestamp() = %v, want nil", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseUpdatedTimestampAcceptsSPCUTCFormat(t *testing.T) {
|
||||||
|
got := ParseUpdatedTimestamp("Updated: 1945 UTC Thu Jun 11 2026")
|
||||||
|
want := time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC)
|
||||||
|
if got == nil || !got.Equal(want) {
|
||||||
|
t.Fatalf("ParseUpdatedTimestamp() = %v, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParsePageUpdatedTimestampAcceptsLiveSPCShape(t *testing.T) {
|
||||||
|
got := ParsePageUpdatedTimestamp(string(readTestFile(t, "day3_prt.html")))
|
||||||
|
want := time.Date(2026, 6, 11, 20, 0, 0, 0, time.UTC)
|
||||||
|
if got == nil || !got.Equal(want) {
|
||||||
|
t.Fatalf("ParsePageUpdatedTimestamp() = %v, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
8
internal/providers/spc/doc.go
Normal file
8
internal/providers/spc/doc.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// Package spc contains deterministic helper code for Storm Prediction Center
|
||||||
|
// products used by sources and normalizers.
|
||||||
|
//
|
||||||
|
// Rules:
|
||||||
|
// - No network I/O here.
|
||||||
|
// - Keep helpers deterministic and easy to unit test.
|
||||||
|
// - Preserve upstream payload fragments needed for canonical mapping.
|
||||||
|
package spc
|
||||||
105
internal/providers/spc/geojson.go
Normal file
105
internal/providers/spc/geojson.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GeoJSONFeatureCollection is the minimal SPC outlook FeatureCollection shape
|
||||||
|
// needed by weatherfeeder.
|
||||||
|
type GeoJSONFeatureCollection struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Features []GeoJSONFeature `json:"features"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeoJSONFeature preserves typed SPC properties and compact raw geometry.
|
||||||
|
type GeoJSONFeature struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Properties GeoJSONProperties `json:"properties"`
|
||||||
|
Geometry json.RawMessage `json:"geometry"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeoJSONProperties contains the SPC fields used by canonical mapping.
|
||||||
|
type GeoJSONProperties struct {
|
||||||
|
ValidISO string `json:"VALID_ISO"`
|
||||||
|
ExpireISO string `json:"EXPIRE_ISO"`
|
||||||
|
IssueISO string `json:"ISSUE_ISO"`
|
||||||
|
Forecaster string `json:"FORECASTER"`
|
||||||
|
Label string `json:"LABEL"`
|
||||||
|
Label2 string `json:"LABEL2"`
|
||||||
|
DN *int `json:"DN"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeGeoJSON decodes an SPC GeoJSON outlook product and compacts feature
|
||||||
|
// geometry JSON for stable downstream storage.
|
||||||
|
func DecodeGeoJSON(raw []byte) (GeoJSONFeatureCollection, error) {
|
||||||
|
var collection GeoJSONFeatureCollection
|
||||||
|
if err := json.Unmarshal(raw, &collection); err != nil {
|
||||||
|
return GeoJSONFeatureCollection{}, fmt.Errorf("decode geojson: %w", err)
|
||||||
|
}
|
||||||
|
for i := range collection.Features {
|
||||||
|
geom, err := compactJSON(collection.Features[i].Geometry)
|
||||||
|
if err != nil {
|
||||||
|
return GeoJSONFeatureCollection{}, fmt.Errorf("features[%d].geometry: %w", i, err)
|
||||||
|
}
|
||||||
|
collection.Features[i].Geometry = geom
|
||||||
|
}
|
||||||
|
return collection, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *GeoJSONProperties) UnmarshalJSON(raw []byte) error {
|
||||||
|
type alias GeoJSONProperties
|
||||||
|
var aux struct {
|
||||||
|
alias
|
||||||
|
DN any `json:"DN"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &aux); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*p = GeoJSONProperties(aux.alias)
|
||||||
|
dn, err := parseSeverityRank(aux.DN)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.DN = dn
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseSeverityRank(value any) (*int, error) {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case nil:
|
||||||
|
return nil, nil
|
||||||
|
case float64:
|
||||||
|
rank := int(v)
|
||||||
|
if float64(rank) != v {
|
||||||
|
return nil, fmt.Errorf("DN must be an integer, got %v", v)
|
||||||
|
}
|
||||||
|
return &rank, nil
|
||||||
|
case string:
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
if v == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
rank, err := strconv.Atoi(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("DN must be an integer, got %q", v)
|
||||||
|
}
|
||||||
|
return &rank, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("DN must be an integer or string, got %T", value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func compactJSON(raw json.RawMessage) (json.RawMessage, error) {
|
||||||
|
if len(raw) == 0 {
|
||||||
|
return nil, fmt.Errorf("missing")
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := json.Compact(&buf, raw); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return json.RawMessage(buf.Bytes()), nil
|
||||||
|
}
|
||||||
90
internal/providers/spc/geojson_test.go
Normal file
90
internal/providers/spc/geojson_test.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDecodeGeoJSONExposesSPCPropertiesAndCompactGeometry(t *testing.T) {
|
||||||
|
raw := readTestFile(t, "day1_cat.geojson")
|
||||||
|
|
||||||
|
got, err := DecodeGeoJSON(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("DecodeGeoJSON() error = %v", err)
|
||||||
|
}
|
||||||
|
if got.Type != "FeatureCollection" {
|
||||||
|
t.Fatalf("Type = %q, want FeatureCollection", got.Type)
|
||||||
|
}
|
||||||
|
if len(got.Features) != 1 {
|
||||||
|
t.Fatalf("Features length = %d, want 1", len(got.Features))
|
||||||
|
}
|
||||||
|
|
||||||
|
feature := got.Features[0]
|
||||||
|
props := feature.Properties
|
||||||
|
if props.ValidISO != "2026-06-11T13:00:00Z" {
|
||||||
|
t.Fatalf("VALID_ISO = %q", props.ValidISO)
|
||||||
|
}
|
||||||
|
if props.ExpireISO != "2026-06-12T12:00:00Z" {
|
||||||
|
t.Fatalf("EXPIRE_ISO = %q", props.ExpireISO)
|
||||||
|
}
|
||||||
|
if props.IssueISO != "2026-06-11T12:34:56Z" {
|
||||||
|
t.Fatalf("ISSUE_ISO = %q", props.IssueISO)
|
||||||
|
}
|
||||||
|
if props.Forecaster != "SMITH" {
|
||||||
|
t.Fatalf("FORECASTER = %q", props.Forecaster)
|
||||||
|
}
|
||||||
|
if props.Label != "SLGT" {
|
||||||
|
t.Fatalf("LABEL = %q", props.Label)
|
||||||
|
}
|
||||||
|
if props.Label2 != "Slight Risk" {
|
||||||
|
t.Fatalf("LABEL2 = %q", props.Label2)
|
||||||
|
}
|
||||||
|
if props.DN == nil || *props.DN != 3 {
|
||||||
|
t.Fatalf("DN = %v, want 3", props.DN)
|
||||||
|
}
|
||||||
|
|
||||||
|
wantGeometry := `{"type":"Polygon","coordinates":[[[-91.0,38.0],[-90.0,38.0],[-90.0,39.0],[-91.0,39.0],[-91.0,38.0]]]}`
|
||||||
|
if string(feature.Geometry) != wantGeometry {
|
||||||
|
t.Fatalf("Geometry = %s, want %s", feature.Geometry, wantGeometry)
|
||||||
|
}
|
||||||
|
if strings.Contains(string(feature.Geometry), "\n") || strings.Contains(string(feature.Geometry), " ") {
|
||||||
|
t.Fatalf("Geometry is not compact: %q", feature.Geometry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeGeoJSONParsesSeverityRankString(t *testing.T) {
|
||||||
|
raw := readTestFile(t, "day2_torn.geojson")
|
||||||
|
|
||||||
|
got, err := DecodeGeoJSON(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("DecodeGeoJSON() error = %v", err)
|
||||||
|
}
|
||||||
|
props := got.Features[0].Properties
|
||||||
|
if props.DN == nil || *props.DN != 5 {
|
||||||
|
t.Fatalf("DN = %v, want 5", props.DN)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseISOTimestampTrimsAndReturnsUTC(t *testing.T) {
|
||||||
|
got, err := ParseISOTimestamp(" 2026-06-11T12:34:56Z ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseISOTimestamp() error = %v", err)
|
||||||
|
}
|
||||||
|
want := time.Date(2026, 6, 11, 12, 34, 56, 0, time.UTC)
|
||||||
|
if !got.Equal(want) {
|
||||||
|
t.Fatalf("ParseISOTimestamp() = %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readTestFile(t *testing.T, name string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
path := filepath.Join("testdata", name)
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read %s: %v", path, err)
|
||||||
|
}
|
||||||
|
return raw
|
||||||
|
}
|
||||||
89
internal/providers/spc/product.go
Normal file
89
internal/providers/spc/product.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
const (
|
||||||
|
OutlookTypeCategorical = "categorical"
|
||||||
|
OutlookTypeTornado = "tornado"
|
||||||
|
OutlookTypeHail = "hail"
|
||||||
|
OutlookTypeWind = "wind"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GeoJSONProduct describes one required SPC convective outlook GeoJSON product.
|
||||||
|
type GeoJSONProduct struct {
|
||||||
|
Key string
|
||||||
|
Day int
|
||||||
|
OutlookType string
|
||||||
|
URL string
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiscussionProduct describes one required SPC convective outlook print page.
|
||||||
|
type DiscussionProduct struct {
|
||||||
|
Key string
|
||||||
|
Day int
|
||||||
|
URL string
|
||||||
|
}
|
||||||
|
|
||||||
|
var geoJSONProducts = []GeoJSONProduct{
|
||||||
|
{Key: "day1_categorical", Day: 1, OutlookType: OutlookTypeCategorical, URL: "https://www.spc.noaa.gov/products/outlook/day1otlk_cat.nolyr.geojson"},
|
||||||
|
{Key: "day1_tornado", Day: 1, OutlookType: OutlookTypeTornado, URL: "https://www.spc.noaa.gov/products/outlook/day1otlk_torn.nolyr.geojson"},
|
||||||
|
{Key: "day1_hail", Day: 1, OutlookType: OutlookTypeHail, URL: "https://www.spc.noaa.gov/products/outlook/day1otlk_hail.nolyr.geojson"},
|
||||||
|
{Key: "day1_wind", Day: 1, OutlookType: OutlookTypeWind, URL: "https://www.spc.noaa.gov/products/outlook/day1otlk_wind.nolyr.geojson"},
|
||||||
|
{Key: "day2_categorical", Day: 2, OutlookType: OutlookTypeCategorical, URL: "https://www.spc.noaa.gov/products/outlook/day2otlk_cat.nolyr.geojson"},
|
||||||
|
{Key: "day2_tornado", Day: 2, OutlookType: OutlookTypeTornado, URL: "https://www.spc.noaa.gov/products/outlook/day2otlk_torn.nolyr.geojson"},
|
||||||
|
{Key: "day2_hail", Day: 2, OutlookType: OutlookTypeHail, URL: "https://www.spc.noaa.gov/products/outlook/day2otlk_hail.nolyr.geojson"},
|
||||||
|
{Key: "day2_wind", Day: 2, OutlookType: OutlookTypeWind, URL: "https://www.spc.noaa.gov/products/outlook/day2otlk_wind.nolyr.geojson"},
|
||||||
|
{Key: "day3_categorical", Day: 3, OutlookType: OutlookTypeCategorical, URL: "https://www.spc.noaa.gov/products/outlook/day3otlk_cat.nolyr.geojson"},
|
||||||
|
{Key: "day3_tornado", Day: 3, OutlookType: OutlookTypeTornado, URL: "https://www.spc.noaa.gov/products/outlook/day3otlk_torn.nolyr.geojson"},
|
||||||
|
{Key: "day3_hail", Day: 3, OutlookType: OutlookTypeHail, URL: "https://www.spc.noaa.gov/products/outlook/day3otlk_hail.nolyr.geojson"},
|
||||||
|
{Key: "day3_wind", Day: 3, OutlookType: OutlookTypeWind, URL: "https://www.spc.noaa.gov/products/outlook/day3otlk_wind.nolyr.geojson"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var discussionProducts = []DiscussionProduct{
|
||||||
|
{Key: "day1", Day: 1, URL: "https://www.spc.noaa.gov/products/outlook/day1otlk_prt.html"},
|
||||||
|
{Key: "day2", Day: 2, URL: "https://www.spc.noaa.gov/products/outlook/day2otlk_prt.html"},
|
||||||
|
{Key: "day3", Day: 3, URL: "https://www.spc.noaa.gov/products/outlook/day3otlk_prt.html"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeoJSONProducts returns the required SPC convective outlook GeoJSON products
|
||||||
|
// in stable day/type order.
|
||||||
|
func GeoJSONProducts() []GeoJSONProduct {
|
||||||
|
out := make([]GeoJSONProduct, len(geoJSONProducts))
|
||||||
|
copy(out, geoJSONProducts)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiscussionProducts returns the required SPC convective outlook print pages in
|
||||||
|
// stable day order.
|
||||||
|
func DiscussionProducts() []DiscussionProduct {
|
||||||
|
out := make([]DiscussionProduct, len(discussionProducts))
|
||||||
|
copy(out, discussionProducts)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeoJSONProductByKey returns product metadata for a configured product key.
|
||||||
|
func GeoJSONProductByKey(key string) (GeoJSONProduct, bool) {
|
||||||
|
for _, product := range geoJSONProducts {
|
||||||
|
if product.Key == key {
|
||||||
|
return product, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return GeoJSONProduct{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiscussionProductByKey returns discussion metadata for a configured day key.
|
||||||
|
func DiscussionProductByKey(key string) (DiscussionProduct, bool) {
|
||||||
|
for _, product := range discussionProducts {
|
||||||
|
if product.Key == key {
|
||||||
|
return product, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return DiscussionProduct{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateProductDay(day int) error {
|
||||||
|
if day < 1 || day > 3 {
|
||||||
|
return fmt.Errorf("day must be 1, 2, or 3, got %d", day)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
56
internal/providers/spc/product_test.go
Normal file
56
internal/providers/spc/product_test.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGeoJSONProductsStableOrder(t *testing.T) {
|
||||||
|
got := GeoJSONProducts()
|
||||||
|
if len(got) != 12 {
|
||||||
|
t.Fatalf("GeoJSONProducts() length = %d, want 12", len(got))
|
||||||
|
}
|
||||||
|
|
||||||
|
wantKeys := []string{
|
||||||
|
"day1_categorical",
|
||||||
|
"day1_tornado",
|
||||||
|
"day1_hail",
|
||||||
|
"day1_wind",
|
||||||
|
"day2_categorical",
|
||||||
|
"day2_tornado",
|
||||||
|
"day2_hail",
|
||||||
|
"day2_wind",
|
||||||
|
"day3_categorical",
|
||||||
|
"day3_tornado",
|
||||||
|
"day3_hail",
|
||||||
|
"day3_wind",
|
||||||
|
}
|
||||||
|
for i, want := range wantKeys {
|
||||||
|
if got[i].Key != want {
|
||||||
|
t.Fatalf("GeoJSONProducts()[%d].Key = %q, want %q", i, got[i].Key, want)
|
||||||
|
}
|
||||||
|
if err := validateProductDay(got[i].Day); err != nil {
|
||||||
|
t.Fatalf("GeoJSONProducts()[%d].Day invalid: %v", i, err)
|
||||||
|
}
|
||||||
|
if got[i].URL == "" {
|
||||||
|
t.Fatalf("GeoJSONProducts()[%d].URL is empty", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDiscussionProductsStableOrder(t *testing.T) {
|
||||||
|
got := DiscussionProducts()
|
||||||
|
if len(got) != 3 {
|
||||||
|
t.Fatalf("DiscussionProducts() length = %d, want 3", len(got))
|
||||||
|
}
|
||||||
|
|
||||||
|
wantKeys := []string{"day1", "day2", "day3"}
|
||||||
|
for i, want := range wantKeys {
|
||||||
|
if got[i].Key != want {
|
||||||
|
t.Fatalf("DiscussionProducts()[%d].Key = %q, want %q", i, got[i].Key, want)
|
||||||
|
}
|
||||||
|
if got[i].Day != i+1 {
|
||||||
|
t.Fatalf("DiscussionProducts()[%d].Day = %d, want %d", i, got[i].Day, i+1)
|
||||||
|
}
|
||||||
|
if got[i].URL == "" {
|
||||||
|
t.Fatalf("DiscussionProducts()[%d].URL is empty", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
internal/providers/spc/raw.go
Normal file
45
internal/providers/spc/raw.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RawConvectiveOutlookBundle is the provider payload shape for SPC convective
|
||||||
|
// outlook fetch bundles.
|
||||||
|
type RawConvectiveOutlookBundle struct {
|
||||||
|
LocationID string `json:"locationId,omitempty"`
|
||||||
|
LocationName string `json:"locationName,omitempty"`
|
||||||
|
Latitude float64 `json:"latitude"`
|
||||||
|
Longitude float64 `json:"longitude"`
|
||||||
|
FetchedAt time.Time `json:"fetchedAt"`
|
||||||
|
Products []RawOutlookProduct `json:"products"`
|
||||||
|
Discussions []RawDiscussionPage `json:"discussions"`
|
||||||
|
RSS *RawRSSFeed `json:"rss,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RawOutlookProduct contains one fetched SPC GeoJSON product.
|
||||||
|
type RawOutlookProduct struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Day int `json:"day"`
|
||||||
|
OutlookType string `json:"outlookType"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
FetchedAt time.Time `json:"fetchedAt"`
|
||||||
|
Body json.RawMessage `json:"body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RawDiscussionPage contains one fetched SPC print page.
|
||||||
|
type RawDiscussionPage struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Day int `json:"day"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
FetchedAt time.Time `json:"fetchedAt"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RawRSSFeed contains optional fetched SPC RSS metadata.
|
||||||
|
type RawRSSFeed struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
FetchedAt time.Time `json:"fetchedAt"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
}
|
||||||
51
internal/providers/spc/raw_test.go
Normal file
51
internal/providers/spc/raw_test.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRawConvectiveOutlookBundleJSONShape(t *testing.T) {
|
||||||
|
fetchedAt := time.Date(2026, 6, 11, 20, 0, 0, 0, time.UTC)
|
||||||
|
bundle := RawConvectiveOutlookBundle{
|
||||||
|
LocationID: "stl",
|
||||||
|
LocationName: "St. Louis, MO",
|
||||||
|
Latitude: 38.6239,
|
||||||
|
Longitude: -90.3571,
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Products: []RawOutlookProduct{{
|
||||||
|
Key: "day1_categorical",
|
||||||
|
Day: 1,
|
||||||
|
OutlookType: OutlookTypeCategorical,
|
||||||
|
URL: "https://example.invalid/day1.geojson",
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Body: json.RawMessage(`{"type":"FeatureCollection","features":[]}`),
|
||||||
|
}},
|
||||||
|
Discussions: []RawDiscussionPage{{
|
||||||
|
Key: "day1",
|
||||||
|
Day: 1,
|
||||||
|
URL: "https://example.invalid/day1.html",
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Body: "Day 1 Convective Outlook",
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
raw, err := json.Marshal(bundle)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Marshal() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var got map[string]any
|
||||||
|
if err := json.Unmarshal(raw, &got); err != nil {
|
||||||
|
t.Fatalf("Unmarshal() error = %v", err)
|
||||||
|
}
|
||||||
|
for _, key := range []string{"locationId", "locationName", "latitude", "longitude", "fetchedAt", "products", "discussions"} {
|
||||||
|
if _, ok := got[key]; !ok {
|
||||||
|
t.Fatalf("marshaled bundle missing key %q in %s", key, raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, ok := got["rss"]; ok {
|
||||||
|
t.Fatalf("marshaled bundle included empty rss: %s", raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
81
internal/providers/spc/rss.go
Normal file
81
internal/providers/spc/rss.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RSSFeed is a minimal view of the optional SPC RSS feed.
|
||||||
|
type RSSFeed struct {
|
||||||
|
Title string
|
||||||
|
Link string
|
||||||
|
Description string
|
||||||
|
LastBuildDate *time.Time
|
||||||
|
Items []RSSItem
|
||||||
|
}
|
||||||
|
|
||||||
|
// RSSItem is a minimal view of one optional SPC RSS item.
|
||||||
|
type RSSItem struct {
|
||||||
|
Title string
|
||||||
|
Link string
|
||||||
|
Description string
|
||||||
|
PubDate string
|
||||||
|
GUID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseRSSFeed decodes supplemental SPC RSS metadata.
|
||||||
|
func ParseRSSFeed(raw string) (RSSFeed, error) {
|
||||||
|
var doc struct {
|
||||||
|
Channel struct {
|
||||||
|
Title string `xml:"title"`
|
||||||
|
Link string `xml:"link"`
|
||||||
|
Description string `xml:"description"`
|
||||||
|
LastBuildDate string `xml:"lastBuildDate"`
|
||||||
|
Items []struct {
|
||||||
|
Title string `xml:"title"`
|
||||||
|
Link string `xml:"link"`
|
||||||
|
Description string `xml:"description"`
|
||||||
|
PubDate string `xml:"pubDate"`
|
||||||
|
GUID string `xml:"guid"`
|
||||||
|
} `xml:"item"`
|
||||||
|
} `xml:"channel"`
|
||||||
|
}
|
||||||
|
if err := xml.Unmarshal([]byte(raw), &doc); err != nil {
|
||||||
|
return RSSFeed{}, fmt.Errorf("decode rss: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
feed := RSSFeed{
|
||||||
|
Title: strings.TrimSpace(doc.Channel.Title),
|
||||||
|
Link: strings.TrimSpace(doc.Channel.Link),
|
||||||
|
Description: strings.TrimSpace(doc.Channel.Description),
|
||||||
|
LastBuildDate: parseRSSDate(doc.Channel.LastBuildDate),
|
||||||
|
Items: make([]RSSItem, 0, len(doc.Channel.Items)),
|
||||||
|
}
|
||||||
|
for _, item := range doc.Channel.Items {
|
||||||
|
feed.Items = append(feed.Items, RSSItem{
|
||||||
|
Title: strings.TrimSpace(item.Title),
|
||||||
|
Link: strings.TrimSpace(item.Link),
|
||||||
|
Description: strings.TrimSpace(item.Description),
|
||||||
|
PubDate: strings.TrimSpace(item.PubDate),
|
||||||
|
GUID: strings.TrimSpace(item.GUID),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return feed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRSSDate(value string) *time.Time {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
if value == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, layout := range []string{time.RFC1123Z, time.RFC1123} {
|
||||||
|
t, err := time.Parse(layout, value)
|
||||||
|
if err == nil {
|
||||||
|
tt := t.UTC()
|
||||||
|
return &tt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
43
internal/providers/spc/rss_test.go
Normal file
43
internal/providers/spc/rss_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseRSSFeed(t *testing.T) {
|
||||||
|
const raw = `<?xml version="1.0"?>
|
||||||
|
<rss version="2.0">
|
||||||
|
<channel>
|
||||||
|
<title>SPC AC RSS</title>
|
||||||
|
<link>https://www.spc.noaa.gov/products/</link>
|
||||||
|
<description>SPC products</description>
|
||||||
|
<lastBuildDate>Thu, 11 Jun 2026 19:00:00 +0000</lastBuildDate>
|
||||||
|
<item>
|
||||||
|
<title>Day 1 Convective Outlook</title>
|
||||||
|
<link>https://www.spc.noaa.gov/products/outlook/day1otlk.html</link>
|
||||||
|
<description>Outlook text</description>
|
||||||
|
<pubDate>Thu, 11 Jun 2026 18:55:00 +0000</pubDate>
|
||||||
|
<guid>day1</guid>
|
||||||
|
</item>
|
||||||
|
</channel>
|
||||||
|
</rss>`
|
||||||
|
|
||||||
|
got, err := ParseRSSFeed(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseRSSFeed() error = %v", err)
|
||||||
|
}
|
||||||
|
if got.Title != "SPC AC RSS" {
|
||||||
|
t.Fatalf("Title = %q", got.Title)
|
||||||
|
}
|
||||||
|
wantBuild := time.Date(2026, 6, 11, 19, 0, 0, 0, time.UTC)
|
||||||
|
if got.LastBuildDate == nil || !got.LastBuildDate.Equal(wantBuild) {
|
||||||
|
t.Fatalf("LastBuildDate = %v, want %s", got.LastBuildDate, wantBuild)
|
||||||
|
}
|
||||||
|
if len(got.Items) != 1 {
|
||||||
|
t.Fatalf("Items length = %d, want 1", len(got.Items))
|
||||||
|
}
|
||||||
|
if got.Items[0].GUID != "day1" {
|
||||||
|
t.Fatalf("Item GUID = %q", got.Items[0].GUID)
|
||||||
|
}
|
||||||
|
}
|
||||||
29
internal/providers/spc/testdata/day1_cat.geojson
vendored
Normal file
29
internal/providers/spc/testdata/day1_cat.geojson
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"type": "FeatureCollection",
|
||||||
|
"features": [
|
||||||
|
{
|
||||||
|
"type": "Feature",
|
||||||
|
"properties": {
|
||||||
|
"VALID_ISO": "2026-06-11T13:00:00Z",
|
||||||
|
"EXPIRE_ISO": "2026-06-12T12:00:00Z",
|
||||||
|
"ISSUE_ISO": "2026-06-11T12:34:56Z",
|
||||||
|
"FORECASTER": "SMITH",
|
||||||
|
"LABEL": "SLGT",
|
||||||
|
"LABEL2": "Slight Risk",
|
||||||
|
"DN": 3
|
||||||
|
},
|
||||||
|
"geometry": {
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [
|
||||||
|
[
|
||||||
|
[-91.0, 38.0],
|
||||||
|
[-90.0, 38.0],
|
||||||
|
[-90.0, 39.0],
|
||||||
|
[-91.0, 39.0],
|
||||||
|
[-91.0, 38.0]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
22
internal/providers/spc/testdata/day1_prt.html
vendored
Normal file
22
internal/providers/spc/testdata/day1_prt.html
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head><title>Day 1 Convective Outlook</title></head>
|
||||||
|
<body>
|
||||||
|
<table>
|
||||||
|
<tr><td align="center" class="rpttext" nowrap>Updated: Thu Jun 11 12:45:00 UTC 2026 (<a href="archive/day1-geojson.zip">geojson</a>)</td></tr>
|
||||||
|
</table>
|
||||||
|
<pre>
|
||||||
|
<script>window.bad = "<b>ignore me</b>";</script>
|
||||||
|
SPC AC 111234
|
||||||
|
Day 1 Convective Outlook
|
||||||
|
NWS Storm Prediction Center Norman OK
|
||||||
|
|
||||||
|
...SUMMARY...
|
||||||
|
Severe thunderstorms are possible across parts of the central Plains
|
||||||
|
and mid Mississippi Valley this afternoon and evening.
|
||||||
|
|
||||||
|
...DISCUSSION...
|
||||||
|
The primary threats will be damaging wind and large hail.
|
||||||
|
</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
19
internal/providers/spc/testdata/day2_prt_corr.html
vendored
Normal file
19
internal/providers/spc/testdata/day2_prt_corr.html
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<table>
|
||||||
|
<tr><td class="rpttext">Updated: Thu Jun 11 17:30:00 UTC 2026 </td></tr>
|
||||||
|
</table>
|
||||||
|
<pre>
|
||||||
|
SPC AC 111730
|
||||||
|
Day 2 Convective Outlook CORR 1
|
||||||
|
NWS Storm Prediction Center Norman OK
|
||||||
|
|
||||||
|
...SUMMARY...
|
||||||
|
Scattered severe thunderstorms remain possible across the southern Plains.
|
||||||
|
|
||||||
|
...DISCUSSION...
|
||||||
|
Corrected outlook text remains otherwise unchanged.
|
||||||
|
</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
29
internal/providers/spc/testdata/day2_torn.geojson
vendored
Normal file
29
internal/providers/spc/testdata/day2_torn.geojson
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"type": "FeatureCollection",
|
||||||
|
"features": [
|
||||||
|
{
|
||||||
|
"type": "Feature",
|
||||||
|
"properties": {
|
||||||
|
"VALID_ISO": "2026-06-12T12:00:00Z",
|
||||||
|
"EXPIRE_ISO": "2026-06-13T12:00:00Z",
|
||||||
|
"ISSUE_ISO": "2026-06-11T17:30:00Z",
|
||||||
|
"FORECASTER": "DOE",
|
||||||
|
"LABEL": "5",
|
||||||
|
"LABEL2": "5% Tornado",
|
||||||
|
"DN": "5"
|
||||||
|
},
|
||||||
|
"geometry": {
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [
|
||||||
|
[
|
||||||
|
[-100.0, 35.0],
|
||||||
|
[-98.0, 35.0],
|
||||||
|
[-98.0, 37.0],
|
||||||
|
[-100.0, 37.0],
|
||||||
|
[-100.0, 35.0]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
19
internal/providers/spc/testdata/day3_prt.html
vendored
Normal file
19
internal/providers/spc/testdata/day3_prt.html
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<table>
|
||||||
|
<tr><td class="rpttext">Updated: Thu Jun 11 20:00:00 UTC 2026 </td></tr>
|
||||||
|
</table>
|
||||||
|
<pre>
|
||||||
|
SPC AC 112000
|
||||||
|
Day 3 Convective Outlook
|
||||||
|
NWS Storm Prediction Center Norman OK
|
||||||
|
|
||||||
|
...SUMMARY...
|
||||||
|
A corridor of strong to severe storms may develop near a frontal zone.
|
||||||
|
|
||||||
|
...DISCUSSION...
|
||||||
|
Confidence remains moderate for organized storms.
|
||||||
|
</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
31
internal/providers/spc/testdata/day3_wind.geojson
vendored
Normal file
31
internal/providers/spc/testdata/day3_wind.geojson
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"type": "FeatureCollection",
|
||||||
|
"features": [
|
||||||
|
{
|
||||||
|
"type": "Feature",
|
||||||
|
"properties": {
|
||||||
|
"VALID_ISO": "2026-06-13T12:00:00Z",
|
||||||
|
"EXPIRE_ISO": "2026-06-14T12:00:00Z",
|
||||||
|
"ISSUE_ISO": "2026-06-11T19:45:00Z",
|
||||||
|
"FORECASTER": "LEE",
|
||||||
|
"LABEL": "15",
|
||||||
|
"LABEL2": "15% Wind",
|
||||||
|
"DN": 15
|
||||||
|
},
|
||||||
|
"geometry": {
|
||||||
|
"type": "MultiPolygon",
|
||||||
|
"coordinates": [
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[-103.0, 34.0],
|
||||||
|
[-101.0, 34.0],
|
||||||
|
[-101.0, 36.0],
|
||||||
|
[-103.0, 36.0],
|
||||||
|
[-103.0, 34.0]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
24
internal/providers/spc/time.go
Normal file
24
internal/providers/spc/time.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ParseISOTimestamp parses SPC ISO timestamps from GeoJSON properties.
|
||||||
|
func ParseISOTimestamp(value string) (time.Time, error) {
|
||||||
|
return time.Parse(time.RFC3339, strings.TrimSpace(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptionalISOTimestamp(value string) *time.Time {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
if value == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
t, err := ParseISOTimestamp(value)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tt := t.UTC()
|
||||||
|
return &tt
|
||||||
|
}
|
||||||
@@ -8,29 +8,42 @@
|
|||||||
// Canonical input schemas:
|
// Canonical input schemas:
|
||||||
// - weather.observation.v1 -> model.WeatherObservation
|
// - weather.observation.v1 -> model.WeatherObservation
|
||||||
// - weather.forecast.v1 -> model.WeatherForecastRun
|
// - weather.forecast.v1 -> model.WeatherForecastRun
|
||||||
|
// - weather.forecast_discussion.v1 -> model.WeatherForecastDiscussion
|
||||||
|
// - weather.weather_story.v1 -> model.WeatherStoryRun
|
||||||
// - weather.alert.v1 -> model.WeatherAlertRun
|
// - weather.alert.v1 -> model.WeatherAlertRun
|
||||||
|
// - weather.outlook.v1 -> model.WeatherOutlookRun
|
||||||
//
|
//
|
||||||
// Parent/child relationships:
|
// Parent/child relationships:
|
||||||
// - observations.event_id -> observation_present_weather.event_id
|
// - observations.event_id -> observation_present_weather.event_id
|
||||||
// - forecasts.event_id -> forecast_periods.run_event_id
|
// - forecasts.event_id -> forecast_periods.run_event_id
|
||||||
|
// - forecast_discussions.event_id -> forecast_discussion_key_messages.run_event_id
|
||||||
|
// - weather_story_runs.event_id -> weather_stories.run_event_id
|
||||||
// - alert_runs.event_id -> alerts.run_event_id
|
// - alert_runs.event_id -> alerts.run_event_id
|
||||||
// - alerts.(run_event_id, alert_index) -> alert_references.(run_event_id, alert_index)
|
// - alerts.(run_event_id, alert_index) -> alert_references.(run_event_id, alert_index)
|
||||||
|
// - outlook_runs.event_id -> outlooks.run_event_id
|
||||||
//
|
//
|
||||||
// Dedupe and retention behavior:
|
// Dedupe and retention behavior:
|
||||||
// - Parent primary keys (event_id): observations, forecasts, alert_runs.
|
// - Parent primary keys (event_id): observations, forecasts, alert_runs, outlook_runs.
|
||||||
// - Child primary keys use positional indexes to preserve payload order.
|
// - Child primary keys use positional indexes to preserve payload order.
|
||||||
// - Prune columns:
|
// - Prune columns:
|
||||||
// - observations.observed_at
|
// - observations.observed_at
|
||||||
// - observation_present_weather.observed_at
|
// - observation_present_weather.observed_at
|
||||||
// - forecasts.issued_at
|
// - forecasts.issued_at
|
||||||
// - forecast_periods.issued_at
|
// - forecast_periods.issued_at
|
||||||
|
// - forecast_discussions.issued_at
|
||||||
|
// - forecast_discussion_key_messages.issued_at
|
||||||
|
// - weather_story_runs.as_of
|
||||||
|
// - weather_stories.as_of
|
||||||
// - alert_runs.as_of
|
// - alert_runs.as_of
|
||||||
// - alerts.as_of
|
// - alerts.as_of
|
||||||
// - alert_references.as_of
|
// - alert_references.as_of
|
||||||
|
// - outlook_runs.as_of
|
||||||
|
// - outlooks.as_of
|
||||||
//
|
//
|
||||||
// Envelope field mapping (shared parent columns)
|
// Envelope field mapping (shared parent columns)
|
||||||
//
|
//
|
||||||
// These columns exist on observations, forecasts, and alert_runs:
|
// These columns exist on parent tables such as observations, forecasts,
|
||||||
|
// forecast_discussions, weather_story_runs, alert_runs, and outlook_runs:
|
||||||
// - event_id TEXT -> event.id
|
// - event_id TEXT -> event.id
|
||||||
// - event_kind TEXT -> event.kind
|
// - event_kind TEXT -> event.kind
|
||||||
// - event_source TEXT -> event.source
|
// - event_source TEXT -> event.source
|
||||||
@@ -101,7 +114,7 @@
|
|||||||
// - end_time TIMESTAMPTZ -> payload.periods[i].endTime
|
// - end_time TIMESTAMPTZ -> payload.periods[i].endTime
|
||||||
// - name TEXT NULL -> payload.periods[i].name
|
// - name TEXT NULL -> payload.periods[i].name
|
||||||
// - is_day BOOLEAN NULL -> payload.periods[i].isDay
|
// - is_day BOOLEAN NULL -> payload.periods[i].isDay
|
||||||
// - condition_code INTEGER -> payload.periods[i].conditionCode
|
// - condition_code INTEGER NULL -> payload.periods[i].conditionCode
|
||||||
// - text_description TEXT NULL -> payload.periods[i].textDescription
|
// - text_description TEXT NULL -> payload.periods[i].textDescription
|
||||||
// - temperature_c DOUBLE PRECISION NULL -> payload.periods[i].temperatureC
|
// - temperature_c DOUBLE PRECISION NULL -> payload.periods[i].temperatureC
|
||||||
// - temperature_c_min DOUBLE PRECISION NULL -> payload.periods[i].temperatureCMin
|
// - temperature_c_min DOUBLE PRECISION NULL -> payload.periods[i].temperatureCMin
|
||||||
@@ -120,7 +133,35 @@
|
|||||||
// - snowfall_depth_mm DOUBLE PRECISION NULL -> payload.periods[i].snowfallDepthMm
|
// - snowfall_depth_mm DOUBLE PRECISION NULL -> payload.periods[i].snowfallDepthMm
|
||||||
// - uv_index DOUBLE PRECISION NULL -> payload.periods[i].uvIndex
|
// - uv_index DOUBLE PRECISION NULL -> payload.periods[i].uvIndex
|
||||||
//
|
//
|
||||||
// 5. alert_runs (PK: event_id)
|
// 5. weather_story_runs (PK: event_id)
|
||||||
|
//
|
||||||
|
// - event_id TEXT -> event.id
|
||||||
|
// - event_kind TEXT -> event.kind
|
||||||
|
// - event_source TEXT -> event.source
|
||||||
|
// - event_schema TEXT -> event.schema
|
||||||
|
// - event_emitted_at TIMESTAMPTZ -> event.emitted_at
|
||||||
|
// - event_effective_at TIMESTAMPTZ NULL -> event.effective_at
|
||||||
|
// - office_id TEXT NULL -> payload.officeId
|
||||||
|
// - as_of TIMESTAMPTZ -> payload.asOf
|
||||||
|
// - story_count INTEGER -> len(payload.stories)
|
||||||
|
//
|
||||||
|
// 6. weather_stories (PK: run_event_id, story_index)
|
||||||
|
//
|
||||||
|
// - run_event_id TEXT -> weather_story_runs.event_id / payload.stories[i]
|
||||||
|
// - story_index INTEGER -> i (array position in payload.stories)
|
||||||
|
// - as_of TIMESTAMPTZ -> payload.asOf (copied from parent)
|
||||||
|
// - office_id TEXT NULL -> payload.stories[i].officeId
|
||||||
|
// - start_time TIMESTAMPTZ -> payload.stories[i].startTime
|
||||||
|
// - end_time TIMESTAMPTZ -> payload.stories[i].endTime
|
||||||
|
// - updated_at TIMESTAMPTZ -> payload.stories[i].updatedAt
|
||||||
|
// - title TEXT NULL -> payload.stories[i].title
|
||||||
|
// - description TEXT NULL -> payload.stories[i].description
|
||||||
|
// - alt_text TEXT NULL -> payload.stories[i].altText
|
||||||
|
// - priority BOOLEAN -> payload.stories[i].priority
|
||||||
|
// - story_order INTEGER -> payload.stories[i].order
|
||||||
|
// - download_url TEXT NULL -> payload.stories[i].downloadUrl
|
||||||
|
//
|
||||||
|
// 7. alert_runs (PK: event_id)
|
||||||
//
|
//
|
||||||
// - event_id TEXT -> event.id
|
// - event_id TEXT -> event.id
|
||||||
// - event_kind TEXT -> event.kind
|
// - event_kind TEXT -> event.kind
|
||||||
@@ -135,7 +176,7 @@
|
|||||||
// - longitude DOUBLE PRECISION NULL -> payload.longitude
|
// - longitude DOUBLE PRECISION NULL -> payload.longitude
|
||||||
// - alert_count INTEGER -> len(payload.alerts)
|
// - alert_count INTEGER -> len(payload.alerts)
|
||||||
//
|
//
|
||||||
// 6. alerts (PK: run_event_id, alert_index)
|
// 8. alerts (PK: run_event_id, alert_index)
|
||||||
//
|
//
|
||||||
// - run_event_id TEXT -> alert_runs.event_id / payload.alerts[i]
|
// - run_event_id TEXT -> alert_runs.event_id / payload.alerts[i]
|
||||||
// - alert_index INTEGER -> i (array position in payload.alerts)
|
// - alert_index INTEGER -> i (array position in payload.alerts)
|
||||||
@@ -160,7 +201,7 @@
|
|||||||
// - sender_name TEXT NULL -> payload.alerts[i].senderName
|
// - sender_name TEXT NULL -> payload.alerts[i].senderName
|
||||||
// - reference_count INTEGER -> len(payload.alerts[i].references)
|
// - reference_count INTEGER -> len(payload.alerts[i].references)
|
||||||
//
|
//
|
||||||
// 7. alert_references (PK: run_event_id, alert_index, reference_index)
|
// 9. alert_references (PK: run_event_id, alert_index, reference_index)
|
||||||
//
|
//
|
||||||
// - run_event_id TEXT -> alert_runs.event_id / payload.alerts[i].references[j]
|
// - run_event_id TEXT -> alert_runs.event_id / payload.alerts[i].references[j]
|
||||||
// - alert_index INTEGER -> i (array position in payload.alerts)
|
// - alert_index INTEGER -> i (array position in payload.alerts)
|
||||||
@@ -171,6 +212,48 @@
|
|||||||
// - sender TEXT NULL -> payload.alerts[i].references[j].sender
|
// - sender TEXT NULL -> payload.alerts[i].references[j].sender
|
||||||
// - sent TIMESTAMPTZ NULL -> payload.alerts[i].references[j].sent
|
// - sent TIMESTAMPTZ NULL -> payload.alerts[i].references[j].sent
|
||||||
//
|
//
|
||||||
|
// 10. outlook_runs (PK: event_id)
|
||||||
|
//
|
||||||
|
// - event_id TEXT -> event.id
|
||||||
|
// - event_kind TEXT -> event.kind
|
||||||
|
// - event_source TEXT -> event.source
|
||||||
|
// - event_schema TEXT -> event.schema
|
||||||
|
// - event_emitted_at TIMESTAMPTZ -> event.emitted_at
|
||||||
|
// - event_effective_at TIMESTAMPTZ NULL -> event.effective_at
|
||||||
|
// - location_id TEXT NULL -> payload.locationId
|
||||||
|
// - location_name TEXT NULL -> payload.locationName
|
||||||
|
// - latitude DOUBLE PRECISION NULL -> payload.latitude
|
||||||
|
// - longitude DOUBLE PRECISION NULL -> payload.longitude
|
||||||
|
// - as_of TIMESTAMPTZ -> payload.asOf
|
||||||
|
// - issued_at TIMESTAMPTZ NULL -> payload.issuedAt
|
||||||
|
// - outlook_count INTEGER -> len(payload.outlooks)
|
||||||
|
//
|
||||||
|
// 11. outlooks (PK: run_event_id, outlook_index)
|
||||||
|
//
|
||||||
|
// - run_event_id TEXT -> outlook_runs.event_id / payload.outlooks[i]
|
||||||
|
// - outlook_index INTEGER -> i (array position in payload.outlooks)
|
||||||
|
// - as_of TIMESTAMPTZ -> payload.asOf (copied from parent)
|
||||||
|
// - outlook_id TEXT -> payload.outlooks[i].id
|
||||||
|
// - provider TEXT -> payload.outlooks[i].provider
|
||||||
|
// - product TEXT -> payload.outlooks[i].product
|
||||||
|
// - day INTEGER -> payload.outlooks[i].day
|
||||||
|
// - outlook_type TEXT -> payload.outlooks[i].outlookType
|
||||||
|
// - label TEXT -> payload.outlooks[i].label
|
||||||
|
// - label_text TEXT NULL -> payload.outlooks[i].labelText
|
||||||
|
// - severity_rank INTEGER NULL -> payload.outlooks[i].severityRank
|
||||||
|
// - valid_from TIMESTAMPTZ -> payload.outlooks[i].validFrom
|
||||||
|
// - valid_to TIMESTAMPTZ -> payload.outlooks[i].validTo
|
||||||
|
// - issued_at TIMESTAMPTZ -> payload.outlooks[i].issuedAt
|
||||||
|
// - expires_at TIMESTAMPTZ -> payload.outlooks[i].expiresAt
|
||||||
|
// - forecaster TEXT NULL -> payload.outlooks[i].forecaster
|
||||||
|
// - headline TEXT NULL -> payload.outlooks[i].headline
|
||||||
|
// - summary TEXT NULL -> payload.outlooks[i].summary
|
||||||
|
// - discussion TEXT NULL -> payload.outlooks[i].discussion
|
||||||
|
// - source_url TEXT NULL -> payload.outlooks[i].sourceUrl
|
||||||
|
// - image_url TEXT NULL -> payload.outlooks[i].imageUrl
|
||||||
|
// - contains_location BOOLEAN -> payload.outlooks[i].containsLocation
|
||||||
|
// - geometry_json TEXT -> compact JSON payload.outlooks[i].geometry
|
||||||
|
//
|
||||||
// Reconstructing canonical JSON payloads
|
// Reconstructing canonical JSON payloads
|
||||||
//
|
//
|
||||||
// - WeatherObservation:
|
// - WeatherObservation:
|
||||||
@@ -181,8 +264,16 @@
|
|||||||
// read one row from forecasts, then join forecast_periods by run_event_id
|
// read one row from forecasts, then join forecast_periods by run_event_id
|
||||||
// ordered by period_index to rebuild periods.
|
// ordered by period_index to rebuild periods.
|
||||||
//
|
//
|
||||||
|
// - WeatherStoryRun:
|
||||||
|
// read one row from weather_story_runs, then join weather_stories by
|
||||||
|
// run_event_id ordered by story_index to rebuild stories.
|
||||||
|
//
|
||||||
// - WeatherAlertRun:
|
// - WeatherAlertRun:
|
||||||
// read one row from alert_runs, join alerts by run_event_id ordered by
|
// read one row from alert_runs, join alerts by run_event_id ordered by
|
||||||
// alert_index, then join alert_references by (run_event_id, alert_index)
|
// alert_index, then join alert_references by (run_event_id, alert_index)
|
||||||
// ordered by reference_index to rebuild references per alert.
|
// ordered by reference_index to rebuild references per alert.
|
||||||
|
//
|
||||||
|
// - WeatherOutlookRun:
|
||||||
|
// read one row from outlook_runs, then join outlooks by run_event_id ordered
|
||||||
|
// by outlook_index to rebuild outlooks.
|
||||||
package postgres
|
package postgres
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -22,8 +23,12 @@ func mapPostgresEvent(_ context.Context, e fkevent.Event) ([]fksinks.PostgresWri
|
|||||||
return mapForecastEvent(e)
|
return mapForecastEvent(e)
|
||||||
case standards.SchemaWeatherForecastDiscussionV1:
|
case standards.SchemaWeatherForecastDiscussionV1:
|
||||||
return mapForecastDiscussionEvent(e)
|
return mapForecastDiscussionEvent(e)
|
||||||
|
case standards.SchemaWeatherStoryV1:
|
||||||
|
return mapWeatherStoryEvent(e)
|
||||||
case standards.SchemaWeatherAlertV1:
|
case standards.SchemaWeatherAlertV1:
|
||||||
return mapAlertEvent(e)
|
return mapAlertEvent(e)
|
||||||
|
case standards.SchemaWeatherOutlookV1:
|
||||||
|
return mapOutlookEvent(e)
|
||||||
default:
|
default:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -137,7 +142,7 @@ func mapForecastEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error) {
|
|||||||
"end_time": p.EndTime.UTC(),
|
"end_time": p.EndTime.UTC(),
|
||||||
"name": nullableString(p.Name),
|
"name": nullableString(p.Name),
|
||||||
"is_day": nullableBool(p.IsDay),
|
"is_day": nullableBool(p.IsDay),
|
||||||
"condition_code": int(p.ConditionCode),
|
"condition_code": nullableWMOCode(p.ConditionCode),
|
||||||
"text_description": nullableString(p.TextDescription),
|
"text_description": nullableString(p.TextDescription),
|
||||||
"temperature_c": nullableFloat64(p.TemperatureC),
|
"temperature_c": nullableFloat64(p.TemperatureC),
|
||||||
"temperature_c_min": nullableFloat64(p.TemperatureCMin),
|
"temperature_c_min": nullableFloat64(p.TemperatureCMin),
|
||||||
@@ -218,6 +223,59 @@ func mapForecastDiscussionEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error
|
|||||||
return writes, nil
|
return writes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapWeatherStoryEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error) {
|
||||||
|
run, err := decodePayload[model.WeatherStoryRun](e.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("decode weather story payload: %w", err)
|
||||||
|
}
|
||||||
|
if run.AsOf.IsZero() {
|
||||||
|
return nil, fmt.Errorf("decode weather story payload: asOf is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
asOf := run.AsOf.UTC()
|
||||||
|
writes := make([]fksinks.PostgresWrite, 0, 1+len(run.Stories))
|
||||||
|
writes = append(writes, fksinks.PostgresWrite{
|
||||||
|
Table: tableWeatherStoryRuns,
|
||||||
|
Values: map[string]any{
|
||||||
|
"event_id": e.ID,
|
||||||
|
"event_kind": string(e.Kind),
|
||||||
|
"event_source": e.Source,
|
||||||
|
"event_schema": e.Schema,
|
||||||
|
"event_emitted_at": e.EmittedAt.UTC(),
|
||||||
|
"event_effective_at": nullableTime(e.EffectiveAt),
|
||||||
|
"office_id": nullableString(run.OfficeID),
|
||||||
|
"as_of": asOf,
|
||||||
|
"story_count": len(run.Stories),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
for i, story := range run.Stories {
|
||||||
|
if story.StartTime.IsZero() || story.EndTime.IsZero() || story.UpdatedAt.IsZero() {
|
||||||
|
return nil, fmt.Errorf("decode weather story payload: stories[%d] startTime/endTime/updatedAt are required", i)
|
||||||
|
}
|
||||||
|
writes = append(writes, fksinks.PostgresWrite{
|
||||||
|
Table: tableWeatherStories,
|
||||||
|
Values: map[string]any{
|
||||||
|
"run_event_id": e.ID,
|
||||||
|
"story_index": i,
|
||||||
|
"as_of": asOf,
|
||||||
|
"office_id": nullableString(story.OfficeID),
|
||||||
|
"start_time": story.StartTime.UTC(),
|
||||||
|
"end_time": story.EndTime.UTC(),
|
||||||
|
"updated_at": story.UpdatedAt.UTC(),
|
||||||
|
"title": nullableString(story.Title),
|
||||||
|
"description": nullableString(story.Description),
|
||||||
|
"alt_text": nullableString(story.AltText),
|
||||||
|
"priority": story.Priority,
|
||||||
|
"story_order": story.Order,
|
||||||
|
"download_url": nullableString(story.DownloadURL),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return writes, nil
|
||||||
|
}
|
||||||
|
|
||||||
func mapAlertEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error) {
|
func mapAlertEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error) {
|
||||||
run, err := decodePayload[model.WeatherAlertRun](e.Payload)
|
run, err := decodePayload[model.WeatherAlertRun](e.Payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -301,6 +359,109 @@ func mapAlertEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error) {
|
|||||||
return writes, nil
|
return writes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapOutlookEvent(e fkevent.Event) ([]fksinks.PostgresWrite, error) {
|
||||||
|
run, err := decodePayload[model.WeatherOutlookRun](e.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("decode outlook payload: %w", err)
|
||||||
|
}
|
||||||
|
if run.AsOf.IsZero() {
|
||||||
|
return nil, fmt.Errorf("decode outlook payload: asOf is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
asOf := run.AsOf.UTC()
|
||||||
|
writes := make([]fksinks.PostgresWrite, 0, 1+len(run.Outlooks))
|
||||||
|
writes = append(writes, fksinks.PostgresWrite{
|
||||||
|
Table: tableOutlookRuns,
|
||||||
|
Values: map[string]any{
|
||||||
|
"event_id": e.ID,
|
||||||
|
"event_kind": string(e.Kind),
|
||||||
|
"event_source": e.Source,
|
||||||
|
"event_schema": e.Schema,
|
||||||
|
"event_emitted_at": e.EmittedAt.UTC(),
|
||||||
|
"event_effective_at": nullableTime(e.EffectiveAt),
|
||||||
|
"location_id": nullableString(run.LocationID),
|
||||||
|
"location_name": nullableString(run.LocationName),
|
||||||
|
"latitude": nullableFloat64(run.Latitude),
|
||||||
|
"longitude": nullableFloat64(run.Longitude),
|
||||||
|
"as_of": asOf,
|
||||||
|
"issued_at": nullableTime(run.IssuedAt),
|
||||||
|
"outlook_count": len(run.Outlooks),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
for i, outlook := range run.Outlooks {
|
||||||
|
if err := validateOutlook(outlook, i); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
geometryJSON, err := requiredCompactJSONText(outlook.Geometry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("decode outlook payload: outlooks[%d].geometry: %w", i, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
writes = append(writes, fksinks.PostgresWrite{
|
||||||
|
Table: tableOutlooks,
|
||||||
|
Values: map[string]any{
|
||||||
|
"run_event_id": e.ID,
|
||||||
|
"outlook_index": i,
|
||||||
|
"as_of": asOf,
|
||||||
|
"outlook_id": outlook.ID,
|
||||||
|
"provider": outlook.Provider,
|
||||||
|
"product": outlook.Product,
|
||||||
|
"day": outlook.Day,
|
||||||
|
"outlook_type": outlook.OutlookType,
|
||||||
|
"label": outlook.Label,
|
||||||
|
"label_text": nullableString(outlook.LabelText),
|
||||||
|
"severity_rank": nullableInt(outlook.SeverityRank),
|
||||||
|
"valid_from": outlook.ValidFrom.UTC(),
|
||||||
|
"valid_to": outlook.ValidTo.UTC(),
|
||||||
|
"issued_at": outlook.IssuedAt.UTC(),
|
||||||
|
"expires_at": outlook.ExpiresAt.UTC(),
|
||||||
|
"forecaster": nullableString(outlook.Forecaster),
|
||||||
|
"headline": nullableString(outlook.Headline),
|
||||||
|
"summary": nullableString(outlook.Summary),
|
||||||
|
"discussion": nullableString(outlook.Discussion),
|
||||||
|
"source_url": nullableString(outlook.SourceURL),
|
||||||
|
"image_url": nullableString(outlook.ImageURL),
|
||||||
|
"contains_location": outlook.ContainsLocation,
|
||||||
|
"geometry_json": geometryJSON,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return writes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateOutlook(outlook model.WeatherOutlook, index int) error {
|
||||||
|
if strings.TrimSpace(outlook.ID) == "" {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].id is required", index)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(outlook.Provider) == "" {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].provider is required", index)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(outlook.Product) == "" {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].product is required", index)
|
||||||
|
}
|
||||||
|
if outlook.Day == 0 {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].day is required", index)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(outlook.OutlookType) == "" {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].outlookType is required", index)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(outlook.Label) == "" {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].label is required", index)
|
||||||
|
}
|
||||||
|
if outlook.ValidFrom.IsZero() || outlook.ValidTo.IsZero() {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d] validFrom/validTo are required", index)
|
||||||
|
}
|
||||||
|
if outlook.IssuedAt.IsZero() || outlook.ExpiresAt.IsZero() {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d] issuedAt/expiresAt are required", index)
|
||||||
|
}
|
||||||
|
if len(outlook.Geometry) == 0 {
|
||||||
|
return fmt.Errorf("decode outlook payload: outlooks[%d].geometry is required", index)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func decodePayload[T any](payload any) (T, error) {
|
func decodePayload[T any](payload any) (T, error) {
|
||||||
var out T
|
var out T
|
||||||
if payload == nil {
|
if payload == nil {
|
||||||
@@ -363,6 +524,13 @@ func nullableBool(v *bool) any {
|
|||||||
return *v
|
return *v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nullableInt(v *int) any {
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return *v
|
||||||
|
}
|
||||||
|
|
||||||
func nullableTime(v *time.Time) any {
|
func nullableTime(v *time.Time) any {
|
||||||
if v == nil || v.IsZero() {
|
if v == nil || v.IsZero() {
|
||||||
return nil
|
return nil
|
||||||
@@ -370,6 +538,13 @@ func nullableTime(v *time.Time) any {
|
|||||||
return v.UTC()
|
return v.UTC()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nullableWMOCode(v *model.WMOCode) any {
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return int(*v)
|
||||||
|
}
|
||||||
|
|
||||||
func compactJSONText(v any) (any, error) {
|
func compactJSONText(v any) (any, error) {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -383,3 +558,19 @@ func compactJSONText(v any) (any, error) {
|
|||||||
}
|
}
|
||||||
return string(b), nil
|
return string(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func requiredCompactJSONText(v any) (string, error) {
|
||||||
|
compact, err := compactJSONText(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s, ok := compact.(string)
|
||||||
|
if !ok || strings.TrimSpace(s) == "" || strings.TrimSpace(s) == "null" {
|
||||||
|
return "", fmt.Errorf("is required")
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := json.Compact(&buf, []byte(s)); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return buf.String(), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,13 +63,13 @@ func TestMapPostgresEventForecastStructPayload(t *testing.T) {
|
|||||||
StartTime: time.Date(2026, 3, 16, 19, 0, 0, 0, time.UTC),
|
StartTime: time.Date(2026, 3, 16, 19, 0, 0, 0, time.UTC),
|
||||||
EndTime: time.Date(2026, 3, 16, 20, 0, 0, 0, time.UTC),
|
EndTime: time.Date(2026, 3, 16, 20, 0, 0, 0, time.UTC),
|
||||||
IsDay: &isDay,
|
IsDay: &isDay,
|
||||||
ConditionCode: model.WMOCode(2),
|
ConditionCode: wmoCodePtr(model.WMOCode(2)),
|
||||||
TemperatureC: &temp,
|
TemperatureC: &temp,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
StartTime: time.Date(2026, 3, 16, 20, 0, 0, 0, time.UTC),
|
StartTime: time.Date(2026, 3, 16, 20, 0, 0, 0, time.UTC),
|
||||||
EndTime: time.Date(2026, 3, 16, 21, 0, 0, 0, time.UTC),
|
EndTime: time.Date(2026, 3, 16, 21, 0, 0, 0, time.UTC),
|
||||||
ConditionCode: model.WMOCode(3),
|
ConditionCode: nil,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -94,6 +94,9 @@ func TestMapPostgresEventForecastStructPayload(t *testing.T) {
|
|||||||
if got := writes[1].Values["period_index"]; got != 0 {
|
if got := writes[1].Values["period_index"]; got != 0 {
|
||||||
t.Fatalf("first period index = %#v, want 0", got)
|
t.Fatalf("first period index = %#v, want 0", got)
|
||||||
}
|
}
|
||||||
|
if got := writes[2].Values["condition_code"]; got != nil {
|
||||||
|
t.Fatalf("second period condition_code = %#v, want nil", got)
|
||||||
|
}
|
||||||
|
|
||||||
assertAllWritesIncludeAllColumns(t, writes)
|
assertAllWritesIncludeAllColumns(t, writes)
|
||||||
}
|
}
|
||||||
@@ -190,6 +193,276 @@ func TestMapPostgresEventForecastDiscussionStructPayload(t *testing.T) {
|
|||||||
assertAllWritesIncludeAllColumns(t, writes)
|
assertAllWritesIncludeAllColumns(t, writes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventWeatherStoryStructPayload(t *testing.T) {
|
||||||
|
run := model.WeatherStoryRun{
|
||||||
|
OfficeID: "LSX",
|
||||||
|
AsOf: time.Date(2026, 5, 30, 9, 0, 34, 0, time.UTC),
|
||||||
|
Stories: []model.WeatherStory{
|
||||||
|
{
|
||||||
|
OfficeID: "LSX",
|
||||||
|
StartTime: time.Date(2026, 5, 30, 8, 46, 0, 0, time.UTC),
|
||||||
|
EndTime: time.Date(2026, 5, 31, 11, 0, 0, 0, time.UTC),
|
||||||
|
UpdatedAt: time.Date(2026, 5, 30, 9, 0, 34, 0, time.UTC),
|
||||||
|
Title: "Several Chances for Rain Through Monday",
|
||||||
|
Description: "Scattered showers and thunderstorms.",
|
||||||
|
AltText: "This slide shows the forecast.",
|
||||||
|
Priority: true,
|
||||||
|
Order: 1,
|
||||||
|
DownloadURL: "https://api.weather.gov/offices/LSX/weatherstories/download/story-1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
writes, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherStoryV1, "weather_story", run))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(writes) != 2 {
|
||||||
|
t.Fatalf("mapPostgresEvent() writes len = %d, want 2", len(writes))
|
||||||
|
}
|
||||||
|
if writes[0].Table != tableWeatherStoryRuns {
|
||||||
|
t.Fatalf("writes[0].Table = %q, want %q", writes[0].Table, tableWeatherStoryRuns)
|
||||||
|
}
|
||||||
|
if got := writes[0].Values["story_count"]; got != 1 {
|
||||||
|
t.Fatalf("weather_story_runs story_count = %#v, want 1", got)
|
||||||
|
}
|
||||||
|
if writes[1].Table != tableWeatherStories {
|
||||||
|
t.Fatalf("writes[1].Table = %q, want %q", writes[1].Table, tableWeatherStories)
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["download_url"]; got != "https://api.weather.gov/offices/LSX/weatherstories/download/story-1" {
|
||||||
|
t.Fatalf("weather_stories download_url = %#v", got)
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["story_order"]; got != 1 {
|
||||||
|
t.Fatalf("weather_stories story_order = %#v, want 1", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertAllWritesIncludeAllColumns(t, writes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventOutlookStructPayload(t *testing.T) {
|
||||||
|
lat := 38.6239
|
||||||
|
lon := -90.3571
|
||||||
|
issuedAt := time.Date(2026, 6, 11, 19, 45, 0, 0, time.FixedZone("UTC-5", -5*60*60))
|
||||||
|
severity := 3
|
||||||
|
run := model.WeatherOutlookRun{
|
||||||
|
LocationID: "stl",
|
||||||
|
LocationName: "St. Louis, MO",
|
||||||
|
Latitude: &lat,
|
||||||
|
Longitude: &lon,
|
||||||
|
AsOf: time.Date(2026, 6, 12, 0, 45, 0, 0, time.UTC),
|
||||||
|
IssuedAt: &issuedAt,
|
||||||
|
Outlooks: []model.WeatherOutlook{
|
||||||
|
{
|
||||||
|
ID: "outlook-1",
|
||||||
|
Provider: "spc",
|
||||||
|
Product: "convective",
|
||||||
|
Day: 1,
|
||||||
|
OutlookType: "categorical",
|
||||||
|
Label: "SLGT",
|
||||||
|
LabelText: "Slight Risk",
|
||||||
|
SeverityRank: &severity,
|
||||||
|
ValidFrom: time.Date(2026, 6, 11, 13, 0, 0, 0, time.FixedZone("UTC-5", -5*60*60)),
|
||||||
|
ValidTo: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
IssuedAt: issuedAt,
|
||||||
|
ExpiresAt: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
Forecaster: "SMITH",
|
||||||
|
Headline: "Day 1 Convective Outlook",
|
||||||
|
Summary: "Severe thunderstorms are possible.",
|
||||||
|
Discussion: "Full discussion text.",
|
||||||
|
SourceURL: "https://example.invalid/day1.geojson",
|
||||||
|
ContainsLocation: true,
|
||||||
|
Geometry: json.RawMessage(`{ "type" : "Polygon", "coordinates" : [ [ [ -91.0, 38.0 ], [ -90.0, 38.0 ], [ -90.0, 39.0 ], [ -91.0, 39.0 ], [ -91.0, 38.0 ] ] ] }`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "outlook-2",
|
||||||
|
Provider: "spc",
|
||||||
|
Product: "convective",
|
||||||
|
Day: 1,
|
||||||
|
OutlookType: "wind",
|
||||||
|
Label: "15",
|
||||||
|
ValidFrom: time.Date(2026, 6, 11, 13, 0, 0, 0, time.UTC),
|
||||||
|
ValidTo: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
IssuedAt: time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC),
|
||||||
|
ExpiresAt: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
ContainsLocation: false,
|
||||||
|
Geometry: json.RawMessage(`{"type":"Polygon","coordinates":[[[-100,35],[-98,35],[-98,37],[-100,37],[-100,35]]]}`),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
writes, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherOutlookV1, "outlook", run))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(writes) != 3 {
|
||||||
|
t.Fatalf("mapPostgresEvent() writes len = %d, want 3", len(writes))
|
||||||
|
}
|
||||||
|
if writes[0].Table != tableOutlookRuns {
|
||||||
|
t.Fatalf("writes[0].Table = %q, want %q", writes[0].Table, tableOutlookRuns)
|
||||||
|
}
|
||||||
|
if got := writes[0].Values["outlook_count"]; got != 2 {
|
||||||
|
t.Fatalf("outlook_runs outlook_count = %#v, want 2", got)
|
||||||
|
}
|
||||||
|
if got := writes[0].Values["issued_at"]; got != issuedAt.UTC() {
|
||||||
|
t.Fatalf("outlook_runs issued_at = %#v, want UTC %s", got, issuedAt.UTC())
|
||||||
|
}
|
||||||
|
if writes[1].Table != tableOutlooks || writes[2].Table != tableOutlooks {
|
||||||
|
t.Fatalf("outlook writes not in expected order")
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["outlook_index"]; got != 0 {
|
||||||
|
t.Fatalf("first outlook index = %#v, want 0", got)
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["outlook_id"]; got != "outlook-1" {
|
||||||
|
t.Fatalf("first outlook_id = %#v, want outlook-1", got)
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["provider"]; got != "spc" {
|
||||||
|
t.Fatalf("first provider = %#v, want spc", got)
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["valid_from"]; got != run.Outlooks[0].ValidFrom.UTC() {
|
||||||
|
t.Fatalf("first valid_from = %#v, want UTC %s", got, run.Outlooks[0].ValidFrom.UTC())
|
||||||
|
}
|
||||||
|
if got := writes[1].Values["geometry_json"]; got != `{"type":"Polygon","coordinates":[[[-91.0,38.0],[-90.0,38.0],[-90.0,39.0],[-91.0,39.0],[-91.0,38.0]]]}` {
|
||||||
|
t.Fatalf("first geometry_json = %#v", got)
|
||||||
|
}
|
||||||
|
if got := writes[2].Values["contains_location"]; got != false {
|
||||||
|
t.Fatalf("second contains_location = %#v, want false", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertAllWritesIncludeAllColumns(t, writes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventOutlookRejectsMissingAsOf(t *testing.T) {
|
||||||
|
_, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherOutlookV1, "outlook", model.WeatherOutlookRun{}))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = nil, want missing asOf error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "asOf is required") {
|
||||||
|
t.Fatalf("error = %q, want asOf context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventOutlookRejectsMissingIDAndProvider(t *testing.T) {
|
||||||
|
base := model.WeatherOutlook{
|
||||||
|
ID: "outlook-1",
|
||||||
|
Provider: "spc",
|
||||||
|
Product: "convective",
|
||||||
|
Day: 1,
|
||||||
|
OutlookType: "categorical",
|
||||||
|
Label: "SLGT",
|
||||||
|
ValidFrom: time.Date(2026, 6, 11, 13, 0, 0, 0, time.UTC),
|
||||||
|
ValidTo: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
IssuedAt: time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC),
|
||||||
|
ExpiresAt: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
Geometry: json.RawMessage(`{"type":"Polygon","coordinates":[[[-91,38],[-90,38],[-90,39],[-91,39],[-91,38]]]}`),
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
mutate func(*model.WeatherOutlook)
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "missing id",
|
||||||
|
mutate: func(outlook *model.WeatherOutlook) { outlook.ID = "" },
|
||||||
|
wantErr: "outlooks[0].id is required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing provider",
|
||||||
|
mutate: func(outlook *model.WeatherOutlook) { outlook.Provider = "" },
|
||||||
|
wantErr: "outlooks[0].provider is required",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
outlook := base
|
||||||
|
tt.mutate(&outlook)
|
||||||
|
run := model.WeatherOutlookRun{
|
||||||
|
AsOf: time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC),
|
||||||
|
Outlooks: []model.WeatherOutlook{outlook},
|
||||||
|
}
|
||||||
|
_, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherOutlookV1, "outlook", run))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = nil, want %q", tt.wantErr)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), tt.wantErr) {
|
||||||
|
t.Fatalf("error = %q, want %q", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventOutlookRejectsMissingRequiredTimes(t *testing.T) {
|
||||||
|
run := model.WeatherOutlookRun{
|
||||||
|
AsOf: time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC),
|
||||||
|
Outlooks: []model.WeatherOutlook{{
|
||||||
|
ID: "outlook-1",
|
||||||
|
Provider: "spc",
|
||||||
|
Product: "convective",
|
||||||
|
Day: 1,
|
||||||
|
OutlookType: "categorical",
|
||||||
|
Label: "SLGT",
|
||||||
|
Geometry: json.RawMessage(`{"type":"Polygon","coordinates":[[[-91,38],[-90,38],[-90,39],[-91,39],[-91,38]]]}`),
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
_, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherOutlookV1, "outlook", run))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = nil, want missing time error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "outlooks[0] validFrom/validTo are required") {
|
||||||
|
t.Fatalf("error = %q, want outlook time context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventOutlookRejectsEmptyGeometry(t *testing.T) {
|
||||||
|
run := model.WeatherOutlookRun{
|
||||||
|
AsOf: time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC),
|
||||||
|
Outlooks: []model.WeatherOutlook{{
|
||||||
|
ID: "outlook-1",
|
||||||
|
Provider: "spc",
|
||||||
|
Product: "convective",
|
||||||
|
Day: 1,
|
||||||
|
OutlookType: "categorical",
|
||||||
|
Label: "SLGT",
|
||||||
|
ValidFrom: time.Date(2026, 6, 11, 13, 0, 0, 0, time.UTC),
|
||||||
|
ValidTo: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
IssuedAt: time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC),
|
||||||
|
ExpiresAt: time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC),
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
_, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherOutlookV1, "outlook", run))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = nil, want geometry error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "outlooks[0].geometry is required") {
|
||||||
|
t.Fatalf("error = %q, want geometry context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventWeatherStoryRejectsMissingAsOf(t *testing.T) {
|
||||||
|
_, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherStoryV1, "weather_story", model.WeatherStoryRun{}))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = nil, want missing asOf error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "asOf is required") {
|
||||||
|
t.Fatalf("error = %q, want asOf context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapPostgresEventWeatherStoryRejectsMissingStoryTimes(t *testing.T) {
|
||||||
|
run := model.WeatherStoryRun{
|
||||||
|
AsOf: time.Date(2026, 5, 30, 9, 0, 34, 0, time.UTC),
|
||||||
|
Stories: []model.WeatherStory{{Title: "missing times"}},
|
||||||
|
}
|
||||||
|
_, err := mapPostgresEvent(context.Background(), testEvent(standards.SchemaWeatherStoryV1, "weather_story", run))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("mapPostgresEvent() error = nil, want missing story times error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "stories[0] startTime/endTime/updatedAt are required") {
|
||||||
|
t.Fatalf("error = %q, want story time context", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMapPostgresEventMapPayload(t *testing.T) {
|
func TestMapPostgresEventMapPayload(t *testing.T) {
|
||||||
run := model.WeatherForecastRun{
|
run := model.WeatherForecastRun{
|
||||||
IssuedAt: time.Date(2026, 3, 16, 18, 0, 0, 0, time.UTC),
|
IssuedAt: time.Date(2026, 3, 16, 18, 0, 0, 0, time.UTC),
|
||||||
@@ -198,7 +471,7 @@ func TestMapPostgresEventMapPayload(t *testing.T) {
|
|||||||
{
|
{
|
||||||
StartTime: time.Date(2026, 3, 16, 19, 0, 0, 0, time.UTC),
|
StartTime: time.Date(2026, 3, 16, 19, 0, 0, 0, time.UTC),
|
||||||
EndTime: time.Date(2026, 3, 16, 20, 0, 0, 0, time.UTC),
|
EndTime: time.Date(2026, 3, 16, 20, 0, 0, 0, time.UTC),
|
||||||
ConditionCode: model.WMOCode(2),
|
ConditionCode: wmoCodePtr(model.WMOCode(2)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -299,3 +572,8 @@ func tableColumnCounts() map[string]int {
|
|||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wmoCodePtr(v model.WMOCode) *model.WMOCode {
|
||||||
|
out := v
|
||||||
|
return &out
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,9 +11,13 @@ const (
|
|||||||
tableForecastPeriods = "forecast_periods"
|
tableForecastPeriods = "forecast_periods"
|
||||||
tableForecastDiscussions = "forecast_discussions"
|
tableForecastDiscussions = "forecast_discussions"
|
||||||
tableForecastDiscussionKeyMessages = "forecast_discussion_key_messages"
|
tableForecastDiscussionKeyMessages = "forecast_discussion_key_messages"
|
||||||
|
tableWeatherStoryRuns = "weather_story_runs"
|
||||||
|
tableWeatherStories = "weather_stories"
|
||||||
tableAlertRuns = "alert_runs"
|
tableAlertRuns = "alert_runs"
|
||||||
tableAlerts = "alerts"
|
tableAlerts = "alerts"
|
||||||
tableAlertReferences = "alert_references"
|
tableAlertReferences = "alert_references"
|
||||||
|
tableOutlookRuns = "outlook_runs"
|
||||||
|
tableOutlooks = "outlooks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PostgresSchema returns weatherfeeder's Postgres schema definition.
|
// PostgresSchema returns weatherfeeder's Postgres schema definition.
|
||||||
@@ -104,7 +108,7 @@ func PostgresSchema() fksinks.PostgresSchema {
|
|||||||
{Name: "end_time", Type: "TIMESTAMPTZ", Nullable: false},
|
{Name: "end_time", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
{Name: "name", Type: "TEXT", Nullable: true},
|
{Name: "name", Type: "TEXT", Nullable: true},
|
||||||
{Name: "is_day", Type: "BOOLEAN", Nullable: true},
|
{Name: "is_day", Type: "BOOLEAN", Nullable: true},
|
||||||
{Name: "condition_code", Type: "INTEGER", Nullable: false},
|
{Name: "condition_code", Type: "INTEGER", Nullable: true},
|
||||||
{Name: "text_description", Type: "TEXT", Nullable: true},
|
{Name: "text_description", Type: "TEXT", Nullable: true},
|
||||||
{Name: "temperature_c", Type: "DOUBLE PRECISION", Nullable: true},
|
{Name: "temperature_c", Type: "DOUBLE PRECISION", Nullable: true},
|
||||||
{Name: "temperature_c_min", Type: "DOUBLE PRECISION", Nullable: true},
|
{Name: "temperature_c_min", Type: "DOUBLE PRECISION", Nullable: true},
|
||||||
@@ -174,6 +178,51 @@ func PostgresSchema() fksinks.PostgresSchema {
|
|||||||
{Name: "idx_wf_discussion_message_issued_at", Columns: []string{"issued_at"}},
|
{Name: "idx_wf_discussion_message_issued_at", Columns: []string{"issued_at"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: tableWeatherStoryRuns,
|
||||||
|
Columns: []fksinks.PostgresColumn{
|
||||||
|
{Name: "event_id", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_kind", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_source", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_schema", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_emitted_at", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "event_effective_at", Type: "TIMESTAMPTZ", Nullable: true},
|
||||||
|
{Name: "office_id", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "as_of", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "story_count", Type: "INTEGER", Nullable: false},
|
||||||
|
},
|
||||||
|
PrimaryKey: []string{"event_id"},
|
||||||
|
PruneColumn: "as_of",
|
||||||
|
Indexes: []fksinks.PostgresIndex{
|
||||||
|
{Name: "idx_wf_story_run_office_as_of", Columns: []string{"office_id", "as_of"}},
|
||||||
|
{Name: "idx_wf_story_run_as_of", Columns: []string{"as_of"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: tableWeatherStories,
|
||||||
|
Columns: []fksinks.PostgresColumn{
|
||||||
|
{Name: "run_event_id", Type: "TEXT REFERENCES weather_story_runs(event_id) ON DELETE CASCADE", Nullable: false},
|
||||||
|
{Name: "story_index", Type: "INTEGER", Nullable: false},
|
||||||
|
{Name: "as_of", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "office_id", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "start_time", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "end_time", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "updated_at", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "title", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "description", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "alt_text", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "priority", Type: "BOOLEAN", Nullable: false},
|
||||||
|
{Name: "story_order", Type: "INTEGER", Nullable: false},
|
||||||
|
{Name: "download_url", Type: "TEXT", Nullable: true},
|
||||||
|
},
|
||||||
|
PrimaryKey: []string{"run_event_id", "story_index"},
|
||||||
|
PruneColumn: "as_of",
|
||||||
|
Indexes: []fksinks.PostgresIndex{
|
||||||
|
{Name: "idx_wf_stories_start_time", Columns: []string{"start_time"}},
|
||||||
|
{Name: "idx_wf_stories_end_time", Columns: []string{"end_time"}},
|
||||||
|
{Name: "idx_wf_stories_updated_at", Columns: []string{"updated_at"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: tableAlertRuns,
|
Name: tableAlertRuns,
|
||||||
Columns: []fksinks.PostgresColumn{
|
Columns: []fksinks.PostgresColumn{
|
||||||
@@ -250,6 +299,65 @@ func PostgresSchema() fksinks.PostgresSchema {
|
|||||||
{Name: "idx_wf_alert_refs_sent", Columns: []string{"sent"}},
|
{Name: "idx_wf_alert_refs_sent", Columns: []string{"sent"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: tableOutlookRuns,
|
||||||
|
Columns: []fksinks.PostgresColumn{
|
||||||
|
{Name: "event_id", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_kind", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_source", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_schema", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "event_emitted_at", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "event_effective_at", Type: "TIMESTAMPTZ", Nullable: true},
|
||||||
|
{Name: "location_id", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "location_name", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "latitude", Type: "DOUBLE PRECISION", Nullable: true},
|
||||||
|
{Name: "longitude", Type: "DOUBLE PRECISION", Nullable: true},
|
||||||
|
{Name: "as_of", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "issued_at", Type: "TIMESTAMPTZ", Nullable: true},
|
||||||
|
{Name: "outlook_count", Type: "INTEGER", Nullable: false},
|
||||||
|
},
|
||||||
|
PrimaryKey: []string{"event_id"},
|
||||||
|
PruneColumn: "as_of",
|
||||||
|
Indexes: []fksinks.PostgresIndex{
|
||||||
|
{Name: "idx_wf_outlook_run_location_as_of", Columns: []string{"location_id", "as_of"}},
|
||||||
|
{Name: "idx_wf_outlook_run_as_of", Columns: []string{"as_of"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: tableOutlooks,
|
||||||
|
Columns: []fksinks.PostgresColumn{
|
||||||
|
{Name: "run_event_id", Type: "TEXT REFERENCES outlook_runs(event_id) ON DELETE CASCADE", Nullable: false},
|
||||||
|
{Name: "outlook_index", Type: "INTEGER", Nullable: false},
|
||||||
|
{Name: "as_of", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "outlook_id", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "provider", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "product", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "day", Type: "INTEGER", Nullable: false},
|
||||||
|
{Name: "outlook_type", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "label", Type: "TEXT", Nullable: false},
|
||||||
|
{Name: "label_text", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "severity_rank", Type: "INTEGER", Nullable: true},
|
||||||
|
{Name: "valid_from", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "valid_to", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "issued_at", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "expires_at", Type: "TIMESTAMPTZ", Nullable: false},
|
||||||
|
{Name: "forecaster", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "headline", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "summary", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "discussion", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "source_url", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "image_url", Type: "TEXT", Nullable: true},
|
||||||
|
{Name: "contains_location", Type: "BOOLEAN", Nullable: false},
|
||||||
|
{Name: "geometry_json", Type: "TEXT", Nullable: false},
|
||||||
|
},
|
||||||
|
PrimaryKey: []string{"run_event_id", "outlook_index"},
|
||||||
|
PruneColumn: "as_of",
|
||||||
|
Indexes: []fksinks.PostgresIndex{
|
||||||
|
{Name: "idx_wf_outlooks_contains_valid", Columns: []string{"contains_location", "valid_from", "valid_to"}},
|
||||||
|
{Name: "idx_wf_outlooks_day_type_label", Columns: []string{"day", "outlook_type", "label"}},
|
||||||
|
{Name: "idx_wf_outlooks_valid", Columns: []string{"valid_from", "valid_to"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
MapEvent: mapPostgresEvent,
|
MapEvent: mapPostgresEvent,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestWeatherPostgresSchemaShape(t *testing.T) {
|
func TestWeatherPostgresSchemaShape(t *testing.T) {
|
||||||
s := PostgresSchema()
|
s := PostgresSchema()
|
||||||
@@ -15,9 +18,13 @@ func TestWeatherPostgresSchemaShape(t *testing.T) {
|
|||||||
tableForecastPeriods: true,
|
tableForecastPeriods: true,
|
||||||
tableForecastDiscussions: true,
|
tableForecastDiscussions: true,
|
||||||
tableForecastDiscussionKeyMessages: true,
|
tableForecastDiscussionKeyMessages: true,
|
||||||
|
tableWeatherStoryRuns: true,
|
||||||
|
tableWeatherStories: true,
|
||||||
tableAlertRuns: true,
|
tableAlertRuns: true,
|
||||||
tableAlerts: true,
|
tableAlerts: true,
|
||||||
tableAlertReferences: true,
|
tableAlertReferences: true,
|
||||||
|
tableOutlookRuns: true,
|
||||||
|
tableOutlooks: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(s.Tables) != len(wantTables) {
|
if len(s.Tables) != len(wantTables) {
|
||||||
@@ -40,3 +47,94 @@ func TestWeatherPostgresSchemaShape(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWeatherPostgresSchemaIncludesOutlookTables(t *testing.T) {
|
||||||
|
runColumns := columnsForTable(t, tableOutlookRuns)
|
||||||
|
for _, col := range []string{"event_id", "event_kind", "event_source", "event_schema", "event_emitted_at", "event_effective_at", "location_id", "location_name", "latitude", "longitude", "as_of", "issued_at", "outlook_count"} {
|
||||||
|
if !runColumns[col] {
|
||||||
|
t.Fatalf("%s missing %s column", tableOutlookRuns, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTablePrimaryKey(t, tableOutlookRuns, []string{"event_id"})
|
||||||
|
assertTableIndex(t, tableOutlookRuns, "idx_wf_outlook_run_location_as_of", []string{"location_id", "as_of"})
|
||||||
|
assertTableIndex(t, tableOutlookRuns, "idx_wf_outlook_run_as_of", []string{"as_of"})
|
||||||
|
|
||||||
|
outlookColumns := columnsForTable(t, tableOutlooks)
|
||||||
|
for _, col := range []string{"run_event_id", "outlook_index", "as_of", "outlook_id", "provider", "product", "day", "outlook_type", "label", "label_text", "severity_rank", "valid_from", "valid_to", "issued_at", "expires_at", "forecaster", "headline", "summary", "discussion", "source_url", "image_url", "contains_location", "geometry_json"} {
|
||||||
|
if !outlookColumns[col] {
|
||||||
|
t.Fatalf("%s missing %s column", tableOutlooks, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTablePrimaryKey(t, tableOutlooks, []string{"run_event_id", "outlook_index"})
|
||||||
|
assertTableIndex(t, tableOutlooks, "idx_wf_outlooks_contains_valid", []string{"contains_location", "valid_from", "valid_to"})
|
||||||
|
assertTableIndex(t, tableOutlooks, "idx_wf_outlooks_day_type_label", []string{"day", "outlook_type", "label"})
|
||||||
|
assertTableIndex(t, tableOutlooks, "idx_wf_outlooks_valid", []string{"valid_from", "valid_to"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherPostgresSchemaIncludesWeatherStoryColumns(t *testing.T) {
|
||||||
|
runColumns := columnsForTable(t, tableWeatherStoryRuns)
|
||||||
|
if !runColumns["as_of"] {
|
||||||
|
t.Fatalf("%s missing as_of column", tableWeatherStoryRuns)
|
||||||
|
}
|
||||||
|
if !runColumns["story_count"] {
|
||||||
|
t.Fatalf("%s missing story_count column", tableWeatherStoryRuns)
|
||||||
|
}
|
||||||
|
|
||||||
|
storyColumns := columnsForTable(t, tableWeatherStories)
|
||||||
|
for _, col := range []string{"start_time", "end_time", "updated_at", "title", "description", "alt_text", "priority", "story_order", "download_url"} {
|
||||||
|
if !storyColumns[col] {
|
||||||
|
t.Fatalf("%s missing %s column", tableWeatherStories, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertTablePrimaryKey(t *testing.T, table string, want []string) {
|
||||||
|
t.Helper()
|
||||||
|
for _, tbl := range PostgresSchema().Tables {
|
||||||
|
if tbl.Name != table {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Join(tbl.PrimaryKey, ",") != strings.Join(want, ",") {
|
||||||
|
t.Fatalf("%s primary key = %#v, want %#v", table, tbl.PrimaryKey, want)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Fatalf("missing table %q", table)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertTableIndex(t *testing.T, table string, name string, want []string) {
|
||||||
|
t.Helper()
|
||||||
|
for _, tbl := range PostgresSchema().Tables {
|
||||||
|
if tbl.Name != table {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, idx := range tbl.Indexes {
|
||||||
|
if idx.Name == name {
|
||||||
|
if strings.Join(idx.Columns, ",") != strings.Join(want, ",") {
|
||||||
|
t.Fatalf("%s index %s columns = %#v, want %#v", table, name, idx.Columns, want)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatalf("%s missing index %s", table, name)
|
||||||
|
}
|
||||||
|
t.Fatalf("missing table %q", table)
|
||||||
|
}
|
||||||
|
|
||||||
|
func columnsForTable(t *testing.T, table string) map[string]bool {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
schema := PostgresSchema()
|
||||||
|
for _, tbl := range schema.Tables {
|
||||||
|
if tbl.Name != table {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cols := make(map[string]bool, len(tbl.Columns))
|
||||||
|
for _, col := range tbl.Columns {
|
||||||
|
cols[col.Name] = true
|
||||||
|
}
|
||||||
|
return cols
|
||||||
|
}
|
||||||
|
t.Fatalf("missing table %q", table)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/nws"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/nws"
|
||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/openmeteo"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/openmeteo"
|
||||||
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/openweather"
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/openweather"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/internal/sources/spc"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||||
fksource "gitea.maximumdirect.net/ejr/feedkit/sources"
|
fksource "gitea.maximumdirect.net/ejr/feedkit/sources"
|
||||||
@@ -22,11 +23,15 @@ var pollDriverRegistrations = []pollDriverRegistration{
|
|||||||
{driver: "nws_forecast_discussion", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
{driver: "nws_forecast_discussion", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
||||||
return nws.NewForecastDiscussionSource(cfg)
|
return nws.NewForecastDiscussionSource(cfg)
|
||||||
}},
|
}},
|
||||||
|
{driver: "nws_weatherstories", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return nws.NewWeatherStoriesSource(cfg) }},
|
||||||
{driver: "openmeteo_observation", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return openmeteo.NewObservationSource(cfg) }},
|
{driver: "openmeteo_observation", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return openmeteo.NewObservationSource(cfg) }},
|
||||||
{driver: "openmeteo_forecast", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return openmeteo.NewForecastSource(cfg) }},
|
{driver: "openmeteo_forecast", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return openmeteo.NewForecastSource(cfg) }},
|
||||||
{driver: "openweather_observation", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
{driver: "openweather_observation", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
||||||
return openweather.NewObservationSource(cfg)
|
return openweather.NewObservationSource(cfg)
|
||||||
}},
|
}},
|
||||||
|
{driver: "spc_convective_outlook", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
||||||
|
return spc.NewConvectiveOutlookSource(cfg)
|
||||||
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterBuiltins registers the source drivers that ship with this binary.
|
// RegisterBuiltins registers the source drivers that ship with this binary.
|
||||||
|
|||||||
@@ -47,6 +47,19 @@ func TestRegisterBuiltinsRegistersNWSForecastDiscussionDriver(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegisterBuiltinsRegistersNWSWeatherStoriesDriver(t *testing.T) {
|
||||||
|
reg := fksource.NewRegistry()
|
||||||
|
RegisterBuiltins(reg)
|
||||||
|
|
||||||
|
in, err := reg.BuildInput(sourceConfigForDriver("nws_weatherstories"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("BuildInput(nws_weatherstories) error = %v", err)
|
||||||
|
}
|
||||||
|
if _, ok := in.(fksource.PollSource); !ok {
|
||||||
|
t.Fatalf("BuildInput(nws_weatherstories) type = %T, want PollSource", in)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRegisterBuiltinsDoesNotRegisterLegacyNWSForecastDriver(t *testing.T) {
|
func TestRegisterBuiltinsDoesNotRegisterLegacyNWSForecastDriver(t *testing.T) {
|
||||||
reg := fksource.NewRegistry()
|
reg := fksource.NewRegistry()
|
||||||
RegisterBuiltins(reg)
|
RegisterBuiltins(reg)
|
||||||
@@ -70,9 +83,11 @@ func TestRegisterBuiltinsRegistersAllCurrentDrivers(t *testing.T) {
|
|||||||
"nws_forecast_hourly",
|
"nws_forecast_hourly",
|
||||||
"nws_forecast_narrative",
|
"nws_forecast_narrative",
|
||||||
"nws_forecast_discussion",
|
"nws_forecast_discussion",
|
||||||
|
"nws_weatherstories",
|
||||||
"openmeteo_observation",
|
"openmeteo_observation",
|
||||||
"openmeteo_forecast",
|
"openmeteo_forecast",
|
||||||
"openweather_observation",
|
"openweather_observation",
|
||||||
|
"spc_convective_outlook",
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, driver := range drivers {
|
for _, driver := range drivers {
|
||||||
@@ -91,13 +106,18 @@ func sourceConfigForDriver(driver string) config.SourceConfig {
|
|||||||
if driver == "openweather_observation" {
|
if driver == "openweather_observation" {
|
||||||
url = "https://example.invalid?units=metric"
|
url = "https://example.invalid?units=metric"
|
||||||
}
|
}
|
||||||
|
params := map[string]any{
|
||||||
|
"url": url,
|
||||||
|
"user_agent": "test-agent",
|
||||||
|
}
|
||||||
|
if driver == "spc_convective_outlook" {
|
||||||
|
params["latitude"] = 38.6239
|
||||||
|
params["longitude"] = -90.3571
|
||||||
|
}
|
||||||
return config.SourceConfig{
|
return config.SourceConfig{
|
||||||
Name: "test-source",
|
Name: "test-source",
|
||||||
Driver: driver,
|
Driver: driver,
|
||||||
Mode: config.SourceModePoll,
|
Mode: config.SourceModePoll,
|
||||||
Params: map[string]any{
|
Params: params,
|
||||||
"url": url,
|
|
||||||
"user_agent": "test-agent",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
118
internal/sources/nws/weatherstories.go
Normal file
118
internal/sources/nws/weatherstories.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package nws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
fksources "gitea.maximumdirect.net/ejr/feedkit/sources"
|
||||||
|
nwscommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/nws"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WeatherStoriesSource polls an NWS weatherstories endpoint and emits a RAW weather story Event.
|
||||||
|
//
|
||||||
|
// Output schema:
|
||||||
|
// - standards.SchemaRawNWSWeatherStoriesV1
|
||||||
|
type WeatherStoriesSource struct {
|
||||||
|
http *fksources.HTTPSource
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWeatherStoriesSource(cfg config.SourceConfig) (*WeatherStoriesSource, error) {
|
||||||
|
const driver = "nws_weatherstories"
|
||||||
|
|
||||||
|
hs, err := fksources.NewHTTPSource(driver, cfg, "application/geo+json, application/json")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &WeatherStoriesSource{http: hs}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WeatherStoriesSource) Name() string { return s.http.Name }
|
||||||
|
|
||||||
|
func (s *WeatherStoriesSource) Kinds() []event.Kind {
|
||||||
|
return []event.Kind{event.Kind("weather_story")}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WeatherStoriesSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||||
|
raw, meta, changed, err := s.fetchRaw(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !changed {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var effectiveAt *time.Time
|
||||||
|
switch {
|
||||||
|
case !meta.ParsedLatestUpdateTime.IsZero():
|
||||||
|
t := meta.ParsedLatestUpdateTime.UTC()
|
||||||
|
effectiveAt = &t
|
||||||
|
case !meta.ParsedLatestStartTime.IsZero():
|
||||||
|
t := meta.ParsedLatestStartTime.UTC()
|
||||||
|
effectiveAt = &t
|
||||||
|
}
|
||||||
|
|
||||||
|
emittedAt := time.Now().UTC()
|
||||||
|
eventID := fksources.DefaultEventID("", s.http.Name, effectiveAt, emittedAt)
|
||||||
|
|
||||||
|
return fksources.SingleEvent(
|
||||||
|
event.Kind("weather_story"),
|
||||||
|
s.http.Name,
|
||||||
|
standards.SchemaRawNWSWeatherStoriesV1,
|
||||||
|
eventID,
|
||||||
|
emittedAt,
|
||||||
|
effectiveAt,
|
||||||
|
raw,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type weatherStoriesMeta struct {
|
||||||
|
Stories []struct {
|
||||||
|
StartTime string `json:"startTime"`
|
||||||
|
UpdateTime string `json:"updateTime"`
|
||||||
|
} `json:"stories"`
|
||||||
|
|
||||||
|
ParsedLatestUpdateTime time.Time `json:"-"`
|
||||||
|
ParsedLatestStartTime time.Time `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WeatherStoriesSource) fetchRaw(ctx context.Context) (json.RawMessage, weatherStoriesMeta, bool, error) {
|
||||||
|
raw, changed, err := s.http.FetchJSONIfChanged(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, weatherStoriesMeta{}, false, err
|
||||||
|
}
|
||||||
|
if !changed {
|
||||||
|
return nil, weatherStoriesMeta{}, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var meta weatherStoriesMeta
|
||||||
|
if err := json.Unmarshal(raw, &meta); err != nil {
|
||||||
|
return raw, weatherStoriesMeta{}, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, story := range meta.Stories {
|
||||||
|
if ts := strings.TrimSpace(story.UpdateTime); ts != "" {
|
||||||
|
if t, err := nwscommon.ParseTime(ts); err == nil {
|
||||||
|
t = t.UTC()
|
||||||
|
if meta.ParsedLatestUpdateTime.IsZero() || t.After(meta.ParsedLatestUpdateTime) {
|
||||||
|
meta.ParsedLatestUpdateTime = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ts := strings.TrimSpace(story.StartTime); ts != "" {
|
||||||
|
if t, err := nwscommon.ParseTime(ts); err == nil {
|
||||||
|
t = t.UTC()
|
||||||
|
if meta.ParsedLatestStartTime.IsZero() || t.After(meta.ParsedLatestStartTime) {
|
||||||
|
meta.ParsedLatestStartTime = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return raw, meta, true, nil
|
||||||
|
}
|
||||||
158
internal/sources/nws/weatherstories_test.go
Normal file
158
internal/sources/nws/weatherstories_test.go
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
package nws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWeatherStoriesSourcePollEmitsExpectedEventAndPrefersLatestUpdateTime(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = w.Write([]byte(`{
|
||||||
|
"stories": [
|
||||||
|
{"startTime":"2026-05-30T08:46:00+00:00","updateTime":"2026-05-30T09:00:34+00:00"},
|
||||||
|
{"startTime":"2026-05-30T10:00:00+00:00","updateTime":"2026-05-30T11:00:34+00:00"}
|
||||||
|
]
|
||||||
|
}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
src, err := NewWeatherStoriesSource(weatherStoriesSourceConfig(srv.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewWeatherStoriesSource() error = %v", err)
|
||||||
|
}
|
||||||
|
if got := src.Kinds(); len(got) != 1 || got[0] != event.Kind("weather_story") {
|
||||||
|
t.Fatalf("Kinds() = %#v, want [weather_story]", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(events) != 1 {
|
||||||
|
t.Fatalf("Poll() len = %d, want 1", len(events))
|
||||||
|
}
|
||||||
|
|
||||||
|
got := events[0]
|
||||||
|
if got.Kind != event.Kind("weather_story") {
|
||||||
|
t.Fatalf("Kind = %q, want weather_story", got.Kind)
|
||||||
|
}
|
||||||
|
if got.Schema != standards.SchemaRawNWSWeatherStoriesV1 {
|
||||||
|
t.Fatalf("Schema = %q, want %q", got.Schema, standards.SchemaRawNWSWeatherStoriesV1)
|
||||||
|
}
|
||||||
|
wantEffectiveAt := time.Date(2026, 5, 30, 11, 0, 34, 0, time.UTC)
|
||||||
|
if got.EffectiveAt == nil || !got.EffectiveAt.Equal(wantEffectiveAt) {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want %s", got.EffectiveAt, wantEffectiveAt.Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
if _, ok := got.Payload.(json.RawMessage); !ok {
|
||||||
|
t.Fatalf("Payload type = %T, want json.RawMessage", got.Payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherStoriesSourcePollEffectiveAtFallsBackToLatestStartTime(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = w.Write([]byte(`{
|
||||||
|
"stories": [
|
||||||
|
{"startTime":"2026-05-30T08:46:00+00:00","updateTime":"bad"},
|
||||||
|
{"startTime":"2026-05-31T11:00:00+00:00","updateTime":""}
|
||||||
|
]
|
||||||
|
}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
src, err := NewWeatherStoriesSource(weatherStoriesSourceConfig(srv.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewWeatherStoriesSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(events) != 1 {
|
||||||
|
t.Fatalf("Poll() len = %d, want 1", len(events))
|
||||||
|
}
|
||||||
|
wantEffectiveAt := time.Date(2026, 5, 31, 11, 0, 0, 0, time.UTC)
|
||||||
|
if events[0].EffectiveAt == nil || !events[0].EffectiveAt.Equal(wantEffectiveAt) {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want %s", events[0].EffectiveAt, wantEffectiveAt.Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherStoriesSourcePollReturnsNoEventsWhenUnchanged(t *testing.T) {
|
||||||
|
const etag = `"stories-v1"`
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Header.Get("If-None-Match") == etag {
|
||||||
|
w.WriteHeader(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("ETag", etag)
|
||||||
|
_, _ = w.Write([]byte(`{"stories":[]}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
src, err := NewWeatherStoriesSource(weatherStoriesSourceConfig(srv.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewWeatherStoriesSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
first, err := src.Poll(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("first Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(first) != 1 {
|
||||||
|
t.Fatalf("first Poll() len = %d, want 1", len(first))
|
||||||
|
}
|
||||||
|
|
||||||
|
second, err := src.Poll(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("second Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(second) != 0 {
|
||||||
|
t.Fatalf("second Poll() len = %d, want 0", len(second))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeatherStoriesSourcePollMetadataDecodeFailureStillEmitsRawEvent(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, _ = w.Write([]byte(`not-json`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
src, err := NewWeatherStoriesSource(weatherStoriesSourceConfig(srv.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewWeatherStoriesSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(events) != 1 {
|
||||||
|
t.Fatalf("Poll() len = %d, want 1", len(events))
|
||||||
|
}
|
||||||
|
if events[0].EffectiveAt != nil {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want nil", events[0].EffectiveAt)
|
||||||
|
}
|
||||||
|
if events[0].Schema != standards.SchemaRawNWSWeatherStoriesV1 {
|
||||||
|
t.Fatalf("Schema = %q, want %q", events[0].Schema, standards.SchemaRawNWSWeatherStoriesV1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherStoriesSourceConfig(url string) config.SourceConfig {
|
||||||
|
return config.SourceConfig{
|
||||||
|
Name: "test-weatherstories-source",
|
||||||
|
Driver: "nws_weatherstories",
|
||||||
|
Mode: config.SourceModePoll,
|
||||||
|
Params: map[string]any{
|
||||||
|
"url": url,
|
||||||
|
"user_agent": "test-agent",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
407
internal/sources/spc/convective_outlook.go
Normal file
407
internal/sources/spc/convective_outlook.go
Normal file
@@ -0,0 +1,407 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
fksources "gitea.maximumdirect.net/ejr/feedkit/sources"
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/transport"
|
||||||
|
spcprovider "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/spc"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
driverConvectiveOutlook = "spc_convective_outlook"
|
||||||
|
|
||||||
|
acceptGeoJSON = "application/geo+json, application/json"
|
||||||
|
acceptDiscussion = "text/html, application/xhtml+xml"
|
||||||
|
acceptRSS = "application/rss+xml, application/xml, text/xml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fetchProduct struct {
|
||||||
|
Key string
|
||||||
|
Day int
|
||||||
|
OutlookType string
|
||||||
|
URL string
|
||||||
|
Accept string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConvectiveOutlookSource polls SPC Day 1-3 convective outlook products and
|
||||||
|
// emits one raw outlook bundle event.
|
||||||
|
type ConvectiveOutlookSource struct {
|
||||||
|
name string
|
||||||
|
userAgent string
|
||||||
|
locationID string
|
||||||
|
locationName string
|
||||||
|
latitude float64
|
||||||
|
longitude float64
|
||||||
|
client *http.Client
|
||||||
|
bodyLimit int64
|
||||||
|
|
||||||
|
geoJSONProducts []fetchProduct
|
||||||
|
discussions []fetchProduct
|
||||||
|
rssURL string
|
||||||
|
|
||||||
|
lastHash [sha256.Size]byte
|
||||||
|
hasHash bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConvectiveOutlookSource(cfg config.SourceConfig) (*ConvectiveOutlookSource, error) {
|
||||||
|
name := strings.TrimSpace(cfg.Name)
|
||||||
|
if name == "" {
|
||||||
|
return nil, fmt.Errorf("%s: name is required", driverConvectiveOutlook)
|
||||||
|
}
|
||||||
|
if cfg.Params == nil {
|
||||||
|
return nil, fmt.Errorf("%s %q: params are required", driverConvectiveOutlook, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
userAgent, ok := cfg.ParamString("user_agent", "userAgent")
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("%s %q: params.user_agent is required", driverConvectiveOutlook, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
latitude, err := requireFloatParam(cfg, "latitude")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s %q: %w", driverConvectiveOutlook, name, err)
|
||||||
|
}
|
||||||
|
longitude, err := requireFloatParam(cfg, "longitude")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s %q: %w", driverConvectiveOutlook, name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := transport.DefaultHTTPTimeout
|
||||||
|
if _, exists := cfg.Params["http_timeout"]; exists {
|
||||||
|
var ok bool
|
||||||
|
timeout, ok = cfg.ParamDuration("http_timeout")
|
||||||
|
if !ok || timeout <= 0 {
|
||||||
|
return nil, fmt.Errorf("source %q: params.http_timeout must be a positive duration", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyLimit := transport.DefaultHTTPResponseBodyLimitBytes
|
||||||
|
if _, exists := cfg.Params["http_response_body_limit_bytes"]; exists {
|
||||||
|
rawLimit, ok := cfg.ParamInt("http_response_body_limit_bytes")
|
||||||
|
if !ok || rawLimit <= 0 {
|
||||||
|
return nil, fmt.Errorf("source %q: params.http_response_body_limit_bytes must be a positive integer", name)
|
||||||
|
}
|
||||||
|
bodyLimit = int64(rawLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
geoJSONProducts, err := configuredGeoJSONProducts(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s %q: %w", driverConvectiveOutlook, name, err)
|
||||||
|
}
|
||||||
|
discussions, err := configuredDiscussionProducts(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s %q: %w", driverConvectiveOutlook, name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rssURL := ""
|
||||||
|
if rawRSSURL, ok := cfg.ParamString("rss_url", "rssURL"); ok {
|
||||||
|
rssURL = rawRSSURL
|
||||||
|
}
|
||||||
|
|
||||||
|
locationID, _ := cfg.ParamString("location_id", "locationID")
|
||||||
|
locationName, _ := cfg.ParamString("location_name", "locationName")
|
||||||
|
|
||||||
|
return &ConvectiveOutlookSource{
|
||||||
|
name: name,
|
||||||
|
userAgent: userAgent,
|
||||||
|
locationID: locationID,
|
||||||
|
locationName: locationName,
|
||||||
|
latitude: latitude,
|
||||||
|
longitude: longitude,
|
||||||
|
client: transport.NewHTTPClient(timeout),
|
||||||
|
bodyLimit: bodyLimit,
|
||||||
|
geoJSONProducts: geoJSONProducts,
|
||||||
|
discussions: discussions,
|
||||||
|
rssURL: rssURL,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ConvectiveOutlookSource) Name() string { return s.name }
|
||||||
|
|
||||||
|
func (s *ConvectiveOutlookSource) Kinds() []event.Kind {
|
||||||
|
return []event.Kind{event.Kind("outlook")}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ConvectiveOutlookSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||||
|
fetchedAt := time.Now().UTC()
|
||||||
|
bundle := spcprovider.RawConvectiveOutlookBundle{
|
||||||
|
LocationID: s.locationID,
|
||||||
|
LocationName: s.locationName,
|
||||||
|
Latitude: s.latitude,
|
||||||
|
Longitude: s.longitude,
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Products: make([]spcprovider.RawOutlookProduct, 0, len(s.geoJSONProducts)),
|
||||||
|
Discussions: make([]spcprovider.RawDiscussionPage, 0, len(s.discussions)),
|
||||||
|
}
|
||||||
|
hash := sha256.New()
|
||||||
|
|
||||||
|
var latestIssue time.Time
|
||||||
|
for _, product := range s.geoJSONProducts {
|
||||||
|
body, err := s.fetch(ctx, product.URL, product.Accept)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("fetch geojson %s: %w", product.Key, err)
|
||||||
|
}
|
||||||
|
addHashPart(hash, product.Key, product.URL, body)
|
||||||
|
|
||||||
|
bundle.Products = append(bundle.Products, spcprovider.RawOutlookProduct{
|
||||||
|
Key: product.Key,
|
||||||
|
Day: product.Day,
|
||||||
|
OutlookType: product.OutlookType,
|
||||||
|
URL: product.URL,
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Body: json.RawMessage(body),
|
||||||
|
})
|
||||||
|
if t := latestIssueTime(body); !t.IsZero() && (latestIssue.IsZero() || t.After(latestIssue)) {
|
||||||
|
latestIssue = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var latestUpdated time.Time
|
||||||
|
for _, product := range s.discussions {
|
||||||
|
body, err := s.fetch(ctx, product.URL, product.Accept)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("fetch discussion %s: %w", product.Key, err)
|
||||||
|
}
|
||||||
|
addHashPart(hash, product.Key, product.URL, body)
|
||||||
|
|
||||||
|
bodyText := string(body)
|
||||||
|
bundle.Discussions = append(bundle.Discussions, spcprovider.RawDiscussionPage{
|
||||||
|
Key: product.Key,
|
||||||
|
Day: product.Day,
|
||||||
|
URL: product.URL,
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Body: bodyText,
|
||||||
|
})
|
||||||
|
if t := discussionUpdatedTime(bodyText); !t.IsZero() && (latestUpdated.IsZero() || t.After(latestUpdated)) {
|
||||||
|
latestUpdated = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var rssBuild time.Time
|
||||||
|
if s.rssURL != "" {
|
||||||
|
body, err := s.fetch(ctx, s.rssURL, acceptRSS)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("fetch rss: %w", err)
|
||||||
|
}
|
||||||
|
addHashPart(hash, "rss", s.rssURL, body)
|
||||||
|
|
||||||
|
bodyText := string(body)
|
||||||
|
bundle.RSS = &spcprovider.RawRSSFeed{
|
||||||
|
URL: s.rssURL,
|
||||||
|
FetchedAt: fetchedAt,
|
||||||
|
Body: bodyText,
|
||||||
|
}
|
||||||
|
if feed, err := spcprovider.ParseRSSFeed(bodyText); err == nil && feed.LastBuildDate != nil {
|
||||||
|
rssBuild = feed.LastBuildDate.UTC()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentHash [sha256.Size]byte
|
||||||
|
copy(currentHash[:], hash.Sum(nil))
|
||||||
|
if s.hasHash && currentHash == s.lastHash {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
s.lastHash = currentHash
|
||||||
|
s.hasHash = true
|
||||||
|
|
||||||
|
effectiveAt := chooseEffectiveTime(latestIssue, latestUpdated, rssBuild, fetchedAt)
|
||||||
|
emittedAt := time.Now().UTC()
|
||||||
|
eventID := fksources.DefaultEventID("", s.name, &effectiveAt, emittedAt)
|
||||||
|
|
||||||
|
return fksources.SingleEvent(
|
||||||
|
event.Kind("outlook"),
|
||||||
|
s.name,
|
||||||
|
standards.SchemaRawSPCConvectiveOutlookV1,
|
||||||
|
eventID,
|
||||||
|
emittedAt,
|
||||||
|
&effectiveAt,
|
||||||
|
bundle,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ConvectiveOutlookSource) fetch(ctx context.Context, url, accept string) ([]byte, error) {
|
||||||
|
return transport.FetchBodyWithLimit(ctx, s.client, url, s.userAgent, accept, s.bodyLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func configuredGeoJSONProducts(cfg config.SourceConfig) ([]fetchProduct, error) {
|
||||||
|
overrides, err := optionalStringMap(cfg, "geojson_urls")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := make([]fetchProduct, 0, len(spcprovider.GeoJSONProducts()))
|
||||||
|
for _, product := range spcprovider.GeoJSONProducts() {
|
||||||
|
url := product.URL
|
||||||
|
if override := strings.TrimSpace(overrides[product.Key]); override != "" {
|
||||||
|
url = override
|
||||||
|
}
|
||||||
|
out = append(out, fetchProduct{
|
||||||
|
Key: product.Key,
|
||||||
|
Day: product.Day,
|
||||||
|
OutlookType: product.OutlookType,
|
||||||
|
URL: url,
|
||||||
|
Accept: acceptGeoJSON,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func configuredDiscussionProducts(cfg config.SourceConfig) ([]fetchProduct, error) {
|
||||||
|
overrides, err := optionalStringMap(cfg, "discussion_urls")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := make([]fetchProduct, 0, len(spcprovider.DiscussionProducts()))
|
||||||
|
for _, product := range spcprovider.DiscussionProducts() {
|
||||||
|
url := product.URL
|
||||||
|
if override := strings.TrimSpace(overrides[product.Key]); override != "" {
|
||||||
|
url = override
|
||||||
|
}
|
||||||
|
out = append(out, fetchProduct{
|
||||||
|
Key: product.Key,
|
||||||
|
Day: product.Day,
|
||||||
|
URL: url,
|
||||||
|
Accept: acceptDiscussion,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func optionalStringMap(cfg config.SourceConfig, key string) (map[string]string, error) {
|
||||||
|
raw, ok := cfg.Params[key]
|
||||||
|
if !ok || raw == nil {
|
||||||
|
return map[string]string{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
out := map[string]string{}
|
||||||
|
switch typed := raw.(type) {
|
||||||
|
case map[string]string:
|
||||||
|
for k, v := range typed {
|
||||||
|
if strings.TrimSpace(k) != "" && strings.TrimSpace(v) != "" {
|
||||||
|
out[strings.TrimSpace(k)] = strings.TrimSpace(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case map[string]any:
|
||||||
|
for k, v := range typed {
|
||||||
|
s, ok := v.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("params.%s[%q] must be a string", key, k)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(k) != "" && strings.TrimSpace(s) != "" {
|
||||||
|
out[strings.TrimSpace(k)] = strings.TrimSpace(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("params.%s must be a string map", key)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func requireFloatParam(cfg config.SourceConfig, key string) (float64, error) {
|
||||||
|
raw, ok := cfg.Params[key]
|
||||||
|
if !ok || raw == nil {
|
||||||
|
return 0, fmt.Errorf("params.%s is required", key)
|
||||||
|
}
|
||||||
|
v, ok := numberFromAny(raw)
|
||||||
|
if !ok {
|
||||||
|
return 0, fmt.Errorf("params.%s must be a number", key)
|
||||||
|
}
|
||||||
|
if math.IsNaN(v) || math.IsInf(v, 0) {
|
||||||
|
return 0, fmt.Errorf("params.%s must be finite", key)
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func numberFromAny(raw any) (float64, bool) {
|
||||||
|
switch v := raw.(type) {
|
||||||
|
case float64:
|
||||||
|
return v, true
|
||||||
|
case float32:
|
||||||
|
return float64(v), true
|
||||||
|
case int:
|
||||||
|
return float64(v), true
|
||||||
|
case int8:
|
||||||
|
return float64(v), true
|
||||||
|
case int16:
|
||||||
|
return float64(v), true
|
||||||
|
case int32:
|
||||||
|
return float64(v), true
|
||||||
|
case int64:
|
||||||
|
return float64(v), true
|
||||||
|
case uint:
|
||||||
|
return float64(v), true
|
||||||
|
case uint8:
|
||||||
|
return float64(v), true
|
||||||
|
case uint16:
|
||||||
|
return float64(v), true
|
||||||
|
case uint32:
|
||||||
|
return float64(v), true
|
||||||
|
case uint64:
|
||||||
|
return float64(v), true
|
||||||
|
case string:
|
||||||
|
parsed, err := strconv.ParseFloat(strings.TrimSpace(v), 64)
|
||||||
|
return parsed, err == nil
|
||||||
|
default:
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func latestIssueTime(raw []byte) time.Time {
|
||||||
|
collection, err := spcprovider.DecodeGeoJSON(raw)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
var latest time.Time
|
||||||
|
for _, feature := range collection.Features {
|
||||||
|
t, err := spcprovider.ParseISOTimestamp(feature.Properties.IssueISO)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t = t.UTC()
|
||||||
|
if latest.IsZero() || t.After(latest) {
|
||||||
|
latest = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return latest
|
||||||
|
}
|
||||||
|
|
||||||
|
func discussionUpdatedTime(rawHTML string) time.Time {
|
||||||
|
t := spcprovider.ParsePageUpdatedTimestamp(rawHTML)
|
||||||
|
if t == nil {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
return t.UTC()
|
||||||
|
}
|
||||||
|
|
||||||
|
func chooseEffectiveTime(issue time.Time, updated time.Time, rss time.Time, fetched time.Time) time.Time {
|
||||||
|
switch {
|
||||||
|
case !issue.IsZero():
|
||||||
|
return issue.UTC()
|
||||||
|
case !updated.IsZero():
|
||||||
|
return updated.UTC()
|
||||||
|
case !rss.IsZero():
|
||||||
|
return rss.UTC()
|
||||||
|
default:
|
||||||
|
return fetched.UTC()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func addHashPart(hash interface{ Write([]byte) (int, error) }, key, url string, body []byte) {
|
||||||
|
_, _ = hash.Write([]byte(key))
|
||||||
|
_, _ = hash.Write([]byte{0})
|
||||||
|
_, _ = hash.Write([]byte(url))
|
||||||
|
_, _ = hash.Write([]byte{0})
|
||||||
|
_, _ = hash.Write(body)
|
||||||
|
_, _ = hash.Write([]byte{0})
|
||||||
|
}
|
||||||
359
internal/sources/spc/convective_outlook_test.go
Normal file
359
internal/sources/spc/convective_outlook_test.go
Normal file
@@ -0,0 +1,359 @@
|
|||||||
|
package spc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||||
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||||
|
spcprovider "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/spc"
|
||||||
|
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceKinds(t *testing.T) {
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(map[string]any{}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
got := src.Kinds()
|
||||||
|
if len(got) != 1 || got[0] != event.Kind("outlook") {
|
||||||
|
t.Fatalf("Kinds() = %#v, want [outlook]", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceRequiresLatitudeAndLongitude(t *testing.T) {
|
||||||
|
for _, key := range []string{"latitude", "longitude"} {
|
||||||
|
cfg := convectiveOutlookConfig(map[string]any{})
|
||||||
|
delete(cfg.Params, key)
|
||||||
|
|
||||||
|
_, err := NewConvectiveOutlookSource(cfg)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() without %s error = nil, want error", key)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "params."+key+" is required") {
|
||||||
|
t.Fatalf("error = %q, want missing %s", err, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourcePollEmitsRawBundle(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{})
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(events) != 1 {
|
||||||
|
t.Fatalf("Poll() returned %d events, want 1", len(events))
|
||||||
|
}
|
||||||
|
got := events[0]
|
||||||
|
if got.Kind != event.Kind("outlook") {
|
||||||
|
t.Fatalf("Kind = %q, want outlook", got.Kind)
|
||||||
|
}
|
||||||
|
if got.Schema != standards.SchemaRawSPCConvectiveOutlookV1 {
|
||||||
|
t.Fatalf("Schema = %q, want %q", got.Schema, standards.SchemaRawSPCConvectiveOutlookV1)
|
||||||
|
}
|
||||||
|
wantEffective := time.Date(2026, 6, 11, 19, 45, 0, 0, time.UTC)
|
||||||
|
if got.EffectiveAt == nil || !got.EffectiveAt.Equal(wantEffective) {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want %s", got.EffectiveAt, wantEffective)
|
||||||
|
}
|
||||||
|
|
||||||
|
bundle, ok := got.Payload.(spcprovider.RawConvectiveOutlookBundle)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Payload type = %T, want RawConvectiveOutlookBundle", got.Payload)
|
||||||
|
}
|
||||||
|
if bundle.LocationID != "stl" || bundle.LocationName != "St. Louis, MO" {
|
||||||
|
t.Fatalf("location metadata = %q/%q", bundle.LocationID, bundle.LocationName)
|
||||||
|
}
|
||||||
|
if bundle.Latitude != 38.6239 || bundle.Longitude != -90.3571 {
|
||||||
|
t.Fatalf("coordinates = %v,%v", bundle.Latitude, bundle.Longitude)
|
||||||
|
}
|
||||||
|
if len(bundle.Products) != 12 {
|
||||||
|
t.Fatalf("Products length = %d, want 12", len(bundle.Products))
|
||||||
|
}
|
||||||
|
if len(bundle.Discussions) != 3 {
|
||||||
|
t.Fatalf("Discussions length = %d, want 3", len(bundle.Discussions))
|
||||||
|
}
|
||||||
|
if bundle.RSS != nil {
|
||||||
|
t.Fatalf("RSS = %#v, want nil", bundle.RSS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceEffectiveAtFallsBackToDiscussionUpdated(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{blankIssueISO: true})
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
want := time.Date(2026, 6, 11, 20, 0, 0, 0, time.UTC)
|
||||||
|
if events[0].EffectiveAt == nil || !events[0].EffectiveAt.Equal(want) {
|
||||||
|
t.Fatalf("EffectiveAt = %v, want %s", events[0].EffectiveAt, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceIncludesRSSOnlyWhenConfigured(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{})
|
||||||
|
|
||||||
|
withoutRSS, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource(without RSS) error = %v", err)
|
||||||
|
}
|
||||||
|
events, err := withoutRSS.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll(without RSS) error = %v", err)
|
||||||
|
}
|
||||||
|
if events[0].Payload.(spcprovider.RawConvectiveOutlookBundle).RSS != nil {
|
||||||
|
t.Fatalf("RSS present without rss_url")
|
||||||
|
}
|
||||||
|
|
||||||
|
withRSS, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, true)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource(with RSS) error = %v", err)
|
||||||
|
}
|
||||||
|
events, err = withRSS.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll(with RSS) error = %v", err)
|
||||||
|
}
|
||||||
|
if events[0].Payload.(spcprovider.RawConvectiveOutlookBundle).RSS == nil {
|
||||||
|
t.Fatalf("RSS missing with rss_url")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceUnchangedResponseEmitsNoEvents(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{})
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("first Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(events) != 1 {
|
||||||
|
t.Fatalf("first Poll() events = %d, want 1", len(events))
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err = src.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("second Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(events) != 0 {
|
||||||
|
t.Fatalf("second Poll() events = %d, want 0", len(events))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceGeoJSONFailureReturnsError(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{failGeoJSONKey: "day2_wind"})
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(t.Context())
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Poll() error = nil, want error")
|
||||||
|
}
|
||||||
|
if len(events) != 0 {
|
||||||
|
t.Fatalf("Poll() events = %d, want 0", len(events))
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "fetch geojson day2_wind") {
|
||||||
|
t.Fatalf("error = %q", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourceDiscussionFailureReturnsError(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{failDiscussionKey: "day2"})
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(t.Context())
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Poll() error = nil, want error")
|
||||||
|
}
|
||||||
|
if len(events) != 0 {
|
||||||
|
t.Fatalf("Poll() events = %d, want 0", len(events))
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "fetch discussion day2") {
|
||||||
|
t.Fatalf("error = %q", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvectiveOutlookSourcePayloadJSONShape(t *testing.T) {
|
||||||
|
srv := newSPCTestServer(t, spcServerOptions{})
|
||||||
|
src, err := NewConvectiveOutlookSource(convectiveOutlookConfig(serverOverrideParams(srv.URL, false)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewConvectiveOutlookSource() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
events, err := src.Poll(t.Context())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Poll() error = %v", err)
|
||||||
|
}
|
||||||
|
raw, err := json.Marshal(events[0].Payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Marshal(payload) error = %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(raw), `"products"`) || !strings.Contains(string(raw), `"discussions"`) {
|
||||||
|
t.Fatalf("payload JSON missing raw bundle fields: %s", raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type spcServerOptions struct {
|
||||||
|
blankIssueISO bool
|
||||||
|
failGeoJSONKey string
|
||||||
|
failDiscussionKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSPCTestServer(t *testing.T, opts spcServerOptions) *httptest.Server {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
for _, product := range spcprovider.GeoJSONProducts() {
|
||||||
|
product := product
|
||||||
|
mux.HandleFunc("/geojson/"+product.Key, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Header.Get("Accept") != acceptGeoJSON {
|
||||||
|
t.Errorf("geojson Accept = %q, want %q", r.Header.Get("Accept"), acceptGeoJSON)
|
||||||
|
}
|
||||||
|
if product.Key == opts.failGeoJSONKey {
|
||||||
|
http.Error(w, "failed", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/geo+json")
|
||||||
|
_, _ = w.Write(geoJSONFixtureForProduct(t, product.Key, opts.blankIssueISO))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for _, product := range spcprovider.DiscussionProducts() {
|
||||||
|
product := product
|
||||||
|
mux.HandleFunc("/discussion/"+product.Key, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Header.Get("Accept") != acceptDiscussion {
|
||||||
|
t.Errorf("discussion Accept = %q, want %q", r.Header.Get("Accept"), acceptDiscussion)
|
||||||
|
}
|
||||||
|
if product.Key == opts.failDiscussionKey {
|
||||||
|
http.Error(w, "failed", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
_, _ = w.Write(discussionFixtureForProduct(t, product.Key))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
mux.HandleFunc("/rss", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Header.Get("Accept") != acceptRSS {
|
||||||
|
t.Errorf("rss Accept = %q, want %q", r.Header.Get("Accept"), acceptRSS)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/rss+xml")
|
||||||
|
_, _ = w.Write([]byte(testRSS))
|
||||||
|
})
|
||||||
|
return httptest.NewServer(mux)
|
||||||
|
}
|
||||||
|
|
||||||
|
func convectiveOutlookConfig(extra map[string]any) config.SourceConfig {
|
||||||
|
params := map[string]any{
|
||||||
|
"latitude": 38.6239,
|
||||||
|
"longitude": -90.3571,
|
||||||
|
"location_id": "stl",
|
||||||
|
"location_name": "St. Louis, MO",
|
||||||
|
"user_agent": "test-agent",
|
||||||
|
}
|
||||||
|
for k, v := range extra {
|
||||||
|
params[k] = v
|
||||||
|
}
|
||||||
|
return config.SourceConfig{
|
||||||
|
Name: "spc-test",
|
||||||
|
Driver: driverConvectiveOutlook,
|
||||||
|
Mode: config.SourceModePoll,
|
||||||
|
Params: params,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func serverOverrideParams(baseURL string, includeRSS bool) map[string]any {
|
||||||
|
geoJSONURLs := map[string]any{}
|
||||||
|
for _, product := range spcprovider.GeoJSONProducts() {
|
||||||
|
geoJSONURLs[product.Key] = baseURL + "/geojson/" + product.Key
|
||||||
|
}
|
||||||
|
discussionURLs := map[string]any{}
|
||||||
|
for _, product := range spcprovider.DiscussionProducts() {
|
||||||
|
discussionURLs[product.Key] = baseURL + "/discussion/" + product.Key
|
||||||
|
}
|
||||||
|
|
||||||
|
out := map[string]any{
|
||||||
|
"geojson_urls": geoJSONURLs,
|
||||||
|
"discussion_urls": discussionURLs,
|
||||||
|
}
|
||||||
|
if includeRSS {
|
||||||
|
out["rss_url"] = baseURL + "/rss"
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func geoJSONFixtureForProduct(t *testing.T, key string, blankIssueISO bool) []byte {
|
||||||
|
t.Helper()
|
||||||
|
var name string
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(key, "day1_"):
|
||||||
|
name = "day1_cat.geojson"
|
||||||
|
case strings.HasPrefix(key, "day2_"):
|
||||||
|
name = "day2_torn.geojson"
|
||||||
|
case strings.HasPrefix(key, "day3_"):
|
||||||
|
name = "day3_wind.geojson"
|
||||||
|
default:
|
||||||
|
t.Fatalf("unknown product key %q", key)
|
||||||
|
}
|
||||||
|
raw := readSPCTestFixture(t, name)
|
||||||
|
if blankIssueISO {
|
||||||
|
raw = []byte(strings.ReplaceAll(string(raw), `"ISSUE_ISO": "2026-06-11T12:34:56Z"`, `"ISSUE_ISO": ""`))
|
||||||
|
raw = []byte(strings.ReplaceAll(string(raw), `"ISSUE_ISO": "2026-06-11T17:30:00Z"`, `"ISSUE_ISO": ""`))
|
||||||
|
raw = []byte(strings.ReplaceAll(string(raw), `"ISSUE_ISO": "2026-06-11T19:45:00Z"`, `"ISSUE_ISO": ""`))
|
||||||
|
}
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
|
||||||
|
func discussionFixtureForProduct(t *testing.T, key string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
switch key {
|
||||||
|
case "day1":
|
||||||
|
return readSPCTestFixture(t, "day1_prt.html")
|
||||||
|
case "day2":
|
||||||
|
return readSPCTestFixture(t, "day2_prt_corr.html")
|
||||||
|
case "day3":
|
||||||
|
return readSPCTestFixture(t, "day3_prt.html")
|
||||||
|
default:
|
||||||
|
t.Fatalf("unknown discussion key %q", key)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readSPCTestFixture(t *testing.T, name string) []byte {
|
||||||
|
t.Helper()
|
||||||
|
path := filepath.Join("..", "..", "providers", "spc", "testdata", name)
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read fixture %s: %v", path, err)
|
||||||
|
}
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
|
||||||
|
const testRSS = `<?xml version="1.0"?>
|
||||||
|
<rss version="2.0">
|
||||||
|
<channel>
|
||||||
|
<title>SPC AC RSS</title>
|
||||||
|
<lastBuildDate>Thu, 11 Jun 2026 21:00:00 +0000</lastBuildDate>
|
||||||
|
</channel>
|
||||||
|
</rss>`
|
||||||
@@ -42,7 +42,7 @@ type WeatherAlert struct {
|
|||||||
Headline string `json:"headline,omitempty"`
|
Headline string `json:"headline,omitempty"`
|
||||||
|
|
||||||
Severity string `json:"severity,omitempty"` // e.g. Extreme/Severe/Moderate/Minor/Unknown
|
Severity string `json:"severity,omitempty"` // e.g. Extreme/Severe/Moderate/Minor/Unknown
|
||||||
Urgency string `json:"urgency,omitempty"` // e.g. Immediate/Expected/Future/Past/Unknown
|
Urgency string `json:"urgency,omitempty"` // provider-defined urgency value
|
||||||
Certainty string `json:"certainty,omitempty"` // e.g. Observed/Likely/Possible/Unlikely/Unknown
|
Certainty string `json:"certainty,omitempty"` // e.g. Observed/Likely/Possible/Unlikely/Unknown
|
||||||
|
|
||||||
Status string `json:"status,omitempty"` // e.g. Actual/Exercise/Test/System/Unknown
|
Status string `json:"status,omitempty"` // e.g. Actual/Exercise/Test/System/Unknown
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// Package model defines weatherfeeder's canonical domain payload types.
|
// Package model defines weatherfeeder's canonical domain payload types.
|
||||||
//
|
//
|
||||||
// These structs are emitted as the Payload of canonical events (schemas "weather.*.vN").
|
// These structs are emitted as the Payload of canonical events (schemas "weather.*.vN").
|
||||||
// JSON tags are treated as part of the wire contract for sinks (stdout today; others later).
|
// JSON tags are treated as part of the wire contract for configured sinks.
|
||||||
//
|
//
|
||||||
// Compatibility guidance:
|
// Compatibility guidance:
|
||||||
// - Prefer additive changes.
|
// - Prefer additive changes.
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ type WeatherForecastRun struct {
|
|||||||
LocationName string `json:"locationName,omitempty"`
|
LocationName string `json:"locationName,omitempty"`
|
||||||
IssuedAt time.Time `json:"issuedAt"` // required: when this run was generated/issued
|
IssuedAt time.Time `json:"issuedAt"` // required: when this run was generated/issued
|
||||||
|
|
||||||
// Some providers include both a generated time and a later update time.
|
// Some providers include both a generated time and a subsequent update time.
|
||||||
// Keep UpdatedAt optional; many providers won’t supply it.
|
// Keep UpdatedAt optional; many providers won’t supply it.
|
||||||
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
|
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
|
||||||
|
|
||||||
@@ -71,9 +71,9 @@ type WeatherForecastPeriod struct {
|
|||||||
// Providers vary in whether they explicitly include this.
|
// Providers vary in whether they explicitly include this.
|
||||||
IsDay *bool `json:"isDay,omitempty"`
|
IsDay *bool `json:"isDay,omitempty"`
|
||||||
|
|
||||||
// Canonical internal representation (provider-independent).
|
// Canonical internal representation (provider-independent), when applicable.
|
||||||
// Like WeatherObservation, this is required; use an “unknown” WMOCode if unmappable.
|
// Some products (notably narrative) may not provide or imply a canonical WMO code.
|
||||||
ConditionCode WMOCode `json:"conditionCode"`
|
ConditionCode *WMOCode `json:"conditionCode,omitempty"`
|
||||||
|
|
||||||
// Human-facing narrative summary for this period.
|
// Human-facing narrative summary for this period.
|
||||||
TextDescription string `json:"textDescription,omitempty"`
|
TextDescription string `json:"textDescription,omitempty"`
|
||||||
|
|||||||
42
model/outlook.go
Normal file
42
model/outlook.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WeatherOutlookRun is a snapshot of convective outlook polygons for a
|
||||||
|
// configured location as-of a provider issue time.
|
||||||
|
type WeatherOutlookRun struct {
|
||||||
|
LocationID string `json:"locationId,omitempty"`
|
||||||
|
LocationName string `json:"locationName,omitempty"`
|
||||||
|
Latitude *float64 `json:"latitude,omitempty"`
|
||||||
|
Longitude *float64 `json:"longitude,omitempty"`
|
||||||
|
AsOf time.Time `json:"asOf"`
|
||||||
|
IssuedAt *time.Time `json:"issuedAt,omitempty"`
|
||||||
|
Outlooks []WeatherOutlook `json:"outlooks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WeatherOutlook is a canonical representation of one outlook polygon.
|
||||||
|
type WeatherOutlook struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Provider string `json:"provider"`
|
||||||
|
Product string `json:"product"`
|
||||||
|
Day int `json:"day"`
|
||||||
|
OutlookType string `json:"outlookType"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
LabelText string `json:"labelText,omitempty"`
|
||||||
|
SeverityRank *int `json:"severityRank,omitempty"`
|
||||||
|
ValidFrom time.Time `json:"validFrom"`
|
||||||
|
ValidTo time.Time `json:"validTo"`
|
||||||
|
IssuedAt time.Time `json:"issuedAt"`
|
||||||
|
ExpiresAt time.Time `json:"expiresAt"`
|
||||||
|
Forecaster string `json:"forecaster,omitempty"`
|
||||||
|
Headline string `json:"headline,omitempty"`
|
||||||
|
Summary string `json:"summary,omitempty"`
|
||||||
|
Discussion string `json:"discussion,omitempty"`
|
||||||
|
SourceURL string `json:"sourceUrl,omitempty"`
|
||||||
|
ImageURL string `json:"imageUrl,omitempty"`
|
||||||
|
ContainsLocation bool `json:"containsLocation"`
|
||||||
|
Geometry json.RawMessage `json:"geometry"`
|
||||||
|
}
|
||||||
28
model/weather_story.go
Normal file
28
model/weather_story.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// WeatherStoryRun is a snapshot of NWS weather stories for an office as-of a point in time.
|
||||||
|
type WeatherStoryRun struct {
|
||||||
|
OfficeID string `json:"officeId,omitempty"`
|
||||||
|
AsOf time.Time `json:"asOf"`
|
||||||
|
|
||||||
|
Stories []WeatherStory `json:"stories"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WeatherStory is a provider-independent representation of a single story card.
|
||||||
|
type WeatherStory struct {
|
||||||
|
OfficeID string `json:"officeId,omitempty"`
|
||||||
|
|
||||||
|
StartTime time.Time `json:"startTime"`
|
||||||
|
EndTime time.Time `json:"endTime"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
|
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
AltText string `json:"altText,omitempty"`
|
||||||
|
|
||||||
|
Priority bool `json:"priority"`
|
||||||
|
Order int `json:"order"`
|
||||||
|
DownloadURL string `json:"downloadUrl,omitempty"`
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// File: internal/standards/doc.go
|
// File: standards/doc.go
|
||||||
//
|
//
|
||||||
// Package standards defines weatherfeeder’s provider-agnostic “project law”:
|
// Package standards defines weatherfeeder’s provider-agnostic “project law”:
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -18,14 +18,19 @@ const (
|
|||||||
SchemaRawNWSHourlyForecastV1 = "raw.nws.hourly.forecast.v1"
|
SchemaRawNWSHourlyForecastV1 = "raw.nws.hourly.forecast.v1"
|
||||||
SchemaRawNWSNarrativeForecastV1 = "raw.nws.narrative.forecast.v1"
|
SchemaRawNWSNarrativeForecastV1 = "raw.nws.narrative.forecast.v1"
|
||||||
SchemaRawNWSForecastDiscussionV1 = "raw.nws.forecast_discussion.v1"
|
SchemaRawNWSForecastDiscussionV1 = "raw.nws.forecast_discussion.v1"
|
||||||
|
SchemaRawNWSWeatherStoriesV1 = "raw.nws.weatherstories.v1"
|
||||||
SchemaRawOpenMeteoHourlyForecastV1 = "raw.openmeteo.hourly.forecast.v1"
|
SchemaRawOpenMeteoHourlyForecastV1 = "raw.openmeteo.hourly.forecast.v1"
|
||||||
SchemaRawOpenWeatherHourlyForecastV1 = "raw.openweather.hourly.forecast.v1"
|
SchemaRawOpenWeatherHourlyForecastV1 = "raw.openweather.hourly.forecast.v1"
|
||||||
|
|
||||||
SchemaRawNWSAlertsV1 = "raw.nws.alerts.v1"
|
SchemaRawNWSAlertsV1 = "raw.nws.alerts.v1"
|
||||||
|
|
||||||
|
SchemaRawSPCConvectiveOutlookV1 = "raw.spc.convective_outlook.v1"
|
||||||
|
|
||||||
// Canonical domain schemas (emitted after normalization).
|
// Canonical domain schemas (emitted after normalization).
|
||||||
SchemaWeatherObservationV1 = "weather.observation.v1"
|
SchemaWeatherObservationV1 = "weather.observation.v1"
|
||||||
SchemaWeatherForecastV1 = "weather.forecast.v1"
|
SchemaWeatherForecastV1 = "weather.forecast.v1"
|
||||||
SchemaWeatherForecastDiscussionV1 = "weather.forecast_discussion.v1"
|
SchemaWeatherForecastDiscussionV1 = "weather.forecast_discussion.v1"
|
||||||
|
SchemaWeatherStoryV1 = "weather.weather_story.v1"
|
||||||
SchemaWeatherAlertV1 = "weather.alert.v1"
|
SchemaWeatherAlertV1 = "weather.alert.v1"
|
||||||
|
SchemaWeatherOutlookV1 = "weather.outlook.v1"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user