Source

pyramid_perfstat / pyramid_perfstat / schemas / models.py

#!/usr/bin/python
# -*- coding: utf-8 -*- 
'''
Created on 11 mai 2012

@author: sbard
'''
from sqlalchemy import Table, Column, Sequence, DateTime, Integer, Unicode, \
    ForeignKey
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relation, \
    column_property
from sqlalchemy.sql import and_
from zope.sqlalchemy import ZopeTransactionExtension


StatBase = declarative_base()

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

def initialize_sql(engine):
    if engine is None :
        # use default
        engine = create_engine('sqlite://perf_app.db')
    
    DBSession.configure(bind=engine)
    StatBase.metadata.bind = engine
    StatBase.metadata.create_all(engine)

class SqlRecordAvg(StatBase):
    """
    record request compute duration
    record sigma sql duration
    min, average, max
    """
    
##    duration_avg = Column(Integer, primary_key=True)
##    duration_min = Column(Integer, primary_key=True)
##    duration_max = Column(Integer, primary_key=True)
## 
##    duration_cpt =
##    sql_duration_avg = 
##    sql_duration_min =
##    sql_duration_max =
##    sql_nb_queries_avg =
##    sql_nb_queries_min =
##    sql_nb_queries_max =  
    
    def __init__(self, (duration_avg,
                        duration_min,
                        duration_max,
                        duration_cpt,
                        sql_duration_avg,
                        sql_duration_min,
                        sql_duration_max,
                        sql_nb_queries_avg,
                        sql_nb_queries_min,
                        sql_nb_queries_max)):
        """
        """
        
        self.duration_avg = duration_avg
        self.duration_min = duration_min
        self.duration_max = duration_max

        if self.duration_cpt is None :
           self.duration_cpt = 1
        else :
           self.duration_cpt = duration_cpt

        if self.duration_avg is None :
           self.init = True
        else :
           self.init = False

        self.sql_duration_avg = sql_duration_avg
        self.sql_duration_min = sql_duration_min
        self.sql_duration_max = sql_duration_max
        
        self.sql_nb_queries_avg = sql_nb_queries_avg
        self.sql_nb_queries_min = sql_nb_queries_min
        self.sql_nb_queries_max = sql_nb_queries_max

    def get_data(self):
        """
        """
        return [ self.duration_avg,
                 self.duration_min,
                 self.duration_max,
                 self.duration_cpt,
                 self.sql_duration_avg,
                 self.sql_duration_min,
                 self.sql_duration_max,
                 self.sql_nb_queries_avg,
                 self.sql_nb_queries_min,
                 self.sql_nb_queries_max ]


    def upkeep(self, old_avg, new_data, old_nb):
        """
        old_avg : the last average we'd like to extend
        new_data : the new recorded data which extend the average
        old_nb : the size of data list which produce old_avg
        """
        return (old_avg+(new_data/float(old_nb)))*(float(old_nb)/float(old_nb+1))

    def cumulative_upkeep(self, new_duration, new_sql_duration, nb_queries):
        """
        """
        # update min/max 
        if self.duration_min is not None and new_duration < self.duration_min :
                  self.duration_min = new_duration
        elif self.duration_min is None :
                  self.duration_min = new_duration
    
        if self.duration_max is not None and new_duration > self.duration_max :
                  self.duration_max = new_duration
        elif self.duration_min is None :
                  self.duration_min = new_duration
    
        if self.duration_avg is None :
             self.duration_avg = new_duration
        else :
             self.duration_avg = self.upkeep(self.duration_avg, new_duration, self.duration_cpt)
    
        if self.sql_duration_min is not None and new_sql_duration < self.sql_duration_min :
              self.sql_duration_min = new_sql_duration
        elif self.sql_duration_min is None :
              self.sql_duration_min = new_sql_duration
    
        if self.sql_duration_max is not None and new_sql_duration > self.sql_duration_max :
              self.sql_duration_max = new_sql_duration
        elif self.sql_duration_min is None :
              self.sql_duration_min = new_sql_duration
            
        if nb_queries < self.sql_nb_queries_min :
              self.sql_nb_queries_min = nb_queries
        if nb_queries > self.sql_nb_queries_max :
              self.sql_nb_queries_max = nb_queries
    
        if self.sql_duration_avg is None :
           self.sql_duration_avg = new_sql_duration
        else :
           self.sql_duration_avg = self.upkeep(self.sql_duration_avg, new_sql_duration, self.duration_cpt)
    
        self.duration_cpt+=1