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.
This commit is contained in:
agessaman
2026-03-07 13:23:37 -08:00
parent f86fe0b140
commit adf5bc191e
3 changed files with 14 additions and 7 deletions
+1 -1
View File
@@ -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
+2
View File
@@ -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.
+11 -6
View File
@@ -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