Exemplo n.º 1
0
def action_add(command_data, opts):
    client_name = opts.client_name
    if client_name == None:
        print "Need client name to add model for"
        sys.exit(1)

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        sys.exit(1)

    model_name = opts.model_name
    if model_name == None:
        print "Need model name to use"
        sys.exit(1)

    default_models = command_data["conf_data"]["default_models"]
    if model_name not in default_models.keys():
        print "Invalid model name: {model_name}".format(**locals())
        sys.exit(1)

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)

    zk_client = command_data["zkdetails"]["zk_client"]
    node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)
    if not os.path.isfile(data_fpath):
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
            f = open(data_fpath)
            json = f.read()
            f.close()
            data = seldon_utils.json_to_dict(json)
            zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
        else:
            default_model_data = default_models[model_name]["config"]
            if default_model_data.has_key("inputPath"):
                default_model_data["inputPath"]=command_data["conf_data"]["seldon_models"]
            if default_model_data.has_key("outputPath"):
                default_model_data["outputPath"]=command_data["conf_data"]["seldon_models"]
            data = default_model_data
            write_data_to_file(data_fpath, data)
            zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
    else:
        f = open(data_fpath)
        json = f.read()
        f.close()
        data = seldon_utils.json_to_dict(json)
        zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
        print "Model [{model_name}] already added".format(**locals())
Exemplo n.º 2
0
def action_edit(command_data, opts):
    client_name = opts.client_name
    if client_name == None:
        print "Need client name to show the attr for"
        sys.exit(1)

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        sys.exit(1)

    zk_client = command_data["zkdetails"]["zk_client"]
    ensure_client_has_attr(zkroot, zk_client, client_name)

    data_fpath = zkroot + gdata["all_clients_node_path"] + "/" + client_name + "/attr/_data_"
    #do the edit
    from subprocess import call
    editor=seldon_utils.get_editor()
    call([editor, data_fpath])

    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if data is None:
        print "Invalid attr json!"
    else:
        write_data_to_file(data_fpath, data)
        node_path = gdata["all_clients_node_path"]+"/"+client_name+"/attr"
        zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
        show_attr(data)
Exemplo n.º 3
0
def action_configure(command_data, opts):
    client_name = opts.client_name
    if (client_name == None) or (len(client_name) == 0):
        print "Need client name to comfigure explanations for"
        sys.exit(1)

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        sys.exit(1)

    zk_client = command_data["zkdetails"]["zk_client"]
    ensure_client_has_recommendation_explanation(zkroot, zk_client,
                                                 client_name)

    data_fpath = zkroot + gdata[
        "all_clients_node_path"] + "/" + client_name + "/recommendation_explanation/_data_"
    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if opts.cache_enabled != None:
        data["cache_enabled"] = True if opts.cache_enabled == "true" else False
    if opts.default_locale != None:
        data["default_locale"] = opts.default_locale
    if opts.explanations_enabled != None:
        data[
            "explanations_enabled"] = True if opts.explanations_enabled == "true" else False

    write_data_to_file(data_fpath, data)
    node_path = gdata[
        "all_clients_node_path"] + "/" + client_name + "/recommendation_explanation"
    zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
    action_show(command_data, opts)
Exemplo n.º 4
0
def action_edit(command_data, opts):
    client_name = opts.client_name
    if client_name == None:
        print "Need client name to show the attr for"
        sys.exit(1)

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        sys.exit(1)

    zk_client = command_data["zkdetails"]["zk_client"]
    ensure_client_has_attr(zkroot, zk_client, client_name)

    data_fpath = zkroot + gdata[
        "all_clients_node_path"] + "/" + client_name + "/attr/_data_"
    #do the edit
    from subprocess import call
    editor = seldon_utils.get_editor()
    call([editor, data_fpath])

    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if data is None:
        print "Invalid attr json!"
    else:
        write_data_to_file(data_fpath, data)
        node_path = gdata["all_clients_node_path"] + "/" + client_name + "/attr"
        zk_utils.node_set(zk_client, node_path,
                          seldon_utils.dict_to_json(data))
        show_attr(data)
