Skip to content

Commit 1f62227

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

File tree

7 files changed

+98
-11
lines changed

7 files changed

+98
-11
lines changed

dsmr_datalogger/management/commands/dsmr_fake_datasource.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import time
55

66
from django.core.management.base import BaseCommand, CommandError
7-
from dsmr_backend.mixins import InfiniteManagementCommandMixin
87
from django.utils.translation import ugettext as _
98
from django.core.management import call_command
109
from django.utils import timezone
10+
from django.conf import settings
11+
12+
from dsmr_backend.mixins import InfiniteManagementCommandMixin
1113

1214

1315
class Command(InfiniteManagementCommandMixin, BaseCommand):
@@ -41,6 +43,9 @@ def add_arguments(self, parser):
4143

4244
def run(self, **options):
4345
""" InfiniteManagementCommandMixin listens to handle() and calls run() in a loop. """
46+
if not settings.DEBUG:
47+
raise CommandError(_('Intended usage is NOT production! Only allowed when DEBUG = True'))
48+
4449
if not options.get('acked_warning'):
4550
raise CommandError(_('Intended usage is NOT production! Force by using --ack-to-mess-up-my-data'))
4651

dsmr_frontend/templates/dsmr_frontend/trends.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
</div>
6767
{% endif %}
6868

69-
{% if capabilities.electricity %}
69+
{% if capabilities.electricity and not frontend_settings.merge_electricity_tariffs %}
7070
<div class="row">
7171
<div class="col-md-12">
7272
<div class="panel" style="padding-bottom: 24px;">
@@ -126,12 +126,15 @@
126126
Chart.defaults.global.animation = false;
127127
Chart.defaults.global.segmentShowStroke = true;
128128

129-
{% if capabilities.electricity %}
129+
{% if capabilities.electricity and not frontend_settings.merge_electricity_tariffs %}
130130
render_electricity_consumption_by_tariff_week_chart();
131131
render_electricity_consumption_by_tariff_month_chart();
132-
render_average_electricity_consumed_chart();
133132
{% endif %}
134133

134+
{% if capabilities.electricity %}
135+
render_average_electricity_consumed_chart();
136+
{% endif %}
137+
135138
{% if capabilities.electricity_returned %}
136139
render_average_electricity_returned_chart();
137140
{% endif %}

dsmr_frontend/tests/test_webinterface.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_http_500_development(self, get_context_data_mock, debug_setting_mock):
7070

7171
@mock.patch('django.utils.timezone.now')
7272
def test_dashboard(self, now_mock):
73-
now_mock.return_value = timezone.make_aware(timezone.datetime(2016, 1, 1))
73+
now_mock.return_value = timezone.make_aware(timezone.datetime(2015, 11, 15))
7474

7575
weather_settings = WeatherSettings.get_solo()
7676
weather_settings.track = True

dsmr_frontend/views/dashboard.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class Dashboard(TemplateView):
2424

2525
def get_context_data(self, **kwargs):
2626
frontend_settings = FrontendSettings.get_solo()
27+
weather_settings = WeatherSettings.get_solo()
2728
context_data = super(Dashboard, self).get_context_data(**kwargs)
2829
context_data['capabilities'] = dsmr_backend.services.get_capabilities()
2930
context_data['frontend_settings'] = frontend_settings
31+
context_data['track_temperature'] = weather_settings.track
3032
context_data['notifications'] = Notification.objects.unread()
3133

3234
electricity = ElectricityConsumption.objects.all().order_by('read_at')
@@ -39,7 +41,10 @@ def get_context_data(self, **kwargs):
3941
gas = gas.reverse()[:self.gas_max]
4042
temperature = temperature.reverse()[:self.temperature_max]
4143
else:
42-
# We can't slice using negative offsets, so we should fetch a (quick) count first)
44+
# We can't slice using negative offsets, so we should fetch a (quick) count first. However, counts tend to
45+
# be slow, so we need to filter first by, let's say, the last week.
46+
electricity = electricity.filter(read_at__gt=timezone.now() - timezone.timedelta(days=7))
47+
4348
electricity_offset = max(0, electricity.count() - self.electricity_max)
4449
gas_offset = max(0, gas.count() - self.gas_max)
4550
temperature_offset = max(0, temperature.count() - self.temperature_max)
@@ -69,9 +74,7 @@ def get_context_data(self, **kwargs):
6974
[float(x.currently_delivered) for x in gas]
7075
)
7176

