Beispiel #1
0
def api_get_metadata(request):
    """Get metadata using the ISI Datamart"""
    req_info = get_request_body_as_json(request)
    if not req_info.success:
        return JsonResponse(get_json_error(req_info.err_msg))

    json_req_obj = req_info.result_obj

    # check if data is valid
    form = dm_forms.DatamartCustomForm(json_req_obj)
    if not form.is_valid():
        return JsonResponse(\
                get_json_success('invalid input',
                                 errors=form.errors.as_json()))

    metadata_info = DatamartJobUtilISI.datamart_get_metadata(
        json_req_obj['custom'])

    if not metadata_info.success:
        json_resp = get_json_error(metadata_info.err_msg)
    else:
        json_resp = get_json_success('it worked',
                                     data=metadata_info.result_obj)

    return JsonResponse(json_resp)
Beispiel #2
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))
def view_save_problem_form(request):
    """View test form"""
    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

    info_dict = dict()
    if request.POST:
        save_problem_form = SaveProblemForm(request.POST)
        if save_problem_form.is_valid():
            content = save_problem_form.cleaned_data

            bpw = BasicProblemWriter(user_workspace,
                                     content[PROBLEM_REQ_FILENAME],
                                     content[PROBLEM_REQ_DATA])

            if bpw.has_error():
                return JsonResponse(get_json_error(bpw.error_message))

            data_info = dict(filename=bpw.new_filepath,
                             timestamp=datetime.now())

            info = get_json_success('file created!', data=data_info)
            return JsonResponse(info)
        else:
            info_dict['form_errs'] = save_problem_form.errors
            save_problem_form = SaveProblemForm()
    else:
        save_problem_form = SaveProblemForm()

    info_dict['cform'] = save_problem_form

    return render(request, 'ta2_interfaces/view_save_problem_form.html',
                  info_dict)
Beispiel #4
0
def api_augment_async(request):
    """Run steps of augment, create new dataset folder structure, etc"""
    # Get the latest UserWorkspace
    #
    ws_info = get_latest_user_workspace(request)
    if not ws_info.success:
        user_msg = 'User workspace not found: %s' % ws_info.err_msg
        return JsonResponse(get_json_error(user_msg))

    user_workspace = ws_info.result_obj

    # Request as python dict
    #
    json_req_info = get_request_body_as_json(request)
    if not json_req_info.success:
        return JsonResponse(get_json_error(json_req_info.err_msg))

    augment_params = json_req_info.result_obj

    # print('augment_params', augment_params)

    augment_info = dm_tasks.make_augment_call(user_workspace, augment_params)

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

    return JsonResponse(get_json_success(augment_info.result_obj))
Beispiel #5
0
def api_datamart_info(request):
    """Return the current DatamartInfo objects"""
    is_pretty = request.GET.get('pretty', False)

    dm_list = DatamartInfo.active_objects.all()

    cnt = dm_list.count()
    if cnt == 0:
        user_msg = 'No active DatamartInfo objects found'
        json_resp = get_json_error(user_msg)
    else:

        dm_list_fmt = [dm.as_dict() for dm in dm_list]

        json_resp = get_json_success(\
                    'Datmart info objects found',
                    data=dict(count=cnt,
                              datamart_list=dm_list_fmt))

    if is_pretty is not False:  # return this as a formatted string?
        json_dump_info = json_dumps(json_resp, indent=4)

        if json_dump_info.success:
            dm_str = f'<pre>{json_dump_info.result_obj}<pre>'
            return HttpResponse(dm_str)

        return HttpResponse(json_dump_info.err_msg)

    return JsonResponse(json_resp)
Beispiel #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)
Beispiel #7
0
def view_get_problem_data_info(request, d3m_config_id=None):
    """Get info on train data and target files, if they exist"""
    if d3m_config_id is None:
        d3m_config = get_latest_d3m_config()
    else:
        d3m_config = D3MConfiguration.objects.filter(id=d3m_config_id).first()

    if d3m_config is None:
        user_msg = 'Config not found! (view_get_problem_data_info)'
        return JsonResponse(get_json_error(user_msg))

    is_pretty = request.GET.get('pretty', False)

    train_data_info = get_train_data_info(d3m_config)

    if not train_data_info.success:
        resp_dict = get_json_error(train_data_info.err_msg)

    else:
        resp_dict = get_json_success('It worked',
                                     data=train_data_info.result_obj)

    if is_pretty is not False:  # return this as a formatted string?
        config_str = '<pre>%s<pre>' % \
                    (json.dumps(resp_dict,
                                indent=4))
        return HttpResponse(config_str)

    return JsonResponse(resp_dict)
