Repository navigation
Conversation
All routes of the API were defined inline in create_app() in api/main.py, which had grown to 1553 lines. Each resource now has its own APIRouter in api/routes/ (health, analysis, runs, predict, explain, organizations, subscriptions, alerts), and main.py only builds the app, adds middleware and includes the routers. Types and helpers used by more than one router move to api/shared.py, with a PROJECT_ROOT constant replacing the parents[3] lookups, which would have resolved to the wrong directory one level deeper in routes/. The code is moved, not changed: the OpenAPI schema, the route table and the middleware stack are identical before and after, with and without a built frontend. Tests that patched climatevision.api.main now patch the module the code moved to, and a new test checks that every router is included, that no route is defined twice, that API routes are matched before the frontend fallback, and where PROJECT_ROOT points. Closes Climate-Vision#58
FastAPI 0.165, which CI installs, keeps an included router as a single entry in app.routes instead of copying its routes there, so comparing app.routes with the routers' routes failed in CI although every route is served. The wiring is now checked against the paths of the OpenAPI schema, and the order against the frontend fallback with requests to an app built over a temporary frontend/dist.
5 of 14 tasks
Brings in Climate-Vision#144. Its fix to get_run_stats, which now counts alerts from organization_alerts, is applied in routes/runs.py, where the endpoint moved to; main.py keeps the refactored version.
Member
|
Thanks @Ayomipo18, this has been needed for a while: |
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.
Summary
Splits
src/climatevision/api/main.pyinto oneAPIRouterper resource, as proposed in #58. All routes were defined inline increate_app(), and the file had grown to 1553 lines since the issue was filed.main.pyis now 172 lines: it builds the app, adds the middleware and includes the routers.The code is moved, not changed. The OpenAPI schema, the route table (paths, methods, names, order) and the middleware stack are byte-for-byte identical before and after, both with and without a built frontend.
Related Issue
Closes #58
Type of Change
Key Changes
schemas.pyis left alone: nothing imports it, and itsAnalysisTypelacksflooding_sar, so switching to it would change validation./redirect stay ahead of them, and the frontend fallback stays last.PROJECT_ROOTinshared.pyreplaces the threePath(__file__).resolve().parents[3]lookups. Copied intoroutes/, they would have resolved one directory too high, so the uploads, the template result and the model weights check would have looked in the wrong place._-prefixed helpers, so it can be matched line for line withgit diff --color-moved --color-moved-ws=allow-indentation-change. Apart from the indentation and@app.→@router., the only differences are black's formatting of the new files.explain_predictionwas imported inmain.pybut never used, so it was not carried over.Testing
pytest tests/): 235 passed, 1 skipped, on both Python 3.9 / FastAPI 0.128 and Python 3.11 / FastAPI 0.165. The one failure,test_onnx_inference.py::test_run_inference_reports_backend_and_latency, fails the same way onmainlocally (missing ONNX runtime).app.openapi(), the middleware stack and the responses to a set of GET requests (API paths, unknown paths, deep links) before and after, with and withoutfrontend/dist. They are identical with FastAPI 0.128 (Python 3.9) and with 0.165 (Python 3.11, what CI installs). On 0.128 the route table (app.routes, including order) is identical too.npm run dev): no frontend change.npm run buildpasses.Tests that patched
climatevision.api.main.run_inference_from_geenow patchclimatevision.api.routes.predict.run_inference_from_gee, where the code now looks it up.WS_RUN_NOT_FOUNDis imported fromroutes.runs.The new
tests/test_api_routers.pychecks that:ROUTERS, which I checked./runsgetsindex.html. It builds the app over a temporaryfrontend/distand fails if the routers are included after the fallback, which I checked.PROJECT_ROOTis the repository root.The first version of these tests compared
app.routeswith the routers' routes and failed in CI. FastAPI 0.165 keeps an included router as a single entry inapp.routes, so the second commit checks behaviour instead of that internal list.flake8 src/ --select=E9,F63,F7,F82reports 0, and the new files are black-clean.Found along the way, not changed here:
GET /api/runs/statsreturns a 500 onmainwith any database. It countsacknowledged = 0in thealertstable, but onlyorganization_alertshas that column. This PR moves the code as it is, so I'll open a separate issue and fix for it.Open PRs: #141 merges cleanly on top of this branch, and its tests pass after the merge. The older open PRs that edit
main.py(#27, #28, #31, #56) would need to move their changes into the matching router.Checklist
developbranch (notmain): it targetsmain, where docs: remove references to nonexistent requirements-dev.txt #123, test(frontend): add Vitest + React Testing Library suite (#9) #125 and Add: WebSocket endpoint for real-time run status (#11) #140 were merged, since there is nodevelopbranch anymore