Skip to content

Commit

Permalink
element: Speed up single element page by using db, removing a section
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Feb 20, 2025
1 parent f233913 commit 7277817
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.1.6 on 2025-02-20 17:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("iati_dashboard", "0003_publisher_activity_files_publisher_file_size_and_more"),
]

operations = [
migrations.AddField(
model_name="publisher",
name="elements",
field=models.GeneratedField(
db_persist=True, expression=models.F("stats_json__elements"), output_field=models.JSONField()
),
),
migrations.AddField(
model_name="publisher",
name="elements_total",
field=models.GeneratedField(
db_persist=True, expression=models.F("stats_json__elements_total"), output_field=models.JSONField()
),
),
]
2 changes: 2 additions & 0 deletions iati_dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def traceable_sum_commitments_and_disbursements_by_publisher_id_denominator(self
"traceable_sum_commitments_and_disbursements_by_publisher_id",
"transaction_months_with_year",
"timelag",
"elements",
"elements_total",
]:
Publisher.add_to_class(
key,
Expand Down
36 changes: 15 additions & 21 deletions iati_dashboard/templates/element.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
as{% extends 'base.html' %}
{% extends 'base.html' %}
{% import '_partials/boxes.html' as boxes with context %}

{% block page_header %}
Expand Down Expand Up @@ -32,21 +32,15 @@ <h3 class="dashboard-panel-heading__title">Publishing this {{ element_or_attribu
<td>Total activities</td>
</thead>
<tbody>
{% for publisher in func.sorted(publishers) %}
{% if publisher in current_stats.inverted_publisher.activities %}
{% for publisher in publishers_with %}
<tr>
<td><a href="{{ url('dash-headlines-publisher-detail', args=[publisher]) }}">{{ publisher }}</a></td>
{% with publisher_inverted=func.get_publisher_stats(publisher, 'inverted-file') %}
<td><a href="#files_{{ publisher }}">{% if 'elements' in publisher_inverted %}{{ publisher_inverted.elements[element]|count }}{% endif %}</a></td>
{% endwith %}
<td>{{ current_stats.inverted_publisher.activity_files.get(publisher)+current_stats.inverted_publisher.organisation_files.get(publisher) }}</td>
{% with publisher_stats=func.get_publisher_stats(publisher) %}
<td>{{ publisher_stats.elements[element] }}</td>
<td>{{ publisher_stats.elements_total[element] }}</td>
{% endwith %}
<td>{{ current_stats.inverted_publisher.activities[publisher] }}</td>
<td><a href="{{ url('dash-headlines-publisher-detail', args=[publisher.short_name]) }}">{{ publisher.short_name }}</a></td>
<td><a href="#files_{{ publisher.short_name }}">{{ publisher.elements[element] }}</a></td>
<td>{{ publisher.activity_files + publisher.organisation_files }}</td>
<td>{{ publisher.elements[element] }}</td>
<td>{{ publisher.elements_total[element] }}</td>
<td>{{ publisher.activities }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
Expand All @@ -68,15 +62,13 @@ <h3 class="dashboard-panel-heading__title">Not publishing this {{ element_or_att
<td>Total organisations</td>
</thead>
<tbody>
{% for publisher in current_stats.inverted_publisher.publishers %}
{% if publisher not in publishers %}
{% for publisher in publishers_without %}
<tr>
<td><a href="{{ url('dash-headlines-publisher-detail', args=[publisher]) }}">{{ publisher }}</a></td>
<td>{{ current_stats.inverted_publisher.activity_files.get(publisher)+current_stats.inverted_publisher.organisation_files.get(publisher) }}</td>
<td>{{ current_stats.inverted_publisher.activities[publisher] }}</td>
<td>{{ current_stats.inverted_publisher.organisations[publisher] }}</td>
<td><a href="{{ url('dash-headlines-publisher-detail', args=[publisher.short_name]) }}">{{ publisher.short_name }}</a></td>
<td>{{ publisher.activity_files + publisher.organisation_files }}</td>
<td>{{ publisher.activities }}</td>
<td>{{ publisher.organisations }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
Expand All @@ -86,6 +78,7 @@ <h3 class="dashboard-panel-heading__title">Not publishing this {{ element_or_att
</div>


{#
<h2>Files</h2>
<table class="table table-striped">
<thead>
Expand All @@ -106,4 +99,5 @@ <h2>Files</h2>
{% endfor %}
</tbody>
</table>
#}
{% endblock %}
13 changes: 8 additions & 5 deletions iati_dashboard/ui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,17 @@ def exploringdata_elements(request):

def exploringdata_element_detail(request, element=None):
template = loader.get_template("element.html")
context = _make_context("elements")
context = _make_context("elements", include_large_dicts=False)

if element not in slugs["element"]["by_slug"]:
context["element"] = element.replace("_", "/")

values = ["id", "short_name", "activities", "organisations", "activity_files", "organisation_files", "elements", "elements_total"]
context["publishers_with"] = models.Publisher.objects.filter(elements__has_key=context["element"]).values(*values)
context["publishers_without"] = models.Publisher.objects.exclude(elements__has_key=context["element"]).values(*values)

if context["publishers_with"].count() == 0:
raise Http404("Unknown element or attribute")

i = slugs["element"]["by_slug"][element]
context["element"] = list(current_stats["inverted_publisher"]["elements"])[i]
context["publishers"] = list(current_stats["inverted_publisher"]["elements"].values())[i]
context["element_or_attribute"] = "attribute" if "@" in context["element"] else "element"

context["breadcrumbs"].append({"view": PAGE_VIEW_NAMES["elements"], "title": '"' + context["element"] + '"'})
Expand Down

0 comments on commit 7277817

Please sign in to comment.