annotate pylons_app/lib/timerproxy.py @ 494:b4d9680cd164 celery

some code fixes templates+helpers new rfc date without tz, added timerproxy formatting
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 22 Sep 2010 04:31:21 +0200
parents a5a17000e45b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
1 from sqlalchemy.interfaces import ConnectionProxy
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
2 import time
492
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
3 from sqlalchemy import log
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
4 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
5
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
6 def color_sql(sql):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
7 COLOR_SEQ = "\033[1;%dm"
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
8 COLOR_SQL = YELLOW
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
9 normal = '\x1b[0m'
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
10 return COLOR_SEQ % COLOR_SQL + sql + normal
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
11
172
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
12 def one_space_trim(s):
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
13 if s.find(" ") == -1:
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
14 return s
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
15 else:
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
16 s = s.replace(' ', ' ')
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
17 return one_space_trim(s)
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
18
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
19 def format_sql(sql):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
20 sql = color_sql(sql)
172
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
21 sql = sql.replace('\n', '')
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
22 sql = one_space_trim(sql)
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
23 sql = sql\
494
b4d9680cd164 some code fixes templates+helpers new rfc date without tz, added timerproxy formatting
Marcin Kuzminski <marcin@python-works.com>
parents: 492
diff changeset
24 .replace(',', ',\n\t')\
172
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
25 .replace('SELECT', '\n\tSELECT \n\t')\
236
5ba66bb4ca95 timerprox sqlformatting update for update and delete keywords
Marcin Kuzminski <marcin@python-works.com>
parents: 172
diff changeset
26 .replace('UPDATE', '\n\tUPDATE \n\t')\
5ba66bb4ca95 timerprox sqlformatting update for update and delete keywords
Marcin Kuzminski <marcin@python-works.com>
parents: 172
diff changeset
27 .replace('DELETE', '\n\tDELETE \n\t')\
172
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
28 .replace('FROM', '\n\tFROM')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
29 .replace('ORDER BY', '\n\tORDER BY')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
30 .replace('LIMIT', '\n\tLIMIT')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
31 .replace('WHERE', '\n\tWHERE')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
32 .replace('AND', '\n\tAND')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
33 .replace('LEFT', '\n\tLEFT')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
34 .replace('INNER', '\n\tINNER')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
35 .replace('INSERT', '\n\tINSERT')\
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
36 .replace('DELETE', '\n\tDELETE')
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
37 return sql
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
38
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
39
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
40 class TimerProxy(ConnectionProxy):
492
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
41
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
42 def __init__(self):
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
43 super(TimerProxy, self).__init__()
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
44 self.logging_name = 'timerProxy'
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
45 self.log = log.instance_logger(self, True)
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
46
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
47 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
492
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
48
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
49 now = time.time()
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
50 try:
492
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
51 self.log.info(">>>>> STARTING QUERY >>>>>")
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
52 return execute(cursor, statement, parameters, context)
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
53 finally:
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
54 total = time.time() - now
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
55 try:
492
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
56 self.log.info(format_sql("Query: %s" % statement % parameters))
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
57 except TypeError:
492
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
58 self.log.info(format_sql("Query: %s %s" % (statement, parameters)))
a5a17000e45b timeproxy logging memory leak fix.
Marcin Kuzminski <marcin@python-works.com>
parents: 462
diff changeset
59 self.log.info("<<<<< TOTAL TIME: %f <<<<<" % total)