From 66444356a76370ec9c1a5b15576b48ff63bbbed3 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 29 Mar 2023 19:04:12 +0200 Subject: profile: show longest duration & duration tracks --- fietsboek/models/track.py | 13 +++++++++++++ fietsboek/templates/profile.jinja2 | 10 ++++++++++ fietsboek/views/profile.py | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py index e0d2820..4b85d11 100644 --- a/fietsboek/models/track.py +++ b/fietsboek/models/track.py @@ -550,6 +550,19 @@ class TrackWithMetadata: return self._meta()["end_time"] return self.cache.end_time + @property + def duration(self) -> datetime.timedelta: + """Returns the duration of this track. + + This is equivalent to ``end_time - start_time``, given that + no DST change happens between those points. + + Alternatively, it is equivalent to ``moving_time + stopped_time``. + + :return: The track duration. + """ + return self.end_time - self.start_time + def html_tooltip(self, localizer: Localizer) -> Markup: """Generate a quick summary of the track as a HTML element. diff --git a/fietsboek/templates/profile.jinja2 b/fietsboek/templates/profile.jinja2 index fe49990..4906247 100644 --- a/fietsboek/templates/profile.jinja2 +++ b/fietsboek/templates/profile.jinja2 @@ -105,6 +105,16 @@

{{ _("page.profile.shortest_distance_track") }}

{{ render_track_card(total.shortest_distance_track) }} {% endif %} + + {% if total.longest_duration_track %} +

{{ _("page.profile.longest_duration_track") }}

+ {{ render_track_card(total.longest_duration_track) }} + {% endif %} + + {% if total.shortest_duration_track %} +

{{ _("page.profile.shortest_duration_track") }}

+ {{ render_track_card(total.shortest_duration_track) }} + {% endif %} {% endblock %} diff --git a/fietsboek/views/profile.py b/fietsboek/views/profile.py index f7b1d79..bf1475c 100644 --- a/fietsboek/views/profile.py +++ b/fietsboek/views/profile.py @@ -23,6 +23,7 @@ class CumulativeStats: The values start out with default values, and tracks can be merged in via :meth:`add`. """ + # pylint: disable=too-many-instance-attributes count: int = 0 """Number of tracks added.""" @@ -51,6 +52,12 @@ class CumulativeStats: shortest_distance_track: Optional[TrackWithMetadata] = None """The track with the shortest distance.""" + longest_duration_track: Optional[TrackWithMetadata] = None + """The track with the longest time.""" + + shortest_duration_track: Optional[TrackWithMetadata] = None + """The track with the shortest time.""" + def add(self, track: TrackWithMetadata): """Adds a track to this stats collection. @@ -76,6 +83,18 @@ class CumulativeStats: ): self.shortest_distance_track = track + if ( + self.longest_duration_track is None + or self.longest_duration_track.duration < track.duration + ): + self.longest_duration_track = track + + if ( + self.shortest_duration_track is None + or self.shortest_duration_track.duration > track.duration + ): + self.shortest_duration_track = track + @property def avg_speed(self) -> float: """Average speed, in m/s.""" -- cgit v1.2.3