Skip to content

Return empty series instead of an HTTP 500 when compute_node_series has no observations - #218

Open
eastagiletracker wants to merge 1 commit into
CUAHSI:developfrom
eastagiletracker:agile-board/compute-node-series-empty-input
Open

Return empty series instead of an HTTP 500 when compute_node_series has no observations#218
eastagiletracker wants to merge 1 commit into
CUAHSI:developfrom
eastagiletracker:agile-board/compute-node-series-empty-input

Conversation

@eastagiletracker

Copy link
Copy Markdown

This PR proposes fixing an HTTP 500 in the /data/compute_node_series node-statistics endpoint when it receives no observations, and adding the backend pytest suite that your make test target runs. We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/198. You can sign in with your GitHub ID to claim ownership of the project.

The fix

When a request to POST /data/compute_node_series carries no node observations — an empty body [], or a series with no points [[]]SwotNodeDataSeriesModel.as_dataframe() builds an empty DataFrame with no columns, and the very next [NodeVariables.list()] column selection raises a KeyError, which FastAPI surfaces to the caller as a 500.

Reproduced on develop at HEAD (fddb1e0), using the same python:3.10 / pandas==2.2.2 / fastapi==0.111.1 the API container pins:

POST /data/compute_node_series  json=[]        -> HTTP 500  (KeyError)
POST /data/compute_node_series  json=[[]]      -> HTTP 500  (KeyError)
POST /data/compute_node_series  json=[<obs>]   -> HTTP 200  (correct median / q0.25 / q0.75)

The traceback is KeyError: "None of [Index(['wse', 'width', 'area_total', 'p_dist_out'], dtype='object')] are in the [columns]".

The statistics feature reaches this endpoint from stores/stats.js getStatistics(), which posts the visible swot_node_series datasets' points; that list is empty whenever no node series is currently visible, or when time-range / data-quality filtering has removed every point. Your open PR #210 already adds a front-end guard for one of these paths (recomputeStatsAndUpdateCharts), which is what pointed me at the failure mode — this change is complementary and hardens the endpoint itself, so the API no longer 500s regardless of what the client sends.

The fix materializes the dataframe once and, when it is empty, returns {"median": [], "q0.25": [], "q0.75": []} — the same response shape as the success path, so the front end's generateStatisticsSeries loop simply builds no series. The non-empty path is unchanged, so existing behavior is preserved.

I also added the api/swotvis/tests/ suite that your Makefile test target (docker compose exec api pytest tests) already invokes but which did not exist yet, and added pytest to requirements-dev.txt. The suite covers the normal computation (median = 15.0 and quartiles 12.5 / 17.5 for two observations of 10 and 20 at one distance) plus the two empty-input regressions.

Verification: the new tests fail against current develop (2 failed, 1 passed — the empty-input cases 500) and pass with the fix (3 passed), run exactly as make test runs them (pytest tests from /swotvis). Your pinned isort==5.13.2 and black -S -l 120 report no changes on the touched files. The rest of the repo is untouched — this is a backend-only change.

How this was managed

This work was tracked as a story on a live agile board we imported from this repository's own issues and pull requests (217 stories, 8 labels): https://eastagiletracker.com/projects/198/stories/62015 — and the board itself, showing your project's history, lives at https://eastagiletracker.com/projects/198.

board

If you'd rather not receive contributions like this, reply no-more-prs on this pull request and we won't open any further ones on your repositories.


Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com

…no observations

POST /data/compute_node_series raised a KeyError (HTTP 500) for an empty
request body ([]) or a series with no points ([[]]): the resulting empty
dataframe has no node-variable columns to select. Return empty
median/q0.25/q0.75 series in that case instead.

Also add the api/swotvis/tests pytest suite that the `make test` target
already invokes but which did not exist, covering the normal computation
and this empty-input regression.
@netlify

netlify Bot commented Aug 11, 2026

Copy link
Copy Markdown

👷 Deploy request for swotviz pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 743f231

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.

1 participant