|
| 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!') |
0 commit comments