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
+23
-2
@@ -5,8 +5,10 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -18,13 +20,20 @@ import (
|
||||
"pulsecast/api/internal/store"
|
||||
)
|
||||
|
||||
// MLClient is the FastAPI surface the handlers need. *ml.Client implements it;
|
||||
// tests use a stub.
|
||||
// MLClient is the FastAPI prediction surface the data handlers need.
|
||||
// *ml.Client implements it; tests use a stub.
|
||||
type MLClient interface {
|
||||
Predict(ctx context.Context, req ml.PredictRequest) ([]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.
|
||||
type Server struct {
|
||||
cfg config.Config
|
||||
@@ -76,6 +85,8 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /api/staffing", s.handleStaffing)
|
||||
mux.HandleFunc("GET /api/scenario", s.handleScenarioGet)
|
||||
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)
|
||||
|
||||
return s.recoverer(s.withCORS(s.routeCheck(mux)))
|
||||
@@ -91,12 +102,16 @@ var methodTable = map[string][]string{
|
||||
"/api/forecast": {"GET"},
|
||||
"/api/staffing": {"GET"},
|
||||
"/api/scenario": {"GET", "POST"},
|
||||
"/api/train": {"POST"},
|
||||
"/api/stream": {"GET"},
|
||||
}
|
||||
|
||||
func (s *Server) routeCheck(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
methods, ok := methodTable[r.URL.Path]
|
||||
if !ok && isTrainJobPath(r.URL.Path) {
|
||||
methods, ok = []string{"GET"}, true
|
||||
}
|
||||
if !ok {
|
||||
writeError(w, http.StatusNotFound, "not_found", "route not found")
|
||||
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 {
|
||||
out := ""
|
||||
for i, method := range methods {
|
||||
|
||||
Reference in new issue
Block a user