Add user statistics pages

This commit is contained in:
rubenwardy 2022-11-09 18:47:44 +00:00
parent 82cd0aefdf
commit 724b80e91e
5 changed files with 147 additions and 100 deletions

@ -252,3 +252,14 @@ def user_check(username):
next_url = url_for("users.profile", username=username)
return redirect(url_for("tasks.check", id=task.id, r=next_url))
@bp.route("/users/<username>/stats/")
def statistics(username):
user = User.query.filter_by(username=username).first()
if user is None:
abort(404)
downloads = db.session.query(func.sum(Package.downloads)).filter(Package.author==user).one()
return render_template("users/stats.html", user=user, downloads=downloads[0])

@ -0,0 +1,105 @@
{% macro render_package_stats_js() %}
<script src="/static/libs/chart.min.js"></script>
<script src="/static/libs/chartjs-adapter-date-fns.bundle.min.js"></script>
<script src="/static/package_charts.js?v=5"></script>
{% endmacro %}
{% macro render_package_stats(source, downloads) %}
<noscript>
<p class="alert alert-danger">
{{ _("JavaScript is required to display charts and statistics") }}
</p>
</noscript>
<div class="row mb-5">
<div class="col-md-4">
<div class="card h-100">
<div class="card-body align-items-center text-center">
<div class="mt-0 mb-3">
<i class="fas fa-download mr-1"></i>
{{ _("Lifetime unique downloads") }}
</div>
<div class="my-0 h4">
{{ downloads }}
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card h-100">
<div class="card-body align-items-center text-center">
<div class="mt-0 mb-3">
<i class="fas fa-download mr-1"></i>
{{ _("Downloads, past 7 days") }}
</div>
<div class="my-0 h4">
<span id='downloads_total7d'></span>
<small class="text-muted ml-2">
({{ _("%(downloads)s per day", downloads=("<span id='downloads_avg7d'></span>" | safe)) }})
</small>
</div>
</div>
</div>
</div>
<div class="col-md-4" id="downloads30">
<div class="card h-100">
<div class="card-body align-items-center text-center">
<div class="mt-0 mb-3">
<i class="fas fa-download mr-1"></i>
{{ _("Downloads, past 30 days") }}
</div>
<div class="my-0 h4">
<span id='downloads_total30d'></span>
<small class="text-muted ml-2">
({{ _("%(downloads)s per day", downloads=("<span id='downloads_avg30d'></span>" | safe)) }})
</small>
</div>
</div>
</div>
</div>
</div>
<div id="loading">{{ _("Loading...") }}</div>
<div id="empty-view" style="display: none;">
{{ _("No data") }}
</div>
<div id="stats-root" data-source="{{ source }}" style="display: none;">
<h3>{{ _("Downloads by Client") }}</h3>
<p class="text-muted">
{{ _("This is a stacked area graph. For total downloads, look at the combined height.") }}
</p>
<canvas id="chart-platform" class="chart"></canvas>
<h3 class="mt-5">{{ _("Downloads by Reason") }}</h3>
<ul>
<li>{{ _("<b>New Install</b>: the user clicked [Install] inside of Minetest.") }}</li>
<li>{{ _("<b>Dependency</b>: was installed automatically to fulfill a dependency.") }}</li>
<li>{{ _("<b>Update</b>: download was to update the package.") }}</li>
<li>{{ _("<b>Other / Unknown</b>: downloaded by a web browser or an outdated Minetest version (before 5.5).") }}</li>
</ul>
<p class="text-muted">
{{ _("This is a stacked area graph. For total downloads, look at the combined height.") }}
</p>
<canvas id="chart-reason" class="chart"></canvas>
<div class="row mt-4 mb-5">
<div class="col-md-6">
<canvas id="chart-reason-pie" class="chart"></canvas>
</div>
</div>
<h3 style="margin-top: 6em;">{{ _("Need more stats?") }}</h3>
<p>
{{ _("Check out the ContentDB Grafana dashboard for CDB-wide stats") }}
</p>
<p>
<a href="https://monitor.rubenwardy.com/d/3ELzFy3Wz/contentdb" class="btn btn-primary">
{{ _("ContentDB Grafana") }}
</a>
</p>
</div>
{% endmacro %}

