Skip to content

feat(stats): add /v2/stats/means-peak for peak operatives and means (#30) - #50

Merged
tomahock merged 2 commits into
mainfrom
feat/stats-means-peak
Oct 26, 2026
Merged

tomahock merged 2 commits into
mainfrom
feat/stats-means-peak

Conversation

@tomahock

Copy link
Copy Markdown
Member

Closes #30

What

GET /v2/stats/means-peak?after=YYYY-MM-DD&before=YYYY-MM-DD[&district=][&concelho=][&freguesia=][&all=1]

As clarified in the issue thread: for each kind of means, the peak reached by a single incident, with the incident and the time it happened.

{
  "success": true,
  "data": {
    "incidents": 412,
    "man":     { "value": 60, "at": "2025-08-10T16:00:00+01:00", "incident": { …IncidentResource… } },
    "terrain": { "value": 14, "at": "…", "incident": { … } },
    "aerial":  { "value": 12, "at": "2025-08-20T13:15:00+01:00", "incident": { … } }
  }
}

Semantics

  • Incidents considered: started (dateTime) between after 00:00 and before 23:59:59, Europe/Lisbon. Fires only, unless all=1.
  • Peak = highest value in the history snapshots of those incidents (saved on every change of man/terrain/aerial). Earliest snapshot wins on ties. at = when that snapshot was recorded.
  • Location filters are case-insensitive (value as sent or the stored casing, like fix(api): return 422 for an unknown concelho on v2 incident endpoints (#27) #49).
  • Non-numeric legacy snapshot values and values ≤ 0 (ICNF-only -1) are ignored. No history → null.
  • Incidents before 2022 have no history (observer only saves from 2022), so older intervals return nulls.
  • 422 on missing/invalid dates, before < after, or an interval > 366 days. Response cached 1h in Redis.

Implementation notes

  • Raw Mongo queries for incident ids and history: laravel-mongodb v5 aliases id → _id, but legacy documents keep the business id in a separate id field and their history references it via id. Covered by a test.
  • Uses the existing history indexes (incidentId/id + created).

Tests

tests/Feature/Controllers/StatsController/MeansPeakTest.php (5 tests: validation, peaks + time + incident, interval boundaries, location filter, string values/nulls, legacy documents). Full suite against a throwaway MongoDB 7: 85 pass.

OpenAPI: docs/stats/paths/v2-stats-means-peak.yaml + docs/stats/models/MeansPeak.yaml, referenced from docs/api.yaml.

Deploy

Deploy as usual. No env changes, no migrations, no queue restart needed.

🤖 Generated with Claude Code

)

For the incidents that started in [after, before] (optionally filtered
by district/concelho/freguesia), returns the highest man, terrain and
aerial values recorded in the incident history, with the time each peak
was recorded and the incident.

Uses raw queries so legacy documents (ObjectId _id plus a separate `id`
field) are matched; laravel-mongodb v5 aliases `id` to `_id`. Interval
limited to 366 days, response cached for 1 hour.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T15:41:42.784585Z 71a6f40 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 71a6f40d95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread app/Http/Controllers/StatsController.php
)

IncidentResource adds history/statusHistory when the current request has
`extend`, and the cache key ignores it, so an extend=1 request could
populate the shared 1h entry with the full collections (and vice versa).
Resolve the incident against an empty request for a fixed shape.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@tomahock
tomahock merged commit 68b10e9 into main Oct 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] Create API call that returns max level of operatives involved

1 participant