From 04d4cee222b3178df6a44d9f8b53d3c0c2bd6d11 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 3 Aug 2022 09:24:13 +0100 Subject: [PATCH] Implement getting alerts in overview --- app/urls.py | 6 ++ core/lib/manage/threshold.py | 56 +++++++++++ core/lib/opensearch.py | 3 +- .../manage/threshold/irc/overview/alerts.html | 92 +++++++++++++++++++ .../threshold/irc/overview/overview.html | 10 +- core/views/manage/threshold/irc.py | 9 ++ 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 core/templates/manage/threshold/irc/overview/alerts.html diff --git a/app/urls.py b/app/urls.py index 3aec021..97231fa 100644 --- a/app/urls.py +++ b/app/urls.py @@ -40,6 +40,7 @@ from core.views.manage.threshold.irc import ( ThresholdIRCNetworkRelays, ThresholdIRCNetworkRelayStatus, ThresholdIRCNetworks, + ThresholdIRCOverviewAlerts, ThresholdIRCStats, ) @@ -101,6 +102,11 @@ urlpatterns = [ ThresholdIRCStats.as_view(), name="threshold_irc_stats", ), + path( + "manage/threshold/irc/overview/alerts/", + ThresholdIRCOverviewAlerts.as_view(), + name="threshold_irc_overview_alerts", + ), path( "manage/threshold/irc/overview/networks/", ThresholdIRCNetworks.as_view(), diff --git a/core/lib/manage/threshold.py b/core/lib/manage/threshold.py index e289c61..efdecd0 100644 --- a/core/lib/manage/threshold.py +++ b/core/lib/manage/threshold.py @@ -1,5 +1,8 @@ import urllib.parse +from django.conf import settings + +from core.lib.opensearch import client, run_main_query from core.lib.threshold import threshold_request @@ -139,3 +142,56 @@ def del_network(net): payload = {} deleted = threshold_request(url, payload, method="DELETE") return deleted + + +def construct_alert_query(): + # Get the initial query + query = { + "size": 25, + "query": { + "bool": { + "must": [ + {"match": {"src": "irc"}}, + ] + } + }, + "sort": [ + { + "ts": { + "order": "desc", + } + } + ], + } + return query + + +def get_irc_alerts(user): + query = construct_alert_query() + print("QUERY1", query) + results = run_main_query( + client, + user, # passed through run_main_query to filter_blacklisted + query, + custom_query=True, + index=settings.OPENSEARCH_INDEX_INT, + ) + print("ALERTS", results) + if not results: + return [] + results_parsed = [] + if "hits" in results.keys(): + if "hits" in results["hits"]: + for item in results["hits"]["hits"]: + element = item["_source"] + element["id"] = item["_id"] + + # Split the timestamp into date and time + ts = element["ts"] + ts_spl = ts.split("T") + date = ts_spl[0] + time = ts_spl[1] + element["date"] = date + element["time"] = time + results_parsed.append(element) + return results_parsed diff --git a/core/lib/opensearch.py b/core/lib/opensearch.py index 657fd7e..9f1ee43 100644 --- a/core/lib/opensearch.py +++ b/core/lib/opensearch.py @@ -123,7 +123,8 @@ def run_main_query(client, user, query, custom_query=False, index=None, size=Non search_query = construct_query(query, size) try: response = client.search(body=search_query, index=index) - except RequestError: + except RequestError as err: + print(err) return False filter_blacklisted(user, response) return response diff --git a/core/templates/manage/threshold/irc/overview/alerts.html b/core/templates/manage/threshold/irc/overview/alerts.html new file mode 100644 index 0000000..9f68ca6 --- /dev/null +++ b/core/templates/manage/threshold/irc/overview/alerts.html @@ -0,0 +1,92 @@ +
+ {% include 'manage/threshold/partials/notify.html' %} + {% if alerts is not None %} +
+ + + + + + + + + +
+
+ +
+ + + + + + + + + {% for alert in alerts %} + + + + + + {% endfor %} + +
tsnametypemsg
+

{{ alert.date }}

+

{{ alert.time }}

+
+ {{ alert.net }}/{{ alert.num }} + + {% if alert.type == 'conn' %} + + + + {% elif alert.type == 'highlight' %} + + + + {% elif alert.type == 'znc' %} + + + + {% elif alert.type == 'query' %} + + + + {% elif alert.type == 'self' %} + + + + {% else %} + {{ alert.type }} + {% endif %} + {{ alert.msg }}
+
+
+ {% endif %} +
\ No newline at end of file diff --git a/core/templates/manage/threshold/irc/overview/overview.html b/core/templates/manage/threshold/irc/overview/overview.html index ac47272..05d0f63 100644 --- a/core/templates/manage/threshold/irc/overview/overview.html +++ b/core/templates/manage/threshold/irc/overview/overview.html @@ -37,6 +37,15 @@ hx-swap="outerHTML"> +
+
+
@@ -62,7 +71,6 @@
- Alerts here
diff --git a/core/views/manage/threshold/irc.py b/core/views/manage/threshold/irc.py index d2a507c..d470a9f 100644 --- a/core/views/manage/threshold/irc.py +++ b/core/views/manage/threshold/irc.py @@ -16,6 +16,15 @@ class ThresholdIRCStats(SuperUserRequiredMixin, View): return render(request, self.stats_template, context) +class ThresholdIRCOverviewAlerts(SuperUserRequiredMixin, View): + stats_template = "manage/threshold/irc/overview/alerts.html" + + def get(self, request): + alerts = threshold.get_irc_alerts(request.user) + context = {"alerts": alerts} + return render(request, self.stats_template, context) + + class ThresholdIRCNetworks(SuperUserRequiredMixin, View): template_name = "manage/threshold/irc/overview/networks.html"