@@ -0,0 +1,2054 @@
+# -*- coding: utf-8 -*-%0Afrom django.core.management.base import BaseCommand%0A%0Afrom cla_butler.stack import is_first_instance, InstanceNotInAsgException, StackException%0A%0Afrom cla_eventlog.constants import LOG_LEVELS, LOG_TYPES%0Afrom cla_eventlog.models import Log%0Afrom legalaid.models import Case%0A%0A%0Aclass Command(BaseCommand):%0A    help = 'Count and alert when outcome codes denormalized to Case instances are missing'%0A%0A    def handle(self, *args, **options):%0A        if self.should_run_housekeeping(**options):%0A            self.check_for_missing_outcome_codes()%0A        else:%0A            self.stdout.write('Not doing housekeeping because running on secondary instance')%0A%0A    def check_for_missing_outcome_codes(self):%0A        outcomes_that_should_be_denormed = Log.objects.filter(level=LOG_LEVELS.HIGH, type=LOG_TYPES.OUTCOME)%0A        outcomes_missing_denormed_code = outcomes_that_should_be_denormed.filter(case__outcome_code='')%0A        outcomes_missing_denormed_code_pks = outcomes_missing_denormed_code.values_list('id', flat=True)%0A        cases_to_re_denorm = Case.objects.filter(outcome_code='', outcome_code_id__isnull=False,%0A                                                 log_set__id__in=outcomes_missing_denormed_code_pks)%0A%0A        if cases_to_re_denorm.exists():%0A            case_references = cases_to_re_denorm.value_list('reference', flat=True)%0A            n = len(case_references)%0A            self.stdout.write('Cases missing denormalized outcome codes: %7B%7D%5Cn'%0A                              'References: %7B%7D'.format(n, case_references))%0A        else:%0A            self.stdout.write('No cases found missing denormalized outcome codes')%0A%0A    def should_run_housekeeping(self, **options):%0A        if options.get('force', False):%0A            return True%0A%0A        try:%0A            return is_first_instance()%0A        except InstanceNotInAsgException:%0A            self.stderr.write('EC2 instance not in an ASG')%0A            return True%0A        except StackException:%0A            self.stderr.write('Not running on EC2 instance')%0A            return True%0A

Monitor missing outcome codes (WIP)