From 91827b60ff86c95096180f624c019e0ceec09ec1 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Thu, 10 Nov 2022 20:23:06 +0100 Subject: [PATCH] feat(alerts): fixed query generator --- api/chalicelib/core/alerts_processor.py | 15 ++++++++++----- api/chalicelib/core/webhook.py | 4 ++-- ee/api/chalicelib/core/alerts_processor.py | 15 ++++++++++----- ee/api/chalicelib/core/webhook.py | 4 ++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/api/chalicelib/core/alerts_processor.py b/api/chalicelib/core/alerts_processor.py index 87599bbf3..c075d8df0 100644 --- a/api/chalicelib/core/alerts_processor.py +++ b/api/chalicelib/core/alerts_processor.py @@ -129,7 +129,7 @@ def Build(a): if a["seriesId"] is not None: q += f""" FROM ({subQ}) AS stat""" else: - q += f""" FROM ({subQ} {"AND timestamp >= % (startDate)s AND timestamp <= % (now)s" if is_ss else ""} + q += f""" FROM ({subQ} {"AND timestamp >= %(startDate)s AND timestamp <= %(now)s" if is_ss else ""} {"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}) AS stat""" params = {**params, **full_args, "startDate": TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000} else: @@ -146,7 +146,7 @@ def Build(a): AND timestamp<=%(now)s {"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}""" params["startDate"] = TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000 - sub2 = f"""{subQ} {"AND timestamp < % (startDate)s AND timestamp >= % (timestamp_sub2)s" if is_ss else ""} + sub2 = f"""{subQ} {"AND timestamp < %(startDate)s AND timestamp >= %(timestamp_sub2)s" if is_ss else ""} {"AND start_ts < %(startDate)s AND start_ts >= %(timestamp_sub2)s" if j_s else ""}""" params["timestamp_sub2"] = TimeUTC.now() - 2 * a["options"]["currentPeriod"] * 60 * 1000 sub1 = f"SELECT (( {sub1} )-( {sub2} )) AS value" @@ -163,10 +163,10 @@ def Build(a): - (a["options"]["currentPeriod"] + a["options"]["currentPeriod"]) \ * 60 * 1000} else: - sub1 = f"""{subQ} {"AND timestamp >= % (startDate)s AND timestamp <= % (now)s" if is_ss else ""} + sub1 = f"""{subQ} {"AND timestamp >= %(startDate)s AND timestamp <= %(now)s" if is_ss else ""} {"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}""" params["startDate"] = TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000 - sub2 = f"""{subQ} {"AND timestamp < % (startDate)s AND timestamp >= % (timestamp_sub2)s" if is_ss else ""} + sub2 = f"""{subQ} {"AND timestamp < %(startDate)s AND timestamp >= %(timestamp_sub2)s" if is_ss else ""} {"AND start_ts < %(startDate)s AND start_ts >= %(timestamp_sub2)s" if j_s else ""}""" params["timestamp_sub2"] = TimeUTC.now() \ - (a["options"]["currentPeriod"] + a["options"]["currentPeriod"]) * 60 * 1000 @@ -184,7 +184,12 @@ def process(): if can_check(alert): logging.info(f"Querying alertId:{alert['alertId']} name: {alert['name']}") query, params = Build(alert) - query = cur.mogrify(query, params) + try: + query = cur.mogrify(query, params) + except Exception as e: + logging.error(f"!!!Error while building alert query for alertId:{alert['alertId']}") + logging.error(e) + continue logging.debug(alert) logging.debug(query) try: diff --git a/api/chalicelib/core/webhook.py b/api/chalicelib/core/webhook.py index d7f8bf671..d74271d58 100644 --- a/api/chalicelib/core/webhook.py +++ b/api/chalicelib/core/webhook.py @@ -160,8 +160,8 @@ def __trigger(hook, data): r = requests.post(url=hook["endpoint"], json=data, headers=headers) if r.status_code != 200: - logging.error("=======> webhook: something went wrong") - logging.error(r) + logging.error("=======> webhook: something went wrong for:") + logging.error(hook) logging.error(r.status_code) logging.error(r.text) return diff --git a/ee/api/chalicelib/core/alerts_processor.py b/ee/api/chalicelib/core/alerts_processor.py index be7f10f89..55e330df9 100644 --- a/ee/api/chalicelib/core/alerts_processor.py +++ b/ee/api/chalicelib/core/alerts_processor.py @@ -134,7 +134,7 @@ def Build(a): if a["seriesId"] is not None: q += f""" FROM ({subQ}) AS stat""" else: - q += f""" FROM ({subQ} {"AND timestamp >= % (startDate)s AND timestamp <= % (now)s" if is_ss else ""} + q += f""" FROM ({subQ} {"AND timestamp >= %(startDate)s AND timestamp <= %(now)s" if is_ss else ""} {"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}) AS stat""" params = {**params, **full_args, "startDate": TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000} else: @@ -151,7 +151,7 @@ def Build(a): AND timestamp<=%(now)s {"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}""" params["startDate"] = TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000 - sub2 = f"""{subQ} {"AND timestamp < % (startDate)s AND timestamp >= % (timestamp_sub2)s" if is_ss else ""} + sub2 = f"""{subQ} {"AND timestamp < %(startDate)s AND timestamp >= %(timestamp_sub2)s" if is_ss else ""} {"AND start_ts < %(startDate)s AND start_ts >= %(timestamp_sub2)s" if j_s else ""}""" params["timestamp_sub2"] = TimeUTC.now() - 2 * a["options"]["currentPeriod"] * 60 * 1000 sub1 = f"SELECT (( {sub1} )-( {sub2} )) AS value" @@ -168,10 +168,10 @@ def Build(a): - (a["options"]["currentPeriod"] + a["options"]["currentPeriod"]) \ * 60 * 1000} else: - sub1 = f"""{subQ} {"AND timestamp >= % (startDate)s AND timestamp <= % (now)s" if is_ss else ""} + sub1 = f"""{subQ} {"AND timestamp >= %(startDate)s AND timestamp <= %(now)s" if is_ss else ""} {"AND start_ts >= %(startDate)s AND start_ts <= %(now)s" if j_s else ""}""" params["startDate"] = TimeUTC.now() - a["options"]["currentPeriod"] * 60 * 1000 - sub2 = f"""{subQ} {"AND timestamp < % (startDate)s AND timestamp >= % (timestamp_sub2)s" if is_ss else ""} + sub2 = f"""{subQ} {"AND timestamp < %(startDate)s AND timestamp >= %(timestamp_sub2)s" if is_ss else ""} {"AND start_ts < %(startDate)s AND start_ts >= %(timestamp_sub2)s" if j_s else ""}""" params["timestamp_sub2"] = TimeUTC.now() \ - (a["options"]["currentPeriod"] + a["options"]["currentPeriod"]) * 60 * 1000 @@ -189,7 +189,12 @@ def process(): if can_check(alert): logging.info(f"Querying alertId:{alert['alertId']} name: {alert['name']}") query, params = Build(alert) - query = cur.mogrify(query, params) + try: + query = cur.mogrify(query, params) + except Exception as e: + logging.error(f"!!!Error while building alert query for alertId:{alert['alertId']}") + logging.error(e) + continue logging.debug(alert) logging.debug(query) try: diff --git a/ee/api/chalicelib/core/webhook.py b/ee/api/chalicelib/core/webhook.py index fdec148dc..4ef0be6a9 100644 --- a/ee/api/chalicelib/core/webhook.py +++ b/ee/api/chalicelib/core/webhook.py @@ -166,8 +166,8 @@ def __trigger(hook, data): r = requests.post(url=hook["endpoint"], json=data, headers=headers) if r.status_code != 200: - logging.error("=======> webhook: something went wrong") - logging.error(r) + logging.error("=======> webhook: something went wrong for:") + logging.error(hook) logging.error(r.status_code) logging.error(r.text) return