72-
context_data['track_temperature'] = WeatherSettings.get_solo().track
73-
74-
if context_data['track_temperature']:
77+
if weather_settings.track:
7578
context_data['temperature_x'] = json.dumps(
7679
[formats.date_format(
7780
timezone.localtime(x.read_at), 'DSMR_GRAPH_SHORT_TIME_FORMAT'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from decimal import Decimal
2+
import random
3+
4+
from django.core.management.base import BaseCommand, CommandError
5+
from django.utils.translation import ugettext as _
6+
from django.conf import settings
7+
from django.db import models
8+
9+
from dsmr_backend.mixins import InfiniteManagementCommandMixin
10+
from dsmr_stats.models.statistics import DayStatistics, HourStatistics
11+
from dsmr_consumption.models.consumption import ElectricityConsumption
12+
from dsmr_datalogger.models.reading import DsmrReading
13+
14+
15+
class Command(InfiniteManagementCommandMixin, BaseCommand):
16+
help = _('Alters any stats generate to fake data. DO NOT USE in production! Used for integration checks.')
17+
name = __name__ # Required for PID file.
18+
sleep_time = 10
19+
20+
def add_arguments(self, parser):
21+
super(Command, self).add_arguments(parser)
22+
parser.add_argument(
23+
'--ack-to-mess-up-my-data',
24+
action='store_true',
25+
dest='acked_warning',
26+
default=False,
27+
help=_('Required option to acknowledge you that you WILL mess up your data with this.')
28+
)
29+
30+
def run(self, **options):
31+
""" InfiniteManagementCommandMixin listens to handle() and calls run() in a loop. """
32+
if not settings.DEBUG:
33+
raise CommandError(_('Intended usage is NOT production! Only allowed when DEBUG = True'))
34+
35+
if not options.get('acked_warning'):
36+
raise CommandError(_('Intended usage is NOT production! Force by using --ack-to-mess-up-my-data'))
37+
38+
self._randomize()
39+
40+
def _randomize(self):
41+
""" Generates 'random' stats data by altering existing ones. """
42+
factor = Decimal(random.random()) # Between 0.0 and 1.0, change every day.
43+
print('Using existing consumption as base, multiplied by {}'.format(factor))
44+
45+
print('Altering readings... (might take quite some time)')
46+
DsmrReading.objects.all().order_by('-pk').update(
47+
electricity_returned_1=models.F('electricity_delivered_1') * factor,
48+
electricity_returned_2=models.F('electricity_delivered_2') * factor,
49+
electricity_currently_returned=models.F('electricity_currently_delivered') * factor,
50+
)
51+
52+
print('Altering electricity consumption... (might take quite some time as well)')
53+
ElectricityConsumption.objects.all().update(
54+
returned_1=models.F('delivered_1') * factor,
55+
returned_2=models.F('delivered_2') * factor,
56+
currently_returned=models.F('currently_delivered') * factor,
57+
)
58+
59+
print('Altering hour statistics...')
60+
HourStatistics.objects.all().update(
61+
electricity1_returned=models.F('electricity1') * factor,
62+
electricity2_returned=models.F('electricity2') * factor,
63+
)
64+
65+
print('Altering day statistics...')
66+
DayStatistics.objects.all().update(
67+
electricity1_returned=models.F('electricity1') * factor,
68+
electricity2_returned=models.F('electricity2') * factor,
69+
)
70+
print('Done!')
384 Bytes
Binary file not shown.

dsmrreader/locales/nl/LC_MESSAGES/django.po

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: DSMR Reader\n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2016-11-20 01:43+0100\n"
11-
"PO-Revision-Date: 2016-11-19 15:03+0100\n"
10+
"POT-Creation-Date: 2016-11-20 17:38+0100\n"
11+
"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"
1414
"Language: nl\n"
@@ -204,6 +204,9 @@ msgstr "Neem gasverbruik mee"
204204
msgid "Include electricity returned (solar panels)"
205205
msgstr "neem teruglevering elektriciteit mee (zonnepanelen)"
206206

207+
msgid "Intended usage is NOT production! Only allowed when DEBUG = True"
208+
msgstr "Bedoeld voor gebruik buiten productie. Alleen mogelijk wanneer instelling DEBUG = True"
209+
207210
msgid "Intended usage is NOT production! Force by using --ack-to-mess-up-my-data"
208211
msgstr "Bedoeld voor gebruik buiten productie. Forceer uitvoer met --ack-to-mess-up-my-data"
209212

@@ -763,6 +766,9 @@ msgstr "MinderGas.nl-configuratie"
763766
msgid "Trend & statistics"
764767
msgstr "Trends & statistieken"
765768

769+
msgid "Alters any stats generate to fake data. DO NOT USE in production! Used for integration checks."
770+
msgstr "Verandert alle bestaande statistieken. NIET GEBRUIKEN IN PRODUCTIE! Wordt gebruikt voor integratietests."
771+
766772
msgid "Clears all statistics generated. Use this to regenerate them after altering prices."
767773
msgstr "Verwijdert alle (dag/uur)statistieken. Gebruik dit wanneer je de energieprijzen hebt aangepast."
768774

0 commit comments

Comments
 (0)