Пример #1
0
def get_saved_workspace_by_request_and_id(request, user_workspace_id):
    """Retrieve a specific workspace by request, checking that it
    is owned by the correct user"""

    # Get the User
    user_info = get_authenticated_user(request)
    if not user_info.success:
        return err_resp(user_info.err_msg)
    user = user_info.result_obj

    # Get the workspace
    #
    ws_info = get_user_workspace_config(user, user_workspace_id)
    if not ws_info.success:
        return err_resp(ws_info.err_msg)
    user_workspace = ws_info.result_obj

    # Does the user in the request match the one in the workspace
    #   - Later add additional permissions here for sharing
    #
    if not user.is_superuser:
        if not user == user_workspace.user:
            err_msg = (f'Sorry! User {user} does not have permission for '
                       f' workspace id: {user_workspace_id}.')
            return err_resp(err_msg)

    return ok_resp(user_workspace)
Пример #2
0
def view_score(request):

    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

    raven_data_info = get_request_body_as_json(request)
    if not raven_data_info.success:
        err_msg = f"request.body not found for solve"
        return JsonResponse(get_json_error(err_msg))
    data = raven_data_info.result_obj

    websocket_id = user_obj.username
    model_id = data['model_id']
    specification = data['specification']

    # sanity check timeout
    if isinstance(data.get('timeout', None), (int, float)):
        timeout = min(max(data.get('timeout'), 0), TIMEOUT_MAX)
    else:
        timeout = TIMEOUT_DEFAULT

    task_handle = tasks.score_task
    if not DEBUG_MODE:
        task_handle = task_handle.delay
    task_handle(websocket_id, model_id, specification)

    return JsonResponse({
        KEY_SUCCESS: True,
        KEY_MESSAGE: "score successfully started"
    })
Пример #3
0
def view_user_raven_config(request, user_workspace_id):
    """Retrieve information for a single workspace"""
    # Get 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

    ws_info = ws_util.get_user_workspace_config(user, user_workspace_id)
    if not ws_info.success:
        user_msg = 'No active workspaces found for user: %s and id: %s' % \
                    (user.username, user_workspace_id)
        return JsonResponse(get_json_error(user_msg))

    ws_dict = ws_info.result_obj.to_dict()

    json_msg = get_json_success('Workspace found.', data=ws_dict)

    if 'pretty' in request.GET:
        fmt_info = format_pretty_from_dict(json_msg)
        if not fmt_info.success:
            return JsonResponse(get_json_error(fmt_info.err_msg))
        return HttpResponse('<pre>%s</pre>' % fmt_info.result_obj)

    return JsonResponse(json_msg)
Пример #4
0
def view_activate_shared_workspace(request, user_workspace_id):
    """Set the UserWorkspace to public"""
    # Get 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

    ws_info = ws_util.get_saved_workspace_by_request_and_id(
        request, user_workspace_id)
    if not ws_info.success:
        user_msg = 'No active workspaces found for user: %s and id: %d' % \
                    (user.username, user_workspace_id)
        return JsonResponse(get_json_error(user_msg))

    workspace = ws_info.result_obj

    if workspace.is_public:
        # Consider it a success if the workspace is already public
        #
        user_msg = 'Workspace is already public'
    else:
        user_msg = 'Workspace is now public'
        workspace.is_public = True
        workspace.save()

    return JsonResponse(\
                get_json_success(user_msg,
                                 data=workspace.to_dict()))
Пример #5
0
def view_delete_config(request, user_workspace_id):
    """If this is the current dataset"""
    # Get 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

    ws_info = ws_util.get_user_workspace_config(user, user_workspace_id)
    if not ws_info.success:
        user_msg = 'No active workspaces found for user: %s and id: %s' % \
                    (user.username, user_workspace_id)
        return JsonResponse(get_json_error(user_msg))

    user_workspace = ws_info.result_obj

    if user_workspace.is_current_workspace:
        user_msg = 'You cannot delete the current workspace from the UI'
        return JsonResponse(get_json_error(user_msg))

    ws_name = '%s' % user_workspace

    user_workspace.delete()
    user_workspace.save()

    user_msg = 'Workspace deleted: %s' % ws_name

    return JsonResponse(get_json_success(user_msg))
