Merge pull request #23 from nibalizer/pep8

Assorted PEP8 fixes.
This commit is contained in:
Daniele Sluijters
2013-11-04 02:33:12 -08:00
6 changed files with 135 additions and 77 deletions

View File

@@ -17,4 +17,3 @@ try:
from puppetboard.app import app as application from puppetboard.app import app as application
except Exception, inst: except Exception, inst:
logging.exception("Error: %s", str(type(inst))) logging.exception("Error: %s", str(type(inst)))

View File

@@ -41,6 +41,7 @@ if not isinstance(numeric_level, int):
logging.basicConfig(level=numeric_level) logging.basicConfig(level=numeric_level)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def stream_template(template_name, **context): def stream_template(template_name, **context):
app.update_template_context(context) app.update_template_context(context)
t = app.jinja_env.get_template(template_name) t = app.jinja_env.get_template(template_name)
@@ -48,64 +49,92 @@ def stream_template(template_name, **context):
rv.enable_buffering(5) rv.enable_buffering(5)
return rv return rv
@app.errorhandler(400) @app.errorhandler(400)
def bad_request(e): def bad_request(e):
return render_template('400.html'), 400 return render_template('400.html'), 400
@app.errorhandler(403) @app.errorhandler(403)
def bad_request(e): def bad_request(e):
return render_template('403.html'), 400 return render_template('403.html'), 400
@app.errorhandler(404) @app.errorhandler(404)
def not_found(e): def not_found(e):
return render_template('404.html'), 404 return render_template('404.html'), 404
@app.errorhandler(412) @app.errorhandler(412)
def precond_failed(e): def precond_failed(e):
"""We're slightly abusing 412 to handle missing features """We're slightly abusing 412 to handle missing features
depending on the API version.""" depending on the API version."""
return render_template('412.html'), 412 return render_template('412.html'), 412
@app.errorhandler(500) @app.errorhandler(500)
def server_error(e): def server_error(e):
return render_template('500.html'), 500 return render_template('500.html'), 500
@app.route('/') @app.route('/')
def index(): def index():
"""This view generates the index page and displays a set of metrics and latest reports on """This view generates the index page and displays a set of metrics and
nodes fetched from PuppetDB. latest reports on nodes fetched from PuppetDB.
""" """
# TODO: Would be great if we could parallelize this somehow, doing these # TODO: Would be great if we could parallelize this somehow, doing these
# requests in sequence is rather pointless. # requests in sequence is rather pointless.
num_nodes = get_or_abort(puppetdb.metric, prefix = 'com.puppetlabs.puppetdb.query.population'
'com.puppetlabs.puppetdb.query.population:type=default,name=num-nodes') num_nodes = get_or_abort(
num_resources = get_or_abort(puppetdb.metric, puppetdb.metric,
'com.puppetlabs.puppetdb.query.population:type=default,name=num-resources') "{0}{1}".format(prefix, ':type=default,name=num-nodes'))
avg_resources_node = get_or_abort(puppetdb.metric, num_resources = get_or_abort(
'com.puppetlabs.puppetdb.query.population:type=default,name=avg-resources-per-node') puppetdb.metric,
"{0}{1}".format(prefix, ':type=default,name=num-resources'))
avg_resources_node = get_or_abort(
puppetdb.metric,
"{0}{1}".format(prefix, ':type=default,name=avg-resources-per-node'))
metrics = { metrics = {
'num_nodes': num_nodes['Value'], 'num_nodes': num_nodes['Value'],
'num_resources': num_resources['Value'], 'num_resources': num_resources['Value'],
'avg_resources_node': "{0:10.6f}".format(avg_resources_node['Value']), 'avg_resources_node': "{0:10.6f}".format(avg_resources_node['Value']),
} }
latest_event_count = puppetdb._query('aggregate-event-counts', query='["=", "latest-report?", true]', summarize_by='certname') latest_event_count = puppetdb._query(
latest_event_count['noopskip'] = latest_event_count['noops']+latest_event_count['skips'] 'aggregate-event-counts',
query='["=", "latest-report?", true]',
summarize_by='certname')
latest_events = puppetdb._query('event-counts', query='["=", "latest-report?", true]', summarize_by='certname') latest_event_count['noopskip'] = (
latest_event_count['noops'] + latest_event_count['skips'])
latest_events = puppetdb._query(
'event-counts',
query='["=", "latest-report?", true]',
summarize_by='certname')
unreported = [] unreported = []
unresponsive_window = datetime.utcnow() - (
timedelta(hours=app.config['UNRESPONSIVE_HOURS']))
for node in puppetdb.nodes(): for node in puppetdb.nodes():
try: try:
node_last_seen = node.report_timestamp.replace(tzinfo=None) node_last_seen = node.report_timestamp.replace(tzinfo=None)
if node_last_seen < (datetime.utcnow()-timedelta(hours=app.config['UNRESPONSIVE_HOURS'])): if node_last_seen < unresponsive_window:
delta = (datetime.utcnow() - node_last_seen) delta = (datetime.utcnow() - node_last_seen)
node.noresponse = str(delta.days) + "d " + str(int(delta.seconds/3600)) +"h " + str(int((delta.seconds%3600)/60))+ "m" node.noresponse = str(delta.days) + "d "
node.noresponse += str(int(delta.seconds / 3600)) + "h "
node.noresponse += str(int((delta.seconds % 3600) / 60)) + "m"
unreported.append(node) unreported.append(node)
except AttributeError: except AttributeError:
unreported.append(node) unreported.append(node)
return render_template('index.html', metrics=metrics, latest_event_count=latest_event_count, latest_events=latest_events, unreported=unreported) return render_template(
'index.html',
metrics=metrics,
latest_event_count=latest_event_count,
latest_events=latest_events,
unreported=unreported)
@app.route('/nodes') @app.route('/nodes')
def nodes(): def nodes():
@@ -118,19 +147,26 @@ def nodes():
works. Once pagination is in place we can change this but we'll need to works. Once pagination is in place we can change this but we'll need to
provide a search feature instead. provide a search feature instead.
""" """
latest_events = puppetdb._query('event-counts', query='["=", "latest-report?", true]', summarize_by='certname') latest_events = puppetdb._query(
'event-counts',
query='["=", "latest-report?", true]',
summarize_by='certname')
nodes = [] nodes = []
for node in yield_or_stop(puppetdb.nodes()): for node in yield_or_stop(puppetdb.nodes()):
# check if node name is contained in any of the event-counts (grouped by certname) # check if node name is contained in any of the
status = [s for s in latest_events if s['subject']['title'] == node.name] # event-counts (grouped by certname)
status = [
s for s in latest_events if
s['subject']['title'] == node.name]
if status: if status:
node.status = status[0] node.status = status[0]
else: else:
node.status = None node.status = None
nodes.append(node) nodes.append(node)
return Response(stream_with_context(stream_template('nodes.html', return Response(stream_with_context(
nodes=nodes))) stream_template('nodes.html', nodes=nodes)))
@app.route('/node/<node_name>') @app.route('/node/<node_name>')
def node(node_name): def node(node_name):
@@ -141,24 +177,32 @@ def node(node_name):
node = get_or_abort(puppetdb.node, node_name) node = get_or_abort(puppetdb.node, node_name)
facts = node.facts() facts = node.facts()
reports = ten_reports(node.reports()) reports = ten_reports(node.reports())
return render_template('node.html', node=node, facts=yield_or_stop(facts), return render_template(
'node.html',
node=node,
facts=yield_or_stop(facts),
reports=yield_or_stop(reports)) reports=yield_or_stop(reports))
@app.route('/reports') @app.route('/reports')
def reports(): def reports():
"""Doesn't do much yet but is meant to show something like the reports of """Doesn't do much yet but is meant to show something like the reports of
the last half our, something like that.""" the last half our, something like that."""
return render_template('reports.html') return render_template('reports.html')
@app.route('/reports/<node>') @app.route('/reports/<node>')
def reports_node(node): def reports_node(node):
"""Fetches all reports for a node and processes them eventually rendering """Fetches all reports for a node and processes them eventually rendering
a table displaying those reports.""" a table displaying those reports."""
reports = ten_reports(yield_or_stop( reports = ten_reports(yield_or_stop(
puppetdb.reports('["=", "certname", "{0}"]'.format(node)))) puppetdb.reports('["=", "certname", "{0}"]'.format(node))))
return render_template('reports_node.html', reports=reports, return render_template(
'reports_node.html',
reports=reports,
nodename=node) nodename=node)
@app.route('/report/latest/<node_name>') @app.route('/report/latest/<node_name>')
def report_latest(node_name): def report_latest(node_name):
"""Redirect to the latest report of a given node. This is a workaround """Redirect to the latest report of a given node. This is a workaround
@@ -171,6 +215,7 @@ def report_latest(node_name):
report = list(yield_or_stop(reports))[0] report = list(yield_or_stop(reports))[0]
return redirect(url_for('report', node=node_name, report_id=report)) return redirect(url_for('report', node=node_name, report_id=report))
@app.route('/report/<node>/<report_id>') @app.route('/report/<node>/<report_id>')
def report(node, report_id): def report(node, report_id):
"""Displays a single report including all the events associated with that """Displays a single report including all the events associated with that
@@ -182,11 +227,14 @@ def report(node, report_id):
if report.hash_ == report_id: if report.hash_ == report_id:
events = puppetdb.events('["=", "report", "{0}"]'.format( events = puppetdb.events('["=", "report", "{0}"]'.format(
report.hash_)) report.hash_))
return render_template('report.html', report=report, return render_template(
'report.html',
report=report,
events=yield_or_stop(events)) events=yield_or_stop(events))
else: else:
abort(404) abort(404)
@app.route('/facts') @app.route('/facts')
def facts(): def facts():
"""Displays an alphabetical list of all facts currently known to """Displays an alphabetical list of all facts currently known to
@@ -202,6 +250,7 @@ def facts():
sorted_facts_dict = sorted(facts_dict.items()) sorted_facts_dict = sorted(facts_dict.items())
return render_template('facts.html', facts_dict=sorted_facts_dict) return render_template('facts.html', facts_dict=sorted_facts_dict)
@app.route('/fact/<fact>') @app.route('/fact/<fact>')
def fact(fact): def fact(fact):
"""Fetches the specific fact from PuppetDB and displays its value per """Fetches the specific fact from PuppetDB and displays its value per
@@ -209,10 +258,12 @@ def fact(fact):
# we can only consume the generator once, lists can be doubly consumed # we can only consume the generator once, lists can be doubly consumed
# om nom nom # om nom nom
localfacts = [f for f in yield_or_stop(puppetdb.facts(name=fact))] localfacts = [f for f in yield_or_stop(puppetdb.facts(name=fact))]
return Response(stream_with_context(stream_template('fact.html', return Response(stream_with_context(stream_template(
'fact.html',
name=fact, name=fact,
facts=localfacts))) facts=localfacts)))
@app.route('/query', methods=('GET', 'POST')) @app.route('/query', methods=('GET', 'POST'))
def query(): def query():
"""Allows to execute raw, user created querries against PuppetDB. This is """Allows to execute raw, user created querries against PuppetDB. This is
@@ -223,7 +274,9 @@ def query():
if app.config['ENABLE_QUERY']: if app.config['ENABLE_QUERY']:
form = QueryForm() form = QueryForm()
if form.validate_on_submit(): if form.validate_on_submit():
result = get_or_abort(puppetdb._query, form.endpoints.data, result = get_or_abort(
puppetdb._query,
form.endpoints.data,
query='[{0}]'.format(form.query.data)) query='[{0}]'.format(form.query.data))
return render_template('query.html', form=form, result=result) return render_template('query.html', form=form, result=result)
return render_template('query.html', form=form) return render_template('query.html', form=form)
@@ -231,6 +284,7 @@ def query():
log.warn('Access to query interface disabled by administrator..') log.warn('Access to query interface disabled by administrator..')
abort(403) abort(403)
@app.route('/metrics') @app.route('/metrics')
def metrics(): def metrics():
metrics = puppetdb._query('metrics', path='mbeans') metrics = puppetdb._query('metrics', path='mbeans')
@@ -238,8 +292,12 @@ def metrics():
metrics[key] = value.split('/')[3] metrics[key] = value.split('/')[3]
return render_template('metrics.html', metrics=sorted(metrics.items())) return render_template('metrics.html', metrics=sorted(metrics.items()))
@app.route('/metric/<metric>') @app.route('/metric/<metric>')
def metric(metric): def metric(metric):
name = urllib.unquote(metric) name = urllib.unquote(metric)
metric = puppetdb.metric(metric) metric = puppetdb.metric(metric)
return render_template('metric.html', name=name, metric=sorted(metric.items())) return render_template(
'metric.html',
name=name,
metric=sorted(metric.items()))

View File

@@ -4,6 +4,7 @@ from __future__ import absolute_import
from flask.ext.wtf import Form from flask.ext.wtf import Form
from wtforms import RadioField, TextAreaField, validators from wtforms import RadioField, TextAreaField, validators
class QueryForm(Form): class QueryForm(Form):
"""The form used to allow freeform queries to be executed against """The form used to allow freeform queries to be executed against
PuppetDB.""" PuppetDB."""
@@ -17,4 +18,3 @@ class QueryForm(Form):
('reports', 'Reports'), ('reports', 'Reports'),
('events', 'Events'), ('events', 'Events'),
]) ])

View File

@@ -6,6 +6,7 @@ from pypuppetdb.errors import EmptyResponseError
from flask import abort from flask import abort
def get_or_abort(func, *args, **kwargs): def get_or_abort(func, *args, **kwargs):
"""Execute the function with its arguments and handle the possible """Execute the function with its arguments and handle the possible
errors that might occur. errors that might occur.