Skip to content

Commit a555c09

Browse files
claudeHuggeK
andcommitted
feat(api): declare where each external data source works, on the map too
New package go/internal/coverage is the machine-readable registry of every external source's geographic reach: the four forecast providers are worldwide, every price provider is European, and each bounded source carries an advisory lat/lon box, country list and licence. GET /api/data-sources (Read tier) answers for the configured site, or for an explicit ?lat=&lon= so the Weather tab can preview a pin before it is saved. The tab renders the verdict under the location picker and refreshes it as the pin drags — a site outside Europe now learns up front that no price source serves it (srcfl#726) instead of getting an empty price curve and silence. Bounds are advisory supersets: covers=false is definitive, covers=true means worth trying, and the upstream API stays authoritative. Without a known site the covers field is omitted entirely rather than defaulting to a misleading yes. The European price-country list is locked to prices/zones.go by TestEuropeanPriceCountriesMatchZoneTable — one registry, not two — which is also why it now carries ME and UA and no invented IE. Co-authored-by: HuggeK <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Wepiu9kFxZkLTEZhoTJTx4
1 parent 631d7e6 commit a555c09

7 files changed

Lines changed: 683 additions & 0 deletions

File tree

‎.changeset/data-source-coverage.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"ftw": patch
3+
---
4+
5+
Every external data source now declares where in the world it works, and the
6+
Settings map says so before you commit to a location. `GET /api/data-sources`
7+
reports each source's kind, coverage area, countries and licence, plus an
8+
advisory `covers` verdict for the configured site (or an explicit `?lat=&lon=`
9+
preview). The Weather tab renders it under the location picker and refreshes
10+
as the pin drags, so a site outside Europe learns up front that price-driven
11+
planning has no source there instead of getting an empty price curve with no
12+
explanation.
13+
14+
The registry's European price-country list is held in lockstep with
15+
`prices/zones.go` by a test, so a bidding zone added there cannot silently
16+
go missing from the coverage answer.

