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 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,6 +47,7 @@ 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
@@ -56,22 +60,24 @@ def _processMetadata(self, metadata):
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 ()
59
65
# name of the (cluster or cluster_node) component
60
66
label = metaStr .get ('fieldLabel' )
61
67
62
68
if label in self .__compTree .keys ():
63
69
_components = self .__compTree [label ]['componentsMap' ]
64
70
_filters = self .__compTree [label ]['filtersMap' ]
65
71
66
- self ._parse_topoJSONStr (self .__metricsDef , self .__metricsType , self .__levels , self .__ids , self .__groupKeys , _components , _filters , metaStr )
72
+ self ._parse_topoJSONStr (self .__metricsDef , self .__metricsType , self .__levels , self .__ids , self .__groupKeys , _components , _filters , _tags , metaStr )
67
73
tree_entry = {}
68
74
tree_entry ['componentsMap' ] = _components
69
75
tree_entry ['filtersMap' ] = _filters
70
76
71
77
# comp_tree[label] = tree_entry
72
78
self .__compTree [label ] = tree_entry
73
79
74
- def _parse_topoJSONStr (self , metrics , metricsType , levels , ids , groupKeys , components , filters , metaStr ):
80
+ def _parse_topoJSONStr (self , metrics , metricsType , levels , ids , groupKeys , components , filters , _tags , metaStr ):
75
81
'''
76
82
This function parses the 'node' or 'attribute' object found in the given JSON string (metaStr) in
77
83
the componets or metrics dictionary. Also the used metric filters (per sensor) will be stored
@@ -85,12 +91,20 @@ def _parse_topoJSONStr(self, metrics, metricsType, levels, ids, groupKeys, compo
85
91
86
92
# check if entity is a component
87
93
if metaStr ['type' ] == 'node' :
94
+ if field_name == "sensor" :
95
+ # remove all partialKey tags from _tags dict except parent since we step in the new sensor level metaKey
96
+ parent = _tags .popitem (last = False )
97
+ _tags .clear ()
98
+ _tags .update ([parent ])
99
+ else :
100
+ _tags [field_name ] = field_value
101
+
88
102
if field_value not in components [field_name ]:
89
103
components [field_name ].add (field_value )
90
104
# check if metaStr includes next level metaStr
91
105
if 'keys' in metaStr and len (metaStr ['keys' ]) > 0 :
92
106
for metaKey in metaStr ['keys' ]:
93
- self ._parse_topoJSONStr (metrics , metricsType , levels , ids , groupKeys , components , filters , metaKey )
107
+ self ._parse_topoJSONStr (metrics , metricsType , levels , ids , groupKeys , components , filters , _tags , metaKey )
94
108
95
109
# check if entity is a metric
96
110
elif metaStr ['type' ] == 'attribute' :
@@ -107,17 +121,17 @@ def _parse_topoJSONStr(self, metrics, metricsType, levels, ids, groupKeys, compo
107
121
if groupKey not in groupKeys :
108
122
# parse sensor relevant data f.e. groupKey, filters, levels
109
123
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 )
124
+ group_tags = copy .deepcopy (_tags )
125
+
126
+ # if not all((value in tags.values()) for value in partKey):
127
+ # rint("different key values")
128
+
129
+ filters [sensor ].append (group_tags )
120
130
if sensor not in levels :
131
+ levTree = {}
132
+ for i , tag_key in enumerate (group_tags .keys ()):
133
+ levTree [i + 1 ] = tag_key
134
+
121
135
levels [sensor ] = levTree
122
136
123
137
# parse key id
0 commit comments