def electricity_tariff_percentage(start_date):
""" Returns the total electricity consumption percentage by tariff (high/low tariff). """
totals = DayStatistics.objects.filter(day__gte=start_date).aggregate(
electricity1=Sum('electricity1'),
electricity2=Sum('electricity2'),
)
# Empty data will crash.
if not all(totals.values()):
return None
global_total = totals['electricity1'] + totals['electricity2']
totals['electricity1'] = math.ceil(totals['electricity1'] / global_total * 100)
totals['electricity2'] = 100 - totals['electricity1']
return totals
评论列表
文章目录