Skip to content

Commit

Permalink
Add 'most' statistics (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
anorthall committed Mar 31, 2023
1 parent fa284f5 commit 72cd979
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/logger/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ def common_clubs(qs, limit=10):
def common_types(qs, limit=10):
"""Get a list of the most common types in a QuerySet"""
return qs.values("type").annotate(count=Count("type")).order_by("-count")[0:limit]


def most_duration(qs, limit=10):
"""Get trips with the most duration from a QuerySet"""
most_duration = {}
for trip in qs:
if trip.duration:
most_duration[trip] = trip.duration
results = sorted(most_duration.items(), key=lambda x: x[1], reverse=True)[0:limit]

humanised_results = {}
for trip, duration in results:
humanised_results[trip] = humanize.precisedelta(
duration, minimum_unit="minutes", format="%.0f"
)
return humanised_results.items()
229 changes: 229 additions & 0 deletions app/logger/templates/statistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,235 @@ <h5>Most common clubs</h5>
</div>
</div>
</div>

<div class="card mt-4">
<div class="card-header">
Biggest trips
</div>

<div class="card-body">
<div class="row g-3">
<div class="col-12">
{% if most_duration %}
<h5>Longest trips</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Duration</th>
</thead>

<tbody>
{% for trip, duration in most_duration %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ duration }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<p class="m-0">Sorry, no data for this!</p>
{% endif %}
</div>

{% if most_vert_up %}
<div class="col-12 col-lg-6">
<h5>Rope climbed</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Rope climbed</th>
</thead>

<tbody>
{% for trip in most_vert_up %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ trip.vert_dist_up|distformat:dist_format }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}

{% if most_vert_down %}
<div class="col-12 col-lg-6">
<h5>Rope descended</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Rope descended</th>
</thead>

<tbody>
{% for trip in most_vert_down %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ trip.vert_dist_down|distformat:dist_format }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}

{% if most_surveyed %}
<div class="col-12 col-lg-6">
<h5>Surveying</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Surveyed</th>
</thead>

<tbody>
{% for trip in most_surveyed %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ trip.surveyed_dist|distformat:dist_format }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}

{% if most_resurveyed %}
<div class="col-12 col-lg-6">
<h5>Resurveying</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Resurveyed</th>
</thead>

<tbody>
{% for trip in most_resurveyed %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ trip.resurveyed_dist|distformat:dist_format }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}

{% if most_aid %}
<div class="col-12 col-lg-6">
<h5>Aid climbing</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Aid climbed</th>
</thead>

<tbody>
{% for trip in most_aid %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ trip.aid_dist|distformat:dist_format }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}

{% if most_horizontal %}
<div class="col-12 col-lg-6">
<h5>Horizontal distance</h5>
<div class="table-responsive">
<table class="table table-borderless table-hover table-sm">
<thead>
<th scope="col">#</th>
<th scope="col">Trip</th>
<th scope="col">Date</th>
<th scope="col">Horizontal distance</th>
</thead>

<tbody>
{% for trip in most_horizontal %}
<tr>
<td>{{ forloop.counter }}</td>
<td>
<a href="{% url 'log:trip_detail' trip.pk %}">
{{ trip.cave_name }}
</a>
</td>
<td>{{ trip.start|date }}</td>
<td>{{ trip.horizontal_dist|distformat:dist_format }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
</div>
</div>
</div>
{% else %}

<div class="card">
Expand Down
17 changes: 17 additions & 0 deletions app/logger/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ def user_statistics(request):
"common_cavers": statistics.common_cavers(trips),
"common_types": statistics.common_types(trips),
"common_clubs": statistics.common_clubs(trips),
"most_duration": statistics.most_duration(trips),
"most_vert_up": trips.filter(vert_dist_up__gt=0).order_by("-vert_dist_up")[
0:10
],
"most_vert_down": trips.filter(vert_dist_down__gt=0).order_by(
"-vert_dist_down"
)[0:10],
"most_surveyed": trips.filter(surveyed_dist__gt=0).order_by("-surveyed_dist")[
0:10
],
"most_resurveyed": trips.filter(resurveyed_dist__gt=0).order_by(
"-resurveyed_dist"
)[0:10],
"most_aid": trips.filter(aid_dist__gt=0).order_by("-aid_dist")[0:10],
"most_horizontal": trips.filter(horizontal_dist__gt=0).order_by(
"-horizontal_dist"
)[0:10],
}
return render(request, "statistics.html", context)

Expand Down

0 comments on commit 72cd979

Please sign in to comment.