Skip to content

Commit 06b0b72

Browse files
committed
Add workspace_health_lines for notebook UI status
1 parent dcd4d31 commit 06b0b72

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

hotdata_core_notebook/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Hotdata client and shared models for notebook integrations."""
22

33
from hotdata_core_notebook.client import HotdataClient, from_env
4+
from hotdata_core_notebook.health import workspace_health_lines
45
from hotdata_core_notebook.env import (
56
default_api_key,
67
default_host,
@@ -15,6 +16,7 @@
1516
__all__ = [
1617
"HotdataClient",
1718
"QueryResult",
19+
"workspace_health_lines",
1820
"default_api_key",
1921
"default_host",
2022
"default_session_id",

hotdata_core_notebook/health.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import annotations
2+
3+
from hotdata.exceptions import ApiException
4+
5+
from hotdata_core_notebook.client import HotdataClient
6+
7+
8+
def workspace_health_lines(client: HotdataClient) -> tuple[bool, list[str]]:
9+
"""Return ``(ok, parts)`` where ``parts`` are short markdown fragments.
10+
11+
On failure, ``ok`` is False and ``parts`` is a single-element list with the error text.
12+
"""
13+
try:
14+
listing = client.connections().list_connections()
15+
n = len(listing.connections)
16+
lines = [
17+
"**API** reachable",
18+
f"**workspace** `{client.workspace_id}`",
19+
f"**connections** {n}",
20+
]
21+
if client.session_id:
22+
lines.append(f"**sandbox** `{client.session_id}`")
23+
return True, lines
24+
except ApiException as e:
25+
return False, [e.reason or str(e)]

tests/test_health.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import annotations
2+
3+
from unittest.mock import patch
4+
5+
from hotdata.exceptions import ApiException
6+
7+
from hotdata_core_notebook.client import HotdataClient
8+
from hotdata_core_notebook.health import workspace_health_lines
9+
10+
11+
def test_workspace_health_ok():
12+
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
13+
listing = type("L", (), {"connections": [object()]})()
14+
15+
class FakeConnectionsApi:
16+
def list_connections(self):
17+
return listing
18+
19+
with patch.object(client, "connections", return_value=FakeConnectionsApi()):
20+
ok, parts = workspace_health_lines(client)
21+
assert ok is True
22+
assert any("reachable" in p for p in parts)
23+
24+
25+
def test_workspace_health_api_error():
26+
client = HotdataClient("k", "ws", host="https://api.hotdata.dev")
27+
28+
class Boom:
29+
def list_connections(self):
30+
raise ApiException(status=500, reason="nope")
31+
32+
with patch.object(client, "connections", return_value=Boom()):
33+
ok, parts = workspace_health_lines(client)
34+
assert ok is False
35+
assert parts == ["nope"]

0 commit comments

Comments
 (0)