summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/diskprediction_cloud/common/cypher.py
blob: 7b7b60e5059d6c9f23ed8889f46adc8c821613c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import absolute_import

import time


class NodeInfo(object):
    """ Neo4j Node information """
    def __init__(self, label, domain_id, name, meta):
        self.label = label
        self.domain_id = domain_id
        self.name = name
        self.meta = meta


class CypherOP(object):
    """ Cypher Operation """

    @staticmethod
    def update(node, key, value, timestamp=int(time.time()*(1000**3))):
        result = ''
        if isinstance(node, NodeInfo):
            if key != 'time':
                cy_value = '\'%s\'' % value
            else:
                cy_value = value
            result = \
                'set %s.%s=case when %s.time >= %s then %s.%s ELSE %s end' % (
                    node.label, key, node.label, timestamp, node.label, key,
                    cy_value)
        return result

    @staticmethod
    def create_or_merge(node, timestamp=int(time.time()*(1000**3))):
        result = ''
        if isinstance(node, NodeInfo):
            meta_list = []
            if isinstance(node.meta, dict):
                for key, value in node.meta.items():
                    meta_list.append(CypherOP.update(node, key, value, timestamp))
            domain_id = '{domainId:\'%s\'}' % node.domain_id
            if meta_list:
                result = 'merge (%s:%s %s) %s %s %s' % (
                    node.label, node.label,
                    domain_id,
                    CypherOP.update(node, 'name', node.name, timestamp),
                    ' '.join(meta_list),
                    CypherOP.update(node, 'time', timestamp, timestamp))
            else:
                result = 'merge (%s:%s %s) %s %s' % (
                    node.label, node.label,
                    domain_id,
                    CypherOP.update(node, 'name', node.name, timestamp),
                    CypherOP.update(node, 'time', timestamp, timestamp))
        return result

    @staticmethod
    def add_link(snode, dnode, relationship, timestamp=None):
        result = ''
        if timestamp is None:
            timestamp = int(time.time()*(1000**3))
        if isinstance(snode, NodeInfo) and isinstance(dnode, NodeInfo):
            cy_snode = CypherOP.create_or_merge(snode, timestamp)
            cy_dnode = CypherOP.create_or_merge(dnode, timestamp)
            target = snode.label + dnode.label
            link = 'merge (%s)-[%s:%s]->(%s) set %s.time=case when %s.time >= %s then %s.time ELSE %s end' % (
                snode.label, target, relationship,
                dnode.label, target,
                target, timestamp,
                target, timestamp)
            result = '%s %s %s' % (cy_snode, cy_dnode, link)
        return result