252 lines
7.3 KiB
Go
252 lines
7.3 KiB
Go
// Package tawhiri implements the legacy Tawhiri-compatible HTTP endpoint
|
|
// (GET /api/v1/prediction). The request/response shapes match the original
|
|
// Cambridge University Spaceflight predictor for drop-in compatibility.
|
|
//
|
|
// Internally the handler builds an engine.Profile from query parameters and
|
|
// dispatches it through the same engine path as the new v2 endpoint.
|
|
package tawhiri
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"predictor-refactored/internal/datasets"
|
|
"predictor-refactored/internal/elevation"
|
|
"predictor-refactored/internal/engine"
|
|
"predictor-refactored/internal/metrics"
|
|
api "predictor-refactored/pkg/rest"
|
|
)
|
|
|
|
// Handler implements api.Handler (the ogen-generated interface for
|
|
// performPrediction and readinessCheck).
|
|
type Handler struct {
|
|
mgr *datasets.Manager
|
|
elev *elevation.Dataset
|
|
metrics metrics.Sink
|
|
log *zap.Logger
|
|
}
|
|
|
|
// New wires a Handler.
|
|
func New(mgr *datasets.Manager, elev *elevation.Dataset, sink metrics.Sink, log *zap.Logger) *Handler {
|
|
if log == nil {
|
|
log = zap.NewNop()
|
|
}
|
|
if sink == nil {
|
|
sink = metrics.Noop()
|
|
}
|
|
return &Handler{mgr: mgr, elev: elev, metrics: sink, log: log}
|
|
}
|
|
|
|
// Compile-time check that Handler satisfies api.Handler.
|
|
var _ api.Handler = (*Handler)(nil)
|
|
|
|
// PerformPrediction runs the Tawhiri-style prediction.
|
|
func (h *Handler) PerformPrediction(ctx context.Context, params api.PerformPredictionParams) (*api.PredictionResponse, error) {
|
|
field := h.mgr.Active()
|
|
if field == nil {
|
|
return nil, newError(http.StatusServiceUnavailable, "no dataset loaded, service is starting up")
|
|
}
|
|
|
|
// Parameters with Tawhiri defaults.
|
|
profileKind := "standard_profile"
|
|
if v, ok := params.Profile.Get(); ok {
|
|
profileKind = string(v)
|
|
}
|
|
ascentRate := 5.0
|
|
if v, ok := params.AscentRate.Get(); ok {
|
|
ascentRate = v
|
|
}
|
|
burstAltitude := 28000.0
|
|
if v, ok := params.BurstAltitude.Get(); ok {
|
|
burstAltitude = v
|
|
}
|
|
descentRate := 5.0
|
|
if v, ok := params.DescentRate.Get(); ok {
|
|
descentRate = v
|
|
}
|
|
launchAlt := 0.0
|
|
if v, ok := params.LaunchAltitude.Get(); ok {
|
|
launchAlt = v
|
|
}
|
|
|
|
lng := params.LaunchLongitude
|
|
if lng < 0 {
|
|
lng += 360
|
|
}
|
|
|
|
launchTime := float64(params.LaunchDatetime.Unix())
|
|
warnings := &engine.Warnings{}
|
|
|
|
// Build the profile.
|
|
var stageNames []string
|
|
var prof engine.Profile
|
|
switch profileKind {
|
|
case "standard_profile":
|
|
stageNames = []string{"ascent", "descent"}
|
|
prof = engine.Profile{
|
|
Direction: engine.Forward,
|
|
Stages: []*engine.Propagator{
|
|
{
|
|
Name: "ascent",
|
|
Step: 60,
|
|
Model: engine.Sum(
|
|
engine.ConstantRate(ascentRate),
|
|
engine.WindTransport(field, warnings),
|
|
),
|
|
Constraints: []engine.Constraint{engine.MaxAltitude{Limit: burstAltitude, On: engine.ActionStop}},
|
|
},
|
|
{
|
|
Name: "descent",
|
|
Step: 60,
|
|
Model: engine.Sum(
|
|
engine.ParachuteDescent(descentRate),
|
|
engine.WindTransport(field, warnings),
|
|
),
|
|
Constraints: descentConstraints(h.elev),
|
|
},
|
|
},
|
|
}
|
|
case "float_profile":
|
|
floatAlt := 25000.0
|
|
if v, ok := params.FloatAltitude.Get(); ok {
|
|
floatAlt = v
|
|
}
|
|
stopTime := params.LaunchDatetime.Add(24 * time.Hour)
|
|
if v, ok := params.StopDatetime.Get(); ok {
|
|
stopTime = v
|
|
}
|
|
stageNames = []string{"ascent", "float"}
|
|
prof = engine.Profile{
|
|
Direction: engine.Forward,
|
|
Stages: []*engine.Propagator{
|
|
{
|
|
Name: "ascent",
|
|
Step: 60,
|
|
Model: engine.Sum(
|
|
engine.ConstantRate(ascentRate),
|
|
engine.WindTransport(field, warnings),
|
|
),
|
|
Constraints: []engine.Constraint{engine.MaxAltitude{Limit: floatAlt, On: engine.ActionStop}},
|
|
},
|
|
{
|
|
Name: "float",
|
|
Step: 60,
|
|
Model: engine.WindTransport(field, warnings),
|
|
Constraints: []engine.Constraint{engine.MaxTime{Limit: float64(stopTime.Unix()), On: engine.ActionStop}},
|
|
},
|
|
},
|
|
}
|
|
default:
|
|
return nil, newError(http.StatusBadRequest, "unknown profile: "+profileKind)
|
|
}
|
|
|
|
started := time.Now().UTC()
|
|
results := prof.Run(launchTime, engine.State{Lat: params.LaunchLatitude, Lng: lng, Altitude: launchAlt})
|
|
completed := time.Now().UTC()
|
|
h.metrics.Prediction(profileKind, completed.Sub(started), nil)
|
|
|
|
resp := &api.PredictionResponse{
|
|
Metadata: api.PredictionResponseMetadata{
|
|
StartDatetime: started,
|
|
CompleteDatetime: completed,
|
|
},
|
|
}
|
|
|
|
for i, r := range results {
|
|
stageName := "ascent"
|
|
if i < len(stageNames) {
|
|
stageName = stageNames[i]
|
|
}
|
|
stageEnum := api.PredictionResponsePredictionItemStageAscent
|
|
switch stageName {
|
|
case "descent":
|
|
stageEnum = api.PredictionResponsePredictionItemStageDescent
|
|
case "float":
|
|
stageEnum = api.PredictionResponsePredictionItemStageFloat
|
|
}
|
|
traj := make([]api.PredictionResponsePredictionItemTrajectoryItem, 0, len(r.Points))
|
|
for _, pt := range r.Points {
|
|
ptLng := pt.Lng
|
|
if ptLng > 180 {
|
|
ptLng -= 360
|
|
}
|
|
traj = append(traj, api.PredictionResponsePredictionItemTrajectoryItem{
|
|
Datetime: time.Unix(int64(pt.Time), 0).UTC(),
|
|
Latitude: pt.Lat,
|
|
Longitude: ptLng,
|
|
Altitude: pt.Altitude,
|
|
})
|
|
}
|
|
resp.Prediction = append(resp.Prediction, api.PredictionResponsePredictionItem{
|
|
Stage: stageEnum,
|
|
Trajectory: traj,
|
|
})
|
|
}
|
|
|
|
resp.Request = api.NewOptPredictionResponseRequest(api.PredictionResponseRequest{
|
|
Dataset: api.NewOptString(field.Epoch().Format("2006-01-02T15:04:05Z")),
|
|
LaunchLatitude: api.NewOptFloat64(params.LaunchLatitude),
|
|
LaunchLongitude: api.NewOptFloat64(params.LaunchLongitude),
|
|
LaunchDatetime: api.NewOptString(params.LaunchDatetime.Format(time.RFC3339)),
|
|
LaunchAltitude: params.LaunchAltitude,
|
|
})
|
|
|
|
if warns := warnings.ToMap(); len(warns) > 0 {
|
|
resp.Warnings = api.NewOptPredictionResponseWarnings(api.PredictionResponseWarnings{})
|
|
}
|
|
|
|
h.log.Info("prediction complete",
|
|
zap.String("profile", profileKind),
|
|
zap.Int("stages", len(results)),
|
|
zap.Duration("elapsed", completed.Sub(started)))
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
// descentConstraints returns the descent termination set: TerrainContact if an
|
|
// elevation dataset is loaded, MinAltitude(0) otherwise.
|
|
func descentConstraints(elev *elevation.Dataset) []engine.Constraint {
|
|
if elev != nil {
|
|
return []engine.Constraint{engine.TerrainContact{Provider: elev, On: engine.ActionStop}}
|
|
}
|
|
return []engine.Constraint{engine.MinAltitude{Limit: 0, On: engine.ActionStop}}
|
|
}
|
|
|
|
// ReadinessCheck reports whether a dataset is currently loaded.
|
|
func (h *Handler) ReadinessCheck(_ context.Context) (*api.ReadinessResponse, error) {
|
|
resp := &api.ReadinessResponse{}
|
|
if field := h.mgr.Active(); field != nil {
|
|
resp.Status = api.ReadinessResponseStatusOk
|
|
resp.DatasetTime = api.NewOptDateTime(field.Epoch())
|
|
} else {
|
|
resp.Status = api.ReadinessResponseStatusNotReady
|
|
resp.ErrorMessage = api.NewOptString("no dataset loaded")
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// NewError implements the ogen Handler interface for unhandled errors.
|
|
func (h *Handler) NewError(_ context.Context, err error) *api.ErrorStatusCode {
|
|
var statusErr *api.ErrorStatusCode
|
|
if errors.As(err, &statusErr) {
|
|
return statusErr
|
|
}
|
|
h.log.Error("unhandled error", zap.Error(err))
|
|
return newError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
func newError(status int, description string) *api.ErrorStatusCode {
|
|
return &api.ErrorStatusCode{
|
|
StatusCode: status,
|
|
Response: api.Error{
|
|
Error: api.ErrorError{
|
|
Type: http.StatusText(status),
|
|
Description: description,
|
|
},
|
|
},
|
|
}
|
|
}
|