summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Reports/utils.py
diff options
context:
space:
mode:
authorTim Laszlo <tim.laszlo@gmail.com>2010-06-05 13:41:23 +0000
committerSol Jerome <solj@ices.utexas.edu>2010-06-07 08:42:52 -0500
commit8781c47d0ddac22192f1c233606d3a2923abd6f9 (patch)
tree0495b92a1ecc1a7fa2351a71b2c685952143b119 /src/lib/Server/Reports/utils.py
parent20f1d3918ecbb22a3452c705ee0139a1ebe1265e (diff)
downloadbcfg2-8781c47d0ddac22192f1c233606d3a2923abd6f9.tar.gz
bcfg2-8781c47d0ddac22192f1c233606d3a2923abd6f9.tar.bz2
bcfg2-8781c47d0ddac22192f1c233606d3a2923abd6f9.zip
Performance improvements for bcfg2-admin reports scrub.
Switched object updates to executemany statements. Added BatchFetch to retreive Django objects in groups. git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@5891 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server/Reports/utils.py')
-rwxr-xr-xsrc/lib/Server/Reports/utils.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lib/Server/Reports/utils.py b/src/lib/Server/Reports/utils.py
new file mode 100755
index 000000000..2ef21e446
--- /dev/null
+++ b/src/lib/Server/Reports/utils.py
@@ -0,0 +1,30 @@
+'''Helper functions for reports'''
+
+class BatchFetch(object):
+ '''Fetch Django objects in smaller batches to save memory'''
+
+ def __init__(self, obj, step=10000):
+ self.count = 0
+ self.block_count = 0
+ self.obj = obj
+ self.data = None
+ self.step = step
+ self.max = obj.count()
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ '''Return the next object from our array and fetch from the
+ database when needed'''
+ if self.block_count + self.count - self.step == self.max:
+ raise StopIteration
+ if self.block_count == 0 or self.count == self.step:
+ # Without list() this turns into LIMIT 1 OFFSET x queries
+ self.data = list(self.obj.all()[self.block_count: \
+ (self.block_count + self.step)])
+ self.block_count += self.step
+ self.count = 0
+ self.count += 1
+ return self.data[self.count - 1]
+