This commit is contained in:
1 parent
7e9c2d9663
commit
c1f701fc16
6 files changed
+496
-13
No files matched your search
@@ -107,6 +107,36 @@ type WeatherInput struct {
|
||||
PrecipMM float64 `json:"precip_mm"`
|
||||
}
|
||||
|
||||
// PlaygroundWeather is the optional absolute weather override for the
|
||||
// playground. Pointers distinguish "not supplied" from a real zero value.
|
||||
type PlaygroundWeather struct {
|
||||
TempC *float64 `json:"temp_c,omitempty"`
|
||||
PrecipMM *float64 `json:"precip_mm,omitempty"`
|
||||
}
|
||||
|
||||
// PlaygroundRequest is the public playground's prediction request. Weather is
|
||||
// omitted entirely when the caller did not override it, so Python uses its
|
||||
// baked hourly weather forecast.
|
||||
type PlaygroundRequest struct {
|
||||
Sensor string `json:"sensor"`
|
||||
Horizon string `json:"horizon"`
|
||||
Scenario string `json:"scenario"`
|
||||
Weather *PlaygroundWeather `json:"weather,omitempty"`
|
||||
IsHoliday *bool `json:"is_holiday,omitempty"`
|
||||
}
|
||||
|
||||
// PlaygroundResponse mirrors the subset of FastAPI POST /predict that the
|
||||
// playground returns to the browser.
|
||||
type PlaygroundResponse struct {
|
||||
Sensor string `json:"sensor"`
|
||||
Horizon string `json:"horizon"`
|
||||
Scenario string `json:"scenario"`
|
||||
Points []model.ForecastPoint `json:"points"`
|
||||
DataVersion string `json:"data_version"`
|
||||
ModelVersion string `json:"model_version"`
|
||||
ContextVersion string `json:"context_version"`
|
||||
}
|
||||
|
||||
// SimulateRequest mirrors the API contract POST /simulate.
|
||||
type SimulateRequest struct {
|
||||
Scenario string `json:"scenario"`
|
||||
@@ -148,6 +178,20 @@ func (c *Client) Predict(ctx context.Context, req PredictRequest) ([]model.Forec
|
||||
return nil, fmt.Errorf("ml: predict returned no points")
|
||||
}
|
||||
|
||||
// PlaygroundPredict calls POST /predict with optional weather and holiday
|
||||
// overrides. Unlike Predict it preserves the response versions for the public
|
||||
// playground.
|
||||
func (c *Client) PlaygroundPredict(ctx context.Context, req PlaygroundRequest) (PlaygroundResponse, error) {
|
||||
var out PlaygroundResponse
|
||||
if err := c.post(ctx, "/predict", req, &out); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if len(out.Points) == 0 {
|
||||
return out, fmt.Errorf("ml: playground predict returned no points")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Simulate calls POST /simulate and returns forecasts keyed
|
||||
// sensor -> horizon -> points.
|
||||
func (c *Client) Simulate(ctx context.Context, req SimulateRequest) (map[string]map[string][]model.ForecastPoint, error) {
|
||||
|
||||
@@ -93,6 +93,72 @@ func TestPredictTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaygroundPredictOptionalWeatherAndHoliday(t *testing.T) {
|
||||
var bodies []map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/predict" {
|
||||
t.Errorf("path=%q", r.URL.Path)
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Errorf("decode request: %v", err)
|
||||
}
|
||||
bodies = append(bodies, body)
|
||||
_, _ = w.Write([]byte(`{
|
||||
"sensor":"hn-bahnhof-01",
|
||||
"horizon":"48h",
|
||||
"scenario":"base",
|
||||
"points":[{"t":"2026-11-16T09:00:00Z","v":12.3,"lo":8.1,"hi":17.2}],
|
||||
"data_version":"d1",
|
||||
"model_version":"m1",
|
||||
"context_version":"c1"
|
||||
}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
client := New(srv.URL, time.Second)
|
||||
if _, err := client.PlaygroundPredict(context.Background(), PlaygroundRequest{
|
||||
Sensor: "hn-bahnhof-01", Horizon: "48h", Scenario: "base",
|
||||
}); err != nil {
|
||||
t.Fatalf("PlaygroundPredict without weather: %v", err)
|
||||
}
|
||||
if len(bodies) != 1 {
|
||||
t.Fatalf("requests=%d", len(bodies))
|
||||
}
|
||||
if _, ok := bodies[0]["weather"]; ok {
|
||||
t.Errorf("weather should be omitted when not supplied: %+v", bodies[0])
|
||||
}
|
||||
if _, ok := bodies[0]["is_holiday"]; ok {
|
||||
t.Errorf("is_holiday should be omitted when not supplied: %+v", bodies[0])
|
||||
}
|
||||
|
||||
temp := 29.0
|
||||
holiday := true
|
||||
resp, err := client.PlaygroundPredict(context.Background(), PlaygroundRequest{
|
||||
Sensor: "hn-bahnhof-01", Horizon: "48h", Scenario: "base",
|
||||
Weather: &PlaygroundWeather{TempC: &temp}, IsHoliday: &holiday,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PlaygroundPredict with weather: %v", err)
|
||||
}
|
||||
if resp.ModelVersion != "m1" || resp.DataVersion != "d1" || len(resp.Points) != 1 {
|
||||
t.Fatalf("response=%+v", resp)
|
||||
}
|
||||
if len(bodies) != 2 {
|
||||
t.Fatalf("requests=%d", len(bodies))
|
||||
}
|
||||
weather, ok := bodies[1]["weather"].(map[string]any)
|
||||
if !ok || weather["temp_c"] != float64(29) {
|
||||
t.Errorf("weather=%+v", bodies[1]["weather"])
|
||||
}
|
||||
if _, ok := weather["precip_mm"]; ok {
|
||||
t.Errorf("precip_mm should be omitted: %+v", weather)
|
||||
}
|
||||
if bodies[1]["is_holiday"] != true {
|
||||
t.Errorf("is_holiday=%+v", bodies[1]["is_holiday"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimulate(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req SimulateRequest
|
||||
|
||||
Reference in new issue
Block a user