Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

feat: use tabulate for observer (#94)

kkkwjx6d9999de3a51
diff --git a/openviking/storage/observers/queue_observer.py b/openviking/storage/observers/queue_observer.py
index fec514d40..8946233f3 100644
--- a/openviking/storage/observers/queue_observer.py
+++ b/openviking/storage/observers/queue_observer.py
@@ -39,7 +39,7 @@ class QueueObserver(BaseObserver):
def _format_status_as_table(self, statuses: Dict[str, QueueStatus]) -> str:
"""
- Format queue statuses as a string table.
+ Format queue statuses as a table using tabulate.
Args:
statuses: Dict mapping queue names to QueueStatus
@@ -47,6 +47,8 @@ class QueueObserver(BaseObserver):
Returns:
Formatted table string
"""
+ from tabulate import tabulate
+
if not statuses:
return "No queue status data available."
@@ -61,11 +63,11 @@ class QueueObserver(BaseObserver):
data.append(
{
"Queue": queue_name,
- "Pending": str(status.pending),
- "In Progress": str(status.in_progress),
- "Processed": str(status.processed),
- "Errors": str(status.error_count),
- "Total": str(total),
+ "Pending": status.pending,
+ "In Progress": status.in_progress,
+ "Processed": status.processed,
+ "Errors": status.error_count,
+ "Total": total,
}
)
total_pending += status.pending
@@ -78,51 +80,15 @@ class QueueObserver(BaseObserver):
data.append(
{
"Queue": "TOTAL",
- "Pending": str(total_pending),
- "In Progress": str(total_in_progress),
- "Processed": str(total_processed),
- "Errors": str(total_errors),
- "Total": str(total_total),
+ "Pending": total_pending,
+ "In Progress": total_in_progress,
+ "Processed": total_processed,
+ "Errors": total_errors,
+ "Total": total_total,
}
)
- # Simple table formatter
- headers = ["Queue", "Pending", "In Progress", "Processed", "Errors", "Total"]
- # Default minimum widths similar to previous col_space
- min_widths = {
- "Queue": 20,
- "Pending": 10,
- "In Progress": 12,
- "Processed": 10,
- "Errors": 8,
- "Total": 10,
- }
-
- col_widths = {h: len(h) for h in headers}
-
- # Calculate max width based on content and min_widths
- for row in data:
- for h in headers:
- content_len = len(str(row.get(h, "")))
- col_widths[h] = max(col_widths[h], content_len, min_widths.get(h, 0))
-
- # Add padding
- for h in headers:
- col_widths[h] += 2
-
- # Build string
- lines = []
-
- # Header
- header_line = "".join(h.ljust(col_widths[h]) for h in headers)
- lines.append(header_line)
-
- # Rows
- for row in data:
- line = "".join(str(row.get(h, "")).ljust(col_widths[h]) for h in headers)
- lines.append(line)
-
- return "\n".join(lines)
+ return tabulate(data, headers="keys", tablefmt="pretty")
def is_healthy(self) -> bool:
return not self.has_errors()
diff --git a/openviking/storage/observers/vikingdb_observer.py b/openviking/storage/observers/vikingdb_observer.py
index 91b7d38f8..4803c6554 100644
--- a/openviking/storage/observers/vikingdb_observer.py
+++ b/openviking/storage/observers/vikingdb_observer.py
@@ -78,6 +78,8 @@ class VikingDBObserver(BaseObserver):
return statuses
def _format_status_as_table(self, statuses: Dict[str, Dict]) -> str:
+ from tabulate import tabulate
+
data = []
total_indexes = 0
total_vectors = 0
@@ -90,8 +92,8 @@ class VikingDBObserver(BaseObserver):
data.append(
{
"Collection": name,
- "Index Count": str(index_count),
- "Vector Count": str(vector_count),
+ "Index Count": index_count,
+ "Vector Count": vector_count,
"Status": "ERROR" if error else "OK",
}
)
@@ -105,37 +107,13 @@ class VikingDBObserver(BaseObserver):
data.append(
{
"Collection": "TOTAL",
- "Index Count": str(total_indexes),
- "Vector Count": str(total_vectors),
+ "Index Count": total_indexes,
+ "Vector Count": total_vectors,
"Status": "",
}
)
- # Simple table formatter
- headers = ["Collection", "Index Count", "Vector Count", "Status"]
- col_widths = {h: len(h) for h in headers}
-
- for row in data:
- for h in headers:
- col_widths[h] = max(col_widths[h], len(str(row.get(h, ""))))
-
- # Add padding
- for h in headers:
- col_widths[h] += 2
-
- # Build string
- lines = []
-
- # Header
- header_line = "".join(h.ljust(col_widths[h]) for h in headers)
- lines.append(header_line)
-
- # Rows
- for row in data:
- line = "".join(str(row.get(h, "")).ljust(col_widths[h]) for h in headers)
- lines.append(line)
-
- return "\n".join(lines)
+ return tabulate(data, headers="keys", tablefmt="pretty")
def is_healthy(self) -> bool:
"""
diff --git a/openviking/storage/observers/vlm_observer.py b/openviking/storage/observers/vlm_observer.py
index 844b7adbf..dc48911f3 100644
--- a/openviking/storage/observers/vlm_observer.py
+++ b/openviking/storage/observers/vlm_observer.py
@@ -40,11 +40,13 @@ class VLMObserver(BaseObserver):
def _format_status_as_table(self) -> str:
"""
- Format token usage status as a string table.
+ Format token usage status as a table using tabulate.
Returns:
Formatted table string representation of token usage
"""
+ from tabulate import tabulate
+
usage_data = self._vlm_instance.get_token_usage()
if not usage_data.get("usage_by_model"):
@@ -61,10 +63,10 @@ class VLMObserver(BaseObserver):
{
"Model": model_name,
"Provider": provider_name,
- "Prompt": str(provider_data["prompt_tokens"]),
- "Completion": str(provider_data["completion_tokens"]),
- "Total": str(provider_data["total_tokens"]),
- "Last Updated": str(provider_data["last_updated"]),
+ "Prompt": provider_data["prompt_tokens"],
+ "Completion": provider_data["completion_tokens"],
+ "Total": provider_data["total_tokens"],
+ "Last Updated": provider_data["last_updated"],
}
)
total_prompt += provider_data["prompt_tokens"]
@@ -79,51 +81,14 @@ class VLMObserver(BaseObserver):
{
"Model": "TOTAL",
"Provider": "",
- "Prompt": str(total_prompt),
- "Completion": str(total_completion),
- "Total": str(total_all),
+ "Prompt": total_prompt,
+ "Completion": total_completion,
+ "Total": total_all,
"Last Updated": "",
}
)
- # Simple table formatter
- headers = ["Model", "Provider", "Prompt", "Completion", "Total", "Last Updated"]
-
- # Default minimum widths similar to previous col_space
- min_widths = {
- "Model": 30,
- "Provider": 12,
- "Prompt": 12,
- "Completion": 12,
- "Total": 12,
- "Last Updated": 20,
- }
-
- col_widths = {h: len(h) for h in headers}
-
- # Calculate max width based on content and min_widths
- for row in data:
- for h in headers:
- content_len = len(str(row.get(h, "")))
- col_widths[h] = max(col_widths[h], content_len, min_widths.get(h, 0))
-
- # Add padding
- for h in headers:
- col_widths[h] += 2
-
- # Build string
- lines = []
-
- # Header
- header_line = "".join(h.ljust(col_widths[h]) for h in headers)
- lines.append(header_line)
-
- # Rows
- for row in data:
- line = "".join(str(row.get(h, "")).ljust(col_widths[h]) for h in headers)
- lines.append(line)
-
- return "\n".join(lines)
+ return tabulate(data, headers="keys", tablefmt="pretty")
def __str__(self) -> str:
return self.get_status_table()
diff --git a/pyproject.toml b/pyproject.toml
index 86b5e5895..0a7c7eb0d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -49,6 +49,7 @@ dependencies = [
"xxhash>=3.0.0",
"jinja2>=3.1.6",
"nest-asyncio>=1.5.0",
+ "tabulate>=0.9.0",
]
[project.optional-dependencies]
diff --git a/uv.lock b/uv.lock
index cf71d735e..d8f5b1091 100644
--- a/uv.lock
+++ b/uv.lock
@@ -1523,6 +1523,7 @@ dependencies = [
{ name = "pyyaml" },
{ name = "readabilipy" },
{ name = "requests" },
+ { name = "tabulate" },
{ name = "typing-extensions" },
{ name = "uvicorn", version = "0.39.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.10'" },
{ name = "uvicorn", version = "0.40.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.10'" },
@@ -1576,6 +1577,7 @@ requires-dist = [
{ name = "ruff", marker = "extra == 'dev'", specifier = ">=0.1.0" },
{ name = "sphinx", marker = "extra == 'doc'", specifier = ">=7.0.0" },
{ name = "sphinx-rtd-theme", marker = "extra == 'doc'", specifier = ">=1.3.0" },
+ { name = "tabulate", specifier = ">=0.9.0" },
{ name = "typing-extensions", specifier = ">=4.5.0" },
{ name = "uvicorn", specifier = ">=0.39.0" },
{ name = "volcengine", specifier = ">=1.0.212" },
@@ -2790,6 +2792,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/d9/52/1064f510b141bd54025f9b55105e26d1fa970b9be67ad766380a3c9b74b0/starlette-0.50.0-py3-none-any.whl", hash = "sha256:9e5391843ec9b6e472eed1365a78c8098cfceb7a74bfd4d6b1c0c0095efb3bca", size = 74033, upload-time = "2025-11-01T15:25:25.461Z" },
]
+[[package]]
+name = "tabulate"
+version = "0.9.0"
+source = { registry = "https://pypi.org/simple" }
+sdist = { url = "https://files.pythonhosted.org/packages/ec/fe/802052aecb21e3797b8f7902564ab6ea0d60ff8ca23952079064155d1ae1/tabulate-0.9.0.tar.gz", hash = "sha256:0095b12bf5966de529c0feb1fa08671671b3368eec77d7ef7ab114be2c068b3c", size = 81090, upload-time = "2022-10-06T17:21:48.54Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/40/44/4a5f08c96eb108af5cb50b41f76142f0afa346dfa99d5296fe7202a11854/tabulate-0.9.0-py3-none-any.whl", hash = "sha256:024ca478df22e9340661486f85298cff5f6dcdba14f3813e8830015b9ed1948f", size = 35252, upload-time = "2022-10-06T17:21:44.262Z" },
+]
+
[[package]]
name = "tomli"
version = "2.3.0"