<commit_before>"""
Celery tasks to save a snapshot of the reports each month
"""
import logging
import traceback

from celery.task import periodic_task
from celery.schedules import crontab
from celery.task.base import task
from dimagi.utils.decorators.memoized import memoized
from django.conf import settings
from corehq.apps.reports import util
from corehq.apps.reports.models import HQUserType
from corehq.apps.users.models import CouchUser, WebUser
from custom.opm.opm_tasks import DEVELOPERS_EMAILS

from ..opm_reports.reports import (BeneficiaryPaymentReport,
                                   IncentivePaymentReport, MetReport, get_report, last_if_none)
from ..opm_reports.constants import DOMAIN
from .models import OpmReportSnapshot
from dimagi.utils.django.email import send_HTML_email


def prepare_snapshot(month, year, ReportClass, block=None):
    existing = OpmReportSnapshot.by_month(month, year, ReportClass.__name__, block)
    assert existing is None, \
        "Existing report found for %s/%s at %s" % (month, year, existing._id)
    report = get_report(ReportClass, month, year, block)
    snapshot = OpmReportSnapshot(
        domain=DOMAIN,
        month=report.month,
        year=report.year,
        block=report.block,
        report_class=ReportClass.__name__,
        headers=report.headers,
        slugs=report.slugs,
        rows=report.rows,
        visible_cols=report.visible_cols

    )
    snapshot.save()
    return snapshot


def save_report(ReportClass, month=None, year=None):
    """
    Save a snapshot of the report.
    Pass a month and year to save an arbitrary month.
    """
    month, year = last_if_none(month, year)
    if ReportClass.__name__ == "MetReport":
        for block in ['atri', 'wazirganj']:
            snapshot = prepare_snapshot(month, year, ReportClass, block)
    else:
        snapshot = prepare_snapshot(month, year, ReportClass)
    return snapshot


@periodic_task(
    run_every=crontab(hour=10, minute=1, day_of_month=1),
    queue=getattr(settings, 'CELERY_PERIODIC_QUEUE', 'celery')
)
def snapshot():
    save_ip_report.delay(IncentivePaymentReport)
    save_bp_report.delay(BeneficiaryPaymentReport)
    save_met_report.delay(MetReport)


def get_admins_emails():
    return map(lambda user: user.get_email(),
               filter(lambda user: user.get_role(DOMAIN, False, False).name == 'Succeed Admin',
               WebUser.by_domain(DOMAIN)))

def send_emails(title, msg):
    emails = get_admins_emails()
    for email in DEVELOPERS_EMAILS + emails:
        send_HTML_email(title, email, msg,
                        email_from=settings.DEFAULT_FROM_EMAIL)


def save(report, f):
    try:
        snapshot = save_report(report)
        title = "[commcarehq] {0} saving success.".format(report.__name__)
        msg = "Saving {0} to doc {1} was finished successfully".format(report.__name__, snapshot._id)
        send_emails(title, msg)
        logging.info(msg)
    except AssertionError as ae:
        logging.error(ae.message)
    except Exception as e:
        tb = traceback.format_exc()
        title = "[commcarehq] {0} saving error.".format(report.__name__)
        msg = "Error in saving doc {0} due to {1}. {2}".format(report.__name__, e.message, tb)
        send_emails(title, msg)
        logging.error(msg)
        f.retry(exc=e)


@task(default_retry_delay=240 * 60, max_retries=12)
def save_ip_report(report):
    save(report, save_ip_report)


@task(default_retry_delay=240 * 60, max_retries=12)
def save_bp_report(report):
    save(report, save_bp_report)


@task(default_retry_delay=240 * 60, max_retries=12)
def save_met_report(report):
    save(report, save_met_report)<commit_msg>Removed unused imports<commit_after>"""
Celery tasks to save a snapshot of the reports each month
"""
import logging
import traceback

from celery.task import periodic_task
from celery.schedules import crontab
from celery.task.base import task

from django.conf import settings

from corehq.apps.users.models import WebUser
from custom.opm.opm_tasks import DEVELOPERS_EMAILS

from ..opm_reports.reports import (BeneficiaryPaymentReport,
                                   IncentivePaymentReport, MetReport, get_report, last_if_none)
from ..opm_reports.constants import DOMAIN
from .models import OpmReportSnapshot
from dimagi.utils.django.email import send_HTML_email


def prepare_snapshot(month, year, ReportClass, block=None):
    existing = OpmReportSnapshot.by_month(month, year, ReportClass.__name__, block)
    assert existing is None, \
        "Existing report found for %s/%s at %s" % (month, year, existing._id)
    report = get_report(ReportClass, month, year, block)
    snapshot = OpmReportSnapshot(
        domain=DOMAIN,
        month=report.month,
        year=report.year,
        block=report.block,
        report_class=ReportClass.__name__,
        headers=report.headers,
        slugs=report.slugs,
        rows=report.rows,
        visible_cols=report.visible_cols

    )
    snapshot.save()
    return snapshot


def save_report(ReportClass, month=None, year=None):
    """
    Save a snapshot of the report.
    Pass a month and year to save an arbitrary month.
    """
    month, year = last_if_none(month, year)
    if ReportClass.__name__ == "MetReport":
        for block in ['atri', 'wazirganj']:
            snapshot = prepare_snapshot(month, year, ReportClass, block)
    else:
        snapshot = prepare_snapshot(month, year, ReportClass)
    return snapshot


@periodic_task(
    run_every=crontab(hour=10, minute=1, day_of_month=1),
    queue=getattr(settings, 'CELERY_PERIODIC_QUEUE', 'celery')
)
def snapshot():
    save_ip_report.delay(IncentivePaymentReport)
    save_bp_report.delay(BeneficiaryPaymentReport)
    save_met_report.delay(MetReport)


def get_admins_emails():
    return map(lambda user: user.get_email(),
               filter(lambda user: user.get_role(DOMAIN, False, False).name == 'Succeed Admin',
               WebUser.by_domain(DOMAIN)))

def send_emails(title, msg):
    emails = get_admins_emails()
    for email in DEVELOPERS_EMAILS + emails:
        send_HTML_email(title, email, msg,
                        email_from=settings.DEFAULT_FROM_EMAIL)


def save(report, f):
    try:
        snapshot = save_report(report)
        title = "[commcarehq] {0} saving success.".format(report.__name__)
        msg = "Saving {0} to doc {1} was finished successfully".format(report.__name__, snapshot._id)
        send_emails(title, msg)
        logging.info(msg)
    except AssertionError as ae:
        logging.error(ae.message)
    except Exception as e:
        tb = traceback.format_exc()
        title = "[commcarehq] {0} saving error.".format(report.__name__)
        msg = "Error in saving doc {0} due to {1}. {2}".format(report.__name__, e.message, tb)
        send_emails(title, msg)
        logging.error(msg)
        f.retry(exc=e)


@task(default_retry_delay=240 * 60, max_retries=12)
def save_ip_report(report):
    save(report, save_ip_report)


@task(default_retry_delay=240 * 60, max_retries=12)
def save_bp_report(report):
    save(report, save_bp_report)


@task(default_retry_delay=240 * 60, max_retries=12)
def save_met_report(report):
    save(report, save_met_report)
{'fraction_non_alphanumeric': 0.09802295036637633, 'fraction_numerical': 0.008848334024609428, 'mean_word_length': 3.5043586550435863, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7562445', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2272, 'n_words': 542}