Add more interval choices

This commit is contained in:
Mark Veidemanis 2023-03-11 17:43:10 +00:00
parent 5f0c555aa3
commit 8dc1e83d0a
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 52 additions and 7 deletions

View File

@ -4,16 +4,18 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from core.clients.aggregators.nordigen import NordigenClient 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 from core.util import logs
log = logs.get_logger("scheduling") log = logs.get_logger("scheduling")
INTERVAL = 5 INTERVAL_AGGREGATOR = 5
INTERVALS_PLATFORM = [x[0] for x in INTERVAL_CHOICES]
async def job(): async def aggregator_job():
print("Running schedule.")
aggregators = Aggregator.objects.filter(enabled=True, fetch_accounts=True) aggregators = Aggregator.objects.filter(enabled=True, fetch_accounts=True)
for aggregator in aggregators: for aggregator in aggregators:
if aggregator.service == "nordigen": if aggregator.service == "nordigen":
@ -25,6 +27,20 @@ async def job():
aggregator.save() 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): class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
""" """
@ -32,8 +48,15 @@ class Command(BaseCommand):
""" """
scheduler = AsyncIOScheduler() scheduler = AsyncIOScheduler()
log.debug(f"Scheduling checking process job every {INTERVAL} seconds") log.debug(f"Scheduling {INTERVAL_AGGREGATOR} second aggregator job")
scheduler.add_job(job, "interval", seconds=INTERVAL) 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() scheduler.start()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
try: try:

View File

@ -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),
),
]

View File

@ -13,6 +13,10 @@ SERVICE_CHOICES = (("nordigen", "Nordigen"),)
PLATFORM_SERVICE_CHOICES = (("agora", "Agora"),) PLATFORM_SERVICE_CHOICES = (("agora", "Agora"),)
INTERVAL_CHOICES = ( INTERVAL_CHOICES = (
(0, "Never"),
(5, "Every 5 seconds"),
(15, "Every 15 seconds"),
(30, "Every 30 seconds"),
(60, "Every minute"), (60, "Every minute"),
(60 * 5, "Every 5 minutes"), (60 * 5, "Every 5 minutes"),
(60 * 10, "Every 10 minutes"), (60 * 10, "Every 10 minutes"),
@ -118,7 +122,7 @@ class Platform(models.Model):
send = models.BooleanField(default=True) send = models.BooleanField(default=True)
cheat = models.BooleanField(default=False) cheat = models.BooleanField(default=False)
dummy = 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) margin = models.FloatField(default=1.20)
max_margin = models.FloatField(default=1.30) max_margin = models.FloatField(default=1.30)