def get_user_undone_trade(self, user_id: int) -> UndoneTrade: if user_id is None: raise ScoringException(3, 'user_id can not be None!') dic = self.db.undoneTrades.find_one({USER_ID: user_id}) if dic is None: return UndoneTrade() return UndoneTrade.parse_obj(dic)
def calculate_score(scenarios_dicts: [], user_id: int = None): # mongoengine_api.launch_app() ds = DataService() rds = RedisCaching(ds) cs = ScoreCalculationService_2(rds, ds) # crm = RedisCachingRulesMasters(rds.rds) for scn_dict in scenarios_dicts: expected_score = scn_dict['Vscore'] # DoneTrade Score Calculation .................................................. dt = DoneTrade(user_id=user_id) dt.timely_trades_count_of_last_3_months = scn_dict['Last3MSD'] dt.timely_trades_count_between_last_3_to_12_months = scn_dict[ 'Last1YSD'] dt.past_due_trades_count_of_last_3_months = scn_dict[ 'B30DayDelayLast3M'] dt.past_due_trades_count_between_last_3_to_12_months = scn_dict[ 'B30DayDelayLast3-12M'] dt.arrear_trades_count_of_last_3_months = scn_dict['A30DayDelayLast3M'] dt.arrear_trades_count_between_last_3_to_12_months = scn_dict[ 'A30DayDelay3-12M'] dt.total_delay_days = scn_dict['AverageDelayRatio'] # todo: 100000000 is fix Denominator that is all_other_users_done_trades_amount, it should be change later dt.trades_total_balance = round( float(scn_dict['SDealAmountRatio']) * ALL_USERS_AVERAGE_DEAL_AMOUNT) done_trades_score = cs.calculate_user_done_trades_score_2( user_id=0, modified_done_trade=dt) ds.delete_done_trades({USER_ID: user_id}) ds.insert_done_trade(dt) # UndoneTrade Score Calculation .................................................. udt = UndoneTrade(user_id=user_id) udt.undue_trades_count = scn_dict['NumNotDueDeal'] udt.past_due_trades_count = scn_dict['UnfinishedB30DayDelay'] udt.arrear_trades_count = scn_dict['UnfinishedA30DayDelay'] udt.undue_trades_total_balance_of_last_year = round( float(scn_dict['NotDueDealAmountRatio']) * dt.trades_total_balance) udt.past_due_trades_total_balance_of_last_year = round( float(scn_dict['UnfinishedB30Din1YRatio']) * dt.trades_total_balance) udt.arrear_trades_total_balance_of_last_year = round( float(scn_dict['UnfinishedA30Din1YRatio']) * dt.trades_total_balance) undone_trades_score = cs.calculate_user_undone_trades_score_2( user_id=0, undone_trade_object=udt, done_trade_object=dt) ds.delete_undone_trades({USER_ID: user_id}) ds.insert_undone_trade(udt) # Loan Score Calculation .................................................. ln = Loan(user_id=user_id) ln.loans_total_count = scn_dict['Loans'] ln.loans_total_balance = ALL_USERS_AVERAGE_PRINCIPAL_INTEREST_AMOUNT ln.past_due_loans_total_count = int(scn_dict['PastDueLoans']) ln.arrear_loans_total_count = int(scn_dict['DelayedLoans']) ln.suspicious_loans_total_count = int( scn_dict['DoubfulCollectionLoans']) ln.monthly_installments_total_balance = float( scn_dict['MonthlyInstallments']) ln.overdue_loans_total_balance = round( float(scn_dict['CurrentLoanAmountRatio']) * ln.loans_total_balance) ln.past_due_loans_total_balance = round( float(scn_dict['PastDueLoanAmountRatio']) * ln.loans_total_balance) ln.arrear_loans_total_balance = round( float(scn_dict['DelayedLoanAmountRatio']) * ln.loans_total_balance) ln.suspicious_loans_total_balance = round( float(scn_dict['DoubtfulCollectionAmountRatio']) * ln.loans_total_balance) loan_score = cs.calculate_user_loans_score_2(user_id=0, loan_object=ln) ds.delete_loans({USER_ID: user_id}) ds.insert_loan(ln) # Cheque Score Calculation .................................................. ch = Cheque(user_id=user_id) ch.unfixed_returned_cheques_count_of_last_3_months = scn_dict[ 'DishonouredChequesL3M'] ch.unfixed_returned_cheques_count_between_last_3_to_12_months = scn_dict[ 'DishonouredChequesL3-12M'] ch.unfixed_returned_cheques_count_of_more_12_months = scn_dict[ 'DishonouredChequesA12M'] ch.unfixed_returned_cheques_count_of_last_5_years = scn_dict[ 'AllDishonouredCheques'] ch.unfixed_returned_cheques_total_balance = round( float(scn_dict['DCAmountRatio']) * ALL_USERS_AVERAGE_UNFIXED_RETURNED_CHEQUES_AMOUNT) cheque_score: [] = cs.calculate_user_cheques_score_2(user_id=0, cheque_object=ch) ds.delete_cheques({USER_ID: user_id}) ds.insert_cheque(ch) # Profile Score Calculation .................................................. if user_id is not None: ds.delete_profiles({USER_ID: user_id}) p = Profile(user_id=user_id) p.has_kyc = scn_dict['KYC'] p.military_service_status = ProfileMilitaryServiceStatusEnum.__getitem__( scn_dict['Military']) p.sim_card_ownership = scn_dict['SimCard'] p.address_verification = scn_dict['Address'] p.membership_date = date.today() - timedelta( days=int(scn_dict['Membership'])) p.recommended_to_others_count = scn_dict['Recommendation'] p.star_count_average = scn_dict['WeightedAveStars'] profile_score = cs.calculate_user_profile_score_2(user_id=0, profile_object=p) # total_pure_score = int(profile_score) + int(done_trades_score) + int(undone_trades_score) + int(loan_score) + int(cheque_score) identities_pure_score = cs.scores_dict.get(IDENTITIES_SCORE) identities_normalized_score = cs.calculate_identities_normalized_score_2( identities_pure_score) histories_pure_score = cs.scores_dict.get(HISTORIES_SCORE) histories_normalized_score = cs.calculate_histories_normalized_score_2( histories_pure_score) volumes_pure_score = cs.scores_dict.get(VOLUMES_SCORE) volumes_normalized_score = cs.calculate_volumes_normalized_score_2( volumes_pure_score) timeliness_pure_score = cs.scores_dict.get(TIMELINESS_SCORE) timeliness_normalized_score = cs.calculate_timeliness_normalized_score_2( timeliness_pure_score) total_pure_score = identities_pure_score + histories_pure_score + volumes_pure_score + timeliness_pure_score total_normalized_score = identities_normalized_score + histories_normalized_score + volumes_normalized_score + timeliness_normalized_score print( '<><><><><><><> expected-score= {} , total_pure_score = {} and total_normalized_score = {} ' '<><><><><><><>'.format(expected_score, total_pure_score, total_normalized_score)) # Profile Score insertion p.score = total_normalized_score p.identities_score = identities_normalized_score p.histories_score = histories_normalized_score p.volumes_score = volumes_normalized_score p.timeliness_score = timeliness_normalized_score ds.delete_profiles({USER_ID: user_id}) ds.insert_profile(p)
def import_undone_trades_data(uid: long): dt = UndoneTrade() dt.drop_collection() dt.user_id = uid dt.calculation_start_date = datetime.now() dt.calculation_start_date = datetime.now() dt.undue_trades_count = 2 dt.past_due_trades_count = 2 dt.arrear_trades_count = 3 dt.undue_trades_total_balance_of_last_year = 3000000000 dt.past_due_trades_total_balance_of_last_year = 2000000000 dt.arrear_trades_total_balance_of_last_year = 10000000 dt.save()
def import_undone_trades_data(uid: long): UndoneTrade.objects(user_id=uid).delete() dt = UndoneTrade() dt.drop_collection() dt.user_id = uid dt.calculation_start_date = datetime.now() dt.calculation_start_date = datetime.now() dt.undue_trades_count = 0 dt.past_due_trades_count = 0 dt.arrear_trades_count = 0 dt.undue_trades_total_balance_of_last_year = 45000000 dt.past_due_trades_total_balance_of_last_year = 0 dt.arrear_trades_total_balance_of_last_year = 0 dt.save()
def calculate_score(scenarios_dicts: [], user_id: int): ds = DataService() rds = RedisCaching(ds) cs = ScoreCalculationService(rds, ds) for scn_dict in scenarios_dicts: # Profile Score Calculation .................................................. recent_p = ds.get_user_profile(user_id) revised_p = create_revised_profile(user_id=user_id, recent_p=recent_p) if is_not_none(scn_dict['KYC']): revised_p.has_kyc = scn_dict['KYC'] if is_not_none(scn_dict['Military']): revised_p.military_service_status = ProfileMilitaryServiceStatusEnum.__getitem__( scn_dict['Military']) if is_not_none(scn_dict['SimCard']): revised_p.sim_card_ownership = scn_dict['SimCard'] if is_not_none(scn_dict['Address']): revised_p.address_verification = scn_dict['Address'] if is_not_none(scn_dict['Membership']): revised_p.membership_date = date.today() - timedelta( days=int(scn_dict['Membership'])) if is_not_none(scn_dict['Recommendation']): revised_p.recommended_to_others_count = scn_dict['Recommendation'] if is_not_none(scn_dict['WeightedAveStars']): revised_p.star_count_average = scn_dict['WeightedAveStars'] cs.calculate_user_profile_score(recent_p=recent_p, revised_p=revised_p) # DoneTrade Score Calculation .................................................. dt = DoneTrade(user_id=user_id) if is_not_none(scn_dict['Last3MSD']): dt.timely_trades_count_of_last_3_months = scn_dict['Last3MSD'] if is_not_none(scn_dict['Last1YSD']): dt.timely_trades_count_between_last_3_to_12_months = scn_dict[ 'Last1YSD'] if is_not_none(scn_dict['B30DayDelayLast3M']): dt.past_due_trades_count_of_last_3_months = scn_dict[ 'B30DayDelayLast3M'] if is_not_none(scn_dict['B30DayDelayLast3-12M']): dt.past_due_trades_count_between_last_3_to_12_months = scn_dict[ 'B30DayDelayLast3-12M'] if is_not_none(scn_dict['A30DayDelayLast3M']): dt.arrear_trades_count_of_last_3_months = scn_dict[ 'A30DayDelayLast3M'] if is_not_none(scn_dict['A30DayDelay3-12M']): dt.arrear_trades_count_between_last_3_to_12_months = scn_dict[ 'A30DayDelay3-12M'] if is_not_none(scn_dict['AverageDelayRatio']): dt.total_delay_days = scn_dict['AverageDelayRatio'] if is_not_none(scn_dict['SDealAmountRatio']): # todo: 100000000 is fix Denominator that is all_other_users_done_trades_amount, it should be change later dt.trades_total_balance = round( float(scn_dict['SDealAmountRatio']) * ALL_USERS_AVERAGE_DEAL_AMOUNT) recent_dt = ds.get_user_done_trade(user_id) cs.calculate_user_done_trades_score(revised_p=revised_p, recent_dt=recent_dt, revised_dt=dt) ds.insert_or_update_done_trade(dt, update_flag=recent_dt.user_id is not None) # UndoneTrade Score Calculation .................................................. udt = UndoneTrade(user_id=user_id) if is_not_none(scn_dict['NumNotDueDeal']): udt.undue_trades_count = scn_dict['NumNotDueDeal'] if is_not_none(scn_dict['UnfinishedB30DayDelay']): udt.past_due_trades_count = scn_dict['UnfinishedB30DayDelay'] if is_not_none(scn_dict['UnfinishedA30DayDelay']): udt.arrear_trades_count = scn_dict['UnfinishedA30DayDelay'] dt.trades_total_balance = get_zero_if_none(dt.trades_total_balance) if is_not_none(scn_dict['NotDueDealAmountRatio']): udt.undue_trades_total_balance_of_last_year = round( float(scn_dict['NotDueDealAmountRatio']) * dt.trades_total_balance) if is_not_none(scn_dict['UnfinishedB30Din1YRatio']): udt.past_due_trades_total_balance_of_last_year = round( float(scn_dict['UnfinishedB30Din1YRatio']) * dt.trades_total_balance) if is_not_none(scn_dict['UnfinishedA30Din1YRatio']): udt.arrear_trades_total_balance_of_last_year = round( float(scn_dict['UnfinishedA30Din1YRatio']) * dt.trades_total_balance) recent_udt = ds.get_user_undone_trade(user_id) cs.calculate_user_undone_trades_score(revised_p=revised_p, recent_udt=recent_udt, revised_udt=udt, dt=dt) ds.insert_or_update_undone_trade(udt, update_flag=recent_udt.user_id is not None) # Loan Score Calculation .................................................. ln = Loan(user_id=user_id) if is_not_none(scn_dict['Loans']): ln.loans_total_count = scn_dict['Loans'] ln.loans_total_balance = ALL_USERS_AVERAGE_PRINCIPAL_INTEREST_AMOUNT if is_not_none(scn_dict['PastDueLoans']): ln.past_due_loans_total_count = int(scn_dict['PastDueLoans']) if is_not_none(scn_dict['DelayedLoans']): ln.arrear_loans_total_count = int(scn_dict['DelayedLoans']) if is_not_none(scn_dict['DoubfulCollectionLoans']): ln.suspicious_loans_total_count = int( scn_dict['DoubfulCollectionLoans']) if is_not_none(scn_dict['MonthlyInstallments']): ln.monthly_installments_total_balance = float( scn_dict['MonthlyInstallments']) if is_not_none(scn_dict['CurrentLoanAmountRatio']): ln.overdue_loans_total_balance = round( float(scn_dict['CurrentLoanAmountRatio']) * ln.loans_total_balance) if is_not_none(scn_dict['PastDueLoanAmountRatio']): ln.past_due_loans_total_balance = round( float(scn_dict['PastDueLoanAmountRatio']) * ln.loans_total_balance) if is_not_none(scn_dict['DelayedLoanAmountRatio']): ln.arrear_loans_total_balance = round( float(scn_dict['DelayedLoanAmountRatio']) * ln.loans_total_balance) if is_not_none(scn_dict['DoubtfulCollectionAmountRatio']): ln.suspicious_loans_total_balance = round( float(scn_dict['DoubtfulCollectionAmountRatio']) * ln.loans_total_balance) recent_ln = ds.get_user_loan(user_id) loan_score = cs.calculate_user_loans_score(revised_p=revised_p, recent_ln=recent_ln, revised_ln=ln) ds.insert_or_update_loan(ln, update_flag=recent_ln.user_id is not None) # Cheque Score Calculation .................................................. ch = Cheque(user_id=user_id) if is_not_none(scn_dict['DishonouredChequesL3M']): ch.unfixed_returned_cheques_count_of_last_3_months = scn_dict[ 'DishonouredChequesL3M'] if is_not_none(scn_dict['DishonouredChequesL3-12M']): ch.unfixed_returned_cheques_count_between_last_3_to_12_months = scn_dict[ 'DishonouredChequesL3-12M'] if is_not_none(scn_dict['DishonouredChequesA12M']): ch.unfixed_returned_cheques_count_of_more_12_months = scn_dict[ 'DishonouredChequesA12M'] if is_not_none(scn_dict['AllDishonouredCheques']): ch.unfixed_returned_cheques_count_of_last_5_years = scn_dict[ 'AllDishonouredCheques'] if is_not_none(scn_dict['DCAmountRatio']): ch.unfixed_returned_cheques_total_balance = round( float(scn_dict['DCAmountRatio']) * ALL_USERS_AVERAGE_UNFIXED_RETURNED_CHEQUES_AMOUNT) recent_ch = ds.get_user_cheque(user_id) cheque_score = cs.calculate_user_cheques_score(revised_p=revised_p, recent_ch=recent_ch, revised_ch=ch) ds.insert_or_update_cheque(ch, update_flag=recent_ch.user_id is not None) ds.insert_or_update_profile(revised_p, update_flag=recent_p.user_id is not None)
def calculate_user_undone_trades_score_2( self, user_id: long, reset_cache=False, undone_trade_object: UndoneTrade = None, done_trade_object: DoneTrade = None): if undone_trade_object is not None: undone_trade = undone_trade_object else: undone_trade: UndoneTrade = UndoneTrade.objects( user_id=user_id).first() if done_trade_object is not None: done_trade = done_trade_object else: done_trade: DoneTrade = DoneTrade.objects(user_id=user_id).first() rds: RedisCachingRulesUndoneTrades = self.rds.get_redis_caching_rules_undone_trades_service( reset_cache) undone_trades_score = 0 normalized_undone_trades_score = 0 score = rds.get_score_of_rules_undone_undue_trades_counts_h10( undone_trade.undue_trades_count) # normalized_score = calculate_normalized_score(H10_RULES_UNDONE_UNDUE_TRADES_COUNTS, score) # normalized_undone_trades_score += normalized_score undone_trades_score += score self.scores_dict[HISTORIES_SCORE] = self.scores_dict.get( HISTORIES_SCORE) + score print('score= {}, undoneTrades:[undue_trades_count-h10]= {}'.format( score, undone_trade.undue_trades_count)) # calculate undue_total_balance_ratio undue_total_balance_ratio = float( undone_trade.undue_trades_total_balance_of_last_year / done_trade.trades_total_balance) score = rds.get_score_of_rules_undone_undue_trades_total_balance_of_last_year_ratios_v15( undue_total_balance_ratio) # normalized_score = calculate_normalized_score(V15_RULES_UNDONE_UNDUE_TRADES_TOTAL_BALANCE_OF_LAST_YEAR_RATIOS, score) # normalized_undone_trades_score += normalized_score undone_trades_score += score self.scores_dict[VOLUMES_SCORE] = self.scores_dict.get( VOLUMES_SCORE) + score print('score= {}, undoneTrades:[undue_total_balance_ratio-v15]= {}'. format(score, undue_total_balance_ratio)) score = rds.get_score_of_rules_undone_past_due_trades_counts_t26( undone_trade.past_due_trades_count) # normalized_score = calculate_normalized_score(T26_RULES_UNDONE_PAST_DUE_TRADES_COUNTS, score) # normalized_undone_trades_score += normalized_score undone_trades_score += score self.scores_dict[TIMELINESS_SCORE] = self.scores_dict.get( TIMELINESS_SCORE) + score print('score= {}, undoneTrades:[past_due_trades_count-t26]= {}'.format( score, undone_trade.past_due_trades_count)) timely_done_trades_of_last_year = ( done_trade.timely_trades_count_of_last_3_months + done_trade.timely_trades_count_between_last_3_to_12_months) # calculate past_due_total_balance_ratio past_due_total_balance_ratio = float( undone_trade.past_due_trades_total_balance_of_last_year / done_trade.trades_total_balance) score = rds.get_score_of_rules_undone_past_due_trades_total_balance_of_last_year_ratios_v13( past_due_total_balance_ratio) score_code = rds.get_code_of_rules_undone_past_due_trades_total_balance_of_last_year_ratios_v13( past_due_total_balance_ratio) if timely_done_trades_of_last_year == 1 and score_code == SCORE_CODE_RULES_UNDONE_PAST_DUE_TRADES_TOTAL_BALANCE_OF_LAST_YEAR_RATIOS: score *= 2 # normalized_score = calculate_normalized_score(V13_RULES_UNDONE_PAST_DUE_TRADES_TOTAL_BALANCE_OF_LAST_YEAR_RATIOS, score) # normalized_undone_trades_score += normalized_score undone_trades_score += score self.scores_dict[VOLUMES_SCORE] = self.scores_dict.get( VOLUMES_SCORE) + score print('score= {}, undoneTrades:[past_due_total_balance_ratio-v13]= {}'. format(score, past_due_total_balance_ratio)) score = rds.get_score_of_rules_undone_arrear_trades_counts_t27( undone_trade.arrear_trades_count) # normalized_score = calculate_normalized_score(T27_RULES_UNDONE_ARREAR_TRADES_COUNTS, score) # normalized_undone_trades_score += normalized_score undone_trades_score += score self.scores_dict[TIMELINESS_SCORE] = self.scores_dict.get( TIMELINESS_SCORE) + score print('score= {}, undoneTrades:[arrear_trades_count-t27]= {}'.format( score, undone_trade.arrear_trades_count)) # calculate arrear_total_balance_ratio arrear_total_balance_ratio = float( undone_trade.arrear_trades_total_balance_of_last_year / done_trade.trades_total_balance) score = rds.get_score_of_rules_undone_arrear_trades_total_balance_of_last_year_ratios_v14( arrear_total_balance_ratio) score_code = rds.get_code_of_rules_undone_arrear_trades_total_balance_of_last_year_ratios_v14( arrear_total_balance_ratio) if timely_done_trades_of_last_year == 1 and score_code == SCORE_CODE_RULES_UNDONE_ARREAR_TRADES_TOTAL_BALANCE_OF_LAST_YEAR_RATIOS: score *= 2 # normalized_score = calculate_normalized_score(V14_RULES_UNDONE_ARREAR_TRADES_TOTAL_BALANCE_OF_LAST_YEAR_RATIOS, score) # normalized_undone_trades_score += normalized_score undone_trades_score += score self.scores_dict[VOLUMES_SCORE] = self.scores_dict.get( VOLUMES_SCORE) + score print('score= {}, undoneTrades:[arrear_total_balance_ratio-v14]= {}'. format(score, arrear_total_balance_ratio)) # print('............. undoneTrades_score = {} , normalized_score = {} ................\n'.format(undone_trades_score, # normalized_undone_trades_score)) print('............. undoneTrades_score = {} ................'.format( undone_trades_score)) print( '... IDENTITIES_SCORE= {} , HISTORIES_SCORE= {}, VOLUMES_SCORE= {}, TIMELINESS_SCORE= {} \n' .format(self.scores_dict.get(IDENTITIES_SCORE), self.scores_dict.get(HISTORIES_SCORE), self.scores_dict.get(VOLUMES_SCORE), self.scores_dict.get(TIMELINESS_SCORE))) return undone_trades_score