Skip to content

Commit 5d864e8

Browse files
committed
feat: debug counters
Simple counters for debugging.
1 parent 3fd88b6 commit 5d864e8

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

backend/src/debugCounters.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type DebugCounterValue = {
2+
count: number;
3+
};
4+
const debugCounterValueMap = new Map<string, DebugCounterValue>();
5+
6+
export function incrementDebugCounter(name: string) {
7+
if (!debugCounterValueMap.has(name)) {
8+
debugCounterValueMap.set(name, { count: 0 });
9+
}
10+
debugCounterValueMap.get(name)!.count++;
11+
}
12+
13+
export function getDebugCounterValues() {
14+
return debugCounterValueMap;
15+
}

backend/src/plugins/BotControl/BotControlPlugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { RemoveDashboardUserCmd } from "./commands/RemoveDashboardUserCmd.js";
2222
import { RestPerformanceCmd } from "./commands/RestPerformanceCmd.js";
2323
import { ServersCmd } from "./commands/ServersCmd.js";
2424
import { BotControlPluginType, zBotControlConfig } from "./types.js";
25+
import { DebugCountersCmd } from "./commands/DebugCountersCmd.js";
2526

2627
export const BotControlPlugin = globalPlugin<BotControlPluginType>()({
2728
name: "bot_control",
@@ -45,6 +46,7 @@ export const BotControlPlugin = globalPlugin<BotControlPluginType>()({
4546
RateLimitPerformanceCmd,
4647
AddServerFromInviteCmd,
4748
ChannelToServerCmd,
49+
DebugCountersCmd,
4850
],
4951

5052
async afterLoad(pluginData) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import moment from "moment-timezone";
2+
import { GuildArchives } from "../../../data/GuildArchives.js";
3+
import { getDebugCounterValues } from "../../../debugCounters.js";
4+
import { getBaseUrl } from "../../../pluginUtils.js";
5+
import { botControlCmd } from "../types.js";
6+
7+
type SortableDebugCounter = {
8+
name: string;
9+
count: number;
10+
};
11+
12+
export const DebugCountersCmd = botControlCmd({
13+
trigger: ["debug_counters"],
14+
permission: "can_performance",
15+
16+
signature: {},
17+
18+
async run({ pluginData, message: msg }) {
19+
const debugCounterValueMap = getDebugCounterValues();
20+
const sortableDebugCounters: SortableDebugCounter[] = [];
21+
for (const [name, value] of debugCounterValueMap) {
22+
sortableDebugCounters.push({ name, count: value.count });
23+
}
24+
25+
sortableDebugCounters.sort((a, b) => b.count - a.count);
26+
27+
const archives = GuildArchives.getGuildInstance("0");
28+
const archiveId = await archives.create(JSON.stringify(sortableDebugCounters, null, 2), moment().add(1, "hour"));
29+
const archiveUrl = archives.getUrl(getBaseUrl(pluginData), archiveId);
30+
msg.channel.send(`Link: ${archiveUrl}`);
31+
},
32+
});
33+
//

0 commit comments

Comments
 (0)