Beispiel #8
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)
Beispiel #9
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))
Beispiel #10
0
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)
Beispiel #11
0
def api_retrieve_event_data_query(request, query_id=None):
    """Retrieve a specific EventDataSavedQuery"""
    if not request.user.is_authenticated:
        user_msg = 'You must be logged in.'
        return JsonResponse(get_json_error(user_msg),
                            status=403)

    if not query_id:
        # shouldn't happen, just used to show plain url ...
        user_msg = 'You must specify a query_id in the url.'
        return JsonResponse(get_json_error(user_msg))

    query_info = EventJobUtil.get_by_id_and_user(query_id, request.user)

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

    user_info = get_json_success('Query found!',
                                 data=query_info.result_obj.as_dict())

    if 'pretty' in request.GET:
        fmt_info = format_pretty_from_dict(user_info)
        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(user_info)
Beispiel #12
0
def api_search_event_data_queries(request):
    """Search about models data ( query data )
    sample input : {
    "name":"query1",
    "description":"query desc",
    "username":"******"
    }
    """
    if not request.user.is_authenticated:
        user_msg = 'You must be logged in.'
        return JsonResponse(get_json_error(user_msg),
                            status=403)

    json_info = get_request_body_as_json(request)
    if not json_info.success:
        return JsonResponse(get_json_error(json_info.err_msg))

    # check if json is empty
    #
    json_data = json_info.result_obj

    search_results = EventJobUtil.search_objects(request.user, json_data)
    if not search_results.success:
        return JsonResponse(get_json_error(search_results.err_msg))

    user_info = get_json_success('results found!',
                                 data=search_results.result_obj)
    if 'pretty' in request.GET:
        fmt_info = format_pretty_from_dict(user_info)
        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(user_info)
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)
Beispiel #14
0
def api_delete_event_data_query(request, query_id=None):
    """Delete a EventDataSavedQuery.
    Checks that the logged in user owns the query"""
    if not request.user.is_authenticated:
        user_msg = 'You must be logged in.'
        return JsonResponse(get_json_error(user_msg),
                            status=403)

    if not query_id:
        # shouldn't happen, just used to show plain url ...
        user_msg = 'You must specify a query_id in the url.'
        return JsonResponse(get_json_error(user_msg))

    query_info = EventJobUtil.get_by_id_and_user(query_id, request.user)

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

    saved_query = query_info.result_obj
    try:
        saved_query.delete()
    except IntegrityError as ex_obj:
        user_obj = 'Failed to delete query. Error: %s' % ex_obj
        return JsonResponse(get_json_error(user_obj))

    return JsonResponse(get_json_success('Query deleted'))
Beispiel #15
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)
Beispiel #16
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()))
Beispiel #17
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'))
Beispiel #18
0
def api_mongo_healthcheck(request):
    """Mongo healthcheck"""
    mongo_check = MongoRetrieveUtil.run_tworavens_healthcheck()

    if mongo_check.success:
        return JsonResponse(get_json_success(\
                            'Mongo is running',
                            data=mongo_check.result_obj))

    return JsonResponse(get_json_error(mongo_check.err_msg))
Beispiel #19
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)
Beispiel #20
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)
Beispiel #21
0
def api_materialize(request):
    """Run materialize using either ISI or NYU"""
    success, json_req_obj = get_request_body_as_json(request)

    if not success:
        return JsonResponse(get_json_error(json_req_obj))

    # ----------------------------------------
    # Get the latest UserWorkspace
    #   - later used for logging
    # ----------------------------------------
    ws_info = get_latest_user_workspace(request)
    if not ws_info.success:
        user_msg = 'User workspace not found: %s' % ws_info.err_msg
        return JsonResponse(get_json_error(user_msg))

    user_workspace = ws_info.result_obj

    # --------------------------------------
    # check the data
    # --------------------------------------
    form = dm_forms.DatamartMaterializeForm(json_req_obj)
    if not form.is_valid():
        print('form.errors.as_json()', form.errors.as_json())
        return JsonResponse(\
                get_json_error("invalid input",
                               errors=form.errors.as_json()))

    # --------------------------------------
    # Retrieve the correct datamart
    # --------------------------------------
    job_util_info = get_datamart_job_util(form.cleaned_data['source'])
    if not job_util_info.success:
        return JsonResponse(get_json_error(job_util_info.err_msg))

    DatamartJobUtil = job_util_info.result_obj  # e.g. DatamartJobUtilISI, DatamartJobUtilNYU

    # --------------------------------------
    # Run datamart_materialize
    # --------------------------------------
    materialize_result = DatamartJobUtil.datamart_materialize(\
                                user_workspace,
                                form.cleaned_data['search_result'])
    if not materialize_result.success:
        return JsonResponse(get_json_error(materialize_result.err_msg))

    return JsonResponse(\
            get_json_success('it worked',
                             data=materialize_result.result_obj))
