-
-
Notifications
You must be signed in to change notification settings - Fork 49
Add GET /runs/trace/{run_id} endpoint
#267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saathviksheerla
wants to merge
4
commits into
openml:main
Choose a base branch
from
saathviksheerla:feat/get-run-trace-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
64ee6c2
Add GET /runs/trace/{run_id} endpoint
saathviksheerla 257c444
Address review feedback: remove None default, add Literal type for se…
saathviksheerla ae1f456
Add docstring to get_run_trace, cast selected to str
saathviksheerla 6b876f3
Use row.selected directly: enum returns plain string from DB
saathviksheerla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from collections.abc import Sequence | ||
| from typing import cast | ||
|
|
||
| from sqlalchemy import Connection, Row, text | ||
|
|
||
|
|
||
| def get_run(run_id: int, expdb: Connection) -> Row | None: | ||
| """Check if a run exists. Used to distinguish 571 (run not found) from 572 (no trace).""" | ||
| return expdb.execute( | ||
| text( | ||
| """ | ||
| SELECT rid | ||
| FROM run | ||
| WHERE rid = :run_id | ||
| """, | ||
| ), | ||
| parameters={"run_id": run_id}, | ||
| ).one_or_none() | ||
|
|
||
|
|
||
| def get_trace(run_id: int, expdb: Connection) -> Sequence[Row]: | ||
| """Fetch all trace iterations for a run, ordered as PHP does: repeat, fold, iteration.""" | ||
| return cast( | ||
| "Sequence[Row]", | ||
| expdb.execute( | ||
| text( | ||
| """ | ||
| SELECT `repeat`, `fold`, `iteration`, setup_string, evaluation, selected | ||
| FROM trace | ||
| WHERE run_id = :run_id | ||
| ORDER BY `repeat` ASC, `fold` ASC, `iteration` ASC | ||
| """, | ||
| ), | ||
| parameters={"run_id": run_id}, | ||
| ).all(), | ||
| ) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from http import HTTPStatus | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException | ||
| from sqlalchemy import Connection | ||
|
|
||
| import database.runs | ||
| from routers.dependencies import expdb_connection | ||
| from schemas.runs import RunTrace, RunTraceResponse, TraceIteration | ||
|
|
||
| router = APIRouter(prefix="/runs", tags=["runs"]) | ||
|
|
||
|
|
||
| @router.get("/trace/{run_id}") | ||
| def get_run_trace( | ||
| run_id: int, | ||
| expdb: Annotated[Connection, Depends(expdb_connection)], | ||
| ) -> RunTraceResponse: | ||
| """Get the optimization trace for a run. | ||
|
|
||
| Returns all hyperparameter configurations tried during tuning, their | ||
| evaluations, and whether each was selected. Mirrors PHP API behavior. | ||
| """ | ||
| # 571: run does not exist at all | ||
| if not database.runs.get_run(run_id, expdb): | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.PRECONDITION_FAILED, | ||
| detail={"code": "571", "message": "Run not found."}, | ||
| ) | ||
|
|
||
| trace_rows = database.runs.get_trace(run_id, expdb) | ||
|
|
||
| # 572: run exists but has no trace data | ||
| if not trace_rows: | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.PRECONDITION_FAILED, | ||
| detail={"code": "572", "message": "No trace found for run."}, | ||
| ) | ||
|
|
||
| return RunTraceResponse( | ||
| trace=RunTrace( | ||
| # Cast to str: PHP returns run_id and all iteration fields as strings. | ||
| run_id=str(run_id), | ||
| trace_iteration=[ | ||
| TraceIteration( | ||
| repeat=str(row.repeat), | ||
| fold=str(row.fold), | ||
| iteration=str(row.iteration), | ||
| setup_string=row.setup_string, | ||
| evaluation=row.evaluation, | ||
| selected=row.selected, | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for row in trace_rows | ||
| ], | ||
| ), | ||
| ) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class TraceIteration(BaseModel): | ||
| repeat: str | ||
| fold: str | ||
| iteration: str | ||
| setup_string: str | ||
| evaluation: str | ||
| selected: Literal["true", "false"] | ||
|
|
||
|
|
||
| class RunTrace(BaseModel): | ||
| run_id: str | ||
| trace_iteration: list[TraceIteration] | ||
|
|
||
|
|
||
| # Wraps RunTrace in {"trace": {...}} to match PHP API response structure. | ||
| class RunTraceResponse(BaseModel): | ||
| trace: RunTrace |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from http import HTTPStatus | ||
|
|
||
| import pytest | ||
| from starlette.testclient import TestClient | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("run_id", [34]) | ||
| def test_get_run_trace(py_api: TestClient, run_id: int) -> None: | ||
| response = py_api.get(f"/runs/trace/{run_id}") | ||
| assert response.status_code == HTTPStatus.OK | ||
|
|
||
| body = response.json() | ||
| assert "trace" in body | ||
|
|
||
| trace = body["trace"] | ||
| assert trace["run_id"] == str(run_id) | ||
| assert "trace_iteration" in trace | ||
| assert len(trace["trace_iteration"]) > 0 | ||
|
|
||
| # Verify structure and types of each iteration — PHP returns all fields as strings | ||
| for iteration in trace["trace_iteration"]: | ||
| assert "repeat" in iteration | ||
| assert "fold" in iteration | ||
| assert "iteration" in iteration | ||
| assert "setup_string" in iteration | ||
| assert "evaluation" in iteration | ||
| assert "selected" in iteration | ||
| assert isinstance(iteration["repeat"], str) | ||
| assert isinstance(iteration["fold"], str) | ||
| assert isinstance(iteration["iteration"], str) | ||
| assert isinstance(iteration["setup_string"], str) | ||
| assert isinstance(iteration["evaluation"], str) | ||
| assert iteration["selected"] in ("true", "false") | ||
|
|
||
|
|
||
| def test_get_run_trace_ordering(py_api: TestClient) -> None: | ||
| """Trace iterations must be ordered by repeat, fold, iteration ASC — matches PHP.""" | ||
| response = py_api.get("/runs/trace/34") | ||
| assert response.status_code == HTTPStatus.OK | ||
|
|
||
| iterations = response.json()["trace"]["trace_iteration"] | ||
| keys = [(int(i["repeat"]), int(i["fold"]), int(i["iteration"])) for i in iterations] | ||
| assert keys == sorted(keys) | ||
|
|
||
|
|
||
| def test_get_run_trace_run_not_found(py_api: TestClient) -> None: | ||
| """Run does not exist at all — expect error 571.""" | ||
| response = py_api.get("/runs/trace/999999") | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"]["code"] == "571" | ||
|
|
||
|
|
||
| def test_get_run_trace_negative_id(py_api: TestClient) -> None: | ||
| """Negative run_id can never exist — expect error 571.""" | ||
| response = py_api.get("/runs/trace/-1") | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"]["code"] == "571" | ||
|
|
||
|
|
||
| def test_get_run_trace_invalid_id(py_api: TestClient) -> None: | ||
| """Non-integer run_id — FastAPI should reject with 422 before hitting our handler.""" | ||
| response = py_api.get("/runs/trace/abc") | ||
| assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY | ||
|
|
||
|
|
||
| def test_get_run_trace_no_trace(py_api: TestClient) -> None: | ||
| """Run exists but has no trace data — expect error 572. | ||
| Run 24 exists in the test DB but has no trace rows.""" | ||
| response = py_api.get("/runs/trace/24") | ||
| assert response.status_code == HTTPStatus.PRECONDITION_FAILED | ||
| assert response.json()["detail"]["code"] == "572" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: openml/server-api
Length of output: 229
🏁 Script executed:
Repository: openml/server-api
Length of output: 488
🏁 Script executed:
Repository: openml/server-api
Length of output: 229
🏁 Script executed:
Repository: openml/server-api
Length of output: 229
🏁 Script executed:
Repository: openml/server-api
Length of output: 759
🏁 Script executed:
Repository: openml/server-api
Length of output: 96
🏁 Script executed:
Repository: openml/server-api
Length of output: 688
🏁 Script executed:
Repository: openml/server-api
Length of output: 3343
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 215
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 83
🏁 Script executed:
Repository: openml/server-api
Length of output: 128
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
# Check the size of the SQL files first wc -l docker/mysql/data/openml.sql docker/mysql/data/openml_expdb.sqlRepository: openml/server-api
Length of output: 139
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
# Read the SQL files directly cat docker/mysql/data/openml.sql cat docker/mysql/data/openml_expdb.sqlRepository: openml/server-api
Length of output: 158
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 2277
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 81
🏁 Script executed:
Repository: openml/server-api
Length of output: 73
🏁 Script executed:
# Read the database/runs.py file cat src/database/runs.pyRepository: openml/server-api
Length of output: 1128
🏁 Script executed:
Repository: openml/server-api
Length of output: 401
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 43
🏁 Script executed:
Repository: openml/server-api
Length of output: 3383
Stringify
evaluationbefore buildingTraceIteration.TraceIteration.evaluationis declared asstr, and tests explicitly assert it must be a string, but line 50 forwards the raw DB value. Lines 46–48 castrepeat,fold, anditerationwithstr()for PHP compatibility;evaluationshould follow the same pattern. If that column is numeric in the DB, the current code relies on implicit Pydantic coercion instead of explicit serialization.🛠️ Proposed fix
TraceIteration( repeat=str(row.repeat), fold=str(row.fold), iteration=str(row.iteration), setup_string=row.setup_string, - evaluation=row.evaluation, + evaluation=str(row.evaluation), selected=row.selected, )🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
evaluation is declared as varchar(265) in the database and SQLAlchemy returns it as a plain string — verified via HEX(LEFT(evaluation,4)). Explicit str() cast is unnecessary here, same as selected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@saathviksheerla, thanks for verifying that directly — ifevaluationisvarchar(265)and SQLAlchemy already returns it as a plain string, the explicitstr()cast is indeed redundant. My suggestion was precautionary without confirmed knowledge of the column type; your hex verification settles it. No change needed here.✏️ Learnings added
🧠 Learnings used