Skip to content
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
14 changes: 8 additions & 6 deletions raydar/dashboard/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
_TEMPORAL_TYPES = ("datetime", "date")


def _to_epoch_millis(value):
def _to_epoch_millis(value, kind):
"""Perspective encodes rows as JSON, which has no datetime, so send millis."""
if isinstance(value, datetime):
return int(value.timestamp() * 1000)
if isinstance(value, date):
return int(datetime.combine(value, time()).timestamp() * 1000)
return int(datetime.combine(value, time(), tzinfo=UTC if kind == "date" else None).timestamp() * 1000)
return value


Expand All @@ -63,12 +63,13 @@ def new_table(self, tablename: str, schema: dict) -> bool:
"""Create a table, returning whether it did not already exist."""
if tablename in self._schemas:
return False
self._schemas[tablename] = schema
self._temporal_columns[tablename] = {column for column, kind in schema.items() if kind in _TEMPORAL_TYPES}
kwargs = {"name": tablename}
if self._limit is not None:
kwargs["limit"] = self._limit
self._tables[tablename] = self._client.table(schema, **kwargs)
table = self._client.table(schema, **kwargs)
self._schemas[tablename] = schema
self._temporal_columns[tablename] = {column for column, kind in schema.items() if kind in _TEMPORAL_TYPES}
self._tables[tablename] = table
return True

def update(self, tablename: str, data) -> None:
Expand All @@ -78,7 +79,8 @@ def update(self, tablename: str, data) -> None:
raise KeyError(f"No such table: {tablename}")
temporal = self._temporal_columns[tablename]
if temporal:
data = [{key: _to_epoch_millis(value) if key in temporal else value for key, value in row.items()} for row in data]
schema = self._schemas[tablename]
data = [{key: _to_epoch_millis(value, schema[key]) if key in temporal else value for key, value in row.items()} for row in data]
self._tables[tablename].update(data)

def clear(self, tablename: str) -> None:
Expand Down
72 changes: 71 additions & 1 deletion raydar/tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from datetime import UTC, date, datetime

import pytest
from perspective import PerspectiveError
from starlette.testclient import TestClient

from raydar.dashboard import Dashboard, LocalDashboard, default_layout
from raydar.dashboard import Dashboard, LocalDashboard, TableHost, default_layout

SCHEMA = {"a": "integer", "b": "string"}

Expand All @@ -16,6 +17,20 @@ def dashboard():
return Dashboard(title="test")


@pytest.fixture(params=["UTC0", "UTC-8", "UTC+5"] if hasattr(time, "tzset") else [None])
def local_timezone(request, monkeypatch):
if request.param is None:
yield
return
try:
with monkeypatch.context() as patch:
patch.setenv("TZ", request.param)
time.tzset()
yield
finally:
time.tzset()


class TestDefaultLayout:
def test_no_tables_yields_an_empty_layout(self):
assert default_layout([]) == {}
Expand All @@ -27,6 +42,30 @@ def test_each_table_gets_a_widget_and_a_viewer(self):
assert layout["panels"]["x"]["table"] == "x"


class TestTableHost:
def test_failed_table_creation_does_not_register_metadata(self):
host = TableHost()
with pytest.raises(PerspectiveError):
host.new_table("t", {"a": "not-a-perspective-type"})

assert host.names() == []
assert "t" not in host._tables
assert "t" not in host._temporal_columns

def test_corrected_schema_can_retry_after_failed_table_creation(self):
host = TableHost()
with pytest.raises(PerspectiveError):
host.new_table("t", {"a": "not-a-perspective-type"})

assert host.new_table("t", SCHEMA)
host.update("t", [{"a": 1, "b": "x"}])
view = host._tables["t"].view()
try:
assert view.to_columns() == {"a": [1], "b": ["x"]}
finally:
view.delete()


class TestDashboard:
def test_apply_creates_tables_and_counts_rows(self, dashboard):
dashboard.apply({"schemas": {"t": SCHEMA}, "updates": {"t": [{"a": 1, "b": "x"}]}})
Expand Down Expand Up @@ -81,6 +120,17 @@ def test_a_failed_batch_still_syncs_what_landed(self, dashboard):
assert dashboard.state.tables == ["t"]
assert dashboard.state.rows == "1"

def test_failed_table_creation_does_not_publish_a_phantom_tab(self, dashboard):
with pytest.raises(PerspectiveError):
dashboard.apply({"schemas": {"existing": SCHEMA, "t": {"a": "not-a-perspective-type"}}})

assert dashboard.state.tables == ["existing"]
assert dashboard.tables.names() == ["existing"]

dashboard.apply({"schemas": {"t": SCHEMA}, "updates": {"existing": [{"a": 1, "b": "x"}], "t": [{"a": 2, "b": "y"}]}})
assert dashboard.state.tables == ["existing", "t"]
assert dashboard.state.rows == "2"

def test_limit_caps_retained_rows(self):
dashboard = Dashboard(limit=2)
dashboard.apply({"schemas": {"t": SCHEMA}, "updates": {"t": [{"a": i, "b": "x"} for i in range(5)]}})
Expand Down Expand Up @@ -110,6 +160,26 @@ def test_datetime_values_are_accepted(self, dashboard):
stored = dashboard.tables._tables["t"].view().to_columns()["when"][0]
assert stored == int(moment.timestamp() * 1000)

def test_date_columns_preserve_calendar_days(self, dashboard, local_timezone):
days = [date(2026, 8, 21), date(2024, 2, 29), date(1969, 12, 31)]
dashboard.apply({"schemas": {"t": {"day": "date"}}, "updates": {"t": [{"day": day} for day in days]}})

view = dashboard.tables._tables["t"].view()
try:
stored = view.to_columns()["day"]
finally:
view.delete()
assert [datetime.fromtimestamp(value / 1000, UTC).date() for value in stored] == days

def test_plain_dates_in_datetime_columns_use_local_midnight(self, dashboard, local_timezone):
dashboard.apply({"schemas": {"t": {"when": "datetime"}}, "updates": {"t": [{"when": date(2026, 8, 21)}]}})

view = dashboard.tables._tables["t"].view()
try:
assert view.to_columns()["when"] == [int(datetime(2026, 8, 21).timestamp() * 1000)]
finally:
view.delete()


class TestDashboardApp:
def test_page_and_assets_are_served(self, dashboard):
Expand Down