Skip to content

Commit

Permalink
Merge 0d2a789 into 812f2ca
Browse files Browse the repository at this point in the history
  • Loading branch information
myxor committed Apr 2, 2022
2 parents 812f2ca + 0d2a789 commit c04605a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
52 changes: 36 additions & 16 deletions dashboard/templatetags/cards.py
Expand Up @@ -281,13 +281,14 @@ def card_statistics(context, child):

changes = _diaperchange_statistics(child)
if changes:
stats.append(
{
"type": "duration",
"stat": changes["btwn_average"],
"title": _("Diaper change frequency"),
}
)
for item in changes:
stats.append(
{
"type": "duration",
"stat": item["btwn_average"],
"title": item["title"],
}
)

feedings = _feeding_statistics(child)
if feedings:
Expand Down Expand Up @@ -385,24 +386,43 @@ def _diaperchange_statistics(child):
:param child: an instance of the Child model.
:returns: a dictionary of statistics.
"""
changes = [
{
"start": timezone.now() - timezone.timedelta(days=3),
"title": _("Diaper change frequency (past 3 days)"),
},
{
"start": timezone.now() - timezone.timedelta(weeks=2),
"title": _("Diaper change frequency (past 2 weeks)"),
},
{
"start": timezone.make_aware(
datetime.combine(date.min, time(0, 0)) + timezone.timedelta(days=1)
),
"title": _("Diaper change frequency"),
},
]
for timespan in changes:
timespan["btwn_total"] = timezone.timedelta(0)
timespan["btwn_count"] = 0
timespan["btwn_average"] = 0.0

instances = models.DiaperChange.objects.filter(child=child).order_by("time")
if len(instances) == 0:
return False
changes = {
"btwn_total": timezone.timedelta(0),
"btwn_count": instances.count() - 1,
"btwn_average": 0.0,
}
last_instance = None

for instance in instances:
if last_instance:
changes["btwn_total"] += instance.time - last_instance.time
for timespan in changes:
if last_instance.time > timespan["start"]:
timespan["btwn_total"] += instance.time - last_instance.time
timespan["btwn_count"] += 1
last_instance = instance

if changes["btwn_count"] > 0:
changes["btwn_average"] = changes["btwn_total"] / changes["btwn_count"]

for timespan in changes:
if timespan["btwn_count"] > 0:
timespan["btwn_average"] = timespan["btwn_total"] / timespan["btwn_count"]
return changes


Expand Down
13 changes: 13 additions & 0 deletions dashboard/tests/tests_templatetags.py
Expand Up @@ -164,6 +164,19 @@ def test_card_sleep_naps_day(self):
def test_card_statistics(self):
data = cards.card_statistics(self.context, self.child)
stats = [
# Statistics date basis is not particularly strong to these diaper change
# examples.
# TODO: Improve testing of diaper change frequency statistics.
{
"type": "duration",
"stat": 0.0,
"title": "Diaper change frequency (past 3 days)",
},
{
"type": "duration",
"stat": 0.0,
"title": "Diaper change frequency (past 2 weeks)",
},
{
"title": "Diaper change frequency",
"stat": timezone.timedelta(0, 44228, 571429),
Expand Down

0 comments on commit c04605a

Please sign in to comment.