def import_done_trades_data(uid: long): dt = DoneTrade() dt.drop_collection() dt.user_id = uid dt.calculation_start_date = datetime.now() dt.timely_trades_count_of_last_3_months = 1 dt.past_due_trades_count_of_last_3_months = 0 dt.arrear_trades_count_of_last_3_months = 0 dt.timely_trades_count_between_last_3_to_12_months = 0 dt.past_due_trades_count_between_last_3_to_12_months = 0 dt.arrear_trades_count_between_last_3_to_12_months = 0 dt.trades_total_balance = 1000000000.0 dt.total_delay_days = 0 dt.save()
def test_import_done_trades_data(self): DoneTrade.drop_collection() for i in range(1): dt = DoneTrade() dt.user_id = 1 dt.timely_trades_count_of_last_3_months = get_random_digits_str(2) dt.timely_trades_count_between_last_3_to_12_months = get_random_digits_str( 2) dt.past_due_trades_count_of_last_3_months = get_random_digits_str( 2) dt.past_due_trades_count_between_last_3_to_12_months = get_random_digits_str( 2) dt.arrear_trades_count_of_last_3_months = get_random_digits_str(2) dt.arrear_trades_count_between_last_3_to_12_months = get_random_digits_str( 2) dt.trades_total_balance = get_random_digits_str(9) dt.total_delay_days = get_random_digits_str(2) gs.save_document(dt) print("flowing document is saved: ", dt.to_json())
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 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)