From adf5bc191ebb2c5f079f508381b7b32f620a2110 Mon Sep 17 00:00:00 2001 From: agessaman Date: Sat, 7 Mar 2026 13:23:37 -0800 Subject: [PATCH] Enhance stats collection for web viewer dashboard - Updated documentation in `data-retention.md` and `web-viewer.md` to clarify how stats are collected and displayed, including the new `collect_stats` configuration option. - Modified `StatsCommand` in `stats_command.py` to introduce `collect_stats`, allowing message and command statistics to be recorded even when the `stats` command is disabled. - Adjusted logic in `record_message`, `record_command_stats`, and `record_path_stats` methods to utilize the new `collect_stats` setting for improved flexibility in stats tracking. --- docs/data-retention.md | 2 +- docs/web-viewer.md | 2 ++ modules/commands/stats_command.py | 17 +++++++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/docs/data-retention.md b/docs/data-retention.md index 340eaaf..1cb8a05 100644 --- a/docs/data-retention.md +++ b/docs/data-retention.md @@ -15,7 +15,7 @@ purging_log_retention_days = 90 mesh_connections_retention_days = 7 ``` -Stats tables (message_stats, command_stats, path_stats) use **`[Stats_Command]`** `data_retention_days` (default 7); the scheduler runs that cleanup daily as well. +Stats tables (message_stats, command_stats, path_stats) use **`[Stats_Command]`** `data_retention_days` (default 7); the scheduler runs that cleanup daily as well. Stats are **collected** when the stats command is enabled, or when the optional **`collect_stats = true`** is set under `[Stats_Command]` (so the web viewer dashboard can show message/command/path stats even if the `stats` chat command is disabled). ## Tables and defaults diff --git a/docs/web-viewer.md b/docs/web-viewer.md index 7eb7ce8..b8c7a4c 100644 --- a/docs/web-viewer.md +++ b/docs/web-viewer.md @@ -120,6 +120,8 @@ curl http://localhost:5000/api/stats The viewer uses the same database as the bot by default (`[Bot] db_path`, typically `meshcore_bot.db`). That single file holds repeater contacts, mesh graph, packet stream, and other data so the viewer can show everything. +**Dashboard stats** (message/command counts, top users, etc.) come from the stats tables (`message_stats`, `command_stats`, `path_stats`). To populate these when the `stats` chat command is disabled, you can set the optional config under `[Stats_Command]`: `collect_stats = true`. + ## Migrating from a separate web viewer database If you previously had the web viewer using a **separate** database (e.g. `[Web_Viewer] db_path = bot_data.db`), you can switch to the shared database so the viewer shows repeater/graph data and uses one file. diff --git a/modules/commands/stats_command.py b/modules/commands/stats_command.py index d8cdb71..9842b4f 100644 --- a/modules/commands/stats_command.py +++ b/modules/commands/stats_command.py @@ -48,6 +48,11 @@ class StatsCommand(BaseCommand): self.stats_enabled = self.get_config_value('Stats_Command', 'enabled', fallback=None, value_type='bool') if self.stats_enabled is None: self.stats_enabled = self.get_config_value('Stats_Command', 'stats_enabled', fallback=True, value_type='bool') + # Optional: collect_stats (defaults to stats_enabled). When true, messages/commands/paths + # are recorded for the web viewer dashboard even if enabled = false. + self.collect_stats = self.get_config_value('Stats_Command', 'collect_stats', fallback=None, value_type='bool') + if self.collect_stats is None: + self.collect_stats = self.stats_enabled self.data_retention_days = self.get_config_value('Stats_Command', 'data_retention_days', fallback=7, value_type='int') self.auto_cleanup = self.get_config_value('Stats_Command', 'auto_cleanup', fallback=True, value_type='bool') self.track_all_messages = self.get_config_value('Stats_Command', 'track_all_messages', fallback=True, value_type='bool') @@ -129,11 +134,11 @@ class StatsCommand(BaseCommand): def record_message(self, message: MeshMessage) -> None: """Record a message in the stats database. - + Args: message: The message to record statistics for. """ - if not self.stats_enabled or not self.track_all_messages: + if not self.collect_stats or not self.track_all_messages: return try: @@ -173,7 +178,7 @@ class StatsCommand(BaseCommand): command_name: The name of the command executed. response_sent: Whether a response was sent back to the user. """ - if not self.stats_enabled or not self.track_command_details: + if not self.collect_stats or not self.track_command_details: return try: @@ -204,13 +209,13 @@ class StatsCommand(BaseCommand): def record_path_stats(self, message: MeshMessage) -> None: """Record path statistics for longest path tracking. - + Args: message: The message containing path information. """ - if not self.stats_enabled or not self.track_all_messages: + if not self.collect_stats or not self.track_all_messages: return - + # Only record if we have meaningful path data if not message.hops or message.hops <= 0 or not message.path: return