<s> import logging <EOL> from django . conf import settings <EOL> from pytz import timezone <EOL> from datetime import datetime , timedelta , date <EOL> from pact . enums import ( <EOL> CASE_ART_REGIMEN_PROP , <EOL> CASE_NONART_REGIMEN_PROP , <EOL> DAY_SLOTS_BY_TIME , <EOL> DOT_ART , <EOL> DOT_DAYS_INTERVAL , <EOL> DOT_NONART , <EOL> DOT_OBSERVATION_DIRECT , <EOL> DOT_UNCHECKED_CELL , <EOL> ) <EOL> from pact . models import CObservation <EOL> class DOTDayDose ( object ) : <EOL> drug_class = None <EOL> total_doses = <NUM_LIT:0> <EOL> def __init__ ( self , drug_class ) : <EOL> self . drug_class = drug_class <EOL> self . total_doses_hist = { } <EOL> self . dose_dict = { } <EOL> def has_obs ( self , obs ) : <EOL> if self . dose_dict . get ( obs . dose_number , None ) is None : <EOL> return False <EOL> else : <EOL> return True <EOL> def add_obs ( self , obs ) : <EOL> if not self . has_obs ( obs ) : <EOL> self . dose_dict [ obs . dose_number ] = [ ] <EOL> self . dose_dict [ obs . dose_number ] . append ( obs ) <EOL> def update_total_doses ( self , obs ) : <EOL> if self . total_doses < obs . total_doses : <EOL> self . total_doses = obs . total_doses <EOL> if '<STR_LIT>' not in self . total_doses_hist : <EOL> self . total_doses_hist [ obs . total_doses ] = [ ] <EOL> self . total_doses_hist [ obs . total_doses ] . append ( obs ) <EOL> class DOTDay ( object ) : <EOL> nonart = None <EOL> art = None <EOL> def __init__ ( self ) : <EOL> self . nonart = DOTDayDose ( DOT_NONART ) <EOL> self . art = DOTDayDose ( DOT_ART ) <EOL> def sort_all_observations ( self ) : <EOL> for day_doses in [ self . nonart , self . art ] : <EOL> dose_dict = day_doses . dose_dict <EOL> for dose_num in dose_dict . keys ( ) : <EOL> observations = dose_dict [ dose_num ] <EOL> dose_dict [ dose_num ] = sort_observations ( observations ) <EOL> def update_dosedata ( self , obs ) : <EOL> if obs . is_art : <EOL> dot_day_dose = self . art <EOL> else : <EOL> dot_day_dose = self . nonart <EOL> dot_day_dose . update_total_doses ( obs ) <EOL> dot_day_dose . add_obs ( obs ) <EOL> @ classmethod <EOL> def merge_from_observations ( cls , day_observations ) : <EOL> """<STR_LIT>""" <EOL> dot_day = cls ( ) <EOL> for obs in day_observations : <EOL> dot_day . update_dosedata ( obs ) <EOL> dot_day . sort_all_observations ( ) <EOL> return dot_day <EOL> def to_case_json ( self , casedoc , regimen_labels ) : <EOL> """<STR_LIT>""" <EOL> def get_obs_for_dosenum ( obs_list , dose_num , label ) : <EOL> if len ( obs_list ) > <NUM_LIT:0> : <EOL> obs = obs_list [ <NUM_LIT:0> ] <EOL> day_slot = label <EOL> if obs . day_slot != '<STR_LIT>' and obs . day_slot is not None : <EOL> day_slot = obs . day_slot <EOL> if ( obs . day_note is not None and len ( obs . day_note ) > <NUM_LIT:0> <EOL> and obs . day_note != "<STR_LIT>" ) : <EOL> day_note = obs . day_note <EOL> else : <EOL> day_note = '<STR_LIT>' <EOL> return [ obs . adherence , obs . method , day_note , day_slot ] <EOL> else : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , label ] <EOL> ret = [ ] <EOL> for ix , dose_data in enumerate ( [ self . nonart , self . art ] ) : <EOL> drug_arr = [ ] <EOL> labels_arr = regimen_labels [ ix ] <EOL> dose_nums = dose_data . dose_dict . keys ( ) <EOL> dose_nums . sort ( ) <EOL> for dose_num in dose_nums : <EOL> if dose_num is not None : <EOL> obs_list = dose_data . dose_dict [ dose_num ] <EOL> drug_arr . append ( get_obs_for_dosenum ( obs_list , dose_num , labels_arr [ dose_num ] ) ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' ) <EOL> if len ( drug_arr ) <= dose_data . total_doses : <EOL> if dose_data . drug_class == DOT_NONART : <EOL> max_doses = int ( getattr ( casedoc , '<STR_LIT>' , None ) or <NUM_LIT:0> ) <EOL> elif dose_data . drug_class == DOT_ART : <EOL> max_doses = int ( getattr ( casedoc , '<STR_LIT>' , None ) or <NUM_LIT:0> ) <EOL> delta = max_doses - dose_data . total_doses <EOL> for x in range ( <NUM_LIT:0> , delta ) : <EOL> drug_arr . append ( [ "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' , labels_arr [ x ] if x < len ( labels_arr ) else - <NUM_LIT:1> ] ) <EOL> ret . append ( drug_arr ) <EOL> return ret <EOL> def filter_obs_for_day ( this_date , observations ) : <EOL> assert this_date . __class__ == date <EOL> ret = filter ( lambda x : x [ '<STR_LIT>' ] . date ( ) == this_date , observations ) <EOL> return ret <EOL> def query_observations ( case_id , start_date , end_date ) : <EOL> """<STR_LIT>""" <EOL> startkey = [ case_id , '<STR_LIT>' , start_date . year , start_date . month , start_date . day ] <EOL> endkey = [ case_id , '<STR_LIT>' , end_date . year , end_date . month , end_date . day ] <EOL> observations = CObservation . view ( '<STR_LIT>' , <EOL> startkey = startkey , endkey = endkey ) . all ( ) <EOL> return observations <EOL> def query_observations_singledoc ( doc_id ) : <EOL> """<STR_LIT>""" <EOL> key = [ '<STR_LIT>' , doc_id ] <EOL> observations = CObservation . view ( '<STR_LIT>' , key = key , <EOL> classes = { None : CObservation } ) . all ( ) <EOL> return observations <EOL> def cmp_observation ( x , y ) : <EOL> """<STR_LIT>""" <EOL> assert x . observed_date . date ( ) == y . observed_date . date ( ) <EOL> x_is_reconciliation = getattr ( x , '<STR_LIT>' , None ) <EOL> y_is_reconciliation = getattr ( y , '<STR_LIT>' , None ) <EOL> if x_is_reconciliation and y_is_reconciliation : <EOL> return cmp ( y . submitted_date , x . submitted_date ) <EOL> elif x_is_reconciliation and not y_is_reconciliation : <EOL> return <NUM_LIT:1> <EOL> elif not x_is_reconciliation and y_is_reconciliation : <EOL> return - <NUM_LIT:1> <EOL> elif x . method == DOT_OBSERVATION_DIRECT and y . method == DOT_OBSERVATION_DIRECT : <EOL> return cmp ( y . encounter_date , x . encounter_date ) <EOL> elif x . method == DOT_OBSERVATION_DIRECT and y . method != DOT_OBSERVATION_DIRECT : <EOL> return <NUM_LIT:1> <EOL> elif x . method != DOT_OBSERVATION_DIRECT and y . method == DOT_OBSERVATION_DIRECT : <EOL> return - <NUM_LIT:1> <EOL> elif ( x . adherence , x . method ) == DOT_UNCHECKED_CELL and ( y . adherence , y . method ) != DOT_UNCHECKED_CELL : <EOL> return - <NUM_LIT:1> <EOL> elif ( x . adherence , x . method ) != DOT_UNCHECKED_CELL and ( y . adherence , y . method ) == DOT_UNCHECKED_CELL : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return cmp ( y . encounter_date , x . encounter_date ) <EOL> def sort_observations ( observations ) : <EOL> """<STR_LIT>""" <EOL> return sorted ( observations , cmp = cmp_observation , reverse = True ) <EOL> def get_dots_case_json ( casedoc , anchor_date = None ) : <EOL> """<STR_LIT>""" <EOL> if anchor_date is None : <EOL> anchor_date = datetime . now ( tz = timezone ( settings . TIME_ZONE ) ) <EOL> enddate = anchor_date <EOL> ret = { <EOL> '<STR_LIT>' : [ <EOL> int ( getattr ( casedoc , CASE_NONART_REGIMEN_PROP , None ) or <NUM_LIT:0> ) , <EOL> int ( getattr ( casedoc , CASE_ART_REGIMEN_PROP , None ) or <NUM_LIT:0> ) , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> list ( casedoc . nonart_labels ) , <EOL> list ( casedoc . art_labels ) <EOL> ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : anchor_date . strftime ( "<STR_LIT>" ) , <EOL> } <EOL> observations = query_observations ( <EOL> casedoc . _id , enddate - timedelta ( days = DOT_DAYS_INTERVAL ) , enddate ) <EOL> for delta in range ( DOT_DAYS_INTERVAL ) : <EOL> obs_date = enddate - timedelta ( days = delta ) <EOL> day_arr = filter_obs_for_day ( obs_date . date ( ) , observations ) <EOL> day_data = DOTDay . merge_from_observations ( day_arr ) <EOL> ret [ '<STR_LIT>' ] . append ( day_data . to_case_json ( casedoc , ret [ '<STR_LIT>' ] ) ) <EOL> ret [ '<STR_LIT>' ] . reverse ( ) <EOL> return ret </s>
