From 3d6ec674eaadf5014176e8ef063cb69aba98e9a8 Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Fri, 20 Dec 2024 17:35:49 +0100
Subject: [PATCH] Added msg-stats

---
 scripts/msg_stats.py | 80 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 scripts/msg_stats.py

diff --git a/scripts/msg_stats.py b/scripts/msg_stats.py
new file mode 100644
index 00000000..3bde7ee4
--- /dev/null
+++ b/scripts/msg_stats.py
@@ -0,0 +1,80 @@
+import asyncio
+import time
+
+from xaal.lib import AsyncEngine, Message, MessageAction
+
+MSG_STATS = {
+    "total": 0,
+    "alive": 0,
+    "request_isalive": 0,
+    "request": 0,
+    "reply": 0,
+    "get_description": 0,
+    "get_attributes": 0,
+    "attributes_change": 0,
+    "notify": 0,
+}
+
+DEVICE_STATS = {}
+
+
+def count(msg: Message):
+    msg_count(msg)
+    device_count(msg)
+
+
+def device_count(msg: Message):
+    addr = msg.source
+    try:
+        DEVICE_STATS[addr] += 1
+    except KeyError:
+        DEVICE_STATS[addr] = 1
+
+
+def msg_count(msg: Message):
+    # Count the number of messages received
+    MSG_STATS["total"] += 1
+    if msg.is_request_isalive():
+        MSG_STATS["request_isalive"] += 1
+        return
+    if msg.is_alive():
+        MSG_STATS["alive"] += 1
+        return
+    if msg.is_reply() or msg.is_request():
+        # This msg is a get attribute or description type
+        if msg.action == MessageAction.GET_ATTRIBUTES.value:
+            MSG_STATS["get_attributes"] += 1
+            return
+        if msg.action == MessageAction.GET_DESCRIPTION.value:
+            MSG_STATS["get_description"] += 1
+            return
+    if msg.is_request():
+        # direct request / not isalive / not attributes / description
+        MSG_STATS["request"] += 1
+    if msg.is_reply():
+        MSG_STATS["reply"] += 1
+    if msg.is_attributes_change():
+        MSG_STATS["attributes_change"] += 1
+    if msg.is_notify():
+        MSG_STATS["notify"] += 1
+
+
+async def show():
+    t0 = time.time()
+    while 1:
+        await asyncio.sleep(5)
+        t = time.time() - t0
+        print(MSG_STATS)
+        print(DEVICE_STATS)
+
+
+def run():
+    eng = AsyncEngine()
+    eng.disable_msg_filter()
+    eng.subscribe(count)
+    eng.on_start(show)
+    eng.run()
+
+
+if __name__ == "__main__":
+    run()
-- 
GitLab