Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
cd6ea6e903 | |||
22018fd2ba | |||
a5b2854bf6 | |||
518c0ecd56 | |||
a6cd7a3430 | |||
dba8414fd9 | |||
0e2113eefd | |||
c4b0347f3b | |||
c6ed21167c | |||
4ce15c44fc |
24
CHANGELOG.md
24
CHANGELOG.md
@ -1,3 +1,27 @@
|
||||
## 1.2.0 / 2023-11-01 20:18+01:00
|
||||
|
||||
### New
|
||||
* Add yearly stats page (https://git.kucharczyk.xyz/lukas/timetracker/issues/15)
|
||||
|
||||
### Enhancements
|
||||
* Add a button to start session from game overview
|
||||
|
||||
## 1.1.2 / 2023-10-13 16:30+02:00
|
||||
|
||||
### Enhancements
|
||||
* Durations are formatted in a consisent manner across all pages
|
||||
|
||||
### Fixes
|
||||
* Game Overview: display duration when >1 hour instead of displaying 0
|
||||
|
||||
## 1.1.1 / 2023-10-09 20:52+02:00
|
||||
|
||||
### New
|
||||
* Add notes section to game overview
|
||||
|
||||
### Enhancements
|
||||
* Make it possible to add any data on the game overview page
|
||||
|
||||
## 1.1.0 / 2023-10-09 00:01+02:00
|
||||
|
||||
### New
|
||||
|
@ -6,17 +6,10 @@ RUN npm install && \
|
||||
|
||||
FROM python:3.10.9-slim-bullseye
|
||||
|
||||
ENV VERSION_NUMBER 1.1.0
|
||||
ENV VERSION_NUMBER 1.2.0
|
||||
ENV PROD 1
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
|
||||
RUN apt update && \
|
||||
apt install -y \
|
||||
bash \
|
||||
vim \
|
||||
curl && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN useradd -m --uid 1000 timetracker
|
||||
WORKDIR /home/timetracker/app
|
||||
COPY . /home/timetracker/app/
|
||||
|
@ -32,6 +32,8 @@ def format_duration(
|
||||
from the formatting string. For example:
|
||||
- 61 seconds as "%s" = 61 seconds
|
||||
- 61 seconds as "%m %s" = 1 minutes 1 seconds"
|
||||
Format specifiers can include width and precision options:
|
||||
- %5.2H: hours formatted with width 5 and 2 decimal places (padded with zeros)
|
||||
"""
|
||||
minute_seconds = 60
|
||||
hour_seconds = 60 * minute_seconds
|
||||
@ -46,18 +48,32 @@ def format_duration(
|
||||
remainder = seconds = seconds_total
|
||||
if "%d" in format_string:
|
||||
days, remainder = divmod(seconds_total, day_seconds)
|
||||
if "%H" in format_string:
|
||||
hours, remainder = divmod(remainder, hour_seconds)
|
||||
if "%m" in format_string:
|
||||
if re.search(r"%\d*\.?\d*H", format_string):
|
||||
hours_float, remainder = divmod(remainder, hour_seconds)
|
||||
hours = float(hours_float) + remainder / hour_seconds
|
||||
if re.search(r"%\d*\.?\d*m", format_string):
|
||||
minutes, seconds = divmod(remainder, minute_seconds)
|
||||
literals = {
|
||||
"%d": str(days),
|
||||
"%H": str(hours),
|
||||
"%m": str(minutes),
|
||||
"%s": str(seconds),
|
||||
"%r": str(seconds_total),
|
||||
"d": str(days),
|
||||
"H": str(hours),
|
||||
"m": str(minutes),
|
||||
"s": str(seconds),
|
||||
"r": str(seconds_total),
|
||||
}
|
||||
formatted_string = format_string
|
||||
for pattern, replacement in literals.items():
|
||||
formatted_string = re.sub(pattern, replacement, formatted_string)
|
||||
# Match format specifiers with optional width and precision
|
||||
match = re.search(rf"%(\d*\.?\d*){pattern}", formatted_string)
|
||||
if match:
|
||||
format_spec = match.group(1)
|
||||
if "." in format_spec:
|
||||
# Format the number as float if precision is specified
|
||||
replacement = f"{float(replacement):{format_spec}f}"
|
||||
else:
|
||||
# Format the number as integer if no precision is specified
|
||||
replacement = f"{int(float(replacement)):>{format_spec}}"
|
||||
# Replace the format specifier with the formatted number
|
||||
formatted_string = re.sub(
|
||||
rf"%\d*\.?\d*{pattern}", replacement, formatted_string
|
||||
)
|
||||
return formatted_string
|
||||
|
@ -114,7 +114,7 @@ class Session(models.Model):
|
||||
return timedelta(seconds=(manual + calculated).total_seconds())
|
||||
|
||||
def duration_formatted(self) -> str:
|
||||
result = format_duration(self.duration_seconds(), "%H:%m")
|
||||
result = format_duration(self.duration_seconds(), "%02.0H:%02.0m")
|
||||
return result
|
||||
|
||||
@property
|
||||
|
@ -791,6 +791,16 @@ select {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.my-5 {
|
||||
margin-top: 1.25rem;
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
.my-6 {
|
||||
margin-top: 1.5rem;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.mb-1 {
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
@ -799,6 +809,10 @@ select {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.ml-1 {
|
||||
margin-left: 0.25rem;
|
||||
}
|
||||
|
||||
.ml-2 {
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
@ -831,6 +845,14 @@ select {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.h-4 {
|
||||
height: 1rem;
|
||||
}
|
||||
|
||||
.h-5 {
|
||||
height: 1.25rem;
|
||||
}
|
||||
|
||||
.h-6 {
|
||||
height: 1.5rem;
|
||||
}
|
||||
@ -839,10 +861,18 @@ select {
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.w-5 {
|
||||
width: 1.25rem;
|
||||
}
|
||||
|
||||
.w-6 {
|
||||
width: 1.5rem;
|
||||
}
|
||||
|
||||
.w-7 {
|
||||
width: 1.75rem;
|
||||
}
|
||||
|
||||
.w-full {
|
||||
width: 100%;
|
||||
}
|
||||
@ -881,10 +911,18 @@ select {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.justify-center {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.justify-between {
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.gap-2 {
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.self-center {
|
||||
align-self: center;
|
||||
}
|
||||
@ -907,6 +945,10 @@ select {
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.rounded-sm {
|
||||
border-radius: 0.125rem;
|
||||
}
|
||||
|
||||
.border-gray-200 {
|
||||
--tw-border-opacity: 1;
|
||||
border-color: rgb(229 231 235 / var(--tw-border-opacity));
|
||||
@ -922,6 +964,11 @@ select {
|
||||
background-color: rgb(22 163 74 / var(--tw-bg-opacity));
|
||||
}
|
||||
|
||||
.bg-violet-600 {
|
||||
--tw-bg-opacity: 1;
|
||||
background-color: rgb(124 58 237 / var(--tw-bg-opacity));
|
||||
}
|
||||
|
||||
.bg-white {
|
||||
--tw-bg-opacity: 1;
|
||||
background-color: rgb(255 255 255 / var(--tw-bg-opacity));
|
||||
@ -972,6 +1019,11 @@ select {
|
||||
line-height: 2.5rem;
|
||||
}
|
||||
|
||||
.text-5xl {
|
||||
font-size: 3rem;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.text-base {
|
||||
font-size: 1rem;
|
||||
line-height: 1.5rem;
|
||||
@ -996,6 +1048,10 @@ select {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.italic {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.text-slate-300 {
|
||||
--tw-text-opacity: 1;
|
||||
color: rgb(203 213 225 / var(--tw-text-opacity));
|
||||
@ -1236,6 +1292,11 @@ th label {
|
||||
background-color: rgb(21 128 61 / var(--tw-bg-opacity));
|
||||
}
|
||||
|
||||
.hover\:bg-violet-700:hover {
|
||||
--tw-bg-opacity: 1;
|
||||
background-color: rgb(109 40 217 / var(--tw-bg-opacity));
|
||||
}
|
||||
|
||||
.hover\:underline:hover {
|
||||
text-decoration-line: underline;
|
||||
}
|
||||
@ -1256,6 +1317,11 @@ th label {
|
||||
--tw-ring-color: rgb(34 197 94 / var(--tw-ring-opacity));
|
||||
}
|
||||
|
||||
.focus\:ring-violet-500:focus {
|
||||
--tw-ring-opacity: 1;
|
||||
--tw-ring-color: rgb(139 92 246 / var(--tw-ring-opacity));
|
||||
}
|
||||
|
||||
.focus\:ring-offset-2:focus {
|
||||
--tw-ring-offset-width: 2px;
|
||||
}
|
||||
@ -1264,6 +1330,10 @@ th label {
|
||||
--tw-ring-offset-color: #bfdbfe;
|
||||
}
|
||||
|
||||
.focus\:ring-offset-violet-200:focus {
|
||||
--tw-ring-offset-color: #ddd6fe;
|
||||
}
|
||||
|
||||
:is(.dark .dark\:bg-gray-800) {
|
||||
--tw-bg-opacity: 1;
|
||||
background-color: rgb(31 41 55 / var(--tw-bg-opacity));
|
||||
@ -1274,6 +1344,11 @@ th label {
|
||||
background-color: rgb(17 24 39 / var(--tw-bg-opacity));
|
||||
}
|
||||
|
||||
:is(.dark .dark\:text-slate-500) {
|
||||
--tw-text-opacity: 1;
|
||||
color: rgb(100 116 139 / var(--tw-text-opacity));
|
||||
}
|
||||
|
||||
:is(.dark .dark\:text-slate-600) {
|
||||
--tw-text-opacity: 1;
|
||||
color: rgb(71 85 105 / var(--tw-text-opacity));
|
||||
@ -1284,16 +1359,6 @@ th label {
|
||||
color: rgb(255 255 255 / var(--tw-text-opacity));
|
||||
}
|
||||
|
||||
:is(.dark .dark\:text-slate-400) {
|
||||
--tw-text-opacity: 1;
|
||||
color: rgb(148 163 184 / var(--tw-text-opacity));
|
||||
}
|
||||
|
||||
:is(.dark .dark\:text-slate-500) {
|
||||
--tw-text-opacity: 1;
|
||||
color: rgb(100 116 139 / var(--tw-text-opacity));
|
||||
}
|
||||
|
||||
@media (min-width: 640px) {
|
||||
.sm\:inline {
|
||||
display: inline;
|
||||
@ -1320,6 +1385,10 @@ th label {
|
||||
padding-left: 0.5rem;
|
||||
}
|
||||
|
||||
.sm\:pl-4 {
|
||||
padding-left: 1rem;
|
||||
}
|
||||
|
||||
.sm\:decoration-2 {
|
||||
text-decoration-thickness: 2px;
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
<button
|
||||
type="button"
|
||||
{% comment %}
|
||||
title
|
||||
text
|
||||
{% endcomment %}
|
||||
<a
|
||||
href="{{ link }}"
|
||||
title="{{ title }}"
|
||||
autofocus
|
||||
class="truncate max-w-xs sm:max-w-md lg:max-w-lg py-1 px-2 bg-green-600 hover:bg-green-700 focus:ring-green-500 focus:ring-offset-blue-200 text-white transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-lg"
|
||||
class="truncate max-w-xs py-1 px-2 text-xs bg-green-600 hover:bg-green-700 focus:ring-green-500 focus:ring-offset-blue-200 text-white transition ease-in duration-200 text-center font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-sm"
|
||||
>
|
||||
<svg
|
||||
{% comment %} <svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
@ -18,5 +21,6 @@
|
||||
d="M5.25 5.653c0-.856.917-1.398 1.667-.986l11.54 6.348a1.125 1.125 0 010 1.971l-11.54 6.347a1.125 1.125 0 01-1.667-.985V5.653z"
|
||||
/>
|
||||
</svg>
|
||||
{% endcomment %}
|
||||
{{ text }}
|
||||
</button>
|
||||
</a>
|
||||
|
26
games/templates/components/button_start.html
Normal file
26
games/templates/components/button_start.html
Normal file
@ -0,0 +1,26 @@
|
||||
{% comment %}
|
||||
title
|
||||
text
|
||||
{% endcomment %}
|
||||
<button
|
||||
type="button"
|
||||
title="{{ title }}"
|
||||
autofocus
|
||||
class="truncate max-w-xs sm:max-w-md lg:max-w-lg py-1 px-2 bg-green-600 hover:bg-green-700 focus:ring-green-500 focus:ring-offset-blue-200 text-white transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-lg"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke-width="1.5"
|
||||
stroke="currentColor"
|
||||
class="self-center w-6 h-6 inline"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
d="M5.25 5.653c0-.856.917-1.398 1.667-.986l11.54 6.348a1.125 1.125 0 010 1.971l-11.54 6.347a1.125 1.125 0 01-1.667-.985V5.653z"
|
||||
/>
|
||||
</svg>
|
||||
{{ text }}
|
||||
</button>
|
21
games/templates/components/edit_button.html
Normal file
21
games/templates/components/edit_button.html
Normal file
@ -0,0 +1,21 @@
|
||||
<a href="{{ edit_url }}">
|
||||
<button
|
||||
type="button"
|
||||
title="Edit"
|
||||
class="ml-1 py-1 px-2 flex justify-center items-center bg-violet-600 hover:bg-violet-700 focus:ring-violet-500 focus:ring-offset-violet-200 text-white transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 w-7 h-4 rounded-lg"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
class="w-5 h-5"
|
||||
>
|
||||
<path
|
||||
d="M5.433 13.917l1.262-3.155A4 4 0 017.58 9.42l6.92-6.918a2.121 2.121 0 013 3l-6.92 6.918c-.383.383-.84.685-1.343.886l-3.154 1.262a.5.5 0 01-.65-.65z"
|
||||
/>
|
||||
<path
|
||||
d="M3.5 5.75c0-.69.56-1.25 1.25-1.25H10A.75.75 0 0010 3H4.75A2.75 2.75 0 002 5.75v9.5A2.75 2.75 0 004.75 18h9.5A2.75 2.75 0 0017 15.25V10a.75.75 0 00-1.5 0v5.25c0 .69-.56 1.25-1.25 1.25h-9.5c-.69 0-1.25-.56-1.25-1.25v-9.5z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</a>
|
@ -10,8 +10,8 @@
|
||||
<div class="mx-auto text-center my-4">
|
||||
<a
|
||||
id="last-session-start"
|
||||
href="{% url 'start_session' last.id %}"
|
||||
hx-get="{% url 'start_session' last.id %}"
|
||||
href="{% url 'start_session_same_as_last' last.id %}"
|
||||
hx-get="{% url 'start_session_same_as_last' last.id %}"
|
||||
hx-indicator="#indicator"
|
||||
hx-swap="afterbegin"
|
||||
hx-target=".responsive-table tbody"
|
||||
@ -19,7 +19,7 @@
|
||||
onClick="document.querySelector('#last-session-start').classList.add('invisible')"
|
||||
class="{% if last.timestamp_end == null %}invisible{% endif %}"
|
||||
>
|
||||
{% include 'components/button.html' with text=last.purchase title="Start session of last played game" only %}
|
||||
{% include 'components/button_start.html' with text=last.purchase title="Start session of last played game" only %}
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
64
games/templates/stats.html
Normal file
64
games/templates/stats.html
Normal file
@ -0,0 +1,64 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{{ title }}{% endblock title %}
|
||||
|
||||
{% load static %}
|
||||
|
||||
{% block content %}
|
||||
<div class="dark:text-white max-w-sm sm:max-w-xl lg:max-w-3xl mx-auto">
|
||||
<h1 class="text-5xl text-center my-6">Stats for {{ year }}</h1>
|
||||
<table class="responsive-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Total hours</th>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Total games</th>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Total 2023 games</th>
|
||||
</tr>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_hours }}</td>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_games }}</td>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ total_2023_games }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<h1 class="text-5xl text-center my-6">Top games by playtime</h1>
|
||||
<table class="responsive-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Name</th>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Playtime (hours)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for purchase in top_10_by_playtime %}
|
||||
<tr>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">
|
||||
<a href="{% url 'view_game' purchase.edition.game.id %}">{{ purchase.edition.name }}
|
||||
|
||||
</a>
|
||||
</td>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ purchase.formatted_playtime }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<h1 class="text-5xl text-center my-6">Platforms by playtime</h1>
|
||||
<table class="responsive-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Platform</th>
|
||||
<th class="px-2 sm:px-4 md:px-6 md:py-2">Playtime (hours)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for item in total_playtime_per_platform %}
|
||||
<tr>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ item.platform_name }} </td>
|
||||
<td class="px-2 sm:px-4 md:px-6 md:py-2 font-mono">{{ item.formatted_playtime }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% endblock content %}
|
@ -6,7 +6,12 @@
|
||||
|
||||
{% block content %}
|
||||
<div class="dark:text-white max-w-sm sm:max-w-xl lg:max-w-3xl mx-auto">
|
||||
<h1 class="text-4xl">{{ game.name }} <span class="dark:text-slate-500">(#{{ game.pk }})</span></h1>
|
||||
<h1 class="text-4xl flex items-center">
|
||||
{{ game.name }}
|
||||
<span class="dark:text-slate-500">(#{{ game.pk }})</span>
|
||||
{% url 'edit_game' game.id as edit_url %}
|
||||
{% include 'components/edit_button.html' with edit_url=edit_url %}
|
||||
</h1>
|
||||
<h2 class="text-lg my-2 ml-2">
|
||||
{{ total_hours }} <span class="dark:text-slate-500">total</span>
|
||||
{{ session_average }} <span class="dark:text-slate-500">avg</span>
|
||||
@ -17,7 +22,7 @@
|
||||
<h1 class="text-3xl mt-4 mb-1">Editions <span class="dark:text-slate-500">({{ editions.count }})</span></h1>
|
||||
<ul>
|
||||
{% for edition in editions %}
|
||||
<li class="sm:pl-2">
|
||||
<li class="sm:pl-2 flex items-center">
|
||||
{{ edition.name }} ({{ edition.platform }}, {{ edition.year_released }})
|
||||
{% if edition.wikidata %}
|
||||
<span class="hidden sm:inline">
|
||||
@ -26,19 +31,57 @@
|
||||
</a>
|
||||
</span>
|
||||
{% endif %}
|
||||
{% url 'edit_edition' edition.id as edit_url %}
|
||||
{% include 'components/edit_button.html' with edit_url=edit_url %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<h1 class="text-3xl mt-4 mb-1">Purchases <span class="dark:text-slate-500">({{ purchases.count }})</span></h1>
|
||||
<ul>
|
||||
{% for purchase in purchases %}
|
||||
<li class="sm:pl-2">{{ purchase.platform }} ({{ purchase.get_ownership_type_display }}, {{ purchase.date_purchased | date:"Y" }}, {{ purchase.price }} {{ purchase.price_currency}})</li>
|
||||
<li class="sm:pl-2 flex items-center">
|
||||
{{ purchase.platform }}
|
||||
({{ purchase.get_ownership_type_display }}, {{ purchase.date_purchased | date:"Y" }}, {{ purchase.price }} {{ purchase.price_currency}})
|
||||
{% url 'edit_purchase' purchase.id as edit_url %}
|
||||
{% include 'components/edit_button.html' with edit_url=edit_url %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<h1 class="text-3xl mt-4 mb-1">Sessions <span class="dark:text-slate-500">({{ sessions.count }})</span></h1>
|
||||
<h1 class="text-3xl mt-4 mb-1 flex gap-2 items-center">
|
||||
Sessions
|
||||
<span class="dark:text-slate-500">
|
||||
({{ sessions.count }})
|
||||
</span>
|
||||
{% url 'start_game_session' game.id as add_session_link %}
|
||||
{% include 'components/button.html' with title="Start new session" text="New" link=add_session_link %}
|
||||
</h1>
|
||||
<ul>
|
||||
{% for session in sessions %}
|
||||
<li class="sm:pl-2">{{ session.timestamp_start | date:"d/m/Y" }} ({{ session.device.get_type_display | default:"Unknown" }}, {{ session.duration_formatted }})</li>
|
||||
<li class="sm:pl-2 flex items-center">
|
||||
{{ session.timestamp_start | date:"d/m/Y" }}
|
||||
({{ session.device.get_type_display | default:"Unknown" }}, {{ session.duration_formatted }})
|
||||
{% url 'edit_session' session.id as edit_url %}
|
||||
{% include 'components/edit_button.html' with edit_url=edit_url %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<h1 class="text-3xl mt-4 mb-1">Notes <span class="dark:text-slate-500">({{ sessions_with_notes.count }})</span></h1>
|
||||
<ul>
|
||||
{% for session in sessions_with_notes %}
|
||||
<li class="sm:pl-2">
|
||||
<ul>
|
||||
<li class="block dark:text-slate-500">
|
||||
<span class="flex items-center">
|
||||
{{ session.timestamp_start | date:"d/m/Y H:m" }}
|
||||
{% url 'edit_session' session.id as edit_session_url %}
|
||||
{% include 'components/edit_button.html' with edit_url=edit_session_url %}
|
||||
</span>
|
||||
</li>
|
||||
<li class="sm:pl-4 italic">
|
||||
{{ session.note|linebreaks }}
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
|
@ -19,9 +19,14 @@ urlpatterns = [
|
||||
name="update_session",
|
||||
),
|
||||
path(
|
||||
"start-session/<int:last_session_id>",
|
||||
views.start_session,
|
||||
name="start_session",
|
||||
"start-session-same-as-last/<int:last_session_id>",
|
||||
views.start_session_same_as_last,
|
||||
name="start_session_same_as_last",
|
||||
),
|
||||
path(
|
||||
"start-session/<int:game_id>",
|
||||
views.start_game_session,
|
||||
name="start_game_session",
|
||||
),
|
||||
# path(
|
||||
# "delete_session/by-id/<int:session_id>",
|
||||
@ -68,4 +73,9 @@ urlpatterns = [
|
||||
{"filter": "ownership_type"},
|
||||
name="list_sessions_by_ownership_type",
|
||||
),
|
||||
path(
|
||||
"stats/<int:year>",
|
||||
views.stats,
|
||||
name="stats_by_year",
|
||||
),
|
||||
]
|
||||
|
@ -101,8 +101,8 @@ def view_game(request, game_id=None):
|
||||
context["sessions"] = Session.objects.filter(
|
||||
purchase__edition__game_id=game_id
|
||||
).order_by("-timestamp_start")
|
||||
context["total_hours"] = int(
|
||||
format_duration(context["sessions"].total_duration_unformatted(), "%H")
|
||||
context["total_hours"] = float(
|
||||
format_duration(context["sessions"].total_duration_unformatted(), "%2.1H")
|
||||
)
|
||||
context["session_average"] = round(
|
||||
(context["total_hours"]) / int(context["sessions"].count()), 1
|
||||
@ -112,6 +112,7 @@ def view_game(request, game_id=None):
|
||||
# so the most recent are on top
|
||||
context["last_session"] = context["sessions"].first()
|
||||
context["first_session"] = context["sessions"].last()
|
||||
context["sessions_with_notes"] = context["sessions"].exclude(note="")
|
||||
return render(request, "view_game.html", context)
|
||||
|
||||
|
||||
@ -139,7 +140,24 @@ def edit_edition(request, edition_id=None):
|
||||
return render(request, "add.html", context)
|
||||
|
||||
|
||||
def start_session(request, last_session_id: int):
|
||||
def start_game_session(request, game_id: int):
|
||||
last_session = (
|
||||
Session.objects.filter(purchase__edition__game_id=game_id)
|
||||
.order_by("-timestamp_start")
|
||||
.first()
|
||||
)
|
||||
session = SessionForm(
|
||||
{
|
||||
"purchase": last_session.purchase.id,
|
||||
"timestamp_start": now_with_tz(),
|
||||
"device": last_session.device,
|
||||
}
|
||||
)
|
||||
session.save()
|
||||
return redirect("list_sessions")
|
||||
|
||||
|
||||
def start_session_same_as_last(request, last_session_id: int):
|
||||
last_session = Session.objects.get(id=last_session_id)
|
||||
session = SessionForm(
|
||||
{
|
||||
@ -211,6 +229,48 @@ def list_sessions(
|
||||
return render(request, "list_sessions.html", context)
|
||||
|
||||
|
||||
def stats(request, year: int):
|
||||
first_day_of_year = datetime(year, 1, 1)
|
||||
year_sessions = Session.objects.filter(timestamp_start__gte=first_day_of_year)
|
||||
year_purchases = Purchase.objects.filter(session__in=year_sessions).distinct()
|
||||
year_purchases_with_playtime = year_purchases.annotate(
|
||||
total_playtime=Sum(
|
||||
F("session__duration_calculated") + F("session__duration_manual")
|
||||
)
|
||||
)
|
||||
top_10_by_playtime = year_purchases_with_playtime.order_by("-total_playtime")[:10]
|
||||
for purchase in top_10_by_playtime:
|
||||
purchase.formatted_playtime = format_duration(purchase.total_playtime, "%2.0H")
|
||||
|
||||
total_playtime_per_platform = (
|
||||
year_sessions.values("purchase__platform__name") # Group by platform name
|
||||
.annotate(
|
||||
total_playtime=Sum(F("duration_calculated") + F("duration_manual"))
|
||||
) # Sum the duration_calculated for each group
|
||||
.annotate(platform_name=F("purchase__platform__name")) # Rename the field
|
||||
.values(
|
||||
"platform_name", "total_playtime"
|
||||
) # Select the renamed field and total_playtime
|
||||
.order_by("-total_playtime") # Optional: Order by the renamed platform name
|
||||
)
|
||||
for item in total_playtime_per_platform:
|
||||
item["formatted_playtime"] = format_duration(item["total_playtime"], "%2.0H")
|
||||
|
||||
context = {
|
||||
"total_hours": format_duration(
|
||||
year_sessions.total_duration_unformatted(), "%2.0H"
|
||||
),
|
||||
"total_games": year_purchases.count(),
|
||||
"total_2023_games": year_purchases.filter(edition__year_released=year).count(),
|
||||
"top_10_by_playtime_formatted": top_10_by_playtime,
|
||||
"top_10_by_playtime": top_10_by_playtime,
|
||||
"year": year,
|
||||
"total_playtime_per_platform": total_playtime_per_platform,
|
||||
}
|
||||
|
||||
return render(request, "stats.html", context)
|
||||
|
||||
|
||||
def add_purchase(request):
|
||||
context = {}
|
||||
now = datetime.now()
|
||||
|
@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "timetracker"
|
||||
version = "1.1.0"
|
||||
version = "1.2.0"
|
||||
description = "A simple time tracker."
|
||||
authors = ["Lukáš Kucharczyk <lukas@kucharczyk.xyz>"]
|
||||
license = "GPL"
|
||||
|
@ -6,7 +6,6 @@ from common.time import format_duration
|
||||
|
||||
class FormatDurationTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
|
||||
return super().setUp()
|
||||
|
||||
def test_only_days(self):
|
||||
@ -19,6 +18,21 @@ class FormatDurationTest(unittest.TestCase):
|
||||
result = format_duration(delta, "%H hours")
|
||||
self.assertEqual(result, "1 hours")
|
||||
|
||||
def test_only_hours_fractional(self):
|
||||
delta = timedelta(hours=1)
|
||||
result = format_duration(delta, "%.1H hours")
|
||||
self.assertEqual(result, "1.0 hours")
|
||||
|
||||
def test_less_than_hour_with_precision(self):
|
||||
delta = timedelta(hours=0.5)
|
||||
result = format_duration(delta, "%.1H hours")
|
||||
self.assertEqual(result, "0.5 hours")
|
||||
|
||||
def test_less_than_hour_without_precision(self):
|
||||
delta = timedelta(hours=0.5)
|
||||
result = format_duration(delta, "%H hours")
|
||||
self.assertEqual(result, "0 hours")
|
||||
|
||||
def test_overflow_hours(self):
|
||||
delta = timedelta(hours=25)
|
||||
result = format_duration(delta, "%H hours")
|
||||
|
Reference in New Issue
Block a user