import logging

from django.apps import AppConfig
from django.conf import settings
from django.db.models.signals import pre_save

_log = logging.getLogger()

started = False

TEST_MODE = getattr(settings, "TEST_MODE", False)


class CorrelationConfig(AppConfig):
    name = 'correlation'
    verbose_name = 'Correlation'

    def ready(self):
        if not TEST_MODE:
            from correlation.services.signals import rule_pre_save_update
            pre_save.connect(rule_pre_save_update, sender=self)
            global started
            if not started:
                from logstash.tasks import initial_task_update
                res = initial_task_update.apply_async(countdown=60, args=(False))
                _log.info(f"Starting correlator rules initialization with task id {res.id}")
                started = True
