<commit_before>"""
Celery tasks to save a snapshot of the reports each month
"""
import datetime, logging
from celery import task
from django.http import HttpRequest

from dimagi.utils.dates import DateSpan
from dimagi.utils.couch.database import get_db

from ..opm_reports.reports import (BeneficiaryPaymentReport,
    IncentivePaymentReport, get_report)
from ..opm_reports.constants import DOMAIN
from .models import OpmReportSnapshot


def save_report(ReportClass, month=None, year=None):
    """
    Save a snapshot of the report.
    Pass a month and year to save an arbitrary month.
    """
    existing = OpmReportSnapshot.by_month(month, year, ReportClass.__name__)
    assert existing is None, \
        "Existing report found for %d/%d at %s" % (month, year, existing._id)
    report = get_report(ReportClass, month, year)
    snapshot = OpmReportSnapshot(
        domain=DOMAIN,
        month=month,
        year=year,
        report_class=ReportClass.__name__,
        headers=report.headers,
        slugs=report.slugs,
        rows=report.rows,
    )
    snapshot.save()
    return snapshot


def snapshot():
    for report in [IncentivePaymentReport, BeneficiaryPaymentReport]:
        snapshot = save_report(report)
        msg = "Saving {} to doc {}".format(report.__name__, snapshot._id)
        logging.info(msg)<commit_msg>2.6 style string formatting for opm snapshots<commit_after>"""
Celery tasks to save a snapshot of the reports each month
"""
import datetime, logging
from celery import task
from django.http import HttpRequest

from dimagi.utils.dates import DateSpan
from dimagi.utils.couch.database import get_db

from ..opm_reports.reports import (BeneficiaryPaymentReport,
    IncentivePaymentReport, get_report)
from ..opm_reports.constants import DOMAIN
from .models import OpmReportSnapshot


def save_report(ReportClass, month=None, year=None):
    """
    Save a snapshot of the report.
    Pass a month and year to save an arbitrary month.
    """
    existing = OpmReportSnapshot.by_month(month, year, ReportClass.__name__)
    assert existing is None, \
        "Existing report found for %d/%d at %s" % (month, year, existing._id)
    report = get_report(ReportClass, month, year)
    snapshot = OpmReportSnapshot(
        domain=DOMAIN,
        month=month,
        year=year,
        report_class=ReportClass.__name__,
        headers=report.headers,
        slugs=report.slugs,
        rows=report.rows,
    )
    snapshot.save()
    return snapshot


@task()
def snapshot():
    for report in [IncentivePaymentReport, BeneficiaryPaymentReport]:
        snapshot = save_report(report)
        msg = "Saving {0} to doc {1}".format(report.__name__, snapshot._id)
        logging.info(msg)
{'fraction_non_alphanumeric': 0.08797653958944282, 'fraction_numerical': 0.001466275659824047, 'mean_word_length': 3.4664484451718494, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7032164', 'n_tokens_mistral': 792, 'n_tokens_neox': 781, 'n_words': 237}