feat(anomaly): AI-powered carbon emission anomaly detection (rebased on main, port fix) — supersedes #10#11
Merged
Merged
Conversation
Original work by Anshika Sharma (PR #10) — rebased on top of current main. Original PR was based on an old main (only PR #5 merged) and the diff showed a 2300-line 'deletion' of PRs #6, #7, #9 as a side-effect. This commit reconstructs only the additive changes: BACKEND - backend/src/routes/anomaly.routes.ts (new) Pure stats anomaly detector: rolling z-score (window=5) and sudden percentage spike. Stateless POST /api/v1/anomaly/detect endpoint. Severity grading (low/medium/high) by zScore and pct increase. Mounted via the established routes/index.ts pattern (consistent with /auth, /aqi, /reports), not via a parallel app.use in app.ts. FRONTEND - src/lib/anomaly.ts (new) Typed wrapper around the shared api() helper; uses VITE_API_URL. - src/App.tsx Adds RUN SPIKE SCAN button on the AI Anomaly Detection capability card with severity-colored result panel. BUG FIX vs original PR #10: - Original frontend hard-coded http://localhost:5000/api/v1/anomaly/detect. Backend runs on :4000 — the feature would have 100% failed in the demo. Switched to api() helper which reads VITE_API_URL. Architecture preserved: the route shape mirrors what a future FastAPI + Isolation Forest microservice would expose, so swapping in real ML later is contained to the detect() function body.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request was created by @kiro-agent on behalf of @D-source1602 👻
Comment with /kiro fix to address specific feedback or /kiro all to address everything.
Learn about Kiro autonomous agent
Summary
Conflict-free version of PR #10 (anomaly detection by @anshikasharma3332). Original PR's branch was based on an old
main(only PR #5 merged), so its diff against currentmainshowed a 2300-line "deletion" of PRs #6, #7, and #9 as a side-effect.This PR rebases the work cleanly: only the additive changes (203 lines), original authorship preserved.
Action needed: merge this PR, then close PR #10.
What this ships
A working AI anomaly detection feature, end-to-end.
Backend
backend/src/routes/anomaly.routes.ts— pure stats detectorlow/medium/highbased on z-score + pctPOST /api/v1/anomaly/detect— accepts{ readings: [{ timestamp, co2 }, ...] }{ hasAnomaly, alerts: [...], summary }detect()function bodybackend/src/routes/index.ts— anomaly router mounted alongside/auth,/aqi,/reports(consistent with the established pattern, not a parallelapp.useinapp.ts)Frontend
src/lib/anomaly.ts— typed wrapper using the sharedapi()helper (so it picks upVITE_API_URLautomatically)src/App.tsx— addsRUN SPIKE SCANbutton on the AI Anomaly Detection capability card on the Dashboard, with a severity-colored result panel that shows alert details on detection or a green "no spike" badge otherwiseBug fix vs. original PR #10
The original frontend code hard-coded:
But the backend listens on port 4000. The feature would have failed 100% of the time in a demo. This PR uses the shared
api()helper which readsVITE_API_URLfrom.env.Conflict resolution strategy
Rather than carrying 2300 lines of "deletion noise" forward, I:
main(which has PRs feat(auth): Phase 3 — JWT auth controller, RBAC middleware, admin seed #5–feat(reports): PDF report generator (rebased on main, conflict-free) — supersedes #8 #9 merged)git -c user.name="Anshika Sharma" -c user.email="anshikasharma3332@gmail.com" commitgit log -1on this commit shows the original author/committer.To run after merging
Open
http://localhost:5173→ log in → on the Dashboard's Capabilities section, click RUN SPIKE SCAN on the AI Anomaly Detection card. The mock 5-reading series ends with a 260 µg/m³ spike — you should see a red alert panel with severity HIGH and a confidence score.Smoke test directly with curl:
Should return
hasAnomaly: truewith one alert.Diff stat
4 files, +203 / 0.