def view_hello(request): """gRPC: Call from UI as a hearbeat""" user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) # -------------------------------- # Behavioral logging # -------------------------------- log_data = dict(session_key=get_session_key(request), feature_id=ta2_static.HELLO, activity_l1=bl_static.L1_SYSTEM_ACTIVITY, activity_l2=bl_static.L2_APP_LAUNCH) LogEntryMaker.create_ta2ta3_entry(user_info.result_obj, log_data) # note: this is just a heartbeat, so no params are sent # # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='Hello', request_msg=('no params for this call')) # Let's call the TA2! # resp_info = ta2_hello() if not resp_info.success: return JsonResponse(get_json_error(resp_info.err_msg)) json_str = resp_info.result_obj # Convert JSON str to python dict - err catch here # - let it blow up for now--should always return JSON json_format_info = json_loads(json_str) if not json_format_info.success: return JsonResponse(get_json_error(json_format_info.err_msg)) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_format_info.result_obj) json_info = get_json_success('success!', data=json_format_info.result_obj) return JsonResponse(json_info, safe=False)
def view_list_primitives(request): """gRPC: Call from UI with a ListPrimitivesRequest""" user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) # -------------------------------- # (2) Begin to log D3M call # -------------------------------- call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='ListPrimitives', request_msg='no params for this call') # -------------------------------- # (2a) Behavioral logging # -------------------------------- log_data = dict(session_key=get_session_key(request), feature_id=ta2_static.LIST_PRIMITIVES, activity_l1=bl_static.L1_SYSTEM_ACTIVITY, activity_l2=bl_static.L2_ACTIVITY_BLANK) LogEntryMaker.create_ta2ta3_entry(user_info.result_obj, log_data) # Let's call the TA2! # search_info = list_primitives() #print('search_info', search_info) if not search_info.success: return JsonResponse(get_json_error(search_info.err_msg)) # Convert JSON str to python dict - err catch here # json_format_info = json_loads(search_info.result_obj) if not json_format_info.success: return JsonResponse(get_json_error(json_format_info.err_msg)) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_format_info.result_obj) json_info = get_json_success('success!', data=json_format_info.result_obj) return JsonResponse(json_info, safe=False)
def view_end_search_solutions(request): """gRPC: Call from UI with a EndSearchSolutionsRequest""" print('view_end_search_solutions 1') user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) user = user_info.result_obj print('view_end_search_solutions 2') req_body_info = get_request_body(request) if not req_body_info.success: return JsonResponse(get_json_error(req_body_info.err_msg)) print('view_end_search_solutions 3') # -------------------------------- # Behavioral logging # -------------------------------- log_data = dict(session_key=get_session_key(request), feature_id=ta2_static.END_SEARCH_SOLUTIONS, activity_l1=bl_static.L1_SYSTEM_ACTIVITY, activity_l2=bl_static.L2_ACTIVITY_BLANK) LogEntryMaker.create_ta2ta3_entry(user, log_data) print('view_end_search_solutions 4') # Let's call the TA2 and end the session! # params = dict(user=user) search_info = end_search_solutions(req_body_info.result_obj, **params) if not search_info.success: return JsonResponse(get_json_error(search_info.err_msg)) # The session is over, write the log entries files # #LogEntryMaker.write_user_log_from_request(request) # User is done at this point! # Write out the log and delete it.... user_workspace = None ws_info = get_latest_user_workspace(request) if ws_info.success: user_workspace = ws_info.result_obj ResetUtil.write_and_clear_behavioral_logs(user, user_workspace) json_info = get_json_success('success!', data=search_info.result_obj) return JsonResponse(json_info, safe=False)
def view_stop_search_solutions(request): """gRPC: Call from UI with a StopSearchSolutions""" user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) req_body_info = get_request_body(request) if not req_body_info.success: return JsonResponse(get_json_error(req_body_info.err_msg)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type=ta2_static.STOP_SEARCH_SOLUTIONS, request_msg=req_body_info.result_obj) # -------------------------------- # Behavioral logging # -------------------------------- log_data = dict(session_key=get_session_key(request), feature_id=ta2_static.STOP_SEARCH_SOLUTIONS, activity_l1=bl_static.L1_SYSTEM_ACTIVITY, activity_l2=bl_static.L2_ACTIVITY_BLANK) LogEntryMaker.create_ta2ta3_entry(user_info.result_obj, log_data) # Let's call the TA2! # search_info = stop_search_solutions(req_body_info.result_obj) #print('search_info', search_info) if not search_info.success: return JsonResponse(get_json_error(search_info.err_msg)) # Convert JSON str to python dict - err catch here # - let it blow up for now--should always return JSON json_dict = json.loads(search_info.result_obj, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) json_info = get_json_success('success!', data=json_dict) return JsonResponse(json_info, safe=False)
def run_describe_solution(self, pipeline_id, solution_id, msg_cnt=-1): """sync: Run a DescribeSolution call for each solution_id""" print(f'run_describe_solution 1. pipeline_id: {pipeline_id}') # ---------------------------------- # Create the input # ---------------------------------- req_params = {ta2_static.KEY_SOLUTION_ID: solution_id} json_str_info = json_dumps(req_params) if not json_str_info.success: self.add_err_msg(json_str_info.err_msg) return json_str_input = json_str_info.result_obj # -------------------------------- # (2) Save request # -------------------------------- stored_request = StoredRequest(\ user=self.user_object, search_id=self.search_id, pipeline_id=pipeline_id, workspace='(not specified)', request_type=ta2_static.DESCRIBE_SOLUTION, is_finished=False, request=req_params) stored_request.save() # -------------------------------- # (2a) Behavioral logging # -------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static.DESCRIBE_SOLUTION, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SUMMARIZATION, other=req_params) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) print( f'run_describe_solution 2. stored_request.pipeline_id: {stored_request.pipeline_id}' ) # ---------------------------------- # Run Describe Solution # ---------------------------------- describe_info = describe_solution(json_str_input) if not describe_info.success: self.add_err_msg(describe_info.err_msg) StoredResponse.add_err_response(\ stored_request, describe_info.err_msg) return # ---------------------------------- # Parse the DescribeSolutionResponse # ---------------------------------- describe_data_info = json_loads(describe_info.result_obj) if not describe_data_info.success: self.add_err_msg(describe_data_info.err_msg) StoredResponse.add_err_response(\ stored_request, describe_data_info.err_msg) return # ----------------------------------------------- # Add the pipline id to the result # ----------------------------------------------- describe_data = describe_data_info.result_obj describe_data[ta2_static.KEY_PIPELINE_ID] = pipeline_id describe_data[ta2_static.KEY_SEARCH_ID] = self.search_id describe_data[ta2_static.KEY_SOLUTION_ID] = solution_id describe_data.move_to_end(ta2_static.KEY_PIPELINE_ID, last=False) # params = dict() # if not stored_request.pipeline_id: # params['pipeline_id'] = describe_data[KEY_PIPELINE_ID] stored_info = StoredResponse.add_success_response(\ stored_request, describe_data, pipeline_id=pipeline_id) if not stored_info.success: print('stored info fail!', stored_info.err_msg) print( f'run_describe_solution 3. stored_info.result_obj.pipeline_id: {stored_info.result_obj.pipeline_id}' ) print( f'run_describe_solution 4. stored_request.pipeline_id: {stored_request.pipeline_id}' ) # ----------------------------------------- # Tracking this in the behavioral log, # e.g. checking time lapse between creation # of solution and if user investigates this model, # later, if at all # ----------------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static.DESCRIBE_SOLUTION_RESPONSE, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SEARCH, other=describe_data) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) # ----------------------------------------------- # send responses back to WebSocket # --------------------------------------------- ws_msg = WebsocketMessage.get_success_message(\ 'DescribeSolution', 'it worked', msg_cnt=msg_cnt, data=describe_data) print('ws_msg: %s' % ws_msg) #print('ws_msg', ws_msg.as_dict()) ws_msg.send_message(self.websocket_id)
def run_get_search_solution_results(self): """Run SearchSolutions against a TA2""" # ----------------------------------- # (1) make GRPC request object # ----------------------------------- params_dict = dict(searchId=self.search_id) params_info = json_dumps(params_dict) if not params_info.success: self.send_websocket_err_msg(\ ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, params_info.err_msg) return try: grpc_req = Parse(params_info.result_obj, core_pb2.GetSearchSolutionsResultsRequest()) except ParseError as err_obj: err_msg = ('GetSearchSolutionsResultsRequest: Failed to' ' convert JSON to gRPC: %s') % (err_obj) self.send_websocket_err_msg(\ ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, params_info.err_msg) return # -------------------------------- # (2) Save the request to the db # -------------------------------- stored_request = StoredRequest(\ user=self.user_object, search_id=self.search_id, workspace='(not specified)', request_type=ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, is_finished=False, request=params_dict) stored_request.save() # -------------------------------- # (2a) Behavioral logging # -------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SEARCH, other=params_dict) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) # -------------------------------- # (3) Make the gRPC request # -------------------------------- core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return err_resp(err_msg) msg_cnt = 0 try: # ----------------------------------------- # Iterate through the streaming responses # Note: The StoredResponse.id becomes the pipeline id # ----------------------------------------- for reply in core_stub.GetSearchSolutionsResults(\ grpc_req, timeout=settings.TA2_GRPC_LONG_TIMEOUT): msg_cnt += 1 # ----------------------------------------------- # Parse the response into JSON + store response # ----------------------------------------------- msg_json_str = message_to_json(reply) msg_json_info = json_loads(msg_json_str) if not msg_json_info.success: user_msg = 'Failed to convert response to JSON: %s' % \ msg_json_info.err_msg self.send_websocket_err_msg(\ ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, user_msg) StoredResponse.add_stream_err_response(\ stored_response, user_msg) # Wait for next response.... continue result_json = msg_json_info.result_obj # TA2s (specifically NYU) responds once when trying a new pipeline, with a message missing a solutionId # the same process responds again once the solution contains a solutionId print('results json from TA2') print(result_json) if not result_json.get('solutionId'): continue if ta2_static.KEY_SOLUTION_ID not in result_json: user_msg = '"%s" not found in response to JSON: %s' % \ (ta2_static.KEY_SOLUTION_ID, result_json) StoredResponse.add_stream_err_response(\ stored_response, user_msg) self.send_websocket_err_msg(\ ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, user_msg) # Wait for next response.... continue # Solution id used for DescribeSolution... # solution_id = result_json[ta2_static.KEY_SOLUTION_ID] # ----------------------------------------- # Looks good, save the response # ----------------------------------------- stored_resp_info = StoredResponse.add_stream_success_response(\ stored_request, result_json) # ----------------------------------------- # Tracking this in the behavioral log, # e.g. checking time lapse between creation # of solution and if user investigates this model, # later, if at all # ----------------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static. GET_SEARCH_SOLUTIONS_RESULTS_RESPONSE, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SEARCH, other=result_json) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) # ----------------------------------------- # Make sure the response was saved (probably won't happen) # ----------------------------------------- if not stored_resp_info.success: # Not good but probably won't happen # send a message to the user... # user_msg = 'Failed to store response from %s: %s' % \ (ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, msg_json_info.err_msg) StoredResponse.add_stream_err_response(\ stored_response, user_msg) self.send_websocket_err_msg(\ ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, user_msg) # Wait for the next response... continue # --------------------------------------------- # Looks good! Get the StoredResponse # - This id will be used as the pipeline id # --------------------------------------------- stored_response = stored_resp_info.result_obj stored_response.use_id_as_pipeline_id() StoredResponse.add_stream_success_response(\ stored_response, stored_response) # ----------------------------------------------- # send responses back to WebSocket # --------------------------------------------- ws_msg = WebsocketMessage.get_success_message(\ ta2_static.GET_SEARCH_SOLUTIONS_RESULTS, 'it worked', msg_cnt=msg_cnt, data=stored_response.as_dict()) print('ws_msg: %s' % ws_msg) #print('ws_msg', ws_msg.as_dict()) ws_msg.send_message(self.websocket_id) stored_response.mark_as_sent_to_user() print('msg received #%d' % msg_cnt) # ----------------------------------------------- # continue the process describe/score/etc # ----------------------------------------------- # DescribeSolution - run sync # self.run_describe_solution(stored_response.pipeline_id, solution_id, msg_cnt) # FitSolution - run async # print('PRE run_fit_solution') self.run_fit_solution(stored_response.pipeline_id, solution_id) print('POST run_fit_solution') print('PRE run_score_solution') self.run_score_solution(stored_response.pipeline_id, solution_id) print('POST run_score_solution') # ----------------------------------------------- # All results arrived, send message to UI # ----------------------------------------------- ws_msg = WebsocketMessage.get_success_message( \ ta2_static.ENDGetSearchSolutionsResults, {'searchId': self.search_id, 'message': 'it worked'}) print('ws_msg: %s' % ws_msg) ws_msg.send_message(self.websocket_id) except grpc.RpcError as err_obj: stored_request.set_error_status(str(err_obj)) return except Exception as err_obj: stored_request.set_error_status(str(err_obj)) return StoredRequestUtil.set_finished_ok_status(stored_request.id)
def make_search_solutions_call(all_params, websocket_id, user_id, **kwargs): """Return the result of a SearchSolutions call. If successful, an async process is kicked off""" if not websocket_id: return err_resp('websocket_id must be set') print('make_search_solutions_call 1') param_check = SearchSolutionsHelper.check_params(all_params) if not param_check.success: return param_check print('make_search_solutions_call 2') try: user_obj = User.objects.get(pk=user_id) except User.DoesNotExist: user_msg = 'No user found for id: %s' % user_id return err_resp(user_msg) search_solution_params = all_params[ ta2_static.KEY_SEARCH_SOLUTION_PARAMS] # -------------------------------- # (2) Logging # -------------------------------- stored_request = StoredRequest(\ user=user_obj, # search_id=self.search_id, workspace='(not specified)', request_type=ta2_static.SEARCH_SOLUTIONS, is_finished=False, request=search_solution_params) stored_request.save() # -------------------------------- # (2a) Behavioral logging # -------------------------------- session_key = kwargs.get(SESSION_KEY, None) log_data = dict(session_key=session_key, feature_id=ta2_static.SEARCH_SOLUTIONS, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SEARCH, other=search_solution_params) LogEntryMaker.create_ta2ta3_entry(user_obj, log_data) # 11/6/2019 - late night hack, these variables shouldn't be here # - introduced somewhere in the .js when setting a problem # search_solution_params.pop('id', None) search_solution_params.pop('session_key', None) # Run SearchSolutions against the TA2 # search_info = search_solutions(search_solution_params) if not search_info.success: StoredResponse.add_err_response(stored_request, search_info.err_msg) return search_info print('make_search_solutions_call 2') search_info_json = json_loads(search_info.result_obj) if not search_info_json.success: StoredResponse.add_err_response(stored_request, search_info_json.err_msg) return search_info_json search_info_data = search_info_json.result_obj print('search_info_data', json_dumps(search_info_data)[1]) print('make_search_solutions_call 3') if not ta2_static.KEY_SEARCH_ID in search_info_data: user_msg = 'searchId not found in the SearchSolutionsResponse' StoredResponse.add_err_response(stored_request, user_msg) return err_resp(user_msg) search_id = search_info_data['searchId'] StoredResponse.add_success_response(stored_request, search_info_data, search_id=search_id) # Async task to run GetSearchSolutionsResults # extra_params = {SESSION_KEY: session_key} SearchSolutionsHelper.kick_off_solution_results.delay(\ search_id, websocket_id, user_id, all_search_params=all_params, **extra_params) # Back to the UI, looking good # return ok_resp(search_info_data)
def run_get_score_solution_responses(self, request_id): """(2) Run GetScoreSolutionResults""" if self.has_error(): return if not request_id: self.send_websocket_err_msg(ta2_static.GET_SCORE_SOLUTION_RESULTS, 'request_id must be set') return # ----------------------------------- # (1) make GRPC request object # ----------------------------------- params_dict = {ta2_static.KEY_REQUEST_ID: request_id} params_info = json_dumps(params_dict) try: grpc_req = Parse(params_info.result_obj, core_pb2.GetScoreSolutionResultsRequest()) except ParseError as err_obj: err_msg = ('Failed to convert JSON to gRPC: %s') % (err_obj) self.send_websocket_err_msg(ta2_static.GET_SCORE_SOLUTION_RESULTS, err_msg) return # -------------------------------- # (2) Save the request to the db # -------------------------------- stored_request = StoredRequest(\ user=self.user_object, request_type=ta2_static.GET_SCORE_SOLUTION_RESULTS, search_id=self.search_id, pipeline_id=self.pipeline_id, is_finished=False, request=params_dict) stored_request.save() # -------------------------------- # (2a) Behavioral logging # -------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static.GET_SCORE_SOLUTION_RESULTS, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SUMMARIZATION, other=params_dict) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) # -------------------------------- # (3) Make the gRPC request # -------------------------------- core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return err_resp(err_msg) msg_cnt = 0 try: # ----------------------------------------- # Iterate through the streaming responses # Note: The StoredResponse.id becomes the pipeline id # ----------------------------------------- for reply in core_stub.GetScoreSolutionResults(\ grpc_req, timeout=settings.TA2_GRPC_LONG_TIMEOUT): msg_cnt += 1 stored_response = None # to hold a StoredResponse object # ----------------------------------------------- # Parse the response into JSON + store response # ----------------------------------------------- msg_json_str = message_to_json(reply) msg_json_info = json_loads(msg_json_str) if not msg_json_info.success: err_msg = ('Failed to convert JSON to gRPC: %s') % \ (err_obj,) StoredResponse.add_stream_err_response( stored_request, user_msg) self.send_websocket_err_msg(\ ta2_static.GET_SCORE_SOLUTION_RESULTS, err_msg) # Wait for next response.... continue result_json = msg_json_info.result_obj # ----------------------------------------- # Looks good, save the response # ----------------------------------------- stored_resp_info = StoredResponse.add_stream_success_response(\ stored_request, result_json) # ----------------------------------------- # Make sure the response was saved (probably won't happen) # ----------------------------------------- if not stored_resp_info.success: # Not good but probably won't happen # send a message to the user... # self.send_websocket_err_msg(\ ta2_static.GET_SCORE_SOLUTION_RESULTS, stored_resp_info.err_msg) # StoredResponse.add_stream_err_response(\ stored_request, stored_resp_info.err_msg) # continue # --------------------------------------------- # Looks good! Get the StoredResponse # - send responses back to WebSocket # --------------------------------------------- stored_response = stored_resp_info.result_obj stored_response.set_pipeline_id(self.pipeline_id) # --------------------------------------------- # If progress is complete, # send response back to WebSocket # --------------------------------------------- progress_val = get_dict_value(\ result_json, [ta2_static.KEY_PROGRESS, ta2_static.KEY_PROGRESS_STATE]) if (not progress_val.success) or \ (progress_val.result_obj != ta2_static.KEY_PROGRESS_COMPLETED): user_msg = 'GetScoreSolutionResultsResponse is not yet complete' LOGGER.info(user_msg) # wait for next message... continue ws_msg = WebsocketMessage.get_success_message(\ ta2_static.GET_SCORE_SOLUTION_RESULTS, 'it worked', msg_cnt=msg_cnt, data=stored_response.as_dict()) LOGGER.info('ws_msg: %s' % ws_msg) #print('ws_msg', ws_msg.as_dict()) ws_msg.send_message(self.websocket_id) # stored_response.mark_as_sent_to_user() except grpc.RpcError as err_obj: stored_request.set_error_status(str(err_obj)) return except Exception as err_obj: stored_request.set_error_status(str(err_obj)) return StoredRequestUtil.set_finished_ok_status(stored_request.id)
def run_process(self): """(1) Run ScoreSolution""" if self.has_error(): return # ---------------------------------- # Create the input # ---------------------------------- LOGGER.info('ScoreSolutionHelper.run_process 2') json_str_info = json_dumps(self.score_params) if not json_str_info.success: self.add_err_msg(json_str_info.err_msg) return json_str_input = json_str_info.result_obj # ---------------------------------- # (2) Save the request # ---------------------------------- stored_request = StoredRequest(\ user=self.user_object, search_id=self.search_id, pipeline_id=self.pipeline_id, workspace='(not specified)', request_type=ta2_static.SCORE_SOLUTION, is_finished=False, request=self.score_params) stored_request.save() # -------------------------------- # (2a) Behavioral logging # -------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static.SCORE_SOLUTION, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_SUMMARIZATION, other=self.score_params) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) # ---------------------------------- # Run ScoreSolution # ---------------------------------- LOGGER.info('run ScoreSolution: %s', json_str_input) fit_info = score_solution(json_str_input) if not fit_info.success: print('ScoreSolution err_msg: ', fit_info.err_msg) StoredResponse.add_err_response(stored_request, fit_info.err_msg) self.send_websocket_err_msg(ta2_static.SCORE_SOLUTION, fit_info.err_msg) return # ---------------------------------- # Parse the ScoreSolutionResponse # ---------------------------------- response_info = json_loads(fit_info.result_obj) if not response_info.success: print('ScoreSolution grpc err_msg: ', response_info.err_msg) StoredResponse.add_err_response(stored_request, response_info.err_msg) self.send_websocket_err_msg(ta2_static.SCORE_SOLUTION, response_info.err_msg) return result_json = response_info.result_obj # ---------------------------------- # Get the requestId # ---------------------------------- if not ta2_static.KEY_REQUEST_ID in result_json: user_msg = (' "%s" not found in response to JSON: %s') % \ (ta2_static.KEY_REQUEST_ID, result_json) StoredResponse.add_err_response(stored_request, user_msg) self.send_websocket_err_msg(ta2_static.SCORE_SOLUTION, user_msg) return StoredResponse.add_success_response(stored_request, result_json) self.run_get_score_solution_responses( result_json[ta2_static.KEY_REQUEST_ID])
def solution_export3(user, raven_json, **kwargs): """ Send a SolutionExportRequest to the SolutionExport command """ if not isinstance(user, User): err_msg = '"user" must be a User object' return err_resp(err_msg) if not isinstance(raven_json, dict): err_msg = 'raven_dict must be a python dict' return err_resp(err_msg) if not ta2_static.KEY_SEARCH_ID in raven_json: err_msg = (f'Key: "{ta2_static.KEY_SEARCH_ID}" not found in the' f' "raven_json" dict. (solution_export3)') return err_resp(err_msg) search_id = raven_json.pop( ta2_static.KEY_SEARCH_ID) # not needed for GRPC call session_key = kwargs.get(SESSION_KEY, '') # -------------------------------- # Convert dict to string # -------------------------------- raven_json_info = json_dumps(raven_json) if not raven_json_info.success: return err_resp(raven_json_info.err_msg) raven_json_str = raven_json_info.result_obj # -------------------------------- # convert the JSON string to a gRPC request # -------------------------------- try: req = Parse(raven_json_str, core_pb2.SolutionExportRequest()) except ParseError as err_obj: err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj) return err_resp(err_msg) # In test mode, return canned response # if settings.TA2_STATIC_TEST_MODE: resp = core_pb2.SolutionExportResponse() return ok_resp(message_to_json(resp)) core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return err_resp(err_msg) # -------------------------------- # Save the request to the db # -------------------------------- stored_request = StoredRequest(\ user=user, search_id=search_id, workspace='(not specified)', request_type=ta2_static.SOLUTION_EXPORT, is_finished=False, request=raven_json) stored_request.save() # -------------------------------- # Behavioral logging # -------------------------------- log_data = dict(session_key=session_key, feature_id=ta2_static.SOLUTION_EXPORT, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_EXPORT, other=raven_json) LogEntryMaker.create_ta2ta3_entry(user, log_data) # -------------------------------- # Send the gRPC request # -------------------------------- try: reply = core_stub.SolutionExport(\ req, timeout=settings.TA2_GRPC_SHORT_TIMEOUT) except Exception as err_obj: user_msg = f'Error: {err_obj}' StoredResponse.add_err_response(stored_request, user_msg) return err_resp(user_msg) # -------------------------------- # Convert the reply to JSON and send it back # -------------------------------- resp_json_str = message_to_json(reply) resp_json_dict_info = json_loads(resp_json_str) if not resp_json_dict_info.success: user_msg = (f'Failed to convert GRPC response to JSON:' f' {resp_json_dict_info.err_msg}') StoredResponse.add_err_response(stored_request, user_msg) return err_resp(user_msg) StoredResponse.add_success_response(stored_request, resp_json_dict_info.result_obj) return ok_resp(resp_json_str)
def run_process(self): """(1) Run ProduceSolution""" if self.has_error(): return # ---------------------------------- # Create the input # ---------------------------------- json_str_info = json_dumps(self.produce_params) if not json_str_info.success: self.add_err_msg(json_str_info.err_msg) return json_str_input = json_str_info.result_obj # -------------------------------- # (2) Save the request to the db # -------------------------------- req_type = ta2_static.PRODUCE_SOLUTION stored_request = StoredRequest(\ user=self.user_object, request_type=req_type, pipeline_id=self.pipeline_id, search_id=self.search_id, is_finished=False, request=self.produce_params) stored_request.save() # -------------------------------- # (2a) Behavioral logging # -------------------------------- log_data = dict(session_key=self.session_key, feature_id=ta2_static.PRODUCE_SOLUTION, activity_l1=bl_static.L1_MODEL_SELECTION, activity_l2=bl_static.L2_MODEL_EXPLANATION, other=self.produce_params) LogEntryMaker.create_ta2ta3_entry(self.user_object, log_data) # ---------------------------------- # Run FitSolution # ---------------------------------- produce_info = produce_solution(json_str_input) if not produce_info.success: StoredResponse.add_err_response(stored_request, produce_info.err_msg) self.send_websocket_err_msg(ta2_static.PRODUCE_SOLUTION, produce_info.err_msg) return # ---------------------------------- # Parse the ProduceSolutionResponse # ---------------------------------- response_info = json_loads(produce_info.result_obj) if not response_info.success: StoredResponse.add_err_response(stored_request, response_info.err_msg) self.send_websocket_err_msg(ta2_static.PRODUCE_SOLUTION, response_info.err_msg) return result_json = response_info.result_obj # ---------------------------------- # Get the requestId # ---------------------------------- if not ta2_static.KEY_REQUEST_ID in result_json: user_msg = (' "%s" not found in response to JSON: %s') % \ (ta2_static.KEY_REQUEST_ID, result_json) # StoredResponse.add_err_response(stored_request, user_msg) # self.send_websocket_err_msg(ta2_static.PRODUCE_SOLUTION, user_msg) return # Store success response # StoredResponse.add_success_response(stored_request, result_json) print('produce 3') self.run_get_produce_solution_responses(result_json[ta2_static.KEY_REQUEST_ID])