def log_preprocess_call(user, json_data, session_id=''): """Note: The preprocess call also does problem discovery.""" # -------------------------------- # Behavioral logging # -------------------------------- # Check the request for an l1_activity, default to DATA_PREPARATION # activity_l1_val = json_data[bl_static.KEY_L1_ACTIVITY] \ if bl_static.KEY_L1_ACTIVITY in json_data \ else bl_static.L1_DATA_PREPARATION # Check the request for an l1_activity, default to DATA_PREPARATION # activity_l2_val = json_data[bl_static.KEY_L2_ACTIVITY] \ if bl_static.KEY_L2_ACTIVITY in json_data \ else bl_static.L2_DATA_EXPLORE log_data = dict(session_key=session_id, feature_id=rook_static.PREPROCESS_DATA, activity_l1=activity_l1_val, activity_l2=activity_l2_val) LogEntryMaker.create_system_entry(user, log_data) # Log the discovery activity # log_data2 = dict(session_key=session_id, feature_id=rook_static.PROBLEM_DISCOVERY, activity_l1=bl_static.L1_PROBLEM_DEFINITION, activity_l2=activity_l2_val) LogEntryMaker.create_system_entry(user, log_data2)
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 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)