engine refactor
This commit is contained in:
parent
9e663db9dc
commit
81b8e763bd
37 changed files with 3532 additions and 1639 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"predictor-refactored/internal/api/httpjson"
|
||||
"predictor-refactored/internal/datasets"
|
||||
"predictor-refactored/internal/elevation"
|
||||
"predictor-refactored/internal/engine"
|
||||
|
|
@ -46,85 +47,109 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := validateRequest(req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
field := h.mgr.Active()
|
||||
if field == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "no dataset loaded, service is starting up")
|
||||
resp, err := Run(h.mgr, h.elev, req)
|
||||
if err != nil {
|
||||
if perr, ok := err.(*PredictionError); ok {
|
||||
writeError(w, perr.Status, perr.Description)
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
h.metrics.Prediction("v2", resp.CompletedAt.Sub(resp.StartedAt), nil)
|
||||
h.log.Info("v2 prediction complete",
|
||||
zap.Int("stages", len(resp.Stages)),
|
||||
zap.Duration("elapsed", resp.CompletedAt.Sub(resp.StartedAt)))
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// PredictionError carries an HTTP status alongside the message so async
|
||||
// callers can map the failure back to a useful HTTP response.
|
||||
type PredictionError struct {
|
||||
Status int
|
||||
Description string
|
||||
}
|
||||
|
||||
func (e *PredictionError) Error() string { return e.Description }
|
||||
|
||||
// Run executes a PredictionRequest against the manager's active wind field.
|
||||
// Shared between the sync /api/v2/prediction handler and the async
|
||||
// /api/v1/predictions worker.
|
||||
func Run(mgr *datasets.Manager, elev *elevation.Dataset, req PredictionRequest) (*PredictionResponse, error) {
|
||||
field := mgr.Active()
|
||||
if field == nil {
|
||||
return nil, &PredictionError{Status: http.StatusServiceUnavailable, Description: "no dataset loaded, service is starting up"}
|
||||
}
|
||||
|
||||
// Normalize longitude to [0, 360) for internal use.
|
||||
lng := req.Launch.Longitude
|
||||
if lng < 0 {
|
||||
lng += 360
|
||||
}
|
||||
|
||||
warnings := &engine.Warnings{}
|
||||
var terrain engine.TerrainProvider
|
||||
if h.elev != nil {
|
||||
terrain = h.elev
|
||||
events := engine.NewEventSink()
|
||||
deps := engine.BuildDeps{Wind: field, Events: events}
|
||||
if elev != nil {
|
||||
deps.Terrain = elev
|
||||
}
|
||||
|
||||
prof, err := buildProfile(req, field, terrain, warnings)
|
||||
prof, err := buildProfile(req, deps)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
return nil, &PredictionError{Status: http.StatusBadRequest, Description: err.Error()}
|
||||
}
|
||||
|
||||
started := time.Now().UTC()
|
||||
results := prof.Run(float64(req.Launch.Time.Unix()), engine.State{
|
||||
Lat: req.Launch.Latitude,
|
||||
Lng: lng,
|
||||
Altitude: req.Launch.Altitude,
|
||||
})
|
||||
Lat: req.Launch.Latitude, Lng: lng, Altitude: req.Launch.Altitude,
|
||||
}, events)
|
||||
completed := time.Now().UTC()
|
||||
h.metrics.Prediction("v2", completed.Sub(started), nil)
|
||||
|
||||
resp := PredictionResponse{
|
||||
resp := &PredictionResponse{
|
||||
Stages: make([]StageResult, 0, len(results)),
|
||||
Events: events.Snapshot(),
|
||||
StartedAt: started,
|
||||
CompletedAt: completed,
|
||||
Dataset: DatasetInfo{
|
||||
Source: field.Source(),
|
||||
Epoch: field.Epoch(),
|
||||
},
|
||||
Dataset: DatasetInfo{Source: field.Source(), Epoch: field.Epoch()},
|
||||
}
|
||||
for _, r := range results {
|
||||
stage := StageResult{
|
||||
Name: r.Propagator,
|
||||
Outcome: outcomeString(r.Outcome),
|
||||
}
|
||||
if r.Constraint != nil {
|
||||
stage.Constraint = r.Constraint.Name()
|
||||
}
|
||||
stage.Trajectory = make([]TrajectoryPoint, len(r.Points))
|
||||
for i, pt := range r.Points {
|
||||
ptLng := pt.Lng
|
||||
if ptLng > 180 {
|
||||
ptLng -= 360
|
||||
}
|
||||
stage.Trajectory[i] = TrajectoryPoint{
|
||||
Time: time.Unix(int64(pt.Time), 0).UTC(),
|
||||
Latitude: pt.Lat,
|
||||
Longitude: ptLng,
|
||||
Altitude: pt.Altitude,
|
||||
}
|
||||
}
|
||||
resp.Stages = append(resp.Stages, stage)
|
||||
}
|
||||
if warns := warnings.ToMap(); len(warns) > 0 {
|
||||
resp.Warnings = warns
|
||||
resp.Stages = append(resp.Stages, toStageResult(r))
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
h.log.Info("v2 prediction complete",
|
||||
zap.Int("stages", len(results)),
|
||||
zap.Duration("elapsed", completed.Sub(started)))
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
func toStageResult(r engine.Result) StageResult {
|
||||
stage := StageResult{
|
||||
Name: r.Propagator,
|
||||
Outcome: r.Outcome.String(),
|
||||
Events: r.Events,
|
||||
}
|
||||
if r.Constraint != nil {
|
||||
stage.Constraint = r.ConstraintName
|
||||
stage.Termination = &TerminationInfo{
|
||||
ViolationTime: time.Unix(int64(r.ViolationTime), 0).UTC(),
|
||||
ViolationState: r.ViolationState,
|
||||
RefinedTime: time.Unix(int64(r.RefinedTime), 0).UTC(),
|
||||
RefinedState: r.RefinedState,
|
||||
}
|
||||
}
|
||||
stage.Trajectory = make([]TrajectoryPoint, len(r.Points))
|
||||
for i, pt := range r.Points {
|
||||
ptLng := pt.Lng
|
||||
if ptLng > 180 {
|
||||
ptLng -= 360
|
||||
}
|
||||
stage.Trajectory[i] = TrajectoryPoint{
|
||||
Time: time.Unix(int64(pt.Time), 0).UTC(),
|
||||
Latitude: pt.Lat,
|
||||
Longitude: ptLng,
|
||||
Altitude: pt.Altitude,
|
||||
}
|
||||
}
|
||||
return stage
|
||||
}
|
||||
|
||||
func validateRequest(req PredictionRequest) error {
|
||||
|
|
@ -148,26 +173,5 @@ func validateRequest(req PredictionRequest) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func outcomeString(o engine.Outcome) string {
|
||||
switch o {
|
||||
case engine.OutcomeStopped:
|
||||
return "stopped"
|
||||
case engine.OutcomeFallback:
|
||||
return "fallback"
|
||||
default:
|
||||
return "continued"
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, description string) {
|
||||
writeJSON(w, status, ErrorResponse{Error: ErrorBody{
|
||||
Type: http.StatusText(status),
|
||||
Description: description,
|
||||
}})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, body any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
}
|
||||
var writeJSON = httpjson.Write
|
||||
var writeError = httpjson.Error
|
||||
|
|
|
|||
|
|
@ -4,14 +4,11 @@ import (
|
|||
"fmt"
|
||||
|
||||
"predictor-refactored/internal/engine"
|
||||
"predictor-refactored/internal/weather"
|
||||
)
|
||||
|
||||
// buildProfile translates a PredictionRequest into an engine.Profile.
|
||||
//
|
||||
// elev may be nil when no terrain dataset is loaded; TerrainContact constraints
|
||||
// will return an error in that case.
|
||||
func buildProfile(req PredictionRequest, field weather.WindField, elev engine.TerrainProvider, warnings *engine.Warnings) (engine.Profile, error) {
|
||||
// buildProfile translates a PredictionRequest into an engine.Profile via
|
||||
// the engine registry.
|
||||
func buildProfile(req PredictionRequest, deps engine.BuildDeps) (engine.Profile, error) {
|
||||
if len(req.Profile) == 0 {
|
||||
return engine.Profile{}, fmt.Errorf("profile must contain at least one stage")
|
||||
}
|
||||
|
|
@ -37,24 +34,27 @@ func buildProfile(req PredictionRequest, field weather.WindField, elev engine.Te
|
|||
|
||||
props := make([]*engine.Propagator, len(req.Profile))
|
||||
for i, stage := range req.Profile {
|
||||
model, err := buildModel(stage.Model, field, warnings)
|
||||
if err != nil {
|
||||
return engine.Profile{}, fmt.Errorf("stage %q: %w", stage.Name, err)
|
||||
if stage.Name == "" {
|
||||
return engine.Profile{}, fmt.Errorf("stage %d: name is required", i)
|
||||
}
|
||||
constraints, err := buildConstraints(stage.Constraints, elev)
|
||||
built, err := engine.BuildModel(stage.Model, deps)
|
||||
if err != nil {
|
||||
return engine.Profile{}, fmt.Errorf("stage %q model: %w", stage.Name, err)
|
||||
}
|
||||
constraints, err := buildConstraintList(stage.Constraints, deps)
|
||||
if err != nil {
|
||||
return engine.Profile{}, fmt.Errorf("stage %q: %w", stage.Name, err)
|
||||
}
|
||||
props[i] = &engine.Propagator{
|
||||
Name: stage.Name,
|
||||
Step: step,
|
||||
Model: model,
|
||||
Model: built.Model,
|
||||
BuildModel: built.Build,
|
||||
Constraints: constraints,
|
||||
Tolerance: tol,
|
||||
}
|
||||
}
|
||||
|
||||
// Wire fallbacks once all stages exist.
|
||||
for i, stage := range req.Profile {
|
||||
if stage.FallbackIndex == nil {
|
||||
continue
|
||||
|
|
@ -66,80 +66,22 @@ func buildProfile(req PredictionRequest, field weather.WindField, elev engine.Te
|
|||
props[i].Fallback = props[idx]
|
||||
}
|
||||
|
||||
return engine.Profile{Stages: props, Direction: dir}, nil
|
||||
}
|
||||
|
||||
func buildModel(spec ModelSpec, field weather.WindField, warnings *engine.Warnings) (engine.Model, error) {
|
||||
var base engine.Model
|
||||
switch spec.Type {
|
||||
case "constant_rate":
|
||||
base = engine.ConstantRate(spec.Rate)
|
||||
case "parachute_descent":
|
||||
if spec.SeaLevelRate <= 0 {
|
||||
return nil, fmt.Errorf("parachute_descent requires positive sea_level_rate")
|
||||
}
|
||||
base = engine.ParachuteDescent(spec.SeaLevelRate)
|
||||
case "piecewise":
|
||||
segs := make([]engine.RateSegment, len(spec.Segments))
|
||||
for i, s := range spec.Segments {
|
||||
segs[i] = engine.RateSegment{Until: s.Until, Rate: s.Rate}
|
||||
}
|
||||
base = engine.Piecewise(segs)
|
||||
case "wind":
|
||||
if field == nil {
|
||||
return nil, fmt.Errorf("wind model requires a loaded dataset")
|
||||
}
|
||||
return engine.WindTransport(field, warnings), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown model type %q", spec.Type)
|
||||
globals, err := buildConstraintList(req.Globals, deps)
|
||||
if err != nil {
|
||||
return engine.Profile{}, fmt.Errorf("globals: %w", err)
|
||||
}
|
||||
|
||||
if spec.IncludeWind {
|
||||
if field == nil {
|
||||
return nil, fmt.Errorf("include_wind requires a loaded dataset")
|
||||
}
|
||||
return engine.Sum(base, engine.WindTransport(field, warnings)), nil
|
||||
}
|
||||
return base, nil
|
||||
return engine.Profile{Stages: props, Direction: dir, Globals: globals}, nil
|
||||
}
|
||||
|
||||
func buildConstraints(specs []ConstraintSpec, elev engine.TerrainProvider) ([]engine.Constraint, error) {
|
||||
func buildConstraintList(specs []engine.ConstraintSpec, deps engine.BuildDeps) ([]engine.Constraint, error) {
|
||||
out := make([]engine.Constraint, 0, len(specs))
|
||||
for _, spec := range specs {
|
||||
action, err := parseAction(spec.Action)
|
||||
for i, spec := range specs {
|
||||
c, err := engine.BuildConstraint(spec, deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var c engine.Constraint
|
||||
switch spec.Type {
|
||||
case "max_altitude":
|
||||
c = engine.MaxAltitude{Limit: spec.Limit, On: action}
|
||||
case "min_altitude":
|
||||
c = engine.MinAltitude{Limit: spec.Limit, On: action}
|
||||
case "max_time":
|
||||
c = engine.MaxTime{Limit: spec.Limit, On: action}
|
||||
case "terrain_contact":
|
||||
if elev == nil {
|
||||
return nil, fmt.Errorf("terrain_contact requires an elevation dataset")
|
||||
}
|
||||
c = engine.TerrainContact{Provider: elev, On: action}
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown constraint type %q", spec.Type)
|
||||
return nil, fmt.Errorf("constraint[%d]: %w", i, err)
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseAction(s string) (engine.Action, error) {
|
||||
switch s {
|
||||
case "", "stop":
|
||||
return engine.ActionStop, nil
|
||||
case "fallback":
|
||||
return engine.ActionFallback, nil
|
||||
case "clip":
|
||||
return engine.ActionClip, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unknown constraint action %q", s)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,25 @@
|
|||
// Package v2 implements the new primary prediction endpoint, which accepts a
|
||||
// user-defined profile (chain of propagators with optional constraints) and
|
||||
// returns the resulting trajectory.
|
||||
// Package v2 implements the profile-driven prediction endpoint.
|
||||
//
|
||||
// Endpoint: POST /api/v2/prediction
|
||||
//
|
||||
// The request schema is built on the engine package's ConstraintSpec and
|
||||
// ModelSpec, so adding new model or constraint types in the engine requires
|
||||
// no changes here — they become available automatically via the registry.
|
||||
package v2
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
// PredictionRequest is the request body for POST /api/v2/prediction.
|
||||
"predictor-refactored/internal/engine"
|
||||
)
|
||||
|
||||
// PredictionRequest is the body of POST /api/v2/prediction.
|
||||
type PredictionRequest struct {
|
||||
Launch Launch `json:"launch"`
|
||||
Profile []Stage `json:"profile"`
|
||||
Options Options `json:"options,omitempty"`
|
||||
Direction string `json:"direction,omitempty"` // "forward" (default) or "reverse"
|
||||
Launch Launch `json:"launch"`
|
||||
Profile []StageSpec `json:"profile"`
|
||||
Globals []engine.ConstraintSpec `json:"globals,omitempty"`
|
||||
Options Options `json:"options,omitempty"`
|
||||
Direction string `json:"direction,omitempty"` // "forward" (default) or "reverse"
|
||||
}
|
||||
|
||||
// Launch is the initial state of the balloon (or, for reverse predictions,
|
||||
|
|
@ -24,68 +31,47 @@ type Launch struct {
|
|||
Altitude float64 `json:"altitude"`
|
||||
}
|
||||
|
||||
// Stage is one entry in the propagator chain.
|
||||
type Stage struct {
|
||||
Name string `json:"name"`
|
||||
Model ModelSpec `json:"model"`
|
||||
Constraints []ConstraintSpec `json:"constraints,omitempty"`
|
||||
// FallbackIndex, when set, points to another stage in the same profile to
|
||||
// transfer to on ActionFallback constraints. Optional.
|
||||
// StageSpec is one entry in the propagator chain.
|
||||
type StageSpec struct {
|
||||
Name string `json:"name"`
|
||||
Model engine.ModelSpec `json:"model"`
|
||||
Constraints []engine.ConstraintSpec `json:"constraints,omitempty"`
|
||||
// FallbackIndex, when set, points to another stage in the same profile
|
||||
// to transfer to on ActionFallback constraints.
|
||||
FallbackIndex *int `json:"fallback_index,omitempty"`
|
||||
}
|
||||
|
||||
// ModelSpec describes the per-stage propagation model.
|
||||
type ModelSpec struct {
|
||||
// Type selects the model: "constant_rate", "parachute_descent", "piecewise", "wind".
|
||||
Type string `json:"type"`
|
||||
// Rate (m/s) for constant_rate.
|
||||
Rate float64 `json:"rate,omitempty"`
|
||||
// SeaLevelRate (m/s, positive) for parachute_descent.
|
||||
SeaLevelRate float64 `json:"sea_level_rate,omitempty"`
|
||||
// Segments for piecewise.
|
||||
Segments []PiecewiseSegment `json:"segments,omitempty"`
|
||||
// IncludeWind sums a WindTransport model into the resulting derivative,
|
||||
// allowing the same stage to model both vertical motion and wind drift.
|
||||
IncludeWind bool `json:"include_wind"`
|
||||
}
|
||||
|
||||
// PiecewiseSegment is one entry in a piecewise rate schedule.
|
||||
type PiecewiseSegment struct {
|
||||
Until float64 `json:"until"` // UNIX seconds; segment applies for t < Until
|
||||
Rate float64 `json:"rate"` // m/s
|
||||
}
|
||||
|
||||
// ConstraintSpec describes one constraint attached to a stage.
|
||||
type ConstraintSpec struct {
|
||||
// Type: "max_altitude", "min_altitude", "max_time", "terrain_contact".
|
||||
Type string `json:"type"`
|
||||
// Limit is interpreted per Type: metres for altitude, UNIX seconds for time.
|
||||
Limit float64 `json:"limit,omitempty"`
|
||||
// Action: "stop" (default), "fallback", "clip".
|
||||
Action string `json:"action,omitempty"`
|
||||
}
|
||||
|
||||
// Options tweaks the integrator behaviour.
|
||||
// Options tweaks integrator behaviour.
|
||||
type Options struct {
|
||||
StepSeconds float64 `json:"step_seconds,omitempty"`
|
||||
Tolerance float64 `json:"tolerance,omitempty"`
|
||||
}
|
||||
|
||||
// PredictionResponse is the response body for POST /api/v2/prediction.
|
||||
// PredictionResponse is the body of a successful POST response.
|
||||
type PredictionResponse struct {
|
||||
Stages []StageResult `json:"stages"`
|
||||
Warnings map[string]any `json:"warnings,omitempty"`
|
||||
Dataset DatasetInfo `json:"dataset"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
CompletedAt time.Time `json:"completed_at"`
|
||||
Stages []StageResult `json:"stages"`
|
||||
Events []engine.EventSummary `json:"events,omitempty"`
|
||||
Dataset DatasetInfo `json:"dataset"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
CompletedAt time.Time `json:"completed_at"`
|
||||
}
|
||||
|
||||
// StageResult is the outcome of one stage.
|
||||
type StageResult struct {
|
||||
Name string `json:"name"`
|
||||
Outcome string `json:"outcome"` // "stopped" | "fallback" | "continued"
|
||||
Constraint string `json:"constraint,omitempty"`
|
||||
Trajectory []TrajectoryPoint `json:"trajectory"`
|
||||
Name string `json:"name"`
|
||||
Outcome string `json:"outcome"`
|
||||
Constraint string `json:"constraint,omitempty"`
|
||||
Termination *TerminationInfo `json:"termination,omitempty"`
|
||||
Events []engine.EventSummary `json:"events,omitempty"`
|
||||
Trajectory []TrajectoryPoint `json:"trajectory"`
|
||||
}
|
||||
|
||||
// TerminationInfo exposes the violation+refinement detail from the engine.
|
||||
type TerminationInfo struct {
|
||||
ViolationTime time.Time `json:"violation_time"`
|
||||
ViolationState engine.State `json:"violation_state"`
|
||||
RefinedTime time.Time `json:"refined_time"`
|
||||
RefinedState engine.State `json:"refined_state"`
|
||||
}
|
||||
|
||||
// TrajectoryPoint is one sampled point of the trajectory.
|
||||
|
|
@ -96,13 +82,13 @@ type TrajectoryPoint struct {
|
|||
Altitude float64 `json:"altitude"`
|
||||
}
|
||||
|
||||
// DatasetInfo identifies the dataset the prediction was computed against.
|
||||
// DatasetInfo identifies the wind dataset used.
|
||||
type DatasetInfo struct {
|
||||
Source string `json:"source"`
|
||||
Epoch time.Time `json:"epoch"`
|
||||
}
|
||||
|
||||
// ErrorResponse is the JSON error shape used by both v2 and admin endpoints.
|
||||
// ErrorResponse is the JSON error shape.
|
||||
type ErrorResponse struct {
|
||||
Error ErrorBody `json:"error"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue