def view_search_describe_fit_score_solutions(request): """gRPC: Call from UI with params to Search, Describe, Fit, and Score solutions""" # ------------------------------------ # Retrieve the User # ------------------------------------ user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) user_obj = user_info.result_obj websocket_id = user_obj.username # websocket pushes currently based on username user_id = user_obj.id # ------------------------------------ # Parse the JSON request # ------------------------------------ req_json_info = get_request_body_as_json(request) if not req_json_info.success: return JsonResponse(get_json_error(req_json_info.err_msg)) extra_params = {SESSION_KEY: get_session_key(request)} search_info = SearchSolutionsHelper.make_search_solutions_call(\ req_json_info.result_obj, websocket_id, user_id, **extra_params) if not search_info.success: return JsonResponse(get_json_error(search_info.err_msg)) json_info = get_json_success('success!', data=search_info.result_obj) return JsonResponse(json_info, safe=False)
def view_pebbles_home(request): """Serve up the workspace, the current home page. Include global js settings""" app_config = AppConfiguration.get_config() if app_config is None: return HttpResponseRedirect(reverse('view_no_domain_config_error')) # Is this D3M Mode? If so, make sure: # (1) there is D3M config information # (2) user is logged in # if app_config.is_d3m_domain(): # (1) Is there a valid D3M config? d3m_config = get_latest_d3m_config() if not d3m_config: return HttpResponseRedirect(\ reverse('view_d3m_config_error')) # (2) Is the user authenticated? if not request.user.is_authenticated(): return HttpResponseRedirect(\ reverse('login')) session_key = get_session_key(request) dinfo = dict(title='TwoRavens', session_key=session_key, app_config=app_config.convert_to_dict()) return render(request, 'index.html', dinfo)
def view_export_log_csv(request): """Export the behavioral log as a .csv""" # ---------------------------------------- # Get the user and session_key # ---------------------------------------- user_info = get_authenticated_user(request) if not user_info.success: # If not logged in, you end up on the log in page return HttpResponseRedirect(reverse('home')) user = user_info.result_obj session_key = get_session_key(request) log_entry_info = BehavioralLogFormatter.get_log_entries(user, session_key) if not log_entry_info.success: return HttpResponse(log_entry_info.err_msg) # Create the HttpResponse object with the appropriate CSV header. # response = HttpResponse(content_type='text/csv') log_fname = f'behavioral_log_{get_timestamp_string()}.csv' response['Content-Disposition'] = f'attachment; filename="{log_fname}"' blf = BehavioralLogFormatter(csv_output_object=response, log_entries=log_entry_info.result_obj) if blf.has_error(): user_msg = 'Error: %s' % blf.get_error_message() return HttpResponse(user_msg) #writer = csv.writer(response) #writer.writerow(['First row', 'Foo', 'Bar', 'Baz']) #writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"]) return blf.get_csv_output_object()
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_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_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_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_create_log_entry(request, is_verbose=False): """Make log entry endpoint""" user_info = get_authenticated_user(request) if not user_info.success: user_msg = 'Can only log entries when user is logged in.' return JsonResponse(get_json_error(user_msg)) user = user_info.result_obj session_key = get_session_key(request) # ---------------------------------------- # Get the log data # ---------------------------------------- json_info = get_request_body_as_json(request) if not json_info.success: return JsonResponse(get_json_error(json_info.err_msg)) log_data = json_info.result_obj log_data.update(dict(session_key=session_key)) # Default L2 to unkown # if not bl_static.KEY_L2_ACTIVITY in log_data: log_data[bl_static.KEY_L2_ACTIVITY] = bl_static.L2_ACTIVITY_BLANK # Note: this form is also used by the LogEntryMaker # - redundant but ok for now, want to return form errors # in a separate field # f = BehavioralLogEntryForm(log_data) if not f.is_valid(): print('nope: %s' % f.errors) user_msg = 'Error found in log entry.' return JsonResponse(get_json_error(user_msg, errors=f.errors)) log_create_info = LogEntryMaker.create_log_entry(\ user, log_data['type'], log_data) if not log_create_info.success: return JsonResponse(get_json_error(log_create_info.err_msg)) user_msg = 'Log entry saved!' if is_verbose: return JsonResponse(get_json_success(\ user_msg, data=log_create_info.result_obj.to_dict())) return JsonResponse(get_json_success(user_msg))
def __init__(self, request_obj): """Save state based on the Django request object""" self.request_obj = request_obj self.session_key = get_session_key(request_obj) # Set user if request_obj.user.is_authenticated(): self.is_authenticated = True self.loggedin_user = request_obj.user else: self.is_authenticated = False self.loggedin_user = None
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_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_R_preprocess(request): """Route to rook preprocess Example input: { "data": "/ravens_volume/test_data/196_autoMpg/TRAIN/dataset_TRAIN/tables/learningData.csv", "datastub": "196_ag_problem_TRAIN" } """ # used for logging user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) json_info = get_request_body_as_json(request) if not json_info.success: return JsonResponse(get_json_error(json_info.err_msg)) json_data = json_info.result_obj LOGGER.info('view_rook_preprocess input: %s', json_data) # print('view_rook_preprocess, json_data', json_data) if not rook_static.KEY_DATA in json_data: err_msg = (f'The key "{rook_static.KEY_DATA}" was not found' f' in the preprocess request') return JsonResponse(get_json_error(err_msg)) if not rook_static.KEY_DATASTUB in json_data: err_msg = (f'The key "{rook_static.KEY_DATASTUB}" was not found' f' in the preprocess request') return JsonResponse(get_json_error(err_msg)) log_preprocess_call(user_info.result_obj, json_data, get_session_key(request)) putil = PreprocessUtil(json_data[rook_static.KEY_DATA], datastub=json_data[rook_static.KEY_DATASTUB]) if putil.has_error(): return JsonResponse(get_json_error(putil.get_error_message())) info = get_json_success('it worked', data=putil.get_preprocess_data()) return JsonResponse(info, encoder=RavenJSONEncoder)
def get_rook_entry(request_obj, call_type, outgoing_url, request_msg): """Init ServiceCallEntry object for a ROOK call""" assert request_obj is not None,\ "request_obj cannot be None" session_id = get_session_key(request_obj) user = None if request_obj.user.is_authenticated(): user = request_obj.user return ServiceCallEntry(call_type=call_type, service_type=SERVICE_TYPE_ROOK, outgoing_url=outgoing_url, session_id=session_id, user=user, request_msg=request_msg)
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 test_state(request): """test form""" session_key = get_session_key(request) if KEY_ADD_CNT in request.session: add_cnt = request.session[KEY_ADD_CNT] else: add_cnt = 0 request.session[KEY_ADD_CNT] = add_cnt if 'add1' in request.GET: add_cnt += 1 request.session[KEY_ADD_CNT] = add_cnt return HttpResponseRedirect(reverse('test_state')) info = dict(title='test page', session_key=session_key, add_cnt=add_cnt) return render(request, 'test_state.html', info)
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_current_workspace(request): """Retrieve a workspace by session_key and user""" if not request.user.is_authenticated(): err_msg = ('Not logged in') return JsonResponse(dict(success=False, message=err_msg), status=http.HTTPStatus.UNAUTHORIZED) # Look for a workspace by session_key and user # session_key = get_session_key(request) params = dict(user=request.user, session_key=session_key) current_workspace = SavedWorkspace.objects.filter(**params).first() if not current_workspace: err_msg = ('No workspace found' ' for session_key [%s] and the current user') % ( session_key, ) return JsonResponse(dict(success=False, message=err_msg), status=http.HTTPStatus.BAD_REQUEST) return JsonResponse(current_workspace.as_dict())
def get_partials_datasets(request): # request body req_body_info = get_request_body_as_json(request) if not req_body_info.success: return JsonResponse(get_json_error(req_body_info.err_msg)) req_info = req_body_info.result_obj # workspace user_workspace_info = get_latest_user_workspace(request) if not user_workspace_info.success: return JsonResponse(get_json_error(user_workspace_info.err_msg)) user_workspace = user_workspace_info.result_obj # user user_info = get_authenticated_user(request) if not user_info.success: return JsonResponse(get_json_error(user_info.err_msg)) activity_l1 = bl_static.L1_PROBLEM_DEFINITION activity_l2 = bl_static.L2_ACTIVITY_BLANK log_data = dict(session_key=get_session_key(request), feature_id='PARTIALS_APP', activity_l1=activity_l1, activity_l2=activity_l2) LogEntryMaker.create_system_entry(user_workspace.user, log_data) try: response = create_partials_datasets(req_info, user_workspace.id) except Exception: print("caught traceback when creating ICE datasets:", flush=True) print(traceback.format_exc(), flush=True) response = { KEY_SUCCESS: False, KEY_MESSAGE: "Internal error while creating ICE datasets." } return JsonResponse(response)
def get_dm3_entry(request_obj, call_type, request_msg): """Init ServiceCallEntry object for a D3M call""" assert request_obj is not None,\ "request_obj cannot be None" session_id = get_session_key(request_obj) user = None if request_obj.user.is_authenticated(): user = request_obj.user if settings.TA2_STATIC_TEST_MODE: outgoing_url = '(no TA2, static test mode)' else: outgoing_url = settings.TA2_TEST_SERVER_URL return ServiceCallEntry(call_type=call_type, service_type=SERVICE_TYPE_D3M, outgoing_url=outgoing_url, session_id=session_id, user=user, request_msg=request_msg)
def view_show_log_onscreen(request): """View a log base on the user's session_id, or just username""" # ---------------------------------------- # Get the user and session_key # ---------------------------------------- user_info = get_authenticated_user(request) if not user_info.success: # If not logged in, you end up on the log in page return HttpResponseRedirect(reverse('home')) user = user_info.result_obj session_key = get_session_key(request) log_entry_info = BehavioralLogFormatter.get_log_entries(user, session_key) if not log_entry_info.success: return HttpResponse(log_entry_info.err_msg) dinfo = dict(user=user, session_key=session_key, log_entries=log_entry_info.result_obj) return render(request, 'behavioral_logs/view_user_log.html', dinfo)
def view_workspace_info(request): """View current workspace and/or other saved workspaces""" # Look for a workspace matching the session key session_key = get_session_key(request) current_workspace = SavedWorkspace.objects.filter(session_key=session_key\ ).order_by('-modified'\ ).first() # pull some session info, if there are any # if request.user.is_authenticated(): other_workspaces = SavedWorkspace.objects.filter(user=request.user) if current_workspace: other_workspaces = other_workspaces.exclude( id=current_workspace.id) else: other_workspaces = None info = dict(title='test session info', current_workspace=current_workspace, other_workspaces=other_workspaces, session_key=request.session.session_key) return render(request, 'view_workspace_info.html', info)
def list_user_workspaces(request): """Retrieve a workspace by the currently logged-in user""" if not request.user.is_authenticated(): err_msg = ('Not logged in') return JsonResponse(dict(success=False, message=err_msg), status=http.HTTPStatus.UNAUTHORIZED) params = {KW_SESSION_KEY: get_session_key(request)} success, ws_list_or_err = WorkspaceRetriever.list_workspaces_by_request(\ request, as_dict=True, **params) if not success: return JsonResponse(dict(success=False, message=ws_list_or_err), status=http.HTTPStatus.BAD_REQUEST) if not ws_list_or_err: err_msg = ('No workspaces found for this user.') return JsonResponse(dict(success=False, message=err_msg), status=http.HTTPStatus.NOT_FOUND) return JsonResponse(ws_list_or_err, safe=False)
def view_solution_export3(request): """gRPC: Call from UI with a SolutionExportRequest""" # Retrieve the User # 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 req_body_info = get_request_body_as_json(request) if not req_body_info.success: return JsonResponse(get_json_error(req_body_info.err_msg)) session_key = get_session_key(request) # Let's call the TA2! # search_info = solution_export3(user, req_body_info.result_obj, session_key=session_key) # 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)) 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)
def view_R_route(request, app_name_in_url): """Route TwoRavens calls to Rook orig: TwoRavens -> Rook view: TwoRavens -> Django 2ravens -> Rook This is a bit messy. Still trying to handle two UI calls: - old ones, form POSTs sent with solaJSON key - new ones, straight JSON requests """ # ----------------------------- # get the app info # ----------------------------- rook_app_info = RAppInfo.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/R_services/app_names.py")').format(\ app_name_in_url)) # ----------------------------- # Used for logging # ----------------------------- user_workspace_info = get_latest_user_workspace(request) if not user_workspace_info.success: return JsonResponse(get_json_error(user_workspace_info.err_msg)) user_workspace = user_workspace_info.result_obj # ----------------------------- # additional params # ----------------------------- raven_data_text = {} # default additional_params = {} # params to add to a JSON call, e.g. for PARTIALS_APP # ----------------------------- # look for the "solaJSON" variable in the POST # ----------------------------- if 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 raven_data_info = get_request_body_as_json(request) if not raven_data_info.success: 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)) raven_data_text = raven_data_info.result_obj # 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 raven_data_text.find(ROOK_ZESSIONID) == -1: print('MAJOR ISSUE: NOT SESSION AT ALL (R_services.views.py)') elif isinstance(raven_data_text, dict): # We have a dict, make sure it gets a session if ROOK_ZESSIONID in raven_data_text: if raven_data_text[ROOK_ZESSIONID] in [None, '']: raven_data_text[ROOK_ZESSIONID] = session_key elif ROOK_ZESSIONID not in raven_data_text: raven_data_text[ROOK_ZESSIONID] = session_key # Add the additional params raven_data_text.update(additional_params) try: raven_data_text = json.dumps(raven_data_text) except TypeError: return JsonResponse(\ dict(success=False, message='Failed to convert data to JSON')) # print('raven_data_text', raven_data_text) app_data = json.loads(raven_data_text) # -------------------------------- # Behavioral logging # -------------------------------- print('rook_app_info.name:', rook_app_info.name) feature_id = rook_app_info.name if rook_app_info.name == app_names.EXPLORE_APP: activity_l1 = bl_static.L1_DATA_PREPARATION activity_l2 = bl_static.L2_DATA_EXPLORE elif rook_app_info.name == app_names.PLOTDATA_APP: feature_id = 'EXPLORE_VIEW_PLOTS' activity_l1 = bl_static.L1_DATA_PREPARATION activity_l2 = bl_static.L2_DATA_EXPLORE else: activity_l1 = bl_static.L1_PROBLEM_DEFINITION activity_l2 = bl_static.L2_ACTIVITY_BLANK log_data = dict(session_key=session_key, feature_id=feature_id, activity_l1=activity_l1, activity_l2=activity_l2) LogEntryMaker.create_system_entry(user_workspace.user, log_data) # Call R services # rook_svc_url = rook_app_info.get_rook_server_url() print('rook_svc_url', rook_svc_url) try: rservice_req = requests.post(rook_svc_url, json=app_data) except ConnectionError: err_msg = 'R Server not responding: %s' % rook_svc_url resp_dict = dict(message=err_msg) return JsonResponse(resp_dict) print('status code from rook call: %s' % rservice_req.status_code) # print('rook text: %s' % rservice_req.text) return HttpResponse(rservice_req.text)
def view_pebbles_home(request): """Serve up the workspace, the current home page. Include global js settings""" if not request.user.is_authenticated: return HttpResponseRedirect(reverse('login')) app_config = AppConfiguration.get_config() if app_config is None: return HttpResponseRedirect(reverse('view_no_domain_config_error')) 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 # Is this D3M Mode? If so, make sure: # (1) there is D3M config information # (2) user is logged in # if app_config.is_d3m_domain(): # (1) Is there a valid D3M config? d3m_config_info = get_latest_d3m_config() if not d3m_config_info: return HttpResponseRedirect(reverse('view_list_dataset_choices_html')) # return HttpResponseRedirect(reverse('view_d3m_config_error')) session_key = get_session_key(request) else: session_key = '(event-data-no-session-key)' dinfo = dict(title='TwoRavens', session_key=session_key, DEBUG=settings.DEBUG, ALLOW_SOCIAL_AUTH=settings.ALLOW_SOCIAL_AUTH, CSRF_COOKIE_NAME=settings.CSRF_COOKIE_NAME, app_config=app_config.convert_to_dict(), # TA2_STATIC_TEST_MODE=settings.TA2_STATIC_TEST_MODE, TA2_TEST_SERVER_URL=settings.TA2_TEST_SERVER_URL, # TA2_D3M_SOLVER_ENABLED=pybool_to_js(settings.TA2_D3M_SOLVER_ENABLED), TA2_WRAPPED_SOLVERS=settings.TA2_WRAPPED_SOLVERS, # TA3_GRPC_USER_AGENT=settings.TA3_GRPC_USER_AGENT, TA3TA2_API_VERSION=TA3TA2Util.get_api_version(), DISPLAY_DATAMART_UI=settings.DISPLAY_DATAMART_UI, WEBSOCKET_PREFIX=settings.WEBSOCKET_PREFIX) log_data = dict(session_key=session_key, feature_id=bl_static.FID_START_RAVENS_PEBBLES_PAGE, activity_l1=bl_static.L1_DATA_PREPARATION, activity_l2=bl_static.L2_DATA_OPEN) LogEntryMaker.create_system_entry(user, log_data) #print('-' * 40) #print(dinfo['app_config']) return render(request, 'index.html', dinfo)
def __init__(self, request_obj): """Save state based on the Django request object""" self.request_obj = request_obj self.session_key = get_session_key(request_obj)