<s> from datetime import timedelta <EOL> from django . db import connection <EOL> from corehq . apps . locations . models import SQLLocation <EOL> from custom . ilsgateway . models import DeliveryGroups <EOL> from dimagi . utils . dates import get_business_day_of_month <EOL> from dimagi . utils . decorators . memoized import memoized <EOL> class DashboardConfig ( object ) : <EOL> def __init__ ( self , domain , location_id , start_date , end_date ) : <EOL> self . domain = domain <EOL> self . location_id = location_id <EOL> self . start_date = start_date <EOL> self . end_date = end_date <EOL> @ property <EOL> @ memoized <EOL> def sql_location ( self ) : <EOL> return SQLLocation . objects . get ( domain = self . domain , location_id = self . location_id ) <EOL> @ property <EOL> @ memoized <EOL> def descendants ( self ) : <EOL> return list ( self . sql_location . get_descendants ( ) . filter ( location_type__administrative = False ) <EOL> . exclude ( is_archived = True ) ) <EOL> @ property <EOL> @ memoized <EOL> def case_ids ( self ) : <EOL> return tuple ( [ sql_location . supply_point_id for sql_location in self . descendants ] ) <EOL> @ memoized <EOL> def get_location_ids ( self , group ) : <EOL> if not group : <EOL> return tuple ( [ sql_location . supply_point_id for sql_location in self . descendants ] ) <EOL> else : <EOL> return tuple ( <EOL> [ <EOL> sql_location . location_id <EOL> for sql_location in self . descendants <EOL> if sql_location . metadata . get ( '<STR_LIT>' ) == group <EOL> ] <EOL> ) <EOL> @ property <EOL> @ memoized <EOL> def soh_data_total ( self ) : <EOL> if not self . case_ids : <EOL> return { } <EOL> previous_month_date = ( self . start_date . replace ( day = <NUM_LIT:1> ) - timedelta ( days = <NUM_LIT:1> ) ) <EOL> pm_last_business_day = get_business_day_of_month ( previous_month_date . year , previous_month_date . month , - <NUM_LIT:1> ) <EOL> on_time_date = get_business_day_of_month ( self . start_date . year , self . start_date . month , <NUM_LIT:6> ) <EOL> cm_last_business_day = get_business_day_of_month ( self . start_date . year , self . start_date . month , - <NUM_LIT:1> ) <EOL> query = """<STR_LIT>""" <EOL> with connection . cursor ( ) as cursor : <EOL> cursor . execute ( query , [ <EOL> on_time_date , self . domain , pm_last_business_day , <EOL> cm_last_business_day - timedelta ( microseconds = <NUM_LIT:1> ) , self . case_ids <EOL> ] ) <EOL> rows = cursor . fetchall ( ) <EOL> return dict ( rows ) <EOL> @ property <EOL> @ memoized <EOL> def rr_data_total ( self ) : <EOL> current_submitting_group = DeliveryGroups ( self . start_date . month ) . current_submitting_group ( ) <EOL> location_ids = self . get_location_ids ( current_submitting_group ) <EOL> if not location_ids : <EOL> return { } <EOL> on_time_date = get_business_day_of_month ( self . start_date . year , self . start_date . month , <NUM_LIT> ) <EOL> query = """<STR_LIT>""" <EOL> with connection . cursor ( ) as cursor : <EOL> cursor . execute ( query , [ on_time_date , self . start_date , self . end_date , location_ids ] ) <EOL> rows = cursor . fetchall ( ) <EOL> rows = [ ( ( status , status_value ) , count ) for status , status_value , count in rows ] <EOL> rows . append ( ( '<STR_LIT>' , len ( location_ids ) ) ) <EOL> return dict ( rows ) <EOL> @ property <EOL> @ memoized <EOL> def delivery_data_total ( self ) : <EOL> current_delivering_group = DeliveryGroups ( self . start_date . month ) . current_delivering_group ( ) <EOL> location_ids = self . get_location_ids ( current_delivering_group ) <EOL> if not location_ids : <EOL> return { } <EOL> query = """<STR_LIT>""" <EOL> with connection . cursor ( ) as cursor : <EOL> cursor . execute ( query , [ self . start_date , self . end_date , location_ids ] ) <EOL> rows = cursor . fetchall ( ) <EOL> rows . append ( ( '<STR_LIT>' , len ( location_ids ) ) ) <EOL> return dict ( rows ) </s>
