# (c) cavaliba.com - app_cmt - tasks.py

from celery import shared_task

import app_home.cache as cache
from app_cmt.common import cleanup_hour, cleanup_raw, sample_day, sample_hour
from app_home.log import DEBUG, log

#   -- sample


@shared_task(ignore_result=True)
def task_sample_hour():
    """Celery task: roll CMTRaw up into CMTHour for the last sampling window."""

    cache.init()

    aaa = None
    total = sample_hour()
    log(
        DEBUG,
        aaa=aaa,
        app="cmt",
        view="cron",
        action="sample_hour",
        status="OK",
        data=f"{total} updated",
    )


@shared_task(ignore_result=True)
def task_sample_day():
    """Celery task: roll CMTHour up into CMTDay for the last sampling window."""

    cache.init()

    aaa = None
    total = sample_day()
    log(
        DEBUG,
        aaa=aaa,
        app="cmt",
        view="cron",
        action="sample_day",
        status="OK",
        data=f"{total} updated",
    )


#   -- cleanup


@shared_task(ignore_result=True)
def task_cleanup_raw():
    """Celery task: delete CMTRaw rows older than CMT_RAW_KEEP_DAYS."""

    cache.init()

    aaa = None
    cleanup_raw()
    log(DEBUG, aaa=aaa, app="cmt", view="cron", action="cleanup_raw", status="OK", data="")


@shared_task(ignore_result=True)
def task_cleanup_hour():
    """Celery task: delete CMTHour rows older than CMT_SAMPLE_HOUR_KEEP_DAYS."""

    cache.init()

    aaa = None
    cleanup_hour()
    log(DEBUG, aaa=aaa, app="cmt", view="cron", action="cleanup_hour", status="OK", data="")
