[web] restore query logging functionality (closes #3972561)
authorJulien Cristau <julien.cristau@logilab.fr>
Wed, 02 Jul 2014 12:52:50 +0200
changeset 9876 ff98039cb4cd
parent 9868 87181b34a47f
child 9877 4a604b6e3067
[web] restore query logging functionality (closes #3972561) The query-log-file option stopped working when the web stack was moved from dbapi to repoapi.
web/application.py
web/test/unittest_application.py
--- a/web/application.py	Fri Jul 04 14:30:16 2014 +0200
+++ b/web/application.py	Wed Jul 02 12:52:50 2014 +0200
@@ -299,6 +299,21 @@
         """wrapper around _publish to log all queries executed for a given
         accessed path
         """
+        def wrap_set_cnx(func):
+            def wrap_execute(cnx):
+                orig_execute = cnx.execute
+                def execute(rql, kwargs=None, build_descr=True):
+                    tstart, cstart = time(), clock()
+                    rset = orig_execute(rql, kwargs, build_descr=build_descr)
+                    cnx.executed_queries.append((rql, kwargs, time() - tstart, clock() - cstart))
+                    return rset
+                return execute
+            def set_cnx(cnx):
+                func(cnx)
+                cnx.execute = wrap_execute(cnx)
+                cnx.executed_queries = []
+            return set_cnx
+        req.set_cnx = wrap_set_cnx(req.set_cnx)
         try:
             return self.main_handle_request(req, path)
         finally:
--- a/web/test/unittest_application.py	Fri Jul 04 14:30:16 2014 +0200
+++ b/web/test/unittest_application.py	Wed Jul 02 12:52:50 2014 +0200
@@ -19,6 +19,7 @@
 
 import base64, Cookie
 import httplib
+import tempfile
 
 from logilab.common.testlib import TestCase, unittest_main
 from logilab.common.decorators import clear_cache, classproperty
@@ -435,6 +436,14 @@
             req.form['rql'] = 'rql:Any OV1, X WHERE X custom_workflow OV1?'
             self.app_handle_request(req)
 
+    def test_log_queries(self):
+        logfile = tempfile.NamedTemporaryFile()
+        self.config.global_set_option('query-log-file', logfile.name)
+        with self.admin_access.web_request() as req:
+            self.app.handle_request(req, 'view')
+        self.assertTrue(logfile.read())
+        logfile.close()
+
 
 if __name__ == '__main__':
     unittest_main()