feat: train proxy and correct staffing peak fixture

This commit is contained in:
Noel Sabia committed 2026-11-14 04:45:18 +01:00
1 parent f284969a4b
commit 4b9c2da9f7
7 files changed
+575 -10

No files matched your search

+54
View File
@@ -9,6 +9,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
@@ -18,10 +19,15 @@ import (
// DefaultTimeout is the contract limit for a live prediction.
const DefaultTimeout = 1500 * time.Millisecond
// controlTimeout bounds training submissions and status reads. Uploads are
// larger than prediction payloads, so they get more than DefaultTimeout.
const controlTimeout = 30 * time.Second
// Client talks to the FastAPI ML service.
type Client struct {
baseURL string
http *http.Client
control *http.Client
}
// New returns a client for baseURL (for example http://localhost:8000).
@@ -32,6 +38,7 @@ func New(baseURL string, timeout time.Duration) *Client {
return &Client{
baseURL: strings.TrimRight(baseURL, "/"),
http: &http.Client{Timeout: timeout},
control: &http.Client{Timeout: controlTimeout},
}
}
@@ -57,6 +64,15 @@ type SimulateRequest struct {
Sensors string `json:"sensors"`
}
// TrainRequest is the JSON form of POST /train.
type TrainRequest struct {
Path string `json:"path"`
Model string `json:"model"`
}
// TrainModels are the accepted training targets from CONTRACT.md §3.
var TrainModels = []string{"small", "forecast"}
// Predict calls POST /predict and returns the forecast points. The contract
// response is {"points":[...]}; a bare array is accepted as well.
func (c *Client) Predict(ctx context.Context, req PredictRequest) ([]model.ForecastPoint, error) {
@@ -99,6 +115,44 @@ func (c *Client) Simulate(ctx context.Context, req SimulateRequest) (map[string]
return out.Forecasts, nil
}
// Train proxies POST /train. body is forwarded verbatim (JSON or multipart),
// and the upstream status and response are returned unchanged for the Go
// handler to relay. Training itself runs in FastAPI; this call only queues it.
func (c *Client) Train(ctx context.Context, contentType string, body io.Reader) (int, []byte, error) {
return c.controlRequest(ctx, http.MethodPost, "/train", contentType, body)
}
// TrainStatus proxies GET /train/{job_id}.
func (c *Client) TrainStatus(ctx context.Context, jobID string) (int, []byte, error) {
return c.controlRequest(ctx, http.MethodGet, "/train/"+url.PathEscape(jobID), "", nil)
}
// controlRequest performs a training/control request and returns the raw
// upstream status and body. Non-2xx responses are not errors: the proxy
// forwards them so clients see FastAPI's 400/404/409 semantics.
func (c *Client) controlRequest(ctx context.Context, method, path, contentType string, body io.Reader) (int, []byte, error) {
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, body)
if err != nil {
return 0, nil, fmt.Errorf("ml: build request: %w", err)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
req.Header.Set("Accept", "application/json")
resp, err := c.control.Do(req)
if err != nil {
return 0, nil, fmt.Errorf("ml: %s: %w", path, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
if err != nil {
return 0, nil, fmt.Errorf("ml: read %s: %w", path, err)
}
return resp.StatusCode, raw, nil
}
func (c *Client) post(ctx context.Context, path string, body, dst any) error {
raw, err := c.postRaw(ctx, path, body)
if err != nil {
+66
View File
@@ -3,8 +3,10 @@ package ml
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
@@ -125,3 +127,67 @@ func TestNewUsesDefaultTimeout(t *testing.T) {
t.Fatalf("timeout=%s, want %s", client.http.Timeout, DefaultTimeout)
}
}
func TestTrainForwardsBodyContentTypeAndStatus(t *testing.T) {
var gotMethod, gotPath, gotType, gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
gotType = r.Header.Get("Content-Type")
raw, _ := io.ReadAll(r.Body)
gotBody = string(raw)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued","model":"small"}`))
}))
defer srv.Close()
status, body, err := New(srv.URL, time.Second).Train(context.Background(), "application/json",
strings.NewReader(`{"path":"/tmp/new.csv","model":"small"}`))
if err != nil {
t.Fatalf("Train: %v", err)
}
if status != http.StatusAccepted {
t.Fatalf("status=%d, want 202", status)
}
if !strings.Contains(string(body), `"job_id":"job-1"`) {
t.Fatalf("body=%s", body)
}
if gotMethod != http.MethodPost || gotPath != "/train" {
t.Fatalf("request=%s %s, want POST /train", gotMethod, gotPath)
}
if gotType != "application/json" || !strings.Contains(gotBody, "/tmp/new.csv") {
t.Fatalf("content-type=%q body=%q", gotType, gotBody)
}
}
func TestTrainStatusForwardsJobIDAndUpstreamError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method=%s, want GET", r.Method)
}
if r.URL.Path == "/train/job-1" {
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"succeeded","error":null}`))
return
}
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"detail":"job not found"}`))
}))
defer srv.Close()
client := New(srv.URL, time.Second)
status, body, err := client.TrainStatus(context.Background(), "job-1")
if err != nil {
t.Fatalf("TrainStatus: %v", err)
}
if status != http.StatusOK || !strings.Contains(string(body), `"succeeded"`) {
t.Fatalf("status=%d body=%s", status, body)
}
status, body, err = client.TrainStatus(context.Background(), "missing")
if err != nil {
t.Fatalf("TrainStatus missing: %v", err)
}
if status != http.StatusNotFound || !strings.Contains(string(body), "job not found") {
t.Fatalf("status=%d body=%s, want forwarded 404", status, body)
}
}