‎go/internal/api/api.go‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/srcfl/ftw/go/internal/config"
3737
"github.com/srcfl/ftw/go/internal/configreload"
3838
"github.com/srcfl/ftw/go/internal/control"
39+
"github.com/srcfl/ftw/go/internal/coverage"
3940
"github.com/srcfl/ftw/go/internal/driverrepo"
4041
"github.com/srcfl/ftw/go/internal/drivers"
4142
"github.com/srcfl/ftw/go/internal/evcloud"
@@ -483,6 +484,7 @@ func (s *Server) routes() {
483484
s.handle("GET /api/prices", Read, s.handlePrices)
484485
s.handle("GET /api/prices/zones", Read, s.handlePriceZones)
485486
s.handle("GET /api/forecast", Read, s.handleForecast)
487+
s.handle("GET /api/data-sources", Read, s.handleDataSources)
486488
s.handle("GET /api/mpc/plan", Read, s.handleMPCPlan)
487489
s.handle("POST /api/mpc/replan", Configure, s.handleMPCReplan)
488490
s.handle("GET /api/mpc/diagnose", Read, s.handleMPCDiagnose)
@@ -2873,6 +2875,79 @@ func (s *Server) handlePVModelReset(w http.ResponseWriter, r *http.Request) {
28732875
s.handleForecastLearningReset(w, r, "pv")
28742876
}
28752877

2878+
// ---- /api/data-sources ----
2879+
//
2880+
// Where each external data source works, and whether it covers this site.
2881+
// Response: {latitude, longitude, sources:[{id, kind, label, area, countries,
2882+
// worldwide, requires_key, license, note, covers}]}. `covers` is advisory: for
2883+
// a bounded source it is a lat/lon box test, so true means "worth trying".
2884+
// False is reliable — that location is definitely not served.
2885+
//
2886+
// This exists because some sources are regional (every price provider is
2887+
// European) and nothing previously said so: a site outside those areas got an
2888+
// empty result and no explanation. See #726.
2889+
func (s *Server) handleDataSources(w http.ResponseWriter, r *http.Request) {
2890+
var lat, lon float64
2891+
var haveSite bool
2892+
// Weather is an optional config section, so it is nil on a site that has
2893+
// never configured one — which is exactly the site most likely to be
2894+
// looking at this endpoint.
2895+
if s.deps.CfgMu != nil {
2896+
s.deps.CfgMu.RLock()
2897+
if s.deps.Cfg != nil && s.deps.Cfg.Weather != nil {
2898+
lat, lon = s.deps.Cfg.Weather.Latitude, s.deps.Cfg.Weather.Longitude
2899+
haveSite = lat != 0 || lon != 0
2900+
}
2901+
s.deps.CfgMu.RUnlock()
2902+
}
2903+
2904+
// An explicit ?lat=&lon= overrides the configured site so the Weather tab
2905+
// can preview coverage for a pin the operator is still dragging around,
2906+
// before they save it.
2907+
if v := r.URL.Query().Get("lat"); v != "" {
2908+
if f, err := strconv.ParseFloat(v, 64); err == nil {
2909+
lat, haveSite = f, true
2910+
}
2911+
}
2912+
if v := r.URL.Query().Get("lon"); v != "" {
2913+
if f, err := strconv.ParseFloat(v, 64); err == nil {
2914+
lon, haveSite = f, true
2915+
}
2916+
}
2917+
2918+
items := make([]map[string]any, 0, len(coverage.All()))
2919+
for _, src := range coverage.All() {
2920+
item := map[string]any{
2921+
"id": src.ID,
2922+
"kind": string(src.Kind),
2923+
"label": src.Label,
2924+
"area": src.Area,
2925+
"worldwide": src.Worldwide(),
2926+
"requires_key": src.RequiresKey,
2927+
}
2928+
if len(src.Countries) > 0 {
2929+
item["countries"] = src.Countries
2930+
}
2931+
if src.License != "" {
2932+
item["license"] = src.License
2933+
}
2934+
if src.Note != "" {
2935+
item["note"] = src.Note
2936+
}
2937+
// Without a site location there is nothing to test against, so omit
2938+
// `covers` entirely rather than defaulting it to a misleading true.
2939+
if haveSite {
2940+
item["covers"] = src.Covers(lat, lon)
2941+
}
2942+
items = append(items, item)
2943+
}
2944+
resp := map[string]any{"sources": items}
2945+
if haveSite {
2946+
resp["latitude"], resp["longitude"] = lat, lon
2947+
}
2948+
writeJSON(w, 200, resp)
2949+
}
2950+
28762951
// ---- static ----
28772952

28782953
// staticContentTypes pins the Content-Type of every asset kind the web tree
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"sync"
8+
"testing"
9+
10+
"github.com/srcfl/ftw/go/internal/config"
11+
)
12+
13+
type dataSource struct {
14+
ID string `json:"id"`
15+
Kind string `json:"kind"`
16+
Label string `json:"label"`
17+
Area string `json:"area"`
18+
Countries []string `json:"countries"`
19+
Worldwide bool `json:"worldwide"`
20+
RequiresKey bool `json:"requires_key"`
21+
Note string `json:"note"`
22+
Covers *bool `json:"covers"`
23+
}
24+
25+
type dataSourcesResp struct {
26+
Latitude *float64 `json:"latitude"`
27+
Longitude *float64 `json:"longitude"`
28+
Sources []dataSource `json:"sources"`
29+
}
30+
31+
func getDataSources(t *testing.T, deps *Deps, query string) dataSourcesResp {
32+
t.Helper()
33+
srv := New(deps)
34+
req := httptest.NewRequest(http.MethodGet, "/api/data-sources"+query, nil)
35+
rr := httptest.NewRecorder()
36+
srv.Handler().ServeHTTP(rr, req)
37+
if rr.Code != 200 {
38+
t.Fatalf("status = %d, want 200", rr.Code)
39+
}
40+
var resp dataSourcesResp
41+
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
42+
t.Fatal(err)
43+
}
44+
return resp
45+
}
46+
47+
func depsAt(lat, lon float64) *Deps {
48+
cfg := &config.Config{Weather: &config.Weather{Latitude: lat, Longitude: lon}}
49+
return &Deps{Cfg: cfg, CfgMu: &sync.RWMutex{}}
50+
}
51+
52+
func find(t *testing.T, resp dataSourcesResp, id string) dataSource {
53+
t.Helper()
54+
for _, s := range resp.Sources {
55+
if s.ID == id {
56+
return s
57+
}
58+
}
59+
t.Fatalf("source %q missing from response", id)
60+
return dataSource{}
61+
}
62+
63+
func TestDataSourcesListsEverySource(t *testing.T) {
64+
resp := getDataSources(t, depsAt(59.33, 18.07), "")
65+
for _, id := range []string{
66+
"met_no", "openweather", "open_meteo", "forecast_solar",
67+
"sourceful", "elprisetjustnu", "entsoe",
68+
} {
69+
find(t, resp, id) // fails the test if absent
70+
}
71+
}
72+
73+
// A Nordic site: the Swedish price feed and the European ones all apply.
74+
func TestDataSourcesCoversNordicSite(t *testing.T) {
75+
resp := getDataSources(t, depsAt(59.33, 18.07), "")
76+
for _, id := range []string{"elprisetjustnu", "sourceful", "entsoe", "open_meteo"} {
77+
s := find(t, resp, id)
78+
if s.Covers == nil || !*s.Covers {
79+
t.Errorf("%s: want covers=true for Stockholm", id)
80+
}
81+
}
82+
}
83+
84+
// The case that motivated this endpoint: outside Europe the forecast still
85+
// works, but every price provider does not.
86+
func TestDataSourcesExplainsWhySydneyIsLimited(t *testing.T) {
87+
resp := getDataSources(t, depsAt(-33.87, 151.21), "")
88+
89+
for _, id := range []string{"met_no", "openweather", "open_meteo", "forecast_solar"} {
90+
s := find(t, resp, id)
91+
if s.Covers == nil || !*s.Covers {
92+
t.Errorf("%s: forecast providers are worldwide, want covers=true", id)
93+
}
94+
}
95+
for _, id := range []string{"sourceful", "elprisetjustnu", "entsoe"} {
96+
s := find(t, resp, id)
97+
if s.Covers == nil || *s.Covers {
98+
t.Errorf("%s: want covers=false in Sydney", id)
99+
}
100+
if s.Note == "" && s.Area == "" {
101+
t.Errorf("%s: an uncovered source must still explain its area", id)
102+
}
103+
}
104+
}
105+
106+
// The Weather tab previews a pin before it is saved, so an explicit lat/lon
107+
// must override the configured site.
108+
func TestDataSourcesQueryOverridesConfiguredSite(t *testing.T) {
109+
deps := depsAt(59.33, 18.07) // configured: Stockholm
110+
resp := getDataSources(t, deps, "?lat=-33.87&lon=151.21")
111+
if s := find(t, resp, "sourceful"); s.Covers == nil || *s.Covers {
112+
t.Error("query lat/lon should override config and report not covered")
113+
}
114+
if resp.Latitude == nil || *resp.Latitude != -33.87 {
115+
t.Errorf("latitude = %v, want the overridden -33.87", resp.Latitude)
116+
}
117+
}
118+
119+
// With no location configured there is nothing to test against, so `covers`
120+
// must be absent rather than defaulting to a misleading true.
121+
func TestDataSourcesOmitsCoversWithoutASite(t *testing.T) {
122+
resp := getDataSources(t, &Deps{}, "")
123+
if len(resp.Sources) == 0 {
124+
t.Fatal("sources should still be listed without a site")
125+
}
126+
for _, s := range resp.Sources {
127+
if s.Covers != nil {
128+
t.Errorf("%s: covers should be omitted when no site is known", s.ID)
129+
}
130+
}
131+
if resp.Latitude != nil || resp.Longitude != nil {
132+
t.Error("latitude/longitude should be omitted when no site is known")
133+
}
134+
}
135+
136+
// Metadata is the whole point of the endpoint; assert it actually arrives.
137+
func TestDataSourcesCarriesRegionMetadata(t *testing.T) {
138+
resp := getDataSources(t, depsAt(59.33, 18.07), "")
139+
140+
se := find(t, resp, "elprisetjustnu")
141+
if se.Worldwide {
142+
t.Error("elprisetjustnu must not be reported worldwide")
143+
}
144+
if se.Area == "" || len(se.Countries) == 0 {
145+
t.Error("elprisetjustnu should carry an area and country list")
146+
}
147+
if se.RequiresKey {
148+
t.Error("elprisetjustnu needs no API key")
149+
}
150+
151+
if sf := find(t, resp, "sourceful"); len(sf.Countries) == 0 {
152+
t.Error("sourceful should carry the European country list")
153+
}
154+
if en := find(t, resp, "entsoe"); !en.RequiresKey {
155+
t.Error("entsoe requires an API key")
156+
}
157+
if ow := find(t, resp, "openweather"); !ow.RequiresKey {
158+
t.Error("openweather requires an API key")
159+
}
160+
if mn := find(t, resp, "met_no"); !mn.Worldwide {
161+
t.Error("met_no is worldwide")
162+
}
163+
}

0 commit comments

Comments
 (0)