Add tags for number of users and channels

This commit is contained in:
Mark Veidemanis 2022-07-21 13:51:48 +01:00
parent eb039af9f2
commit f0c548db6f
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 79 additions and 7 deletions

View File

@ -6,7 +6,13 @@ from django.shortcuts import render
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
from core.lib.threshold import annotate_online, get_chans, get_users
from core.lib.threshold import (
annotate_num_chans,
annotate_num_users,
annotate_online,
get_chans,
get_users,
)
logger = logging.getLogger(__name__)
@ -87,6 +93,8 @@ class ThresholdInfoModal(LoginRequiredMixin, APIView):
channel = request.data["channel"]
channels = get_chans(net, [nick])
users = get_users(net, [channel])
num_users = annotate_num_users(net, channels)
num_chans = annotate_num_chans(net, users)
if channels:
inter_users = get_users(net, channels)
else:
@ -103,5 +111,7 @@ class ThresholdInfoModal(LoginRequiredMixin, APIView):
"users": users,
"inter_chans": inter_chans,
"inter_users": inter_users,
"num_users": num_users,
"num_chans": num_chans,
}
return render(request, self.template_name, context)

View File

@ -50,9 +50,10 @@ def threshold_request(url, data):
return False
try:
response = r.json()
sort_data(response)
except JSONDecodeError:
logging.error(f"Invalid JSON response: {r.text}")
sort_data(response)
return False
return response
@ -81,3 +82,21 @@ def annotate_online(net, query):
if not online_info:
return {}
return online_info
def annotate_num_users(net, query):
url = "num_users"
payload = {"net": net, "query": query}
user_num_map = threshold_request(url, payload)
if not user_num_map:
return {}
return user_num_map
def annotate_num_chans(net, query):
url = "num_chans"
payload = {"net": net, "query": query}
chan_num_map = threshold_request(url, payload)
if not chan_num_map:
return {}
return chan_num_map

View File

@ -1,3 +1,5 @@
{% load index %}
<script>
var modal = document.querySelector('.modal'); // assuming you have only 1
var html = document.querySelector('html');
@ -67,6 +69,11 @@
<i class="fa-solid fa-hashtag" aria-hidden="true"></i>
</span>
{{ channel }}
{% if nick in num_chans %}
<span class="tag">
{{ num_users|index:channel }}
</span>
{% endif %}
</a>
{% endfor %}
</div>
@ -78,6 +85,11 @@
<i class="fa-solid fa-user" aria-hidden="true"></i>
</span>
{{ user }}
{% if channel in num_users %}
<span class="tag">
{{ num_chans|index:user }}
</span>
{% endif %}
</a>
{% endfor %}
</div>

View File

@ -77,7 +77,7 @@
<p>{{ item.date }}</p>
<p>{{ item.time }}</p>
</td>
<td>{{ item.msg }}</td>
<td style="max-width: 10em">{{ item.msg }}</td>
<td>{{ item.host }}</td>
<td>
{% if item.online is True %}
@ -96,6 +96,11 @@
</span>
{{ item.nick }}
{% endif %}
{% if item.num_chans is not None %}
<span class="tag">
{{ item.num_chans }}
</span>
{% endif %}
</td>
<td>
{% if item.src == 'irc' %}
@ -105,13 +110,20 @@
hx-vals='{"net": "{{ item.net }}", "nick": "{{ item.nick }}", "channel": "{{ item.channel }}"}'
hx-target="#modals-here"
hx-trigger="click"
class="btn btn-primary">
class="button is-small">
Information
</button>
<div id="modals-here"></div>
{% endif %}
</td>
<td>{{ item.channel }}</td>
<td>
{{ item.channel }}
{% if item.num_users is not None %}
<span class="tag">
{{ item.num_users }}
</span>
{% endif %}
</td>
<td>{{ item.net }}</td>
</tr>
{% endfor %}

View File

@ -0,0 +1,7 @@
from django import template
register = template.Library()
@register.filter
def index(h, key):
return h[key]

View File

@ -7,7 +7,7 @@ from django.shortcuts import render
from django.views import View
from core.lib.opensearch import initialise_opensearch, run_main_query
from core.lib.threshold import annotate_online
from core.lib.threshold import annotate_num_chans, annotate_num_users, annotate_online
client = initialise_opensearch()
@ -42,14 +42,26 @@ def query_results(request, post_params, api=False):
if "net" in x:
nets.add(x["net"])
# Annotate the online attribute from Threshold
for net in nets:
# Annotate the online attribute from Threshold
online_info = annotate_online(
net, [x["nick"] for x in results_parsed if x["src"] == "irc"]
)
# Annotate the number of users in the channel
num_users = annotate_num_users(
net, [x["channel"] for x in results_parsed if x["src"] == "irc"]
)
# Annotate the number channels the user is on
num_chans = annotate_num_chans(
net, [x["nick"] for x in results_parsed if x["src"] == "irc"]
)
for item in results_parsed:
if item["nick"] in online_info:
item["online"] = online_info[item["nick"]]
if item["channel"] in num_users:
item["num_users"] = num_users[item["channel"]]
if item["nick"] in num_chans:
item["num_chans"] = num_chans[item["nick"]]
context = {
"query": query,