def view_get_dataflow_results(request): """gRPC: Call from UI to GetDataflowResults""" success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='DescribeDataflow', request_msg=raven_data_or_err) # Let's call the TA2 and start the session! # json_str = get_data_flow_results(raven_data_or_err) # Convert JSON str to python dict - err catch here # - let it blow up for now--should always return JSON json_dict = json.loads(json_str, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
def view_startsession(request): """gRPC: Call from UI to start session user_agent = can come from UI. Version id will originate on the server """ session_key = get_session_key(request) success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='start_session', request_msg=raven_data_or_err) # Let's call the TA2 and start the session! # json_str = start_session(raven_data_or_err) # Convert JSON str to python dict - err catch here # - let it blow up for now--should always return JSON json_dict = json.loads(json_str, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
def view_get_execute_pipeline_results(request): """view for GetExecutePipelineResults""" session_key = get_session_key(request) success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='get_execute_pipeline_results', request_msg=raven_data_or_err) # Let's call the TA2! # json_str = get_execute_pipeline_results(raven_data_or_err) # Convert JSON str to python dict - err catch here # json_dict = json.loads(json_str, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
def view_describe_solution(request): """gRPC: Call from UI with a DescribeSolutionRequest""" 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='DescribeSolution', request_msg=req_body_info.result_obj) # Let's call the TA2! # search_info = describe_solution(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 view_set_problem_doc(request): """gRPC: Call from UI to SetProblemDoc""" session_key = get_session_key(request) success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='SetProblemDoc', request_msg=raven_data_or_err) # Let's call the TA2! # json_str = set_problem_doc(raven_data_or_err) # Convert JSON str to python dict - err catch here # json_dict = json.loads(json_str, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
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_execute_pipeline(request): """ This is a more complex request that does 2 things: (1) Writes the data portion of the JSON from the UI to a file in "temp_storage_root" - e.g. create a directory and add the file with a unique name (2) Send a gRPC request message replacing "some uri" with reference to the file written in - e.g. `file://{temp_storage_root}/the_file_with_data.json` {"context": {"sessionId": "session_01"}, "pipelineId": "pipeline_1", "predictFeatures": [{"featureId": "cylinders", "dataUri": "<<DATA_URI>>"}, {"featureId": "displacement", "dataUri": "<<DATA_URI>>"}, {"featureId": "horsepower", "dataUri": "<<DATA_URI>>"}, {"featureId": "weight", "dataUri": "<<DATA_URI>>"}, {"featureId": "acceleration", "dataUri": "<<DATA_URI>>"}, {"featureId": "model", "dataUri": "<<DATA_URI>>"}, {"featureId": "class", "dataUri": "<<DATA_URI>>"}], "data": [[5.4496644295302, 5.4496644295302], [192.81711409396, 192.81711409396], [103.211604095563, 103.211604095563], [2978.70469798658, 2978.70469798658], [15.6577181208054, 15.6577181208054], [76.0771812080537, 76.0771812080537], [1.5738255033557, 1.5738255033557], [23.5268456375839, 23.5268456375839]]} """ session_key = get_session_key(request) success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='execute_pipeline', request_msg=raven_data_or_err) # Let's call the TA2 and start the session! # fmt_request, json_str_or_err = execute_pipeline(raven_data_or_err) if fmt_request is None: if call_entry: call_entry.save_d3m_response(json_str_or_err) return JsonResponse(dict(status=False, message=json_str_or_err)) # Convert JSON str to python dict - err catch here # json_dict = {} json_dict['grpcResp'] = json.loads(json_str_or_err) json_dict['data2'] = json.loads(fmt_request) # request with updated file uris # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
def view_score_solutions(request): """gRPC: Call from UI with a GetScoreSolutionResultsRequest""" 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='GetScoreSolutionResults', request_msg=req_body_info.result_obj) # Let's call the TA2! # # websocket id: trying username for now, may change this in the future # websocket_id = user_info.result_obj.username search_info = get_score_solutions_results(\ req_body_info.result_obj, user_info.result_obj, websocket_id=websocket_id) #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 = search_info.result_obj #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 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 view_get_produce_solution_results(request): """gRPC: Call from UI with a GetProduceSolutionResultsRequest""" 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='GetProduceSolutionResults', request_msg=req_body_info.result_obj) # Let's call the TA2! # # websocket id: trying username for now, may change this in the future # websocket_id = user_info.result_obj.username search_info = get_produce_solution_results(\ req_body_info.result_obj, user_info.result_obj, websocket_id=websocket_id) #print('search_info', search_info) if not search_info.success: return JsonResponse(get_json_error(search_info.err_msg)) json_dict = search_info.result_obj # 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 view_export_pipeline(request): """gRPC: Call from UI to export pipeline session_id = from UI; originally from startsession commmand example string: { "context":{ "session_id":"session_0" }, "pipelineId":"pipe1", "pipelineExecUri":"<<EXECUTABLE_URI>>" } """ session_key = get_session_key(request) success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='export_pipeline', request_msg=raven_data_or_err) # Let's call the TA2 and start the session! # json_str = export_pipeline(raven_data_or_err, call_entry) #print('json_str: [%s]' % json_str) # Convert JSON str to python dict - err catch here # json_dict = json.loads(json_str, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
def view_delete_pipelines(request): """gRPC: Call from UI to delete pipelines session_id = from UI; originally from startsession commmand example string: '{ "context": { "session_id": "session_0" }, "delete_pipeline_ids" : ["pipeline_01", "pipeline_02"] };' """ session_key = get_session_key(request) success, raven_data_or_err = get_request_body(request) if not success: return JsonResponse(dict(status=False, message=raven_data_or_err)) # Begin to log D3M call # call_entry = None if ServiceCallEntry.record_d3m_call(): call_entry = ServiceCallEntry.get_dm3_entry(\ request_obj=request, call_type='DeletePipelines', request_msg=raven_data_or_err) # Let's call the TA2 and start the session! # json_str = delete_pipelines(raven_data_or_err) # Convert JSON str to python dict - err catch here # json_dict = json.loads(json_str, object_pairs_hook=OrderedDict) # Save D3M log # if call_entry: call_entry.save_d3m_response(json_dict) return JsonResponse(json_dict, safe=False)
def view_produce_solution(request): """gRPC: Call from UI with a ProduceSolutionRequest""" 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='ProduceSolution', request_msg=req_body_info.result_obj) # Let's call the TA2! # search_info = produce_solution(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 # 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_rook_route(request, app_name_in_url): """Route TwoRavens calls to Rook orig: TwoRavens -> Rook view: TwoRavens -> Django 2ravens -> Rook """ # get the app info # rook_app_info = RookAppInfo.get_appinfo_from_url(app_name_in_url) if rook_app_info is None: raise Http404(('unknown rook app: "{0}" (please add "{0}" to ' ' "tworaven_apps/rook_services/app_names.py")').format(\ app_name_in_url)) # record session metadata, if appropriate WorkspaceUtil.record_state(request) # look for the "solaJSON" variable in the POST # if rook_app_info.is_health_check(): # this is a health check raven_data_text = 'healthcheck' elif request.POST and UI_KEY_SOLA_JSON in request.POST: # this is a POST with a JSON string under the key solaJSON key raven_data_text = request.POST[UI_KEY_SOLA_JSON] else: # See if the body is JSON format req_found, raven_data_text = get_request_body_as_json(request) if not req_found: # Nope, send an error err_msg = ("Neither key '%s' found in POST" " nor JSON in request.body") % UI_KEY_SOLA_JSON return JsonResponse(dict(status="ERROR", message=err_msg)) # Retrieve post data and attempt to insert django session id # (if none exists) # # retrieve session key session_key = get_session_key(request) if isinstance(raven_data_text, str): blank_session_str = '%s":""' % ROOK_ZESSIONID if raven_data_text.find(blank_session_str) > -1: # was converting to JSON, but now just simple text substitution # updated_session_str = '%s":"%s"' % (ROOK_ZESSIONID, session_key) raven_data_text = raven_data_text.replace(blank_session_str, updated_session_str) elif ROOK_ZESSIONID in raven_data_text: if raven_data_text[ROOK_ZESSIONID] in [None, '']: raven_data_text[ROOK_ZESSIONID] = session_key if not isinstance(raven_data_text, str): try: raven_data_text = json.dumps(raven_data_text) except TypeError: JsonResponse(dict(success=False, message='Failed to convert data to JSON')) app_data = dict(solaJSON=raven_data_text) rook_svc_url = rook_app_info.get_rook_server_url() # Begin object to capture request # call_entry = None if rook_app_info.record_this_call(): call_entry = ServiceCallEntry.get_rook_entry(\ request_obj=request, call_type=rook_app_info.name, outgoing_url=rook_svc_url, request_msg=raven_data_text) #print('rook_svc_url: %s' % rook_svc_url) # Call R services # try: rservice_req = requests.post(rook_svc_url, data=app_data) except ConnectionError: err_msg = 'R Server not responding: %s' % rook_svc_url if rook_app_info.record_this_call(): call_entry.add_error_message(err_msg) call_entry.save() resp_dict = dict(message=err_msg) return JsonResponse(resp_dict) # Save request result # if rook_app_info.record_this_call(): if rservice_req.status_code == 200: call_entry.add_success_message(rservice_req.text, rservice_req.status_code) else: call_entry.add_error_message(rservice_req.text, rservice_req.status_code) call_entry.save() # Return the response to the user # #print(40 * '=') #print(r.text) #d = r.json() #print(json.dumps(d, indent=4)) print('status code from rook call: %d' % rservice_req.status_code) return HttpResponse(rservice_req.text)