User statistics tracking microservice for CyTube channels
Project description
Kryten User Statistics Tracker
A microservice for tracking comprehensive user statistics in CyTube channels via the Kryten bridge.
Features
Core Statistics
- User Tracking: Records all seen usernames with first/last seen timestamps
- Message Counts: Tracks public messages per user per channel
- PM Counts: Tracks private messages from each user
- Channel Population: Snapshots every 5 minutes (connected users and active chatters)
- Media Changes: Logs all media title changes with timestamps
- User Activity Time:
- Total time in channel
- Active (not-AFK) time in channel
- Uses CyTube's native
setAFKevents for accurate tracking
Emote Tracking
- Tracks emote usage per user per channel
- Detects hashtags matching the master emote list
- Example:
#PogChampor#Kappa
Kudos System
Two types of kudos tracking:
- ++ Kudos: Detects
++usernameorusername++patterns - Phrase Kudos: Detects username near trigger phrases
- Configurable phrases (e.g., "lol", "rofl", "haha", emojis)
- Supports both
"phrase username"and"username phrase"patterns - Includes repeating laughter detection (haha, hehe, hoho, etc.)
Username Aliases
- Configurable N+1 aliases per username
- Example: "kevinchrist" → ["kc", "kchrist", "kevin"]
- Kudos automatically resolved to canonical username
Data Exposure
- Prometheus Metrics: HTTP endpoint on port 28282 (configurable)
- Service health metrics
- Application statistics (users, messages, kudos, emotes, etc.)
- Real-time active session count
- NATS Query Endpoints: Request/reply pattern for programmatic access
- User statistics (messages, activity, kudos, emotes)
- Channel leaderboards and population data
- Media history
- Global leaderboards
- System health and stats
See QUERY_ENDPOINTS.md for complete API documentation.
Installation
Prerequisites
- Python 3.11+
- NATS server running
- Kryten-Robot bridge connected to target channel(s)
- kryten-py library
Quick Start
-
Clone or copy the repository
cd d:\Devel\kryten-userstats
-
Copy configuration template
# Windows Copy-Item config.example.json config.json
# Linux/macOS cp config.example.json config.json
-
Edit configuration
- Set your NATS connection details
- Configure channels to monitor
- Adjust snapshot interval (default 5 minutes)
-
Configure aliases (optional) Edit
aliases.jsonto add username aliases -
Start the tracker
# Windows .\start-userstats.ps1
# Linux/macOS chmod +x start-userstats.sh ./start-userstats.sh
Configuration
config.json
{
"nats": {
"servers": ["nats://localhost:4222"],
"user": "${NATS_USER}",
"password": "${NATS_PASSWORD}"
},
"channels": [
{
"domain": "cytu.be",
"channel": "420grindhouse"
}
],
"database": {
"path": "data/userstats.db"
},
"snapshots": {
"interval_seconds": 300
},
"kudos": {
"default_phrases": [
"lol",
"rofl",
"lmao",
"haha",
"😂",
"🤣",
"😆"
]
}
}
aliases.json
{
"aliases": {
"kevinchrist": ["kc", "kchrist", "kevin"],
"exampleuser": ["example", "ex"]
},
"kudos_phrases": [
"lol",
"rofl",
"lmao",
"haha",
"nice",
"great",
"awesome"
]
}
Database Schema
The tracker uses SQLite3 for persistent storage with the following tables:
Core Tables
users- All seen usernames with timestampsuser_aliases- Username → alias mappingsmessage_counts- Public message counts per user/channelpm_counts- PM counts per userpopulation_snapshots- Channel population every N minutesmedia_changes- Media title change loguser_activity- Total and not-AFK time per user/channel
Emote & Kudos Tables
emote_usage- Emote usage counts per user/channel/emotekudos_plusplus- ++ kudos counts per user/channelkudos_phrases- Phrase kudos counts per user/channel/phrasekudos_trigger_phrases- Configured trigger phrases
Usage Examples
Running in Background
# Windows
.\start-userstats.ps1 -Background
# Stop
Stop-Process -Id (Get-Content userstats.pid)
Custom Configuration
# Windows
.\start-userstats.ps1 -ConfigFile custom-config.json
# Linux/macOS
./start-userstats.sh custom-config.json
Monitoring Logs
# Windows
Get-Content -Path userstats.log -Wait
# Linux/macOS
tail -f userstats.log
Querying Statistics
CLI Tool (Recommended)
Use the included CLI tool for easy querying:
# System statistics
python query_cli.py system stats
# System health
python query_cli.py system health
# User statistics
python query_cli.py user alice
python query_cli.py user alice --channel 420grindhouse
# Leaderboards
python query_cli.py leaderboard messages --limit 10
python query_cli.py leaderboard kudos --limit 10
python query_cli.py leaderboard emotes --limit 20
# Channel statistics
python query_cli.py channel top 420grindhouse --limit 10
python query_cli.py channel media 420grindhouse --limit 20
python query_cli.py channel population 420grindhouse --hours 24
HTTP Metrics (Prometheus)
# View metrics
curl http://localhost:28282/metrics
# Or use the example script
python examples/metrics_example.py
NATS Queries (Advanced)
See QUERY_ENDPOINTS.md for complete API documentation and examples.
# Using nats-cli
nats request cytube.query.userstats.cytu.be.system.stats '{}'
# Using Python
python examples/query_example.py
Database Queries
Example SQL queries for common statistics:
Top Message Senders
SELECT username, channel, message_count
FROM message_counts
ORDER BY message_count DESC
LIMIT 10;
Top Kudos Recipients (++)
SELECT username, channel, kudos_count
FROM kudos_plusplus
ORDER BY kudos_count DESC
LIMIT 10;
Most Used Emotes
SELECT emote, SUM(usage_count) as total
FROM emote_usage
GROUP BY emote
ORDER BY total DESC
LIMIT 10;
Recent Media Changes
SELECT channel, timestamp, media_title
FROM media_changes
ORDER BY timestamp DESC
LIMIT 20;
User Activity Report
SELECT
username,
channel,
total_time_seconds / 3600.0 as hours_total,
not_afk_time_seconds / 3600.0 as hours_active
FROM user_activity
ORDER BY not_afk_time_seconds DESC;
Architecture
The tracker follows the kryten-misc pattern:
- Database Layer (
database.py): SQLite3 operations with async executor - Activity Tracker (
activity_tracker.py): Monitors user sessions and AFK status - Kudos Detector (
kudos_detector.py): Regex-based ++ and phrase detection - Emote Detector (
emote_detector.py): Hashtag matching against emote list - Main Application (
main.py): Event handlers and coordination
Requirements
- Python 3.11+
- kryten-py >= 0.2.3
- NATS server
- Kryten-Robot bridge (connected to monitored channels)
Troubleshooting
Tracker Not Receiving Events
- Check NATS connection:
nats-cli sub "cytube.events.>" - Verify Kryten-Robot bridge is running and connected
- Ensure channel names match between config and bridge
Database Locked Errors
- SQLite only allows one writer at a time
- Tracker uses async executor to prevent blocking
- If issues persist, check for other processes accessing the DB
Kudos Not Detected
- Check trigger phrases in database:
SELECT * FROM kudos_trigger_phrases - Verify username aliases:
SELECT * FROM user_aliases - Test regex patterns with sample messages
License
MIT License - See kryten-py for details
Credits
Built using:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kryten_userstats-0.5.0.tar.gz.
File metadata
- Download URL: kryten_userstats-0.5.0.tar.gz
- Upload date:
- Size: 27.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38b6dd00d15955410e82da3844656adf9802118f3c984e2393ade36e5f5fab7b
|
|
| MD5 |
892a4cdcc419a713bf674b84d0fc96e3
|
|
| BLAKE2b-256 |
d1af7b4c72bd60c510db3a2f5b19528f0d7e96b091a255244b6562c1faf1fd8a
|
Provenance
The following attestation bundles were made for kryten_userstats-0.5.0.tar.gz:
Publisher:
python-publish.yml on grobertson/kryten-userstats
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kryten_userstats-0.5.0.tar.gz -
Subject digest:
38b6dd00d15955410e82da3844656adf9802118f3c984e2393ade36e5f5fab7b - Sigstore transparency entry: 762222621
- Sigstore integration time:
-
Permalink:
grobertson/kryten-userstats@d6121ae58c4fa0f272039b2593f7dca4f56206a0 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/grobertson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d6121ae58c4fa0f272039b2593f7dca4f56206a0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file kryten_userstats-0.5.0-py3-none-any.whl.
File metadata
- Download URL: kryten_userstats-0.5.0-py3-none-any.whl
- Upload date:
- Size: 28.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7b98a5c62a0e723367214c00abd570bf51ba6e81191eb4b635508fb67e02615
|
|
| MD5 |
3e86196098b7a2f7e3ed9ea8c802e01c
|
|
| BLAKE2b-256 |
56515f29987eb5057b8e643a5bcdcde19e116b94af568c8af23a2c5480c39596
|
Provenance
The following attestation bundles were made for kryten_userstats-0.5.0-py3-none-any.whl:
Publisher:
python-publish.yml on grobertson/kryten-userstats
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kryten_userstats-0.5.0-py3-none-any.whl -
Subject digest:
f7b98a5c62a0e723367214c00abd570bf51ba6e81191eb4b635508fb67e02615 - Sigstore transparency entry: 762222624
- Sigstore integration time:
-
Permalink:
grobertson/kryten-userstats@d6121ae58c4fa0f272039b2593f7dca4f56206a0 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/grobertson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d6121ae58c4fa0f272039b2593f7dca4f56206a0 -
Trigger Event:
release
-
Statement type: