8000 fix(server): enforce series limit in sparse timeseries by ezyang · Pull Request #153 · ezyang/scubaduck · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(server): enforce series limit in sparse timeseries #153

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

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions scubaduck/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ def query() -> Any: # pyright: ignore[reportUnusedFunction]
)

bucket_size: int | None = None
series_limit = params.limit
if params.graph_type == "timeseries":
bucket_size = _granularity_seconds(
params.granularity,
Expand Down Expand Up @@ -717,6 +718,24 @@ def _serialize(value: Any) -> Any:
return value

rows = [[_serialize(v) for v in r] for r in rows]

if (
params.graph_type == "timeseries"
and params.group_by
and series_limit is not None
):
key_slice = slice(1, 1 + len(params.group_by))
kept: set[tuple[Any, ...]] = set()
filtered: list[list[Any]] = []
for row in rows:
key = tuple(row[key_slice])
if key not in kept:
if len(kept) >= series_limit:
continue
kept.add(key)
filtered.append(row)
rows = filtered

result: Dict[str, Any] = {"sql": sql, "rows": rows}
if params.start is not None:
result["start"] = str(params.start)
CABD Expand Down
24 changes: 24 additions & 0 deletions tests/test_server_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,30 @@ def test_timeseries_limit_applies_to_series() -> None:
assert all(r[1] == "alice" for r in data["rows"])


def test_timeseries_sparse_limit_filtering() -> None:
app = server.app
client = app.test_client()
payload: dict[str, Any] = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-03 00:00:00",
"graph_type": "timeseries",
"limit": 2,
"group_by": ["user"],
"columns": ["value"],
"x_axis": "timestamp",
"granularity": "1 day",
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert len(data["rows"]) == 3
users = {r[1] for r in data["rows"]}
assert users == {"alice", "bob"}


def test_timeseries_auto_and_fine_buckets() -> None:
app = server.app
client = app.test_client()
Expand Down
Loading
0