Beispiel #22
0
def api_add_event_data_query(request):
    """
    Add an EventDataSavedQuery to the database
    Example Json included in the body of th request:
        {
           "name":"User entered query name",
           "description":"In this query I am ...."
           "query":[ "... mongo query, either list or dict ..." ],
           "collection_name":"acled_africa",
           "collection_type":"subset",
           "result_count":161939,
        }
    """
    if not request.user.is_authenticated:
        user_msg = 'You must be logged in.'
        return JsonResponse(get_json_error(user_msg),
                            status=403)

    json_info = get_request_body_as_json(request)
    # if json is not valid
    if not json_info.success:
        return JsonResponse(get_json_error(json_info.err_msg))

    # Validate form results
    #
    event_data_info = json_info.result_obj
    event_data_info['user'] = request.user.id
    frm = EventDataSavedQueryForm(event_data_info)

    if not frm.is_valid():
        user_msg = dict(success=False,
                        message='Invalid input',
                        errors=frm.errors)
        return JsonResponse(user_msg)

    # Save the object
    #
    saved_query = EventDataSavedQuery(**frm.cleaned_data)

    try:
        saved_query.save()
    except IntegrityError:
        # rare to get here--maybe simultaneous saves...
        user_msg = EventDataSavedQueryForm.get_duplicate_record_error_msg()
        return JsonResponse(get_json_error(user_msg))

    ok_info = get_json_success('Query saved!', data=saved_query.as_dict())

    return JsonResponse(ok_info)
Beispiel #23
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)
Beispiel #24
0
def api_search(request):
    """Search the datamart with a JSON request.  The 'source' will
    determine which datamart to search"""
    # for logging
    ws_info = get_latest_user_workspace(request)
    if not ws_info.success:
        user_msg = 'User workspace not found: %s' % ws_info.err_msg
        return JsonResponse(get_json_error(user_msg))
    user_workspace = ws_info.result_obj

    success, json_req_obj = get_request_body_as_json(request)

    if not success:
        return JsonResponse(get_json_error(json_req_obj))

    # check if data is valid
    form = dm_forms.DatamartSearchForm(json_req_obj)
    if not form.is_valid():
        #print('form.errors', form.errors.as_json())
        print('\ntype form.errors', type(form.errors.as_json()))
        json_errs = json.loads(form.errors.as_json())
        err_msgs = [
            dal['message'] for dval_list in json_errs.values()
            for dal in dval_list if 'message' in dal
        ]
        print('\nerr_msgs', err_msgs)

        json_err = get_json_error('Input error: %s' % ('. '.join(err_msgs)))
        return JsonResponse(json_err)

    # Retrieve the appropriate DatamartJobUtil
    #
    job_util_info = get_datamart_job_util(form.cleaned_data['source'])
    if not job_util_info.success:
        return JsonResponse(get_json_error(job_util_info.err_msg))

    # e.g. DatamartJobUtilISI, DatamartJobUtilNYU
    DatamartJobUtil = job_util_info.result_obj

    #data_path = json_req_obj['data_path'] if 'data_path' in json_req_obj else None


    success, results_obj_err = DatamartJobUtil.datamart_search(\
                                    form.cleaned_data['query'],
                                    **dict(user_workspace=user_workspace))
    if not success:
        return JsonResponse(get_json_error(results_obj_err))

    return JsonResponse(get_json_success('it worked', data=results_obj_err))
Beispiel #25
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)
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)
Beispiel #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)
def view_grpc_search_history_json(request, search_id):
    """View stored request/responses based on search_id"""
    if not search_id:
        err_info = get_json_error('No search_id was found')
        return JsonResponse(get_json_error(err_info))

    search_history_util = SearchHistoryUtil(search_id=search_id)

    if search_history_util.has_error():
        err_info = f'Error found: {search_history_util.get_err_msg()}'
        #print(f'Error found: f{search_history_util.get_error()}')
        return JsonResponse(get_json_error(err_info))

    info_dict = dict(search_id=search_id,
                     json_history=search_history_util.get_finalized_history())

    user_info = get_json_success('History found', data=info_dict)
    return JsonResponse(user_info)
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)
Beispiel #30
0
def api_search_by_dataset(request):
    """For search, submit the entire dataset.
    Return the calls async"""
    # (1) get the request body
    #
    success, json_req_obj = get_request_body_as_json(request)
    if not success:
        return JsonResponse(get_json_error(json_req_obj))

    # (2) Get the latest UserWorkspace
    #
    ws_info = get_latest_user_workspace(request)
    if not ws_info.success:
        user_msg = 'User workspace not found: %s' % ws_info.err_msg
        return JsonResponse(get_json_error(user_msg))

    user_workspace = ws_info.result_obj

    # (3) Which datamart?
    #
    form = dm_forms.DatamartSearchByDatasetForm(json_req_obj)
    if not form.is_valid():
        print('form.errors.as_json()', form.errors.as_json())
        return JsonResponse(\
                get_json_error("invalid input",
                               errors=form.errors.as_json()))

    # (4) Location of the current dataset
    #
    # augment is embedded within a manipulations pipeline, so the data path may be different
    dataset_path = json_req_obj['dataset_path']

    # (5) Kick off async search
    #
    call_info = dm_tasks.make_search_by_dataset_call(\
                        form.cleaned_data['source'],
                        user_workspace.id,
                        dataset_path,
                        query=json_req_obj.get('query', None))

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

    return JsonResponse(get_json_success('Search by dataset has started!'))