---
estimated_steps: 19
estimated_files: 1
skills_used: []
---

# T01: Add `order` query param to pipeline events endpoint

Add an `order` query parameter (values: `asc`/`desc`, default `desc`) to the `GET /admin/pipeline/events/{video_id}` endpoint in `backend/routers/pipeline.py` on ub01. Validate the value and apply `PipelineEvent.created_at.asc()` or `.desc()` accordingly. This preserves the existing default behavior (desc) while enabling chronological viewing.

## Steps

1. SSH to ub01, read the current `list_pipeline_events` function in `/vmPool/r/repos/xpltdco/chrysopedia/backend/routers/pipeline.py` (around line 179-225)
2. Add `order: Annotated[str | None, Query(description="Sort order: asc or desc")] = "desc"` parameter to the function signature
3. Add validation: if `order` not in `("asc", "desc")`, raise `HTTPException(400, "order must be 'asc' or 'desc'")`
4. Change the hardcoded `.order_by(PipelineEvent.created_at.desc())` to use `PipelineEvent.created_at.asc() if order == "asc" else PipelineEvent.created_at.desc()`
5. Rebuild and restart the API container: `cd /vmPool/r/repos/xpltdco/chrysopedia && docker compose build chrysopedia-api && docker compose up -d chrysopedia-api`
6. Verify with curl: `curl 'http://ub01:8096/api/v1/admin/pipeline/events/{video_id}?order=asc&limit=3'` should return events in ascending chronological order. Compare timestamps with `?order=desc&limit=3` to confirm different ordering.

## Must-Haves

- [x] `order` param accepts `asc` and `desc`
- [x] Default is `desc` (preserves existing behavior)
- [x] Invalid values return 400
- [x] `order=asc` returns events oldest-first

## Negative Tests

- `?order=invalid` → 400 error
- No `order` param → same as `desc` (backward compatible)

## Verification

- `curl -s 'http://ub01:8096/api/v1/admin/pipeline/events/{video_id}?order=asc&limit=3' | python3 -m json.tool` returns events with ascending timestamps
- `curl -s 'http://ub01:8096/api/v1/admin/pipeline/events/{video_id}?order=invalid'` returns 400

## Inputs

- ``backend/routers/pipeline.py` — existing endpoint with hardcoded DESC ordering (line ~200 on ub01 at /vmPool/r/repos/xpltdco/chrysopedia/backend/routers/pipeline.py)`

## Expected Output

- ``backend/routers/pipeline.py` — modified with `order` query param support (on ub01 at /vmPool/r/repos/xpltdco/chrysopedia/backend/routers/pipeline.py)`

## Verification

curl -s 'http://ub01:8096/api/v1/admin/pipeline/events/{video_id}?order=asc&limit=3' returns events in ascending timestamp order; curl with order=invalid returns HTTP 400
