Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #62 from propyless/pdb3
Browse files Browse the repository at this point in the history
update to handle pdb 3.0
  • Loading branch information
propyless committed Aug 11, 2015
2 parents a093d00 + e8a41e3 commit 8e503a6
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 181 deletions.
30 changes: 15 additions & 15 deletions pano/methods/dictfuncs.py
Expand Up @@ -91,9 +91,9 @@ def check_failed_compile(report_timestamp,

sortables = {
'certname': 0,
'catalog-timestamp': 1,
'report-timestamp': 2,
'facts-timestamp': 3,
'catalog_timestamp': 1,
'report_timestamp': 2,
'facts_timestamp': 3,
'successes': 4,
'noops': 5,
'failures': 6,
Expand Down Expand Up @@ -121,16 +121,16 @@ def append_list(n_data, s_data, m_list):
raise ValueError('Incorrect type given as input. Expects n_data, s_data as dict and m_list as list.')
m_list.append((
n_data['certname'],
filters.date(localtime(json_to_datetime(n_data['catalog-timestamp'])), 'Y-m-d H:i:s') if n_data['catalog-timestamp'] is not None else '',
filters.date(localtime(json_to_datetime(n_data['report-timestamp'])), 'Y-m-d H:i:s') if n_data['report-timestamp'] is not None else '',
filters.date(localtime(json_to_datetime(n_data['facts-timestamp'])), 'Y-m-d H:i:s') if n_data['facts-timestamp'] is not None else '',
filters.date(localtime(json_to_datetime(n_data['catalog_timestamp'])), 'Y-m-d H:i:s') if n_data['catalog_timestamp'] is not None else '',
filters.date(localtime(json_to_datetime(n_data['report_timestamp'])), 'Y-m-d H:i:s') if n_data['report_timestamp'] is not None else '',
filters.date(localtime(json_to_datetime(n_data['facts_timestamp'])), 'Y-m-d H:i:s') if n_data['facts_timestamp'] is not None else '',
s_data.get('successes', 0),
s_data.get('noops', 0),
s_data.get('failures', 0),
s_data.get('skips', 0),
))
return m_list
# if sort field is certname or catalog/report/facts-timestamp then we will sort this way
# if sort field is certname or catalog/report/facts_timestamp then we will sort this way
# or if the get_status is set to "not_all" indicating that the dashboard wants info.
if get_status != 'all':
for node in node_dict:
Expand All @@ -141,12 +141,12 @@ def append_list(n_data, s_data, m_list):
# If the node has failures
if status['failures'] > 0:
failed_list = append_list(node, status, failed_list)
if check_failed_compile(report_timestamp=node.get('report-timestamp', None),
fact_timestamp=node.get('facts-timestamp', None),
catalog_timestamp=node.get('catalog-timestamp', None)):
if check_failed_compile(report_timestamp=node.get('report_timestamp', None),
fact_timestamp=node.get('facts_timestamp', None),
catalog_timestamp=node.get('catalog_timestamp', None)):
mismatch_list = append_list(node, status, mismatch_list)
# If the node is unreported
if is_unreported(node['report-timestamp']):
if is_unreported(node['report_timestamp']):
unreported_list = append_list(node, status, unreported_list)
# If the node has noops
if status['noops'] > 0 \
Expand All @@ -160,11 +160,11 @@ def append_list(n_data, s_data, m_list):
break
if found_node is False:
# If the node is unreported
if is_unreported(node['report-timestamp']):
if is_unreported(node['report_timestamp']):
unreported_list = append_list(node, dict(), unreported_list)
if check_failed_compile(report_timestamp=node.get('report-timestamp', None),
fact_timestamp=node.get('facts-timestamp', None),
catalog_timestamp=node.get('catalog-timestamp', None)):
if check_failed_compile(report_timestamp=node.get('report_timestamp', None),
fact_timestamp=node.get('facts_timestamp', None),
catalog_timestamp=node.get('catalog_timestamp', None)):
mismatch_list = append_list(node, dict(), mismatch_list)
elif sortbycol <= 3 and get_status == 'all':
for node in node_dict:
Expand Down
88 changes: 44 additions & 44 deletions pano/methods/events.py
Expand Up @@ -45,91 +45,91 @@ def db_threaded_requests(i, q):
t_event = q.get()
if t_event['status'] == 'success':
# Classes
if not t_event['containing-class'] in summary['classes_success']:
summary['classes_success'][t_event['containing-class']] = 1
if not t_event['containing_class'] in summary['classes_success']:
summary['classes_success'][t_event['containing_class']] = 1
else:
summary['classes_success'][t_event['containing-class']] += 1
summary['classes_success'][t_event['containing_class']] += 1
# Nodes
if not t_event['certname'] in summary['nodes_success']:
summary['nodes_success'][t_event['certname']] = 1
else:
summary['nodes_success'][t_event['certname']] += 1
# Resources
if not t_event['resource-title'] in summary['resources_success']:
summary['resources_success'][t_event['resource-title']] = 1
if not t_event['resource_title'] in summary['resources_success']:
summary['resources_success'][t_event['resource_title']] = 1
else:
summary['resources_success'][t_event['resource-title']] += 1
summary['resources_success'][t_event['resource_title']] += 1
# Types
if not t_event['resource-type'] in summary['types_success']:
summary['types_success'][t_event['resource-type']] = 1
if not t_event['resource_type'] in summary['types_success']:
summary['types_success'][t_event['resource_type']] = 1
else:
summary['types_success'][t_event['resource-type']] += 1
summary['types_success'][t_event['resource_type']] += 1

if t_event['status'] == 'noop':
# Classes
if not t_event['containing-class'] in summary['classes_noop']:
summary['classes_noop'][t_event['containing-class']] = 1
if not t_event['containing_class'] in summary['classes_noop']:
summary['classes_noop'][t_event['containing_class']] = 1
else:
summary['classes_noop'][t_event['containing-class']] += 1
summary['classes_noop'][t_event['containing_class']] += 1
# Nodes
if not t_event['certname'] in summary['nodes_noop']:
summary['nodes_noop'][t_event['certname']] = 1
else:
summary['nodes_noop'][t_event['certname']] += 1
# Resources
if not t_event['resource-title'] in summary['resources_noop']:
summary['resources_noop'][t_event['resource-title']] = 1
if not t_event['resource_title'] in summary['resources_noop']:
summary['resources_noop'][t_event['resource_title']] = 1
else:
summary['resources_noop'][t_event['resource-title']] += 1
summary['resources_noop'][t_event['resource_title']] += 1
# Types
if not t_event['resource-type'] in summary['types_noop']:
summary['types_noop'][t_event['resource-type']] = 1
if not t_event['resource_type'] in summary['types_noop']:
summary['types_noop'][t_event['resource_type']] = 1
else:
summary['types_noop'][t_event['resource-type']] += 1
summary['types_noop'][t_event['resource_type']] += 1

if t_event['status'] == 'failure':
# Classes
if not t_event['containing-class'] in summary['classes_failure']:
summary['classes_failure'][t_event['containing-class']] = 1
if not t_event['containing_class'] in summary['classes_failure']:
summary['classes_failure'][t_event['containing_class']] = 1
else:
summary['classes_failure'][t_event['containing-class']] += 1
summary['classes_failure'][t_event['containing_class']] += 1
# Nodes
if not t_event['certname'] in summary['nodes_failure']:
summary['nodes_failure'][t_event['certname']] = 1
else:
summary['nodes_failure'][t_event['certname']] += 1
# Resources
if not t_event['resource-title'] in summary['resources_failure']:
summary['resources_failure'][t_event['resource-title']] = 1
if not t_event['resource_title'] in summary['resources_failure']:
summary['resources_failure'][t_event['resource_title']] = 1
else:
summary['resources_failure'][t_event['resource-title']] += 1
summary['resources_failure'][t_event['resource_title']] += 1
# Types
if not t_event['resource-type'] in summary['types_failure']:
summary['types_failure'][t_event['resource-type']] = 1
if not t_event['resource_type'] in summary['types_failure']:
summary['types_failure'][t_event['resource_type']] = 1
else:
summary['types_failure'][t_event['resource-type']] += 1
summary['types_failure'][t_event['resource_type']] += 1

if t_event['status'] == 'skipped':
# Classes
if not t_event['containing-class'] in summary['classes_skipped']:
summary['classes_skipped'][t_event['containing-class']] = 1
if not t_event['containing_class'] in summary['classes_skipped']:
summary['classes_skipped'][t_event['containing_class']] = 1
else:
summary['classes_skipped'][t_event['containing-class']] += 1
summary['classes_skipped'][t_event['containing_class']] += 1
# Nodes
if not t_event['certname'] in summary['nodes_skipped']:
summary['nodes_skipped'][t_event['certname']] = 1
else:
summary['nodes_skipped'][t_event['certname']] += 1
# Resources
if not t_event['resource-title'] in summary['resources_skipped']:
summary['resources_skipped'][t_event['resource-title']] = 1
if not t_event['resource_title'] in summary['resources_skipped']:
summary['resources_skipped'][t_event['resource_title']] = 1
else:
summary['resources_skipped'][t_event['resource-title']] += 1
summary['resources_skipped'][t_event['resource_title']] += 1
# Types
if not t_event['resource-type'] in summary['types_skipped']:
summary['types_skipped'][t_event['resource-type']] = 1
if not t_event['resource_type'] in summary['types_skipped']:
summary['types_skipped'][t_event['resource_type']] = 1
else:
summary['types_skipped'][t_event['resource-type']] += 1
summary['types_skipped'][t_event['resource_type']] += 1
out_q.put(i)
q.task_done()

Expand Down Expand Up @@ -172,16 +172,16 @@ def get_events_summary(request, timespan='latest'):
'query':
{
'operator': 'and',
1: '["=","latest-report?",true]',
2: '["in","certname",["extract","certname",["select-nodes",["null?","deactivated",true]]]]',
1: '["=","latest_report?",true]',
2: '["in","certname",["extract","certname",["select_nodes",["null?","deactivated",true]]]]',
},
}
source_url, source_certs, source_verify = get_server(request)
events = pdb_api_get(
api_url=source_url,
cert=source_certs,
verify=source_verify,
path='events/',
path='/events',
api_version='v4',
params=mk_puppetdb_query(events_params, request))
summary = summary_of_events(events)
Expand All @@ -191,27 +191,27 @@ def get_events_summary(request, timespan='latest'):
def get_report(key, value, request):
source_url, source_certs, source_verify = get_server(request)
# If key is any of the below, all is good!
allowed_keys = ['certname', 'resource-title', 'resource-type', 'containing-class']
allowed_keys = ['certname', 'resource_title', 'resource_type', 'containing_class']
if key in allowed_keys:
pass
# If key does not match above the default will be shown
else:
key = 'containing-class'
key = 'containing_class'

events_params = {
'query':
{
'operator': 'and',
1: '["=","' + key + '","' + value + '"]',
2: '["=","latest-report?",true]',
3: '["in", "certname",["extract", "certname",["select-nodes",["null?","deactivated",true]]]]'
2: '["=","latest_report?",true]',
3: '["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]'
},
}
results = pdb_api_get(
api_url=source_url,
cert=source_certs,
verify=source_verify,
path='events/',
path='/events',
api_version='v4',
params=mk_puppetdb_query(events_params, request),
)
Expand Down
42 changes: 29 additions & 13 deletions pano/puppetdb/puppetdb.py
Expand Up @@ -17,7 +17,9 @@

import urllib.parse as urlparse
import json

import requests

from pano.settings import PUPPETDB_HOST, PUPPETDB_VERIFY_SSL, PUPPETDB_CERTIFICATES, AVAILABLE_SOURCES, \
PUPPETMASTER_CLIENTBUCKET_CERTIFICATES, PUPPETMASTER_CLIENTBUCKET_HOST, PUPPETMASTER_CLIENTBUCKET_SHOW, \
PUPPETMASTER_CLIENTBUCKET_VERIFY_SSL, PUPPETMASTER_FILESERVER_CERTIFICATES, PUPPETMASTER_FILESERVER_HOST, \
Expand Down Expand Up @@ -113,6 +115,10 @@ def api_get(api_url=PUPPETDB_HOST,
:param cert: list of cert and key to use for client authentication
:return: dict
"""

query_paths = ['nodes', 'environments', 'factsets', 'facts', 'fact-names', 'fact-paths', 'fact-contents',
'catalogs', 'resources', 'edges', 'reports', 'events', 'event-counts', 'aggregate-event-counts']

if not params:
params = {}
method = method.lower()
Expand All @@ -123,16 +129,26 @@ def api_get(api_url=PUPPETDB_HOST,
methods = {
'get': requests.get,
}
if path[0] != '/':
path = '/{0}'.format(path)

if api_url[-1] != '/':
api_url = '{0}/'.format(api_url)

if path[0] == '/':
path = path.lstrip('/')

if path in query_paths:
path = 'pdb/query/v4/%s' % path
elif 'mbean' in path:
path = 'metrics/v1/%s' % path


if params:
path += '?{0}'.format(urlparse.urlencode(params))

if params is None:
return list(), list()

url = '{0}{1}'.format(api_url + api_version, path)
url = '{0}{1}'.format(api_url, path)
resp = methods[method](url,
headers=headers,
verify=verify,
Expand Down Expand Up @@ -233,12 +249,12 @@ def query_build(q_dict, user_request):

def order_by_build(ob_dict):
# 'order-by=[{"field": "value", "order": "desc"}]'
if 'order-field' not in ob_dict:
if 'order_field' not in ob_dict:
return None
if 'field' not in ob_dict['order-field'] or 'order' not in ob_dict['order-field']:
if 'field' not in ob_dict['order_field'] or 'order' not in ob_dict['order_field']:
return None
ob_query = '[{"field":"%s","order":"%s"}]' % (ob_dict['order-field']['field'],
ob_dict['order-field']['order'])
ob_query = '[{"field":"%s","order":"%s"}]' % (ob_dict['order_field']['field'],
ob_dict['order_field']['order'])
return ob_query

if type(params) is dict:
Expand All @@ -247,16 +263,16 @@ def order_by_build(ob_dict):
query_dict['query'] = query_build(params['query'], request)
elif 'query' not in params and request:
query_dict['query'] = query_build({}, request)
if 'summarize-by' in params:
query_dict['summarize-by'] = params.get('summarize-by', 'certname')
if 'summarize_by' in params:
query_dict['summarize_by'] = params.get('summarize_by', 'certname')
if 'limit' in params:
query_dict['limit'] = params.get('limit', 10)
if 'offset' in params:
query_dict['offset'] = params.get('offset', 10)
if 'include-total' in params:
query_dict['include-total'] = params.get('include-total', 'true')
if 'order-by' in params:
query_dict['order-by'] = order_by_build(params['order-by'])
if 'include_total' in params:
query_dict['include_total'] = params.get('include_total', 'true')
if 'order_by' in params:
query_dict['order_by'] = order_by_build(params['order_by'])
else:
raise TypeError('mk_puppetdb_query only accept dict() as input.')

Expand Down
2 changes: 1 addition & 1 deletion pano/static/pano/js/query-builder.min.js

Large diffs are not rendered by default.

0 comments on commit 8e503a6

Please sign in to comment.