From 52162c04a65165269eba3a67cb94589adeb44efd Mon Sep 17 00:00:00 2001 From: Corey Hammerton Date: Fri, 4 Sep 2015 21:18:58 -0400 Subject: [PATCH] puppetboard/app.py: Optimizing the inventory page Instead of querying PuppetDB for every fact that it knows about only querying it for the fact names that it knows about. The result is a much smaller result set that is more noticable on environments with large numbers of nodes and/or facts. On one environment with > 2500 fact-names and 35 nodes a curl test to the facts endpoint without any query string returned in ~3.5 seconds. Ran another curl test to the facts endpoint with the query string that the code in this commit creates, the command finished in ~0.040 seconds. --- puppetboard/app.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/puppetboard/app.py b/puppetboard/app.py index 8a54fac..64d5526 100644 --- a/puppetboard/app.py +++ b/puppetboard/app.py @@ -201,9 +201,6 @@ def inventory(): # facts indexed by node name nodelist = set() # a set of node names - # get all the facts from PuppetDB - facts = puppetdb.facts() - # load the list of items/facts we want in our inventory try: inv_facts = app.config['INVENTORY_FACTS'] @@ -220,6 +217,13 @@ def inventory(): fact_desc.append(description) fact_names.append(name) + query = '["or", {0}]'.format( + ', '.join('["=", "name", "{0}"]'.format(name) + for name in fact_names)) + + # 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