Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the dumbest possible visualization of happy/sad article reactions #133

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions portmap/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
import textwrap

from django.contrib import admin
from django.contrib.admin.decorators import register
from django.contrib.auth.admin import UserAdmin
from django.contrib.admin.views.decorators import staff_member_required
from django.db.models import Count
from django.db.models import Count, Q
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.urls import reverse, path
Expand Down Expand Up @@ -143,4 +144,18 @@ def analytics(request):
for view in TrackArticleView.objects.select_related("article").all():
datatype_interest_counts[view.article.datatype]['views'] += 1

return TemplateResponse(request, "admin/analytics.html", {'stats': json.dumps({'datatypeInterest': datatype_interest_counts, 'providerQueriesByDatatype': datatype_provider_query_counts})})

sentiment_query = Article.objects.annotate(happy_count=Count("feedback", filter=Q(feedback__reaction='happy')),
sad_count = Count("feedback", filter=Q(feedback__reaction='sad')))
article_sentiment = [{'name': article.name,
'title': textwrap.shorten(article.title, width=50),
'happy_count': range(article.happy_count),
'sad_count': range(article.sad_count)} for article in sentiment_query.order_by('-sad_count')]


stats = json.dumps({
'datatypeInterest': datatype_interest_counts,
'providerQueriesByDatatype': datatype_provider_query_counts
})

return TemplateResponse(request, "admin/analytics.html", {'stats': stats, 'article_sentiment': article_sentiment})
15 changes: 15 additions & 0 deletions templates/admin/analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ <h1>Queried source and destinations by datatype</h1>
<canvas id="datatype-destinations" width="800"></canvas>
<br >
<canvas id="datatype-transitions" width="800"></canvas>
<br >
<h1>Sentiment by article</h1>
<div id="datatype-sentiment" width="800">
<table>
{% for article in article_sentiment %}
<tr>
<td><a href="/articles/{{ article.name }}">
{{ article.title }}</a></td>
<td>{% for i in article.sad_count %}❌{% endfor %}
<br/>{% for i in article.happy_count %}✅{% endfor %}</td>
</tr>
{% endfor %}
</table>
</div>

<script>
window.stats = JSON.parse('{{ stats | safe }}');
</script>
Expand Down
Loading