feat: proxy PreZero priority endpoints to FastAPI
ci / check (push) Successful in 58s
ci / check (pull_request) Successful in 55s

This commit is contained in:
Noel Sabia committed 2026-11-14 17:50:53 +01:00
1 parent a0ad60ec53
commit 7e9c2d9663
5 files changed
+564 -11

No files matched your search

+40 -11
View File
@@ -34,11 +34,20 @@ type TrainClient interface {
TrainStatus(ctx context.Context, jobID string) (int, []byte, error)
}
// PreZeroClient is the optional FastAPI PreZero read-only surface. Like
// TrainClient it is separate so prediction-only stubs keep working.
type PreZeroClient interface {
PreZeroSummary(ctx context.Context) (int, []byte, error)
PreZeroPriority(ctx context.Context, priority string, limit, offset int) (int, []byte, error)
PreZeroContainer(ctx context.Context, locationID string) (int, []byte, error)
}
// Server holds process-wide state. One instance per pod.
type Server struct {
cfg config.Config
cache *cache.Cache
ml MLClient
prezero PreZeroClient
hub *sse.Hub
scenario *scenario.State
loc *time.Location
@@ -61,10 +70,15 @@ func New(cfg config.Config, snapshots *cache.Cache, client MLClient, hub *sse.Hu
if cfg.MLTimeout <= 0 {
cfg.MLTimeout = ml.DefaultTimeout
}
var prezero PreZeroClient
if pc, ok := client.(PreZeroClient); ok {
prezero = pc
}
return &Server{
cfg: cfg,
cache: snapshots,
ml: client,
prezero: prezero,
hub: hub,
scenario: scenario.New(),
loc: loc,
@@ -89,23 +103,28 @@ func (s *Server) Handler() http.Handler {
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/prezero/summary", s.handlePreZeroSummary)
mux.HandleFunc("GET /api/prezero/priority", s.handlePreZeroPriority)
mux.HandleFunc("GET /api/prezero/containers/{location_id}", s.handlePreZeroContainer)
mux.HandleFunc("GET /api/stream", s.handleStream)
return s.recoverer(s.withCORS(s.routeCheck(mux)))
}
var methodTable = map[string][]string{
"/api/healthz": {"GET"},
"/api/readyz": {"GET"},
"/api/meta": {"GET"},
"/api/sensors": {"GET"},
"/api/series": {"GET"},
"/api/context": {"GET"},
"/api/forecast": {"GET"},
"/api/staffing": {"GET"},
"/api/scenario": {"GET", "POST"},
"/api/train": {"POST"},
"/api/stream": {"GET"},
"/api/healthz": {"GET"},
"/api/readyz": {"GET"},
"/api/meta": {"GET"},
"/api/sensors": {"GET"},
"/api/series": {"GET"},
"/api/context": {"GET"},
"/api/forecast": {"GET"},
"/api/staffing": {"GET"},
"/api/scenario": {"GET", "POST"},
"/api/train": {"POST"},
"/api/prezero/summary": {"GET"},
"/api/prezero/priority": {"GET"},
"/api/stream": {"GET"},
}
// routeCheck keeps unknown routes and methods in the JSON error envelope;
@@ -116,6 +135,9 @@ func (s *Server) routeCheck(next http.Handler) http.Handler {
if !ok && isTrainJobPath(r.URL.Path) {
methods, ok = []string{"GET"}, true
}
if !ok && isPreZeroContainerPath(r.URL.Path) {
methods, ok = []string{"GET"}, true
}
if !ok {
writeError(w, http.StatusNotFound, "not_found", "route not found")
return
@@ -135,6 +157,13 @@ func isTrainJobPath(path string) bool {
return found && jobID != "" && !strings.Contains(jobID, "/")
}
// isPreZeroContainerPath matches exactly one location id after
// /api/prezero/containers/.
func isPreZeroContainerPath(path string) bool {
locationID, found := strings.CutPrefix(path, "/api/prezero/containers/")
return found && locationID != "" && !strings.Contains(locationID, "/")
}
func joinMethods(methods []string) string {
out := ""
for i, method := range methods {