Skip to content

Commit fad8a51

Browse files
Support for both single and high/low tariff #130
1 parent 1f62227 commit fad8a51

File tree

7 files changed

+34
-25
lines changed

7 files changed

+34
-25
lines changed

dsmr_frontend/templates/dsmr_frontend/trends.html

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
</div>
1717
{% else %}
1818
<div class="alert alert-info" role="alert">
19-
{% blocktrans %}All data below is calculated among all consumption tracked until a day ago. Untracked consumption and any usage of today is not taken into account.{% endblocktrans %}
20-
{% blocktrans %}Current data displayed averages {{ hour_statistics_count }} hour(s), among {{ day_statistics_count }} day(s).{% endblocktrans %}
19+
{% blocktrans %}Untracked consumption in the past (and today), is not taken into account.{% endblocktrans %}
2120
</div>
2221
{% endif %}
2322

@@ -26,7 +25,7 @@
2625
<div class="col-md-12">
2726
<div class="panel">
2827
<header class="panel-heading">
29-
{% blocktrans %}Average hourly electricity consumed (in %){% endblocktrans %}
28+
{% blocktrans %}Average hourly electricity consumed (in %) within the past 4 weeks{% endblocktrans %}
3029
</header>
3130
<div class="panel-body" style="height: 350px;">
3231
<canvas id="average-electricity-consumed-chart"></canvas>
@@ -41,7 +40,7 @@
4140
<div class="col-md-12">
4241
<div class="panel">
4342
<header class="panel-heading">
44-
{% blocktrans %}Average hourly electricity returned (in %){% endblocktrans %}
43+
{% blocktrans %}Average hourly electricity returned (in %) within the past 4 weeks{% endblocktrans %}
4544
</header>
4645
<div class="panel-body" style="height: 350px;">
4746
<canvas id="average-electricity-returned-chart"></canvas>
@@ -56,7 +55,7 @@
5655
<div class="col-md-12">
5756
<div class="panel">
5857
<header class="panel-heading">
59-
{% blocktrans %}Average hourly gas consumed (in %){% endblocktrans %}
58+
{% blocktrans %}Average hourly gas consumed (in %) within the past 4 weeks{% endblocktrans %}
6059
</header>
6160
<div class="panel-body" style="height: 350px;">
6261
<canvas id="average-gas-consumed-chart"></canvas>

dsmr_frontend/views/trends.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_context_data(self, **kwargs):
2929
return context_data
3030

3131
# Average of real consumption/return per hour.
32-
average_consumption_by_hour = dsmr_stats.services.average_consumption_by_hour()
32+
average_consumption_by_hour = dsmr_stats.services.average_consumption_by_hour(max_weeks_ago=4)
3333

