feat: train proxy and correct staffing peak fixture
This commit is contained in:
1 parent
f284969a4b
commit
4b9c2da9f7
7 files changed
+575
-10
No files matched your search
@@ -24,8 +24,10 @@ import (
|
|||||||
const fixtureDir = "../../testdata/data"
|
const fixtureDir = "../../testdata/data"
|
||||||
|
|
||||||
type stubML struct {
|
type stubML struct {
|
||||||
predict func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error)
|
predict func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error)
|
||||||
simulate func(context.Context, ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error)
|
simulate func(context.Context, ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error)
|
||||||
|
train func(context.Context, string, io.Reader) (int, []byte, error)
|
||||||
|
trainStatus func(context.Context, string) (int, []byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stubML) Predict(ctx context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error) {
|
func (s *stubML) Predict(ctx context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error) {
|
||||||
@@ -42,6 +44,20 @@ func (s *stubML) Simulate(ctx context.Context, req ml.SimulateRequest) (map[stri
|
|||||||
return s.simulate(ctx, req)
|
return s.simulate(ctx, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *stubML) Train(ctx context.Context, contentType string, body io.Reader) (int, []byte, error) {
|
||||||
|
if s.train == nil {
|
||||||
|
return 0, nil, errors.New("ml unavailable")
|
||||||
|
}
|
||||||
|
return s.train(ctx, contentType, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stubML) TrainStatus(ctx context.Context, jobID string) (int, []byte, error) {
|
||||||
|
if s.trainStatus == nil {
|
||||||
|
return 0, nil, errors.New("ml unavailable")
|
||||||
|
}
|
||||||
|
return s.trainStatus(ctx, jobID)
|
||||||
|
}
|
||||||
|
|
||||||
func loadFixtureStore(t *testing.T) *store.Store {
|
func loadFixtureStore(t *testing.T) *store.Store {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
st, err := store.Load(fixtureDir)
|
st, err := store.Load(fixtureDir)
|
||||||
@@ -168,8 +184,8 @@ func TestSeriesRangeStepAndErrors(t *testing.T) {
|
|||||||
_, ts := newTestServer(t, &stubML{}, nil)
|
_, ts := newTestServer(t, &stubML{}, nil)
|
||||||
|
|
||||||
var body struct {
|
var body struct {
|
||||||
Sensor string `json:"sensor"`
|
Sensor string `json:"sensor"`
|
||||||
Step string `json:"step"`
|
Step string `json:"step"`
|
||||||
Points []model.Point `json:"points"`
|
Points []model.Point `json:"points"`
|
||||||
}
|
}
|
||||||
resp := getJSON(t, ts.URL+"/api/series?sensor=hn-bahnhof-01&from=2026-11-01T00:00:00Z&to=2026-11-01T23:59:59Z&step=15m", &body)
|
resp := getJSON(t, ts.URL+"/api/series?sensor=hn-bahnhof-01&from=2026-11-01T00:00:00Z&to=2026-11-01T23:59:59Z&step=15m", &body)
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"mime"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"pulsecast/api/internal/ml"
|
||||||
|
"pulsecast/api/internal/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// maxTrainBody bounds an uploaded CSV. The raw dataset is ~18 MB; 64 MB leaves
|
||||||
|
// room for a larger export without letting the API accept unbounded bodies.
|
||||||
|
const maxTrainBody = 64 << 20
|
||||||
|
|
||||||
|
// handleTrain proxies POST /api/train to FastAPI. The request is queued there,
|
||||||
|
// so this handler only validates the JSON form and relays the 202/409 response.
|
||||||
|
func (s *Server) handleTrain(w http.ResponseWriter, r *http.Request) {
|
||||||
|
trainer, ok := s.ml.(TrainClient)
|
||||||
|
if !ok {
|
||||||
|
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI training client is not configured")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, maxTrainBody)
|
||||||
|
|
||||||
|
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "Content-Type must be application/json or multipart/form-data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch mediaType {
|
||||||
|
case "application/json":
|
||||||
|
var req ml.TrainRequest
|
||||||
|
if err := decodeJSON(r, &req); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Path == "" {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "path is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Model == "" {
|
||||||
|
req.Model = "small"
|
||||||
|
}
|
||||||
|
if !model.Contains(ml.TrainModels, req.Model) {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "model must be small or forecast")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
raw, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal", "failed to encode request")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.relayTrain(r.Context(), trainer, w, "application/json", bytes.NewReader(raw))
|
||||||
|
case "multipart/form-data":
|
||||||
|
// Forward the stream unchanged: file and model fields belong to FastAPI.
|
||||||
|
s.relayTrain(r.Context(), trainer, w, r.Header.Get("Content-Type"), r.Body)
|
||||||
|
default:
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "Content-Type must be application/json or multipart/form-data")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleTrainStatus proxies GET /api/train/{job_id}.
|
||||||
|
func (s *Server) handleTrainStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
trainer, ok := s.ml.(TrainClient)
|
||||||
|
if !ok {
|
||||||
|
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI training client is not configured")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jobID := r.PathValue("job_id")
|
||||||
|
if jobID == "" {
|
||||||
|
writeError(w, http.StatusNotFound, "not_found", "training job not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
status, body, err := trainer.TrainStatus(r.Context(), jobID)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("train status proxy failed", "job_id", jobID, "error", err)
|
||||||
|
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
relayMLResponse(w, status, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) relayTrain(ctx context.Context, trainer TrainClient, w http.ResponseWriter, contentType string, body io.Reader) {
|
||||||
|
status, raw, err := trainer.Train(ctx, contentType, body)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("train proxy failed", "error", err)
|
||||||
|
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
relayMLResponse(w, status, raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// relayMLResponse forwards FastAPI's status and JSON body. A non-JSON upstream
|
||||||
|
// body is converted to the contract error envelope instead of leaking HTML.
|
||||||
|
func relayMLResponse(w http.ResponseWriter, status int, body []byte) {
|
||||||
|
if status < 100 || status > 599 {
|
||||||
|
writeError(w, http.StatusBadGateway, "bad_gateway", "FastAPI returned an invalid status")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
body = bytes.TrimSpace(body)
|
||||||
|
if len(body) == 0 || !json.Valid(body) {
|
||||||
|
writeError(w, http.StatusBadGateway, "bad_gateway", "FastAPI returned a non-JSON response")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
_, _ = w.Write(body)
|
||||||
|
}
|
||||||
@@ -0,0 +1,285 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"pulsecast/api/internal/config"
|
||||||
|
"pulsecast/api/internal/ml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTrainServer(t *testing.T, fastapiURL string) *httptest.Server {
|
||||||
|
t.Helper()
|
||||||
|
_, ts := newTestServer(t, ml.New(fastapiURL, 500*time.Millisecond), func(cfg *config.Config) {
|
||||||
|
cfg.FastAPIURL = fastapiURL
|
||||||
|
cfg.ControlUser = "control"
|
||||||
|
cfg.ControlPass = "secret"
|
||||||
|
})
|
||||||
|
return ts
|
||||||
|
}
|
||||||
|
|
||||||
|
func controlRequest(t *testing.T, method, url, contentType string, body io.Reader, user, pass string) (*http.Response, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
req, err := http.NewRequest(method, url, body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if contentType != "" {
|
||||||
|
req.Header.Set("Content-Type", contentType)
|
||||||
|
}
|
||||||
|
if user != "" {
|
||||||
|
req.SetBasicAuth(user, pass)
|
||||||
|
}
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s %s: %v", method, url, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
raw, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read %s %s: %v", method, url, err)
|
||||||
|
}
|
||||||
|
return resp, raw
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrainJSONProxyAndControlAuth(t *testing.T) {
|
||||||
|
var calls atomic.Int32
|
||||||
|
var gotBody []byte
|
||||||
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost || r.URL.Path != "/train" {
|
||||||
|
http.Error(w, "unexpected", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
calls.Add(1)
|
||||||
|
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
||||||
|
t.Errorf("FastAPI Content-Type=%q, want application/json", ct)
|
||||||
|
}
|
||||||
|
gotBody, _ = io.ReadAll(r.Body)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued","model":"small","submitted_at":"2026-11-14T09:00:00Z"}`))
|
||||||
|
}))
|
||||||
|
defer fastapi.Close()
|
||||||
|
ts := newTrainServer(t, fastapi.URL)
|
||||||
|
|
||||||
|
body := `{"path":"/tmp/new.csv"}`
|
||||||
|
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "", ""); resp.StatusCode != http.StatusUnauthorized {
|
||||||
|
t.Fatalf("no auth status=%d, want 401", resp.StatusCode)
|
||||||
|
} else if resp.Header.Get("WWW-Authenticate") == "" {
|
||||||
|
t.Error("missing WWW-Authenticate header")
|
||||||
|
}
|
||||||
|
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "control", "wrong"); resp.StatusCode != http.StatusUnauthorized {
|
||||||
|
t.Fatalf("bad auth status=%d, want 401", resp.StatusCode)
|
||||||
|
}
|
||||||
|
if got := calls.Load(); got != 0 {
|
||||||
|
t.Fatalf("FastAPI calls=%d before auth succeeded, want 0", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusAccepted {
|
||||||
|
t.Fatalf("train status=%d body=%s, want 202", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
var job map[string]any
|
||||||
|
if err := json.Unmarshal(raw, &job); err != nil {
|
||||||
|
t.Fatalf("decode train response: %v", err)
|
||||||
|
}
|
||||||
|
if job["job_id"] != "job-1" || job["status"] != "queued" {
|
||||||
|
t.Fatalf("train response=%v", job)
|
||||||
|
}
|
||||||
|
|
||||||
|
var forwarded ml.TrainRequest
|
||||||
|
if err := json.Unmarshal(gotBody, &forwarded); err != nil {
|
||||||
|
t.Fatalf("decode forwarded body: %v", err)
|
||||||
|
}
|
||||||
|
if forwarded.Path != "/tmp/new.csv" || forwarded.Model != "small" {
|
||||||
|
t.Fatalf("forwarded body=%+v, want path and default model=small", forwarded)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train/job-1", "", nil, "", ""); resp.StatusCode != http.StatusUnauthorized {
|
||||||
|
t.Fatalf("status without auth=%d, want 401", resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrainStatusProxy(t *testing.T) {
|
||||||
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/train/job-1":
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
t.Errorf("method=%s, want GET", r.Method)
|
||||||
|
}
|
||||||
|
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"succeeded","model":"forecast","metrics":{"accuracy":0.9},"model_version":"v2","error":null}`))
|
||||||
|
default:
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
_, _ = w.Write([]byte(`{"detail":"job not found"}`))
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer fastapi.Close()
|
||||||
|
ts := newTrainServer(t, fastapi.URL)
|
||||||
|
|
||||||
|
resp, raw := controlRequest(t, http.MethodGet, ts.URL+"/api/train/job-1", "", nil, "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("status=%d body=%s, want 200", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
var job struct {
|
||||||
|
JobID string `json:"job_id"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Metrics map[string]any `json:"metrics"`
|
||||||
|
Error any `json:"error"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &job); err != nil {
|
||||||
|
t.Fatalf("decode status response: %v", err)
|
||||||
|
}
|
||||||
|
if job.JobID != "job-1" || job.Status != "succeeded" || job.Metrics["accuracy"] != 0.9 {
|
||||||
|
t.Fatalf("status response=%+v", job)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, raw = controlRequest(t, http.MethodGet, ts.URL+"/api/train/missing", "", nil, "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusNotFound {
|
||||||
|
t.Fatalf("unknown job status=%d body=%s, want 404", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
if !json.Valid(raw) {
|
||||||
|
t.Fatalf("unknown job body is not JSON: %s", raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrainMultipartProxy(t *testing.T) {
|
||||||
|
var gotFile, gotModel string
|
||||||
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost || r.URL.Path != "/train" {
|
||||||
|
http.Error(w, "unexpected", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data; boundary=") {
|
||||||
|
t.Errorf("Content-Type=%q, want multipart/form-data", r.Header.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
||||||
|
t.Errorf("parse multipart: %v", err)
|
||||||
|
http.Error(w, "bad multipart", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gotModel = r.FormValue("model")
|
||||||
|
file, _, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("form file: %v", err)
|
||||||
|
} else {
|
||||||
|
raw, _ := io.ReadAll(file)
|
||||||
|
file.Close()
|
||||||
|
gotFile = string(raw)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
_, _ = w.Write([]byte(`{"job_id":"job-2","status":"queued","model":"forecast","submitted_at":"2026-11-14T09:00:00Z"}`))
|
||||||
|
}))
|
||||||
|
defer fastapi.Close()
|
||||||
|
ts := newTrainServer(t, fastapi.URL)
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
mw := multipart.NewWriter(&buf)
|
||||||
|
part, err := mw.CreateFormFile("file", "new-data.csv")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := part.Write([]byte("timestamp,count\n2026-11-01T10:00:00Z,42\n")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := mw.WriteField("model", "forecast"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := mw.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", mw.FormDataContentType(), &buf, "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusAccepted {
|
||||||
|
t.Fatalf("multipart status=%d body=%s, want 202", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
if gotModel != "forecast" {
|
||||||
|
t.Errorf("forwarded model=%q, want forecast", gotModel)
|
||||||
|
}
|
||||||
|
if !strings.Contains(gotFile, "42") {
|
||||||
|
t.Errorf("forwarded file=%q, want CSV content", gotFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrainValidationAndUpstreamErrors(t *testing.T) {
|
||||||
|
var calls atomic.Int32
|
||||||
|
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
calls.Add(1)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if r.Method == http.MethodPost {
|
||||||
|
w.WriteHeader(http.StatusConflict)
|
||||||
|
_, _ = w.Write([]byte(`{"detail":"a training job is already running"}`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}))
|
||||||
|
defer fastapi.Close()
|
||||||
|
ts := newTrainServer(t, fastapi.URL)
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
contentType string
|
||||||
|
body string
|
||||||
|
}{
|
||||||
|
{"unknown model", "application/json", `{"path":"/tmp/x.csv","model":"huge"}`},
|
||||||
|
{"missing path", "application/json", `{"model":"small"}`},
|
||||||
|
{"malformed json", "application/json", `{`},
|
||||||
|
{"wrong content type", "text/plain", `path=/tmp/x.csv`},
|
||||||
|
} {
|
||||||
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", tc.contentType, strings.NewReader(tc.body), "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusBadRequest {
|
||||||
|
t.Errorf("%s: status=%d body=%s, want 400", tc.name, resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if got := calls.Load(); got != 0 {
|
||||||
|
t.Fatalf("FastAPI calls=%d for invalid requests, want 0", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv","model":"forecast"}`), "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusConflict {
|
||||||
|
t.Fatalf("busy status=%d body=%s, want upstream 409", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train/job-1", "application/json", strings.NewReader(`{}`), "control", "secret"); resp.StatusCode != http.StatusMethodNotAllowed {
|
||||||
|
t.Errorf("POST job status=%d, want 405", resp.StatusCode)
|
||||||
|
} else if allow := resp.Header.Get("Allow"); allow != "GET" {
|
||||||
|
t.Errorf("Allow=%q, want GET", allow)
|
||||||
|
}
|
||||||
|
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train", "", nil, "control", "secret"); resp.StatusCode != http.StatusMethodNotAllowed {
|
||||||
|
t.Errorf("GET train status=%d, want 405", resp.StatusCode)
|
||||||
|
}
|
||||||
|
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train/a/b", "", nil, "control", "secret"); resp.StatusCode != http.StatusNotFound {
|
||||||
|
t.Errorf("nested job path status=%d, want 404", resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrainFastAPIDownAndAuthUnconfigured(t *testing.T) {
|
||||||
|
_, down := newTestServer(t, ml.New("http://127.0.0.1:1", 50*time.Millisecond), func(cfg *config.Config) {
|
||||||
|
cfg.FastAPIURL = "http://127.0.0.1:1"
|
||||||
|
cfg.ControlUser = "control"
|
||||||
|
cfg.ControlPass = "secret"
|
||||||
|
})
|
||||||
|
resp, raw := controlRequest(t, http.MethodPost, down.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`), "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusBadGateway {
|
||||||
|
t.Fatalf("train with FastAPI down status=%d body=%s, want 502", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
resp, raw = controlRequest(t, http.MethodGet, down.URL+"/api/train/job-1", "", nil, "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusBadGateway {
|
||||||
|
t.Fatalf("status with FastAPI down=%d body=%s, want 502", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, unconfigured := newTestServer(t, &stubML{}, nil)
|
||||||
|
resp, raw = controlRequest(t, http.MethodPost, unconfigured.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`), "control", "secret")
|
||||||
|
if resp.StatusCode != http.StatusServiceUnavailable {
|
||||||
|
t.Fatalf("train without control env status=%d body=%s, want 503", resp.StatusCode, raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
-2
@@ -5,8 +5,10 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -18,13 +20,20 @@ import (
|
|||||||
"pulsecast/api/internal/store"
|
"pulsecast/api/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MLClient is the FastAPI surface the handlers need. *ml.Client implements it;
|
// MLClient is the FastAPI prediction surface the data handlers need.
|
||||||
// tests use a stub.
|
// *ml.Client implements it; tests use a stub.
|
||||||
type MLClient interface {
|
type MLClient interface {
|
||||||
Predict(ctx context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error)
|
Predict(ctx context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error)
|
||||||
Simulate(ctx context.Context, req ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error)
|
Simulate(ctx context.Context, req ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TrainClient is the optional FastAPI training surface. Keeping it separate
|
||||||
|
// lets prediction-only stubs (and the cached fallback) work unchanged.
|
||||||
|
type TrainClient interface {
|
||||||
|
Train(ctx context.Context, contentType string, body io.Reader) (int, []byte, error)
|
||||||
|
TrainStatus(ctx context.Context, jobID string) (int, []byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
// Server holds process-wide state. One instance per pod.
|
// Server holds process-wide state. One instance per pod.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
cfg config.Config
|
cfg config.Config
|
||||||
@@ -76,6 +85,8 @@ func (s *Server) Handler() http.Handler {
|
|||||||
mux.HandleFunc("GET /api/staffing", s.handleStaffing)
|
mux.HandleFunc("GET /api/staffing", s.handleStaffing)
|
||||||
mux.HandleFunc("GET /api/scenario", s.handleScenarioGet)
|
mux.HandleFunc("GET /api/scenario", s.handleScenarioGet)
|
||||||
mux.HandleFunc("POST /api/scenario", s.requireControlAuth(s.handleScenarioPost))
|
mux.HandleFunc("POST /api/scenario", s.requireControlAuth(s.handleScenarioPost))
|
||||||
|
mux.HandleFunc("POST /api/train", s.requireControlAuth(s.handleTrain))
|
||||||
|
mux.HandleFunc("GET /api/train/{job_id}", s.requireControlAuth(s.handleTrainStatus))
|
||||||
mux.HandleFunc("GET /api/stream", s.handleStream)
|
mux.HandleFunc("GET /api/stream", s.handleStream)
|
||||||
|
|
||||||
return s.recoverer(s.withCORS(s.routeCheck(mux)))
|
return s.recoverer(s.withCORS(s.routeCheck(mux)))
|
||||||
@@ -91,12 +102,16 @@ var methodTable = map[string][]string{
|
|||||||
"/api/forecast": {"GET"},
|
"/api/forecast": {"GET"},
|
||||||
"/api/staffing": {"GET"},
|
"/api/staffing": {"GET"},
|
||||||
"/api/scenario": {"GET", "POST"},
|
"/api/scenario": {"GET", "POST"},
|
||||||
|
"/api/train": {"POST"},
|
||||||
"/api/stream": {"GET"},
|
"/api/stream": {"GET"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) routeCheck(next http.Handler) http.Handler {
|
func (s *Server) routeCheck(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
methods, ok := methodTable[r.URL.Path]
|
methods, ok := methodTable[r.URL.Path]
|
||||||
|
if !ok && isTrainJobPath(r.URL.Path) {
|
||||||
|
methods, ok = []string{"GET"}, true
|
||||||
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
writeError(w, http.StatusNotFound, "not_found", "route not found")
|
writeError(w, http.StatusNotFound, "not_found", "route not found")
|
||||||
return
|
return
|
||||||
@@ -110,6 +125,12 @@ func (s *Server) routeCheck(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isTrainJobPath matches exactly one job id after /api/train/.
|
||||||
|
func isTrainJobPath(path string) bool {
|
||||||
|
jobID, found := strings.CutPrefix(path, "/api/train/")
|
||||||
|
return found && jobID != "" && !strings.Contains(jobID, "/")
|
||||||
|
}
|
||||||
|
|
||||||
func joinMethods(methods []string) string {
|
func joinMethods(methods []string) string {
|
||||||
out := ""
|
out := ""
|
||||||
for i, method := range methods {
|
for i, method := range methods {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -18,10 +19,15 @@ import (
|
|||||||
// DefaultTimeout is the contract limit for a live prediction.
|
// DefaultTimeout is the contract limit for a live prediction.
|
||||||
const DefaultTimeout = 1500 * time.Millisecond
|
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.
|
// Client talks to the FastAPI ML service.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
baseURL string
|
baseURL string
|
||||||
http *http.Client
|
http *http.Client
|
||||||
|
control *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a client for baseURL (for example http://localhost:8000).
|
// 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{
|
return &Client{
|
||||||
baseURL: strings.TrimRight(baseURL, "/"),
|
baseURL: strings.TrimRight(baseURL, "/"),
|
||||||
http: &http.Client{Timeout: timeout},
|
http: &http.Client{Timeout: timeout},
|
||||||
|
control: &http.Client{Timeout: controlTimeout},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +64,15 @@ type SimulateRequest struct {
|
|||||||
Sensors string `json:"sensors"`
|
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
|
// Predict calls POST /predict and returns the forecast points. The contract
|
||||||
// response is {"points":[...]}; a bare array is accepted as well.
|
// response is {"points":[...]}; a bare array is accepted as well.
|
||||||
func (c *Client) Predict(ctx context.Context, req PredictRequest) ([]model.ForecastPoint, error) {
|
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
|
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 {
|
func (c *Client) post(ctx context.Context, path string, body, dst any) error {
|
||||||
raw, err := c.postRaw(ctx, path, body)
|
raw, err := c.postRaw(ctx, path, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package ml
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -125,3 +127,67 @@ func TestNewUsesDefaultTimeout(t *testing.T) {
|
|||||||
t.Fatalf("timeout=%s, want %s", client.http.Timeout, DefaultTimeout)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func weekPoints(t *testing.T, values map[int]float64) []model.ForecastPoint {
|
func weekPoints(t *testing.T, values map[int]float64) []model.ForecastPoint {
|
||||||
|
t.Helper()
|
||||||
|
return weekPointsForDay(t, -1, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
// weekPointsForDay builds seven days of store-hour points. Hour overrides apply
|
||||||
|
// only to day (0 = Monday), or to every day when day is negative.
|
||||||
|
func weekPointsForDay(t *testing.T, day int, values map[int]float64) []model.ForecastPoint {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
loc, err := time.LoadLocation(model.StoreHours.TZ)
|
loc, err := time.LoadLocation(model.StoreHours.TZ)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -16,14 +23,14 @@ func weekPoints(t *testing.T, values map[int]float64) []model.ForecastPoint {
|
|||||||
}
|
}
|
||||||
start := time.Date(2026, 11, 16, model.StoreHours.Open, 0, 0, 0, loc)
|
start := time.Date(2026, 11, 16, model.StoreHours.Open, 0, 0, 0, loc)
|
||||||
var points []model.ForecastPoint
|
var points []model.ForecastPoint
|
||||||
for day := 0; day < 7; day++ {
|
for d := 0; d < 7; d++ {
|
||||||
for hour := 0; hour < model.StoreHours.Close-model.StoreHours.Open; hour++ {
|
for hour := 0; hour < model.StoreHours.Close-model.StoreHours.Open; hour++ {
|
||||||
v := 25.0
|
v := 25.0
|
||||||
if override, ok := values[hour]; ok {
|
if override, ok := values[hour]; ok && (day < 0 || day == d) {
|
||||||
v = override
|
v = override
|
||||||
}
|
}
|
||||||
points = append(points, model.ForecastPoint{
|
points = append(points, model.ForecastPoint{
|
||||||
T: start.AddDate(0, 0, day).Add(time.Duration(hour) * time.Hour).UTC(),
|
T: start.AddDate(0, 0, d).Add(time.Duration(hour) * time.Hour).UTC(),
|
||||||
V: v,
|
V: v,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -75,7 +82,8 @@ func TestBuildStandardWeek(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildShiftBlocksAndPeak(t *testing.T) {
|
func TestBuildShiftBlocksAndPeak(t *testing.T) {
|
||||||
points := weekPoints(t, map[int]float64{9: 1000}) // last store hour is a peak
|
// One 19:00 peak on Monday only; the rest of the week stays flat at 25.
|
||||||
|
points := weekPointsForDay(t, 0, map[int]float64{9: 1000})
|
||||||
plan, err := Build("hn-bahnhof-01", "base", "standard", points, model.ContextData{})
|
plan, err := Build("hn-bahnhof-01", "base", "standard", points, model.ContextData{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Build: %v", err)
|
t.Fatalf("Build: %v", err)
|
||||||
|
|||||||
Reference in new issue
Block a user