|
| 1 | +/** |
| 2 | + * Command Usage Tracking |
| 3 | + * |
| 4 | + * Records slash-command invocations in the command_usage table for analytics. |
| 5 | + * Queries are indexed for efficient aggregation by guild, user, and command. |
| 6 | + * |
| 7 | + * @see https://github.com/VolvoxLLC/volvox-bot/issues/122 |
| 8 | + */ |
| 9 | + |
| 10 | +import { getPool } from '../db.js'; |
| 11 | +import { warn } from '../logger.js'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Record a slash-command usage. Call asynchronously (fire-and-forget) so the |
| 15 | + * command response is not blocked. Failures are logged and not thrown. |
| 16 | + * |
| 17 | + * @param {Object} opts |
| 18 | + * @param {import('pg').Pool} [opts.pool] - Database pool (defaults to getPool()) |
| 19 | + * @param {string} opts.guildId - Discord guild ID |
| 20 | + * @param {string} opts.userId - Discord user ID |
| 21 | + * @param {string} opts.commandName - Slash command name |
| 22 | + * @param {string} [opts.channelId] - Discord channel ID (optional) |
| 23 | + */ |
| 24 | +export async function trackCommandUsage(opts) { |
| 25 | + const pool = opts.pool ?? getPool(); |
| 26 | + if (!pool) return; |
| 27 | + |
| 28 | + const { guildId, userId, commandName, channelId } = opts; |
| 29 | + if (!guildId || !userId || !commandName) return; |
| 30 | + |
| 31 | + try { |
| 32 | + await pool.query( |
| 33 | + `INSERT INTO command_usage (guild_id, user_id, command_name, channel_id) |
| 34 | + VALUES ($1, $2, $3, $4)`, |
| 35 | + [guildId, userId, commandName, channelId ?? null], |
| 36 | + ); |
| 37 | + } catch (err) { |
| 38 | + warn('Failed to record command usage', { |
| 39 | + guildId, |
| 40 | + commandName, |
| 41 | + error: err.message, |
| 42 | + }); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Fetch aggregated command usage for a guild in a date range for analytics. |
| 48 | + * Returns rows with command_name and uses, ordered by uses DESC. |
| 49 | + * |
| 50 | + * @param {Object} opts |
| 51 | + * @param {import('pg').Pool} opts.pool - Database pool |
| 52 | + * @param {string} opts.guildId - Guild ID |
| 53 | + * @param {string} opts.from - ISO date string (inclusive) |
| 54 | + * @param {string} opts.to - ISO date string (inclusive) |
| 55 | + * @param {string} [opts.channelId] - Optional channel filter |
| 56 | + * @param {number} [opts.limit=15] - Max number of commands to return |
| 57 | + * @returns {Promise<{ rows: Array<{ command_name: string, uses: number }>, available: boolean }>} |
| 58 | + */ |
| 59 | +export async function getAggregatedCommandUsage(opts) { |
| 60 | + const { pool, guildId, from, to, channelId, limit = 15 } = opts; |
| 61 | + const values = [guildId, from, to]; |
| 62 | + let whereClause = 'guild_id = $1 AND used_at >= $2 AND used_at <= $3'; |
| 63 | + if (channelId) { |
| 64 | + values.push(channelId); |
| 65 | + whereClause += ` AND channel_id = $${values.length}`; |
| 66 | + } |
| 67 | + values.push(limit); |
| 68 | + |
| 69 | + try { |
| 70 | + const result = await pool.query( |
| 71 | + `SELECT |
| 72 | + COALESCE(NULLIF(command_name, ''), 'unknown') AS command_name, |
| 73 | + COUNT(*)::int AS uses |
| 74 | + FROM command_usage |
| 75 | + WHERE ${whereClause} |
| 76 | + GROUP BY command_name |
| 77 | + ORDER BY uses DESC, command_name ASC |
| 78 | + LIMIT $${values.length}`, |
| 79 | + values, |
| 80 | + ); |
| 81 | + return { rows: result.rows, available: true }; |
| 82 | + } catch (err) { |
| 83 | + warn('Command usage analytics query failed', { |
| 84 | + guildId, |
| 85 | + error: err.message, |
| 86 | + }); |
| 87 | + return { rows: [], available: false }; |
| 88 | + } |
| 89 | +} |
0 commit comments