<s> import logging <EOL> from collections import namedtuple <EOL> from corehq . apps . app_manager . const import AMPLIFIES_NOT_SET <EOL> from corehq . apps . app_manager . dbaccessors import get_app <EOL> from corehq . apps . data_analytics . esaccessors import get_app_submission_breakdown_es <EOL> from corehq . apps . data_analytics . models import MALTRow <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . sofabed . models import MISSING_APP_ID <EOL> from corehq . apps . users . util import DEMO_USER_ID , JAVA_ADMIN_USERNAME <EOL> from corehq . util . quickcache import quickcache <EOL> from django . db import IntegrityError <EOL> from django . http . response import Http404 <EOL> logger = logging . getLogger ( __name__ ) <EOL> logger . setLevel ( logging . INFO ) <EOL> MaltAppData = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class MALTTableGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , datespan_object_list ) : <EOL> self . monthspan_list = datespan_object_list <EOL> def build_table ( self ) : <EOL> for domain in Domain . get_all ( ) : <EOL> malt_rows_to_save = [ ] <EOL> logger . info ( "<STR_LIT>" . format ( domain . name ) ) <EOL> all_users_by_id = { user . _id : user for user in domain . all_users ( ) } <EOL> for monthspan in self . monthspan_list : <EOL> try : <EOL> malt_rows_to_save . extend ( self . _get_malt_row_dicts ( domain . name , monthspan , all_users_by_id ) ) <EOL> except Exception as ex : <EOL> logger . error ( "<STR_LIT>" . format <EOL> ( name = domain . name , ex = str ( ex ) ) , exc_info = True ) <EOL> self . _save_to_db ( malt_rows_to_save , domain . _id ) <EOL> def _get_malt_row_dicts ( self , domain_name , monthspan , all_users_by_id ) : <EOL> malt_row_dicts = [ ] <EOL> apps_submitted_for = get_app_submission_breakdown_es ( domain_name , monthspan ) <EOL> for app_row in apps_submitted_for : <EOL> app_id = app_row . app_id <EOL> num_of_forms = app_row . doc_count <EOL> try : <EOL> app_data = self . _app_data ( domain_name , app_id ) <EOL> user_id , username , user_type , email = self . _user_data ( <EOL> app_row . user_id , <EOL> app_row . username , <EOL> all_users_by_id <EOL> ) <EOL> except Exception as ex : <EOL> logger . error ( "<STR_LIT>" . format <EOL> ( id = user_id , app_id = app_id , ex = str ( ex ) ) , exc_info = True ) <EOL> continue <EOL> malt_dict = { <EOL> '<STR_LIT>' : monthspan . startdate , <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT>' : user_type , <EOL> '<STR_LIT>' : domain_name , <EOL> '<STR_LIT>' : num_of_forms , <EOL> '<STR_LIT>' : app_id or MISSING_APP_ID , <EOL> '<STR_LIT>' : app_row . device_id , <EOL> '<STR_LIT>' : MALTRow . AMPLIFY_COUCH_TO_SQL_MAP . get ( app_data . wam , MALTRow . NOT_SET ) , <EOL> '<STR_LIT>' : MALTRow . AMPLIFY_COUCH_TO_SQL_MAP . get ( app_data . pam , MALTRow . NOT_SET ) , <EOL> '<STR_LIT>' : app_data . use_threshold , <EOL> '<STR_LIT>' : app_data . experienced_threshold , <EOL> '<STR_LIT>' : app_data . is_app_deleted , <EOL> } <EOL> malt_row_dicts . append ( malt_dict ) <EOL> return malt_row_dicts <EOL> @ classmethod <EOL> def _save_to_db ( cls , malt_rows_to_save , domain_id ) : <EOL> try : <EOL> MALTRow . objects . bulk_create ( <EOL> [ MALTRow ( ** malt_dict ) for malt_dict in malt_rows_to_save ] <EOL> ) <EOL> except IntegrityError : <EOL> for malt_dict in malt_rows_to_save : <EOL> cls . _update_or_create ( malt_dict ) <EOL> except Exception as ex : <EOL> logger . error ( "<STR_LIT>" . format ( <EOL> id = domain_id , ex = str ( ex ) ) , exc_info = True ) <EOL> @ classmethod <EOL> def _update_or_create ( cls , malt_dict ) : <EOL> try : <EOL> unique_field_dict = { k : v <EOL> for ( k , v ) in malt_dict . iteritems ( ) <EOL> if k in MALTRow . get_unique_fields ( ) } <EOL> prev_obj = MALTRow . objects . get ( ** unique_field_dict ) <EOL> for k , v in malt_dict . iteritems ( ) : <EOL> setattr ( prev_obj , k , v ) <EOL> prev_obj . save ( ) <EOL> except MALTRow . DoesNotExist : <EOL> try : <EOL> MALTRow ( ** malt_dict ) . save ( ) <EOL> except Exception as ex : <EOL> logger . error ( "<STR_LIT>" . format ( <EOL> str ( malt_dict ) , <EOL> str ( ex ) <EOL> ) , exc_info = True ) <EOL> except Exception as ex : <EOL> logger . error ( "<STR_LIT>" . format ( <EOL> str ( malt_dict ) , <EOL> str ( ex ) <EOL> ) , exc_info = True ) <EOL> @ classmethod <EOL> @ quickcache ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _app_data ( cls , domain , app_id ) : <EOL> defaults = MaltAppData ( AMPLIFIES_NOT_SET , AMPLIFIES_NOT_SET , <NUM_LIT:15> , <NUM_LIT:3> , False ) <EOL> if not app_id : <EOL> return defaults <EOL> try : <EOL> app = get_app ( domain , app_id ) <EOL> except Http404 : <EOL> logger . debug ( "<STR_LIT>" % app_id ) <EOL> return defaults <EOL> return MaltAppData ( getattr ( app , '<STR_LIT>' , AMPLIFIES_NOT_SET ) , <EOL> getattr ( app , '<STR_LIT>' , AMPLIFIES_NOT_SET ) , <EOL> getattr ( app , '<STR_LIT>' , <NUM_LIT:15> ) , <EOL> getattr ( app , '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> app . is_deleted ( ) ) <EOL> @ classmethod <EOL> def _user_data ( cls , user_id , username , all_users_by_id ) : <EOL> if user_id in all_users_by_id : <EOL> user = all_users_by_id [ user_id ] <EOL> return ( user . _id , user . username , user . doc_type , user . email ) <EOL> elif user_id == DEMO_USER_ID : <EOL> return ( user_id , username , '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif username == JAVA_ADMIN_USERNAME : <EOL> return ( user_id , username , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> return ( user_id , username , '<STR_LIT>' , '<STR_LIT>' ) </s>
