Skip to content
Snippets Groups Projects
Commit d638b408 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

Updates relative template

parent 35880b56
No related branches found
No related tags found
No related merge requests found
......@@ -137,7 +137,7 @@ class TICKScriptTemplateFiller:
# a class variable used to hold the comparison operator used to build the where clause in TICK script templates,
# these differ if the where clause is built as a string opposed to when it is build as a lambda
_TEMPLATE_COMPARISON_OPERATOR = {"threshold": "=", "relative": "==", "deadman": "=="}
_TEMPLATE_COMPARISON_OPERATOR = {"threshold": "=", "relative": "=", "deadman": "=="}
@staticmethod
def get_comparison_operator(template_type):
......@@ -186,7 +186,7 @@ class TICKScriptTemplateFiller:
:return: a dictionary object ready to be posted to kapacitor to create a "threshold" task from template.
"""
comparison_lambda = "\"real_value\" {0} {1}".format(comparison_operator, critical_value) # build up lambda string, e.g. "real_value" >= 10
comparison_lambda = '"real_value" {0} {1}'.format(comparison_operator, critical_value) # build up lambda string, e.g. "real_value" >= 10
template_vars = {
"db": {
......@@ -228,7 +228,7 @@ class TICKScriptTemplateFiller:
return template_vars
@staticmethod
def _fill_relative_template_vars(db=None, measurement=None, field=None, critical_value=None, comparison_operator=None,
def _fill_relative_template_vars(db=None, measurement=None, field=None, influx_function=None, critical_value=None, comparison_operator=None,
alert_period=None, topic_id=None, where_clause=None, **kwargs):
"""
Creates a dictionary object ready to be posted to kapacitor to create a "relative" task from template.
......@@ -236,6 +236,7 @@ class TICKScriptTemplateFiller:
:param db: db name
:param measurement: measurement name
:param field: field name
:param influx_function: influx function to use for querying
:param critical_value: critical value to compare with
:param comparison_operator: type of comparison
:param alert_period: alert period to use for relative comparison
......@@ -245,8 +246,6 @@ class TICKScriptTemplateFiller:
:return: a dictionary object ready to be posted to kapacitor to create a "relative" task from template.
"""
select_lambda = '"{0}"'.format(field)
comparison_lambda = '"diff" {0} {1}'.format(comparison_operator, critical_value)
template_vars = {
......@@ -258,9 +257,13 @@ class TICKScriptTemplateFiller:
"type": "string",
"value": measurement
},
"selectLambda": {
"type": "lambda",
"value": select_lambda
"field": {
"type": "string",
"value": field
},
"influxFunction": {
"type": "string",
"value": influx_function
},
"comparisonLambda": {
"type": "lambda",
......@@ -278,7 +281,7 @@ class TICKScriptTemplateFiller:
if where_clause is not None:
template_vars["whereClause"] = {
"type": "lambda",
"type": "string",
"value": where_clause
}
......
......@@ -4,9 +4,11 @@ var rp = 'autogen' // default value for the retention policy
var measurement string
var selectLambda lambda // must be a lambda specifying the field to select e.g. "requests"
var field string
var whereClause = lambda: TRUE // default value is a function which returns TRUE, hence no filtering of the query result
var influxFunction string
var whereClause = 'TRUE' // default value is TRUE, hence no filtering of the query result
var messageValue = 'TRUE' // default value is TRUE, as this is what SFEMC expects as a notification for an event rule
......@@ -17,25 +19,19 @@ var alertPeriod duration
var topicID string
var data = stream
| from()
.database(db)
.retentionPolicy(rp)
.measurement(measurement)
.where(whereClause)
| eval(selectLambda)
.as('value')
var past = data
| shift(alertPeriod)
var current = batch
|query('SELECT ' + influxFunction + '(' + field + ') AS value FROM "' + db + '"."' + rp + '"."' + measurement + '" WHERE ' + whereClause)
.period(alertPeriod)
.every(alertPeriod)
var current = data
var past = current
.offset(alertPeriod)
| shift(alertPeriod)
past
| join(current) // NOTE: join buffers a given data point until a point with the correct timestamp to join on arrives
| join(current)
.as('past', 'current')
| eval(lambda: float("current.value" - "past.value"))
.keep()
.as('diff')
| alert()
.id(topicID)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment