debug: re-enable Automod with extra profiling
This commit is contained in:
parent
2d012bc5cf
commit
53d7491c1b
8 changed files with 43 additions and 8 deletions
|
@ -193,15 +193,15 @@ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({
|
|||
|
||||
// prettier-ignore
|
||||
events: [
|
||||
// RunAutomodOnJoinEvt,
|
||||
// RunAutomodOnMemberUpdate,
|
||||
// RunAutomodOnLeaveEvt,
|
||||
RunAutomodOnJoinEvt,
|
||||
RunAutomodOnMemberUpdate,
|
||||
RunAutomodOnLeaveEvt,
|
||||
// Messages use message events from SavedMessages, see onLoad below
|
||||
],
|
||||
|
||||
commands: [AntiraidClearCmd, SetAntiraidCmd, ViewAntiraidCmd],
|
||||
|
||||
async __beforeLoad(pluginData) {
|
||||
async beforeLoad(pluginData) {
|
||||
pluginData.state.queue = new Queue();
|
||||
|
||||
pluginData.state.regexRunner = getRegExpRunner(`guild-${pluginData.guild.id}`);
|
||||
|
@ -224,7 +224,7 @@ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({
|
|||
pluginData.state.cachedAntiraidLevel = await pluginData.state.antiraidLevels.get();
|
||||
},
|
||||
|
||||
async __afterLoad(pluginData) {
|
||||
async afterLoad(pluginData) {
|
||||
pluginData.state.clearRecentActionsInterval = setInterval(() => clearOldRecentActions(pluginData), 1 * MINUTES);
|
||||
pluginData.state.clearRecentSpamInterval = setInterval(() => clearOldRecentSpam(pluginData), 1 * SECONDS);
|
||||
pluginData.state.clearRecentNicknameChangesInterval = setInterval(
|
||||
|
@ -289,7 +289,7 @@ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({
|
|||
registerEventListenersFromMap(mutesEvents, pluginData.state.mutesListeners);
|
||||
},
|
||||
|
||||
async __beforeUnload(pluginData) {
|
||||
async beforeUnload(pluginData) {
|
||||
const countersPlugin = pluginData.getPlugin(CountersPlugin);
|
||||
countersPlugin.offCounterEvent("trigger", pluginData.state.onCounterTrigger);
|
||||
countersPlugin.offCounterEvent("reverseTrigger", pluginData.state.onCounterReverseTrigger);
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { RECENT_ACTION_EXPIRY_TIME } from "../constants";
|
||||
import { AutomodPluginType } from "../types";
|
||||
import { startProfiling } from "../../../utils/easyProfiler";
|
||||
|
||||
export function clearOldRecentActions(pluginData: GuildPluginData<AutomodPluginType>) {
|
||||
const stopProfiling = startProfiling(pluginData.getKnubInstance().profiler, "automod:fns:clearOldRecentActions");
|
||||
const now = Date.now();
|
||||
pluginData.state.recentActions = pluginData.state.recentActions.filter((info) => {
|
||||
return info.context.timestamp + RECENT_ACTION_EXPIRY_TIME > now;
|
||||
});
|
||||
stopProfiling();
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { RECENT_SPAM_EXPIRY_TIME } from "../constants";
|
||||
import { AutomodPluginType } from "../types";
|
||||
import { startProfiling } from "../../../utils/easyProfiler";
|
||||
|
||||
export function clearOldRecentSpam(pluginData: GuildPluginData<AutomodPluginType>) {
|
||||
const stopProfiling = startProfiling(pluginData.getKnubInstance().profiler, "automod:fns:clearOldRecentSpam");
|
||||
const now = Date.now();
|
||||
pluginData.state.recentSpam = pluginData.state.recentSpam.filter((spam) => {
|
||||
return spam.timestamp + RECENT_SPAM_EXPIRY_TIME > now;
|
||||
});
|
||||
stopProfiling();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { AutomodContext, AutomodPluginType } from "../types";
|
||||
import { startProfiling } from "../../../utils/easyProfiler";
|
||||
|
||||
export function clearRecentActionsForMessage(pluginData: GuildPluginData<AutomodPluginType>, context: AutomodContext) {
|
||||
const stopProfiling = startProfiling(
|
||||
pluginData.getKnubInstance().profiler,
|
||||
"automod:fns:clearRecentActionsForMessage",
|
||||
);
|
||||
const message = context.message!;
|
||||
const globalIdentifier = message.user_id;
|
||||
const perChannelIdentifier = `${message.channel_id}-${message.user_id}`;
|
||||
|
@ -9,4 +14,5 @@ export function clearRecentActionsForMessage(pluginData: GuildPluginData<Automod
|
|||
pluginData.state.recentActions = pluginData.state.recentActions.filter((act) => {
|
||||
return act.identifier !== globalIdentifier && act.identifier !== perChannelIdentifier;
|
||||
});
|
||||
stopProfiling();
|
||||
}
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { RecentActionType } from "../constants";
|
||||
import { AutomodPluginType } from "../types";
|
||||
import { startProfiling } from "../../../utils/easyProfiler";
|
||||
|
||||
export function findRecentSpam(
|
||||
pluginData: GuildPluginData<AutomodPluginType>,
|
||||
type: RecentActionType,
|
||||
identifier?: string,
|
||||
) {
|
||||
return pluginData.state.recentSpam.find((spam) => {
|
||||
const stopProfiling = startProfiling(pluginData.getKnubInstance().profiler, "automod:fns:findRecentSpam");
|
||||
const result = pluginData.state.recentSpam.find((spam) => {
|
||||
return spam.type === type && (!identifier || spam.identifiers.includes(identifier));
|
||||
});
|
||||
stopProfiling();
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import { SavedMessage } from "../../../data/entities/SavedMessage";
|
|||
import { RecentActionType } from "../constants";
|
||||
import { AutomodPluginType } from "../types";
|
||||
import { getMatchingRecentActions } from "./getMatchingRecentActions";
|
||||
import { startProfiling } from "../../../utils/easyProfiler";
|
||||
|
||||
export function getMatchingMessageRecentActions(
|
||||
pluginData: GuildPluginData<AutomodPluginType>,
|
||||
|
@ -13,11 +14,16 @@ export function getMatchingMessageRecentActions(
|
|||
count: number,
|
||||
within: number,
|
||||
) {
|
||||
const stopProfiling = startProfiling(
|
||||
pluginData.getKnubInstance().profiler,
|
||||
"automod:fns:getMatchingMessageRecentActions",
|
||||
);
|
||||
const since = moment.utc(message.posted_at).valueOf() - within;
|
||||
const to = moment.utc(message.posted_at).valueOf();
|
||||
const recentActions = getMatchingRecentActions(pluginData, type, identifier, since, to);
|
||||
const totalCount = recentActions.reduce((total, action) => total + action.count, 0);
|
||||
|
||||
stopProfiling();
|
||||
if (totalCount >= count) {
|
||||
return {
|
||||
recentActions,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { GuildPluginData } from "knub";
|
||||
import { RecentActionType } from "../constants";
|
||||
import { AutomodPluginType } from "../types";
|
||||
import { startProfiling } from "../../../utils/easyProfiler";
|
||||
|
||||
export function getMatchingRecentActions(
|
||||
pluginData: GuildPluginData<AutomodPluginType>,
|
||||
|
@ -9,9 +10,10 @@ export function getMatchingRecentActions(
|
|||
since: number,
|
||||
to?: number,
|
||||
) {
|
||||
const stopProfiling = startProfiling(pluginData.getKnubInstance().profiler, "automod:fns:getMatchingRecentActions");
|
||||
to = to || Date.now();
|
||||
|
||||
return pluginData.state.recentActions.filter((action) => {
|
||||
const result = pluginData.state.recentActions.filter((action) => {
|
||||
return (
|
||||
action.type === type &&
|
||||
(!identifier || action.identifier === identifier) &&
|
||||
|
@ -20,4 +22,6 @@ export function getMatchingRecentActions(
|
|||
!action.context.actioned
|
||||
);
|
||||
});
|
||||
stopProfiling();
|
||||
return result;
|
||||
}
|
||||
|
|
9
backend/src/utils/easyProfiler.ts
Normal file
9
backend/src/utils/easyProfiler.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { Profiler } from "knub/dist/Profiler";
|
||||
import { performance } from "perf_hooks";
|
||||
|
||||
export const startProfiling = (profiler: Profiler, key: string) => {
|
||||
const startTime = performance.now();
|
||||
return () => {
|
||||
profiler.addDataPoint(key, performance.now() - startTime);
|
||||
};
|
||||
};
|
Loading…
Add table
Reference in a new issue