Refactor the calculated_sum and manual_sum

This commit is contained in:
Lukáš Kucharczyk 2023-01-05 23:03:46 +01:00
parent 9c56ed4ce8
commit 5172c38c16
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
3 changed files with 36 additions and 10 deletions

View File

@ -6,7 +6,7 @@ RUN npm install && \
FROM python:3.10-slim-bullseye
ENV VERSION_NUMBER 0.1.0-43-gd00bb1c
ENV VERSION_NUMBER 0.1.0-44-g9c56ed4
ENV PROD 1
RUN useradd --create-home --uid 1000 timetracker

View File

@ -3,6 +3,7 @@ from datetime import datetime, timedelta
from django.conf import settings
from zoneinfo import ZoneInfo
from common.util.time import format_duration
from django.db.models import Sum
class Game(models.Model):
@ -46,7 +47,7 @@ class Session(models.Model):
def finish_now(self):
self.timestamp_end = datetime.now(ZoneInfo(settings.TIME_ZONE))
def duration_seconds(self):
def duration_seconds(self) -> timedelta:
if self.duration_manual == None:
if self.timestamp_end == None or self.timestamp_start == None:
return timedelta(0)
@ -54,11 +55,10 @@ class Session(models.Model):
value = self.timestamp_end - self.timestamp_start
else:
value = self.duration_manual
return value.total_seconds()
return timedelta(seconds=value.total_seconds())
def duration_formatted(self) -> str:
dur = self.duration_seconds()
result = format_duration(dur, "%H:%m")
result = format_duration(self.duration_seconds(), "%H:%m")
return result
def duration_any(self):
@ -68,6 +68,32 @@ class Session(models.Model):
else self.duration_manual
)
@staticmethod
def calculated_sum() -> timedelta:
calculated_sum_query = Session.objects.all().aggregate(
Sum("duration_calculated")
)
calculated_sum = (
timedelta(0)
if calculated_sum_query["duration_calculated__sum"] == None
else calculated_sum_query["duration_calculated__sum"]
)
return calculated_sum
@staticmethod
def manual_sum() -> timedelta:
manual_sum_query = Session.objects.all().aggregate(Sum("duration_manual"))
manual_sum = (
timedelta(0)
if manual_sum_query["duration_manual__sum"] == None
else manual_sum_query["duration_manual__sum"]
)
return manual_sum
@staticmethod
def total_sum() -> timedelta:
return Session.manual_sum() + Session.calculated_sum()
def save(self, *args, **kwargs):
if self.timestamp_start != None and self.timestamp_end != None:
self.duration_calculated = self.timestamp_end - self.timestamp_start

View File

@ -2,11 +2,12 @@ from django.shortcuts import render, redirect
from .models import Game, Platform, Purchase, Session
from .forms import SessionForm, PurchaseForm, GameForm, PlatformForm
from datetime import datetime
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from django.conf import settings
from common.util.time import now as now_with_tz, format_duration
from django.db.models import Sum
import logging
def model_counts(request):
@ -107,10 +108,9 @@ def index(request):
if Session.objects.count() == 0:
duration: str = ""
else:
result = Session.objects.all().aggregate(Sum("duration_calculated"))
duration = format_duration(
result["duration_calculated__sum"], "%H hours %m minutes"
context["total_duration"] = format_duration(
Session.total_sum(),
"%H hours %m minutes",
)
context["total_duration"] = duration
context["title"] = "Index"
return render(request, "index.html", context)