Skip to content

Commit

Permalink
feat(agent): counter 0 and max counter (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly authored Aug 11, 2023
1 parent 500f87c commit b9ed7d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/agent/src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,28 @@ export class Rule {
// eslint-disable-next-line max-len
this.#logger.info(`[${rule.name}](state: handle|previous: ${previousCounter}|new: ${logs.length}|next: ${rule.counter}|threshold: ${alertThreshold})`);


const [operator, value] = utils.ruleCountThresholdOperator(alertThreshold);

if (utils.ruleCountMatchOperator(operator, rule.counter, value)) {
if (operator.startsWith("<")) {
// we checking for a max value, so we want to wait the whole interval before sending an alert
const counters = db.prepare("SELECT * FROM counters WHERE ruleId = ? AND timestamp <= ?").all(
const counters = db.prepare("SELECT * FROM counters WHERE ruleId = ? AND timestamp >= ?").all(
rule.id,
timeThreshold
) as DbCounter[];

if (counters.length === 0) {
const diffPolling = dayjs().unix() - utils.durationOrCronToDate(this.#config.polling, "subtract").unix();
const diffInterval = dayjs().unix() - timeThreshold;
const expectedCounterCount = Math.ceil(diffInterval / diffPolling);

// check it fetch since the rule interval (i.e if interval is 1m and we fetch every 30s, we want to check there is at least 2 counters)
if (counters.length < expectedCounterCount) {
return;
}

const countInInterval = counters.reduce((acc, cur) => acc + cur.counter, 0);

if (!utils.ruleCountMatchOperator(operator, countInInterval, value)) {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function cleanRulesInDb(configRules: SigynRule[]) {

export function ruleCountThresholdOperator(counter: number | string): RuleCounterOperatorValue {
if (typeof counter === "number" || kOnlyDigitsRegExp.test(counter)) {
return [">=", Number(counter)];
return [Number(counter) === 0 ? "<=" : ">=", Number(counter)];
}

const match = counter.replace(/\s/g, "").match(kOperatorValueRegExp);
Expand Down

0 comments on commit b9ed7d3

Please sign in to comment.