[repo] move repo stats to Service (closes #2951067)
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 24 Jun 2013 12:05:35 +0200
changeset 9034 cc3442054e48
parent 9033 614bf73cc126
child 9035 63f3d25bab14
[repo] move repo stats to Service (closes #2951067) We currently add a new method on repo that anyone can call. Call service allows to reach the same result with: * Restricted access to manager only (thanks to selection on services), * Less unrelated code on the repo. * No need to fetch a reference to a repo object from client side. `req._cnx.repo` is not an API and config.repository() is on its way out. The old way to access this information (repo.stats()) is deprecated.
doc/4.0.rst
server/__init__.py
server/repository.py
sobjects/services.py
web/views/debug.py
web/views/management.py
--- a/doc/4.0.rst	Mon Jun 24 11:59:45 2013 +0200
+++ b/doc/4.0.rst	Mon Jun 24 12:05:35 2013 +0200
@@ -15,6 +15,9 @@
   now  synchronous. The zmq notification bus looks like a good replacement for
   most async usecase.
 
+* ``repo.stats()`` is now deprecated. The same information are available through
+  a service (``_cw.call_service('repo_stats')``)
+
 
 Deprecated Code Drops
 ----------------------
--- a/server/__init__.py	Mon Jun 24 11:59:45 2013 +0200
+++ b/server/__init__.py	Mon Jun 24 12:05:35 2013 +0200
@@ -30,8 +30,6 @@
 from logilab.common.modutils import LazyObject
 from logilab.common.textutils import splitstrip
 from logilab.common.registry import yes
-from logilab import database
-
 from yams import BASE_GROUPS
 
 from cubicweb import CW_SOFTWARE_ROOT
--- a/server/repository.py	Mon Jun 24 11:59:45 2013 +0200
+++ b/server/repository.py	Mon Jun 24 12:05:35 2013 +0200
@@ -38,6 +38,7 @@
 from time import time, localtime, strftime
 
 from logilab.common.decorators import cached, clear_cache
+from logilab.common.deprecation import deprecated
 from logilab.common.compat import any
 from logilab.common import flatten
 
@@ -511,35 +512,17 @@
 
     # public (dbapi) interface ################################################
 
+    @deprecated("[4.0] use _cw.call_service('repo_stats'")
     def stats(self): # XXX restrict to managers session?
         """Return a dictionary containing some statistics about the repository
         resources usage.
 
         This is a public method, not requiring a session id.
+
+        This method is deprecated in favor of using _cw.call_service('repo_stats')
         """
-        results = {}
-        querier = self.querier
-        source = self.system_source
-        for size, maxsize, hits, misses, title in (
-            (len(querier._rql_cache), self.config['rql-cache-size'],
-            querier.cache_hit, querier.cache_miss, 'rqlt_st'),
-            (len(source._cache), self.config['rql-cache-size'],
-            source.cache_hit, source.cache_miss, 'sql'),
-            ):
-            results['%s_cache_size' % title] =  '%s / %s' % (size, maxsize)
-            results['%s_cache_hit' % title] =  hits
-            results['%s_cache_miss' % title] = misses
-            results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses)
-        results['type_source_cache_size'] = len(self._type_source_cache)
-        results['extid_cache_size'] = len(self._extid_cache)
-        results['sql_no_cache'] = self.system_source.no_cache
-        results['nb_open_sessions'] = len(self._sessions)
-        results['nb_active_threads'] = threading.activeCount()
-        looping_tasks = self._tasks_manager._looping_tasks
-        results['looping_tasks'] = ', '.join(str(t) for t in looping_tasks)
-        results['available_cnxsets'] = self._cnxsets_pool.qsize()
-        results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
-        return results
+        with self.internal_session() as session:
+            return session.call_service('repo_stats')
 
     def gc_stats(self, nmax=20):
         """Return a dictionary containing some statistics about the repository
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sobjects/services.py	Mon Jun 24 12:05:35 2013 +0200
@@ -0,0 +1,57 @@
+# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+"""Define server side service provided by cubicweb"""
+
+import threading
+
+from cubicweb.server import Service
+from cubicweb.predicates import match_user_groups
+
+class StatsService(Service):
+    """Return a dictionary containing some statistics about the repository
+    resources usage.
+    """
+
+    __regid__  = 'repo_stats'
+    __select__ = match_user_groups('managers')
+
+    def call(self):
+        repo = self._cw.repo # Service are repo side only.
+        results = {}
+        querier = repo.querier
+        source = repo.system_source
+        for size, maxsize, hits, misses, title in (
+            (len(querier._rql_cache), repo.config['rql-cache-size'],
+            querier.cache_hit, querier.cache_miss, 'rqlt_st'),
+            (len(source._cache), repo.config['rql-cache-size'],
+            source.cache_hit, source.cache_miss, 'sql'),
+            ):
+            results['%s_cache_size' % title] = '%s / %s' % (size, maxsize)
+            results['%s_cache_hit' % title] = hits
+            results['%s_cache_miss' % title] = misses
+            results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses)
+        results['type_source_cache_size'] = len(repo._type_source_cache)
+        results['extid_cache_size'] = len(repo._extid_cache)
+        results['sql_no_cache'] = repo.system_source.no_cache
+        results['nb_open_sessions'] = len(repo._sessions)
+        results['nb_active_threads'] = threading.activeCount()
+        looping_tasks = repo._tasks_manager._looping_tasks
+        results['looping_tasks'] = ', '.join(str(t) for t in looping_tasks)
+        results['available_cnxsets'] = repo._cnxsets_pool.qsize()
+        results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
+        return results
--- a/web/views/debug.py	Mon Jun 24 11:59:45 2013 +0200
+++ b/web/views/debug.py	Mon Jun 24 12:05:35 2013 +0200
@@ -97,7 +97,7 @@
         w(u'<h2>%s</h2>' % _('Repository'))
         w(u'<h3>%s</h3>' % _('resources usage'))
         w(u'<table>')
-        stats = repo.stats()
+        stats = self._cw.call_service('repo_stats')
         for element in sorted(stats):
             w(u'<tr><th align="left">%s</th><td>%s %s</td></tr>'
                    % (element, xml_escape(unicode(stats[element])),
--- a/web/views/management.py	Mon Jun 24 11:59:45 2013 +0200
+++ b/web/views/management.py	Mon Jun 24 12:05:35 2013 +0200
@@ -181,7 +181,7 @@
     __select__ = none_rset() & match_user_groups('users', 'managers')
 
     def call(self):
-        stats = self._cw.vreg.config.repository(None).stats()
+        stats = self._cw.call_service('repo_stats')
         results = []
         for element in stats:
             results.append(u'%s %s' % (element, stats[element]))