From 3487f659ec80bda2855885c432af6d71586b3ff5 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Tue, 6 Jan 2009 15:17:46 +0200 Subject: [PATCH] Always print SQL counters. bzr revid: p_christ@hol.gr-20090106131746-mwdgfx2l1z9hd02b --- bin/sql_db.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/bin/sql_db.py b/bin/sql_db.py index 30b4df1e2b8..042c41940a3 100644 --- a/bin/sql_db.py +++ b/bin/sql_db.py @@ -59,6 +59,8 @@ def log(msg, lvl=netsvc.LOG_DEBUG): logger = netsvc.Logger() logger.notifyChannel('sql', lvl, msg) +sql_counter = 0 + class Cursor(object): IN_MAX = 1000 sql_from_log = {} @@ -72,7 +74,7 @@ class Cursor(object): @wraps(f) def wrapper(self, *args, **kwargs): if not hasattr(self, '_obj'): - raise psycopg2.ProgrammingError('Unable to use the cursor after having closing it') + raise psycopg2.ProgrammingError('Unable to use the cursor after having closed it') return f(self, *args, **kwargs) return wrapper @@ -104,7 +106,7 @@ class Cursor(object): self.count+=1 if '%d' in query or '%f' in query: log(query, netsvc.LOG_WARNING) - log("SQL queries mustn't containt %d or %f anymore. Use only %s", netsvc.LOG_WARNING) + log("SQL queries cannot contain %d or %f anymore. Use only %s", netsvc.LOG_WARNING) if params: query = query.replace('%d', '%s').replace('%f', '%s') @@ -134,19 +136,20 @@ class Cursor(object): return res def print_log(self): + global sql_counter + sql_counter += self.count def process(type): sqllogs = {'from':self.sql_from_log, 'into':self.sql_into_log} - if not sqllogs[type]: - return - sqllogitems = sqllogs[type].items() - sqllogitems.sort(key=lambda k: k[1][1]) sum = 0 - log("SQL LOG %s:" % (type,)) - for r in sqllogitems: - log("table: %s: %s/%s" %(r[0], str(r[1][1]), r[1][0])) - sum+= r[1][1] - log("SUM:%s/%d" % (sum, self.count)) - sqllogs[type].clear() + if sqllogs[type]: + sqllogitems = sqllogs[type].items() + sqllogitems.sort(key=lambda k: k[1][1]) + log("SQL LOG %s:" % (type,)) + for r in sqllogitems: + log("table: %s: %s/%s" %(r[0], str(r[1][1]), r[1][0])) + sum+= r[1][1] + sqllogs[type].clear() + log("SUM:%s/%d [%d]" % (sum, self.count,sql_counter)) process('from') process('into') self.count = 0