from logging import Logger
from services.model import CleanDatabasesRequest
from externals.repository import AlertsRepository
from services.model import Configs


class CleanDatabasesInteractor(object):
    def __init__(
        self,
        alerts_repository,  # type: AlertsRepository
        configs,            # type: Configs
        logger,             # type: Logger
    ):
        # type: (...) -> None

        self.alerts_repository = alerts_repository
        self.configs = configs
        self.logger = logger

    def process_request(
        self,
        request,  # type: CleanDatabasesRequest
    ):
        # type: (...) -> None

        try:
            outdated_event_days = request.default_outdated_events_days
            if "OutdatedEventDays" in self.configs.alerts:
                outdated_event_days = int(self.configs.alerts["OutdatedEventDays"])

            self.alerts_repository.delete_outdated_events(range_days=outdated_event_days)
            self.alerts_repository.vacuum_database()
        except Exception as e:
            self.logger.exception(e.message)
