annotate pylons_app/lib/timerproxy.py @ 368:e9a6783f5502

fixed user permissions bug when adding permissions to user who couldn load those because of auth decorators Small fix for hg model and injecting dbrepo into cached repos
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 27 Jul 2010 14:54:41 +0200
parents 5ba66bb4ca95
children 298546182b41
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
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
3 import logging
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
4 log = logging.getLogger('timerproxy')
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
5 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
6
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
7 def color_sql(sql):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
8 COLOR_SEQ = "\033[1;%dm"
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
9 COLOR_SQL = YELLOW
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
10 normal = '\x1b[0m'
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
11 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
12
172
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
13 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
14 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
15 return s
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
16 else:
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
17 s = s.replace(' ', ' ')
83c7ee1b5f5c improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents: 153
diff changeset
18 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
19
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
20 def format_sql(sql):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
21 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
22 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
23 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
24 sql = sql\
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):
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
41 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
42 now = time.time()
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
43 try:
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
44 log.info(">>>>> STARTING QUERY >>>>>")
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
45 return execute(cursor, statement, parameters, context)
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
46 finally:
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
47 total = time.time() - now
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
48 try:
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
49 log.info(format_sql("Query: %s" % statement % parameters))
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
50 except TypeError:
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
51 log.info(format_sql("Query: %s %s" % (statement, parameters)))
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
52 log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
53
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
54
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
55
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
56