From 5048662861bfcb6c8d65aa8e2119bb4865653c55 Mon Sep 17 00:00:00 2001 From: Corey Hammerton Date: Sat, 3 Sep 2016 17:10:33 -0400 Subject: [PATCH] puppetboard/app.py: Simplifying the Inventory Code (#289) * puppetboard/app.py: Simplifying the code generating and rendering the Inventory This resolves #275 This update eliminates one iteration over the resulting inventory facts that generates a multidimensional dictionary keyed by the node's certname to another dictionary of key-value pairs of the fact name and fact value. * puppetboard/templates/inventory.html: Wrapping the fact values in links to the Node page This comes as a request from #280 --- puppetboard/app.py | 40 +++++++++++----------------- puppetboard/templates/inventory.html | 10 +++---- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/puppetboard/app.py b/puppetboard/app.py index 3c7406f..73cd00b 100644 --- a/puppetboard/app.py +++ b/puppetboard/app.py @@ -307,14 +307,11 @@ def inventory(env): envs = environments() check_env(env, envs) - fact_desc = [] # a list of fact descriptions to go + headers = [] # a list of fact descriptions to go # in the table header fact_names = [] # a list of inventory fact names - factvalues = {} # values of the facts for all the nodes - # indexed by node name and fact name - nodedata = {} # a dictionary containing list of inventoried - # facts indexed by node name - nodelist = set() # a set of node names + fact_data = {} # a multidimensional dict for node and + # fact data # load the list of items/facts we want in our inventory try: @@ -328,8 +325,8 @@ def inventory(env): # generate a list of descriptions and a list of fact names # from the list of tuples inv_facts. - for description, name in inv_facts: - fact_desc.append(description) + for desc, name in inv_facts: + headers.append(desc) fact_names.append(name) query = AndOperator() @@ -344,26 +341,21 @@ def inventory(env): # get all the facts from PuppetDB facts = puppetdb.facts(query=query) - # convert the json in easy to access data structure for fact in facts: - factvalues[fact.node, fact.name] = fact.value - nodelist.add(fact.node) + if fact.node not in fact_data: + fact_data[fact.node] = {} - # generate the per-host data - for node in nodelist: - nodedata[node] = [] - for fact_name in fact_names: - try: - nodedata[node].append(factvalues[node, fact_name]) - except KeyError: - nodedata[node].append("undef") + fact_data[fact.node][fact.name] = fact.value return Response(stream_with_context( - stream_template('inventory.html', - nodedata=nodedata, - fact_desc=fact_desc, - envs=envs, - current_env=env))) + stream_template( + 'inventory.html', + headers=headers, + fact_names=fact_names, + fact_data=fact_data, + envs=envs, + current_env=env + ))) @app.route('/node/', diff --git a/puppetboard/templates/inventory.html b/puppetboard/templates/inventory.html index a3d0e14..5eedeb7 100644 --- a/puppetboard/templates/inventory.html +++ b/puppetboard/templates/inventory.html @@ -6,16 +6,16 @@ - {% for description in fact_desc %} - + {% for head in headers %} + {{head}} {% endfor %} - {% for nodename in nodedata %} + {% for node, facts in fact_data.iteritems() %} - {% for item in nodedata[nodename] %} - + {% for name in fact_names %} + {% endfor %} {% endfor %}
{{description}}
{{item}}{{facts.get(name, 'undef')}}