193 lines
8.8 KiB
Markdown
193 lines
8.8 KiB
Markdown
# ERD — Feedmill Recounter
|
||
|
||
Data model for the feedmill recounter. **No database** — jobs live in
|
||
memory (`JobQueue._jobs` dict, `src/job.py:82`) and die on restart; state
|
||
survives only as filesystem artifacts under `$OUTPUT_DIR` (env override
|
||
`OUTPUT_DIR`, default `./output`) and `$UPLOAD_DIR` (default `./uploads`).
|
||
|
||
**No foreign keys enforced** — every reference is soft: job stores plain
|
||
string paths, zone presets are name strings, model stems are dict keys.
|
||
|
||
```mermaid
|
||
erDiagram
|
||
upload_video ||--o{ job : "re-analyze, N runs per file"
|
||
model_group ||--o{ job_result : "selected format runs as"
|
||
model_group ||--o{ conversion_job : "source stem converts to"
|
||
zone_preset ||--o{ job : "soft name ref, zone_mode=fixed"
|
||
job ||--o{ job_result : "1 for single model, 1 merged row for many"
|
||
job ||--|| output_dir : "1:1, output/job-"
|
||
output_dir ||--o{ result_file : "written on completion"
|
||
output_dir ||--o{ sample_frame : "lazy thumbnail/sample cache"
|
||
upload_video ||--o{ upload_video : "same name re-upload, _N suffix"
|
||
|
||
job {
|
||
TEXT job_id PK "job-<uuid4[:8]>, in-memory only"
|
||
TEXT video_path "abs/rel path under uploads/"
|
||
TEXT status "PENDING | RUNNING | COMPLETED | FAILED | CANCELLED"
|
||
FLOAT progress "0.0 - 1.0"
|
||
TEXT current_model "label while running"
|
||
TEXT zone_mode "dynamic | freeze | fixed"
|
||
TEXT zone_preset "name in zones.json, '' if dynamic"
|
||
BLOB fixed_zone "TruckROI | None, built at submit time"
|
||
INT output_max_height "transcode cap, None = source height"
|
||
TEXT preview_path "/tmp/feedmill_preview_<job_id>.jpg"
|
||
INT live_fps "MJPEG live stats"
|
||
INT live_loading "MJPEG live stats"
|
||
INT live_unloading "MJPEG live stats"
|
||
INT live_box_loading "MJPEG live stats"
|
||
INT live_box_unloading "MJPEG live stats"
|
||
TEXT error "exception str when FAILED"
|
||
TIMESTAMP created_at "default time.time()"
|
||
TIMESTAMP completed_at "NULL while running"
|
||
}
|
||
|
||
job_result {
|
||
TEXT model_name PK "filename, or 'Merged (a, b, c)'"
|
||
TEXT output_path "video path under output/job-*/"
|
||
INT loading_count "sack loading (crossing events)"
|
||
INT unloading_count "sack unloading"
|
||
INT net_count "loading - unloading"
|
||
INT box_loading_count "box loading"
|
||
INT box_unloading_count "box unloading"
|
||
INT box_net_count "box_loading - box_unloading"
|
||
INT batch_count "BatchManager state transitions"
|
||
INT frame_count "frames processed"
|
||
FLOAT duration_seconds "wall clock of pipeline run"
|
||
TEXT error "per-model failure, nullable"
|
||
}
|
||
|
||
model_group {
|
||
TEXT stem PK "filename stem, shared across formats"
|
||
TEXT formats "list: .engine > .pt > .onnx preference"
|
||
TEXT format_paths "dict ext -> resolved path"
|
||
TEXT known_classes "sack / truck / box / person"
|
||
TEXT default_format "engine if present"
|
||
}
|
||
|
||
conversion_job {
|
||
TEXT job_id PK "conv-<uuid4[:8]>, in-memory only"
|
||
TEXT stem FK "model stem being converted"
|
||
TEXT source_path "uploaded .pt/.onnx source"
|
||
TEXT status "pending | running | completed | failed | cancelled"
|
||
TEXT stage "queued | exporting onnx | exporting engine | done"
|
||
FLOAT progress "0.0 - 100.0"
|
||
TEXT error "exception str when FAILED, nullable"
|
||
FLOAT started_at "NULL while queued"
|
||
FLOAT completed_at "NULL while running"
|
||
TEXT output_paths "produced .onnx/.engine paths"
|
||
}
|
||
|
||
zone_preset {
|
||
TEXT name PK "key under zones.json presets"
|
||
FLOATS polygon "TruckROI points, video-pixel space"
|
||
BOOL active "matches zones.json active_preset"
|
||
}
|
||
|
||
upload_video {
|
||
TEXT path PK "uploads/<unique-filename>"
|
||
TEXT filename "original + _N on collision"
|
||
INT size_bytes "stat().st_size"
|
||
TIMESTAMP mtime "sort key in /api/videos"
|
||
INT height "source height, >720 triggers transcode"
|
||
}
|
||
|
||
output_dir {
|
||
TEXT job_id PK "output/job-<id>"
|
||
TIMESTAMP mtime "orphan re-attach threshold, 300s"
|
||
BOOL orphan "register_orphans re-attaches age>300s"
|
||
}
|
||
|
||
result_file {
|
||
TEXT filename PK "within output/job-*/"
|
||
TEXT kind "annotated.mp4 | thumbnail.jpg | predictions.csv"
|
||
INT size_bytes "download compression kicks in >250MB"
|
||
}
|
||
|
||
sample_frame {
|
||
TEXT path PK "output/job-*/samples/frame_N.jpg"
|
||
TEXT cache_marker "samples/.cached = newline list of paths"
|
||
}
|
||
```
|
||
|
||
## Keys
|
||
|
||
- `job.job_id`: generated `job-{uuid4().hex[:8]}` (`src/job.py:125`); not
|
||
stable across restarts — jobs are never reloaded, only their output dirs.
|
||
- `model_group.stem`: implicit key over `models/` files sharing a stem.
|
||
- `zone_preset.name`: key of `zones.json` → `presets[]`.
|
||
- `output_dir.job_id`: directory name equals `job.job_id`.
|
||
|
||
## Relationships (logical)
|
||
|
||
| From | To | How |
|
||
|---|---|---|
|
||
| `job` → `upload_video` | N—1 | `job.video_path` string; `/upload/reuse` re-queues same file. |
|
||
| `job` → `job_result` | 1—N | Single model → 1 `JobResult`; many models → 1 merged row (`src/job.py:308`). |
|
||
| `job_result` → `model_group` | N—1 | `JobResult.model_name` = `ModelConfig.filename`; stem links to `ModelGroup`. |
|
||
| `job` → `zone_preset` | N—1 | Soft: `zone_preset` string + `zone_mode=fixed`; preset deleted → `fixed_zone` still embedded. |
|
||
| `job` → `output_dir` | 1—1 | `output_dir = $OUTPUT_DIR/job_id`, created at `add_job` (`src/job.py:137`). |
|
||
|
||
## Job status state machine
|
||
|
||
```
|
||
PENDING ──▶ RUNNING ──▶ COMPLETED
|
||
│ ├──────▶ FAILED (exception in pipeline, job.error set)
|
||
└──────────▶ CANCELLED (api_cancel_job; worker checks in cancel_check)
|
||
```
|
||
|
||
- `delete_job` only accepts `COMPLETED | FAILED | CANCELLED` (`src/job.py:179`).
|
||
- Worker clears `latest_frame` on any terminal state; preview file removed in
|
||
`finally` (`src/job.py:335`).
|
||
|
||
## Storage layout (filesystem, not SQLite)
|
||
|
||
| Path | Role |
|
||
|---|---|
|
||
| `$UPLOAD_DIR/<name>[_N].<ext>` | source videos; `secure_filename` + extension whitelist |
|
||
| `$OUTPUT_DIR/job-<id>/` | per-job output: annotated mp4, `thumbnail.jpg`, `samples/`, `predictions.csv` |
|
||
| `$OUTPUT_DIR/job-<id>/samples/.cached` | newline list of extracted sample frame paths |
|
||
| `$OUTPUT_DIR/job-<id>/<name>.mp4` | the annotated video: compressed **in place** when the job finishes (same name, `os.replace`); swept 5 days after the job |
|
||
| `$OUTPUT_DIR/job-<id>/<name>.*.compress.tmp` | temp of an encode in flight; a stale one (killed encode) is swept after 2 h |
|
||
| `$MODELS_DIR/<stem>.{engine,pt,onnx}` | weight files grouped by stem; uploaded models land here; conversion writes `.onnx`/`.engine` beside the source |
|
||
| `/tmp/feedmill_preview_<id>.jpg` | live MJPEG frame, deleted in worker `finally` |
|
||
| `zones.json` | `presets[]` + `active_preset`, geometry only |
|
||
| `models/<stem>.{engine,pt,onnx}` | weight files grouped by stem |
|
||
| `config.yaml` | single source of truth for tunables (detection, roi, stabilizer, pipeline) |
|
||
|
||
## Derived values
|
||
|
||
- `net_count = loading_count − unloading_count`,
|
||
`box_net_count = box_loading_count − box_unloading_count`.
|
||
- `progress` set to `1.0` only on terminal success; FAILED rows keep the last
|
||
partial value.
|
||
- Upload height: source `>720p` is transcoded to 720p before queueing
|
||
(`app.py` `/upload`), so `upload_video.height` is pre-transcode.
|
||
|
||
## Hazards
|
||
- **Conversion jobs are volatile**: `ConversionManager` keeps jobs in memory
|
||
(`src/model_conversion.py`); restart loses all conversion state. Only one
|
||
conversion runs at a time and it blocks analysis/uploads while running.
|
||
|
||
- **Jobs are volatile**: restart loses all `Job` objects — only
|
||
`output/job-*` dirs survive. `_register_orphans` (age > 300 s) re-attaches
|
||
them as synthetic `COMPLETED` jobs with `video_path=""` so they stay
|
||
deletable from the UI (`src/job.py:88`). Younger dirs are skipped because
|
||
they may belong to a process still running.
|
||
- **`video_path=""` on orphans**: thumbnail/sample endpoints on a restarted
|
||
job cannot regenerate from source; they 404 once cache is missing.
|
||
- **Soft zone ref**: deleting a preset from `zones.json` does not invalidate
|
||
jobs already holding an embedded `fixed_zone` — geometry was snapshotted at
|
||
submit time.
|
||
- **Preview path in `/tmp`**: two jobs in one process never collide
|
||
(`job_id` in filename), but stale files survive a hard kill until the next
|
||
`delete_job`/`finally` runs.
|
||
|
||
## Sources
|
||
|
||
- Entity defs: `src/job.py:27-73` (`JobResult`, `Job`),
|
||
`src/model_registry.py:68-94` (`ModelConfig`, `ModelGroup`),
|
||
`src/model_conversion.py:14-33` (`ConversionJob`).
|
||
- Lifecycle: `src/job.py:114-148` (`add_job`), `src/job.py:203-343`
|
||
(`_run_job`), `src/job.py:88-112` (`_register_orphans`).
|
||
- HTTP surface: `app.py:110-209` (upload/reuse), `app.py:317-471` (APIs).
|