From 0c333847f750d258cac1158cd2e665b5a736b414 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Wed, 22 Aug 2018 11:28:31 +0100
Subject: [PATCH] Fixes task not executing bug in the alerts API

---
 src/service/MANIFEST.in                        |  6 +++---
 src/service/clmcservice/alertsapi/tests.py     |  1 +
 src/service/clmcservice/alertsapi/utilities.py | 18 +++++++++++++++++-
 src/service/clmcservice/alertsapi/views.py     |  8 +++++++-
 .../flame_clmc_alerts_definitions.yaml         |  0
 5 files changed, 28 insertions(+), 5 deletions(-)
 rename src/service/clmcservice/{ => static}/flame_clmc_alerts_definitions.yaml (100%)

diff --git a/src/service/MANIFEST.in b/src/service/MANIFEST.in
index 250b3a9..094e7ae 100644
--- a/src/service/MANIFEST.in
+++ b/src/service/MANIFEST.in
@@ -1,6 +1,6 @@
 include VERSION
-include .coveragerc
+include *coveragerc
 include network_config.json
 include *.ini
-include clmcservice/flame_clmc_alerts_definitions.yaml
-recursive-include resources/TICKscript *_template.tick
\ No newline at end of file
+include clmcservice/static/*
+include resources/TICKscript/*-template.tick
\ No newline at end of file
diff --git a/src/service/clmcservice/alertsapi/tests.py b/src/service/clmcservice/alertsapi/tests.py
index e5bf014..b04601f 100644
--- a/src/service/clmcservice/alertsapi/tests.py
+++ b/src/service/clmcservice/alertsapi/tests.py
@@ -172,6 +172,7 @@ class TestAlertsConfigurationAPI(object):
                     kapacitor_response_json = kapacitor_response.json()
                     assert "link" in kapacitor_response_json, "Incorrect response from kapacitor for alert with ID {0} - test file {1}".format(alert_id, test_file_path)
                     assert kapacitor_response_json["status"] == "enabled", "Alert with ID {0} was created but is disabled - test file {1}".format(alert_id, test_file_path)
+                    assert kapacitor_response_json["executing"], "Alert with ID {0} was created and is enabled, but is not executing - test file {1}".format(alert_id, test_file_path)
 
                 # check that all topic IDs were registered within Kapacitor
                 topic_ids = list(topic_handlers.keys())
diff --git a/src/service/clmcservice/alertsapi/utilities.py b/src/service/clmcservice/alertsapi/utilities.py
index f741e5a..23a5dcc 100644
--- a/src/service/clmcservice/alertsapi/utilities.py
+++ b/src/service/clmcservice/alertsapi/utilities.py
@@ -30,7 +30,7 @@ from os.path import join
 from clmcservice import ROOT_DIR
 
 
-CLMC_ALERTS_TOSCA_DEFINITIONS_REL_PATH = ["flame_clmc_alerts_definitions.yaml"]
+CLMC_ALERTS_TOSCA_DEFINITIONS_REL_PATH = ["static", "flame_clmc_alerts_definitions.yaml"]
 
 CLMC_ALERTS_TOSCA_DEFINITIONS_ABS_PATH = join(ROOT_DIR, *CLMC_ALERTS_TOSCA_DEFINITIONS_REL_PATH)
 
@@ -78,6 +78,22 @@ class TICKScriptTemplateFiller:
     A utility class used for TICK script templates filtering.
     """
 
+    # 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": "=="}
+
+    @staticmethod
+    def get_comparison_operator(template_type):
+        """
+        Get the correct comparison operator depending on the template type, if template type not recognized, return "=="
+
+        :param template_type: one of the template types, that are created within kapacitor
+
+        :return: the comparison operator that should be used in the template to build the where clause
+        """
+
+        return TICKScriptTemplateFiller._TEMPLATE_COMPARISON_OPERATOR.get(template_type, "==")
+
     @staticmethod
     def fill_template_vars(template_type, **kwargs):
         """
diff --git a/src/service/clmcservice/alertsapi/views.py b/src/service/clmcservice/alertsapi/views.py
index 01c91b5..00be03f 100644
--- a/src/service/clmcservice/alertsapi/views.py
+++ b/src/service/clmcservice/alertsapi/views.py
@@ -112,7 +112,13 @@ class AlertsConfigurationAPI(object):
                 where_clause = None
                 if "resource_type" in trigger.trigger_tpl["condition"]:
                     tags = condition["resource_type"]
-                    where_clause = " AND ".join(map(lambda tag_name: '"{0}"==\'{1}\''.format(tag_name, tags[tag_name]), tags))
+
+                    # NOTE: if the template has its where clause defined as lambda (stream templates), then use "==" as comparison operator,
+                    #       else if the template's where clause is defined as a string (batch templates), then use "=" as comparison operator
+                    comparison_operator = TICKScriptTemplateFiller.get_comparison_operator(event_type)  # retrieves the correct comparison operator to use for building the where clause
+
+                    # build up the where clause from the tags dictionary
+                    where_clause = " AND ".join(map(lambda tag_name: '"{0}"{1}\'{2}\''.format(tag_name, comparison_operator, tags[tag_name]), tags))
 
                 comparison_operator = COMPARISON_OPERATORS[condition.get("comparison_operator", "gte")]  # if not specified, use "gte" (>=)
 
diff --git a/src/service/clmcservice/flame_clmc_alerts_definitions.yaml b/src/service/clmcservice/static/flame_clmc_alerts_definitions.yaml
similarity index 100%
rename from src/service/clmcservice/flame_clmc_alerts_definitions.yaml
rename to src/service/clmcservice/static/flame_clmc_alerts_definitions.yaml
-- 
GitLab