# (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, log2

#   -- sample


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

    cache.init()

    with log2.timed("cmt.sample_hour", domain="cmt", level=DEBUG) as ev:
        ev["count"] = sample_hour()


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

    cache.init()

    with log2.timed("cmt.sample_day", domain="cmt", level=DEBUG) as ev:
        ev["count"] = sample_day()


#   -- cleanup


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

    cache.init()

    with log2.timed("cmt.cleanup_raw", domain="cmt", level=DEBUG):
        cleanup_raw()


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

    cache.init()

    with log2.timed("cmt.cleanup_hour", domain="cmt", level=DEBUG):
        cleanup_hour()
