def fit_solution(raven_json_str=None): """ Send a FitSolutionRequest to the FitSolution command """ if raven_json_str is None: err_msg = 'No data found for the FitSolutionRequest' 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 # -------------------------------- try: req = Parse(raven_json_str, core_pb2.FitSolutionRequest()) except ParseError as err_obj: err_msg = ('Failed to convert JSON to gRPC: %s' ' (req_search_solutions)' '\nraven_json_str: %s') % \ (err_obj, raven_json_str) print('-' * 40) print(err_msg) return err_resp(err_msg) # In test mode, return canned response # if settings.TA2_STATIC_TEST_MODE: resp = core_pb2.FitSolutionResponse(\ request_id='requestId_%s' % get_alphanumeric_string(6)) return ok_resp(message_to_json(resp)) core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return err_resp(err_msg) # -------------------------------- # Send the gRPC request # -------------------------------- try: reply = core_stub.FitSolution(\ req, timeout=settings.TA2_GRPC_SHORT_TIMEOUT) except Exception as err_obj: return err_resp(str(err_obj)) # -------------------------------- # Convert the reply to JSON and send it back # -------------------------------- return ok_resp(message_to_json(reply))
def write_to_user_problems_root(user_obj, problem_info_string, file_prefix='user_prob'): """Write a JSON string as a new file to the "user_problems_root" directory""" if not problem_info_string: return False, '"problem_info" not specified (UserProblemHelper)' d3m_config_info = get_latest_d3m_user_config(user_obj) if not d3m_config_info.success: return False, 'D3M config not found (UserProblemHelper)' else: d3m_config = d3m_config_info.result_obj user_problems_root = d3m_config.user_problems_root if not isdir(user_problems_root): # directory doesn't exist, try to create it.. try: os.makedirs(user_problems_root, exist_ok=True) except OSError as err_obj: return False,\ 'Could not create user_problems_root: %s' % \ user_problems_root # create a file name based on the d3m config # filepath = None for _ in range(3): # try 3 times, in case name exists # filename includes random element + timestring # fname = '%s_%s_%s.txt' % (\ file_prefix, random_info.get_alphanumeric_string(4), dt.now().strftime('%Y-%m-%d_%H-%M-%S')) filepath = join(d3m_config.user_problems_root, fname) if not isfile(filepath): # great! doesn't exist break # write the file try: open(filepath, 'w').write(problem_info_string) except OSError as err_obj: return False,\ ('Failed to write file to: [%s]' ' (UserProblemHelper): %s') % (filepath, err_obj) except Exception as err_obj: return False,\ ('Failed to write file to: [%s]' ' (UserProblemHelper): %s') % (filepath, err_obj) # return file uri #file_uri = 'file://%s' % filepath return True, filepath
def write_data_for_execute_pipeline(d3m_config, data_info): """Part of the ExecutePipeline, write data to 'temp_storage_root' and return an associated file url""" if not d3m_config: return None, 'No D3MConfiguration specified.' if not data_info: return None, 'No data_info specified.' try: data_str = json.dumps(data_info) except TypeError: return None, 'Failed to convert to data_info to string' if not isdir(d3m_config.temp_storage_root): return None, 'temp_storage_root not accessible: [%s]' % \ d3m_config.temp_storage_root rand_str = random_info.get_alphanumeric_string(4) # create a file name based on # fname = '%s_data_%s_%s.json' % (d3m_config.slug[:6], rand_str, dt.now().strftime('%Y-%m-%d_%H-%M-%S')) filepath = join(d3m_config.temp_storage_root, fname) # write the file try: open(filepath, 'w').write(data_str) except: return None, 'Failed to write file to: [%s]' % filepath # return file uri file_uri = 'file://%s' % filepath return file_uri, None
def search_solutions(raven_json_str=None): """ Send a SearchSolutionsRequest to the SearchSolutions command """ print('raven_json_str', raven_json_str) if raven_json_str is None: err_msg = 'No data found for the SearchSolutionsRequest' return err_resp(err_msg) # This is a dict or OrderedDict, make it a json string # if isinstance(raven_json_str, dict): json_str_info = json_dumps(raven_json_str) if not json_str_info.success: return json_str_info raven_json_str = json_str_info.result_obj else: # Make sure the string is valid JSON # raven_json_info = json_loads(raven_json_str) if not raven_json_info.success: return err_resp(raven_json_info.err_msg) print('SearchSolutionsRequest (string)', raven_json_str) # -------------------------------- # convert the JSON string to a gRPC request # -------------------------------- try: req = Parse(raven_json_str, core_pb2.SearchSolutionsRequest()) except ParseError as err_obj: err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj) return err_resp(err_msg) print('req', req) print('-' * 40) print('raven_json_str', raven_json_str) print('-' * 40) # In test mode, return canned response # if settings.TA2_STATIC_TEST_MODE: search_id = 'search_id_%s' % get_alphanumeric_string(6) resp = core_pb2.SearchSolutionsResponse(search_id=search_id) # print('message_to_json(req)', message_to_json(resp)) return ok_resp(message_to_json(resp)) core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return err_resp(err_msg) # -------------------------------- # Send the gRPC request # -------------------------------- try: reply = core_stub.SearchSolutions(\ req, timeout=settings.TA2_GRPC_SHORT_TIMEOUT) except Exception as err_obj: return err_resp(str(err_obj)) # -------------------------------- # Convert the reply to JSON and send it back # -------------------------------- return ok_resp(message_to_json(reply))
def start_session(raven_json_str=None): """Start session command This command sends a UserAgent and the protocol version to the TA2 service """ if raven_json_str is None: err_msg = 'No data found. Please send a "user_agent"' return get_failed_precondition_sess_response(err_msg) # Default if the user_agent is not from the UI #raven_dict = dict(user_agent=settings.TA2_GPRC_USER_AGENT) # The UI has sent JSON in string format that contains the user_agent try: raven_dict = json.loads(raven_json_str) except json.decoder.JSONDecodeError as err_obj: err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj) return get_failed_precondition_sess_response(err_msg) # check for a user_agent # if not KEY_USER_AGENT_FROM_UI in raven_dict: return get_failed_precondition_sess_response(ERR_MSG_NO_USER_AGENT) # The protocol version always comes from the latest # version we have in the repo (just copied in for now) # raven_dict['version'] = TA2Connection.get_protocol_version() # -------------------------------- # Convert back to string for TA2 call # -------------------------------- content = json.dumps(raven_dict) # -------------------------------- # convert the JSON string to a gRPC request # -------------------------------- try: req = Parse(content, core_pb2.SessionRequest()) except ParseError as err_obj: err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj) return get_failed_precondition_sess_response(err_msg) # In test mode, check if the incoming JSON is legit (in line above) # -- then return canned response # if settings.TA2_STATIC_TEST_MODE: rnd_session_id = random_info.get_alphanumeric_string(7) info_dict = dict(session_id=rnd_session_id, api_version=TA3TA2Util.get_api_version()) return get_grpc_test_json('test_responses/startsession_ok.json', info_dict) #if random.randint(1,10) == 3: # return get_grpc_test_json('test_responses/startsession_badassertion.json') #else: # return get_grpc_test_json('test_responses/startsession_ok.json', d) # -------------------------------- # Get the connection, return an error if there are channel issues # -------------------------------- core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return get_failed_precondition_sess_response(err_msg) #return dict(status=core_pb2.FAILED_PRECONDITION, # details=err_msg) # -------------------------------- # Send the gRPC request # -------------------------------- try: reply = core_stub.StartSession(req) except Exception as ex: return get_failed_precondition_sess_response(str(ex)) # -------------------------------- # Convert the reply to JSON and send it back # -------------------------------- return MessageToJson(reply)
def end_session(raven_json_str): """end session command This command needs a session id from the start_session cmd e.g. string: '{"session_id" : "123556"}' """ # The UI has sent JSON in string format that contains the session_id try: raven_dict = json.loads(raven_json_str) except json.decoder.JSONDecodeError as err_obj: err_msg = 'Failed to convert UI Str to JSON for end_session: %s' % ( err_obj) return get_failed_precondition_response(err_msg) # The protocol version always comes from the latest # version we have in the repo (just copied in for now) # if not KEY_SESSION_ID_FROM_UI in raven_dict: return get_failed_precondition_response(ERR_NO_SESSION_ID) # -------------------------------- # Convert back to string for TA2 call # -------------------------------- content = json.dumps(raven_dict) # -------------------------------- # convert the JSON string to a gRPC request # -------------------------------- try: req = Parse(content, core_pb2.SessionContext()) except ParseError as err_obj: err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj) return get_failed_precondition_response(err_msg) # In test mode, check if the incoming JSON is legit (in line above) # -- then return canned response below # if settings.TA2_STATIC_TEST_MODE: rnd_session_id = random_info.get_alphanumeric_string(7) tinfo = dict(session_id=rnd_session_id) #if random.randint(1, 3) == 3: # return get_grpc_test_json('test_responses/endsession_badassertion.json') return get_grpc_test_json('test_responses/endsession_ok.json', tinfo) if settings.TA2_STATIC_TEST_MODE: rnd_session_id = random_info.get_alphanumeric_string(7) tinfo = dict(session_id=rnd_session_id) if random.randint(1, 3) == 3: return get_grpc_test_json( request, 'test_responses/endsession_badassertion.json') return get_grpc_test_json(request, 'test_responses/endsession_ok.json', tinfo) # -------------------------------- # Get the connection, return an error if there are channel issues # -------------------------------- core_stub, err_msg = TA2Connection.get_grpc_stub() if err_msg: return get_failed_precondition_response(err_msg) # -------------------------------- # Send the gRPC request # -------------------------------- try: reply = core_stub.EndSession(req) except Exception as ex: return get_failed_precondition_response(str(ex)) # -------------------------------- # Convert the reply to JSON and send it back # -------------------------------- return MessageToJson(reply)