Skip to content

Commit

Permalink
#3 | Created consumption analysis tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Feb 5, 2016
1 parent 601bceb commit 4960c4b
Show file tree
Hide file tree
Showing 18 changed files with 1,268 additions and 64 deletions.
2 changes: 1 addition & 1 deletion dsmr_backend/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TestBackend(TestCase):
@mock.patch('dsmr_backend.signals.backend_called.send_robust')
def test_consumption_creation_signal(self, signal_mock):
""" Test dsmr_backend signal trigger. """
""" Test outgoing signal. """
self.assertFalse(signal_mock.called)
call_command('dsmr_backend')
self.assertTrue(signal_mock.called)
2 changes: 1 addition & 1 deletion dsmr_consumption/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AppConfig(AppConfig):
def ready(self):
dsmr_backend.signals.backend_called.connect(
receiver=self._on_backend_called_signal,
dispatch_uid=self.__class__.__name__
dispatch_uid=self.__class__
)

def _on_backend_called_signal(self, sender, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions dsmr_consumption/models/consumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class ElectricityConsumption(models.Model):
""" Point in time of electricity consumption, extracted from reading(s). """
""" Point in time of electricity consumption (usage), extracted from reading(s). """
read_at = models.DateTimeField(unique=True)
delivered_1 = models.DecimalField(
max_digits=9,
Expand Down Expand Up @@ -53,7 +53,7 @@ class Meta:


class GasConsumption(models.Model):
""" Hourly consumption, interpolated on the previous value read the hour before. """
""" Hourly consumption (usage), interpolated on the previous value read the hour before. """
read_at = models.DateTimeField(unique=True)
delivered = models.DecimalField(
max_digits=9,
Expand Down
18 changes: 9 additions & 9 deletions dsmr_consumption/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def compact(dsmr_reading):


def day_consumption(day):
consumption = {'day': day.date()}
consumption = {
'day': day
}
day_start = timezone.datetime(
year=day.year,
month=day.month,
Expand All @@ -126,7 +128,7 @@ def day_consumption(day):
# This WILL fail when we either have no prices at all or conflicting ranges.
try:
consumption['daily_energy_price'] = EnergySupplierPrice.objects.by_date(
target_date=consumption['day']
target_date=day
)
except EnergySupplierPrice.DoesNotExist:
# Default to zero prices.
Expand All @@ -138,12 +140,9 @@ def day_consumption(day):
gas_readings = GasConsumption.objects.filter(
read_at__gte=day_start, read_at__lt=day_end,
).order_by('read_at')
temperature_readings = TemperatureReading.objects.filter(
read_at__gte=day_start, read_at__lt=day_end,
).order_by('read_at')

if not electricity_readings.exists() or not gas_readings.exists():
raise LookupError("No readings found for: {}".format(day.date()))
raise LookupError("No readings found for: {}".format(day))

electricity_reading_count = electricity_readings.count()
gas_reading_count = gas_readings.count()
Expand Down Expand Up @@ -184,10 +183,11 @@ def day_consumption(day):
consumption['total_cost'] = round_price(
consumption['electricity1_cost'] + consumption['electricity2_cost'] + consumption['gas_cost']
)
consumption['notes'] = Note.objects.filter(
day=consumption['day']
).values_list('description', flat=True)
consumption['notes'] = Note.objects.filter(day=day).values_list('description', flat=True)

temperature_readings = TemperatureReading.objects.filter(
read_at__gte=day_start, read_at__lt=day_end,
).order_by('read_at')
consumption['average_temperature'] = temperature_readings.aggregate(
avg_temperature=Avg('degrees_celcius'),
)['avg_temperature'] or 0
Expand Down
6 changes: 0 additions & 6 deletions dsmr_consumption/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"""
Project wide signals used among apps.
"""
from django.dispatch import Signal


Expand All @@ -9,6 +6,3 @@

# Triggered whenever a new GAS consumption model was created and stored to database.
gas_consumption_created = Signal(providing_args=["instance"])

# Triggered whenever a new electricity statistics model was created and stored to database.
electricity_statistics_created = Signal(providing_args=["instance"])
4 changes: 2 additions & 2 deletions dsmr_consumption/tests/management_commands/test_compactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dsmr_consumption.models.settings import ConsumptionSettings


class TestDsmrStatsCompactor(CallCommandStdoutMixin, TestCase):
class TestCompactor(CallCommandStdoutMixin, TestCase):
""" Test 'dsmr_backend' management command. """
fixtures = ['dsmr_consumption/test_dsmrreading.json']

Expand All @@ -36,7 +36,7 @@ def test_processing(self):

@mock.patch('dsmr_consumption.signals.gas_consumption_created.send_robust')
def test_consumption_creation_signal(self, signal_mock):
""" Test outgoing signal communication. """
""" Test incoming signal communication. """
self.assertFalse(signal_mock.called)
self._call_command_stdout('dsmr_backend')
self.assertTrue(signal_mock.called)
Expand Down
6 changes: 3 additions & 3 deletions dsmr_frontend/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_context_data(self, **kwargs):

try:
context_data['consumption'] = dsmr_consumption.services.day_consumption(
day=latest_electricity.read_at.astimezone(settings.LOCAL_TIME_ZONE)
day=latest_electricity.read_at.astimezone(settings.LOCAL_TIME_ZONE).date()
)
except LookupError:
pass
Expand Down Expand Up @@ -124,7 +124,7 @@ def get_context_data(self, **kwargs):

try:
day_consumption = dsmr_consumption.services.day_consumption(
day=current_day
day=current_day.date()
)
except LookupError:
continue
Expand Down Expand Up @@ -152,7 +152,7 @@ def get_context_data(self, **kwargs):

try:
context_data['consumption'] = dsmr_consumption.services.day_consumption(
day=timezone.now().astimezone(settings.LOCAL_TIME_ZONE)
day=timezone.now().astimezone(settings.LOCAL_TIME_ZONE).date()
)
except LookupError:
pass
Expand Down
4 changes: 2 additions & 2 deletions dsmr_stats/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class AppConfig(AppConfig):
def ready(self):
dsmr_backend.signals.backend_called.connect(
receiver=self._on_backend_called_signal,
dispatch_uid=self.__class__.__name__
dispatch_uid=self.__class__
)

def _on_backend_called_signal(self, sender, **kwargs):
# Import below prevents an AppRegistryNotReady error on Django init.
import dsmr_stats.services
dsmr_stats.services.create_daily_statistics(day)
dsmr_stats.services.analyze()
Loading

0 comments on commit 4960c4b

Please sign in to comment.