20
20
@author: HWASSMAN
21
21
'''
22
22
23
-
24
- from collections import defaultdict
23
+ import copy
24
+ from collections import defaultdict , OrderedDict
25
25
from typing import Set , List , Dict , DefaultDict
26
26
27
+ import analytics
28
+ from utils import cond_execution_time , get_runtime_statistics
29
+
27
30
# dict.iteritems() deprecated in python 3
28
31
iterval = lambda d : (getattr (d , 'itervalues' , None ) or d .values )()
29
32
@@ -44,34 +47,38 @@ def __init__(self, jsonStr=None):
44
47
if self .topo :
45
48
self ._processMetadata (self .topo )
46
49
50
+ @get_runtime_statistics (enabled = analytics .runtime_profiling )
47
51
def _processMetadata (self , metadata ):
48
52
'''
49
53
For each component of the highest level (cluster or cluster_node) splits metadata in
50
54
sub components, filters and metrics related info maps
51
55
'''
52
-
56
+
53
57
for metaStr in metadata :
54
58
55
59
# sub components dictionary of the highest level component (cluster or cluster_node)
56
60
_components = defaultdict (set )
57
61
# filters dictionary, per sensor, per (cluster or cluster_node) component
58
62
_filters = defaultdict (list )
63
+ # ordered dictionary to store filedLabel:fieldName pairs in order as they occur in metaStr
64
+ _tags = OrderedDict ()
65
+
59
66
# name of the (cluster or cluster_node) component
60
67
label = metaStr .get ('fieldLabel' )
61
68
62
69
if label in self .__compTree .keys ():
63
70
_components = self .__compTree [label ]['componentsMap' ]
64
71
_filters = self .__compTree [label ]['filtersMap' ]
65
-
66
- self ._parse_topoJSONStr (self .__metricsDef , self .__metricsType , self .__levels , self .__ids , self .__groupKeys , _components , _filters , metaStr )
72
+
73
+ self ._parse_topoJSONStr (self .__metricsDef , self .__metricsType , self .__levels , self .__ids , self .__groupKeys , _components , _filters , _tags , metaStr )
67
74
tree_entry = {}
68
75
tree_entry ['componentsMap' ] = _components
69
76
tree_entry ['filtersMap' ] = _filters
70
77
71
78
# comp_tree[label] = tree_entry
72
79
self .__compTree [label ] = tree_entry
73
80
74
- def _parse_topoJSONStr (self , metrics , metricsType , levels , ids , groupKeys , components , filters , metaStr ):
81
+ def _parse_topoJSONStr (self , metrics , metricsType , levels , ids , groupKeys , components , filters , _tags , metaStr ):
75
82
'''
76
83
This function parses the 'node' or 'attribute' object found in the given JSON string (metaStr) in
77
84
the componets or metrics dictionary. Also the used metric filters (per sensor) will be stored
@@ -85,12 +92,20 @@ def _parse_topoJSONStr(self, metrics, metricsType, levels, ids, groupKeys, compo
85
92
86
93
# check if entity is a component
87
94
if metaStr ['type' ] == 'node' :
95
+ if field_name == "sensor" :
96
+ # remove all partialKey tags from _tags dict except parent since we step in the new sensor level metaKey
97
+ parent = _tags .popitem (last = False )
98
+ _tags .clear ()
99
+ _tags .update ([parent ])
100
+ else :
101
+ _tags [field_name ] = field_value
102
+
88
103
if field_value not in components [field_name ]:
89
104
components [field_name ].add (field_value )
90
105
# check if metaStr includes next level metaStr
91
106
if 'keys' in metaStr and len (metaStr ['keys' ]) > 0 :
92
107
for metaKey in metaStr ['keys' ]:
93
- self ._parse_topoJSONStr (metrics , metricsType , levels , ids , groupKeys , components , filters , metaKey )
108
+ self ._parse_topoJSONStr (metrics , metricsType , levels , ids , groupKeys , components , filters , _tags , metaKey )
94
109
95
110
# check if entity is a metric
96
111
elif metaStr ['type' ] == 'attribute' :
@@ -107,17 +122,17 @@ def _parse_topoJSONStr(self, metrics, metricsType, levels, ids, groupKeys, compo
107
122
if groupKey not in groupKeys :
108
123
# parse sensor relevant data f.e. groupKey, filters, levels
109
124
groupKeys [groupKey ] = len (groupKeys ) + 1
110
- tags = {}
111
- levTree = {}
112
- for i , compValue in enumerate (partKey ):
113
- for compLabel in components :
114
- if compValue in components [compLabel ]:
115
- levTree [i + 1 ] = compLabel
116
- tags [compLabel ] = compValue
117
- # if tags not in filters[sensor]:
118
- # not needed as groupkeys check will allow this to be reached only once
119
- filters [sensor ].append (tags )
125
+ group_tags = copy .deepcopy (_tags )
126
+
127
+ # if not all((value in tags.values()) for value in partKey):
128
+ # print("different key values")
129
+
130
+ filters [sensor ].append (group_tags )
120
131
if sensor not in levels :
132
+ levTree = {}
133
+ for i , tag_key in enumerate (group_tags .keys ()):
134
+ levTree [i + 1 ] = tag_key
135
+
121
136
levels [sensor ] = levTree
122
137
123
138
# parse key id
0 commit comments