<commit_before>from __future__ import absolute_import, print_function

import csv
import datetime

from django.core.management import BaseCommand

from casexml.apps.case.mock import CaseFactory
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from custom.enikshay.case_utils import get_occurrence_case_from_episode, get_person_case_from_episode
from custom.enikshay.const import ENROLLED_IN_PRIVATE
from custom.enikshay.exceptions import ENikshayCaseNotFound

TEST_TO_LABEL = {
    'microscopy-zn': "Microscopy-ZN",
    'microscopy-fluorescent': "Microscopy-Fluorescent",
    'other_dst_tests': "Other DST Tests",
    'other_clinical_tests': "Other Clinical Tests",
    'tst': "TST",
    'igra': "IGRA",
    'chest_x-ray': "Chest X-ray",
    'cytopathology': "Cytopathology",
    'histopathology': "Histopathology",
    'cbnaat': "CBNAAT",
    'culture': "Culture",
    'dst': "DST",
    'line_probe_assay': "Line Probe Assay",
    'fl_line_probe_assay': "FL LPA",
    'sl_line_probe_assay': "SL LPA",
    'gene_sequencing': "Gene Sequencing",
    'other_clinical': "Other Clinical",
    'other_dst': "Other DST",
}


class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('domain')
        parser.add_argument(
            "log_path",
            help="Path to write the log to"
        )
        parser.add_argument('log_errors')
        parser.add_argument(
            '--commit',
            action='store_true',
            help="Actually modifies the cases. Without this flag, it's a dry run."
        )

    def handle(self, domain, log_path, **options):
        commit = options['commit']
        accessor = CaseAccessors(domain)
        factory = CaseFactory(domain)

        headers = [
            'case_id',
            'current_test_confirming_diagnosis',
            'current_date_of_diagnosis',
            'current_treatment_initiation_date',
            'current_diagnosis_test_result_date',
            'current_diagnosis_lab_facility_name',
            'current_diagnosis_lab_facility_id',
            'current_diagnosis_test_lab_serial_number',
            'current_diagnosis_test_summary',
            'current_diagnosis_test_type',
            'current_diagnosis_test_type_label',
            'info_diagnosis_test_case_id',
            'diagnosis_test_result_date',
            'diagnosis_lab_facility_name',
            'diagnosis_lab_facility_id',
            'diagnosis_test_lab_serial_number',
            'diagnosis_test_summary',
            'diagnosis_test_type',
            'diagnosis_test_type_label',
            'datamigration_diagnosis_test_information2',
        ]

        print("Starting {} migration on {} at {}".format(
            "real" if commit else "fake", domain, datetime.datetime.utcnow()
        ))

        with open(log_path, "w") as log_file:
            writer = csv.writer(log_file)
            writer.writerow(headers)

            for episode_case_id in accessor.get_case_ids_in_domain(type='episode'):
                episode = accessor.get_case(episode_case_id)
                case_properties = episode.dynamic_case_properties()

                if self.should_migrate_case(episode_case_id, case_properties, domain):
                    test_confirming_diagnosis = case_properties.get('test_confirming_diagnosis')
                    date_of_diagnosis = case_properties.get('date_of_diagnosis')<commit_msg>add progress bar<commit_after>from __future__ import absolute_import, print_function

import csv
import datetime

from django.core.management import BaseCommand

from casexml.apps.case.mock import CaseFactory
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.util.log import with_progress_bar
from custom.enikshay.case_utils import get_occurrence_case_from_episode, get_person_case_from_episode
from custom.enikshay.const import ENROLLED_IN_PRIVATE
from custom.enikshay.exceptions import ENikshayCaseNotFound

TEST_TO_LABEL = {
    'microscopy-zn': "Microscopy-ZN",
    'microscopy-fluorescent': "Microscopy-Fluorescent",
    'other_dst_tests': "Other DST Tests",
    'other_clinical_tests': "Other Clinical Tests",
    'tst': "TST",
    'igra': "IGRA",
    'chest_x-ray': "Chest X-ray",
    'cytopathology': "Cytopathology",
    'histopathology': "Histopathology",
    'cbnaat': "CBNAAT",
    'culture': "Culture",
    'dst': "DST",
    'line_probe_assay': "Line Probe Assay",
    'fl_line_probe_assay': "FL LPA",
    'sl_line_probe_assay': "SL LPA",
    'gene_sequencing': "Gene Sequencing",
    'other_clinical': "Other Clinical",
    'other_dst': "Other DST",
}


class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('domain')
        parser.add_argument(
            "log_path",
            help="Path to write the log to"
        )
        parser.add_argument('log_errors')
        parser.add_argument(
            '--commit',
            action='store_true',
            help="Actually modifies the cases. Without this flag, it's a dry run."
        )

    def handle(self, domain, log_path, **options):
        commit = options['commit']
        accessor = CaseAccessors(domain)
        factory = CaseFactory(domain)

        headers = [
            'case_id',
            'current_test_confirming_diagnosis',
            'current_date_of_diagnosis',
            'current_treatment_initiation_date',
            'current_diagnosis_test_result_date',
            'current_diagnosis_lab_facility_name',
            'current_diagnosis_lab_facility_id',
            'current_diagnosis_test_lab_serial_number',
            'current_diagnosis_test_summary',
            'current_diagnosis_test_type',
            'current_diagnosis_test_type_label',
            'info_diagnosis_test_case_id',
            'diagnosis_test_result_date',
            'diagnosis_lab_facility_name',
            'diagnosis_lab_facility_id',
            'diagnosis_test_lab_serial_number',
            'diagnosis_test_summary',
            'diagnosis_test_type',
            'diagnosis_test_type_label',
            'datamigration_diagnosis_test_information2',
        ]

        print("Starting {} migration on {} at {}".format(
            "real" if commit else "fake", domain, datetime.datetime.utcnow()
        ))

        with open(log_path, "w") as log_file:
            writer = csv.writer(log_file)
            writer.writerow(headers)

            for episode_case_id in with_progress_bar(accessor.get_case_ids_in_domain(type='episode')):
                episode = accessor.get_case(episode_case_id)
                case_properties = episode.dynamic_case_properties()

                if self.should_migrate_case(episode_case_id, case_properties, domain):
                    test_confirming_diagnosis = case_properties.get('test_confirming_diagnosis')
                    date_of_diagnosis = case_properties.get('date_of_diagnosis')
{'fraction_non_alphanumeric': 0.09650390060676105, 'fraction_numerical': 0.0002889338341519792, 'mean_word_length': 2.9223796033994334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '533546', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2078, 'n_words': 365}