<commit_before>from couchdbkit.ext.django.schema import (Document, StringProperty,
    StringListProperty, IntegerProperty, ListProperty)

from ..opm_reports.constants import DOMAIN, InvalidRow


class OpmReportSnapshot(Document):
    """
    Represents a snapshot of a report, to be taken at the end of each month
    """
    domain = StringProperty()
    month = IntegerProperty()
    year = IntegerProperty()
    report_class = StringProperty()
    headers = StringListProperty()
    slugs = StringListProperty()
    rows = ListProperty()
    block = StringProperty()
    visible_cols = ListProperty()

    @classmethod
    def by_month(cls, month, year, report_class, block=None):
        return cls.view(
            'opm_tasks/opm_snapshots',
            key=[DOMAIN, month, year, report_class, block],
            reduce=False,
            include_docs=True
        ).first()

    @classmethod
    def filtered(cls, snapshot, report):
        filtered_rows = []

        need_filering = False
        for key, field in report.filter_fields:
            keys = report.filter_data.get(field, [])
            if keys:
                need_filering = True

        if need_filering:
            for row in snapshot.rows:
                def key_finder(key):
                    index = snapshot.slugs.index(key)
                    return row[index]
                try:
                    report.filter(key_finder)
                except InvalidRow:
                    pass
                else:
                    filtered_rows.append(row)
            snapshot.rows = filtered_rows
        return snapshot

    @classmethod
    def from_view(cls, report):
        block = None
        if report.block:
            block = report.block.lower()
        snapshot = cls.view(
            'opm_tasks/opm_snapshots',
            key=[DOMAIN, report.month, report.year, report.__class__.__name__, block],
            reduce=False,
            include_docs=True
        ).first()
        if not snapshot:<commit_msg>code refactored<commit_after>from couchdbkit.ext.django.schema import (Document, StringProperty,
    StringListProperty, IntegerProperty, ListProperty)

from ..opm_reports.constants import DOMAIN, InvalidRow


class OpmReportSnapshot(Document):
    """
    Represents a snapshot of a report, to be taken at the end of each month
    """
    domain = StringProperty()
    month = IntegerProperty()
    year = IntegerProperty()
    report_class = StringProperty()
    headers = StringListProperty()
    slugs = StringListProperty()
    rows = ListProperty()
    block = StringProperty()
    visible_cols = ListProperty()

    @classmethod
    def by_month(cls, month, year, report_class, block=None):
        return cls.view(
            'opm_tasks/opm_snapshots',
            key=[DOMAIN, month, year, report_class, block],
            reduce=False,
            include_docs=True
        ).first()

    @classmethod
    def filtered(cls, snapshot, report):
        filtered_rows = []

        need_filering = False
        filters = []
        for key, field in report.filter_fields:
            keys = report.filter_data.get(field, [])
            if keys:
                need_filering = True
                filters.append((key, field))
        if need_filering:
            for row in snapshot.rows:
                def key_finder(key):
                    index = snapshot.slugs.index(key)
                    return row[index]
                try:
                    report.filter(fn=key_finder, filter_fields=filters)
                except InvalidRow:
                    pass
                else:
                    filtered_rows.append(row)
            snapshot.rows = filtered_rows
        return snapshot

    @classmethod
    def from_view(cls, report):
        block = None
        if report.block:
            block = report.block.lower()
        snapshot = cls.view(
            'opm_tasks/opm_snapshots',
            key=[DOMAIN, report.month, report.year, report.__class__.__name__, block],
            reduce=False,
            include_docs=True
        ).first()
        if not snapshot:
{'fraction_non_alphanumeric': 0.08533333333333333, 'fraction_numerical': 0.0, 'mean_word_length': 1.8934081346423561, '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': '5299779', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1070, 'n_words': 268}