def start_the_reset(self): """Reset various logs/directories, as appropriate""" if self.has_error(): return # (1) stop any TA2 Searches using the request history # StoredRequestUtil.stop_search_requests(**dict(user=self.user)) # (1a) Stop/Delete any celery tasks # ResetUtil.clear_celery_tasks() # (2) Clear TA2/TA3 output directory # if self.d3m_config: clear_output_directory(self.d3m_config) # (2a) Re-build output directories, if needed # check_build_output_directories(self.d3m_config) # (3) Clear StoredRequest/StoredResponse objects for current user # clear_info = SearchHistoryUtil.clear_grpc_stored_history(self.user) if clear_info.success: print('\n'.join(clear_info.result_obj)) else: print(clear_info.err_msg) # (4) Write out and clear behavioral_logs # ResetUtil.write_and_clear_behavioral_logs(self.user, self.user_workspace) # (5) Clear user workspaces # delete_info = ws_util.delete_user_workspaces(self.user) if not delete_info.success: print(delete_info.err_msg) else: print('workspaces cleared')
def get_score_solutions_results(raven_json_str, user_obj, websocket_id=None): """ Send a GetScoreSolutionResultsRequest to the GetScoreSolutionResults command Expected input from raven_json_str: {requestId: scoreId, pipelineId: response1.id} """ if user_obj is None: return err_resp("The user_obj cannot be None") if not raven_json_str: err_msg = 'No data found for the GetScoreSolutionResultsRequest' return err_resp(err_msg) # -------------------------------- # Make sure it's valid JSON # -------------------------------- raven_json_info = json_loads(raven_json_str) if not raven_json_info.success: return err_resp(raven_json_info.err_msg) # -------------------------------- # convert the JSON string to a gRPC request # Done for error checking; call repeated in celery task # -------------------------------- try: req = Parse(raven_json_str, core_pb2.GetScoreSolutionResultsRequest()) except ParseError as err_obj: err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj) return err_resp(err_msg) # -------------------------------- # Save the request to the db # -------------------------------- stored_request = StoredRequest(\ user=user_obj, workspace='(not specified)', request_type='GetScoreSolutionResults', is_finished=False, request=raven_json_info.result_obj) stored_request.save() # In test mode, return canned response # if settings.TA2_STATIC_TEST_MODE: resp_str = get_grpc_test_json(\ 'test_responses/GetScoreSolutionResultsResponse_ok.json', dict()) resp_info = json_loads(resp_str) if not resp_info.success: return err_resp(resp_info.err_msg) # Save the stored response # StoredResponse.add_response(\ stored_request.id, response=resp_info.result_obj) StoredRequestUtil.set_finished_ok_status(stored_request.id) # Return the stored **request** (not response) # return ok_resp(stored_request.as_dict()) stream_and_store_results.delay(raven_json_str, stored_request.id, 'core_pb2.GetScoreSolutionResultsRequest', 'GetScoreSolutionResults', websocket_id=websocket_id) return ok_resp(stored_request.as_dict())
def stream_and_store_results(raven_json_str, stored_request_id, grpc_req_obj_name, grpc_call_name, **kwargs): """Make the grpc call which has a streaming response grpc_req_obj_name: "core_pb2.GetSearchSolutionsResultsRequest", etc grpc_call_name: "GetSearchSolutionsResults", etc """ core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: StoredRequestUtil.set_error_status(stored_request_id, err_msg) return # optional: used to stream messages back to client via channels # websocket_id = kwargs.get('websocket_id', None) # grpc_req_obj = eval(grpc_req_obj_name) grpc_rpc_call_function = eval('core_stub.%s' % grpc_call_name) # -------------------------------- # convert the JSON string to a gRPC request # Yes: done for the 2nd time # -------------------------------- try: req = Parse(raven_json_str, grpc_req_obj()) except ParseError as err_obj: err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj) StoredRequestUtil.set_error_status(stored_request_id, err_msg) return # -------------------------------- # Send the gRPC request # -------------------------------- msg_cnt = 0 try: # ----------------------------------------- # Iterate through the streaming responses # ----------------------------------------- for reply in grpc_rpc_call_function(\ req, timeout=settings.TA2_GRPC_LONG_TIMEOUT): msg_cnt += 1 stored_resp = None # to hold a StoredResponse object # ----------------------------------------- # parse the response # ----------------------------------------- msg_json_str = message_to_json(reply) msg_json_info = json_loads(msg_json_str) # ----------------------------------------- # does it look ok? # ----------------------------------------- if not msg_json_info.success: print('PROBLEM HERE TO LOG!') user_msg = 'failed to store response: %s' % \ msg_json_info.err_msg ws_msg = WebsocketMessage.get_fail_message(\ grpc_call_name, user_msg, msg_cnt=msg_cnt) ws_msg.send_message(websocket_id) continue # ----------------------------------------- # Looks good, save the response # ----------------------------------------- stored_resp_info = StoredResponse.add_response(\ stored_request_id, response=msg_json_info.result_obj) # ----------------------------------------- # 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: %s' % \ msg_json_info.err_msg ws_msg = WebsocketMessage.get_fail_message(\ grpc_call_name, user_msg, msg_cnt=msg_cnt) ws_msg.send_message(websocket_id) # Wait for the next response... continue # ----------------------------------------------- # send responses back to any open WebSockets # --------------------------------------------- if websocket_id: stored_resp = stored_resp_info.result_obj ws_msg = WebsocketMessage.get_success_message(\ grpc_call_name, 'it worked', msg_cnt=msg_cnt, data=stored_resp.as_dict()) print('ws_msg: %s' % ws_msg) #print('ws_msg', ws_msg.as_dict()) ws_msg.send_message(websocket_id) StoredResponse.mark_as_read(stored_resp) # ----------------------------------------------- print('msg received #%d' % msg_cnt) except grpc.RpcError as err_obj: StoredRequestUtil.set_error_status(\ stored_request_id, str(err_obj)) return #except Exception as err_obj: # StoredRequestUtil.set_error_status(\ # stored_request_id, # str(err_obj)) # return StoredRequestUtil.set_finished_ok_status(stored_request_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 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)