feat: add /bootstrap snapshot, numeric UID 10001, CI gate
GET /bootstrap returns the 8 sensors, hourly series for all of them, context, meta, and versions in the CONTRACT.md shape. Snapshot artifacts are projected to contract fields once at boot; incomplete artifacts return 503. Adds tests for shape, sensor count, hourly alignment, and versions. Dockerfile now creates UID/GID 10001, uses COPY --chown=10001:10001, and runs as USER 10001:10001. Adds make check and a minimal Gitea Actions workflow that runs the same local gate: uv sync --frozen, compileall, pytest.
This commit is contained in:
1 parent
7ec4fc8bb3
commit
8656d9bc8a
6 files changed
+210
-12
No files matched your search
+70
-1
@@ -1,4 +1,4 @@
|
||||
"""FastAPI ML service: /healthz, /readyz, /meta, /predict, /simulate, /train.
|
||||
"""FastAPI ML service: /healthz, /readyz, /meta, /bootstrap, /predict, /simulate, /train.
|
||||
|
||||
Shapes follow CONTRACT.md. The service is internal: only Go exposes it publicly,
|
||||
and the training routes require a bearer control token.
|
||||
@@ -36,6 +36,7 @@ log = logging.getLogger(__name__)
|
||||
|
||||
MAX_UPLOAD_BYTES = 60 * 1024 * 1024
|
||||
ACTIVE_STATUSES = {"queued", "running"}
|
||||
SENSOR_FIELDS = ("id", "name", "lat", "lon", "mean", "p95", "battery")
|
||||
|
||||
|
||||
def _utc_now() -> str:
|
||||
@@ -48,6 +49,9 @@ class Runtime:
|
||||
metrics: dict | None = None
|
||||
weather: pd.DataFrame | None = None
|
||||
weather_meta: dict | None = None
|
||||
sensors: list[dict] | None = None
|
||||
series: dict[str, list[dict]] | None = None
|
||||
context: dict | None = None
|
||||
small_bundle: dict | None = None
|
||||
small_metrics: dict | None = None
|
||||
model_source: str = "baked"
|
||||
@@ -75,6 +79,38 @@ def _load_runtime(runtime: Runtime) -> None:
|
||||
json.loads(meta_path.read_text()) if meta_path.exists() else None
|
||||
)
|
||||
|
||||
# Baked snapshot artifacts served by GET /bootstrap. Go caches this once at
|
||||
# startup, so loading them at boot keeps the endpoint a pure in-memory read.
|
||||
# Project to the contract fields; artifacts carry extra audit fields
|
||||
# (last_seen) that Go does not need.
|
||||
sensors_path = artifacts / "sensors.json"
|
||||
if sensors_path.exists():
|
||||
sensors = json.loads(sensors_path.read_text())
|
||||
runtime.sensors = [
|
||||
{key: sensor.get(key) for key in SENSOR_FIELDS} for sensor in sensors
|
||||
]
|
||||
else:
|
||||
log.warning("snapshot artifact missing: %s", sensors_path)
|
||||
|
||||
series_path = artifacts / "series.json"
|
||||
if series_path.exists():
|
||||
series = json.loads(series_path.read_text()).get("series") or {}
|
||||
runtime.series = {
|
||||
sensor: [
|
||||
{"t": point["t"], "v": point["v"]}
|
||||
for point in series.get(sensor, [])
|
||||
]
|
||||
for sensor in config.USABLE_SENSORS
|
||||
}
|
||||
else:
|
||||
log.warning("snapshot artifact missing: %s", series_path)
|
||||
|
||||
context_path = artifacts / "context.json"
|
||||
if context_path.exists():
|
||||
runtime.context = json.loads(context_path.read_text())
|
||||
else:
|
||||
log.warning("snapshot artifact missing: %s", context_path)
|
||||
|
||||
small_path = artifacts / "small_model.joblib"
|
||||
small_metrics_path = artifacts / "small_metrics.json"
|
||||
if small_path.exists():
|
||||
@@ -468,6 +504,39 @@ def create_app() -> FastAPI:
|
||||
}
|
||||
return payload
|
||||
|
||||
@app.get("/bootstrap")
|
||||
def bootstrap() -> dict:
|
||||
"""Snapshot Go caches at startup: sensors, hourly series, context, meta."""
|
||||
state = runtime()
|
||||
if state.bundle is None or state.metrics is None:
|
||||
raise HTTPException(503, "model is not loaded")
|
||||
if state.sensors is None or state.series is None or state.context is None:
|
||||
raise HTTPException(503, "snapshot artifacts are not loaded")
|
||||
if len(state.sensors) != len(config.USABLE_SENSORS) or not all(
|
||||
state.series.get(sensor) for sensor in config.USABLE_SENSORS
|
||||
):
|
||||
raise HTTPException(503, "snapshot is incomplete")
|
||||
|
||||
metrics = state.metrics
|
||||
context = state.context
|
||||
return {
|
||||
"data_version": metrics.get("data_version"),
|
||||
"model_version": metrics.get("model_version"),
|
||||
"context_version": metrics.get("context_version"),
|
||||
"sensors": state.sensors,
|
||||
"series": state.series,
|
||||
"context": {
|
||||
"weather": context.get("weather"),
|
||||
"holidays": context.get("holidays"),
|
||||
"updated_at": context.get("updated_at"),
|
||||
},
|
||||
"meta": {
|
||||
"coverage": metrics.get("coverage"),
|
||||
"metrics": metrics.get("metrics"),
|
||||
"limits": metrics.get("limits", []),
|
||||
},
|
||||
}
|
||||
|
||||
@app.post("/predict")
|
||||
def predict_endpoint(body: PredictRequest) -> dict:
|
||||
state = runtime()
|
||||
|
||||
Reference in new issue
Block a user