From 7e84acd1d27a16ff68519c3c3bc0fe8c0db6d9d1 Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 22 Feb 2010 03:49:57 +0000 Subject: [PATCH] missed the new data file in previous commit. also, track task start time on the task object git-svn-id: svn://svn.open-ils.org/ILS-Contrib/constrictor/trunk@790 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- constrictor/data.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ constrictor/task.py | 6 ++-- 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 constrictor/data.py diff --git a/constrictor/data.py b/constrictor/data.py new file mode 100644 index 000000000..0724218fb --- /dev/null +++ b/constrictor/data.py @@ -0,0 +1,99 @@ +# ----------------------------------------------------------------------- +# Copyright (C) 2010 Equinox Software Inc. +# Bill Erickson +# +# 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() + + diff --git a/constrictor/task.py b/constrictor/task.py index 712629904..98dd2cb4e 100644 --- a/constrictor/task.py +++ b/constrictor/task.py @@ -37,6 +37,7 @@ class Task(object): 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 @@ class Task(object): 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 @@ class Task(object): 'name' : self.name, 'duration' : self.duration, 'success' : self.success, + 'run_time' : self.run_time, 'thread_id' : ScriptThread.getThreadID() } -- 2.11.0