mirror of https://github.com/snachodog/mybuddy.git
add enhanced feedings statistics
This commit is contained in:
parent
3316ce3c2d
commit
853ab54b8c
|
@ -5,6 +5,8 @@ from django.db.models.functions import TruncDate
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from datetime import date, datetime, time
|
||||||
|
|
||||||
from core import models
|
from core import models
|
||||||
|
|
||||||
|
|
||||||
|
@ -170,10 +172,11 @@ def card_statistics(child):
|
||||||
'title': _('Diaper change frequency')})
|
'title': _('Diaper change frequency')})
|
||||||
|
|
||||||
feedings = _feeding_statistics(child)
|
feedings = _feeding_statistics(child)
|
||||||
stats.append({
|
for item in feedings:
|
||||||
'type': 'duration',
|
stats.append({
|
||||||
'stat': feedings['btwn_average'],
|
'type': 'duration',
|
||||||
'title': _('Feeding frequency')})
|
'stat': item['btwn_average'],
|
||||||
|
'title': item['title']})
|
||||||
|
|
||||||
naps = _nap_statistics(child)
|
naps = _nap_statistics(child)
|
||||||
stats.append({
|
stats.append({
|
||||||
|
@ -235,22 +238,40 @@ def _feeding_statistics(child):
|
||||||
:param child: an instance of the Child model.
|
:param child: an instance of the Child model.
|
||||||
:returns: a dictionary of statistics.
|
:returns: a dictionary of statistics.
|
||||||
"""
|
"""
|
||||||
|
feedings = [
|
||||||
|
{
|
||||||
|
'start': timezone.now()-timezone.timedelta(days=3),
|
||||||
|
'title': _('Feeding frequency')+ " 3 "+_("days")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'start': timezone.now()-timezone.timedelta(weeks=2),
|
||||||
|
'title': _('Feeding frequency') + " 2 "+_("weeks")
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'start': timezone.make_aware(datetime.combine(date.min, time(0,0)) + timezone.timedelta(days=1) ),
|
||||||
|
'title': _('Feeding frequency')
|
||||||
|
}
|
||||||
|
]
|
||||||
|
for timespan in feedings:
|
||||||
|
timespan['btwn_total'] = timezone.timedelta(0)
|
||||||
|
timespan['btwn_count'] = 0
|
||||||
|
timespan['btwn_average'] = 0.0
|
||||||
|
|
||||||
instances = models.Feeding.objects.filter(child=child).order_by('start')
|
instances = models.Feeding.objects.filter(child=child).order_by('start')
|
||||||
feedings = {
|
|
||||||
'btwn_total': timezone.timedelta(0),
|
|
||||||
'btwn_count': instances.count() - 1,
|
|
||||||
'btwn_average': 0.0}
|
|
||||||
last_instance = None
|
last_instance = None
|
||||||
|
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
if last_instance:
|
if last_instance:
|
||||||
feedings['btwn_total'] += instance.start - last_instance.end
|
for timespan in feedings:
|
||||||
|
if last_instance.start > timespan['start']:
|
||||||
|
timespan['btwn_total'] += instance.start - last_instance.end
|
||||||
|
timespan['btwn_count'] += 1
|
||||||
last_instance = instance
|
last_instance = instance
|
||||||
|
|
||||||
if feedings['btwn_count'] > 0:
|
for timespan in feedings:
|
||||||
feedings['btwn_average'] = \
|
if timespan['btwn_count'] > 0:
|
||||||
feedings['btwn_total'] / feedings['btwn_count']
|
timespan['btwn_average'] = \
|
||||||
|
timespan['btwn_total'] / timespan['btwn_count']
|
||||||
return feedings
|
return feedings
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue