def _query_id_by_key(self, label, key, val,
                         cond_dict={}):
        params = self._detail_setup(
            label, None, key, val, cond_dict,
            None, None, None, None)
        result = self._send_cmd(params)

        try:
            encoded = json.loads(result.text, encoding='utf-8')
            if "id" in encoded["results"][0]["data"][0]["graph"]["nodes"][0]:
                pass
        except Exception:
            msg = "Neo4jApi Get no results ( {0}.{1} )".format(
                self.__class__.__name__,
                self._query_id_by_key.__name__)
            if not ifttt:
                _logger.error("Get no results from DiskProphet.")
            else:
                print "Get no results from DiskProphet."
            raise dpclient.DbError(msg)
        else:
            encoded_list = []
            for i in encoded["results"][0]["data"]:
                encoded_list.append(str(i["graph"]["nodes"][0]["id"]))

            return encoded_list
    def query_nodes(self, label="VMVirtualMachine", key="domainId", val=[],
                    cond_dict={}):
        result = self._send_cmd(self._detail_setup(
            label, None, key, val, cond_dict,
            None, None, None, None))

        try:
            encoded = json.loads(result.text, encoding='utf-8')
            if "id" in encoded["results"][0]["data"][0]["graph"]["nodes"][0]:
                pass
        except Exception:
            msg = "Neo4jApi Get no results ( {0}.{1} )".format(
                self.__class__.__name__,
                self.query_nodes.__name__)
            if not ifttt:
                _logger.error("Get no results from DiskProphet.")
            else:
                print "Get no results from DiskProphet."
            raise dpclient.DbError(msg)
        else:
            encoded_list = []
            for i in encoded["results"][0]["data"]:
                encoded_list.append(i["graph"]["nodes"][0]["properties"])

        return encoded_list
 def merge_dict(self, dict1, dict2):
     if not dict1:
         return dict2
     elif not dict2:
         return dict1
     elif type(dict1) is list and type(dict2) is list:
         for val in dict2:
             if val not in dict1:
                 dict1.append(val)
         return dict1
     elif type(dict1) is str and type(dict2) is str:
         if dict1 != dict2:
             dict1 += ", {0}".format(dict2)
         return dict1
     else:
         try:
             merged_dict = {}
             dict1_dup = {}
             dict2_dup = {}
             for key, val in dict1.iteritems():
                 if key not in dict2:
                     merged_dict.update({key: val})
                 else:
                     dict1_dup.update({key: val})
             for key, val in dict2.iteritems():
                 if key not in dict1:
                     merged_dict.update({key: val})
                 else:
                     dict2_dup.update({key: val})
             for key in dict1_dup.keys():
                 merged_dict.update(
                     {key: self.merge_dict(dict1_dup[key], dict2_dup[key])})
             return merged_dict
         except Exception:
             msg = "Merge dict is failed ( {0}.{1} )".format(
                 self.__class__.__name__, self.merge_dict.__name__)
             raise dpclient.DbError(msg)
    def get_disklist(self):
        disk_watchdog = []
        disk_list = {}

        start_time = int(time.time() * 1000000000) - (3600 * 24 * 1000000000)
        cond_dict = {"time": start_time}

        result = self.query_relationship(cond_dict)

        try:
            for key1, val1 in result.iteritems():
                for key2, val2 in val1["Datastores"].iteritems():
                    for key3, val3 in val2["Disks"].iteritems():
                        if key1 in self._datastore_hosts:
                            host_domainid = self._datastore_hosts[key1][0]
                        else:
                            host_domainid = key1
                        if key3 not in disk_list:
                            disk_list.update({key3: [host_domainid]})
                            disk_watchdog.append(key3)
                        elif host_domainid not in disk_list[key3]:
                            disk_list[key3].append(host_domainid)
        except Exception:
            msg = "Neo4JApi Get wrong data ( {0}.{1} )".format(
                self.__class__.__name__, self.get_disklist.__name__)
            if not ifttt:
                _logger.error(
                    "An error occurred when analysing DiskProphet data.")
            else:
                print "An error occurred when analysing DiskProphet data."
            raise dpclient.DbError(msg)

        self.query_prediction(disk_watchdog)
        diskinfo = self.query_diskinfo(disk_watchdog)

        for key1, val1 in disk_list.iteritems():
            if key1 in self._prediction_result:
                for val2 in self._prediction_result[key1].values():
                    if val1[0] in self._domainid_hosts:
                        if self._domainid_hosts[
                                val1[0]] not in self._host_info:
                            self._host_info.update(
                                {self._domainid_hosts[val1[0]]: {}})
                        target = self._host_info[self._domainid_hosts[val1[0]]]
                        for key3, val3 in val2.iteritems():
                            if self._domainid_hosts[
                                    val1[0]] not in self._host_status:
                                self._host_status.update({
                                    self._domainid_hosts[val1[0]]:
                                    val3["near_failure"][0]
                                })
                            else:
                                if "Bad" == val3["near_failure"][0] or\
                                        ("Warning" == val3["near_failure"][0] and "Bad" != self._host_status[self._domainid_hosts[val1[0]]]) or\
                                        ("Good" == val3["near_failure"][0] and "Bad" != self._host_status[self._domainid_hosts[val1[0]]] and
                                         "Warning" != self._host_status[self._domainid_hosts[val1[0]]]):
                                    if val3["near_failure"][
                                            0] != self._host_status[
                                                self._domainid_hosts[val1[0]]]:
                                        self._host_status.update({
                                            self._domainid_hosts[val1[0]]:
                                            val3["near_failure"][0]
                                        })
                                        target.clear()
                            if val3["near_failure"][0] == self._host_status[
                                    self._domainid_hosts[val1[0]]]:
                                if val3["disk_name"][0] not in target:
                                    target.update({val3["disk_name"][0]: {}})
                                if not ifttt:
                                    target[val3["disk_name"][0]].update({
                                        "near_failure":
                                        val3["near_failure"][0],
                                        "disk_name":
                                        diskinfo[key1]
                                    })
                                else:
                                    target[val3["disk_name"][0]].update({
                                        "near_failure":
                                        val3["near_failure"][0],
                                        "disk_name":
                                        diskinfo[key1],
                                        "time":
                                        key3
                                    })
    def query_by_label_rels(self, vms=[], label="VMVirtualMachine",
                            rels=None, cond_dict={},
                            tag="domainId", output_tag="domainId"):
        if rels:
            left_rel = ""
            right_rel = ""
            result_type = " n"
            for idx, rel in enumerate(rels):
                left_rel += "-[r{1}:{0}]->(t{1})".format(rel, idx)
                right_rel += "<-[r{1}:{0}]-(t{1})".format(rel, idx)
                result_type += ",r{0},t{0}".format(idx)
            _rels = [left_rel, right_rel]
        else:
            _rels = ["-[r*]->(t)", "<-[r*]-(t)"]
            result_type = " n,r,t"
        params = ""
        arranged = {}

        for i in _rels:
            params = self._detail_setup(
                label, i, None, None, None,
                self._query_id_by_key(label, tag, vms, cond_dict),
                result_type, None, params)

        result = self._send_cmd(params)

        try:
            encoded = json.loads(result.text, encoding='utf-8')
            if tag in encoded["results"][0][
                    "data"][0]["graph"]["nodes"][0]["properties"]:
                pass
        except Exception:
            msg = "Neo4jApi Get no results ( {0}.{1} )".format(
                self.__class__.__name__,
                self.query_by_label_rels.__name__)
            if not ifttt:
                _logger.error("Get no results from DiskProphet.")
            else:
                print "Get no results from DiskProphet."
            raise dpclient.DbError(msg)
        else:
            labels_pool = {}
            target = {}
            key = ""
            count = 0
            for i in encoded["results"][0]["data"]:
                for j in i["graph"]["nodes"]:
                    labels_pool[str(j["labels"][0])] = str(
                        j["properties"][output_tag])

                key = labels_pool.get(label)
                if key in arranged:
                    pass
                else:
                    arranged[key] = {}
                target = arranged.get(key)
                count += 1

                if "VMDatastore" in labels_pool:
                    key = labels_pool.get("VMDatastore")
                    if "Datastores" not in target:
                        target["Datastores"] = {}
                    if key in target["Datastores"]:
                        pass
                    else:
                        target["Datastores"][key] = {"Disks": {}}

                if "VMDisk" in labels_pool:
                    if "VMDatastore" in labels_pool:
                        key = labels_pool.get("VMDatastore")
                    else:
                        key = "None_#{0}".format(count)
                    if "Datastores" not in target:
                        target["Datastores"] = {}
                    if key in target["Datastores"]:
                        pass
                    else:
                        target["Datastores"][key] = {"Disks": {}}
                    target["Datastores"][key]["Disks"][
                        labels_pool.get("VMDisk")] = {"Groups": ""}

                if "VMVSanDiskGroup" in labels_pool:
                    if "VMDisk" in labels_pool:
                        key_disks = labels_pool.get("VMDisk")
                    else:
                        key_disks = "None_#{0}".format(count)
                    if "VMDatastore" in labels_pool:
                        key = labels_pool.get("VMDatastore")
                    else:
                        key = "None_#{0}".format(count)
                    if "Datastores" not in target:
                        target["Datastores"] = {}
                    if key not in target["Datastores"]:
                        target["Datastores"][key] = {"Disks": {}}
                    if key_disks not in target["Datastores"][key]["Disks"]:
                        target["Datastores"][key]["Disks"][key_disks] = {
                            "Groups": ""}
                    target["Datastores"][key]["Disks"][key_disks]["Groups"] = \
                        labels_pool.get("VMVSanDiskGroup")

                if "VMVirtualMachine" in labels_pool:
                    key = labels_pool.get("VMVirtualMachine")
                    if "VMs" not in target:
                        target["VMs"] = []
                    if key not in target["VMs"]:
                        target["VMs"].append(key)

                if "VMClusterCenter" in labels_pool:
                    if "Clusters" not in target:
                        target["Clusters"] = {}
                    target["Clusters"][labels_pool.get(
                        "VMClusterCenter")] = {"Types": "Cluster"}

                if "VMHost" in labels_pool:
                    if "Clusters" not in target:
                        target["Clusters"] = {}
                    target["Clusters"][labels_pool.get(
                        "VMHost")] = {"Types": "Host"}

                labels_pool.clear()

            return arranged