Example #1
0
def gremlin_explain(query, host, port, use_ssl, request_param_generator):
    result = do_gremlin_explain(query, host, port, use_ssl,
                                request_param_generator)
    if 'explain' in result:
        html = pre_container_template.render(content=result['explain'])
    else:
        html = pre_container_template.render(content='No explain found')
    return html
Example #2
0
    def test_do_gremlin_explain(self):
        query = 'g.V().limit(1)'
        request_generator = DefaultRequestGenerator()
        results = do_gremlin_explain(query, self.host, self.port, self.ssl,
                                     request_generator)

        self.assertEqual(type(results), dict)
        self.assertTrue('explain' in results)
    def test_do_gremlin_explain_with_iam(self):
        query = 'g.V().limit(1)'
        request_generator = create_request_generator(
            AuthModeEnum.IAM, IAMAuthCredentialsProvider.ENV)
        results = do_gremlin_explain(query, self.host, self.port, self.ssl,
                                     request_generator)

        self.assertEqual(type(results), dict)
        self.assertTrue('explain' in results)
    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('--store-to',
                            type=str,
                            default='',
                            help='store query result to this variable')
        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)

            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:
                gn = GremlinNetwork()
                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)