mirror of
https://github.com/Burn-E99/TheArtificer.git
synced 2026-01-08 13:27:55 -05:00
Change dailyRate to hourlyRate, added math to calculate the rates
This commit is contained in:
@ -30,7 +30,7 @@ export const generateReport = (msg: string) => ({
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateStats = (guildCount: number, channelCount: number, memberCount: number, rollCount: bigint, utilityCount: bigint) => ({
|
||||
export const generateStats = (guildCount: number, channelCount: number, memberCount: number, rollCount: bigint, utilityCount: bigint, rollRate: number, utilityRate: number) => ({
|
||||
embeds: [{
|
||||
color: infoColor2,
|
||||
title: 'The Artificer\'s Statistics:',
|
||||
@ -53,12 +53,14 @@ export const generateStats = (guildCount: number, channelCount: number, memberCo
|
||||
},
|
||||
{
|
||||
name: 'Roll Commands:',
|
||||
value: `${rollCount}`,
|
||||
value: `${rollCount}
|
||||
(${rollRate.toFixed(2)} per hour)`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Utility Commands:',
|
||||
value: `${utilityCount}`,
|
||||
value: `${utilityCount}
|
||||
(${utilityRate.toFixed(2)} per hour)`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
|
||||
@ -17,15 +17,17 @@ export const stats = async (message: DiscordenoMessage) => {
|
||||
const m = await message.send(compilingStats);
|
||||
|
||||
// Calculate how many times commands have been run
|
||||
const rollQuery = await dbClient.query(`SELECT count FROM command_cnt WHERE command = "roll";`).catch((e) => utils.commonLoggers.dbError('stats.ts:23', 'query', e));
|
||||
const totalQuery = await dbClient.query(`SELECT SUM(count) as count FROM command_cnt;`).catch((e) => utils.commonLoggers.dbError('stats.ts:24', 'query', e));
|
||||
const rollQuery = await dbClient.query(`SELECT count, hourlyRate FROM command_cnt WHERE command = "roll";`).catch((e) => utils.commonLoggers.dbError('stats.ts:23', 'query', e));
|
||||
const totalQuery = await dbClient.query(`SELECT SUM(count) as count, SUM(hourlyRate) as hourlyRate FROM command_cnt;`).catch((e) => utils.commonLoggers.dbError('stats.ts:24', 'query', e));
|
||||
const rolls = BigInt(rollQuery[0].count);
|
||||
const rollRate = parseFloat(rollQuery[0].hourlyRate);
|
||||
const total = BigInt(totalQuery[0].count);
|
||||
const totalRate = parseFloat(totalQuery[0].hourlyRate);
|
||||
|
||||
const cachedGuilds = await cacheHandlers.size('guilds');
|
||||
const cachedChannels = await cacheHandlers.size('channels');
|
||||
const cachedMembers = await cacheHandlers.size('members');
|
||||
m.edit(generateStats(cachedGuilds + cache.dispatchedGuildIds.size, cachedChannels + cache.dispatchedChannelIds.size, cachedMembers, rolls, total - rolls)).catch((e: Error) =>
|
||||
m.edit(generateStats(cachedGuilds + cache.dispatchedGuildIds.size, cachedChannels + cache.dispatchedChannelIds.size, cachedMembers, rolls, (total - rolls), rollRate, (totalRate - rollRate))).catch((e: Error) =>
|
||||
utils.commonLoggers.messageSendError('stats.ts:38', message, e)
|
||||
);
|
||||
} catch (e) {
|
||||
|
||||
@ -12,7 +12,9 @@ import {
|
||||
log,
|
||||
LT,
|
||||
} from '../deps.ts';
|
||||
|
||||
import { PastCommandCount } from './mod.d.ts';
|
||||
import { dbClient } from './db.ts';
|
||||
import utils from './utils.ts';
|
||||
import config from '../config.ts';
|
||||
|
||||
// getRandomStatus() returns status as string
|
||||
@ -59,4 +61,40 @@ const updateListStatistics = (botID: bigint, serverCount: number): void => {
|
||||
});
|
||||
};
|
||||
|
||||
export default { getRandomStatus, updateListStatistics };
|
||||
// Keep one week of data
|
||||
const hoursToKeep = 7 * 24;
|
||||
const previousHours: Array<Array<PastCommandCount>> = []
|
||||
// updateHourlyRates() returns nothing
|
||||
// Updates the hourlyRate for command usage
|
||||
const updateHourlyRates = async () => {
|
||||
try {
|
||||
const newestHour = await dbClient.query(`SELECT command, count FROM command_cnt ORDER BY command;`).catch((e) => utils.commonLoggers.dbError('intervals.ts:71', 'query', e));
|
||||
previousHours.push(newestHour);
|
||||
if (previousHours.length > 1) {
|
||||
const oldestHour = previousHours[0];
|
||||
|
||||
const computedDiff: Array<PastCommandCount> = []
|
||||
for (let i = 0; i < newestHour.length; i++) {
|
||||
computedDiff.push({
|
||||
command: newestHour[i].command,
|
||||
count: (newestHour[i].count - oldestHour[i].count),
|
||||
});
|
||||
log(LT.LOG, `Updating hourlyRate | Computing diffs: ${JSON.stringify(computedDiff)}`);
|
||||
}
|
||||
|
||||
// Update DB
|
||||
computedDiff.forEach(async (cmd) => {
|
||||
log(LT.LOG, `Updating hourlyRate | Storing to DB: ${JSON.stringify(cmd)}`);
|
||||
await dbClient.execute(`UPDATE command_cnt SET hourlyRate = ? WHERE command = ?`, [(cmd.count / previousHours.length), cmd.command]).catch((e) => utils.commonLoggers.dbError('intervals.ts:88', 'update', e));
|
||||
});
|
||||
}
|
||||
|
||||
if (previousHours.length > hoursToKeep) {
|
||||
previousHours.unshift();
|
||||
}
|
||||
} catch (e) {
|
||||
log(LT.ERROR, `Something went wrong in previousHours interval | Error: ${e.name} - ${e.message}`)
|
||||
}
|
||||
};
|
||||
|
||||
export default { getRandomStatus, updateListStatistics, updateHourlyRates };
|
||||
|
||||
5
src/mod.d.ts
vendored
5
src/mod.d.ts
vendored
@ -41,3 +41,8 @@ export type QueuedRoll = {
|
||||
rollCmd: string;
|
||||
modifiers: RollModifiers;
|
||||
};
|
||||
|
||||
export type PastCommandCount = {
|
||||
command: string;
|
||||
count: number;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user