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

View File

@ -50,9 +50,10 @@ def threshold_request(url, data):
return False return False
try: try:
response = r.json() response = r.json()
sort_data(response)
except JSONDecodeError: except JSONDecodeError:
logging.error(f"Invalid JSON response: {r.text}") logging.error(f"Invalid JSON response: {r.text}")
sort_data(response) return False
return response return response
@ -81,3 +82,21 @@ def annotate_online(net, query):
if not online_info: if not online_info:
return {} return {}
return online_info 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> <script>
var modal = document.querySelector('.modal'); // assuming you have only 1 var modal = document.querySelector('.modal'); // assuming you have only 1
var html = document.querySelector('html'); var html = document.querySelector('html');
@ -67,6 +69,11 @@
<i class="fa-solid fa-hashtag" aria-hidden="true"></i> <i class="fa-solid fa-hashtag" aria-hidden="true"></i>
</span> </span>
{{ channel }} {{ channel }}
{% if nick in num_chans %}
<span class="tag">
{{ num_users|index:channel }}
</span>
{% endif %}
</a> </a>
{% endfor %} {% endfor %}
</div> </div>
@ -78,6 +85,11 @@
<i class="fa-solid fa-user" aria-hidden="true"></i> <i class="fa-solid fa-user" aria-hidden="true"></i>
</span> </span>
{{ user }} {{ user }}
{% if channel in num_users %}
<span class="tag">
{{ num_chans|index:user }}
</span>
{% endif %}
</a> </a>
{% endfor %} {% endfor %}
</div> </div>

View File

@ -77,7 +77,7 @@
<p>{{ item.date }}</p> <p>{{ item.date }}</p>
<p>{{ item.time }}</p> <p>{{ item.time }}</p>
</td> </td>
<td>{{ item.msg }}</td> <td style="max-width: 10em">{{ item.msg }}</td>
<td>{{ item.host }}</td> <td>{{ item.host }}</td>
<td> <td>
{% if item.online is True %} {% if item.online is True %}
@ -96,6 +96,11 @@
</span> </span>
{{ item.nick }} {{ item.nick }}
{% endif %} {% endif %}
{% if item.num_chans is not None %}
<span class="tag">
{{ item.num_chans }}
</span>
{% endif %}
</td> </td>
<td> <td>
{% if item.src == 'irc' %} {% if item.src == 'irc' %}
@ -105,13 +110,20 @@
hx-vals='{"net": "{{ item.net }}", "nick": "{{ item.nick }}", "channel": "{{ item.channel }}"}' hx-vals='{"net": "{{ item.net }}", "nick": "{{ item.nick }}", "channel": "{{ item.channel }}"}'
hx-target="#modals-here" hx-target="#modals-here"
hx-trigger="click" hx-trigger="click"
class="btn btn-primary"> class="button is-small">
Information Information
</button> </button>
<div id="modals-here"></div> <div id="modals-here"></div>
{% endif %} {% endif %}
</td> </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> <td>{{ item.net }}</td>
</tr> </tr>
{% endfor %} {% 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 django.views import View
from core.lib.opensearch import initialise_opensearch, run_main_query 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() client = initialise_opensearch()
@ -42,14 +42,26 @@ def query_results(request, post_params, api=False):
if "net" in x: if "net" in x:
nets.add(x["net"]) nets.add(x["net"])
# Annotate the online attribute from Threshold
for net in nets: for net in nets:
# Annotate the online attribute from Threshold
online_info = annotate_online( online_info = annotate_online(
net, [x["nick"] for x in results_parsed if x["src"] == "irc"] 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: for item in results_parsed:
if item["nick"] in online_info: if item["nick"] in online_info:
item["online"] = online_info[item["nick"]] 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 = { context = {
"query": query, "query": query,