<s> from django . db . models . aggregates import Avg , Max <EOL> from custom . ilsgateway . tanzania import ILSData <EOL> from corehq . apps . commtrack . models import SQLProduct <EOL> from corehq . apps . reports . graph_models import MultiBarChart , Axis <EOL> from custom . ilsgateway . models import GroupSummary , SupplyPointStatusTypes , DeliveryGroups , ProductAvailabilityData , ProductAvailabilityDashboardChart <EOL> class RandRSubmissionData ( ILSData ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> @ property <EOL> def rows ( self ) : <EOL> if '<STR_LIT>' in self . config : <EOL> data_config = self . config [ '<STR_LIT>' ] <EOL> submitted_on_time = data_config . rr_data_total . get ( ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> not_submitted_on_time = data_config . rr_data_total . get ( ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> submitted_late = data_config . rr_data_total . get ( ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> not_submitted_late = data_config . rr_data_total . get ( ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> return [ GroupSummary ( <EOL> title = SupplyPointStatusTypes . R_AND_R_FACILITY , <EOL> responded = submitted_on_time + not_submitted_on_time + submitted_late + not_submitted_late , <EOL> on_time = submitted_on_time , <EOL> complete = submitted_on_time + submitted_late , <EOL> total = data_config . rr_data_total . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> ) ] <EOL> if self . config [ '<STR_LIT>' ] : <EOL> rr = GroupSummary . objects . filter ( <EOL> title = SupplyPointStatusTypes . R_AND_R_FACILITY , <EOL> org_summary__in = self . config [ '<STR_LIT>' ] <EOL> ) . aggregate ( Avg ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) , Max ( '<STR_LIT>' ) ) <EOL> return [ GroupSummary ( <EOL> title = SupplyPointStatusTypes . R_AND_R_FACILITY , <EOL> responded = rr [ '<STR_LIT>' ] , <EOL> on_time = rr [ '<STR_LIT>' ] , <EOL> complete = rr [ '<STR_LIT>' ] , <EOL> total = rr [ '<STR_LIT>' ] <EOL> ) ] <EOL> return [ GroupSummary ( <EOL> title = SupplyPointStatusTypes . R_AND_R_FACILITY , <EOL> responded = <NUM_LIT:0> , <EOL> on_time = <NUM_LIT:0> , <EOL> complete = <NUM_LIT:0> , <EOL> total = <NUM_LIT:0> <EOL> ) ] <EOL> class DistrictSummaryData ( ILSData ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> show_table = True <EOL> show_chart = False <EOL> @ property <EOL> def rows ( self ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> def prepare_processing_info ( data ) : <EOL> return { <EOL> '<STR_LIT>' : data [ <NUM_LIT:0> ] - ( data [ <NUM_LIT:1> ] + data [ <NUM_LIT:2> ] ) , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> org_summary = self . config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> total = org_summary . total_orgs <EOL> avg_lead_time = org_summary . average_lead_time_in_days <EOL> if avg_lead_time : <EOL> avg_lead_time = "<STR_LIT>" % avg_lead_time <EOL> endmonth = self . config [ '<STR_LIT>' ] . month <EOL> dg = DeliveryGroups ( month = endmonth ) <EOL> rr_data = RandRSubmissionData ( config = self . config ) . rows [ <NUM_LIT:0> ] <EOL> delivery_data = DeliverySubmissionData ( config = self . config ) . rows [ <NUM_LIT:0> ] <EOL> submitting_group = dg . current_submitting_group ( month = endmonth ) <EOL> processing_group = dg . current_processing_group ( month = endmonth ) <EOL> delivery_group = dg . current_delivering_group ( month = endmonth ) <EOL> ( rr_complete , rr_total ) = ( rr_data . complete , rr_data . total ) if rr_data else ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if delivery_data : <EOL> ( delivery_complete , delivery_total ) = ( delivery_data . complete , delivery_data . total ) <EOL> else : <EOL> ( delivery_complete , delivery_total ) = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> processing_numbers = prepare_processing_info ( [ total , rr_total , delivery_total ] ) <EOL> return { <EOL> "<STR_LIT>" : processing_numbers [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : processing_numbers [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : rr_total , <EOL> "<STR_LIT>" : int ( rr_complete ) , <EOL> "<STR_LIT>" : delivery_total , <EOL> "<STR_LIT>" : int ( delivery_complete ) , <EOL> "<STR_LIT>" : delivery_group , <EOL> "<STR_LIT>" : submitting_group , <EOL> "<STR_LIT>" : processing_group , <EOL> "<STR_LIT>" : total , <EOL> "<STR_LIT>" : avg_lead_time , <EOL> } <EOL> else : <EOL> return None <EOL> class SohSubmissionData ( ILSData ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> @ property <EOL> def rows ( self ) : <EOL> soh_data = [ ] <EOL> if '<STR_LIT>' in self . config : <EOL> data_config = self . config [ '<STR_LIT>' ] <EOL> late = data_config . soh_data_total . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> on_time = data_config . soh_data_total . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> soh_data . append ( GroupSummary ( <EOL> title = SupplyPointStatusTypes . SOH_FACILITY , <EOL> responded = late + on_time , <EOL> on_time = on_time , <EOL> complete = late + on_time , <EOL> total = len ( data_config . descendants ) <EOL> ) ) <EOL> return soh_data <EOL> if self . config [ '<STR_LIT>' ] : <EOL> try : <EOL> sohs = GroupSummary . objects . filter ( <EOL> title = SupplyPointStatusTypes . SOH_FACILITY , <EOL> org_summary__in = self . config [ '<STR_LIT>' ] <EOL> ) . aggregate ( Avg ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) , Max ( '<STR_LIT>' ) ) <EOL> soh_data . append ( GroupSummary ( <EOL> title = SupplyPointStatusTypes . SOH_FACILITY , <EOL> responded = sohs [ '<STR_LIT>' ] , <EOL> on_time = sohs [ '<STR_LIT>' ] , <EOL> complete = sohs [ '<STR_LIT>' ] , <EOL> total = sohs [ '<STR_LIT>' ] <EOL> ) ) <EOL> except GroupSummary . DoesNotExist : <EOL> return soh_data <EOL> return soh_data <EOL> class DeliverySubmissionData ( ILSData ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> @ property <EOL> def rows ( self ) : <EOL> del_data = [ ] <EOL> if '<STR_LIT>' in self . config : <EOL> data_config = self . config [ '<STR_LIT>' ] <EOL> delivered = data_config . delivery_data_total . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> not_delivered = data_config . delivery_data_total . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> del_data . append ( GroupSummary ( <EOL> title = SupplyPointStatusTypes . DELIVERY_FACILITY , <EOL> responded = delivered + not_delivered , <EOL> on_time = delivered + not_delivered , <EOL> complete = delivered , <EOL> total = data_config . delivery_data_total . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> ) ) <EOL> return del_data <EOL> if self . config [ '<STR_LIT>' ] : <EOL> try : <EOL> data = GroupSummary . objects . filter ( <EOL> title = SupplyPointStatusTypes . DELIVERY_FACILITY , <EOL> org_summary__in = self . config [ '<STR_LIT>' ] <EOL> ) . aggregate ( Avg ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) , Max ( '<STR_LIT>' ) ) <EOL> del_data . append ( GroupSummary ( <EOL> title = SupplyPointStatusTypes . DELIVERY_FACILITY , <EOL> responded = data [ '<STR_LIT>' ] , <EOL> on_time = data [ '<STR_LIT>' ] , <EOL> complete = data [ '<STR_LIT>' ] , <EOL> total = data [ '<STR_LIT>' ] <EOL> ) ) <EOL> except GroupSummary . DoesNotExist : <EOL> return del_data <EOL> return del_data <EOL> class ILSMultiBarChart ( MultiBarChart ) : <EOL> template_partial = '<STR_LIT>' <EOL> class ProductAvailabilitySummary ( ILSData ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> def __init__ ( self , config , css_class , chart_stacked = True ) : <EOL> super ( ProductAvailabilitySummary , self ) . __init__ ( config , css_class ) <EOL> self . chart_stacked = chart_stacked <EOL> @ property <EOL> def rows ( self ) : <EOL> product_availability = [ ] <EOL> if self . config [ '<STR_LIT>' ] : <EOL> product_availability = ProductAvailabilityData . objects . filter ( <EOL> date__range = ( self . config [ '<STR_LIT>' ] , self . config [ '<STR_LIT>' ] ) , <EOL> location_id = self . config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . location_id , <EOL> product__in = self . config [ '<STR_LIT>' ] <EOL> ) . values ( '<STR_LIT>' ) . annotate ( <EOL> with_stock = Avg ( '<STR_LIT>' ) , <EOL> without_data = Avg ( '<STR_LIT>' ) , <EOL> without_stock = Avg ( '<STR_LIT>' ) , <EOL> total = Max ( '<STR_LIT>' ) <EOL> ) <EOL> return product_availability <EOL> @ property <EOL> def charts ( self ) : <EOL> product_dashboard = ProductAvailabilityDashboardChart ( ) <EOL> product_availability = self . rows <EOL> def convert_product_data_to_stack_chart ( rows , chart_config ) : <EOL> ret_data = [ ] <EOL> for k in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> datalist = [ ] <EOL> for product in rows : <EOL> prd_code = SQLProduct . objects . get ( product_id = product [ '<STR_LIT>' ] ) . code <EOL> if k == '<STR_LIT>' : <EOL> datalist . append ( [ prd_code , product [ '<STR_LIT>' ] ] ) <EOL> elif k == '<STR_LIT>' : <EOL> datalist . append ( [ prd_code , product [ '<STR_LIT>' ] ] ) <EOL> elif k == '<STR_LIT>' : <EOL> datalist . append ( [ prd_code , product [ '<STR_LIT>' ] ] ) <EOL> ret_data . append ( { '<STR_LIT>' : chart_config . label_color [ k ] , '<STR_LIT:label>' : k , '<STR_LIT:data>' : datalist } ) <EOL> return ret_data <EOL> chart = ILSMultiBarChart ( '<STR_LIT>' , x_axis = Axis ( '<STR_LIT>' ) , y_axis = Axis ( '<STR_LIT>' , format = '<STR_LIT:d>' ) ) <EOL> chart . tooltipFormat = '<STR_LIT>' <EOL> chart . rotateLabels = - <NUM_LIT> <EOL> chart . marginBottom = <NUM_LIT> <EOL> chart . marginRight = <NUM_LIT:20> <EOL> chart . marginLeft = <NUM_LIT:50> <EOL> chart . height = <NUM_LIT> <EOL> chart . stacked = self . chart_stacked <EOL> for row in convert_product_data_to_stack_chart ( product_availability , product_dashboard ) : <EOL> chart . add_dataset ( row [ '<STR_LIT:label>' ] , [ <EOL> { '<STR_LIT:x>' : r [ <NUM_LIT:0> ] , '<STR_LIT:y>' : int ( round ( r [ <NUM_LIT:1> ] ) ) } <EOL> for r in sorted ( row [ '<STR_LIT:data>' ] , key = lambda x : x [ <NUM_LIT:0> ] ) ] , color = row [ '<STR_LIT>' ] <EOL> ) <EOL> return [ chart ] </s>
