feat: autonomous budget optimization engine (#92)#467
Open
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
Open
feat: autonomous budget optimization engine (#92)#467sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
Conversation
Implements issue rohitdash08#92: detect overspending patterns, recommend category-level reallocations, and return an adaptive budget plan based on spending history. New files: packages/backend/app/services/budget_optimizer.py - get_budget_optimization(uid, months, anchor) → full analysis dict - _iter_months(): generates (year, month) tuples for the look-back window - _monthly_category_spend(): per-category expense aggregation per month - _monthly_income(): income aggregation per month - _detect_overspending(): flags categories consuming >1.5× their fair share - _compute_trend(): MoM average change + direction (increasing/decreasing/stable) - _build_reallocations(): trim suggestions (−10% on flagged categories) + 50/30/20 target budget when income is available - No external dependencies — pure Python + SQLAlchemy packages/backend/app/routes/budget.py - GET /budget/optimize?months=3&anchor=YYYY-MM-DD — full analysis - GET /budget/optimize/summary — lightweight summary - Input validation: months 1–12, anchor ISO date - JWT-protected, per-user isolation packages/backend/tests/test_budget_optimizer.py - 32 tests covering: unit (iter_months, detect_overspending, compute_trend), service (empty history, structure), HTTP (auth, params validation, overspend detection, income→50/30/20, net flow, user isolation, summary) Updated: packages/backend/app/routes/__init__.py — register budget_bp at /budget /claim rohitdash08#92 Closes rohitdash08#92
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.
Implements autonomous budget optimization to fix #92.
Analyses a user's spending history to detect overspending patterns, recommend category-level reallocations, and return an adaptive budget plan. Pure Python + SQLAlchemy — no external dependencies.
Endpoints
GET /budget/optimize— full analysisGET /budget/optimize/summary— lightweight version (trend + net flow + top 3 recommendations)Both JWT-protected. Query params:
months(1–12, default 3),anchor(YYYY-MM-DD, default today).What it detects
Example response (abbreviated)
{ "analysis_period_months": 3, "avg_monthly_expenses": 12400.00, "avg_monthly_income": 15000.00, "net_flow": 2600.00, "trend": { "direction": "increasing", "avg_mom_change_pct": 8.3 }, "overspending_alerts": [ { "category_name": "Entertainment", "amount": 4200.00, "share_pct": 33.9, "excess": 2050.00, "reason": "This category accounts for 33.9% of total expenses..." } ], "recommendations": [ { "type": "trim", "category_name": "Entertainment", "savings_opportunity": 420.00, ... }, { "type": "target_budget", "needs_target": 7500.00, "wants_target": 4500.00, "savings_target": 3000.00, ... } ] }Files
app/services/budget_optimizer.py— core analysis engineapp/routes/budget.py— Flask blueprintapp/routes/__init__.py— blueprint registrationtests/test_budget_optimizer.py— 32 tests (unit + integration)/claim #92
Closes #92