Statistics: Add .csv download

This commit is contained in:
rubenwardy 2022-11-08 18:47:28 +00:00
parent 5fc603682d
commit 20bf16abbf
4 changed files with 33 additions and 4 deletions

@ -55,7 +55,7 @@ def get_package_tabs(user: User, package: Package):
{
"id": "stats",
"title": gettext("Statistics"),
"url": package.getURL("packages.stats")
"url": package.getURL("packages.statistics")
},
{
"id": "share",

@ -16,7 +16,7 @@
import typing
from urllib.parse import quote as urlescape
from flask import render_template
from flask import render_template, make_response
from celery import uuid
from flask_wtf import FlaskForm
from flask_login import login_required
@ -712,6 +712,31 @@ def game_support(package):
@bp.route("/packages/<author>/<name>/stats/")
@is_package_page
def stats(package):
def statistics(package):
return render_template("packages/stats.html",
package=package, tabs=get_package_tabs(current_user, package), current_tab="stats")
@bp.route("/packages/<author>/<name>/stats.csv")
@is_package_page
def stats_csv(package):
stats: List[PackageDailyStats] = package.daily_stats.order_by(db.asc(PackageDailyStats.date)).all()
columns = ["platform_minetest", "platform_other", "reason_new",
"reason_dependency", "reason_update"]
result = "Date, " + ", ".join(columns) + "\n"
for stat in stats:
stat: PackageDailyStats
result += stat.date.isoformat()
for i, key in enumerate(columns):
result += ", " + str(getattr(stat, key))
result += "\n"
date = datetime.datetime.utcnow().date()
res = make_response(result, 200)
res.headers["Content-Disposition"] = f"attachment; filename={package.author.username}_{package.name}_stats_{date.isoformat()}.csv"
res.headers["Content-type"] = "text/csv"
return res

@ -11,6 +11,10 @@
{% endblock %}
{% block content %}
<a class="btn btn-secondary float-right" href="{{ package.getURL('packages.stats_csv') }}">
<i class="fas fa-download mr-1"></i>
{{ _("Download (.csv)") }}
</a>
<h2 class="mt-0">{{ _("Statistics") }}</h2>
<noscript>
<p class="alert alert-danger">

@ -206,7 +206,7 @@
<span class="count">{{ _("Issue Tracker") }}</span>
</a>
{% endif %}
<a class="btn" href="{{ package.getURL('packages.stats') }}">
<a class="btn" href="{{ package.getURL('packages.statistics') }}">
<i class="fas fa-chart-line"></i>
<span class="count">{{ _("Statistics") }}</span>
</a>