-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_query.py
366 lines (339 loc) · 13.3 KB
/
_query.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import math
import visdcc
import pandas
import flatten_json
import dash.html as html
import dash_ace
import dash
import requests
import typing
import pathlib
import json
import re
import math
import diskcache
from dash_bootstrap_templates import ThemeSwitchAIO
import dash_bootstrap_components as bootstrap
import dash_loading_spinners as spinners
import __utilities__ as utilities
from __global__ import *
from __errors__ import *
from _metadata import get_metadata
_callback_manager = dash.DiskcacheManager(diskcache.Cache())
@app.callback(
dash.Output('net', 'options'),
dash.Input('graphSettings', 'data'),
)
def _load_graph_settings(settings):
return settings
@app.callback(
dash.Output('graphSettings', 'data'),
dash.Input('url', 'pathname'),
dash.State('graphSettings', 'data'),
)
def _load_node_labels(pathname, settings):
if pathname != QUERY_DIRECTORY:
raise dash.exceptions.PreventUpdate
if 'groups' not in settings:
settings['groups'] = {}
color_map = settings['groups']
graphs = get_metadata('Metadata', 'Graph')
for graph in graphs:
for node in graph['Vertices']:
if node['Label'] not in color_map:
color_map[node['Label']] = {
'borderWidth': 0,
'color': {'background': '#97C2FC'},
}
return settings
@app.callback(
dash.Output('queryResults', 'data'),
dash.Output('graphData', 'data'),
dash.Input('runButton', 'n_clicks'),
dash.State('queryInput', 'value'),
dash.State('node-limit', 'data'),
prevent_initial_call=True,
manager=_callback_manager,
background=True,
running=[
(dash.Output('runButton', 'disabled'), True, False),
(dash.Output('outputPaneSpinner', 'children'), spinners.Grid(color='#325d88'), None),
(dash.Output('net', 'style'), {'display': 'none'}, {'display': 'block'}),
(dash.Output('tableDiv', 'style'), {'display': 'none'}, {'display': 'block'}),
]
)
def _execute_query(n_clicks, query_input, node_limit):
settings_file = pathlib.Path(__file__).parent / 'settings/graphix.json'
if not settings_file.exists():
raise FileNotFoundError(settings_file.name)
# Don't proceed if our query-text is empty.
if query_input is None or (query_input is str and len(query_input) == 0) or n_clicks < 1:
raise dash.exceptions.PreventUpdate
# Grab our client information.
with settings_file.open('r') as fp:
settings_json = json.load(fp)
cluster_uri = f"http://{settings_json['cluster']['address']}:" \
f"{settings_json['cluster']['port']}/query/service"
# Issue our query.
api_parameters = {'statement': 'SET `graphix.compiler.add-context` "true"; ' + query_input}
response = requests.post(cluster_uri, api_parameters).json()
if response['status'] != 'success':
raise GraphixStatementError(response)
if not response.get('results'):
return {}, {'nodes': [], 'edges': []}
# Store variable names representing nodes and edges.
node_variables = {
node["graphElement"]["variable"]: node["graphElement"]["labels"][0]
for node in response["graphix"]["patterns"]["vertices"]
}
edge_variables = {
edge["graphElement"]["variable"]: {
"label": edge["graphElement"]["labels"][0],
"from": edge["edgeElement"]["leftVertex"]["variable"],
"to": edge["edgeElement"]["rightVertex"]["variable"]
}
for edge in response["graphix"]["patterns"]["edges"]
}
if not edge_variables:
edge_variables = {
edge["graphElement"]["variable"]: {
"label": edge["graphElement"]["labels"][0],
"from": edge["edgeElement"]["leftVertex"]["variable"],
"to": edge["edgeElement"]["rightVertex"]["variable"]
}
for edge in response["graphix"]["patterns"]["paths"]
}
# Turn query result into nodes and edges.
idx = 0
nodes = {}
edges = []
for entry in response['results']:
# Go through each node, cache each node's id.
id_dict = {}
for variable, node in entry.items():
if idx >= node_limit:
break
if variable[0] == '$':
variable = '#' + variable[1:]
if variable in node_variables:
node_tuple = json.dumps(node)
if node_tuple in nodes:
id_dict[variable] = nodes[node_tuple]["id"]
else:
idx += 1
id_dict[variable] = idx
label = node['name'] if 'name' in node else f"{node_variables[variable]}-{idx}"
title_json = json.dumps(node, indent=2)
nodes[node_tuple] = {
"id": idx,
"data": node,
"label": "\n".join(label.split()),
"group": node_variables[variable],
"title": f"<pre><code>{title_json}</code></pre>"
}
# Go through each edge.
for variable, edge in entry.items():
if variable[0] == '$':
variable = '#' + variable[1:]
if variable in edge_variables:
edge_definition = edge_variables[variable]
label = edge_definition["label"]
if edge_definition["from"] not in id_dict or edge_definition["to"] not in id_dict:
continue
title_json = json.dumps(edge, indent=2)
edges.append({
"from": id_dict[edge_definition["from"]],
"to": id_dict[edge_definition["to"]],
"data": edge,
"label": label,
"title": f"<pre><code>{title_json}</code></pre>"
})
return response['results'], {'nodes': list(nodes.values()), 'edges': edges}
def get_name(obj):
return obj['name']
@app.callback(
dash.Output('tableViewer', 'data'),
dash.Output('tableViewer', 'columns'),
dash.Input('queryResults', 'data'),
dash.Input('outputTabs', 'active_tab')
)
def _update_table(query_results, active_tab):
if active_tab != 'tableOutputTab':
raise dash.exceptions.PreventUpdate
# Our results need to be flattened for use in a table.
table_data = [flatten_json.flatten(x) for x in query_results]
column_names = set(k for kk in [x.keys() for x in table_data] for k in kk)
table_columns = [{"name": i, "id": i} for i in column_names]
table_columns.sort(key=get_name)
for table_row in table_data:
del_list = []
for key, value in table_row.items():
if type(value) is list:
del_list.append(key)
for key in del_list:
del table_row[key]
return table_data, table_columns
@app.callback(
dash.Output('net', 'data'),
dash.Output('outputTabs', 'active_tab'),
dash.Input('graphData', 'data'),
dash.Input('outputTabs', 'active_tab')
)
def _update_graph(graph_data, active_tab):
if active_tab != 'graphOutputTab':
raise dash.exceptions.PreventUpdate
if not graph_data['nodes']:
print('Cannot show data as graph, switch to Table Viewer tab...')
return graph_data, 'tableOutputTab'
return graph_data, 'graphOutputTab'
@app.callback(
dash.Output('queryInput', 'theme'),
dash.Output('tableViewer', 'style_header'),
dash.Output('tableViewer', 'style_data'),
dash.Output('tableViewer', 'style_data_conditional'),
dash.Input(ThemeSwitchAIO.ids.switch('theme'), 'value')
)
def _update_theme(is_light):
if is_light:
query_input_theme = 'textmate'
style_header = {
'backgroundColor': 'rgb(220, 220, 220)',
'color': 'black',
'fontWeight': 'bold'
}
style_data = {
'color': 'black',
'backgroundColor': 'white'
}
style_data_conditional = [
{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(240, 240, 240)',
}
]
else:
query_input_theme = 'twilight'
style_header = {
'backgroundColor': 'rgb(30, 30, 30)',
'color': 'white',
'fontWeight': 'bold'
}
style_data = {
'backgroundColor': 'rgb(70, 70, 70)',
'color': 'white'
}
style_data_conditional = [
{
'if': {'row_index': 'odd'},
'backgroundColor': 'rgb(50, 50, 50)',
}
]
return query_input_theme, style_header, style_data, style_data_conditional
def build_page():
def _build_input_pane():
return bootstrap.Card(
className='vh-100',
children=[
bootstrap.CardBody(
children=[
dash_ace.DashAceEditor(
id='queryInput',
mode=None,
theme='textmate',
value='FROM GRAPH Gelp.GelpGraph\n\t(r:Review)-[a:ABOUT]->(b:Business)\nSELECT r, a, b;',
showGutter=True,
showPrintMargin=False,
maxLines=math.inf,
syntaxKeywords={
'support.function': '|'.join(x for x in utilities.scrape_functions().keys()),
'keyword.other': '|'.join(x for x in utilities.scrape_keywords()),
'constant.language': 'true|false'
},
className='h-100 w-100'
),
html.Div(
className='position-relative',
children=[
html.Button(
id='runButton',
className='btn btn-primary position-absolute bottom-0 end-0 queryButton',
children=[
html.Span(className="bi bi-play", style={'font-size': '16px'}),
" Run "
],
type='button',
n_clicks=0
),
]
)
]
)
]
)
def _build_output_pane():
return bootstrap.Card(
id='outputPane',
className='vh-100',
children=[
bootstrap.CardBody(
className='position-relative',
children=[
bootstrap.Tabs(
id='outputTabs',
children=[
bootstrap.Tab(
tab_id='graphOutputTab',
children=[
visdcc.Network(
id='net',
data={
'nodes': [
{'id': 1},
{'id': 2},
],
'edges': [
{'from': 1, 'to': 2},
],
},
options={
'height': '600px',
'width': '100%',
}
),
],
label="Graph Viewer",
),
bootstrap.Tab(
tab_id='tableOutputTab',
children=[
html.Div(
dash.dash_table.DataTable(
id='tableViewer',
),
id='tableDiv'
)
],
label="Table Viewer",
)
]
),
html.Div(
id='outputPaneSpinner',
className='position-absolute top-50 start-50 translate-middle',
)
]
)
]
)
return bootstrap.Container(
children=[
bootstrap.Row(
children=[
bootstrap.Col(_build_input_pane(), width=6),
bootstrap.Col(_build_output_pane(), width=6)
],
className="g-2",
)
],
fluid=True
)