Пример #6
0
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)
Пример #7
0
def view_latest_raven_configs(request, summary_only=False):
    """View config list with d3mconfig as separate object"""
    # Get 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

    params = dict(summary_only=summary_only)
    workspace_info = ws_util.get_user_workspaces_as_dict(user, **params)

    if not workspace_info.success:
        return JsonResponse(get_json_error(workspace_info.err_msg))

    json_msg = get_json_success(\
                'Workspaces found: %d' % len(workspace_info.result_obj),
                data=workspace_info.result_obj)

    if 'pretty' in request.GET:
        fmt_info = format_pretty_from_dict(json_msg)
        if not fmt_info.success:
            return JsonResponse(get_json_error(fmt_info.err_msg))

        return HttpResponse('<pre>%s</pre>' % fmt_info.result_obj)

    return JsonResponse(json_msg)
Пример #8
0
def view_clear_logs_for_user(request, json_resp=False):
    """Delete logs for the current user and return to the logs page.
    Unless there's an error, then you get a JSON response..."""
    user_info = get_authenticated_user(request)
    if not user_info.success:
        # If not logged in, you end up on the log in page
        return JsonResponse(get_json_error("Not logged in"))

    log_entry_info = BehavioralLogFormatter.get_log_entries(
        user_info.result_obj)
    if not log_entry_info.success:
        return JsonResponse(get_json_error(log_entry_info.err_msg))

    log_entries = log_entry_info.result_obj

    num_entries = log_entries.count()

    if num_entries > 0:
        log_entries.delete()
        user_msg = 'count of deleted log entries: %s' % num_entries
    else:
        user_msg = 'No log entries to delete'

    if json_resp:
        return JsonResponse(get_json_success(user_msg))

    return HttpResponseRedirect(reverse('view_show_log_onscreen'))
Пример #9
0
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()
Пример #10
0
def view_write_user_problem(request):
    """Format the user problem and write it to a file
    - Pull the current D3M config and update it based on the info
      provided
    """
    success, dict_info_or_err = get_request_body_as_json(request)
    if not success:
        return JsonResponse(dict(success=False, message=dict_info_or_err))

    problem_updates = dict_info_or_err

    user_info = get_authenticated_user(request)
    if not user_info.success:
        return JsonResponse(get_json_error(user_info.err_msg))

    problem_helper = UserProblemHelper(user_info.result_obj, problem_updates)
    if problem_helper.has_error:
        return JsonResponse(\
                dict(success=False,
                     message=problem_helper.error_message))

    return JsonResponse(dict(success=True,
                             message=problem_helper.get_success_message(),
                             data=dict(\
                                filepath=problem_helper.problem_filepath,
                                fileuri=problem_helper.problem_file_uri)))
Пример #11
0
def view_format_retrieve_user_problem(request):
    """Format the user problem and return the doc (instead of writing to file)
    """
    success, dict_info_or_err = get_request_body_as_json(request)
    if not success:
        return JsonResponse(get_json_error(dict_info_or_err))

    problem_updates = dict_info_or_err

    user_info = get_authenticated_user(request)
    if not user_info.success:
        return JsonResponse(get_json_error(user_info.err_msg))

    problem_helper = UserProblemHelper(user_info.result_obj,
                                       problem_updates,
                                       save_schema_to_file=False)

    if problem_helper.has_error:
        return JsonResponse(\
                dict(success=False,
                     message=problem_helper.error_message))

    return JsonResponse(\
                dict(success=True,
                     message=problem_helper.get_success_message(),
                     data=dict(\
                        new_problem_doc=problem_helper.new_problem_doc)))
Пример #12
0
def view_stored_response(request, hash_id):
    """Return a StoredResponse object"""
    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

    try:
        resp = StoredResponse.objects.get(\
                                hash_id=hash_id,)
                                # stored_request__user=user)
    except StoredResponse.DoesNotExist:
        user_msg = 'StoredResponse not found.'
        return JsonResponse(get_json_error(user_msg))

    StoredResponse.mark_as_read(resp)

    if 'pretty' in request.GET:
        json_str = '<pre>%s<pre>' % \
                   (json.dumps(resp.as_dict(), indent=4))
        return HttpResponse(json_str)

    resp_info = get_json_success('ok',
                                 data=resp.as_dict())
    return JsonResponse(resp_info)
