Skip to content

Commit 51f49e9

Browse files
authored
Admin: calculate stats from the Admin page (mozilla#4063)
1 parent 8b2b7a4 commit 51f49e9

6 files changed

Lines changed: 101 additions & 5 deletions

File tree

pontoon/administration/static/css/admin.css

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
.add {
2-
float: right;
3-
}
4-
51
menu.controls {
62
display: flex;
73
align-items: center;
84
gap: 10px;
95
}
106

7+
menu.controls .search-wrapper {
8+
flex: 1;
9+
}
10+
11+
menu.controls #add-project,
12+
menu.controls #toggle-projects {
13+
flex-shrink: 0;
14+
width: 200px;
15+
}
16+
1117
.disabled-projects {
1218
display: none;
1319
}
@@ -24,3 +30,14 @@ menu.controls {
2430
.request-toggle.back:before {
2531
content: '';
2632
}
33+
34+
#recalculate-stats {
35+
float: right;
36+
width: 200px;
37+
background: var(--button-background-2);
38+
color: var(--light-grey-7);
39+
}
40+
41+
#recalculate-stats:hover {
42+
background: var(--button-background-hover-2);
43+
}

pontoon/administration/static/js/admin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,33 @@ document.addEventListener('DOMContentLoaded', () => {
1818
toggleBtn.dataset.showDisabled = (!showingDisabled).toString();
1919
});
2020
});
21+
22+
$(function () {
23+
$('#recalculate-stats').on('click', function (e) {
24+
e.preventDefault();
25+
26+
const button = $(this),
27+
title = button.html();
28+
29+
if (button.is('.in-progress')) {
30+
return;
31+
}
32+
33+
button.addClass('in-progress').html('Calculating...');
34+
35+
$.ajax({
36+
url: '/admin/calculate-stats/',
37+
success() {
38+
button.html('Done');
39+
},
40+
error() {
41+
button.html('Whoops!');
42+
},
43+
complete() {
44+
setTimeout(function () {
45+
button.removeClass('in-progress').html(title);
46+
}, 2000);
47+
},
48+
});
49+
});
50+
});

pontoon/administration/tasks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import logging
2+
3+
from celery import shared_task
4+
5+
from django.core.management import call_command
6+
7+
from pontoon.base.tasks import PontoonTask
8+
9+
10+
log = logging.getLogger(__name__)
11+
12+
13+
@shared_task(base=PontoonTask, name="calculate_stats")
14+
def calculate_stats_task():
15+
try:
16+
call_command("calculate_stats")
17+
except Exception as err:
18+
log.error(f"Calculate Stats failed: {err}")

pontoon/administration/templates/admin.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
{% block class %}admin{% endblock %}
2020

2121
{% block heading %}
22-
{{ Heading.heading(title='Admin', subtitle="Don't mess it up. Please.") }}
22+
<section id="heading">
23+
<div class="container clearfix">
24+
<div class="controls">
25+
<button id="recalculate-stats" class="button">
26+
Re-calculate stats
27+
</button>
28+
</div>
29+
<h1 id="title">Admin</h1>
30+
<h2 id="subtitle">Don't mess it up. Please.</h2>
31+
</div>
32+
</section>
2333
{% endblock %}
2434

2535
{% block bottom %}
@@ -38,6 +48,7 @@
3848
</div>
3949
<a
4050
class="add button small"
51+
id="add-project"
4152
href="{{ url('pontoon.admin.project.new') }}"
4253
>Add new project</a
4354
>

pontoon/administration/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
]
3737
),
3838
),
39+
# Calculate Stats
40+
path(
41+
"calculate-stats/",
42+
views.manually_calculate_stats,
43+
name="pontoon.admin.calculate-stats",
44+
),
3945
# AJAX view: Get slug
4046
path("get-slug/", views.get_slug, name="pontoon.admin.project.slug"),
4147
# AJAX view: Get project locales

pontoon/administration/views.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
RepositoryInlineFormSet,
1919
TagInlineFormSet,
2020
)
21+
from pontoon.administration.tasks import calculate_stats_task
2122
from pontoon.base import utils
2223
from pontoon.base.models import (
2324
Entity,
@@ -544,6 +545,19 @@ def manage_project_strings(request, slug=None):
544545
return render(request, "admin_project_strings.html", data)
545546

546547

548+
@login_required(redirect_field_name="", login_url="/403")
549+
@require_AJAX
550+
def manually_calculate_stats(request):
551+
if not request.user.has_perm("base.can_manage_project"):
552+
return HttpResponseForbidden(
553+
"Forbidden: You don't have permission for calculating statistics"
554+
)
555+
556+
calculate_stats_task.delay()
557+
558+
return HttpResponse("ok")
559+
560+
547561
@login_required(redirect_field_name="", login_url="/403")
548562
@require_AJAX
549563
def manually_sync_project(request, slug):

0 commit comments

Comments
 (0)