puppetboard/app.py: Enhancing queries for Node and Report states (#271)
* puppetboard/app.py: Enhancing queries for Node and Report states This resolves #264 On the Nodes and Reports tabs when the user adds a status query string argument additional query clauses are generated based on its value. Can be one of failed, changed, unchanged, noop or unreported (for Nodes only) No query clause is generated for noop on the Nodes tab. The query field latest_report_noop was added in PuppetDB 4.1 and we do not want to break compatability between minor or bug-fix versions. * puppetboard/app.py: Simplifying the query logic in nodes() The new logic starts with a blank `AndOperator()` object then proceeds to build the query based on environment and status values. After all after all checking if there are no operations declare the object as None. * puppetboard/app.py: Simplifying the query logic for reports() Similar to the work done for nodes() * puppetboard/app.py: Fixing pep8 formatting in nodes() * Add pagination to reports/<node>
This commit is contained in:
@@ -7,7 +7,7 @@ try:
|
||||
from urllib import unquote
|
||||
except ImportError:
|
||||
from urllib.parse import unquote
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from itertools import tee
|
||||
|
||||
from flask import (
|
||||
@@ -245,16 +245,32 @@ def nodes(env):
|
||||
:type env: :obj:`string`
|
||||
"""
|
||||
envs = environments()
|
||||
status_arg = request.args.get('status', '')
|
||||
check_env(env, envs)
|
||||
|
||||
if env == '*':
|
||||
query = None
|
||||
else:
|
||||
query = AndOperator()
|
||||
query = AndOperator()
|
||||
|
||||
if env != '*':
|
||||
query.add(EqualsOperator("catalog_environment", env))
|
||||
query.add(EqualsOperator("facts_environment", env))
|
||||
|
||||
status_arg = request.args.get('status', '')
|
||||
if status_arg in ['failed', 'changed', 'unchanged']:
|
||||
query.add(EqualsOperator('latest_report_status', status_arg))
|
||||
elif status_arg == 'unreported':
|
||||
unreported = datetime.datetime.utcnow()
|
||||
unreported = (unreported -
|
||||
timedelta(hours=app.config['UNRESPONSIVE_HOURS']))
|
||||
unreported = unreported.replace(microsecond=0).isoformat()
|
||||
|
||||
unrep_query = OrOperator()
|
||||
unrep_query.add(NullOperator('report_timestamp', True))
|
||||
unrep_query.add(LessEqualOperator('report_timestamp', unreported))
|
||||
|
||||
query.add(unrep_query)
|
||||
|
||||
if len(query.operations) == 0:
|
||||
query = None
|
||||
|
||||
nodelist = puppetdb.nodes(
|
||||
query=query,
|
||||
unreported=app.config['UNRESPONSIVE_HOURS'],
|
||||
@@ -431,13 +447,25 @@ def reports(env, page):
|
||||
envs = environments()
|
||||
check_env(env, envs)
|
||||
limit = request.args.get('limit', app.config['REPORTS_COUNT'])
|
||||
reports_query = None
|
||||
status_arg = request.args.get('status')
|
||||
reports_query = AndOperator()
|
||||
total_query = ExtractOperator()
|
||||
|
||||
total_query.add_field(FunctionOperator("count"))
|
||||
|
||||
if env != '*':
|
||||
reports_query = EqualsOperator("environment", env)
|
||||
reports_query.add(EqualsOperator("environment", env))
|
||||
|
||||
if status_arg in ['failed', 'changed', 'unchanged']:
|
||||
reports_query.add(EqualsOperator('status', status_arg))
|
||||
reports_query.add(EqualsOperator('noop', False))
|
||||
elif status_arg == 'noop':
|
||||
reports_query.add(EqualsOperator('noop', True))
|
||||
|
||||
if len(reports_query.operations) == 0:
|
||||
reports_query = None
|
||||
|
||||
if reports_query is not None:
|
||||
total_query.add_query(reports_query)
|
||||
|
||||
try:
|
||||
@@ -512,23 +540,42 @@ def reports_node(env, node_name, page):
|
||||
:type page: :obj:`int`
|
||||
"""
|
||||
envs = environments()
|
||||
status_arg = request.args.get('status')
|
||||
check_env(env, envs)
|
||||
query = AndOperator()
|
||||
total_query = ExtractOperator()
|
||||
|
||||
total_query.add_field(FunctionOperator("count"))
|
||||
|
||||
if env != '*':
|
||||
if env == '*':
|
||||
if status_arg in ['failed', 'changed', 'unchanged']:
|
||||
query.add(EqualsOperator('status', status_arg))
|
||||
elif status_arg == 'noop':
|
||||
query.add(EqualsOperator('noop', True))
|
||||
else:
|
||||
query.add(EqualsOperator("environment", env))
|
||||
|
||||
if status_arg in ['failed', 'changed', 'unchanged']:
|
||||
query.add(EqualsOperator('status', status_arg))
|
||||
query.add(EqualsOperator('noop', False))
|
||||
elif status_arg == 'noop':
|
||||
query.add(EqualsOperator('noop', True))
|
||||
|
||||
query.add(EqualsOperator("certname", node_name))
|
||||
total_query.add_query(query)
|
||||
|
||||
limit = request.args.get('limit', app.config['REPORTS_COUNT'])
|
||||
|
||||
try:
|
||||
paging_args = {'limit': int(limit)}
|
||||
paging_args['offset'] = int((page - 1) * paging_args['limit'])
|
||||
except ValueError:
|
||||
paging_args = {}
|
||||
|
||||
reports = get_or_abort(puppetdb.reports,
|
||||
query=query,
|
||||
limit=app.config['REPORTS_COUNT'],
|
||||
offset=(page - 1) * app.config['REPORTS_COUNT'],
|
||||
order_by=DEFAULT_ORDER_BY)
|
||||
order_by=DEFAULT_ORDER_BY,
|
||||
**paging_args)
|
||||
total = get_or_abort(puppetdb._query,
|
||||
'reports',
|
||||
query=total_query)
|
||||
@@ -568,7 +615,7 @@ def reports_node(env, node_name, page):
|
||||
reports=reports,
|
||||
reports_count=app.config['REPORTS_COUNT'],
|
||||
report_event_counts=report_event_counts,
|
||||
pagination=Pagination(page, app.config['REPORTS_COUNT'], total),
|
||||
pagination=Pagination(page, paging_args.get('limit', total), total),
|
||||
envs=envs,
|
||||
current_env=env)
|
||||
|
||||
|
||||
@@ -42,4 +42,15 @@
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="ui dropdown" style="float:right;">
|
||||
<div class="text">Filter By</div>
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="menu">
|
||||
<a class="item" href="{{url_for_field('status', 'failed')}}">Failed</a>
|
||||
<a class="item" href="{{url_for_field('status', 'changed')}}">Changed</a>
|
||||
<a class="item" href="{{url_for_field('status', 'unchanged')}}">Unchanged</a>
|
||||
<a class="item" href="{{url_for_field('status', 'noop')}}">Noop</a>
|
||||
<a class="item" href="{{url_for_field('status', 'unreported')}}">Unreported</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
@@ -14,4 +14,14 @@
|
||||
<a class="{% if limit == '*' %}active {% endif %}item" href="{{url_for_field('limit', '*')}}">All</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui dropdown" style="float:right;">
|
||||
<div class="text">Filter By</div>
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="menu">
|
||||
<a class="item" href="{{url_for_field('status', 'failed')}}">Failed</a>
|
||||
<a class="item" href="{{url_for_field('status', 'changed')}}">Changed</a>
|
||||
<a class="item" href="{{url_for_field('status', 'unchanged')}}">Unchanged</a>
|
||||
<a class="item" href="{{url_for_field('status', 'noop')}}">Noop</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
Reference in New Issue
Block a user