From 8dc1e83d0a6d1ae0fea3ff71dd5565974fadeb3d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 11 Mar 2023 17:43:10 +0000 Subject: [PATCH] Add more interval choices --- core/management/commands/scheduling.py | 35 +++++++++++++++---- ...7_alter_platform_cheat_interval_seconds.py | 18 ++++++++++ core/models.py | 6 +++- 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 core/migrations/0017_alter_platform_cheat_interval_seconds.py diff --git a/core/management/commands/scheduling.py b/core/management/commands/scheduling.py index 2b12232..d357d7c 100644 --- a/core/management/commands/scheduling.py +++ b/core/management/commands/scheduling.py @@ -4,16 +4,18 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from django.core.management.base import BaseCommand from core.clients.aggregators.nordigen import NordigenClient -from core.models import Aggregator +from core.clients.platforms.agora import AgoraClient +from core.models import INTERVAL_CHOICES, Aggregator, Platform from core.util import logs log = logs.get_logger("scheduling") -INTERVAL = 5 +INTERVAL_AGGREGATOR = 5 + +INTERVALS_PLATFORM = [x[0] for x in INTERVAL_CHOICES] -async def job(): - print("Running schedule.") +async def aggregator_job(): aggregators = Aggregator.objects.filter(enabled=True, fetch_accounts=True) for aggregator in aggregators: if aggregator.service == "nordigen": @@ -25,6 +27,20 @@ async def job(): aggregator.save() +async def platform_job(interval): + print("PLATFORM JOB FOR", interval) + if interval == 0: + return + platforms = Platform.objects.filter(enabled=True, cheat_interval_seconds=interval) + for platform in platforms: + if platform.service == "agora": + if platform.cheat is True: + instance = await AgoraClient(platform) + await instance.cheat() + else: + raise NotImplementedError(f"No such client library: {platform.service}") + + class Command(BaseCommand): def handle(self, *args, **options): """ @@ -32,8 +48,15 @@ class Command(BaseCommand): """ scheduler = AsyncIOScheduler() - log.debug(f"Scheduling checking process job every {INTERVAL} seconds") - scheduler.add_job(job, "interval", seconds=INTERVAL) + log.debug(f"Scheduling {INTERVAL_AGGREGATOR} second aggregator job") + scheduler.add_job(aggregator_job, "interval", seconds=INTERVAL_AGGREGATOR) + for interval in INTERVALS_PLATFORM: + if interval == 0: + continue + log.debug(f"Scheduling {interval} second platform job") + scheduler.add_job( + platform_job, "interval", seconds=interval, args=[interval] + ) scheduler.start() loop = asyncio.get_event_loop() try: diff --git a/core/migrations/0017_alter_platform_cheat_interval_seconds.py b/core/migrations/0017_alter_platform_cheat_interval_seconds.py new file mode 100644 index 0000000..ab8e74f --- /dev/null +++ b/core/migrations/0017_alter_platform_cheat_interval_seconds.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.7 on 2023-03-11 17:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0016_alter_platform_cheat_interval_seconds'), + ] + + operations = [ + migrations.AlterField( + model_name='platform', + name='cheat_interval_seconds', + field=models.IntegerField(choices=[(0, 'Never'), (5, 'Every 5 seconds'), (15, 'Every 15 seconds'), (30, 'Every 30 seconds'), (60, 'Every minute'), (300, 'Every 5 minutes'), (600, 'Every 10 minutes'), (3600, 'Every hour'), (14400, 'Every 4 hours'), (86400, 'Every day')], default=0), + ), + ] diff --git a/core/models.py b/core/models.py index caf3e97..0cdf8d9 100644 --- a/core/models.py +++ b/core/models.py @@ -13,6 +13,10 @@ SERVICE_CHOICES = (("nordigen", "Nordigen"),) PLATFORM_SERVICE_CHOICES = (("agora", "Agora"),) INTERVAL_CHOICES = ( + (0, "Never"), + (5, "Every 5 seconds"), + (15, "Every 15 seconds"), + (30, "Every 30 seconds"), (60, "Every minute"), (60 * 5, "Every 5 minutes"), (60 * 10, "Every 10 minutes"), @@ -118,7 +122,7 @@ class Platform(models.Model): send = models.BooleanField(default=True) cheat = models.BooleanField(default=False) dummy = models.BooleanField(default=False) - cheat_interval_seconds = models.IntegerField(default=3600, choices=INTERVAL_CHOICES) + cheat_interval_seconds = models.IntegerField(default=0, choices=INTERVAL_CHOICES) margin = models.FloatField(default=1.20) max_margin = models.FloatField(default=1.30)