r5424 - trunk/cumin/python/cumin

croberts at fedoraproject.org croberts at fedoraproject.org
Wed Jun 27 19:38:40 UTC 2012


Author: croberts
Date: 2012-06-27 19:38:39 +0000 (Wed, 27 Jun 2012)
New Revision: 5424

Modified:
   trunk/cumin/python/cumin/stat.py
Log:
Database requests to retrieve multiple stats for a single chart are now done concurrently.  

Modified: trunk/cumin/python/cumin/stat.py
===================================================================
--- trunk/cumin/python/cumin/stat.py	2012-06-22 13:58:47 UTC (rev 5423)
+++ trunk/cumin/python/cumin/stat.py	2012-06-27 19:38:39 UTC (rev 5424)
@@ -1,7 +1,8 @@
 import cStringIO
 import logging
 from datetime import datetime, timedelta
-from time import time
+from time import time, sleep
+import threading
 
 from wooly import Widget, Page, Parameter, Attribute
 from wooly.util import StringCatalog, escape_entity, Writer
@@ -472,10 +473,12 @@
 
         signature = self.object_signature.get(session)
         
-        adapter = SamplesSqlAdapter(self.app, rosemary_class, signature, session)
         stats = [getattr(rosemary_class, x) for x in self.stats.get(session)]
-
-        return (adapter, stats)
+        adapters = dict()
+        for stat in stats:
+            adapters[stat] = SamplesSqlAdapter(self.app, rosemary_class, signature, session)
+            
+        return (adapters, stats)
     
     # builds an object that is similar to a RosemaryStatistic that can 
     # be used later in the chart rendering to give values to the chart items
@@ -533,7 +536,7 @@
                 ret["xy"] = title_xy[cached_samples[stat][1]]
                 rets[stat.name] = ret
             return str(rets)
-
+        
 class ModifiedAgentIdParameter(StringParameter):
     def get(self, session):
         agent = super(ModifiedAgentIdParameter, self).get(session)
@@ -929,7 +932,11 @@
         super(AreaChart, self).__init__(app, name, page)
 
         self.alpha = 3
-
+    
+    def getStatSamples(self, adapter, samples, stat, dur, interval, method, end_seconds_ago, delta):
+        # get more samples than needed to allow chart to clip correctly (explains the + 600)
+        samples[stat] = adapter[stat].samples(stat, dur+600, interval, method, secs2=end_seconds_ago, delta=delta)
+    
     def fetch_samples(self, adapter, dur, interval, method, mode, delta, stats, end_seconds_ago=0):
         samples = dict()
         if mode == "rate":
@@ -949,10 +956,16 @@
 
                 samples[stat] = ns
         else:
-            for stat in stats:
-                # get more samples than needed to allow chart to clip correctly
-                samples[stat] = adapter.samples(stat, dur+600, interval, method, secs2=end_seconds_ago, delta=delta)
+            threads = []
+            for i, stat in enumerate(stats):
+                # start a thread to do the db query, etc for each stat requested
+                threads.append(threading.Thread(target = self.getStatSamples, args = (adapter, samples, stat, dur, interval, method, end_seconds_ago, delta)))
+                threads[i].start()
 
+        for th in threads:
+            while th.isAlive():
+                pass
+                
         return samples
 
     def get_max_min(self, session, stats, samples, time_span=99999, end_seconds_ago=0):



More information about the cumin-developers mailing list