Skip to content

Commit

Permalink
Add option to get nodes without using event-counts
Browse files Browse the repository at this point in the history
As event-counts query may be very slow and has experimental
status it is beneficial for some users to get nodes without
exact number of changes resources in the last report
for compatibility and performance benefit.
  • Loading branch information
Greg Dubicki committed Jun 16, 2019
1 parent 1fae0a3 commit 3d36e90
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions pypuppetdb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,27 @@ def _query(self, endpoint, path=None, query=None,

# Method stubs

def nodes(self, unreported=2, with_status=False, **kwargs):
def nodes(self, unreported=2, with_status=False, with_event_numbers=True,
**kwargs):
"""Query for nodes by either name or query. If both aren't
provided this will return a list of all nodes. This method
also fetches the nodes status and event counts of the latest
report from puppetdb.
also (optionally) fetches the nodes status and (optionally)
event counts of the latest report from puppetdb.
:param with_status: (optional) include the node status in the\
returned nodes
:type with_status: :bool:
:param unreported: (optional) amount of hours when a node gets
marked as unreported
:type unreported: :obj:`None` or integer
:param with_event_numbers: (optional) include the exact number of\
changed/unchanged/failed/noop events when\
with_status is set to True. If set to False
only "some" string is provided if there are
resources with such status in the last report.
This provides performance benefits as potentially
slow event-counts query is omitted completely.
:type with_event_numbers: :bool:
:param \*\*kwargs: The rest of the keyword arguments are passed
to the _query function
Expand All @@ -413,7 +422,7 @@ def nodes(self, unreported=2, with_status=False, **kwargs):
if type(nodes) == dict:
nodes = [nodes, ]

if with_status:
if with_status and with_event_numbers:
latest_events = self.event_counts(
query=EqualsOperator("latest_report?", True),
summarize_by='certname'
Expand All @@ -424,25 +433,39 @@ def nodes(self, unreported=2, with_status=False, **kwargs):
node['events'] = None

if with_status:
status = [s for s in latest_events
if s['subject']['title'] == node['certname']]
if with_event_numbers:
status = [s for s in latest_events
if s['subject']['title'] == node['certname']]

try:
try:
node['status_report'] = node['latest_report_status']

if status:
node['events'] = status[0]
except KeyError:
if status:
node['events'] = status = status[0]
if status['successes'] > 0:
node['status_report'] = 'changed'
if status['noops'] > 0:
node['status_report'] = 'noop'
if status['failures'] > 0:
node['status_report'] = 'failed'
else:
node['status_report'] = 'unchanged'
else:
node['status_report'] = node['latest_report_status']

if status:
node['events'] = status[0]
except KeyError:
if status:
node['events'] = status = status[0]
if status['successes'] > 0:
node['status_report'] = 'changed'
if status['noops'] > 0:
node['status_report'] = 'noop'
if status['failures'] > 0:
node['status_report'] = 'failed'
else:
node['status_report'] = 'unchanged'
node['events'] = {
'successes': 0,
'failures': 0,
'noops': 0,
}
if node['status_report'] == 'changed':
node['events']['successes'] = 'some'
elif node['status_report'] == 'noop':
node['events']['noops'] = 'some'
elif node['status_report'] == 'failed':
node['events']['failures'] = 'some'

# node report age
if node['report_timestamp'] is not None:
Expand Down

0 comments on commit 3d36e90

Please sign in to comment.