Skip to content

Commit

Permalink
fraud_detection: Optimize time-recurrences which always match
Browse files Browse the repository at this point in the history
Rather than always (re)evaluating a time rec which matches 100% of the
time, just set it to NULL, so drouting quickly skips the check.

(cherry picked from commit 2a3a105)
(cherry picked from commit 3fd880a)
  • Loading branch information
liviuchircu committed Jan 25, 2021
1 parent cdbacb9 commit 9b96e85
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions modules/fraud_detection/frd_load.c
Expand Up @@ -225,12 +225,22 @@ static int parse_week_days(const str *week_days, unsigned short *day_set)
}

static int create_time_rec(const str *time_start, const str *time_end,
const str *week_days, tmrec_p trec)
const str *week_days, tmrec_p trec, tmrec_p *out_rec)
{
int end_h, end_m;

memset(trec, 0, sizeof(tmrec_t));

/* the default, "catch-all" time rec - using NULL is optimal */
if (str_match(time_start, _str("00:00")) &&
str_match(time_end, _str("23:59")) &&
str_match(week_days, _str("Mon-Sun"))) {
*out_rec = NULL;
return 0;
} else {
*out_rec = trec;
}

if (strtime(time_start, &trec->ts.tm_hour, &trec->ts.tm_min) != 0
|| strtime(time_end, &end_h, &end_m) != 0)
return -1;
Expand Down Expand Up @@ -344,6 +354,8 @@ static int frd_load_data(dr_head_p drp, free_list_t **fl)
fl_it->n = row_count;

for (i = 0; i < row_count; ++i) {
tmrec_p trec;

values = ROW_VALUES(rows + i);
fl_it->trec[i].byday = NULL;

Expand All @@ -366,7 +378,8 @@ static int frd_load_data(dr_head_p drp, free_list_t **fl)
get_str_from_dbval(end_h_col.s, values + 4, 1, 1, end_time, null_val);
get_str_from_dbval(days_col.s, values + 5, 1, 1, days, null_val);

if (create_time_rec(&start_time, &end_time, &days, fl_it->trec + i) != 0)
if (create_time_rec(&start_time, &end_time, &days, fl_it->trec + i,
&trec) != 0)
goto null_val;

/* Now load the thresholds */
Expand All @@ -379,7 +392,7 @@ static int frd_load_data(dr_head_p drp, free_list_t **fl)
}

/* Rule OK, time to put it in DR */
if (drb.add_rule(drp, rid, &prefix, pid, 0, fl_it->trec + i,
if (drb.add_rule(drp, rid, &prefix, pid, 0, trec,
(void*)(&fl_it->thr[i])) != 0) {

LM_ERR("Cannot add rule in dr <%u>. Skipping...\n", rid);
Expand Down

0 comments on commit 9b96e85

Please sign in to comment.