コード例 #1
0
def get_data_flow_results(info_str=None):
    """Ask a TA2 to GetDataflowResults via gRPC"""
    if info_str is None:
        err_msg = 'UI Str for PipelineReference is None'
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Is this valid JSON?
    # --------------------------------
    try:
        raven_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, dataflow_ext_pb2.PipelineReference())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # In test mode, return canned response
    #
    if settings.TA2_STATIC_TEST_MODE:
        info_dict = dict(pipelineId=raven_dict.get('pipelineId'))
        return get_grpc_test_json(
            'test_responses/get_dataflow_results_ok.json', info_dict)

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    dataflow_stub, err_msg = TA2Connection.get_grpc_dataflow_stub()
    if err_msg:
        return get_failed_precondition_sess_response(err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    try:
        reply = dataflow_stub.GetDataflowResults(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    if reply and str(reply) == VAL_GRPC_STATE_CODE_NONE:
        err_msg = ('Unkown gRPC state.'
                   ' (Was an GetDataflowResults request sent?)')
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Convert the reply to JSON and send it on
    # --------------------------------
    results = map(MessageToJson, reply)
    result_str = '[' + ', '.join(results) + ']'

    return result_str
コード例 #2
0
def set_problem_doc(info_str=None):
    """
    SetProblemDocRequest={"ReplaceProblemDocField":{"metric":"ROC_AUC"}}

    Accept UI input as JSON *string* similar to
     {"context": {"session_id": "session_0"}, "ReplaceProblemDocField": {"metric": "ACCURACY", "taskType": "CLASSIFICATION"}}
    """
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for SetProblemDoc is None'
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Convert info string to dict
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    #content = json.dumps(info_dict)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, core_pb2.SetProblemDocRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:
        return get_grpc_test_json('test_responses/set_problem_doc_ok.json',
                                  dict())

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    try:
        reply = core_stub.SetProblemDoc(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    # --------------------------------
    # Convert the reply to JSON and send it on
    # --------------------------------
    return MessageToJson(reply)
コード例 #3
0
ファイル: req_end_session.py プロジェクト: kundu-me/TwoRavens
def end_session(raven_json_str):
    """end session command
    This command needs a session id from the start_session cmd
    e.g. string: '{"session_id" : "123556"}'
    """
    # The UI has sent JSON in string format that contains the session_id
    try:
        raven_dict = json.loads(raven_json_str)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON for end_session: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # The protocol version always comes from the latest
    # version we have in the repo (just copied in for now)
    #
    if not 'session_id' in raven_dict:
        err_msg = 'No session_id found: %s' % (raven_json_str)
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Convert back to string for TA2 call
    # --------------------------------
    content = json.dumps(raven_dict)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(content, core_pb2.SessionContext())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)


    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

        #return dict(status=core_pb2.FAILED_PRECONDITION,
        #            details=err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    try:
        reply = core_stub.EndSession(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))


    # --------------------------------
    # Convert the reply to JSON and send it back
    # --------------------------------
    return MessageToJson(reply)
コード例 #4
0
def delete_pipelines(info_str):
    """Ask a TA2 to DeletePipelines via gRPC"""
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for DeletePipelines is None'
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Is this valid JSON?
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, core_pb2.PipelineDeleteRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:
        return get_grpc_test_json('test_responses/list_pipelines_ok.json',
                                  dict())

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request - returns a stream
    # --------------------------------
    try:
        reply = core_stub.DeletePipelines(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    # --------------------------------
    # Convert the reply to JSON and send it back
    # --------------------------------
    return MessageToJson(reply)
コード例 #5
0
def execute_pipeline(info_str=None):
    """Ask a TA2 to ListPipelines via gRPC

    This call is a bit different b/c it writes part of the data to a file
    and places that file uri into the original request

    Success:  (updated request str, grpc json response)
    Failure: (None, error message)
    """
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for PipelineListResult is None'
        return None, get_failed_precondition_response(err_msg)

    if info_str.find(VAL_DATA_URI) == -1:
        err_msg = ('Expected to see place holder for file uri.'
                   ' Placeholder is "%s"') % VAL_DATA_URI
        return None, get_failed_precondition_response(err_msg)

    d3m_config = get_latest_d3m_config()
    if not d3m_config:
        err_msg = ('The D3M configuration is not available.'
                   ' Therefore, there is no "temp_storage_root" directory to'
                   ' write the data.')
        return None, get_failed_precondition_response(err_msg)

    # --------------------------------
    # Is this valid JSON?
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return None, get_failed_precondition_response(err_msg)

    if not KEY_DATA in info_dict:
        err_msg = ('The JSON request did not contain a "%s" key.') % KEY_DATA
        return None, get_failed_precondition_response(err_msg)

    file_uri, err_msg = write_data_for_execute_pipeline(
        d3m_config, info_dict[KEY_DATA])

    if err_msg is not None:
        return None, get_failed_precondition_response(err_msg)

    # Reformat the original content
    #
    # (1) remove the data key
    if KEY_DATA in info_dict:
        del info_dict[KEY_DATA]

    # (2) convert it back to a JSON string
    info_str = json.dumps(info_dict)

    # (3) replace the VAL_DATA_URI with the file_uri
    info_str_formatted = info_str.replace(VAL_DATA_URI, file_uri)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str_formatted, core_pb2.PipelineExecuteRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return None, get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:

        #return info_str_formatted,\
        #       get_grpc_test_json('test_responses/execute_results_1pipe_ok.json',
        #                          dict())
        #---
        template_info = get_predict_file_info_dict()

        template_str = get_grpc_test_json(
            'test_responses/execute_results_1pipe_ok.json', template_info)

        # These next lines embed file uri content into the JSON
        embed_util = FileEmbedUtil(template_str)
        if embed_util.has_error:
            return get_failed_precondition_response(embed_util.error_message)

        test_note = ('Test.  An actual result would be the test JSON with'
                     ' the "data" section removed and DATA_URI replaced'
                     ' with a file path to where the "data" section was'
                     ' written.')

        return json.dumps(dict(note=test_note)), embed_util.get_final_results()
        #---
        #return info_str_formatted,\
        #       get_grpc_test_json('test_responses/execute_results_1pipe_ok.json',
        #                          dict())

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return None, get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request - returns a stream
    # --------------------------------
    try:
        reply = core_stub.ExecutePipeline(req)
    except Exception as ex:
        return None, get_failed_precondition_response(str(ex))

    # --------------------------------
    # Convert the reply to JSON and send it on
    # --------------------------------
    results = map(MessageToJson, reply)
    result_str = '[' + ', '.join(results) + ']'

    embed_util = FileEmbedUtil(result_str)
    if embed_util.has_error:
        return get_failed_precondition_response(embed_util.error_message)

    return info_str_formatted, embed_util.get_final_results()
コード例 #6
0
def export_pipeline(info_str=None, call_entry=None):
    """Ask a TA2 to ExportPipeline via gRPC"""
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for ExportPipeline is None'
        return get_failed_precondition_response(err_msg)

    if info_str.find(VAL_EXECUTABLE_URI) == -1:
        err_msg = ('Expected to see place holder for executable uri.'
                   ' Placeholder is "%s"') % VAL_EXECUTABLE_URI
        return None, get_failed_precondition_response(err_msg)


    d3m_config = get_latest_d3m_config()
    if not d3m_config:
        err_msg = ('The D3M configuration is not available.'
                   ' Therefore, there is no "executables_root" directory to'
                   ' write the data.')
        return None, get_failed_precondition_response(err_msg)

    # --------------------------------
    # Is this valid JSON?
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Construct and set a write directory for the executable
    # --------------------------------

    # get the pipeline id
    pipeline_id, err_msg = get_pipeline_id(info_dict)
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # dir = d3m_config.executables_root + pipeline_id
    executable_write_dir = join('file://%s' % d3m_config.executables_root,
                                pipeline_id)

    # update the dict + info_str
    info_dict[KEY_PIPELINE_EXEC_URI] = executable_write_dir
    if KEY_PIPELINE_EXEC_URI_FROM_UI in info_dict:
        del info_dict[KEY_PIPELINE_EXEC_URI_FROM_UI]


    try:
        info_str = json.dumps(info_dict)
    except TypeError as ex_obj:
        err_msg = 'Failed to PipelineExportRequest info to JSON: %s' % ex_obj
        return get_failed_precondition_response(err_msg)

    #print('info_str', info_str)
    if call_entry:
        call_entry.request_msg = info_str
        
    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, core_pb2.PipelineExportRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:
        return get_grpc_test_json('test_responses/export_pipeline_ok.json',
                                  dict())

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request - returns a stream
    # --------------------------------
    try:
        reply = core_stub.ExportPipeline(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    # --------------------------------
    # Convert the reply to JSON and send it back
    # --------------------------------
    return MessageToJson(reply)
コード例 #7
0
def get_execute_pipeline_results(info_str=None):
    """Ask a TA2 to GetExecutePipelineResults via gRPC"""
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for PipelineExecuteResultsRequest is None'
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Is this valid JSON?
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, core_pb2.PipelineExecuteResultsRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:

        template_info = get_predict_file_info_dict()

        template_str = get_grpc_test_json(
            'test_responses/execute_results_ok.json', template_info)

        embed_util = FileEmbedUtil(template_str)
        if embed_util.has_error:
            return get_failed_precondition_response(embed_util.error_message)

        return embed_util.get_final_results()

        #return get_grpc_test_json('test_responses/execute_results_ok.json',
        #                          dict())

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    #print('req: %s' % req)

    # --------------------------------
    # Send the gRPC request - returns a stream
    # --------------------------------
    try:
        reply = core_stub.GetExecutePipelineResults(req)
    except grpc.RpcError as ex:
        return get_failed_precondition_response(str(ex))
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    #print('reply', reply)
    """
    if reply and str(reply) == VAL_GRPC_STATE_CODE_NONE:
        err_msg = ('Unknown gRPC state.'
                   ' (Was an ExecutePipeline request sent?)')
        return get_failed_precondition_response(err_msg)
    """
    try:
        print(MessageToJson(reply))
    except:
        print('failed unary convert to JSON')
    #print('reply: %s' % reply)

    # --------------------------------
    # Convert the reply to JSON and send it on
    # --------------------------------
    results = map(MessageToJson, reply)
    result_str = '[' + ', '.join(results) + ']'

    embed_util = FileEmbedUtil(result_str)
    if embed_util.has_error:
        return get_failed_precondition_response(embed_util.error_message)

    return embed_util.get_final_results()
コード例 #8
0
def pipeline_create(info_str=None):
    """Send the pipeline create request via gRPC"""
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for %s is None' % PIPELINE_CREATE_REQUEST
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Convert info string to dict
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if KEY_CONTEXT_FROM_UI not in info_dict:
        return get_failed_precondition_response(ERR_NO_CONTEXT)

    if KEY_SESSION_ID_FROM_UI not in info_dict[KEY_CONTEXT_FROM_UI]:
        return get_failed_precondition_response(ERR_NO_SESSION_ID)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, core_pb2.PipelineCreateRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:

        template_info = get_predict_file_info_dict(info_dict.get('task'))

        template_str = get_grpc_test_json('test_responses/createpipeline_ok.json',
                                          template_info)

        # These next lines embed file uri content into the JSON
        embed_util = FileEmbedUtil(template_str)
        if embed_util.has_error:
            return get_failed_precondition_response(embed_util.error_message)

        return embed_util.get_final_results()
        #return get_grpc_test_json('test_responses/createpipeline_ok.json',
        #                          template_info)

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    messages = []

    try:
        for reply in core_stub.CreatePipelines(req):
            user_msg = MessageToJson(reply)
            print(user_msg)
            messages.append(user_msg)
    except Exception as ex:
        return get_reply_exception_response(str(ex))

    print('end of queue. make message list')

    result_str = '['+', '.join(messages)+']'

    print('embed file contents')
    embed_util = FileEmbedUtil(result_str)
    if embed_util.has_error:
        print('file embed error')
        return get_failed_precondition_response(embed_util.error_message)

    print('return results')
    return embed_util.get_final_results()
コード例 #9
0
def get_create_pipeline_results(info_str=None):
    """Send the pipeline create request via gRPC"""
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for %s is None' % PIPELINE_CREATE_RESULTS_REQUEST
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Convert info string to dict
    # --------------------------------
    try:
        info_dict = json.loads(info_str, object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if KEY_CONTEXT_FROM_UI not in info_dict:
        return get_failed_precondition_response(ERR_NO_CONTEXT)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(info_str, core_pb2.PipelineCreateResultsRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    if settings.TA2_STATIC_TEST_MODE:

        template_info = get_predict_file_info_dict(info_dict.get('task'))

        template_str = get_grpc_test_json(
            'test_responses/createpipeline_ok.json', template_info)

        # These next lines embed file uri content into the JSON
        embed_util = FileEmbedUtil(template_str)
        if embed_util.has_error:
            return get_failed_precondition_response(embed_util.error_message)

        return embed_util.get_final_results()
        #return get_grpc_test_json('test_responses/createpipeline_ok.json',
        #                          template_info)

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    try:
        reply = core_stub.GetCreatePipelineResults(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    try:
        print(MessageToJson(reply))
    except:
        print('failed unary convert to JSON')
    # --------------------------------
    # Convert the reply to JSON and send it on
    # --------------------------------
    results = map(MessageToJson, reply)

    result_str = '[' + ', '.join(results) + ']'

    embed_util = FileEmbedUtil(result_str)
    if embed_util.has_error:
        return get_failed_precondition_response(embed_util.error_message)

    return embed_util.get_final_results()
コード例 #10
0
def update_problem_schema(info_str=None):
    """
    Accept UI input as JSON *string* similar to
     {"taskType" : "REGRESSION",
      "taskSubtype" : "TASK_SUBTYPE_UNDEFINED",
      "outputType" : "REAL",
      "metric" : "ROOT_MEAN_SQUARED_ERROR"}
    """
    if info_str is None:
        info_str = get_test_info_str()

    if info_str is None:
        err_msg = 'UI Str for UpdateProblemSchema is None'
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Convert info string to dict
    # --------------------------------
    try:
        info_dict = json.loads(info_str)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # create UpdateProblemSchemaRequest compatible JSON
    # --------------------------------
    updates_list = []
    for key, val in info_dict.items():
        updates_list.append({key : val})

    final_dict = dict(updates=updates_list)

    content = json.dumps(final_dict)


    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(content, core_pb2.UpdateProblemSchemaRequest())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)


    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    try:
        reply = core_stub.UpdateProblemSchema(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    # --------------------------------
    # Convert the reply to JSON and send it on
    # --------------------------------
    return MessageToJson(reply)
コード例 #11
0
ファイル: req_end_session.py プロジェクト: Mital188/TwoRavens
def end_session(raven_json_str):
    """end session command
    This command needs a session id from the start_session cmd
    e.g. string: '{"session_id" : "123556"}'
    """
    # The UI has sent JSON in string format that contains the session_id
    try:
        raven_dict = json.loads(raven_json_str)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert UI Str to JSON for end_session: %s' % (
            err_obj)
        return get_failed_precondition_response(err_msg)

    # The protocol version always comes from the latest
    # version we have in the repo (just copied in for now)
    #
    if not KEY_SESSION_ID_FROM_UI in raven_dict:
        return get_failed_precondition_response(ERR_NO_SESSION_ID)

    # --------------------------------
    # Convert back to string for TA2 call
    # --------------------------------
    content = json.dumps(raven_dict)

    # --------------------------------
    # convert the JSON string to a gRPC request
    # --------------------------------
    try:
        req = Parse(content, core_pb2.SessionContext())
    except ParseError as err_obj:
        err_msg = 'Failed to convert JSON to gRPC: %s' % (err_obj)
        return get_failed_precondition_response(err_msg)

    # In test mode, check if the incoming JSON is legit (in line above)
    # -- then return canned response below
    #
    if settings.TA2_STATIC_TEST_MODE:
        rnd_session_id = random_info.get_alphanumeric_string(7)
        tinfo = dict(session_id=rnd_session_id)
        #if random.randint(1, 3) == 3:
        #    return get_grpc_test_json('test_responses/endsession_badassertion.json')

        return get_grpc_test_json('test_responses/endsession_ok.json', tinfo)

    if settings.TA2_STATIC_TEST_MODE:
        rnd_session_id = random_info.get_alphanumeric_string(7)
        tinfo = dict(session_id=rnd_session_id)
        if random.randint(1, 3) == 3:
            return get_grpc_test_json(
                request, 'test_responses/endsession_badassertion.json')

        return get_grpc_test_json(request, 'test_responses/endsession_ok.json',
                                  tinfo)

    # --------------------------------
    # Get the connection, return an error if there are channel issues
    # --------------------------------
    core_stub, err_msg = TA2Connection.get_grpc_stub()
    if err_msg:
        return get_failed_precondition_response(err_msg)

    # --------------------------------
    # Send the gRPC request
    # --------------------------------
    try:
        reply = core_stub.EndSession(req)
    except Exception as ex:
        return get_failed_precondition_response(str(ex))

    # --------------------------------
    # Convert the reply to JSON and send it back
    # --------------------------------
    return MessageToJson(reply)