Skip to content

Commit

Permalink
bot-monitor: persist same webKey in db as used in emails
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Apr 22, 2024
1 parent 03afa7f commit 5f736ba
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions bot-monitor/Alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ export class Alert {
log(`[i] Sending email for "${this.name}" to ${this.rule.email}`);
let subject = `[${this.rule.bot}] ${this.rule.task} failure`;

const webKey = crypto.randomBytes(32).toString('hex');
this.rule.webKey = crypto.randomBytes(32).toString('hex');
if (this.rule.email.includes('@')) {
await mailTransporter.sendMail({
from: '[email protected]',
to: this.rule.email,
subject: subject,
html: this.getEmailBodyHtml(webKey),
html: this.getEmailBodyHtml(),
});
} else {
await new bot.User(this.rule.email).email(
subject,
this.getEmailBodyPlain(webKey),
this.getEmailBodyPlain(),
{ccme: true}
).catch(err => {
if (err.code === 'notarget') {
Expand All @@ -88,24 +88,24 @@ export class Alert {
});
}

getEmailBodyHtml(webKey: string): string {
getEmailBodyHtml(): string {
const taskKey = `${this.rule.bot}: ${this.rule.task}`;
return `${this.rule.bot}'s task <b>${this.rule.task}</b> failed to run per the configuration specified at <a href="https://en.wikipedia.org/wiki/Wikipedia:Bot_activity_monitor/Configurations">Wikipedia:Bot activity monitor/Configurations</a>. Detected only ${this.actions} ${this.rule.action === 'edit' ? 'edit' : `"${this.rule.action}" action`}s in the last ${this.rule.duration}, whereas at least ${this.rule.minEdits} were expected.` +
`<br><br>` +
`If your bot is behaving as expected, then you may want to <a href="https://en.wikipedia.org/wiki/Wikipedia:Bot_activity_monitor/Configurations?action=edit">modify the task configuration instead</a>. Or to unsubscribe from these email notifications, remove the |email= parameter from the {{/task}} template.` +
`<br><br>` +
`To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${webKey}` +
`To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${this.rule.webKey}` +
`<br><br>` +
`Thanks!`;
}

getEmailBodyPlain(webKey: string): string {
getEmailBodyPlain(): string {
const taskKey = `${this.rule.bot}: ${this.rule.task}`;
return `${this.rule.bot}'s task "${this.rule.task}" failed to run per the configuration specified at Wikipedia:Bot activity monitor/Configurations (<https://en.wikipedia.org/wiki/Wikipedia:Bot_activity_monitor/Configurations>). Detected only ${this.actions} ${this.rule.action === 'edit' ? 'edit' : `"${this.rule.action}" action`}s in the last ${this.rule.duration}, whereas at least ${this.rule.minEdits} were expected.` +
`\n\n` +
`If your bot is behaving as expected, then you may want to modify the task configuration instead. Or to unsubscribe from these email notifications, remove the |email= parameter from the {{/task}} template.` +
`\n\n` +
`To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${webKey}` +
`To temporarily pause these notifications, click here: https://sdzerobot.toolforge.org/bot-monitor/pause?task=${encodeURIComponent(taskKey)}&webKey=${this.rule.webKey}` +
`\n\n` +
`Thanks!`;
}
Expand Down
3 changes: 1 addition & 2 deletions bot-monitor/AlertsDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {MwnDate} from "mwn";
import {getKey, Rule} from "./Rule";
import {bot, toolsdb} from "../botbase";
import * as fs from "fs/promises";
import * as crypto from "crypto";
import {getRedisInstance, Redis} from "../redis";
import {ResultSetHeader} from "mysql2";
import {CustomError} from "../utils";
Expand Down Expand Up @@ -46,7 +45,7 @@ class MariadbAlertsDb implements AlertsDb {
async saveLastEmailedTime(rule: Rule): Promise<void> {
await this.db.run(
`REPLACE INTO alerts (name, lastEmailed, webKey) VALUES(?, UTC_TIMESTAMP(), ?)`,
[ getKey(rule, 250), crypto.randomBytes(32).toString('hex') ]
[ getKey(rule, 250), rule.webKey ]
);
}

Expand Down
1 change: 1 addition & 0 deletions bot-monitor/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Rule {
fromDate: MwnDate
alertPage: MwnTitle
email: string
webKey?: string

// pingUser: string
}
Expand Down

0 comments on commit 5f736ba

Please sign in to comment.