Exemplo n.º 5
0
def action_configure(command_data, opts):
    client_name = opts.client_name
    if (client_name == None) or (len(client_name) == 0):
        print "Need client name to comfigure explanations for"
        sys.exit(1)

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        sys.exit(1)

    zk_client = command_data["zkdetails"]["zk_client"]
    ensure_client_has_recommendation_explanation(zkroot, zk_client, client_name)

    data_fpath = zkroot + gdata["all_clients_node_path"] + "/" + client_name + "/recommendation_explanation/_data_"
    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if opts.cache_enabled != None:
        data["cache_enabled"] = True if opts.cache_enabled == "true" else False
    if opts.default_locale!= None:
        data["default_locale"] = opts.default_locale
    if opts.explanations_enabled != None:
        data["explanations_enabled"] = True if opts.explanations_enabled == "true" else False

    write_data_to_file(data_fpath, data)
    node_path = gdata["all_clients_node_path"]+"/"+client_name+"/recommendation_explanation"
    zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
    action_show(command_data, opts)
Exemplo n.º 6
0
def write_data_to_file(data_fpath, data):
    json = seldon_utils.dict_to_json(data, True) if isinstance(data,dict) else str(data)
    seldon_utils.mkdir_p(os.path.dirname(data_fpath))
    f = open(data_fpath,'w')
    f.write(json)
    f.write('\n')
    f.close()
    print "Writing data to file[{data_fpath}]".format(**locals())
Exemplo n.º 7
0
def write_data_to_file(data_fpath, data):
    json = seldon_utils.dict_to_json(data, True) if isinstance(data,dict) else str(data)
    seldon_utils.mkdir_p(os.path.dirname(data_fpath))
    f = open(data_fpath,'w')
    f.write(json)
    f.write('\n')
    f.close()
    print "Writing data to file[{data_fpath}]".format(**locals())
Exemplo n.º 8
0
def subcmd_edit(command_data):
    def have_client_name():
        if client_name == None:
            print "Need client name to edit model for"
            return False
        else:
            return True

    def have_valid_client_name():
        if not is_existing_client(zkroot, client_name):
            print "Invalid client[{client_name}]".format(**locals())
            return False
        else:
            return True

    client_name = command_data["subcmd_args"][0] if len(command_data["subcmd_args"])>0 else None
    if not have_client_name(): return
    zk_client = command_data["zkdetails"]["zk_client"]
    zkroot = command_data["zkdetails"]["zkroot"]
    if not have_valid_client_name(): return

    models_for_client_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline".format(zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name)

    models = os.listdir(models_for_client_fpath)

    for idx,model in enumerate(models):
        print "    [{idx}] {model}".format(**locals())
    q="Choose model: "
    model_idx = None
    try:
        model_idx = int(raw_input(q))
    except ValueError:
        pass

    model_name = models[model_idx] if model_idx != None and model_idx>=0 and model_idx<len(models) else None

    if model_name == None:
        print "Invaild model"
        return

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)

    #do the edit
    editor=seldon_utils.get_editor()
    call([editor, data_fpath])

    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if data is None:
        print "Invalid model json!"
    else:
        write_data_to_file(data_fpath, data)
        node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)
        pp(node_path)
        zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
Exemplo n.º 9
0
def subcmd_add(command_data):
    client_name = command_data["subcmd_args"][0] if len(
        command_data["subcmd_args"]) > 0 else None
    if client_name == None:
        print "Need client name to add model for"
        return

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        return

    default_models = command_data["default_models"]
    models = default_models.keys()
    for idx, model in enumerate(models):
        print "    [{idx}] {model}".format(**locals())
    q = "Choose model: "
    model_idx = None
    try:
        model_idx = int(raw_input(q))
    except ValueError:
        pass

    model_name = models[
        model_idx] if model_idx != None and model_idx >= 0 and model_idx < len(
            models) else None

    if model_name == None:
        print "Invaild model"
        return

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(
        zkroot=zkroot,
        all_clients_node_path=gdata["all_clients_node_path"],
        client_name=client_name,
        model_name=model_name)

    zk_client = command_data["zkdetails"]["zk_client"]
    if not os.path.isfile(data_fpath):
        node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(
            all_clients_node_path=gdata["all_clients_node_path"],
            client_name=client_name,
            model_name=model_name)
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
        else:
            default_model_data = default_models[model_name]["config"]
            if default_model_data.has_key("inputPath"):
                default_model_data["inputPath"] = command_data["seldon_models"]
            if default_model_data.has_key("outputPath"):
                default_model_data["outputPath"] = command_data[
                    "seldon_models"]
            data = default_model_data
            write_data_to_file(data_fpath, data)
            zk_utils.node_set(zk_client, node_path,
                              seldon_utils.dict_to_json(data))
    else:
        print "Model [{model_name}] already added".format(**locals())