@ -4,10 +4,10 @@
{{ _("Statistics") }} - {{ package.title }}
{% endblock %}
{% from "macros/stats.html" import render_package_stats, render_package_stats_js %}
{% block scriptextra %}
<script src="/static/libs/chart.min.js"></script>
<script src="/static/libs/chartjs-adapter-date-fns.bundle.min.js"></script>
<script src="/static/package_charts.js?v=4"></script>
{{ render_package_stats_js() }}
{% endblock %}
{% block content %}
@ -16,100 +16,5 @@
{{ _("Download (.csv)") }}
</a>
<h2 class="mt-0">{{ _("Statistics") }}</h2>
<noscript>
<p class="alert alert-danger">
{{ _("JavaScript is required to display charts and statistics") }}
</p>
</noscript>
<div class="row mb-5">
<div class="col-md-4">
<div class="card h-100">
<div class="card-body align-items-center text-center">
<div class="mt-0 mb-3">
<i class="fas fa-download mr-1"></i>
{{ _("Lifetime unique downloads") }}
</div>
<div class="my-0 h4">
{{ package.downloads }}
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card h-100">
<div class="card-body align-items-center text-center">
<div class="mt-0 mb-3">
<i class="fas fa-download mr-1"></i>
{{ _("Downloads, past 7 days") }}
</div>
<div class="my-0 h4">
<span id='downloads_total7d'></span>
<small class="text-muted ml-2">
({{ _("%(downloads)s per day", downloads=("<span id='downloads_avg7d'></span>" | safe)) }})
</small>
</div>
</div>
</div>
</div>
<div class="col-md-4" id="downloads30">
<div class="card h-100">
<div class="card-body align-items-center text-center">
<div class="mt-0 mb-3">
<i class="fas fa-download mr-1"></i>
{{ _("Downloads, past 30 days") }}
</div>
<div class="my-0 h4">
<span id='downloads_total30d'></span>
<small class="text-muted ml-2">
({{ _("%(downloads)s per day", downloads=("<span id='downloads_avg30d'></span>" | safe)) }})
</small>
</div>
</div>
</div>
</div>
</div>
<div id="loading">{{ _("Loading...") }}</div>
<div id="empty-view" style="display: none;">
{{ _("No data") }}
</div>
<div id="stats-root" data-source="{{ package.getURL('api.package_stats') }}" style="display: none;">
<h3>{{ _("Downloads by Client") }}</h3>
<p class="text-muted">
{{ _("This is a stacked area graph. For total downloads, look at the combined height.") }}
</p>
<canvas id="chart-platform" class="chart"></canvas>
<h3 class="mt-5">{{ _("Downloads by Reason") }}</h3>
<ul>
<li>{{ _("<b>New Install</b>: the user clicked [Install] inside of Minetest.") }}</li>
<li>{{ _("<b>Dependency</b>: was installed automatically to fulfill a dependency.") }}</li>
<li>{{ _("<b>Update</b>: download was to update the package.") }}</li>
<li>{{ _("<b>Other / Unknown</b>: downloaded by a web browser or an outdated Minetest version (before 5.5).") }}</li>
</ul>
<p class="text-muted">
{{ _("This is a stacked area graph. For total downloads, look at the combined height.") }}
</p>
<canvas id="chart-reason" class="chart"></canvas>
<div class="row mt-4 mb-5">
<div class="col-md-6">
<canvas id="chart-reason-pie" class="chart"></canvas>
</div>
</div>
<h3 style="margin-top: 6em;">{{ _("Need more stats?") }}</h3>
<p>
{{ _("Check out the ContentDB Grafana dashboard for CDB-wide stats") }}
</p>
<p>
<a href="https://monitor.rubenwardy.com/d/3ELzFy3Wz/contentdb" class="btn btn-primary">
{{ _("ContentDB Grafana") }}
</a>
</p>
</div>
{{ render_package_stats(package.getURL('api.package_stats'), package.downloads) }}
{% endblock %}

@ -97,10 +97,11 @@
</a>
{% endif %}
{% set package_count = user.packages.filter_by(state='APPROVED').count() %}
<a class="btn" href="{{ url_for('packages.list_all', author=user.username) }}">
<i class="fas fa-box"></i>
<span class="count">
<strong>{{ user.packages.filter_by(state='APPROVED').count() }}</strong>
<strong>{{ package_count }}</strong>
{{ _("packages") }}
</span>
</a>
@ -121,6 +122,15 @@
{{ _("comments") }}
</span>
</a>
{% if package_count %}
<a class="btn" href="{{ url_for('users.statistics', username=user.username) }}">
<i class="fas fa-chart-line"></i>
<span class="count">
{{ _("Statistics") }}
</span>
</a>
{% endif %}
</div>
</div>
</div>

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block title %}
{{ _("Statistics for %(display_name)s's packages", display_name=user.display_name) }}
{% endblock %}
{% from "macros/stats.html" import render_package_stats, render_package_stats_js, render_package_selector %}
{% block scriptextra %}
{{ render_package_stats_js() }}
{% endblock %}
{% block content %}
<h2 class="mt-0">{{ self.title() }}</h2>
{{ render_package_stats(url_for("api.user_stats", username=user.username), downloads) }}
{% endblock %}