Statistics bot
The stats bot runs independently from the main bot:Tracked metrics
The bot tracks:- Chats - Total users and groups
- Videos - Total downloads and unique users
- Images - Slideshow downloads and unique users
- Music - Audio extractions and unique users
- Timestamps - Registration and download times
Database models
Users- User registrationsVideo- Video/image downloadsMusic- Audio extractions
Auto-updating statistics
The bot automatically updates statistics messages in a configured channel:- Overall stats: Updated every hour
- Daily stats: Updated every 5 minutes
Configuration
Statistics formats
Overall statistics
Daily statistics
Timestamps
All statistics include formatted timestamps in multiple time zones:Query builder
The stats system supports filtering by chat type and time period:chat_type='all'- All chatschat_type='users'- Private chats only (user_id > 0)chat_type='groups'- Group chats only (user_id < 0)stats_time=0- All timestats_time=86400- Last 24 hours
Graph generation
The bot generates time-series graphs using matplotlib:Graph features
- Time series plots: Line graphs showing trends over time
- Date formatting: X-axis shows dates with automatic interval selection
- Grid: Light gray grid for readability
- Empty state: “No data available” message when no data exists
- High DPI: 100 DPI for crisp rendering
Data processing
Graphs use pandas for efficient data processing:'%Y-%m-%d'- Daily (frequency: ‘D’)'%Y-%m'- Monthly (frequency: ‘M’)'%Y'- Yearly (frequency: ‘Y’)'%Y-%m-%d %H'- Hourly (frequency: ‘H’)
Async graph generation
Graphs are generated in thread pool to avoid blocking:Botstat integration
The bot integrates with Botstat.io for external analytics:Upload task
Configuration
Database queries
All statistics use efficient SQLAlchemy queries:func.count()for totalsfunc.distinct()for unique users- Filters for time periods and chat types
Performance considerations
Query optimization
- Uses indexed columns (user_id, timestamps)
- Limits data retrieval to required time periods
- Aggregates in database rather than application
Graph rendering
- Runs in thread pool to avoid blocking event loop
- Uses pandas for efficient data processing
- Filters zero values for cleaner plots
- Automatic date interval selection based on data range
Scheduling
- Separate update intervals for overall (1h) and daily (5m) stats
- Misfire grace time disabled for immediate catch-up
- Independent scheduler from main bot

