Skip to content

Instantly share code, notes, and snippets.

@JarbasAl
Created April 22, 2024 07:03
Show Gist options
  • Select an option

  • Save JarbasAl/e02626cfc6100a0890c67a7cac7e978c to your computer and use it in GitHub Desktop.

Select an option

Save JarbasAl/e02626cfc6100a0890c67a7cac7e978c to your computer and use it in GitHub Desktop.
pprint bus messages for hivemind debugging
import json
from rich.console import Console
from rich.table import Table
from rich.json import JSON
from threading import Lock
from ovos_bus_client import MessageBusClient, Message
from ovos_config import Configuration
import sys
import time
def bus_monitor():
l = Lock()
client = MessageBusClient()
console = Console()
def handle_msg(message):
msg = json.loads(message)
sess = msg['context'].get("session", {})
if sess.get("session_id", "default") == "default":
return
with l:
console.print("##### Message Debugger", style="bold")
# Create and print a table for the message details
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Source")
table.add_column("Destination")
table.add_column("Site ID")
table.add_column("Session ID")
table.add_column("User ID")
table.add_row(
msg['context'].get('source'),
msg['context'].get('destination'),
sess.get("site_id"),
sess.get("session_id"),
msg['context'].get("user_id")
)
console.print(table)
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Type", style="dim")
table.add_column("Data", style="dim")
table.add_row(
msg['type'],
JSON(json.dumps(msg['data']))
)
console.print(table)
table = Table(show_header=True, header_style="bold magenta")
for k in msg["context"].get("session", {}):
if k in ["session_id", "site_id", "location", "stt", "tts"]:
continue
table.add_column(k)
table.add_row(*(str(v) for k, v in msg["context"].get("session", {}).items()
if k not in ["session_id", "site_id", "location", "stt", "tts"]))
console.print(table)
table = Table(show_header=True, header_style="bold magenta")
table.add_column("STT", style="dim")
table.add_column("TTS", style="dim")
table.add_row(
JSON(json.dumps(sess["tts"])),
JSON(json.dumps(sess["stt"]))
)
console.print(table)
loc = sess.get("location")
if loc:
table = Table(show_header=True, header_style="bold magenta")
table.add_column("City", style="dim")
table.add_column("Coordinate", style="dim")
table.add_column("Timezone", style="dim")
table.add_row(
JSON(json.dumps(loc["city"])),
JSON(json.dumps(loc["coordinate"])),
JSON(json.dumps(loc["timezone"]))
)
console.print(table)
client.on("message", handle_msg)
try:
client.run_forever()
except KeyboardInterrupt:
console.print("[bold red]Exiting...[/bold red]")
finally:
client.close()
if __name__ == "__main__":
bus_monitor()
@JarbasAl

Copy link
Copy Markdown
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment