Skip to content

Commit 6d9999d

Browse files
authored
feat: use tabulate for observer (#94)
1 parent 393a4c5 commit 6d9999d

5 files changed

Lines changed: 44 additions & 123 deletions

File tree

openviking/storage/observers/queue_observer.py

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ def __str__(self) -> str:
3939

4040
def _format_status_as_table(self, statuses: Dict[str, QueueStatus]) -> str:
4141
"""
42-
Format queue statuses as a string table.
42+
Format queue statuses as a table using tabulate.
4343
4444
Args:
4545
statuses: Dict mapping queue names to QueueStatus
4646
4747
Returns:
4848
Formatted table string
4949
"""
50+
from tabulate import tabulate
51+
5052
if not statuses:
5153
return "No queue status data available."
5254

@@ -61,11 +63,11 @@ def _format_status_as_table(self, statuses: Dict[str, QueueStatus]) -> str:
6163
data.append(
6264
{
6365
"Queue": queue_name,
64-
"Pending": str(status.pending),
65-
"In Progress": str(status.in_progress),
66-
"Processed": str(status.processed),
67-
"Errors": str(status.error_count),
68-
"Total": str(total),
66+
"Pending": status.pending,
67+
"In Progress": status.in_progress,
68+
"Processed": status.processed,
69+
"Errors": status.error_count,
70+
"Total": total,
6971
}
7072
)
7173
total_pending += status.pending
@@ -78,51 +80,15 @@ def _format_status_as_table(self, statuses: Dict[str, QueueStatus]) -> str:
7880
data.append(
7981
{
8082
"Queue": "TOTAL",
81-
"Pending": str(total_pending),
82-
"In Progress": str(total_in_progress),
83-
"Processed": str(total_processed),
84-
"Errors": str(total_errors),
85-
"Total": str(total_total),
83+
"Pending": total_pending,
84+
"In Progress": total_in_progress,
85+
"Processed": total_processed,
86+
"Errors": total_errors,
87+
"Total": total_total,
8688
}
8789
)
8890

89-
# Simple table formatter
90-
headers = ["Queue", "Pending", "In Progress", "Processed", "Errors", "Total"]
91-
# Default minimum widths similar to previous col_space
92-
min_widths = {
93-
"Queue": 20,
94-
"Pending": 10,
95-
"In Progress": 12,
96-
"Processed": 10,
97-
"Errors": 8,
98-
"Total": 10,
99-
}
100-
101-
col_widths = {h: len(h) for h in headers}
102-
103-
# Calculate max width based on content and min_widths
104-
for row in data:
105-
for h in headers:
106-
content_len = len(str(row.get(h, "")))
107-
col_widths[h] = max(col_widths[h], content_len, min_widths.get(h, 0))
108-
109-
# Add padding
110-
for h in headers:
111-
col_widths[h] += 2
112-
113-
# Build string
114-
lines = []
115-
116-
# Header
117-
header_line = "".join(h.ljust(col_widths[h]) for h in headers)
118-
lines.append(header_line)
119-
120-
# Rows
121-
for row in data:
122-
line = "".join(str(row.get(h, "")).ljust(col_widths[h]) for h in headers)
123-
lines.append(line)
124-
125-
return "\n".join(lines)
91+
return tabulate(data, headers="keys", tablefmt="pretty")
12692

12793
def is_healthy(self) -> bool:
12894
return not self.has_errors()

openviking/storage/observers/vikingdb_observer.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ async def _get_collection_statuses(self, collection_names: list) -> Dict[str, Di
7878
return statuses
7979

8080
def _format_status_as_table(self, statuses: Dict[str, Dict]) -> str:
81+
from tabulate import tabulate
82+
8183
data = []
8284
total_indexes = 0
8385
total_vectors = 0
@@ -90,8 +92,8 @@ def _format_status_as_table(self, statuses: Dict[str, Dict]) -> str:
9092
data.append(
9193
{
9294
"Collection": name,
93-
"Index Count": str(index_count),
94-
"Vector Count": str(vector_count),
95+
"Index Count": index_count,
96+
"Vector Count": vector_count,
9597
"Status": "ERROR" if error else "OK",
9698
}
9799
)
@@ -105,37 +107,13 @@ def _format_status_as_table(self, statuses: Dict[str, Dict]) -> str:
105107
data.append(
106108
{
107109
"Collection": "TOTAL",
108-
"Index Count": str(total_indexes),
109-
"Vector Count": str(total_vectors),
110+
"Index Count": total_indexes,
111+
"Vector Count": total_vectors,
110112
"Status": "",
111113
}
112114
)
113115

114-
# Simple table formatter
115-
headers = ["Collection", "Index Count", "Vector Count", "Status"]
116-
col_widths = {h: len(h) for h in headers}
117-
118-
for row in data:
119-
for h in headers:
120-
col_widths[h] = max(col_widths[h], len(str(row.get(h, ""))))
121-
122-
# Add padding
123-
for h in headers:
124-
col_widths[h] += 2
125-
126-
# Build string
127-
lines = []
128-
129-
# Header
130-
header_line = "".join(h.ljust(col_widths[h]) for h in headers)
131-
lines.append(header_line)
132-
133-
# Rows
134-
for row in data:
135-
line = "".join(str(row.get(h, "")).ljust(col_widths[h]) for h in headers)
136-
lines.append(line)
137-
138-
return "\n".join(lines)
116+
return tabulate(data, headers="keys", tablefmt="pretty")
139117

140118
def is_healthy(self) -> bool:
141119
"""

openviking/storage/observers/vlm_observer.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ def get_status_table(self) -> str:
4040

4141
def _format_status_as_table(self) -> str:
4242
"""
43-
Format token usage status as a string table.
43+
Format token usage status as a table using tabulate.
4444
4545
Returns:
4646
Formatted table string representation of token usage
4747
"""
48+
from tabulate import tabulate
49+
4850
usage_data = self._vlm_instance.get_token_usage()
4951

5052
if not usage_data.get("usage_by_model"):
@@ -61,10 +63,10 @@ def _format_status_as_table(self) -> str:
6163
{
6264
"Model": model_name,
6365
"Provider": provider_name,
64-
"Prompt": str(provider_data["prompt_tokens"]),
65-
"Completion": str(provider_data["completion_tokens"]),
66-
"Total": str(provider_data["total_tokens"]),
67-
"Last Updated": str(provider_data["last_updated"]),
66+
"Prompt": provider_data["prompt_tokens"],
67+
"Completion": provider_data["completion_tokens"],
68+
"Total": provider_data["total_tokens"],
69+
"Last Updated": provider_data["last_updated"],
6870
}
6971
)
7072
total_prompt += provider_data["prompt_tokens"]
@@ -79,51 +81,14 @@ def _format_status_as_table(self) -> str:
7981
{
8082
"Model": "TOTAL",
8183
"Provider": "",
82-
"Prompt": str(total_prompt),
83-
"Completion": str(total_completion),
84-
"Total": str(total_all),
84+
"Prompt": total_prompt,
85+
"Completion": total_completion,
86+
"Total": total_all,
8587
"Last Updated": "",
8688
}
8789
)
8890

89-
# Simple table formatter
90-
headers = ["Model", "Provider", "Prompt", "Completion", "Total", "Last Updated"]
91-
92-
# Default minimum widths similar to previous col_space
93-
min_widths = {
94-
"Model": 30,
95-
"Provider": 12,
96-
"Prompt": 12,
97-
"Completion": 12,
98-
"Total": 12,
99-
"Last Updated": 20,
100-
}
101-
102-
col_widths = {h: len(h) for h in headers}
103-
104-
# Calculate max width based on content and min_widths
105-
for row in data:
106-
for h in headers:
107-
content_len = len(str(row.get(h, "")))
108-
col_widths[h] = max(col_widths[h], content_len, min_widths.get(h, 0))
109-
110-
# Add padding
111-
for h in headers:
112-
col_widths[h] += 2
113-
114-
# Build string
115-
lines = []
116-
117-
# Header
118-
header_line = "".join(h.ljust(col_widths[h]) for h in headers)
119-
lines.append(header_line)
120-
121-
# Rows
122-
for row in data:
123-
line = "".join(str(row.get(h, "")).ljust(col_widths[h]) for h in headers)
124-
lines.append(line)
125-
126-
return "\n".join(lines)
91+
return tabulate(data, headers="keys", tablefmt="pretty")
12792

12893
def __str__(self) -> str:
12994
return self.get_status_table()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies = [
4949
"xxhash>=3.0.0",
5050
"jinja2>=3.1.6",
5151
"nest-asyncio>=1.5.0",
52+
"tabulate>=0.9.0",
5253
]
5354

5455
[project.optional-dependencies]

uv.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)