Task ID: task_e_682fd458c4a883238ed262add0d82458
Configuring language runtimes...
+ rm -- /tmp/UXmEVD-setup_script.sh
+ source ./codex_setup.sh
++ uv sync --frozen
Using CPython 3.12.3 interpreter at: /usr/bin/python3.12
Creating virtual environment at: .venv
Building scubaduck @ file:///workspace/scubaduck
Downloading playwright (43.1MiB)
Downloading ruff (11.0MiB)
Downloading duckdb (19.3MiB)
Downloading pyright (5.3MiB)
Downloading ruff
Built scubaduck @ file:///workspace/scubaduck
Downloading duckdb
Downloading playwright
Downloading pyright
Prepared 33 packages in 2.48s
Installed 33 packages in 108ms
+ blinker==1.9.0
+ certifi==2025.4.26
+ charset-normalizer==3.4.2
+ click==8.2.0
+ duckdb==1.2.2
+ execnet==2.1.1
+ flask==3.1.1
+ greenlet==3.2.2
+ idna==3.10
+ iniconfig==2.1.0
+ itsdangerous==2.2.0
+ jinja2==3.1.6
+ markupsafe==3.0.2
+ nodeenv==1.9.1
+ packaging==25.0
+ playwright==1.52.0
+ pluggy==1.6.0
+ pyee==13.0.0
+ pyright==1.1.400
+ pytest==8.3.5
+ pytest-base-url==2.1.0
+ pytest-playwright==0.7.0
+ pytest-xdist==3.6.1
+ python-dateutil==2.9.0.post0
+ python-slugify==8.0.4
+ requests==2.32.3
+ ruff==0.11.10
+ scubaduck==0.1.0 (from file:///workspace/scubaduck)
+ six==1.17.0
+ text-unidecode==1.3
+ typing-extensions==4.13.2
+ urllib3==2.4.0
+ werkzeug==3.1.3
++ source .venv/bin/activate
+++ '[' -z '' ']'
+++ '[' -n x ']'
+++ SCRIPT_PATH=.venv/bin/activate
+++ '[' .venv/bin/activate = /tmp/UXmEVD-setup_script.sh ']'
+++ deactivate nondestructive
+++ unset -f pydoc
+++ '[' -z '' ']'
+++ '[' -z '' ']'
+++ hash -r
+++ '[' -z '' ']'
+++ unset VIRTUAL_ENV
+++ unset VIRTUAL_ENV_PROMPT
+++ '[' '!' nondestructive = nondestructive ']'
+++ VIRTUAL_ENV=/workspace/scubaduck/.venv
+++ '[' linux-gnu = cygwin ']'
+++ '[' linux-gnu = msys ']'
+++ export VIRTUAL_ENV
+++ '[' -z '' ']'
+++ unset SCRIPT_PATH
+++ _OLD_VIRTUAL_PATH=/root/.cargo/bin:/root/.rbenv/shims:/root/.rbenv/bin:/root/.rbenv/shims:/root/.local/share/swiftly/bin:/root/.bun/bin:/root/.nvm/versions/node/v22.16.0/bin:/root/.pyenv/shims:3441PYENV_ROOT/shims:/root/.pyenv/bin:/usr/local/go/bin:/root/go/bin:/root/.rbenv/bin:/root/.rbenv/shims:/root/.bun/bin:/root/.local/bin:/root/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+++ PATH=/workspace/scubaduck/.venv/bin:/root/.cargo/bin:/root/.rbenv/shims:/root/.rbenv/bin:/root/.rbenv/shims:/root/.local/share/swiftly/bin:/root/.bun/bin:/root/.nvm/versions/node/v22.16.0/bin:/root/.pyenv/shims:3441PYENV_ROOT/shims:/root/.pyenv/bin:/usr/local/go/bin:/root/go/bin:/root/.rbenv/bin:/root/.rbenv/shims:/root/.bun/bin:/root/.local/bin:/root/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+++ export PATH
+++ '[' xscubaduck '!=' x ']'
+++ VIRTUAL_ENV_PROMPT=scubaduck
+++ export VIRTUAL_ENV_PROMPT
+++ '[' -z '' ']'
+++ '[' -z '' ']'
+++ _OLD_VIRTUAL_PS1=
+++ PS1='(scubaduck) '
+++ export PS1
+++ alias pydoc
+++ true
+++ hash -r
++ python -c 'import os; import duckdb; con = duckdb.connect(); con.execute(f"SET http_proxy = '\''{os.getenv("HTTP_PROXY")}'\''"); con.execute("INSTALL '\''sqlite'\'';")'
++ playwright install chromium
Downloading Chromium 136.0.7103.25 (playwright build v1169) from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1169/chromium-linux.zip
167.7 MiB [] 0% 0.0s167.7 MiB [] 0% 25.1s167.7 MiB [] 0% 17.1s167.7 MiB [] 0% 11.3s167.7 MiB [] 0% 7.6s167.7 MiB [] 1% 6.1s167.7 MiB [] 1% 5.0s167.7 MiB [] 2% 4.1s167.7 MiB [] 3% 3.1s167.7 MiB [] 4% 2.8s167.7 MiB [] 5% 2.7s167.7 MiB [] 6% 2.5s167.7 MiB [] 7% 2.3s167.7 MiB [] 9% 2.0s167.7 MiB [] 10% 2.0s167.7 MiB [] 11% 2.0s167.7 MiB [] 12% 1.8s167.7 MiB [] 13% 1.8s167.7 MiB [] 14% 1.7s167.7 MiB [] 15% 1.7s167.7 MiB [] 16% 1.7s167.7 MiB [] 17% 1.7s167.7 MiB [] 18% 1.7s167.7 MiB [] 19% 1.7s167.7 MiB [] 20% 1.7s167.7 MiB [] 21% 1.7s167.7 MiB [] 22% 1.7s167.7 MiB [] 23% 1.7s167.7 MiB [] 24% 1.6s167.7 MiB [] 25% 1.5s167.7 MiB [] 27% 1.5s167.7 MiB [] 28% 1.5s167.7 MiB [] 29% 1.4s167.7 MiB [] 30% 1.4s167.7 MiB [] 32% 1.3s167.7 MiB [] 33% 1.3s167.7 MiB [] 35% 1.2s167.7 MiB [] 36% 1.2s167.7 MiB [] 37% 1.2s167.7 MiB [] 38% 1.1s167.7 MiB [] 39% 1.1s167.7 MiB [] 41% 1.1s167.7 MiB [] 42% 1.0s167.7 MiB [] 44% 1.0s167.7 MiB [] 45% 1.0s167.7 MiB [] 46% 0.9s167.7 MiB [] 47% 0.9s167.7 MiB [] 49% 0.9s167.7 MiB [] 50% 0.9s167.7 MiB [] 51% 0.8s167.7 MiB [] 53% 0.8s167.7 MiB [] 55% 0.7s167.7 MiB [] 56% 0.7s167.7 MiB [] 58% 0.7s167.7 MiB [] 59% 0.7s167.7 MiB [] 60% 0.6s167.7 MiB [] 62% 0.6s167.7 MiB [] 63% 0.6s167.7 MiB [] 64% 0.6s167.7 MiB [] 65% 0.5s167.7 MiB [] 67% 0.5s167.7 MiB [] 68% 0.5s167.7 MiB [] 69% 0.5s167.7 MiB [] 70% 0.5s167.7 MiB [] 71% 0.5s167.7 MiB [] 72% 0.4s167.7 MiB [] 74% 0.4s167.7 MiB [] 75% 0.4s167.7 MiB [] 76% 0.4s167.7 MiB [] 77% 0.4s167.7 MiB [] 78% 0.3s167.7 MiB [] 80% 0.3s167.7 MiB [] 81% 0.3s167.7 MiB [] 82% 0.3s167.7 MiB [] 83% 0.3s167.7 MiB [] 84% 0.2s167.7 MiB [] 85% 0.2s167.7 MiB [] 86% 0.2s167.7 MiB [] 87% 0.2s167.7 MiB [] 89% 0.2s167.7 MiB [] 90% 0.1s167.7 MiB [] 91% 0.1s167.7 MiB [] 92% 0.1s167.7 MiB [] 93% 0.1s167.7 MiB [] 95% 0.1s167.7 MiB [] 97% 0.0s167.7 MiB [] 98% 0.0s167.7 MiB [] 99% 0.0s167.7 MiB [] 100% 0.0s
Chromium 136.0.7103.25 (playwright build v1169) downloaded to /root/.cache/ms-playwright/chromium-1169
Downloading FFMPEG playwright build v1011 from https://cdn.playwright.dev/dbazure/download/playwright/builds/ffmpeg/1011/ffmpeg-linux.zip
2.3 MiB [] 0% 0.0s2.3 MiB [] 4% 0.3s2.3 MiB [] 7% 0.4s2.3 MiB [] 16% 0.2s2.3 MiB [] 32% 0.1s2.3 MiB [] 60% 0.1s2.3 MiB [] 100% 0.0s
FFMPEG playwright build v1011 downloaded to /root/.cache/ms-playwright/ffmpeg-1011
Downloading Chromium Headless Shell 136.0.7103.25 (playwright build v1169) from https://cdn.playwright.dev/dbazure/download/playwright/builds/chromium/1169/chromium-headless-shell-linux.zip
101.4 MiB [] 0% 0.0s101.4 MiB [] 0% 15.6s101.4 MiB [] 0% 19.6s101.4 MiB [] 0% 15.9s101.4 MiB [] 0% 11.1s101.4 MiB [] 1% 7.6s101.4 MiB [] 1% 4.8s101.4 MiB [] 3% 3.6s101.4 MiB [] 5% 2.4s101.4 MiB [] 7% 1.9s101.4 MiB [] 9% 1.7s101.4 MiB [] 10% 1.7s101.4 MiB [] 11% 1.6s101.4 MiB [] 13% 1.5s101.4 MiB [] 15% 1.3s101.4 MiB [] 15% 1.4s101.4 MiB [] 17% 1.3s101.4 MiB [] 19% 1.2s101.4 MiB [] 20% 1.2s101.4 MiB [] 22% 1.2s101.4 MiB [] 23% 1.1s101.4 MiB [] 23% 1.2s101.4 MiB [] 25% 1.2s101.4 MiB [] 27% 1.1s101.4 MiB [] 29% 1.1s101.4 MiB [] 30% 1.0s101.4 MiB [] 32% 1.0s101.4 MiB [] 34% 0.9s101.4 MiB [] 36% 0.9s101.4 MiB [] 37% 0.9s101.4 MiB [] 41% 0.8s101.4 MiB [] 44% 0.7s101.4 MiB [] 45% 0.7s101.4 MiB [] 48% 0.6s101.4 MiB [] 51% 0.6s101.4 MiB [] 53% 0.5s101.4 MiB [] 56% 0.5s101.4 MiB [] 59% 0.4s101.4 MiB [] 62% 0.4s101.4 MiB [] 64% 0.4s101.4 MiB [] 67% 0.3s101.4 MiB [] 68% 0.3s101.4 MiB [] 69% 0.3s101.4 MiB [] 70% 0.3s101.4 MiB [] 72% 0.3s101.4 MiB [] 75% 0.3s101.4 MiB [] 78% 0.2s101.4 MiB [] 80% 0.2s101.4 MiB [] 83% 0.2s101.4 MiB [] 86% 0.1s101.4 MiB [] 89% 0.1s101.4 MiB [] 90% 0.1s101.4 MiB [] 93% 0.1s101.4 MiB [] 96% 0.0s101.4 MiB [] 99% 0.0s101.4 MiB [] 100% 0.0s
Chromium Headless Shell 136.0.7103.25 (playwright build v1169) downloaded to /root/.cache/ms-playwright/chromium_headless_shell-1169
++ echo 'source .venv/bin/activate'
I'll check the root for AGENTS instructions to guide how to approach this task.
bash: .venv/bin/activate: No such file or directory
ls
root@376d6ff56bfb:/workspace# ls
scubaduck
root@376d6ff56bfb:/workspace# cd scubaduck
root@376d6ff56bfb:/workspace/scubaduck# ls
AGENTS.md codex_setup.sh scubaduck tests
README.md pyproject.toml stubs uv.lock
root@376d6ff56bfb:/workspace/scubaduck# cat AGENTS.md
- Use "source .venv/bin/activate" to activate the virtual environment before
running any commands
- ONLY use non-interactive commands like cat, sed, apply_patch to do edits.
Do NOT use interactive editors.
- Do NOT attempt to install packages. Only the packages specified in
pyproject.toml are available. You cannot add new packages. If you
desperately want another package, make a note of it in the final PR
description.
- Use conventional commits to format PR title
- There are no nested AGENTS.md files, this is the only agents file
- When using Playwright, ONLY use chromium browser. This browser is already
installed.
- Use "ruff check" to check lint, "ruff format" to autoformat files and
"pyright" to typecheck.
- When writing the PR description, include the original prompt supplied by the
user.
- When you add functionality to the server, add server tests to
tests/test_server.py. When you add features to the frontend, add frontend
tests to tests/test_web.py
I'll inspect the scubaduck folder for date/time parsing code. Let's see what's there.
root@376d6ff56bfb:/workspace/scubaduck# ls
AGENTS.md codex_setup.sh scubaduck tests
README.md pyproject.toml stubs uv.lock
I’ll open up the scubaduck folder to see what’s inside.
root@376d6ff56bfb:/workspace/scubaduck# ls scubaduck
__init__.py sample.csv server.py static
I’m checking the scubaduck/server.py file for context.
root@376d6ff56bfb:/workspace/scubaduck# sed -n '1,160p' scubaduck/server.py
from __future__ import annotations
from dataclasses import dataclass, field, replace
from typing import Any, Dict, List, Tuple, cast
import re
from datetime import datetime, timedelta, timezone
import time
from pathlib import Path
import os
import traceback
import math
import duckdb
from dateutil import parser as dtparser
from flask import Flask, jsonify, request, send_from_directory
def _quote(ident: str) -> str:
"""Return identifier quoted for SQL."""
return f'"{ident.replace('"', '""')}"'
@dataclass
class Filter:
column: str
op: str
value: str | int | float | list[str] | None
@dataclass
class QueryParams:
start: str | None = None
end: str | None = None
order_by: str | None = None
order_dir: str = "ASC"
limit: int | None = None
columns: list[str] = field(default_factory=lambda: [])
filters: list[Filter] = field(default_factory=lambda: [])
derived_columns: dict[str, str] = field(default_factory=lambda: {})
graph_type: str = "samples"
group_by: list[str] = field(default_factory=lambda: [])
aggregate: str | None = None
show_hits: bool = False
x_axis: str | None = None
granularity: str = "Auto"
fill: str = "0"
table: str = "events"
time_column: str | None = "timestamp"
time_unit: str = "s"
def _load_database(path: Path) -> duckdb.DuckDBPyConnection:
if not path.exists():
raise FileNotFoundError(path)
ext = path.suffix.lower()
if ext == ".csv":
con = duckdb.connect()
con.execute(
f"CREATE TABLE events AS SELECT * FROM read_csv_auto('{path.as_posix
()}')"
)
elif ext in {".db", ".sqlite"}:
con = duckdb.connect()
con.execute("LOAD sqlite")
con.execute(f"ATTACH '{path.as_posix()}' AS db (TYPE SQLITE)")
tables = [
r[0]
for r in con.execute(
"SELECT name FROM sqlite_master WHERE type='table'"
).fetchall()
]
for t in tables:
con.execute(f'CREATE VIEW "{t}" AS SELECT * FROM db."{t}"')
else:
con = duckdb.connect(path)
return con
def _create_test_database() -> duckdb.DuckDBPyConnection:
"""Return a DuckDB connection with a small multi-table dataset."""
con = duckdb.connect()
con.execute(
"CREATE TABLE events (id INTEGER PRIMARY KEY, ts INTEGER, val REAL, name
TEXT, flag BOOLEAN)"
)
con.execute("INSERT INTO events VALUES (1, 1704067200, 1.5, 'alice', 1)")
con.execute("INSERT INTO events VALUES (2, 1704070800, 2.0, 'bob', 0)")
con.execute('CREATE TABLE extra (ts INTEGER, "desc" TEXT, num INTEGER)')
con.execute("INSERT INTO extra VALUES (1704067200, 'x', 1)")
con.execute("INSERT INTO extra VALUES (1704070800, 'y', 2)")
return con
_REL_RE = re.compile(
r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|weeks|fortnight|fortnight
s)",
re.IGNORECASE,
)
def parse_time(val: str | None) -> str | None:
"""Parse an absolute or relative time string into ``YYYY-MM-DD HH:MM:SS``.""
"
if val is None or val == "":
return None
s = val.strip()
if s.lower() == "now":
dt = datetime.now(timezone.utc)
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
m = _REL_RE.fullmatch(s)
if m:
qty = float(m.group(1))
unit = m.group(2).lower()
delta: timedelta
if unit.startswith("hour"):
delta = timedelta(hours=qty)
elif unit.startswith("day"):
delta = timedelta(days=qty)
elif unit.startswith("week"):
delta = timedelta(weeks=qty)
elif unit.startswith("fortnight"):
delta = timedelta(weeks=2 * qty)
else: # pragma: no cover - defensive
raise ValueError(f"Unsupported unit: {unit}")
dt = datetime.now(timezone.utc) + delta
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
dt = dtparser.parse(s)
return dt.replace(microsecond=0, tzinfo=None).strftime("%Y-%m-%d %H:%M:%S")
def _granularity_seconds(granularity: str, start: str | None, end: str | None) -
> int:
gran = granularity.lower()
mapping = {
"1 second": 1,
"5 seconds": 5,
"10 seconds": 10,
"30 seconds": 30,
"1 minute": 60,
"4 minutes": 240,
"5 minutes": 300,
"10 minutes": 600,
"15 minutes": 900,
"30 minutes": 1800,
"1 hour": 3600,
"3 hours": 10800,
"6 hours": 21600,
"1 day": 86400,
"1 week": 604800,
"30 days": 2592000,
}
if gran in mapping:
return mapping[gran]
if gran in {"auto", "fine"} and start and end:
try:
s = dtparser.parse(start)
e = dtparser.parse(end)
except Exception:
return 3600
total = max((e - s).total_seconds(), 1)
I need to check lines from 160 onward in server.py to handle time parsing.
root@376d6ff56bfb:/workspace/scubaduck# sed -n '160,320p' scubaduck/server.py
total = max((e - s).total_seconds(), 1)
buckets = 100 if gran == "auto" else 500
return max(int(total // buckets), 1)
return 3600
def _time_expr(col: str, column_types: Dict[str, str] | None, unit: str) -> str:
"""Return SQL expression for column interpreted as timestamp."""
qcol = _quote(col)
if column_types is None:
return qcol
ctype = column_types.get(col, "").upper()
if not any(t in ctype for t in ["TIMESTAMP", "DATE", "TIME"]):
if any(
t in ctype
for t in [
"INT",
"DECIMAL",
"REAL",
"DOUBLE",
"FLOAT",
"NUMERIC",
"HUGEINT",
]
):
if unit == "ns":
expr = f"CAST({qcol} AS BIGINT)"
return f"make_timestamp_ns({expr})"
multiplier = {
"s": 1_000_000,
"ms": 1_000,
"us": 1,
}.get(unit, 1_000_000)
expr = (
f"CAST({qcol} * {multiplier} AS BIGINT)"
if multiplier != 1
else f"CAST({qcol} AS BIGINT)"
)
return f"make_timestamp({expr})"
return qcol
def build_query(params: QueryParams, column_types: Dict[str, str] | None = None)
-> str:
select_parts: list[str] = []
group_cols = params.group_by[:]
selected_for_order = set(params.columns) | set(params.derived_columns.keys()
)
if params.graph_type == "timeseries":
sec = _granularity_seconds(params.granularity, params.start, params.end)
x_axis = params.x_axis or params.time_column
if x_axis is None:
raise ValueError("x_axis required for timeseries")
xexpr = _time_expr(x_axis, column_types, params.time_unit)
if params.start:
bucket_expr = (
f"TIMESTAMP '{params.start}' + INTERVAL '{sec} second' * "
f"CAST(floor((epoch({xexpr}) - epoch(TIMESTAMP '{params.start}')
)/{sec}) AS BIGINT)"
)
else:
bucket_expr = (
f"TIMESTAMP 'epoch' + INTERVAL '{sec} second' * "
f"CAST(floor(epoch({xexpr})/{sec}) AS BIGINT)"
)
select_parts.append(f"{bucket_expr} AS bucket")
group_cols = ["bucket"] + group_cols
selected_for_order.add("bucket")
has_agg = bool(group_cols) or params.aggregate is not None
if has_agg:
select_cols = (
group_cols[1:] if params.graph_type == "timeseries" else group_cols
)
select_parts.extend(_quote(c) for c in select_cols)
agg = (params.aggregate or "count").lower()
selected_for_order.update(group_cols)
def agg_expr(col: str) -> str:
expr = _quote(col)
ctype = column_types.get(col, "").upper() if column_types else ""
if "BOOL" in ctype:
expr = f"CAST({_quote(col)} AS BIGINT)"
if agg.startswith("p"):
quant = float(agg[1:]) / 100
return f"quantile({expr}, {quant})"
if agg == "count distinct":
return f"count(DISTINCT {expr})"
if agg == "avg" and column_types is not None:
if "TIMESTAMP" in ctype or "DATE" in ctype or "TIME" in ctype:
return (
"TIMESTAMP 'epoch' + INTERVAL '1 second' * "
f"CAST(avg(epoch({_quote(col)})) AS BIGINT)"
)
return f"{agg}({expr})"
if agg == "count":
select_parts.append("count(*) AS Count")
selected_for_order.add("Count")
else:
for col in params.columns:
if col in group_cols:
continue
select_parts.append(f"{agg_expr(col)} AS {_quote(col)}")
selected_for_order.add(col)
if params.show_hits:
select_parts.insert(len(group_cols), "count(*) AS Hits")
selected_for_order.add("Hits")
else:
select_parts.extend(_quote(c) for c in params.columns)
selected_for_order.update(params.columns)
order_by = params.order_by if params.order_by in selected_for_order else Non
e
if has_agg and params.derived_columns:
inner_params = replace(
params,
derived_columns={},
order_by=None,
limit=None,
)
inner_sql = build_query(inner_params, column_types)
outer_select = ["t.*"] + [
f"{expr} AS {name}" for name, expr in params.derived_columns.items()
]
indented_inner = "\n".join(" " + line for line in inner_sql.splitline
s())
lines = [
f"SELECT {', '.join(outer_select)}",
"FROM (",
indented_inner,
") t",
]
if order_by:
lines.append(f"ORDER BY {_quote(order_by)} {params.order_dir}")
elif params.graph_type == "timeseries":
lines.append("ORDER BY bucket")
if params.limit is not None:
lines.append(f"LIMIT {params.limit}")
return "\n".join(lines)
for name, expr in params.derived_columns.items():
select_parts.append(f"{expr} AS {name}")
selected_for_order.add(name)
select_clause = ", ".join(select_parts) if select_parts else "*"
lines = [f"SELECT {select_clause}", f'FROM "{params.table}"']
where_parts: list[str] = []
if params.time_column:
time_expr = _time_expr(params.time_column, column_types, params.time_uni
t)
else:
time_expr = None
if time_expr and params.start:
where_parts.append(f"{time_expr} >= '{params.start}'")
if time_expr and params.end:
where_parts.append(f"{time_expr} <= '{params.end}'")
for f in params.filters:
op = f.op
if op in {"empty", "!empty"}:
val = "''"
else:
if f.value is None:
continue
if isinstance(f.value, list):
if not f.value:
continue
root@376d6ff56bfb:/workspace/scubaduck# sed -n '320,640p' scubaduck/server.py
continue
if op == "=":
qcol = _quote(f.column)
vals = " OR ".join(f"{qcol} = '{v}'" for v in f.value)
where_parts.append(f"({vals})")
continue
val = f"'{f.value}'" if isinstance(f.value, str) else str(f.value)
qcol = _quote(f.column)
if op == "contains":
where_parts.append(f"{qcol} ILIKE '%' || {val} || '%'")
elif op == "!contains":
where_parts.append(f"{qcol} NOT ILIKE '%' || {val} || '%'")
elif op == "empty":
where_parts.append(f"{qcol} = {val}")
elif op == "!empty":
where_parts.append(f"{qcol} != {val}")
else:
where_parts.append(f"{qcol} {op} {val}")
if where_parts:
lines.append("WHERE " + " AND ".join(where_parts))
if group_cols:
lines.append("GROUP BY " + ", ".join(_quote(c) for c in group_cols))
if order_by:
lines.append(f"ORDER BY {_quote(order_by)} {params.order_dir}")
elif params.graph_type == "timeseries":
lines.append("ORDER BY bucket")
if params.limit is not None:
lines.append(f"LIMIT {params.limit}")
return "\n".join(lines)
def create_app(db_file: str | Path | None = None) -> Flask:
app = Flask(__name__, static_folder="static")
if db_file is None:
env_db = os.environ.get("SCUBADUCK_DB")
if env_db:
db_file = env_db
if isinstance(db_file, str) and db_file.upper() == "TEST":
con = _create_test_database()
else:
db_path = Path(db_file or Path(__file__).with_name("sample.csv")).resolv
e()
con = _load_database(db_path)
tables = [r[0] for r in con.execute("SHOW TABLES").fetchall()]
if not tables:
raise ValueError("No tables found in database")
default_table = tables[0]
columns_cache: Dict[str, Dict[str, str]] = {}
def get_columns(table: str) -> Dict[str, str]:
if table not in columns_cache:
rows = con.execute(f'PRAGMA table_info("{table}")').fetchall()
if not rows:
raise ValueError(f"Unknown table: {table}")
columns_cache[table] = {r[1]: r[2] for r in rows}
return columns_cache[table]
sample_cache: Dict[Tuple[str, str, str], Tuple[List[str], float]] = {}
CACHE_TTL = 60.0
CACHE_LIMIT = 200
@app.route("/")
def index() -> Any: # pyright: ignore[reportUnusedFunction]
assert app.static_folder is not None
return send_from_directory(app.static_folder, "index.html")
@app.route("/js/<path:filename>")
def js(filename: str) -> Any: # pyright: ignore[reportUnusedFunction]
assert app.static_folder is not None
folder = Path(app.static_folder) / "js"
return send_from_directory(folder, filename)
@app.route("/api/tables")
def tables_endpoint() -> Any: # pyright: ignore[reportUnusedFunction]
return jsonify(tables)
@app.route("/api/columns")
def columns() -> Any: # pyright: ignore[reportUnusedFunction]
table = request.args.get("table", default_table)
rows = con.execute(f'PRAGMA table_info("{table}")').fetchall()
return jsonify([{"name": r[1], "type": r[2]} for r in rows])
def _cache_get(key: Tuple[str, str, str]) -> List[str] | None:
item = sample_cache.get(key)
if item is None:
return None
vals, ts = item
if time.time() - ts > CACHE_TTL:
del sample_cache[key]
return None
sample_cache[key] = (vals, time.time())
return vals
def _cache_set(key: Tuple[str, str, str], vals: List[str]) -> None:
sample_cache[key] = (vals, time.time())
if len(sample_cache) > CACHE_LIMIT:
oldest = min(sample_cache.items(), key=lambda kv: kv[1][1])[0]
del sample_cache[oldest]
@app.route("/api/samples")
def sample_values() -> Any: # pyright: ignore[reportUnusedFunction]
table = request.args.get("table", default_table)
column = request.args.get("column")
substr = request.args.get("q", "")
column_types = get_columns(table)
if not column or column not in column_types:
return jsonify([])
ctype = column_types[column].upper()
if "CHAR" not in ctype and "STRING" not in ctype and "VARCHAR" not in ct
ype:
return jsonify([])
key = (table, column, substr)
cached = _cache_get(key)
if cached is not None:
return jsonify(cached)
qcol = _quote(column)
rows = con.execute(
f"SELECT DISTINCT {qcol} FROM \"{table}\" WHERE CAST({qcol} AS VARCH
AR) ILIKE '%' || ? || '%' LIMIT 20",
[substr],
).fetchall()
values = [r[0] for r in rows]
...
return jsonify({"error": f"Unknown column: {col}"}), 400
if params.order_by and params.order_by not in valid_cols:
return jsonify({"error": f"Unknown column: {params.order_by}"}), 400
if params.group_by or params.graph_type == "timeseries":
agg = (params.aggregate or "count").lower()
if agg.startswith("p") or agg == "sum":
need_numeric = True
allow_time = False
elif agg == "avg" or agg in {"min", "max"}:
need_numeric = False
allow_time = True
else:
need_numeric = False
allow_time = False
if need_numeric or allow_time:
for c in params.columns:
if c in params.group_by or c == params.x_axis:
continue
if c not in column_types:
continue
ctype = column_types.get(c, "").upper()
is_numeric = any(
t in ctype
for t in [
"INT",
"DECIMAL",
"REAL",
"DOUBLE",
"FLOAT",
"NUMERIC",
"HUGEINT",
]
)
is_time = "TIMESTAMP" in ctype or "DATE" in ctype or "TIME"
in ctype
if need_numeric and not is_numeric:
return (
jsonify(
{
"error": f"Aggregate {agg} cannot be applied
to column {c}",
}
),
400,
)
if allow_time and not (is_numeric or is_time):
return (
jsonify(
{
"error": f"Aggregate {agg} cannot be applied
to column {c}",
}
),
400,
)
if (params.start is None or params.end is None) and (
params.x_axis or params.time_column
):
axis = params.x_axis or params.time_column
assert axis is not None
row = cast(
tuple[datetime | None, datetime | None],
con.execute(
f'SELECT min({_quote(axis)}), max({_quote(axis)}) FROM "{par
ams.table}"'
).fetchall()[0],
)
mn, mx = row
divisor = {
"s": 1,
"ms": 1000,
"us": 1_000_000,
"ns": 1_000_000_000,
}.get(params.time_unit, 1)
if isinstance(mn, (int, float)):
try:
mn = datetime.fromtimestamp(int(mn) / divisor, tz=timezone.u
tc)
except Exception:
return (
jsonify(
{
"error": (
f"Invalid time value {mn} for column {axis}"
f" with time_unit {params.time_unit}"
)
}
),
400,
)
if isinstance(mx, (int, float)):
try:
mx = datetime.fromtimestamp(int(mx) / divisor, tz=timezone.u
tc)
except Exception:
return (
jsonify(
{
"error": (
f"Invalid time value {mx} for column {axis}"
f" with time_unit {params.time_unit}"
)
}
),
400,
)
if params.start is None and mn is not None:
params.start = (
mn.strftime("%Y-%m-%d %H:%M:%S") if not isinstance(mn, str)
else mn
)
if params.end is None and mx is not None:
params.end = (
mx.strftime("%Y-%m-%d %H:%M:%S") if not isinstance(mx, str)
else mx
)
bucket_size: int | None = None
if params.graph_type == "timeseries":
bucket_size = _granularity_seconds(
params.granularity,
params.start if isinstance(params.start, str) else None,
params.end if isinstance(params.end, str) else None,
The current regex only supports hours, days, weeks, and fortnights. I'll extend it for months and years using relativedelta
, which is already a dependency. Let's confirm it's available in the project.
root@376d6ff56bfb:/workspace/scubaduck# cat pyproject.toml
[project]
name = "scubaduck"
version = "0.1.0"
description = "Scuba on DuckDB"
readme = "README.md"
requires-python = ">=3.12"
license = {text = "MIT"}
dependencies = [
"duckdb",
"flask",
"python-dateutil>=2.9.0.post0",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.pytest.ini_options]
addopts = "-n auto --tb=native"
[tool.pyright]
# Pyright configuration with strict settings
include = ["scubaduck", "tests"]
exclude = ["**/__pycache__", "dist"]
stubPath = "stubs"
venvPath = "."
venv = ".venv"
reportMissingImports = true
reportMissingTypeStubs = true
pythonVersion = "3.12"
pythonPlatform = "All"
typeCheckingMode = "strict"
reportUnknownMemberType = true
reportUnknownParameterType = true
reportUnknownVariableType = true
reportUnknownArgumentType = true
reportPrivateImportUsage = true
reportUntypedFunctionDecorator = true
reportFunctionMemberAccess = true
reportIncompatibleMethodOverride = true
[tool.ruff]
target-version = "py312"
[tool.uv]
dev-dependencies = [
"pytest>=8.3.5",
"ruff>=0.11.2",
"pytest-xdist>=3.6.1",
"playwright>=1.52.0",
"pyright>=1.1.350",
"pytest-playwright>=0.7.0",
]
I'll enhance parse_time with relativedelta to handle months and years. Next, tests!
root@376d6ff56bfb:/workspace/scubaduck# ls tests
conftest.py test_server_time.py test_web_graph.py
test_multi_table_web.py test_server_timeseries.py test_web_table.py
test_server.py test_web.py test_web_timeseries.py
test_server_basic.py test_web_basics.py web_utils.py
test_server_db_types.py test_web_chips.py
test_server_errors.py test_web_columns.py
I’ll look for parse_time or anything about Start/End field errors in the code.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "parse_time" -r tests
I'll check some tests to understand the style, especially for parse_time.
root@376d6ff56bfb:/workspace/scubaduck# sed -n '1,160p' tests/test_server_time.p
y
from __future__ import annotations
import json
from pathlib import Path
import pytest
from scubaduck import server
def test_integer_time_column(tmp_path: Path) -> None:
csv_file = tmp_path / "events.csv"
csv_file.write_text("created,event\n1704067200,login\n1704070800,logout\n")
app = server.create_app(csv_file)
client = app.test_client()
payload = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-01 01:00:00",
"order_by": "created",
"columns": ["created", "event"],
"time_column": "created",
}
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"]) == 2
def test_integer_time_unit_ms(tmp_path: Path) -> None:
csv_file = tmp_path / "events.csv"
csv_file.write_text("created,event\n1704067200000,login\n1704070800000,logou
t\n")
app = server.create_app(csv_file)
client = app.test_client()
payload = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-01 01:00:00",
"order_by": "created",
"columns": ["created", "event"],
"time_column": "created",
"time_unit": "ms",
}
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"]) == 2
def test_timeseries_default_xaxis_uses_time_column(tmp_path: Path) -> None:
csv_file = tmp_path / "events.csv"
csv_file.write_text("created,event\n1704067200000,login\n1704070800000,logou
t\n")
app = server.create_app(csv_file)
client = app.test_client()
payload = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-01 01:00:00",
"graph_type": "timeseries",
"granularity": "1 hour",
"columns": ["event"],
"aggregate": "Count",
"time_column": "created",
"time_unit": "ms",
}
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"]) == 2
def test_integer_time_unit_us_default_start_end(tmp_path: Path) -> None:
csv_file = tmp_path / "events.csv"
csv_file.write_text(
"created,event\n1704067200000000,login\n1704070800000000,logout\n"
)
app = server.create_app(csv_file)
client = app.test_client()
payload = {
"table": "events",
"order_by": "created",
"columns": ["created", "event"],
"time_column": "created",
"time_unit": "us",
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert data["start"] == "2024-01-01 00:00:00"
assert data["end"] == "2024-01-01 01:00:00"
assert len(data["rows"]) == 2
def test_sqlite_integer_time_unit_us(tmp_path: Path) -> None:
sqlite_file = tmp_path / "events.sqlite"
import sqlite3
conn = sqlite3.connect(sqlite_file)
conn.execute("CREATE TABLE visits (visit_time INTEGER, event TEXT)")
big_ts = 13384551652000000
conn.execute("INSERT INTO visits VALUES (?, ?)", (big_ts, "foo"))
conn.commit()
conn.close() # pyright: ignore[reportUnknownMemberType, reportAttributeAcce
ssIssue]
app = server.create_app(sqlite_file)
client = app.test_client()
payload = {
"table": "visits",
"start": "2394-02-20 00:00:00",
"end": "2394-02-21 00:00:00",
"order_by": "visit_time",
"columns": ["visit_time", "event"],
"time_column": "visit_time",
"time_unit": "us",
}
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"]) == 1
def test_relative_time_query(monkeypatch: pytest.MonkeyPatch) -> None:
app = server.app
client = app.test_client()
from datetime import datetime
fixed_now = datetime(2024, 1, 2, 4, 0, 0)
class FixedDateTime(datetime):
@classmethod
def now(cls, tz=None): # type: ignore[override]
return fixed_now if tz is None else fixed_now.astimezone(tz)
monkeypatch.setattr(server, "datetime", FixedDateTime)
payload = {
"table": "events",
"start": "-1 hour",
"end": "now",
"order_by": "timestamp",
"limit": 100,
"columns": ["timestamp", "event", "value", "user"],
"filters": [],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
root@376d6ff56bfb:/workspace/scubaduck# sed -n '160,320p' tests/test_server_time
.py
data = rv.get_json()
assert rv.status_code == 200
assert len(data["rows"]) == 1
assert data["rows"][0][3] == "charlie"
def test_default_start_end_returned() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"order_by": "timestamp",
"limit": 5,
"columns": ["timestamp"],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert data["start"] == "2024-01-01 00:00:00"
assert data["end"] == "2024-01-02 03:00:00"
def test_time_column_none_no_time_filter() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"columns": ["timestamp", "event"],
"time_column": "",
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert "start" not in data and "end" not in data
assert len(data["rows"]) == 4
I’ll check tests/test_server_timeseries.py and test_server_basic for time-related tests.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "-1" tests/test_server_timeserie
s.py
^C
I'll give it another try and search once more.
root@376d6ff56bfb:/workspace/scubaduck# grep -r "-1" tests | head -n 20
grep: .venv/bin/ruff: binary file matches
.gitignore-.cache
.gitignore:nosetests.xml
.gitignore-coverage.xml
--
AGENTS.md- user.
AGENTS.md:- When you add functionality to the server, add server tests to
AGENTS.md: tests/test_server.py. When you add features to the frontend, add fr
ontend
AGENTS.md: tests to tests/test_web.py
--
.venv/lib/python3.12/site-packages/_pytest/capture.py-@hookimpl(wrapper=True)
.venv/lib/python3.12/site-packages/_pytest/capture.py:def pytest_load_initial_co
nftests(early_config: Config) -> Generator[None]:
.venv/lib/python3.12/site-packages/_pytest/capture.py- ns = early_config.know
n_args_namespace
--
.venv/lib/python3.12/site-packages/_pytest/capture.py- # TextIOWrapper do
esn't expose a mode, but at least some of our
.venv/lib/python3.12/site-packages/_pytest/capture.py: # tests check it.
.venv/lib/python3.12/site-packages/_pytest/capture.py- return self.buffer
.mode.replace("b", "")
--
.venv/lib/python3.12/site-packages/_pytest/assertion/rewrite.py- for pat
in self.fnpats:
.venv/lib/python3.12/site-packages/_pytest/assertion/rewrite.py: # if
the pattern contains subdirectories ("tests/**.py" for example) we can't bail o
ut based
.venv/lib/python3.12/site-packages/_pytest/assertion/rewrite.py- # on
the name alone because we need to match against the full path
I'll look for where parse_time is used in the code.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "parse_time" -r .
./.venv/lib/python3.12/site-packages/pyright/dist/dist/typeshed-fallback/stubs/b
oltons/boltons/timeutils.pyi:8:def parse_timedelta(text: str) -> timedelta: ...
./.venv/lib/python3.12/site-packages/pyright/dist/dist/typeshed-fallback/stubs/b
oltons/boltons/timeutils.pyi:10:parse_td = parse_timedelta
I'm checking if parse_time is only used within the server. Looking for relevant references.
./.venv/lib/python3.12/site-packages/playwright/_impl/_clock.py:30: "
clockInstall", parse_time(time) if time is not None else {}
./.venv/lib/python3.12/site-packages/playwright/_impl/_clock.py:45: await
self._browser_context._channel.send("clockPauseAt", parse_time(time))
./.venv/lib/python3.12/site-packages/playwright/_impl/_clock.py:62: await
self._browser_context._channel.send("clockSetFixedTime", parse_time(time))
./.venv/lib/python3.12/site-packages/playwright/_impl/_clock.py:69: "
clockSetSystemTime", parse_time(time)
./.venv/lib/python3.12/site-packages/playwright/_impl/_clock.py:73:def parse_tim
e(
./scubaduck/server.py:101:def parse_time(val: str | None) -> str | None:
./scubaduck/server.py:447: start = parse_time(payload.get("start"))
./scubaduck/server.py:448: end = parse_time(payload.get("end"))
root@376d6ff56bfb:/workspace/scubaduck# grep -n "month" -r tests | head
I'll look through the tests for any references to time-related queries.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "relative" -r tests | head
tests/test_web_basics.py:206:def test_relative_dropdown(page: Any, server_url: s
tr) -> None:
tests/test_server_time.py:133:def test_relative_time_query(monkeypatch: pytest.M
onkeyPatch) -> None:
)
assert text_align == "right"
def test_relative_dropdown(page: Any, server_url: str) -> None:
page.goto(server_url)
page.wait_for_selector("#order_by option", state="attached")
btn = page.query_selector('[data-target="start-select"]')
assert btn
btn.click()
page.click("#start-select div:text('-3 hours')")
assert page.input_value("#start") == "-3 hours"
def test_end_dropdown_now(page: Any, server_url: str) -> None:
page.goto(server_url)
page.wait_for_selector("#order_by option", state="attached")
page.click('[data-target="end-select"]')
page.click("#end-select div:text('now')")
assert page.input_value("#end") == "now"
def test_invalid_time_error_shown(page: Any, server_url: str) -> None:
data = run_query(
page,
server_url,
start="nonsense",
end="now",
order_by="user",
)
assert "error" in data
msg = page.text_content("#view")
assert "nonsense" in msg
def test_url_query_persistence(page: Any, server_url: str) -> None:
page.goto(server_url)
page.wait_for_selector("#order_by option", state="attached")
page.wait_for_function("window.lastResults !== undefined")
page.fill("#start", "2024-01-01 00:00:00")
page.fill("#end", "2024-01-02 00:00:00")
page.fill("#limit", "1")
page.evaluate("window.lastResults = undefined")
page.click("text=Dive")
page.wait_for_function("window.lastResults !== undefined")
first_url = page.url
first_rows = page.evaluate("window.lastResults.rows.length")
page.fill("#limit", "2")
page.evaluate("window.lastResults = undefined")
page.click("text=Dive")
page.wait_for_function("window.lastResults !== undefined")
second_url = page.url
second_rows = page.evaluate("window.lastResults.rows.length")
assert second_rows != first_rows
assert first_url != second_url
page.go_back()
page.wait_for_function("window.lastResults !== undefined")
I need to extend parse_time
for months and years, then add corresponding tests.
root@376d6ff56bfb:/workspace/scubaduck# sed -n '1,160p' tests/test_server.py
"""Legacy test module.
Tests have been split into multiple files:
- test_server_basic.py
- test_server_db_types.py
- test_server_time.py
- test_server_errors.py
- test_server_timeseries.py
"""
I'll add a test for parse_time, possibly in test_server_time.py or a new file.
import json
from scubaduck import server
def test_basic_query() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-02 00:00:00",
"order_by": "timestamp",
"order_dir": "ASC",
"limit": 10,
"columns": ["timestamp", "event", "value", "user"],
"filters": [],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert data
rows = data["rows"]
# We expect first three rows (until 2024-01-02 00:00:00)
assert len(rows) == 3
assert rows[0][1] == "login"
assert rows[1][1] == "logout"
def test_js_served() -> None:
app = server.app
client = app.test_client()
rv = client.get("/js/chip_input.js")
assert rv.status_code == 200
assert b"initChipInput" in rv.data
def test_filter_multi_token() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-02 03:00:00",
"order_by": "timestamp",
"limit": 10,
"columns": ["timestamp", "event", "value", "user"],
"filters": [{"column": "user", "op": "=", "value": ["alice", "charlie"]}
],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert data
rows = data["rows"]
# Should only return rows for alice and charlie
assert len(rows) == 3
assert rows[0][3] == "alice"
assert rows[-1][3] == "charlie"
def test_empty_filter_is_noop() -> None:
app = server.app
client = app.test_client()
base_payload = {
"table": "events",
"start": "2024-01-01 00:00:00",
"end": "2024-01-03 00:00:00",
"limit": 100,
"columns": ["timestamp", "event", "value", "user"],
}
no_filter = {**base_payload, "filters": []}
empty_filter = {
**base_payload,
"filters": [{"column": "user", "op": "=", "value": None}],
}
I need to check tests/conftest.py to confirm how the app is being created.
from __future__ import annotations
import threading
from collections.abc import Iterator
import pytest
from werkzeug.serving import make_server
from scubaduck.server import app
@pytest.fixture()
def server_url() -> Iterator[str]:
httpd = make_server("127.0.0.1", 0, app)
port = httpd.server_port
thread = threading.Thread(target=httpd.serve_forever)
thread.start()
try:
yield f"http://127.0.0.1:{port}"
finally:
httpd.shutdown()
thread.join()
I need to find where server.app is defined in the codebase.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "app =" -n scubaduck/server.py
353: app = Flask(__name__, static_folder="static")
681:app = create_app()
I'll check the bottom of server.py for the query endpoint after seeing parse_time.
params.end if isinstance(params.end, str) else None,
)
if (
params.limit is not None
and params.start is not None
and params.end is not None
):
try:
start_dt = dtparser.parse(params.start)
end_dt = dtparser.parse(params.end)
buckets = math.ceil(
(end_dt - start_dt).total_seconds() / bucket_size
)
if buckets > 1:
params.limit *= buckets
except Exception:
pass
sql = build_query(params, column_types)
try:
rows = con.execute(sql).fetchall()
except Exception as exc:
tb = traceback.format_exc()
print(f"Query failed:\n{sql}\n{tb}")
return (
jsonify({"sql": sql, "error": str(exc), "traceback": tb}),
400,
)
result: Dict[str, Any] = {"sql": sql, "rows": rows}
if params.start is not None:
result["start"] = str(params.start)
if params.end is not None:
result["end"] = str(params.end)
if bucket_size is not None:
result["bucket_size"] = bucket_size
return jsonify(result)
return app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
I see parse_time is used in the /api/query endpoint, time to modify it.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "parse_time" -n scubaduck/server
.py
101:def parse_time(val: str | None) -> str | None:
447: start = parse_time(payload.get("start"))
448: end = parse_time(payload.get("end"))
I'll check lines 88 to 120 where we explored the parse_time snippet earlier.
con.execute("INSERT INTO extra VALUES (1704070800, 'y', 2)")
return con
_REL_RE = re.compile(
r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|weeks|fortnight|fortnight
s)",
re.IGNORECASE,
)
def parse_time(val: str | None) -> str | None:
"""Parse an absolute or relative time string into ``YYYY-MM-DD HH:MM:SS``.""
"
if val is None or val == "":
return None
s = val.strip()
if s.lower() == "now":
dt = datetime.now(timezone.utc)
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
m = _REL_RE.fullmatch(s)
if m:
qty = float(m.group(1))
unit = m.group(2).lower()
delta: timedelta
if unit.startswith("hour"):
delta = timedelta(hours=qty)
elif unit.startswith("day"):
delta = timedelta(days=qty)
elif unit.startswith("week"):
delta = timedelta(weeks=qty)
elif unit.startswith("fortnight"):
delta = timedelta(weeks=2 * qty)
else: # pragma: no cover - defensive
raise ValueError(f"Unsupported unit: {unit}")
dt = datetime.now(timezone.utc) + delta
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
dt = dtparser.parse(s)
return dt.replace(microsecond=0, tzinfo=None).strftime("%Y-%m-%d %H:%M:%S")
def _granularity_seconds(granularity: str, start: str | None, end: str | None) -
> int:
I’ll update parse_time
to treat months and years as integers, with fractional values converted to approximate days.
I'll use separate handling for months and years with relativedelta
, and ensure type annotation.
I’ll add tests for months/years with monkeypatch
to mock current time. Refining test setup.
"columns": ["visit_time", "event"],
"time_column": "visit_time",
"time_unit": "us",
}
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"]) == 1
def test_relative_time_query(monkeypatch: pytest.MonkeyPatch) -> None:
app = server.app
client = app.test_client()
from datetime import datetime
fixed_now = datetime(2024, 1, 2, 4, 0, 0)
class FixedDateTime(datetime):
@classmethod
def now(cls, tz=None): # type: ignore[override]
return fixed_now if tz is None else fixed_now.astimezone(tz)
monkeypatch.setattr(server, "datetime", FixedDateTime)
payload = {
"table": "events",
"start": "-1 hour",
"end": "now",
"order_by": "timestamp",
"limit": 100,
"columns": ["timestamp", "event", "value", "user"],
"filters": [],
}
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"]) == 1
assert data["rows"][0][3] == "charlie"
def test_default_start_end_returned() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"order_by": "timestamp",
"limit": 5,
"columns": ["timestamp"],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert data["start"] == "2024-01-01 00:00:00"
assert data["end"] == "2024-01-02 03:00:00"
def test_time_column_none_no_time_filter() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"columns": ["timestamp", "event"],
"time_column": "",
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert "start" not in data and "end" not in data
assert len(data["rows"]) == 4
I'll add a test for relative time by month and year and check the data.
root@376d6ff56bfb:/workspace/scubaduck# head scubaduck/sample.csv
timestamp,event,value,user
2024-01-01 00:00:00,login,10,alice
2024-01-01 01:00:00,logout,20,bob
2024-01-02 00:00:00,login,30,alice
2024-01-02 03:00:00,login,40,charlie
I need to patch the current time and validate the start/end with correct values.
I need to adjust the test to match the behavior of datetime and patch accordingly.
fixed_now = datetime(2024, 1, 2, 4, 0, 0)
class FixedDateTime(datetime):
@classmethod
def now(cls, tz=None): # type: ignore[override]
return fixed_now if tz is None else fixed_now.astimezone(tz)
monkeypatch.setattr(server, "datetime", FixedDateTime)
payload = {
"table": "events",
"start": "-1 hour",
"end": "now",
"order_by": "timestamp",
"limit": 100,
"columns": ["timestamp", "event", "value", "user"],
"filters": [],
}
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"]) == 1
assert data["rows"][0][3] == "charlie"
def test_default_start_end_returned() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"order_by": "timestamp",
"limit": 5,
"columns": ["timestamp"],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert data["start"] == "2024-01-01 00:00:00"
assert data["end"] == "2024-01-02 03:00:00"
def test_time_column_none_no_time_filter() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
I'll patch server.datetime to handle parsing "-1000 month" as desired, updating _REL_RE. Then, I'll test for months/years and consider negative values. I'll review the README for any relevant documentation.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "relative" -n README.md
19:* Start/End - Help text: "Sets the start/end of the time range to query. Can
be any kind of datetime string. For example: 'April 23, 2014' or 'yesterday'." T
he UI for this selector supports both relative selections (now, -1 hour, -3 hour
s, -12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days) as wel
l as specifying an absolute date. The way this field is rendered is there is a
free form text box, a drop down arrow (for the relative selectors), and then a c
alendar button (for date selection).
I'll check out the lines near 14 in the README. Let's see what's there.
I really like Scuba (Meta's internal real-time database system). The distributed
, real-time database part of Scuba is quite difficult (and expensive) to replica
te, but I also really like Scuba's UI for doing queries, and I have found myself
wishing that I have access to it even for "small" databases, e.g., I have a sql
ite dataset I want to explore.
Pivotal ideas:
* Time series by default. In the dedicated "time series" view, there are many fe
atures specifically oriented towards working towards tables that represent event
s that occurred over time: the start, end, compare, aggregate and granularity fi
elds all specially privilege the timestamp field. In fact, you can't log events
to Scuba's backing data store without a timestamp, they always come with one. (S
cuba also supports other views that don't presuppose a time series, but the time
series is the most beloved and well used view.) This is in contrast to typical
software which tries to generalize to arbitrary data first, with time series bei
ng added on later.
* It's all about exploration. Scuba is predicated on the idea that you don't kno
w what you're looking for, that you are going to spend time tweaking queries and
changing filters/grouping as part of an investigation to figure out why a syste
m behaves the way it is. So the filters/comparisons/groupings you want to edit a
re always visible on the left sidebar, with the expectation that you're going to
tweak the query to look at something else. Similarly, all the parameters of you
r query get saved into your URL, so your browser history can double up as a quer
y history / you can easily share a query with someone else. This is contrast to
typical software which is often oriented to making pretty dashboards and reports
. (This function is important too, but it's not what I want in exploration mode!
)
* You can fix data problems in the query editor. It's pretty common to have mess
ed up and ended up with a database that doesn't have exactly the columns you nee
d, or some columns that are corrupted in some way. Scuba has pretty robust suppo
rt for defining custom columns with arbitrary SQL functions, grouping over them
as if they were native functions, and doing so with minimal runtime cost (Scuba
aims to turn around your query in milliseconds!) Having to go and run a huge dat
a pipeline to fix your data is a big impediment to exploration; quick and easy c
ustom columns means you can patch over problems when you're investigating and fi
x them for real later.
We're going to build a exploratory data analysis tool like Scuba for time series
database (i.e., a database with a mandatory timestamp representing the time an
event occurred). We'll use DuckDB as the underlying SQL engine served from a Py
thon server, and render the GUI/results as a webpage with vanilla HTML and JS. W
e'll use choices.js to support token inputs. We define a token input to mean a
text input element where as you type a dropdown displays with valid values, and
if you select one or press enter, the selection turns into a token/chip that can
only be deleted as one unit.
To start, we are going to support one views: samples. The samples view only all
ows you to view individual samples from the database, subject to a filter. Our m
ain UI concept is that there is a left sidebar that is the query editor, and the
right side that shows the view. The sidebar is always visible and defaults to
the query parameters of the current view. After you make changes to the query,
clicking the "Dive" button updates the view. The URL of the page encodes all of
the values of the query (and gets updated when you Dive), so the browser's back
button lets you view previous queries.
The query editor's job is to generate a SQL query, which then is applied on the
database, and then the result visualized according to the view.
Here are the settings you can apply to the query. The help text should show up w
hen you mouse over the field name:
* Start/End - Help text: "Sets the start/end of the time range to query. Can be
any kind of datetime string. For example: 'April 23, 2014' or 'yesterday'." The
UI for this selector supports both relative selections (now, -1 hour, -3 hours,
-12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days) as well a
s specifying an absolute date. The way this field is rendered is there is a fre
e form text box, a drop down arrow (for the relative selectors), and then a cale
ndar button (for date selection).
* Order By - Help text: "Choose a column to sort results by." There is an ASC/D
ESC toggle next to it.
* Limit - Help text: "Choose the maximum number of results to show in the chart
after any aggregations have been applied. For example, a limit of 10 will show
no more than 10 rows for a table, etc."
* Filters - You can create as many filters as you want. You can either write a f
ilter using a UI or manual SQL. In the UI, filter consists of a column name, a r
elation (e.g., =, !=, <, >) and then a text field. The text field is a token inp
ut. It accepts multiple tokens for = relation, in which case we match using an O
R for all options.
There is also a "Columns" tab which lets you view all fields in the table, organ
ized by their type. You can also define derived columns, by specifying a column
name and SQL expression. Derived columns can be used for all parts of the UI, in
cluding filters/group by/etc. Columns have checkboxes indicating if we should SE
LECT them or not. Each selected column shows up in the graph. There is an All/N
one link which can be used to select/deselect all checkboxes.
The query UI constructs a SQL query that intuitively has this form:
```
SELECT column, column, ...,
FROM table
WHERE time >= min-timestamp
AND time <= max-timestamp
[AND condition ...]
ORDER BY aggregate(column)
LIMIT number
```
You should write tests for the server backend, demonstrating that at specific qu
ery values we get back the correct rows of data.
## Running the server
I'll open the rest of the README to look for dates or time references.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "month" README.md
The README doesn't mention months or years, so I'll update it with new relative time info.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "fortnight" -r scubaduck | head
scubaduck/static/index.html:224: <div data-value="-1 fortnight">-1
fortnight</div>
scubaduck/static/index.html:243: <div data-value="-1 fortnight">-1
fortnight</div>
scubaduck/server.py:96: r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|we
eks|fortnight|fortnights)",
scubaduck/server.py:121: elif unit.startswith("fortnight"):
I’m checking lines near 220 in static/index.html to see the dropdown options.
root@376d6ff56bfb:/workspace/scubaduck# sed -n '210,260p' scubaduck/static/index
.html
</select>
</div>
<div class="field">
<label>Start<span class="help" title="Sets the start/end of the time r
ange to query. Can be any kind of datetime string. For example: 'April 23, 2014'
or 'yesterday'.">[?]</span></label>
<div class="rel-box">
<input id="start" type="text" />
<button type="button" class="rel-btn" data-target="start-select">	
660;</button>
<div id="start-select" class="rel-dropdown" data-input="start">
<div data-value="-1 hour">-1 hour</div>
<div data-value="-3 hours">-3 hours</div>
<div data-value="-12 hours">-12 hours</div>
<div data-value="-1 day">-1 day</div>
<div data-value="-3 days">-3 days</div>
<div data-value="-1 week">-1 week</div>
<div data-value="-1 fortnight">-1 fortnight</div>
<div data-value="-30 days">-30 days</div>
<div data-value="-90 days">-90 days</div>
</div>
</div>
</div>
<div class="field">
<label>End<span class="help" title="Sets the start/end of the time ran
ge to query. Can be any kind of datetime string. For example: 'April 23, 2014' o
r 'yesterday'.">[?]</span></label>
<div class="rel-box">
<input id="end" type="text" />
<button type="button" class="rel-btn" data-target="end-select">φ
0;</button>
<div id="end-select" class="rel-dropdown" data-input="end">
<div data-value="now">now</div>
<div data-value="-1 hour">-1 hour</div>
<div data-value="-3 hours">-3 hours</div>
<div data-value="-12 hours">-12 hours</div>
<div data-value="-1 day">-1 day</div>
<div data-value="-3 days">-3 days</div>
<div data-value="-1 week">-1 week</div>
<div data-value="-1 fortnight">-1 fortnight</div>
<div data-value="-30 days">-30 days</div>
<div data-value="-90 days">-90 days</div>
</div>
</div>
</div>
<div class="field">
<label>Order By<span class="help" title="Choose a column to sort resul
ts by.">[?]</span></label>
<select id="order_by"></select>
<button id="order_dir" type="button">ASC \u25B2</button>
</div>
<div class="field">
<label>Limit<span class="help" title="Choose the maximum number of res
ults to show in the chart after any aggregations have been applied. For example,
a limit of 10 will show no more than 10 rows for a table, etc.">[?]</span></lab
el>
<input id="limit" type="number" value="100" />
</div>
<div id="x_axis_field" class="field" style="display:none;">
<label>X-axis</label>
<select id="x_axis"></select>
The user reports an issue with relative time entries like "-1000 months." I'll update server.parse_time
to support parsing these even though the UI doesn't feature months/years. A README update may also help clarify this. No UI changes required.
root@376d6ff56bfb:/workspace/scubaduck# grep -n "hour" README.md
19:* Start/End - Help text: "Sets the start/end of the time range to query. Can
be any kind of datetime string. For example: 'April 23, 2014' or 'yesterday'." T
he UI for this selector supports both relative selections (now, -1 hour, -3 hour
s, -12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days) as wel
l as specifying an absolute date. The way this field is rendered is there is a
free form text box, a drop down arrow (for the relative selectors), and then a c
alendar button (for date selection).
I'll update the README to include months and years in the relative dropdown options. Let’s check the setup and activate the environment with source .venv/bin/activate
.
root@376d6ff56bfb:/workspace/scubaduck# source .venv/bin/activate
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll modify the parse_time function as needed to reflect the required adjustments.
con.execute("INSERT INTO extra VALUES (1704067200, 'x', 1)")
con.execute("INSERT INTO extra VALUES (1704070800, 'y', 2)")
return con
_REL_RE = re.compile(
r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|weeks|fortnight|fortnight
s)",
re.IGNORECASE,
)
def parse_time(val: str | None) -> str | None:
"""Parse an absolute or relative time string into ``YYYY-MM-DD HH:MM:SS``.""
"
if val is None or val == "":
return None
s = val.strip()
if s.lower() == "now":
dt = datetime.now(timezone.utc)
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
m = _REL_RE.fullmatch(s)
if m:
qty = float(m.group(1))
unit = m.group(2).lower()
delta: timedelta
if unit.startswith("hour"):
delta = timedelta(hours=qty)
elif unit.startswith("day"):
delta = timedelta(days=qty)
elif unit.startswith("week"):
delta = timedelta(weeks=qty)
elif unit.startswith("fortnight"):
delta = timedelta(weeks=2 * qty)
else: # pragma: no cover - defensive
raise ValueError(f"Unsupported unit: {unit}")
dt = datetime.now(timezone.utc) + delta
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
dt = dtparser.parse(s)
return dt.replace(microsecond=0, tzinfo=None).strftime("%Y-%m-%d %H:%M:%S")
def _granularity_seconds(granularity: str, start: str | None, end: str | None) -
> int:
gran = granularity.lower()
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# apply_patch<<'PATCH'
> *** Begin Patch
> *** Update File: scubaduck/server.py
> @@
> -_REL_RE = re.compile(
> - r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|weeks|fortnight|fortni
ghts)",
> - re.IGNORECASE,
> -)
> +_REL_RE = re.compile(
> + r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|weeks|fortnight|fortni
ghts|month|months|year|years)",
> + re.IGNORECASE,
> +)
> *** End Patch
> PATCH
Success. Updated the following files:
M scubaduck/server.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll go ahead and adjust the parse_time function as needed.
aduck/server.py
101:def parse_time(val: str | None) -> str | None:
447: start = parse_time(payload.get("start"))
448: end = parse_time(payload.get("end"))
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# sed -n '101,130p' scubaduck/
server.py
def parse_time(val: str | None) -> str | None:
"""Parse an absolute or relative time string into ``YYYY-MM-DD HH:MM:SS``.""
"
if val is None or val == "":
return None
s = val.strip()
if s.lower() == "now":
dt = datetime.now(timezone.utc)
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
m = _REL_RE.fullmatch(s)
if m:
qty = float(m.group(1))
unit = m.group(2).lower()
delta: timedelta
if unit.startswith("hour"):
delta = timedelta(hours=qty)
elif unit.startswith("day"):
delta = timedelta(days=qty)
elif unit.startswith("week"):
delta = timedelta(weeks=qty)
elif unit.startswith("fortnight"):
delta = timedelta(weeks=2 * qty)
else: # pragma: no cover - defensive
raise ValueError(f"Unsupported unit: {unit}")
dt = datetime.now(timezone.utc) + delta
return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
dt = dtparser.parse(s)
return dt.replace(microsecond=0, tzinfo=None).strftime("%Y-%m-%d %H:%M:%S")
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I need to add from dateutil.relativedelta import relativedelta
in the server.py imports.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# grep -n "relativedelta" scub
aduck/server.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I’ll add from dateutil.relativedelta import relativedelta
alongside the other imports at the top.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# sed -n '1,30p' scubaduck/ser
ver.py
from __future__ import annotations
from dataclasses import dataclass, field, replace
from typing import Any, Dict, List, Tuple, cast
import re
from datetime import datetime, timedelta, timezone
import time
from pathlib import Path
import os
import traceback
import math
import duckdb
from dateutil import parser as dtparser
from flask import Flask, jsonify, request, send_from_directory
def _quote(ident: str) -> str:
"""Return identifier quoted for SQL."""
return f'"{ident.replace('"', '""')}"'
@dataclass
class Filter:
column: str
op: str
value: str | int | float | list[str] | None
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll add from dateutil.relativedelta import relativedelta
after dtparser
import and patch.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# apply_patch<<'PATCH'
> *** Begin Patch
> *** Update File: scubaduck/server.py
> @@
> -from dateutil import parser as dtparser
> +from dateutil import parser as dtparser
> +from dateutil.relativedelta import relativedelta
> *** End Patch
> PATCH
Success. Updated the following files:
M scubaduck/server.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll go ahead and modify the parse_time function as needed.
> *** Update File: scubaduck/server.py
> @@
> - m = _REL_RE.fullmatch(s)
> - if m:
> - qty = float(m.group(1))
> - unit = m.group(2).lower()
> - delta: timedelta
> - if unit.startswith("hour"):
> - delta = timedelta(hours=qty)
> - elif unit.startswith("day"):
> - delta = timedelta(days=qty)
> - elif unit.startswith("week"):
> - delta = timedelta(weeks=qty)
> - elif unit.startswith("fortnight"):
> - delta = timedelta(weeks=2 * qty)
> - else: # pragma: no cover - defensive
> - raise ValueError(f"Unsupported unit: {unit}")
> - dt = datetime.now(timezone.utc) + delta
> - return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
> + m = _REL_RE.fullmatch(s)
> + if m:
> + qty = float(m.group(1))
> + unit = m.group(2).lower()
> + now = datetime.now(timezone.utc)
> + dt: datetime
> + if unit.startswith("hour"):
> + dt = now + timedelta(hours=qty)
> + elif unit.startswith("day"):
> + dt = now + timedelta(days=qty)
> + elif unit.startswith("week"):
> + dt = now + timedelta(weeks=qty)
> + elif unit.startswith("fortnight"):
> + dt = now + timedelta(weeks=2 * qty)
> + elif unit.startswith("month"):
> + if qty.is_integer():
> + dt = now + relativedelta(months=int(qty))
> + else:
> + dt = now + timedelta(days=30 * qty)
> + elif unit.startswith("year"):
> + if qty.is_integer():
> + dt = now + relativedelta(years=int(qty))
> + else:
> + dt = now + timedelta(days=365 * qty)
> + else: # pragma: no cover - defensive
> + raise ValueError(f"Unsupported unit: {unit}")
> + return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
> *** End Patch
> PATCH
Success. Updated the following files:
M scubaduck/server.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll go ahead and run ruff to format the code.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# ruff format scubaduck/server
.py
1 file left unchanged
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll run a ruff check on the code now.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# ruff check scubaduck/server.
py
All checks passed!
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# pyright scubaduck/server.py
> /tmp/pyright.log && tail -n 20 /tmp/pyright.log
0 errors, 0 warnings, 0 informations
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# cat /tmp/pyright.log
0 errors, 0 warnings, 0 informations
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll open tests/test_server_time.py to integrate the necessary test now.
assert len(data["rows"]) == 1
def test_relative_time_query(monkeypatch: pytest.MonkeyPatch) -> None:
app = server.app
client = app.test_client()
from datetime import datetime
fixed_now = datetime(2024, 1, 2, 4, 0, 0)
class FixedDateTime(datetime):
@classmethod
def now(cls, tz=None): # type: ignore[override]
return fixed_now if tz is None else fixed_now.astimezone(tz)
monkeypatch.setattr(server, "datetime", FixedDateTime)
payload = {
"table": "events",
"start": "-1 hour",
"end": "now",
"order_by": "timestamp",
"limit": 100,
"columns": ["timestamp", "event", "value", "user"],
"filters": [],
}
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"]) == 1
assert data["rows"][0][3] == "charlie"
def test_default_start_end_returned() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
"order_by": "timestamp",
"limit": 5,
"columns": ["timestamp"],
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert data["start"] == "2024-01-01 00:00:00"
assert data["end"] == "2024-01-02 03:00:00"
def test_time_column_none_no_time_filter() -> None:
app = server.app
client = app.test_client()
payload = {
"table": "events",
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# sed -n '188,260p' tests/test
_server_time.py
"table": "events",
"columns": ["timestamp", "event"],
"time_column": "",
}
rv = client.post(
"/api/query", data=json.dumps(payload), content_type="application/json"
)
data = rv.get_json()
assert rv.status_code == 200
assert "start" not in data and "end" not in data
assert len(data["rows"]) == 4
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll create two tests: one for "-1 month" and one for "-1 year" relative time.
I'll handle the date difference using relativedelta or 365 days, then patch the implementation.
> *** Update File: tests/test_server_time.py
> @@
> def test_relative_time_query(monkeypatch: pytest.MonkeyPatch) -> None:
> @@
> assert len(data["rows"]) == 1
> assert data["rows"][0][3] == "charlie"
> +
> +
> +def test_relative_month_year(monkeypatch: pytest.MonkeyPatch) -> None:
> + app = server.app
> + client = app.test_client()
> +
> + from datetime import datetime
> +
> + fixed_now = datetime(2024, 1, 2, 0, 0, 0)
> +
> + class FixedDateTime(datetime):
> + @classmethod
> + def now(cls, tz=None): # type: ignore[override]
> + return fixed_now if tz is None else fixed_now.astimezone(tz)
> +
> + monkeypatch.setattr(server, "datetime", FixedDateTime)
> +
> + payload = {
> + "table": "events",
> + "start": "-1 year",
> + "end": "-1 month",
> + "order_by": "timestamp",
> + "limit": 10,
> + "columns": ["timestamp"],
> + }
> + rv = client.post(
> + "/api/query", data=json.dumps(payload), content_type="application/jso
n"
> + )
> + data = rv.get_json()
> + assert rv.status_code == 200
> + assert data["start"] == "2023-01-02 00:00:00"
> + assert data["end"] == "2023-12-02 00:00:00"
> *** End Patch
> PATCH
Success. Updated the following files:
M tests/test_server_time.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll format the test file using ruff.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# ruff format tests/test_serve
r_time.py
1 file left unchanged
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll run a ruff check to see if all tests are in place.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# ruff check tests/test_server
_time.py
All checks passed!
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll run pyright to check the types in tests/test_server_time.py.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# pyright tests/test_server_ti
me.py > /tmp/pyright.log && tail -n 20 /tmp/pyright.log
0 errors, 0 warnings, 0 informations
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# cat /tmp/pyright.log
0 errors, 0 warnings, 0 informations
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll add months/years to the README and update the relative time dropdown section.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# nl -ba README.md | sed -n '1
5,25p'
15 The query editor's job is to generate a SQL query, which then is applied
on the database, and then the result visualized according to the view.
16
17 Here are the settings you can apply to the query. The help text should s
how up when you mouse over the field name:
18
19 * Start/End - Help text: "Sets the start/end of the time range to query.
Can be any kind of datetime string. For example: 'April 23, 2014' or 'yesterday
'." The UI for this selector supports both relative selections (now, -1 hour, -3
hours, -12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days) a
s well as specifying an absolute date. The way this field is rendered is there
is a free form text box, a drop down arrow (for the relative selectors), and the
n a calendar button (for date selection).
20 * Order By - Help text: "Choose a column to sort results by." There is
an ASC/DESC toggle next to it.
21 * Limit - Help text: "Choose the maximum number of results to show in th
e chart after any aggregations have been applied. For example, a limit of 10 wi
ll show no more than 10 rows for a table, etc."
22 * Filters - You can create as many filters as you want. You can either w
rite a filter using a UI or manual SQL. In the UI, filter consists of a column n
ame, a relation (e.g., =, !=, <, >) and then a text field. The text field is a t
oken input. It accepts multiple tokens for = relation, in which case we match us
ing an OR for all options.
23
24 There is also a "Columns" tab which lets you view all fields in the tabl
e, organized by their type. You can also define derived columns, by specifying a
column name and SQL expression. Derived columns can be used for all parts of th
e UI, including filters/group by/etc. Columns have checkboxes indicating if we s
hould SELECT them or not. Each selected column shows up in the graph. There is
an All/None link which can be used to select/deselect all checkboxes.
25
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I’ll add -1 month, -3 months, and -1 year to the list of options.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# apply_patch<<'PATCH'
> *** Begin Patch
> *** Update File: README.md
> @@
> -* Start/End - Help text: "Sets the start/end of the time range to query. Can
be any kind of datetime string. For example: 'April 23, 2014' or 'yesterday'." T
he UI for this selector supports both relative selections (now, -1 hour, -3 hour
s, -12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days) as wel
l as specifying an absolute date. The way this field is rendered is there is a
free form text box, a drop down arrow (for the relative selectors), and then a c
alendar button (for date selection).
> +* Start/End - Help text: "Sets the start/end of the time range to query. Can
be any kind of datetime string. For example: 'April 23, 2014' or 'yesterday'." T
he UI for this selector supports both relative selections (now, -1 hour, -3 hour
s, -12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days, -1 mon
th, -1 year) as well as specifying an absolute date. The way this field is rend
ered is there is a free form text box, a drop down arrow (for the relative selec
tors), and then a calendar button (for date selection).
> *** End Patch
> PATCH
Success. Updated the following files:
M README.md
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I don't need to format the README for Markdown, but I might format the repo.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# ruff format README.md
error: Failed to parse README.md:1:3: Simple statements must be separated by new
lines or semicolons
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
Since it's not a Python file, I'll skip the format check but run Ruff check.
...
semicolons
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:24: SyntaxError: Compound statements are not allowed on the same li
ne as simple statements
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:32: SyntaxError: Expected 'in', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:39: SyntaxError: Expected ':', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:62: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:67: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:70: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
| ^
39 |
40 | ## Running the server
|
README.md:38:79: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
|
^
39 |
40 | ## Running the server
|
README.md:38:85: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
|
^
39 |
40 | ## Running the server
|
README.md:38:92: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
|
^
39 |
40 | ## Running the server
|
README.md:38:95: SyntaxError: Expected ',', found name
|
36 | ```
37 |
38 | You should write tests for the server backend, demonstrating that at specif
ic query values we get back the correct rows of data.
|
^
39 |
40 | ## Running the server
...
52 | not exist, the server will raise a `FileNotFoundError` during startup.
|
README.md:51:64: SyntaxError: Expected ',', found name
|
49 | variable to point at a different database file (CSV, SQLite or DuckDB) if y
ou
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
| ^
52 | not exist, the server will raise a `FileNotFoundError` during startup.
|
README.md:51:68: SyntaxError: Expected ',', found name
|
49 | variable to point at a different database file (CSV, SQLite or DuckDB) if y
ou
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
| ^
52 | not exist, the server will raise a `FileNotFoundError` during startup.
|
README.md:51:73: SyntaxError: Expected ',', found name
|
49 | variable to point at a different database file (CSV, SQLite or DuckDB) if y
ou
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
| ^
52 | not exist, the server will raise a `FileNotFoundError` during startup.
|
README.md:51:77: SyntaxError: Expected ',', found newline
|
49 | variable to point at a different database file (CSV, SQLite or DuckDB) if y
ou
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
|
^
52 | not exist, the server will raise a `FileNotFoundError` during startup.
|
README.md:52:16: SyntaxError: Expected ',', found name
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
README.md:52:23: SyntaxError: Expected ',', found name
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
README.md:52:28: SyntaxError: Expected ':', found 'raise'
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
README.md:52:36: SyntaxError: Got unexpected token `
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
README.md:52:54: SyntaxError: Got unexpected token `
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
README.md:52:63: SyntaxError: Simple statements must be separated by newlines or
semicolons
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
README.md:52:71: SyntaxError: Expected an identifier
|
50 | want to use another dataset. The special value `TEST` starts the server wit
h a
51 | small in-memory SQLite dataset used by the automated tests. If the file doe
s
52 | not exist, the server will raise a `FileNotFoundError` during startup.
| ^
|
Found 644 errors.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
Ruff check failed on README since it doesn't support Markdown. I'll run it for the repo instead.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# ruff check
All checks passed!
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# pyright > /tmp/pyright.log &
& tail -n 20 /tmp/pyright.log
0 errors, 0 warnings, 0 informations
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# cat /tmp/pyright.log
0 errors, 0 warnings, 0 informations
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# pytest -q
bringing up nodes...
........................................................................ [ 64%]
........................................ [100%]
112 passed in 18.06s
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
Tests passed! Now I’ll check the git status before committing.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# git status --short
M README.md
M scubaduck/server.py
M tests/test_server_time.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# git add README.md scubaduck/
server.py tests/test_server_time.py
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# git commit -m "feat(server):
support month and year relative times" && git status --short
[work 541093a] feat(server): support month and year relative times
3 files changed, 51 insertions(+), 8 deletions(-)
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# git status --short
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll reference the new _REL_RE pattern lines and capture relevant README bullet points.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# nl -ba README.md | sed -n '1
8,20p'
18
19 * Start/End - Help text: "Sets the start/end of the time range to query.
Can be any kind of datetime string. For example: 'April 23, 2014' or 'yesterday
'." The UI for this selector supports both relative selections (now, -1 hour, -3
hours, -12 hours, -1 day, -3 days, -1 week, -1 fortnight, -30 days, -90 days, -
1 month, -1 year) as well as specifying an absolute date. The way this field is
rendered is there is a free form text box, a drop down arrow (for the relative
selectors), and then a calendar button (for date selection).
20 * Order By - Help text: "Choose a column to sort results by." There is
an ASC/DESC toggle next to it.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll grab the lines where parse_time is used to capture the relevant section.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# nl -ba scubaduck/server.py |
sed -n '90,132p'
90 con.execute('CREATE TABLE extra (ts INTEGER, "desc" TEXT, num INTEGE
R)')
91 con.execute("INSERT INTO extra VALUES (1704067200, 'x', 1)")
92 con.execute("INSERT INTO extra VALUES (1704070800, 'y', 2)")
93 return con
94
95
96 _REL_RE = re.compile(
97 r"([+-]?\d+(?:\.\d*)?)\s*(hour|hours|day|days|week|weeks|fortnight|f
ortnights|month|months|year|years)",
98 re.IGNORECASE,
99 )
100
101
102 def parse_time(val: str | None) -> str | None:
103 """Parse an absolute or relative time string into ``YYYY-MM-DD HH:MM
:SS``."""
104 if val is None or val == "":
105 return None
106 s = val.strip()
107 if s.lower() == "now":
108 dt = datetime.now(timezone.utc)
109 return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
110
111 m = _REL_RE.fullmatch(s)
112 if m:
113 qty = float(m.group(1))
114 unit = m.group(2).lower()
115 now = datetime.now(timezone.utc)
116 dt: datetime
117 if unit.startswith("hour"):
118 dt = now + timedelta(hours=qty)
119 elif unit.startswith("day"):
120 dt = now + timedelta(days=qty)
121 elif unit.startswith("week"):
122 dt = now + timedelta(weeks=qty)
123 elif unit.startswith("fortnight"):
124 dt = now + timedelta(weeks=2 * qty)
125 elif unit.startswith("month"):
126 if qty.is_integer():
127 dt = now + relativedelta(months=int(qty))
128 else:
129 dt = now + timedelta(days=30 * qty)
130 elif unit.startswith("year"):
131 if qty.is_integer():
132 dt = now + relativedelta(years=int(qty))
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# nl -ba scubaduck/server.py |
sed -n '132,152p'
132 dt = now + relativedelta(years=int(qty))
133 else:
134 dt = now + timedelta(days=365 * qty)
135 else: # pragma: no cover - defensive
136 raise ValueError(f"Unsupported unit: {unit}")
137 return dt.replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
138
139 dt = dtparser.parse(s)
140 return dt.replace(microsecond=0, tzinfo=None).strftime("%Y-%m-%d %H:
%M:%S")
141
142
143 def _granularity_seconds(granularity: str, start: str | None, end: str |
None) -> int:
144 gran = granularity.lower()
145 mapping = {
146 "1 second": 1,
147 "5 seconds": 5,
148 "10 seconds": 10,
149 "30 seconds": 30,
150 "1 minute": 60,
151 "4 minutes": 240,
152 "5 minutes": 300,
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I'll find the lines related to the new test.
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# nl -ba tests/test_server_tim
e.py | sed -n '134,188p'
134 app = server.app
135 client = app.test_client()
136
137 from datetime import datetime
138
139 fixed_now = datetime(2024, 1, 2, 4, 0, 0)
140
141 class FixedDateTime(datetime):
142 @classmethod
143 def now(cls, tz=None): # type: ignore[override]
144 return fixed_now if tz is None else fixed_now.astimezone(tz)
145
146 monkeypatch.setattr(server, "datetime", FixedDateTime)
147
148 payload = {
149 "table": "events",
150 "start": "-1 hour",
151 "end": "now",
152 "order_by": "timestamp",
153 "limit": 100,
154 "columns": ["timestamp", "event", "value", "user"],
155 "filters": [],
156 }
157 rv = client.post(
158 "/api/query", data=json.dumps(payload), content_type="applicatio
n/json"
159 )
160 data = rv.get_json()
161 assert rv.status_code == 200
162 assert len(data["rows"]) == 1
163 assert data["rows"][0][3] == "charlie"
164
165
166 def test_relative_month_year(monkeypatch: pytest.MonkeyPatch) -> None:
167 app = server.app
168 client = app.test_client()
169
170 from datetime import datetime
171
172 fixed_now = datetime(2024, 1, 2, 0, 0, 0)
173
174 class FixedDateTime(datetime):
175 @classmethod
176 def now(cls, tz=None): # type: ignore[override]
177 return fixed_now if tz is None else fixed_now.astimezone(tz)
178
179 monkeypatch.setattr(server, "datetime", FixedDateTime)
180
181 payload = {
182 "table": "events",
183 "start": "-1 year",
184 "end": "-1 month",
185 "order_by": "timestamp",
186 "limit": 10,
187 "columns": ["timestamp"],
188 }
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck# nl -ba tests/test_server_tim
e.py | sed -n '188,220p'
188 }
189 rv = client.post(
190 "/api/query", data=json.dumps(payload), content_type="applicatio
n/json"
191 )
192 data = rv.get_json()
193 assert rv.status_code == 200
194 assert data["start"] == "2023-01-02 00:00:00"
195 assert data["end"] == "2023-12-02 00:00:00"
196
197
198 def test_default_start_end_returned() -> None:
199 app = server.app
200 client = app.test_client()
201 payload = {
202 "table": "events",
203 "order_by": "timestamp",
204 "limit": 5,
205 "columns": ["timestamp"],
206 }
207 rv = client.post(
208 "/api/query", data=json.dumps(payload), content_type="applicatio
n/json"
209 )
210 data = rv.get_json()
211 assert rv.status_code == 200
212 assert data["start"] == "2024-01-01 00:00:00"
213 assert data["end"] == "2024-01-02 03:00:00"
214
215
216 def test_time_column_none_no_time_filter() -> None:
217 app = server.app
218 client = app.test_client()
219 payload = {
220 "table": "events",
(scubaduck) root@376d6ff56bfb:/workspace/scubaduck#
I’m drafting the PR with the commit message and reference lines. Let’s wrap it!