The goal of this TICK script is to assign threshold base on the site’s size rating. For example if site = 1, then threshold is -50. My batch also have a field call week2week change percentage (base on past data( shift()) for each site, e.g, if the change percentage is 100% + threshold (-50) = alerting threshold is 50%.
I want to write an alertnode that would say if current week2week change percentage > 50%, then alert. But I also wanted to use the stateCount or stateDuration, so that it eliminate any false positive.
I am not sure if my approach to this is correct.
var past = batch
|query('''
SELECT last(week2week_change), last(site_size)
FROM "kapacitor"."default"."traffic_analysis"
''')
.cluster(out_cluster)
.groupBy(domain, locale)
.period(2m)
.every(1m)
.offset(2m)
.align()
|shift(2m)
// From the analysis measurement, get site size and apply threshold base on size ranking
var size = past
|eval(lambda: if("last_1" == 1, -50.0, if("last_1" == 2, -30.0, if("last_1" == 3, -25.0, if("last_1" == 4, -20.0, if("last_1" == 5, -17.0, if("last_1" == 6, -16.0, if("last_1" == 7, -14.0, if("last_1" == 8, -12.0, if("last_1" == 9, -10.0, -8.0))))))))))
.as('percent')
|last('percent')
.as('threshold')
// Mean of past data
var past_mean = past
|mean('last')
.as('past_w2w_change')
// Get current data from analysis measurement
var current = batch
|query('''
SELECT week2week_change
FROM "kapacitor"."default"."traffic_analysis"
''')
.cluster(out_cluster)
.groupBy(groups)
.period(2m)
.every(1m)
.align()
|mean('week2week_change')
.as('curr_w2w_change')
// Join site size threshold and past mean data into a single point
var pastData = size
|join(past_mean, current)
.as('threshold', 'past_mean', 'current')
.tolerance(tolerence)
|eval(lambda: (float("past_mean.past_w2w_change") + float("threshold.threshold")), lambda: float("past_mean.past_w2w_change"), lambda: float("threshold.threshold"), lambda: float("current.curr_w2w_change")
.as('alert_threshold', 'past_w2w_change', 'size_threshold', 'current_w2w_change')