in src/graph_notebook/magics/graph_magic.py [0:0]
def gremlin(self, line, cell, local_ns: dict = None):
parser = argparse.ArgumentParser()
parser.add_argument('query_mode', nargs='?', default='query',
help='query mode (default=query) [query|explain|profile]')
parser.add_argument('-p', '--path-pattern', default='', help='path pattern')
parser.add_argument('-g', '--group-by', type=str, default='T.label',
help='Property used to group nodes (e.g. code, T.region) default is T.label')
parser.add_argument('-d', '--display-property', type=str, default='T.label',
help='Property to display the value of on each node, default is T.label')
parser.add_argument('-de', '--edge-display-property', type=str, default='T.label',
help='Property to display the value of on each edge, default is T.label')
parser.add_argument('-t', '--tooltip-property', type=str, default='',
help='Property to display the value of on each node tooltip. If not specified, tooltip '
'will default to the node label value.')
parser.add_argument('-te', '--edge-tooltip-property', type=str, default='',
help='Property to display the value of on each edge tooltip. If not specified, tooltip '
'will default to the edge label value.')
parser.add_argument('-l', '--label-max-length', type=int, default=10,
help='Specifies max length of vertex label, in characters. Default is 10')
parser.add_argument('-le', '--edge-label-max-length', type=int, default=10,
help='Specifies max length of edge labels, in characters. Default is 10')
parser.add_argument('--store-to', type=str, default='', help='store query result to this variable')
parser.add_argument('--ignore-groups', action='store_true', default=False, help="Ignore all grouping options")
parser.add_argument('--no-results', action='store_false', default=True,
help='Display only the result count. If not used, all query results will be displayed in '
'the profile report by default.')
parser.add_argument('--chop', type=int, default=250,
help='Property to specify max length of profile results string. Default is 250')
parser.add_argument('--serializer', type=str, default='application/json',
help='Specify how to serialize results. Allowed values are any of the valid MIME type or '
'TinkerPop driver "Serializers" enum values. Default is application/json')
parser.add_argument('--indexOps', action='store_true', default=False,
help='Show a detailed report of all index operations.')
parser.add_argument('-sp', '--stop-physics', action='store_true', default=False,
help="Disable visualization physics after the initial simulation stabilizes.")
parser.add_argument('-sd', '--simulation-duration', type=int, default=1500,
help='Specifies maximum duration of visualization physics simulation. Default is 1500ms')
parser.add_argument('--silent', action='store_true', default=False, help="Display no query output.")
parser.add_argument('--no-scroll', action='store_true', default=False,
help="Display the entire output without a scroll bar.")
args = parser.parse_args(line.split())
mode = str_to_query_mode(args.query_mode)
logger.debug(f'Arguments {args}')
if args.no_scroll:
gremlin_layout = UNRESTRICTED_LAYOUT
else:
gremlin_layout = DEFAULT_LAYOUT
if not args.silent:
tab = widgets.Tab()
children = []
titles = []
first_tab_output = widgets.Output(layout=gremlin_layout)
children.append(first_tab_output)
if mode == QueryMode.EXPLAIN:
res = self.client.gremlin_explain(cell)
res.raise_for_status()
query_res = res.content.decode('utf-8')
if not args.silent:
gremlin_metadata = build_gremlin_metadata_from_query(query_type='explain', results=query_res, res=res)
titles.append('Explain')
if 'Neptune Gremlin Explain' in query_res:
first_tab_html = pre_container_template.render(content=query_res)
else:
first_tab_html = pre_container_template.render(content='No explain found')
elif mode == QueryMode.PROFILE:
logger.debug(f'results: {args.no_results}')
logger.debug(f'chop: {args.chop}')
logger.debug(f'serializer: {args.serializer}')
logger.debug(f'indexOps: {args.indexOps}')
if args.serializer in serializers_map:
serializer = serializers_map[args.serializer]
else:
serializer = args.serializer
profile_args = {"profile.results": args.no_results,
"profile.chop": args.chop,
"profile.serializer": serializer,
"profile.indexOps": args.indexOps}
res = self.client.gremlin_profile(query=cell, args=profile_args)
res.raise_for_status()
query_res = res.content.decode('utf-8')
if not args.silent:
gremlin_metadata = build_gremlin_metadata_from_query(query_type='profile', results=query_res, res=res)
titles.append('Profile')
if 'Neptune Gremlin Profile' in query_res:
first_tab_html = pre_container_template.render(content=query_res)
else:
first_tab_html = pre_container_template.render(content='No profile found')
else:
query_start = time.time() * 1000 # time.time() returns time in seconds w/high precision; x1000 to get in ms
query_res = self.client.gremlin_query(cell)
query_time = time.time() * 1000 - query_start
if not args.silent:
gremlin_metadata = build_gremlin_metadata_from_query(query_type='query', results=query_res,
query_time=query_time)
titles.append('Console')
try:
logger.debug(f'groupby: {args.group_by}')
logger.debug(f'display_property: {args.display_property}')
logger.debug(f'edge_display_property: {args.edge_display_property}')
logger.debug(f'label_max_length: {args.label_max_length}')
logger.debug(f'ignore_groups: {args.ignore_groups}')
gn = GremlinNetwork(group_by_property=args.group_by, display_property=args.display_property,
edge_display_property=args.edge_display_property,
tooltip_property=args.tooltip_property,
edge_tooltip_property=args.edge_tooltip_property,
label_max_length=args.label_max_length,
edge_label_max_length=args.edge_label_max_length,
ignore_groups=args.ignore_groups)
if args.path_pattern == '':
gn.add_results(query_res)
else:
pattern = parse_pattern_list_str(args.path_pattern)
gn.add_results_with_pattern(query_res, pattern)
logger.debug(f'number of nodes is {len(gn.graph.nodes)}')
if len(gn.graph.nodes) > 0:
self.graph_notebook_vis_options['physics']['disablePhysicsAfterInitialSimulation'] \
= args.stop_physics
self.graph_notebook_vis_options['physics']['simulationDuration'] = args.simulation_duration
f = Force(network=gn, options=self.graph_notebook_vis_options)
titles.append('Graph')
children.append(f)
logger.debug('added gremlin network to tabs')
except ValueError as value_error:
logger.debug(
f'unable to create gremlin network from result. Skipping from result set: {value_error}')
table_id = f"table-{str(uuid.uuid4()).replace('-', '')[:8]}"
first_tab_html = gremlin_table_template.render(guid=table_id, results=query_res)
if not args.silent:
metadata_output = widgets.Output(layout=gremlin_layout)
titles.append('Query Metadata')
children.append(metadata_output)
tab.children = children
for i in range(len(titles)):
tab.set_title(i, titles[i])
display(tab)
with metadata_output:
display(HTML(gremlin_metadata.to_html()))
with first_tab_output:
display(HTML(first_tab_html))
store_to_ns(args.store_to, query_res, local_ns)