diff --git a/src/service/clmcservice/alertsapi/utilities.py b/src/service/clmcservice/alertsapi/utilities.py index 41890a979f157c0b926a0656c7bccb52d906cb25..a2eec79beb2af43a3ec605656264da70bbbcbec9 100644 --- a/src/service/clmcservice/alertsapi/utilities.py +++ b/src/service/clmcservice/alertsapi/utilities.py @@ -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 } diff --git a/src/service/resources/TICKscript/relative-template.tick b/src/service/resources/TICKscript/relative-template.tick index c9d47bd34807abfc27b7a8080ac6e37c2906e535..1e71cf582e8db60f1e4228056ea29a620b40b648 100644 --- a/src/service/resources/TICKscript/relative-template.tick +++ b/src/service/resources/TICKscript/relative-template.tick @@ -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)