Skip to content

Commit bf9f053

Browse files
Use smaller query to get overall infos without pull the complete node infos
1 parent 0582949 commit bf9f053

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

puppetboard/app.py

+55-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
LessEqualOperator, RegexOperator,
2020
GreaterEqualOperator)
2121

22+
from pypuppetdb.types import Node
23+
from pypuppetdb.utils import json_to_datetime
24+
2225
from puppetboard.forms import ENABLED_QUERY_ENDPOINTS, QueryForm
2326
from puppetboard.utils import (get_or_abort, yield_or_stop,
2427
get_db_version, is_bool)
@@ -152,6 +155,10 @@ def index(env):
152155
'noop': 0
153156
}
154157

158+
nodes = []
159+
160+
node_status_detail_enabled = app.config['NODES_STATUS_DETAIL_ENABLED']
161+
resource_stats_enabled = app.config['RESOURCES_STATS_ENABLED']
155162
if env == '*':
156163
query = app.config['OVERVIEW_FILTER']
157164

@@ -164,7 +171,7 @@ def index(env):
164171
"{0}{1}".format(prefix, ':%sname=num-nodes' % query_type),
165172
version=metric_version)
166173

167-
if app.config['RESOURCES_STATS_ENABLED']:
174+
if resource_stats_enabled:
168175
num_resources = get_or_abort(
169176
puppetdb.metric,
170177
"{0}{1}".format(prefix, ':%sname=num-resources' % query_type),
@@ -179,6 +186,9 @@ def index(env):
179186
(num_resources['Value'] / num_nodes['Value']))
180187
except ZeroDivisionError:
181188
metrics['avg_resources_node'] = 0
189+
190+
if not node_status_detail_enabled:
191+
nodes = get_node_status_summary(query)
182192
else:
183193
query = AndOperator()
184194
query.add(EqualsOperator('catalog_environment', env))
@@ -197,8 +207,7 @@ def index(env):
197207

198208
metrics['num_nodes'] = num_nodes[0]['count']
199209

200-
if app.config['RESOURCES_STATS_ENABLED']:
201-
210+
if resource_stats_enabled:
202211
num_resources_query = ExtractOperator()
203212
num_resources_query.add_field(FunctionOperator('count'))
204213
num_resources_query.add_query(EqualsOperator("environment", env))
@@ -215,26 +224,29 @@ def index(env):
215224
except ZeroDivisionError:
216225
metrics['avg_resources_node'] = 0
217226

218-
if app.config['NODES_STATUS_DETAIL_ENABLED']:
227+
if not node_status_detail_enabled:
228+
nodes = get_node_status_summary(query)
219229

230+
if node_status_detail_enabled:
220231
nodes = get_or_abort(puppetdb.nodes,
221232
query=query,
222233
unreported=app.config['UNRESPONSIVE_HOURS'],
223234
with_status=True,
224235
with_event_numbers=app.config['WITH_EVENT_NUMBERS'])
225236

226-
for node in nodes:
227-
if node.status == 'unreported':
228-
stats['unreported'] += 1
229-
elif node.status == 'changed':
230-
stats['changed'] += 1
231-
elif node.status == 'failed':
232-
stats['failed'] += 1
233-
elif node.status == 'noop':
234-
stats['noop'] += 1
235-
else:
236-
stats['unchanged'] += 1
237+
for node in nodes:
238+
if node.status == 'unreported':
239+
stats['unreported'] += 1
240+
elif node.status == 'changed':
241+
stats['changed'] += 1
242+
elif node.status == 'failed':
243+
stats['failed'] += 1
244+
elif node.status == 'noop':
245+
stats['noop'] += 1
246+
else:
247+
stats['unchanged'] += 1
237248

249+
if node_status_detail_enabled:
238250
if node.status != 'unchanged':
239251
nodes_overview.append(node)
240252

@@ -248,6 +260,33 @@ def index(env):
248260
)
249261

250262

263+
def get_node_status_summary(inner_query):
264+
node_status_query = ExtractOperator()
265+
node_status_query.add_field('certname')
266+
node_status_query.add_field('report_timestamp')
267+
node_status_query.add_field('latest_report_status')
268+
node_status_query.add_query(inner_query)
269+
270+
node_status = get_or_abort(
271+
puppetdb._query,
272+
'nodes',
273+
query=node_status_query
274+
)
275+
276+
now = datetime.utcnow()
277+
node_infos = []
278+
for node_state in node_status:
279+
last_report = json_to_datetime(node_state['report_timestamp'])
280+
last_report = last_report.replace(tzinfo=None)
281+
unreported_border = now - timedelta(hours=app.config['UNRESPONSIVE_HOURS'])
282+
certname = node_state['certname']
283+
284+
node_infos.append(Node(node, name=certname, unreported=last_report < unreported_border,
285+
status_report=node_state['latest_report_status']))
286+
287+
return node_infos
288+
289+
251290
@app.route('/nodes', defaults={'env': app.config['DEFAULT_ENVIRONMENT']})
252291
@app.route('/<env>/nodes')
253292
def nodes(env):
@@ -635,8 +674,6 @@ def facts(env):
635674
"""
636675
envs = environments()
637676
check_env(env, envs)
638-
facts = []
639-
order_by = '[{"field": "name", "order": "asc"}]'
640677
facts = get_or_abort(puppetdb.fact_names)
641678

642679
facts_columns = [[]]
@@ -912,7 +949,7 @@ def metrics(env):
912949
metrics.append(domain + ':' + prop)
913950
else:
914951
raise ValueError("Unknown metric version {} for database version {}"
915-
.format(metric_version, database_version))
952+
.format(metric_version, db_version))
916953

917954
return render_template('metrics.html',
918955
metrics=sorted(metrics),

puppetboard/templates/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ <h1 class="ui header population no-margin-bottom">
6161
{% if config.RESOURCES_STATS_ENABLED %}
6262
<div class="five column row">
6363
<div class="column">
64+
6465
</div>
6566
<div class="column">
67+
6668
</div>
6769
<div class="column">
70+
6871
</div>
6972
<div class="column">
7073
<h1 class="ui header darkblue no-margin-bottom">{{metrics['num_resources']}}</h1>

test/test_app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ def test_facts_view_empty_when_no_facts(client,
781781

782782
searchable = soup.find('div', {'class': 'searchable'})
783783
vals = searchable.find_all('div', {'class': 'column'})
784-
assert len(vals) == 0
784+
assert len(vals) == 1
785785

786786

787787
def test_fact_view_with_graph(client, mocker,

0 commit comments

Comments
 (0)