Пример #13
0
def get_latest_d3m_user_config_by_request(request):
    """Find the lastest UserWorkspace and return the attached d3m_config"""
    user_info = get_authenticated_user(request)
    if not user_info.success:
        return err_resp(user_info.err_msg)

    user = user_info.result_obj
    return get_latest_d3m_user_config(user)
Пример #14
0
def set_shared_workspace_by_hash_id(request, hash_id):
    """Retrieve a shared workspace
    Basic sequence:
    - Is it a public workspace?
    - Does the shared workspace.user match the logged in user?
      - Yes: Proceed as if loading a regular workspace
    - No:
        - Does the logged in user already have this workspace?  (e.g. as an original)
            - Yes: load it directly
            - No: Create a new workspace, copying the data from the shared workspaces
    """
    # Get the User
    user_info = get_authenticated_user(request)
    if not user_info.success:
        return err_resp(user_info.err_msg)
    user = user_info.result_obj

    try:
        workspace = UserWorkspace.objects.get(hash_id=hash_id)
    except UserWorkspace.DoesNotExist:
        user_msg = ('No public workspaces were found for this shared link.'
                    ' <br /><br />(id: hash_id: %s)') % \
                    (hash_id)
        return err_resp(user_msg)

    if not workspace.is_public:
        user_msg = ('No public workspaces were found for this shared link.'
                    '<br /><br />Note: The workspace may have been made private.'
                    ' <br /><br />(id: hash_id: %s)') % \
                    (hash_id)
        return err_resp(user_msg)

    if workspace.user == user:
        # Make this the current workspace
        workspace.is_current_workspace = True
        workspace.save()
        return ok_resp(workspace)

    # Create a new workspace, based on the shared workspace
    #
    params = dict(user=user,
                  name=workspace.name,
                  is_current_workspace=True,
                  is_public=False,
                  d3m_config=workspace.d3m_config,
                  raven_config=workspace.raven_config,
                  original_workspace=workspace.original_workspace,
                  previous_workspace=workspace)

    params = get_default_workspace_params(**params)

    new_workspace = UserWorkspace(**params)
    new_workspace.save()

    # new_workspace.original_workspace = new_workspace
    # new_workspace.save()

    return ok_resp(new_workspace)
Пример #15
0
    def list_workspaces_by_request(request, as_dict=False, **kwargs):
        """Retrieve a list of all workspaces"""
        success, user_or_err = get_authenticated_user(request)
        if not success:
            return False, user_or_err

        return WorkspaceRetriever.list_workspaces_by_user(\
                                            user_or_err,
                                            as_dict,
                                            **kwargs)
Пример #16
0
def get_latest_user_workspace(request):
    """Get latest user workspace"""
    user_info = get_authenticated_user(request)
    if not user_info.success:
        return err_resp(user_info.err_msg)

    user = user_info.result_obj

    params = dict(return_full_workspace=True)
    return get_latest_d3m_user_config(user, create_if_not_found=True, **params)
Пример #17
0
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)
Пример #18
0
def view_solve(request):

    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

    raven_data_info = get_request_body_as_json(request)
    if not raven_data_info.success:
        err_msg = f"request.body not found for solve"
        return JsonResponse(get_json_error(err_msg))
    data = raven_data_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

    websocket_id = user_obj.username
    specification = data['specification']
    system_id = data['system']
    system_params = data.get('system_params', {})

    # create a location where the solver may write to disk
    dest_dir_info = create_destination_directory(user_workspace,
                                                 name='solver_scratch_space')
    if not dest_dir_info[KEY_SUCCESS]:
        return JsonResponse(get_json_error(dest_dir_info.err_msg))
    dest_directory = dest_dir_info[KEY_DATA]
    specification['temp_directory'] = dest_directory

    # TODO: timeout on celery worker
    # sanity check timeout
    if isinstance(data.get('timeout', None), (int, float)):
        timeout = min(max(data.get('timeout'), 0), TIMEOUT_MAX)
    else:
        timeout = TIMEOUT_DEFAULT

    search_id = Search.get_search_id()

    task_handle = tasks.solve_task
    if not DEBUG_MODE:
        task_handle = task_handle.delay
    task_handle(websocket_id, system_id, specification, system_params,
                search_id)

    return JsonResponse({
        KEY_SUCCESS: True,
        KEY_MESSAGE: "solve successfully started",
        KEY_DATA: {
            "search_id": search_id
        }
    })
