Re: Help with getting connection.queries info

2006-11-30 Thread JP

Eric Floehr schrieb:

> from django.db import connection
> class Logging:
> def process_response(self, request, response):
> for query in connection.queries:
> print "[%s] %s" % (query['time'], "
> ".join(query['sql'].split()))
> return response
>
> Which results in all the queries being output to standard output.  Of
> course this could be improved on, but is a base.
>

A small improvement with uses coloring (a la ruby on rails):

from django.db import connection

class Logging:
def process_response(self, request, response):
from sys import stdout
if stdout.isatty():
for query in connection.queries:
print "\033[1;31m[%s]\033[0m \033[1m%s\033[0m" %
(query['time'], " ".join(query['sql'].split()))
return response

Best Regards
JP


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help with getting connection.queries info

2006-11-30 Thread Eric Floehr

Thanks James, that did the trick.  For others that may be looking, here
is my solution:

In my settings file I added below the middleware setting:

if DEBUG:
MIDDLEWARE_CLASSES += ('web.middleware.Logging',)

and in a file called "middleware.py" in my project directory (/web):

from django.db import connection
class Logging:
def process_response(self, request, response):
for query in connection.queries:
print "[%s] %s" % (query['time'], "
".join(query['sql'].split()))
return response

Which results in all the queries being output to standard output.  Of
course this could be improved on, but is a base.

Thanks again,
Eric


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help with getting connection.queries info

2006-11-29 Thread Tom Smith

I have a similar solution/problem...

Firstly, my "solution" was to create a method in my vews.py like this...

def my_render(request, template, dict):
url_time = 0
try:
for query in connection.queries:
log( query['time'] + "\t" + query['sql'])
t = float(query['time'])
url_time = url_time + t
log( str(t) + "\t" + query['sql'] ) 
except Exception, err:
log(err)
connection.queries = []
log("url_time: " + str( url_time) )
return render_to_response(template, dict)

 which is great, when I run my app in development mode... but as  
soon as I run it using FCGI/Lighttpd connection.queries is always  
empty... which is a pain because I want to watch sql queries on the  
server...

I am using the Logging module... would using logging when there are 5  
fcgi python processes cause a problem?

Why does this work fine (or magnificently even) in dev mode and not  
in FCGI mode?

Any ideas? Thanks

tom


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Re: Help with getting connection.queries info

2006-11-28 Thread James Bennett

On 11/28/06, Eric Floehr <[EMAIL PROTECTED]> wrote:
> Does this mean that connection.queries isn't "global"?  When do
> connections get created and destroyed?  How can I dump an array of ALL
> queries and query times since the server started?

The value of 'connection.queries' will persist only as long as the
database connection it's associated with, and Django closes the
connection at the end of each request/response cycle.

Your best bet for keeping a running log of all queries in all requests
over time is to write a middleware class with 'process_response'
method which grabs 'connection.queries' and logs the list of queries
to a file (this will work because response middleware is applied
before the database connection is closed).


-- 
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help with getting connection.queries info

2006-11-28 Thread Eric Floehr

Thanks James, that makes sense!

I think I still have a problem though...I was trying a simple example
of a problem I am having.  I wanted to create a page that would show
all the queries and their times, for performance checking.  So I would
do various things on the website, which would get actual objects, etc.
Then visit the page which dumped connection.queries.  This always
returned an empty array.

Does this mean that connection.queries isn't "global"?  When do
connections get created and destroyed?  How can I dump an array of ALL
queries and query times since the server started?

Thanks again!
Eric


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help with getting connection.queries info

2006-11-28 Thread James Bennett

On 11/28/06, Eric Floehr <[EMAIL PROTECTED]> wrote:
> >>> DEBUG=True
> >>> from myapp.models.references import Stuff
> >>> from django.db import connection
> >>> p = Stuff.objects.all()
> >>> print connection.queries
> []

This doesn't add anything to 'connection.queries' because it hasn't
actually executed a query yet; 'Stuff.objects.all()' is a QuerySet
object, and won't be evaluated until you attempt to access objects
contained in it. This is deliberate, so that repeated calls to
QuerySet methods like 'filter', 'extra', etc. can be used to
programmatically build up complex queries without hitting your
database until you're ready to look at the objects returned.

As soon as you access one or more objects in the QuerySet -- by
slicing, indexing, iterating, etc. -- it will execute a database query
and that query will show up in 'connection.queries'.


-- 
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---