Exemplo n.º 10
0
def action_edit(command_data, opts, extra_args):
    zkroot = command_data["zkdetails"]["zkroot"]

    def get_valid_client():
        client_name = opts.client_name
        if client_name == None:
            print "Need client name to show models for"
            sys.exit(1)

        if not is_existing_client(zkroot, client_name):
            print "Invalid client[{client_name}]".format(**locals())
            sys.exit(1)
        return client_name

    def get_valid_model():
        model_name = opts.model_name
        if model_name == None:
            print "Need model name to use"
            sys.exit(1)

        default_models = command_data["conf_data"]["default_models"]
        if model_name not in default_models.keys():
            print "Invalid model name: {model_name}".format(**locals())
            sys.exit(1)
        return model_name

    client_name = get_valid_client()
    model_name = get_valid_model()

    zk_client = command_data["zkdetails"]["zk_client"]

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(
        zkroot=zkroot,
        all_clients_node_path=gdata["all_clients_node_path"],
        client_name=client_name,
        model_name=model_name)

    #do the edit
    editor = seldon_utils.get_editor()
    call([editor, data_fpath])

    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if data is None:
        print "Invalid model json!"
    else:
        write_data_to_file(data_fpath, data)
        node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(
            all_clients_node_path=gdata["all_clients_node_path"],
            client_name=client_name,
            model_name=model_name)
        pp(node_path)
        zk_utils.node_set(zk_client, node_path,
                          seldon_utils.dict_to_json(data))
Exemplo n.º 11
0
def ensure_client_has_recommendation_explanation(zkroot, zk_client, client_name):
    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/recommendation_explanation/_data_".format(zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name)
    if not os.path.isfile(data_fpath):
        node_path = gdata["all_clients_node_path"]+"/"+client_name+"/recommendation_explanation"
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
        else:
            default_rec_exp = '{"cache_enabled":true,"default_locale":"us-en","explanations_enabled":false}'
            data = seldon_utils.json_to_dict(default_rec_exp)
            write_data_to_file(data_fpath, data)
            zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
Exemplo n.º 12
0
def ensure_client_has_attr(zkroot, zk_client, client_name):
    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/attr/_data_".format(
            zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name)
    if not os.path.isfile(data_fpath):
        node_path = gdata["all_clients_node_path"]+"/"+client_name+"/attr"
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
        else:
            default_attr_json = '{"types":[{"type_attrs":[{"name":"title","value_type":"string"}],"type_id":1,"type_name":"defaulttype"}]}'
            data = seldon_utils.json_to_dict(default_attr_json)
            write_data_to_file(data_fpath, data)
            zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
Exemplo n.º 13
0
def action_edit(command_data, opts):
    zkroot = command_data["zkdetails"]["zkroot"]
    def get_valid_client():
        client_name = opts.client_name
        if client_name == None:
            print "Need client name to show models for"
            sys.exit(1)

        if not is_existing_client(zkroot, client_name):
            print "Invalid client[{client_name}]".format(**locals())
            sys.exit(1)
        return client_name

    def get_valid_model():
        model_name = opts.model_name
        if model_name == None:
            print "Need model name to use"
            sys.exit(1)

        default_models = command_data["conf_data"]["default_models"]
        if model_name not in default_models.keys():
            print "Invalid model name: {model_name}".format(**locals())
            sys.exit(1)
        return model_name

    client_name = get_valid_client()
    model_name = get_valid_model()

    zk_client = command_data["zkdetails"]["zk_client"]

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)

    #do the edit
    editor=seldon_utils.get_editor()
    call([editor, data_fpath])

    f = open(data_fpath)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    if data is None:
        print "Invalid model json!"
    else:
        write_data_to_file(data_fpath, data)
        node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)
        pp(node_path)
        zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
Exemplo n.º 14
0
def store_json(command_data,opts):
    f = open(opts.json)
    json = f.read()
    f.close()
    data = seldon_utils.json_to_dict(json)

    zk_client = command_data["zkdetails"]["zk_client"]
    zkroot = command_data["zkdetails"]["zkroot"]

    data_fpath = zkroot + gdata["all_clients_node_path"] + "/" + opts.client_name + "/attr/_data_"
    if data is None:
        print "Invalid attr json!"
        sys.exit(1)
    else:
        write_data_to_file(data_fpath, data)
        node_path = gdata["all_clients_node_path"]+"/"+opts.client_name+"/attr"
        zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