3434
context_data['avg_consumption_x'] = json.dumps(
3535
['{}:00'.format(int(x['hour_start'])) for x in average_consumption_by_hour]

dsmr_stats/services.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def electricity_tariff_percentage(start_date):
164164
return totals
165165

166166

167-
def average_consumption_by_hour():
168-
""" Calculates the average consumption by hour. Measured over all consumption data. """
167+
def average_consumption_by_hour(max_weeks_ago):
168+
""" Calculates the average consumption by hour. Measured over all consumption data of the past X months. """
169169
sql_extra = {
170170
# Ugly engine check, but still beter than iterating over a hundred thousand items in code.
171171
'postgresql': "date_part('hour', hour_start)",
@@ -179,7 +179,10 @@ def average_consumption_by_hour():
179179
if set_time_zone_sql:
180180
connection.connection.cursor().execute(set_time_zone_sql, [settings.TIME_ZONE]) # pragma: no cover
181181

182-
hour_statistics = HourStatistics.objects.extra({
182+
hour_statistics = HourStatistics.objects.filter(
183+
# This greatly helps reducing the queryset size, but also makes it more relevant.
184+
hour_start__gt=timezone.now() - timezone.timedelta(weeks=max_weeks_ago)
185+
).extra({
183186
'hour_start': sql_extra
184187
}).values('hour_start').order_by('hour_start').annotate(
185188
avg_electricity1=Avg('electricity1'),

dsmr_stats/tests/models/test_statistics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ def setUp(self):
1919

2020
def test_hour_statistics_average(self):
2121
""" #100: Empty gas readings mess up average. """
22-
average_consumption = dsmr_stats.services.average_consumption_by_hour()
22+
average_consumption = dsmr_stats.services.average_consumption_by_hour(max_weeks_ago=4)
2323
self.assertEqual(average_consumption[0]['avg_gas'], 0) # Would have been 'None' before.

dsmr_stats/tests/test_services.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ def test_analyze_service_clear_cache(self, now_mock, clear_cache_mock):
180180
dsmr_stats.services.analyze()
181181
self.assertTrue(clear_cache_mock.called)
182182

183-
def test_average_consumption_by_hour(self):
183+
@mock.patch('django.utils.timezone.now')
184+
def test_average_consumption_by_hour(self, now_mock):
184185
""" Test whether timezones are converted properly when grouping hours. """
186+
now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 1, 25, 12))
185187
HourStatistics.objects.create(
186188
# This should be stored with local timezone, so +1.
187189
hour_start=timezone.make_aware(timezone.datetime(2016, 1, 1, 12)),
@@ -190,7 +192,7 @@ def test_average_consumption_by_hour(self):
190192
electricity1_returned=0,
191193
electricity2_returned=0,
192194
)
193-
hour_stat = dsmr_stats.services.average_consumption_by_hour()[0]
195+
hour_stat = dsmr_stats.services.average_consumption_by_hour(max_weeks_ago=4)[0]
194196

195197
# @see "Trends are always shown in UTC #76", only PostgreSQL can fix this.
196198
if connection.vendor == 'postgresql':
-902 Bytes
Binary file not shown.

dsmrreader/locales/nl/LC_MESSAGES/django.po

+18-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: DSMR Reader\n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2016-11-20 17:38+0100\n"
10+
"POT-Creation-Date: 2016-11-20 17:50+0100\n"
1111
"PO-Revision-Date: 2016-11-20 17:34+0100\n"
1212
"Last-Translator: Dennis Siemensma <dsmr@dennissiemensma.nl>\n"
1313
"Language-Team: Dennis Siemensma <dsmr@dennissiemensma.nl>\n"
@@ -702,23 +702,22 @@ msgstr "Gemeten"
702702
msgid "Electricity return"
703703
msgstr "Elektriciteitsteruglevering"
704704

705-
msgid "All data below is calculated among all consumption tracked until a day ago. Untracked consumption and any usage of today is not taken into account."
706-
msgstr "Alle onderstaande gegevens zijn gebaseerd op al het gemeten verbruik tot en met gisteren. Ongemeten verbruik en het verbruik van vandaag worden hierin niet meegenomen."
707-
708-
#, python-format
709-
msgid "Current data displayed averages %(hour_statistics_count)s hour(s), among %(day_statistics_count)s day(s)."
710-
msgstr "Huidige gegevens betreffen gemiddelden over %(hour_statistics_count)s uren, verdeeld over %(day_statistics_count)s dag(en)."
705+
msgid "Untracked consumption in the past (and today), is not taken into account."
706+
msgstr ""
711707

712-
#, python-format
713-
msgid "Average hourly electricity consumed (in %%)"
708+
#, fuzzy, python-format
709+
#| msgid "Average hourly electricity consumed (in %%)"
710+
msgid "Average hourly electricity consumed (in %%) within the past 4 weeks"
714711
msgstr "Gemiddelde elektriciteitsverbruik per daguur (in %%)"
715712

716-
#, python-format
717-
msgid "Average hourly electricity returned (in %%)"
713+
#, fuzzy, python-format
714+
#| msgid "Average hourly electricity returned (in %%)"
715+
msgid "Average hourly electricity returned (in %%) within the past 4 weeks"
718716
msgstr "Gemiddelde elektriciteit teruglevering per daguur (in %%)"
719717

720-
#, python-format
721-
msgid "Average hourly gas consumed (in %%)"
718+
#, fuzzy, python-format
719+
#| msgid "Average hourly gas consumed (in %%)"
720+
msgid "Average hourly gas consumed (in %%) within the past 4 weeks"
722721
msgstr "Gemiddeld gasverbruik per daguur (in %%)"
723722

724723
msgid "Electricity tariff ratio (passed week)"
@@ -988,6 +987,12 @@ msgstr "Nederlands"
988987
msgid "English"
989988
msgstr "Engels"
990989

990+
#~ msgid "All data below is calculated among all consumption tracked until a day ago. Untracked consumption and any usage of today is not taken into account."
991+
#~ msgstr "Alle onderstaande gegevens zijn gebaseerd op al het gemeten verbruik tot en met gisteren. Ongemeten verbruik en het verbruik van vandaag worden hierin niet meegenomen."
992+
993+
#~ msgid "Current data displayed averages %(hour_statistics_count)s hour(s), among %(day_statistics_count)s day(s)."
994+
#~ msgstr "Huidige gegevens betreffen gemiddelden over %(hour_statistics_count)s uren, verdeeld over %(day_statistics_count)s dag(en)."
995+
991996
#~ msgid "Electricity 1 (per kWh)"
992997
#~ msgstr "Elektriciteit 1 (per kWh)"
993998

0 commit comments

Comments
 (0)