def retrieve(): output = {} #retrieve the json from the ajax call json_file = '' if request.method == 'POST': json_file = request.json print("post request") #if json_file successfully posted.. if json_file != '': # check all required arguments are present: if not all(arg in json_file for arg in ["customerId"]): print("Missing arguments in post request") return json.dumps({"error": "Missing arguments"}) inputCustomerId = json_file["customerId"] print("retreived data: " + str(inputCustomerId)) data_array = [] client_info_obj = {} client_attrition_score_obj = {} client_examine_segement_obj = {} client_profile_all = client.retrieve_entire_client_profile(inputCustomerId) client_profile = client_profile_all[0] if ("error" in client_profile): return json.dumps({"error": client_profile["error"]}) #profile = json.dumps(client_profile, indent=4, sort_keys=True) #print(profile) #get client info, returns a list with first element contatining client info client_info_obj = client_profile["customer"] if ("error" in client_info_obj): return json.dumps({"error": client_info_obj["error"]}) #get client life events client_life_events = client_profile["event_scores"] if (len(client_life_events) > 0): if ("error" in client_life_events[0]): return json.dumps({"error": client_life_events["error"]}) #get client_attrition_score, returns a list with first element contatining info client_attrition_scores = client_profile["scores"] if (len(client_attrition_scores) > 0): if ("error" in client_attrition_scores[0]): return json.dumps({"error": client_attrition_scores[0]["error"]}) for i in range(len(client_attrition_scores)): if (client_attrition_scores[i]["score_code"] == "ATTRITION" and client_attrition_scores[i]["model_scope_forecast_horizon"] == 1): client_attrition_score_obj = client_attrition_scores[i] if (client_attrition_scores[i]["score_code"] == "DYNAMIC_SEGMENTATION"): client_examine_segement_obj = client_attrition_scores[i] #get segment description, returns a list segment_description = client.segment_description() if (len(segment_description) > 0): if ("error" in segment_description[0]): return json.dumps({"error": segment_description[0]["error"]}) #create the output json output = { "clientInfo": client_info_obj, "clientAttritionScore": client_attrition_score_obj, "clientLifeEvents": client_life_events, "clientExamineSegment": client_examine_segement_obj, "segmentDescription": segment_description, "customerId": inputCustomerId, "lifeEventsDescription": life_events_desc, "featuresDescription": features_desc } #return output json return json.dumps(output)
def retrieve(): output = {} #retrieve the json from the ajax call json_file = '' if request.method == 'POST': json_file = request.json print("post request") #if json_file successfully posted.. if json_file != '': # check all required arguments are present: if not all(arg in json_file for arg in ["customerId"]): print("Missing arguments in post request") return json.dumps({"error": "Missing arguments"}) inputCustomerId = json_file["customerId"] print("retreived data: " + str(inputCustomerId)) data_array = [] #get client info, returns a list with first element contatining client info client_info = client.retrieve_basic_client_info(inputCustomerId) if (len(client_info) > 0): client_info_obj = client_info[0] if ("error" in client_info_obj): return json.dumps({"error": client_info_obj["error"]}) #get client_attrition_score, returns a list with first element contatining info client_attrition_score = client.retrieve_client_attrition_score( inputCustomerId) if (len(client_attrition_score) > 0): client_attrition_score_obj = client_attrition_score[0] if ("error" in client_attrition_score_obj): return json.dumps({"error": client_attrition_score_obj["error"]}) #get client life events client_life_events = client.retrieve_relevant_life_events(inputCustomerId) if (len(client_life_events) > 0): if ("error" in client_life_events[0]): return json.dumps({"error": client_life_events["error"]}) #get client segment, returns a list with first element contatining info client_examine_segement = client.examine_client_segment(inputCustomerId) if (len(client_examine_segement) > 0): client_examine_segement_obj = client_examine_segement[0] if ("error" in client_examine_segement_obj): return json.dumps({"error": client_examine_segement["error"]}) #get segment description, returns a list segment_description = client.segment_description() if (len(segment_description) > 0): if ("error" in segment_description[0]): return json.dumps({"error": segment_description[0]["error"]}) #create the output json output = { "clientInfo": client_info_obj, "clientAttritionScore": client_attrition_score, "clientLifeEvents": client_life_events, "clientExamineSegment": client_examine_segement, "segmentDescription": segment_description, "customerId": inputCustomerId, "lifeEventsDescription": life_events_desc, "attritionFeaturesDescription": attrition_features_desc, "customerSegmentsDescription": customer_segments_desc } #return output json return json.dumps(output)