Exemplo n.º 15
0
def ensure_client_has_recommendation_explanation(zkroot, zk_client,
                                                 client_name):
    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/recommendation_explanation/_data_".format(
        zkroot=zkroot,
        all_clients_node_path=gdata["all_clients_node_path"],
        client_name=client_name)
    if not os.path.isfile(data_fpath):
        node_path = gdata[
            "all_clients_node_path"] + "/" + client_name + "/recommendation_explanation"
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
        else:
            default_rec_exp = '{"cache_enabled":true,"default_locale":"us-en","explanations_enabled":false}'
            data = seldon_utils.json_to_dict(default_rec_exp)
            write_data_to_file(data_fpath, data)
            zk_utils.node_set(zk_client, node_path,
                              seldon_utils.dict_to_json(data))
Exemplo n.º 16
0
def subcmd_add(command_data):
    client_name = command_data["subcmd_args"][0] if len(command_data["subcmd_args"])>0 else None
    if client_name == None:
        print "Need client name to add model for"
        return

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        return

    default_models = command_data["default_models"]
    models = default_models.keys()
    for idx,model in enumerate(models):
        print "    [{idx}] {model}".format(**locals())
    q="Choose model: "
    model_idx = None
    try:
        model_idx = int(raw_input(q))
    except ValueError:
        pass

    model_name = models[model_idx] if model_idx != None and model_idx>=0 and model_idx<len(models) else None

    if model_name == None:
        print "Invaild model"
        return

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(zkroot=zkroot,all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)

    zk_client = command_data["zkdetails"]["zk_client"]
    if not os.path.isfile(data_fpath):
        node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(all_clients_node_path=gdata["all_clients_node_path"],client_name=client_name,model_name=model_name)
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
        else:
            default_model_data = default_models[model_name]["config"]
            if default_model_data.has_key("inputPath"):
                default_model_data["inputPath"]=command_data["seldon_models"]
            if default_model_data.has_key("outputPath"):
                default_model_data["outputPath"]=command_data["seldon_models"]
            data = default_model_data
            write_data_to_file(data_fpath, data)
            zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))
    else:
        print "Model [{model_name}] already added".format(**locals())
Exemplo n.º 17
0
def action_add(command_data, opts, extra_args):
    client_name = opts.client_name
    if client_name == None:
        print "Need client name to add model for"
        sys.exit(1)

    zkroot = command_data["zkdetails"]["zkroot"]
    if not is_existing_client(zkroot, client_name):
        print "Invalid client[{client_name}]".format(**locals())
        sys.exit(1)

    model_name = opts.model_name
    if model_name == None:
        print "Need model name to use"
        sys.exit(1)

    default_models = command_data["conf_data"]["default_models"]
    if model_name not in default_models.keys():
        print "Invalid model name: {model_name}".format(**locals())
        sys.exit(1)

    config_args = get_config_args(extra_args)

    data_fpath = "{zkroot}{all_clients_node_path}/{client_name}/offline/{model_name}/_data_".format(
        zkroot=zkroot,
        all_clients_node_path=gdata["all_clients_node_path"],
        client_name=client_name,
        model_name=model_name)

    zk_client = command_data["zkdetails"]["zk_client"]
    node_path = "{all_clients_node_path}/{client_name}/offline/{model_name}".format(
        all_clients_node_path=gdata["all_clients_node_path"],
        client_name=client_name,
        model_name=model_name)
    if not os.path.isfile(data_fpath):
        if zk_client.exists(node_path):
            write_node_value_to_file(zk_client, zkroot, node_path)
            f = open(data_fpath)
            json = f.read()
            f.close()
            data = seldon_utils.json_to_dict(json)
        else:
            default_model_data = default_models[model_name]["config"]
            if default_model_data.has_key("inputPath"):
                default_model_data["inputPath"] = command_data["conf_data"][
                    "seldon_models"]
            if default_model_data.has_key("outputPath"):
                default_model_data["outputPath"] = command_data["conf_data"][
                    "seldon_models"]
            data = default_model_data
    else:
        f = open(data_fpath)
        json = f.read()
        f.close()
        data = seldon_utils.json_to_dict(json)
        print "Model [{model_name}] already added".format(**locals())

    for k in config_args:
        print "adding config ", k, ":", config_args[k]
        data[k] = config_args[k]
    write_data_to_file(data_fpath, data)
    zk_utils.node_set(zk_client, node_path, seldon_utils.dict_to_json(data))