Mercurial > public > src > rhodecode
annotate pylons_app/lib/timerproxy.py @ 172:83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 21 May 2010 03:01:31 +0200 |
parents | a5a3bcc5ee89 |
children | 5ba66bb4ca95 |
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')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
26 .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
|
27 .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
|
28 .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
|
29 .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
|
30 .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
|
31 .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
|
32 .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
|
33 .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
|
34 .replace('DELETE', '\n\tDELETE') |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
35 return sql |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
36 |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
37 |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
38 class TimerProxy(ConnectionProxy): |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
39 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
40 now = time.time() |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
41 try: |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
42 log.info(">>>>> STARTING QUERY >>>>>") |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
43 return execute(cursor, statement, parameters, context) |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
44 finally: |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
45 total = time.time() - now |
90
0c22a870bb79
logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents:
49
diff
changeset
|
46 try: |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
47 log.info(format_sql("Query: %s" % statement % parameters)) |
90
0c22a870bb79
logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents:
49
diff
changeset
|
48 except TypeError: |
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 %s" % (statement, parameters))) |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
50 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
|
51 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
52 |
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 |