Implement getting alerts in overview

This commit is contained in:
Mark Veidemanis 2022-08-03 09:24:13 +01:00
parent f52d01d6f7
commit 04d4cee222
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 174 additions and 2 deletions

View File

@ -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(),

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,92 @@
<div id="alerts">
{% include 'manage/threshold/partials/notify.html' %}
{% if alerts is not None %}
<div class="icons">
<button
class="button is-small">
<span class="icon" data-tooltip="Conn">
<i class="fa-solid fa-cloud-exclamation"></i>
</span>
</button>
<button
class="button is-small">
<span class="icon" data-tooltip="Highlight">
<i class="fa-solid fa-square-quote"></i>
</span>
</button>
<button
class="button is-small">
<span class="icon" data-tooltip="ZNC">
<i class="fa-brands fa-unity"></i>
</span>
</button>
<button
class="button is-small">
<span class="icon" data-tooltip="Query">
<i class="fa-solid fa-inbox"></i>
</span>
</button>
<button
class="button is-small">
<span class="icon" data-tooltip="Self">
<i class="fa-solid fa-message-bot"></i>
</span>
</button>
</div>
<div class="content" style="max-height: 30em; overflow: auto;">
<div class="table-container">
<table class="table is-fullwidth is-hoverable">
<thead>
<th>ts</th>
<th>name</th>
<th>type</th>
<th>msg</th>
</thead>
<tbody>
{% for alert in alerts %}
<tr>
<td>
<p>{{ alert.date }}</p>
<p>{{ alert.time }}</p>
</td>
<td>
{{ alert.net }}/{{ alert.num }}
<td>
{% if alert.type == 'conn' %}
<span class="icon" data-tooltip="Conn">
<i class="fa-solid fa-cloud-exclamation"></i>
</span>
{% elif alert.type == 'highlight' %}
<span class="icon" data-tooltip="Highlight">
<i class="fa-solid fa-square-quote"></i>
</span>
{% elif alert.type == 'znc' %}
<span class="icon" data-tooltip="ZNC">
<i class="fa-brands fa-unity"></i>
</span>
{% elif alert.type == 'query' %}
<span class="icon" data-tooltip="Query">
<i class="fa-solid fa-inbox"></i>
</span>
{% elif alert.type == 'self' %}
<span class="icon" data-tooltip="Self">
<i class="fa-solid fa-message-bot"></i>
</span>
{% else %}
{{ alert.type }}
{% endif %}
</td>
<td style="max-width: 10em" class="wrap">{{ alert.msg }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
</div>

View File

@ -37,6 +37,15 @@
hx-swap="outerHTML">
</div>
<div
style="display: none;"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'threshold_irc_overview_alerts' %}"
hx-trigger="load"
hx-target="#alerts"
hx-swap="outerHTML">
</div>
<div class="columns">
<div class="column">
<div class="box">
@ -62,7 +71,6 @@
<div class="column">
<div class="box">
<div id="alerts">
Alerts here
</div>
</div>
</div>

View File

@ -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"