def test_add_all_V_pattern(self): pattern = [PathPattern.V, PathPattern.V, PathPattern.V] path = Path([], ['SEA', 'DFW', 'AUS']) gn = GremlinNetwork() gn.add_results_with_pattern([path], pattern) self.assertEqual(3, len(gn.graph.nodes)) self.assertEqual(2, len(gn.graph.edges))
def test_add_path_with_edge_object(self): query = "g.V().has('airport','code','AUS').outE().inV().path().by('code').by().limit(10)" results = self.client.gremlin_query(query) gn = GremlinNetwork() pattern = [PathPattern.V, PathPattern.OUT_E, PathPattern.IN_V] gn.add_results_with_pattern(results, pattern) self.assertEqual(11, len(gn.graph.nodes)) self.assertEqual(10, len(gn.graph.edges))
def test_add_v_inE_v_path(self): pattern = [PathPattern.V, PathPattern.E, PathPattern.V] path = Path([], ['SEA', 'route', 'DFW']) gn = GremlinNetwork() gn.add_results_with_pattern([path], pattern) self.assertEqual(2, len(gn.graph.nodes)) self.assertEqual(1, len(gn.graph.edges)) self.assertIsNotNone(gn.graph.edges[('SEA', 'DFW', 'route')])
def test_add_v_and_outV_pattern(self): pattern = [PathPattern.V, PathPattern.OUT_V, PathPattern.V] path = Path([], ['SEA', 'DFW', 'AUS']) gn = GremlinNetwork() gn.add_results_with_pattern([path], pattern) for tup in gn.graph.edges: self.assertEqual(tup[0], 'DFW') # assert that DFW is the incoming vertex for both edges. self.assertEqual(3, len(gn.graph.nodes)) self.assertEqual(2, len(gn.graph.edges))
def test_add_v_inV_outV_pattern(self): pattern = [PathPattern.V, PathPattern.IN_V, PathPattern.OUT_V] path = Path([], ['SEA', 'DFW', 'AUS']) gn = GremlinNetwork() gn.add_results_with_pattern([path], pattern) self.assertEqual(3, len(gn.graph.nodes)) self.assertEqual(2, len(gn.graph.edges)) edges = gn.graph.in_edges('DFW') self.assertEqual(2, len(edges))
def test_out_v_unhashable_dict(self): query = """g.V(). hasLabel('country'). has('desc','Jamaica'). out(). path(). by(valueMap())""" results = self.client.gremlin_query(query) gn = GremlinNetwork() pattern = [PathPattern.V, PathPattern.OUT_V] gn.add_results_with_pattern(results, pattern) node = gn.graph.nodes.get('graph_notebook-2f363b2fa995d0567e638a240efd0a26') self.assertEqual(["Jamaica"], node['properties']['desc'])
def test_path_with_dict(self): query = """g.V().has('airport','code','CZM'). out('route'). path(). by(valueMap('code','city','region','desc','lat','lon'). order(local). by(keys))""" results = self.client.gremlin_query(query) gn = GremlinNetwork() pattern = [PathPattern.V, PathPattern.IN_V] gn.add_results_with_pattern(results, pattern) self.assertEqual(12, len(gn.graph.nodes)) self.assertEqual(11, len(gn.graph.edges))
def test_add_path_by_dist(self): query = """g.V().has('airport','code','AUS'). repeat(outE().inV().simplePath()). until(has('code','WLG')). limit(5). path(). by('code'). by('dist')""" results = self.client.gremlin_query(query) gn = GremlinNetwork() pattern = [PathPattern.V, PathPattern.OUT_E, PathPattern.IN_V, PathPattern.OUT_E] gn.add_results_with_pattern(results, pattern) self.assertEqual(8, len(gn.graph.nodes)) self.assertEqual(11, len(gn.graph.edges))
def test_add_v_inV_outV_longer_path(self): pattern = [PathPattern.V, PathPattern.IN_V, PathPattern.OUT_V] path = Path([], ['SEA', 'DFW', 'AUS', 'LAX', 'JFK']) gn = GremlinNetwork() gn.add_results_with_pattern([path], pattern) self.assertEqual(5, len(gn.graph.nodes)) self.assertEqual(4, len(gn.graph.edges)) dfw_edges = gn.graph.in_edges('DFW') self.assertEqual(2, len(dfw_edges)) lax_edges = gn.graph.in_edges('LAX') self.assertEqual(1, len(lax_edges)) jfk_edges = gn.graph.in_edges('JFK') self.assertEqual(1, len(jfk_edges))
def gremlin(self, line, cell): 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') args = parser.parse_args(line.split()) mode = str_to_query_mode(args.query_mode) tab = widgets.Tab() if mode == QueryMode.EXPLAIN: request_generator = create_request_generator( self.graph_notebook_config.auth_mode, self.graph_notebook_config.iam_credentials_provider_type) raw_html = gremlin_explain(cell, self.graph_notebook_config.host, self.graph_notebook_config.port, self.graph_notebook_config.ssl, request_generator) explain_output = widgets.Output(layout=DEFAULT_LAYOUT) with explain_output: display(HTML(raw_html)) tab.children = [explain_output] tab.set_title(0, 'Explain') display(tab) elif mode == QueryMode.PROFILE: request_generator = create_request_generator( self.graph_notebook_config.auth_mode, self.graph_notebook_config.iam_credentials_provider_type) raw_html = gremlin_profile(cell, self.graph_notebook_config.host, self.graph_notebook_config.port, self.graph_notebook_config.ssl, request_generator) profile_output = widgets.Output(layout=DEFAULT_LAYOUT) with profile_output: display(HTML(raw_html)) tab.children = [profile_output] tab.set_title(0, 'Profile') display(tab) else: client_provider = create_client_provider( self.graph_notebook_config.auth_mode, self.graph_notebook_config.iam_credentials_provider_type) res = do_gremlin_query(cell, self.graph_notebook_config.host, self.graph_notebook_config.port, self.graph_notebook_config.ssl, client_provider) children = [] titles = [] table_output = widgets.Output(layout=DEFAULT_LAYOUT) titles.append('Console') children.append(table_output) try: gn = GremlinNetwork() if args.path_pattern == '': gn.add_results(res) else: pattern = parse_pattern_list_str(args.path_pattern) gn.add_results_with_pattern(res, pattern) logger.debug(f'number of nodes is {len(gn.graph.nodes)}') if len(gn.graph.nodes) > 0: 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}' ) tab.children = children for i in range(len(titles)): tab.set_title(i, titles[i]) display(tab) table_id = f"table-{str(uuid.uuid4()).replace('-', '')[:8]}" table_html = gremlin_table_template.render(guid=table_id, results=res) with table_output: display(HTML(table_html))
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', default='T.label', help='Property used to group nodes (e.g. code, T.region) default is T.label') parser.add_argument('--store-to', type=str, default='', help='store query result to this variable') parser.add_argument('--ignore-groups', action='store_true', help="Ignore all grouping options") args = parser.parse_args(line.split()) mode = str_to_query_mode(args.query_mode) logger.debug(f'Arguments {args}') tab = widgets.Tab() if mode == QueryMode.EXPLAIN: request_generator = create_request_generator(self.graph_notebook_config.auth_mode, self.graph_notebook_config.iam_credentials_provider_type) query_res = do_gremlin_explain(cell, self.graph_notebook_config.host, self.graph_notebook_config.port, self.graph_notebook_config.ssl, request_generator) if 'explain' in query_res: html = pre_container_template.render(content=query_res['explain']) else: html = pre_container_template.render(content='No explain found') explain_output = widgets.Output(layout=DEFAULT_LAYOUT) with explain_output: display(HTML(html)) tab.children = [explain_output] tab.set_title(0, 'Explain') display(tab) elif mode == QueryMode.PROFILE: request_generator = create_request_generator(self.graph_notebook_config.auth_mode, self.graph_notebook_config.iam_credentials_provider_type) query_res = do_gremlin_profile(cell, self.graph_notebook_config.host, self.graph_notebook_config.port, self.graph_notebook_config.ssl, request_generator) if 'profile' in query_res: html = pre_container_template.render(content=query_res['profile']) else: html = pre_container_template.render(content='No profile found') profile_output = widgets.Output(layout=DEFAULT_LAYOUT) with profile_output: display(HTML(html)) tab.children = [profile_output] tab.set_title(0, 'Profile') display(tab) else: client_provider = create_client_provider(self.graph_notebook_config.auth_mode, self.graph_notebook_config.iam_credentials_provider_type) query_res = do_gremlin_query(cell, self.graph_notebook_config.host, self.graph_notebook_config.port, self.graph_notebook_config.ssl, client_provider) children = [] titles = [] table_output = widgets.Output(layout=DEFAULT_LAYOUT) titles.append('Console') children.append(table_output) try: logger.debug(f'groupby: {args.group_by}') if args.ignore_groups: gn = GremlinNetwork() else: gn = GremlinNetwork(group_by_property=args.group_by) 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: 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}') tab.children = children for i in range(len(titles)): tab.set_title(i, titles[i]) display(tab) table_id = f"table-{str(uuid.uuid4()).replace('-', '')[:8]}" table_html = gremlin_table_template.render(guid=table_id, results=query_res) with table_output: display(HTML(table_html)) store_to_ns(args.store_to, query_res, local_ns)