diff --git a/README b/README index aa433579968ddbddef397f5b3769611420260301_UkVBRE1F..9d06eaceef2d4d498d60845cea250d876ee735c3_UkVBRE1F 100644 --- a/README +++ b/README @@ -1,3 +1,22 @@ Summary ------- Simple ElasticSearch indexing integration for CubicWeb + + +Pyramid debug panel +~~~~~~~~~~~~~~~~~~~ + +To activate the debug panel, you'll need to install ``pyramid_debugtoolbar``, +typically with:: + + pip install pyramid_debugtoolbar + +Then, you'll have activate the debug toolbar and include the ElasticSearch +panel in your ``pyramid.ini``: + + pyramid.includes = + pyramid_debugtoolbar + debugtoolbar.includes = + cubicweb_elasticsearch.pviews.espanel + + diff --git a/cubicweb_elasticsearch/pviews/__init__.py b/cubicweb_elasticsearch/pviews/__init__.py new file mode 100644 diff --git a/cubicweb_elasticsearch/pviews/espanel.py b/cubicweb_elasticsearch/pviews/espanel.py new file mode 100644 index 0000000000000000000000000000000000000000..9d06eaceef2d4d498d60845cea250d876ee735c3_Y3ViaWN3ZWJfZWxhc3RpY3NlYXJjaC9wdmlld3MvZXNwYW5lbC5weQ== --- /dev/null +++ b/cubicweb_elasticsearch/pviews/espanel.py @@ -0,0 +1,62 @@ +from datetime import datetime + +from elasticsearch.client import Elasticsearch + +from jinja2 import Environment, PackageLoader, select_autoescape + +from pyramid_debugtoolbar.panels import DebugPanel + + +env = Environment( + loader=PackageLoader('cubicweb_elasticsearch.pviews', 'templates'), + autoescape=select_autoescape(['html', 'xml']) +) + + +class ESDebugPanel(DebugPanel): + """ES queries panel""" + name = 'ES' + has_content = True + template = 'cubicweb_elasticsearch.pviews:templates/espanel.jinja2' + title = 'Queries' + nav_title = 'ElasticSearch' + + def __init__(self, request): + self.data = {'request_path': request.path_info, 'queries': {}} + self.queries = [] + orig_search = Elasticsearch.search + + def traced_search(es_client, index=None, doc_type=None, body=None, **params): + start = datetime.now() + query = { + 'index': index, + 'doc_type': doc_type, + 'body': body, + 'params': params, + 'starttime': start, + 'response': {}, + 'elapsed': 0, + } + self.queries.append(query) + result = orig_search(es_client, index, doc_type, body, **params) + elapsed = datetime.now() - start + miliseconds = elapsed.seconds * 1000 + elapsed.microseconds / 1000 + query.update( + { + 'response': { + 'hits': result.get('hits', []), + 'aggregations': result.get('aggregations', {}), + }, + 'elapsed': miliseconds, + } + ) + return result + Elasticsearch.search = traced_search + + def render_content(self, request): + template = env.get_template('espanel.jinja2') + return template.render(queries=self.queries) + + +def includeme(config): + config.add_debugtoolbar_panel(ESDebugPanel) diff --git a/cubicweb_elasticsearch/pviews/templates/espanel.jinja2 b/cubicweb_elasticsearch/pviews/templates/espanel.jinja2 new file mode 100644 index 0000000000000000000000000000000000000000..9d06eaceef2d4d498d60845cea250d876ee735c3_Y3ViaWN3ZWJfZWxhc3RpY3NlYXJjaC9wdmlld3MvdGVtcGxhdGVzL2VzcGFuZWwuamluamEy --- /dev/null +++ b/cubicweb_elasticsearch/pviews/templates/espanel.jinja2 @@ -0,0 +1,29 @@ +<table class="table table-condensed"> + <thead> + <th>index</th> + <th>doc_type</th> + <th>body</th> + <th>response</th> + <th>params</th> + <th>elapsed (ms)</th> + </thead> + <tbody> + {% for query in queries %} + <tr> + <td>{{ query.index | join(',') }}</td> + <td>{{ query.doc_type | join(',') }}</td> + <td> + <pre><code class="language-json">{{ query.body | tojson(indent=1) }}</code></pre> + </td> + <td> + <pre><code class="language-json">{{ query.response | tojson(indent=1) }}</code></pre> + </td> + <td>{{ query.params }}</td> + <td>{{ "%.2f"|format(query.elapsed|float) }}</td> + </tr> + {% endfor %} + </tbody> +</table> +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/themes/prism.min.css"> +<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.js"></script> +<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/components/prism-json.min.js"></script>