Пример #19
0
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))
Пример #20
0
def view_clear_grpc_stored_history(request):
    """For develop, clear GPRC stored history for a User"""
    user_info = get_authenticated_user(request)
    if not user_info.success:
        return JsonResponse(get_json_error(user_info.err_msg))

    clear_info = SearchHistoryUtil.clear_grpc_stored_history(user_info.result_obj)

    if not clear_info.success:
        return HttpResponse(clear_info.err_msg)

    return HttpResponse('<br />'.join(clear_info.result_obj))
Пример #21
0
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)
Пример #22
0
    def get_by_id_and_request(ws_id, request, as_dict=False):
        """Get SavedWorkspace by id"""
        if ws_id is None:
            return False, ERR_WORKSPACE_ID_IS_NONE

        if request is None:
            return False, ERR_REQUEST_OBJ_IS_NONE

        success, user_or_err = get_authenticated_user(request)
        if not success:
            return False, user_or_err

        return WorkspaceRetriever.get_by_user_and_id(user_or_err, ws_id,
                                                     as_dict)
Пример #23
0
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)
Пример #24
0
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_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)
Пример #26
0
def view_retrieve_d3m_ice_data(request):
    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))

    user_info = get_authenticated_user(request)
    if not user_info.success:
        return JsonResponse(get_json_error(user_info.err_msg))

    req_info = req_body_info.result_obj
    return JsonResponse({
        KEY_SUCCESS: True,
        KEY_DATA: util_results_importance_ice(
            req_info['data_pointer_predictors'],
            req_info['data_pointer_fitted'],
            req_info['variable'])})
Пример #27
0
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)
Пример #28
0
def view_download(request):

    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

    raven_data_info = get_request_body_as_json(request)
    if not raven_data_info.success:
        err_msg = f"request.body not found for solve"
        return JsonResponse(get_json_error(err_msg))
    data = raven_data_info.result_obj
    model_id = data.get('model_id')

    if model_id:
        return JsonResponse({
            KEY_SUCCESS: False,
            KEY_MESSAGE: '"model_id" is a required field'
        })

    save_path = os.path.join(SAVED_MODELS_PATH, model_id)
    export_path = os.path.join(EXPORTED_MODELS_PATH, model_id + '.zip')

    if not os.path.exists(save_path):
        return JsonResponse({
            KEY_SUCCESS: False,
            KEY_MESSAGE: f'model "{model_id}" does not exist'
        })

    if not os.path.exists(EXPORTED_MODELS_PATH):
        os.makedirs(EXPORTED_MODELS_PATH)

    if not os.path.exists(export_path):
        with zipfile.ZipFile(export_path, 'w', zipfile.ZIP_DEFLATED) as zfile:
            for root, dirs, files in os.walk(save_path):
                for file in files:
                    zfile.write(
                        os.path.join(root, file),
                        os.path.relpath(os.path.join(root, file),
                                        os.path.join(save_path, '..')))

    return JsonResponse({
        KEY_SUCCESS: True,
        KEY_DATA: {
            'model_pointer': 'file://' + export_path
        }
    })
Пример #29
0
def view_reset_user_configs(request):
    """Delete UserWorkspace objects based on the User and base Config"""
    # Get the user
    #
    user_info = get_authenticated_user(request)
    if not user_info.success:
        return JsonResponse(get_json_error(user_info.err_msg))

    # Delete workspaces (if any)
    #
    user = user_info.result_obj
    delete_info = ws_util.delete_user_workspaces(user)
    if not delete_info.success:
        return JsonResponse(get_json_error(delete_info.err_msg))

    # Now reset them
    #
    return view_latest_raven_configs(request)
Пример #30
0
def clear_user_workspaces(request):
    """Clear the workspaces of the logged in User and return to pebbles page"""

    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

    reset_util = ResetUtil(user=user, **dict(request_obj=request))
    if reset_util.has_error():
        return JsonResponse(get_json_error(reset_util.get_err_msg()))

    # Now really stop searches, clears logs, etc, etc
    #
    reset_util.start_the_reset()

    return HttpResponseRedirect(reverse('home'))