[open-ils-commits] r790 - constrictor/trunk/constrictor (erickson)

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Feb 21 22:49:59 EST 2010


Author: erickson
Date: 2010-02-21 22:49:57 -0500 (Sun, 21 Feb 2010)
New Revision: 790

Added:
   constrictor/trunk/constrictor/data.py
Modified:
   constrictor/trunk/constrictor/task.py
Log:
missed the new data file in previous commit.  also, track task start time on the task object

Added: constrictor/trunk/constrictor/data.py
===================================================================
--- constrictor/trunk/constrictor/data.py	                        (rev 0)
+++ constrictor/trunk/constrictor/data.py	2010-02-22 03:49:57 UTC (rev 790)
@@ -0,0 +1,99 @@
+# -----------------------------------------------------------------------
+# Copyright (C) 2010  Equinox Software Inc.
+# Bill Erickson <erickson at esilibrary.com>
+# 
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 3
+# of the License, or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# -----------------------------------------------------------------------
+
+from sqlalchemy import *
+import constrictor.log as log
+import time
+
+class Data(object):
+
+    singleton = None
+
+    def __init__(self, db_file):
+        if Data.singleton is None:
+            log.logInfo("sqlite:////%s" % db_file)
+            self.engine = create_engine('sqlite:////%s' % db_file)
+            self.meta = MetaData()
+            self.meta.bind = self.engine
+            self.runtime_data = []
+            self.task_set_id = None
+            Data.singleton = self
+
+    def disconnect(self):
+        if self.engine is not None:
+            self.engine.dispose()
+
+    def get_instance():
+        return Data.singleton
+    get_instance = staticmethod(get_instance)
+
+    def add_task(self, task):
+        self.runtime_data.append(task.to_dict())
+
+    def create_schema(self, force=False):
+
+        self.task_table = Table('task', self.meta,
+            Column('id', Integer, primary_key = True),
+            Column('task_name', Text, nullable = False),
+            Column('task_set', Integer, nullable = False),
+            Column('run_time', Text, nullable = False),
+            Column('thread_id', Integer, nullable = False),
+            Column('duration', Float, nullable = False),
+            Column('success', Boolean, nullable = False)
+        )
+
+        self.task_set_table = Table('task_set', self.meta,
+            Column('id', Integer, primary_key=True),
+            Column('start_time', Text),
+            Column('end_time', Text),
+        )
+
+        if force:
+            self.meta.drop_all()
+
+        self.meta.create_all()
+ 
+
+    def create_task_set(self):
+        res = self.engine.execute(
+            self.task_set_table.insert().values(start_time = time.time())
+        )
+        #self.task_set_id = res.inserted_primary_key
+        self.task_set_id = res.last_inserted_ids()[0]
+        res.close()
+        
+    def store_data(self):
+
+        log.logInfo("Inserting data into data store...")
+
+        # close out the task set
+        self.engine.execute(
+            self.task_set_table.update().values(end_time = time.time())
+        ).close()
+
+        # insert all of the task data
+        for task in self.runtime_data:
+            self.engine.execute(
+                self.task_table.insert().values(
+                    task_name = task['name'],
+                    task_set = self.task_set_id,
+                    run_time = task['run_time'],
+                    thread_id = task['thread_id'],
+                    duration = '%0.3f' % task['duration'],
+                    success = task['success']
+                )
+            ).close()
+
+

Modified: constrictor/trunk/constrictor/task.py
===================================================================
--- constrictor/trunk/constrictor/task.py	2010-02-22 03:10:26 UTC (rev 789)
+++ constrictor/trunk/constrictor/task.py	2010-02-22 03:49:57 UTC (rev 790)
@@ -37,6 +37,7 @@
         self.duration = 0
         self.success = False
         self.complete = False
+        self.run_time = None
 
     def run(self, **kwargs):
         """Override this method with the work to perform"""
@@ -46,11 +47,11 @@
 
         self.reset()
         ret = None # capture the return value from the task
-        start = time.time()
+        self.run_time = time.time()
 
         try:
             ret = self.run(**kwargs)
-            self.duration = time.time() - start
+            self.duration = time.time() - self.run_time
             self.complete = True
             self.success = True
 
@@ -70,6 +71,7 @@
             'name' : self.name,
             'duration' : self.duration,
             'success' : self.success,
+            'run_time' : self.run_time,
             'thread_id' : ScriptThread.getThreadID()
         }
 



More information about the open-ils-commits mailing list