@@ -91,21 +91,99 @@ class DashboardExplorationItem:
91
91
datasources : List [Munch ]
92
92
grafana_url : str
93
93
94
- def format_compact (self ):
94
+ def format (self , with_data_details : bool = False ):
95
+ """
96
+ Generate a representation from selected information.
97
+
98
+ - dashboard
99
+ - datasources
100
+ - details
101
+ - panels/targets
102
+ - annotations
103
+ - templating
104
+ """
95
105
dbshort = OrderedDict (
96
106
title = self .dashboard .dashboard .title ,
97
107
uid = self .dashboard .dashboard .uid ,
98
108
path = self .dashboard .meta .url ,
99
109
url = urljoin (self .grafana_url , self .dashboard .meta .url ),
100
110
)
101
- item = OrderedDict (dashboard = dbshort )
111
+
112
+ dsshort = []
102
113
for datasource in self .datasources :
103
- item .setdefault ("datasources" , [])
104
- dsshort = OrderedDict (
114
+ item = OrderedDict (
105
115
uid = datasource .get ("uid" ),
106
116
name = datasource .name ,
107
117
type = datasource .type ,
108
118
url = datasource .url ,
109
119
)
110
- item ["datasources" ].append (dsshort )
111
- return item
120
+ dsshort .append (item )
121
+
122
+ data = Munch (dashboard = dbshort , datasources = dsshort )
123
+ if with_data_details :
124
+ data .details = self .collect_data_details ()
125
+ return data
126
+
127
+ def collect_data_details (self ):
128
+ """
129
+ Collect details concerned about data from dashboard information.
130
+ """
131
+
132
+ dbdetails = DashboardDetails (dashboard = self .dashboard )
133
+
134
+ ds_panels = self .collect_data_nodes (dbdetails .panels )
135
+ ds_annotations = self .collect_data_nodes (dbdetails .annotations )
136
+ ds_templating = self .collect_data_nodes (dbdetails .templating )
137
+
138
+ targets = []
139
+ for panel in ds_panels :
140
+ panel_item = self ._format_panel_compact (panel )
141
+ for target in panel .targets :
142
+ target ["_panel" ] = panel_item
143
+ targets .append (target )
144
+
145
+ response = OrderedDict (targets = targets , annotations = ds_annotations , templating = ds_templating )
146
+
147
+ return response
148
+
149
+ @staticmethod
150
+ def collect_data_nodes (element ):
151
+ """
152
+ Select all element nodes which have a "datasource" attribute.
153
+ """
154
+ element = element or []
155
+ items = []
156
+
157
+ def add (item ):
158
+ if item is not None and item not in items :
159
+ items .append (item )
160
+
161
+ for node in element :
162
+ if "datasource" in node and node ["datasource" ]:
163
+ add (node )
164
+
165
+ return items
166
+
167
+ @staticmethod
168
+ def _format_panel_compact (panel ):
169
+ """
170
+ Return a compact representation of panel information.
171
+ """
172
+ attributes = ["id" , "title" , "type" , "datasource" ]
173
+ data = OrderedDict ()
174
+ for attribute in attributes :
175
+ data [attribute ] = panel .get (attribute )
176
+ return data
177
+
178
+ @staticmethod
179
+ def _format_data_node_compact (item : Dict ) -> Dict :
180
+ """
181
+ Return a compact representation of an element concerned about data.
182
+ """
183
+ data = OrderedDict ()
184
+ data ["datasource" ] = item .get ("datasource" )
185
+ data ["type" ] = item .get ("type" )
186
+ for key , value in item .items ():
187
+ if "query" in key .lower ():
188
+ data [key ] = value
189
+ return data
0 commit comments