Beispiel #1
0
 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_group_notokens_without_groupby(self):
        vertex = {'type': 'Airport', 'runways': '4', 'code': 'SEA'}

        gn = GremlinNetwork()
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get(
            'graph_notebook-ed8fddedf251d3d5745dccfd53edf51d')
        self.assertEqual(node['group'], '')
 def test_add_value_map_to_network(self):
     airports_path_query = "g.V().has('code', 'SEA').outE().inV().path().by(valueMap(true))"
     results = self.client.gremlin_query(airports_path_query)
     gremlin_network = GremlinNetwork()
     gremlin_network.add_results(results)
     edge_id = '4406'
     expected_label = 'route'
     actual_label = gremlin_network.graph.nodes.get(edge_id)['label']
     self.assertEqual(expected_label, actual_label)
Beispiel #4
0
 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_path_with_repeat(self):
        query = "g.V().has('airport', 'code', 'ANC').repeat(outE().inV().simplePath()).times(2).path().by('code').by()"
        results = self.client.gremlin_query(query)

        gremlin_network = GremlinNetwork()
        gremlin_network.add_results(results)
        self.assertEqual(
            'route',
            gremlin_network.graph.edges[('ANC', 'BLI', '5276')]['label'])
Beispiel #6
0
    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_add_paths_to_network(self):
     airports_path_query = "g.V().has('code', 'SEA').outE().inV().path()"
     results = self.client.gremlin_query(airports_path_query)
     gremlin_network = GremlinNetwork()
     gremlin_network.add_results(results)
     sea_code = '22'
     jnu_code = '1102'
     edge_id = '7527'
     expected_label = 'route'
     actual_label = gremlin_network.graph[sea_code][jnu_code][edge_id][
         'label']
     self.assertEqual(expected_label, actual_label)
Beispiel #8
0
    def test_group_without_groupby(self):
        vertex = {
            T.id: '1234',
            T.label: 'airport',
            'type': 'Airport',
            'runways': '4',
            'code': 'SEA'
        }

        gn = GremlinNetwork()
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get(vertex[T.id])
        self.assertEqual(node['group'], 'airport')
Beispiel #9
0
 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))
Beispiel #10
0
    def test_group_nonexistent_groupby(self):
        vertex = {
            T.id: '1234',
            T.label: 'airport',
            'type': 'Airport',
            'runways': '4',
            'code': 'SEA'
        }

        gn = GremlinNetwork(group_by_property='foo')
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get(vertex[T.id])
        self.assertEqual(node['group'], '')
Beispiel #11
0
 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'])
Beispiel #12
0
 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_primitive_path(self):
        a = 'a'
        edge = 'a_to_b'
        b = 'b'

        p = Path([], [a, edge, b])
        paths = [p]

        gn = GremlinNetwork()
        gn.add_results(paths)

        self.assertTrue(gn.graph.has_node(a))
        self.assertTrue(gn.graph.has_node(b))
        self.assertTrue(gn.graph.has_node(
            edge))  # note, this is not of type Edge so we assume it is a node
        self.assertEqual(2, len(gn.graph.edges))
Beispiel #14
0
    def test_group_returnvertex_groupby_id(self):
        vertex = Vertex(id='1')

        gn = GremlinNetwork(group_by_property="id")
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get('1')
        self.assertEqual(node['group'], '1')

        gn = GremlinNetwork(group_by_property="T.id")
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get('1')
        self.assertEqual(node['group'], '')
Beispiel #15
0
 def test_add_path_with_groupby(self):
     path = Path(
         [], [{
             'country': ['US'],
             'code': ['SEA'],
             'longest': [11901],
             'city': ['Seattle'],
             T.label: 'airport',
             'lon': [-122.30899810791],
             'type': ['airport'],
             'elev': [432],
             T.id: '22',
             'icao': ['KSEA'],
             'runways': [3],
             'region': ['US-WA'],
             'lat': [47.4490013122559],
             'desc': ['Seattle-Tacoma']
         }, {
             'country': ['US'],
             'code': ['ATL'],
             'longest': [12390],
             'city': ['Atlanta'],
             T.label: 'airport',
             'lon': [-84.4281005859375],
             'type': ['airport'],
             'elev': [1026],
             T.id: '1',
             'icao': ['KATL'],
             'runways': [5],
             'region': ['US-GA'],
             'lat': [33.6366996765137],
             'desc': ['Hartsfield - Jackson Atlanta International Airport']
         }])
     gn = GremlinNetwork(group_by_property="code")
     gn.add_results([path])
     node = gn.graph.nodes.get('1')
     self.assertEqual(node['group'], "['ATL']")
Beispiel #16
0
    def test_add_vertex_with_callback(self):
        vertex = {
            T.id: '1234',
            T.label: 'airport',
            'type': 'Airport',
            'runways': '4',
            'code': 'SEA'
        }

        reached_callback = {}
        expected_data = {
            'data': {
                'group': 'airport',
                'label': 'airport',
                'properties': {
                    T.id: '1234',
                    T.label: 'airport',
                    'code': 'SEA',
                    'runways': '4',
                    'type': 'Airport'
                },
                'title': 'airport'
            },
            'node_id': '1234'
        }

        def add_node_callback(network, event_name, data):
            self.assertEqual(event_name, EVENT_ADD_NODE)
            self.assertEqual(expected_data, data)
            reached_callback[event_name] = True

        gn = GremlinNetwork(callbacks={EVENT_ADD_NODE: [add_node_callback]})
        gn.add_vertex(vertex)
        self.assertTrue(reached_callback[EVENT_ADD_NODE])
        node = gn.graph.nodes.get(vertex[T.id])
        self.assertEqual(expected_data['data']['properties'],
                         node['properties'])
Beispiel #17
0
    def test_ignore_group(self):
        vertex = {
            T.id: '1234',
            T.label: 'airport',
            'type': 'Airport',
            'runways': '4',
            'code': 'SEA'
        }

        gn = GremlinNetwork(ignore_groups=True)
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get(vertex[T.id])
        self.assertEqual(node['group'], '')

        gn = GremlinNetwork(group_by_property="code", ignore_groups=True)
        gn.add_vertex(vertex)
        node = gn.graph.nodes.get(vertex[T.id])
        self.assertEqual(node['group'], '')
Beispiel #18
0
    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))
Beispiel #19
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', 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)
 def test_add_path_with_integer(self):
     path = Path([], ['ANC', 3030, 'DFW'])
     gn = GremlinNetwork()
     gn.add_results([path])
     self.assertEqual(len(path), len(gn.graph.nodes))