Skip to content

Commit

Permalink
imp(notifier): improve redis structure key
Browse files Browse the repository at this point in the history
  • Loading branch information
e11sy committed Feb 2, 2025
1 parent fc8170b commit 17f5a04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions workers/notifier/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export default class NotifierWorker extends Worker {
return;
}

const currentEventCount = await this.redis.computeEventCountForPeriod(rule._id.toString(), event.groupHash, rule.eventThresholdPeriod);
const currentEventCount = await this.redis.computeEventCountForPeriod(projectId, rule._id.toString(), event.groupHash, rule.eventThresholdPeriod);

/**
* If threshold reached, then send event to channels
*/
if (rule.threshold === currentEventCount) {
await this.addEventToChannels(projectId, rule, event);
await this.sendEventsToChannels(projectId, rule, event);
}
}
} catch (e) {
Expand Down Expand Up @@ -122,13 +122,13 @@ export default class NotifierWorker extends Worker {
}

/**
* Add event to channel's buffer or set timer if it doesn't exist
* Send events to sender for each enabled channel in rule
*
* @param {string} projectId - project id event is related to
* @param {Rule} rule - notification rule
* @param {NotifierEvent} event - received event
*/
private async addEventToChannels(projectId: string, rule: Rule, event: NotifierEvent): Promise<void> {
private async sendEventsToChannels(projectId: string, rule: Rule, event: NotifierEvent): Promise<void> {
const channels: Array<[string, Channel]> = Object.entries(rule.channels as { [name: string]: Channel });

for (const [name, options] of channels) {
Expand Down
12 changes: 9 additions & 3 deletions workers/notifier/src/redisHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ export default class RedisHelper {
/**
* Method that updates the event count respectfully to the threshold reset period
*
* @param projectId - id of the project used as a part of structure key
* @param ruleId - id of the rule used as a part of structure key
* @param groupHash - event group hash used as a part of structure key
* @param thresholdPeriod - period of time used to reset the event count
* @returns {number} current event count
*/
public async computeEventCountForPeriod(ruleId: string, groupHash: NotifierEvent['groupHash'], thresholdPeriod: Rule['eventThresholdPeriod']): Promise<number> {
public async computeEventCountForPeriod(
projectId: string,
ruleId: string,
groupHash: NotifierEvent['groupHash'],
thresholdPeriod: Rule['eventThresholdPeriod']
): Promise<number> {
const script = `
local key = KEYS[1]
local currentTimestamp = tonumber(ARGV[1])
Expand All @@ -63,13 +69,13 @@ export default class RedisHelper {
return newCounter
`;

const key = `${ruleId}:${groupHash}:${thresholdPeriod}:times`;
const key = `${projectId}:${ruleId}:${groupHash}:${thresholdPeriod}:times`;

const currentTimestamp = Date.now();

const currentEventCount = await this.redisClient.eval(script, {
keys: [ key ],
arguments: [currentTimestamp.toString(), (currentTimestamp + thresholdPeriod).toString()],
arguments: [currentTimestamp.toString(), (thresholdPeriod).toString()],
}) as number;

return (currentEventCount !== null) ? currentEventCount : 0;
Expand Down

0 comments on commit 17f5a04

Please sign in to comment.