[repo] move repo.gc_stats to Service API (closes #2951068)
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 24 Jun 2013 12:03:37 +0200
changeset 9035 63f3d25bab14
parent 9034 cc3442054e48
child 9036 cf519fd876c6
[repo] move repo.gc_stats to Service API (closes #2951068) We currently have a method on the repo that anyone can call. `call_service` gives a similar thing, modulo: * can be restricted access to manager only (thanks to selection on services), * less unrelated code on the repository class, * no need to fetch a reference to a repo object from the 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.gc_stats()) is deprecated.
doc/4.0.rst
server/repository.py
sobjects/services.py
--- a/doc/4.0.rst	Mon Jun 24 12:05:35 2013 +0200
+++ b/doc/4.0.rst	Mon Jun 24 12:03:37 2013 +0200
@@ -18,6 +18,9 @@
 * ``repo.stats()`` is now deprecated. The same information are available through
   a service (``_cw.call_service('repo_stats')``)
 
+* ``repo.gc_stats()`` is now deprecated. The same information are available through
+  a service (``_cw.call_service('repo_gc_stats')``)
+
 
 Deprecated Code Drops
 ----------------------
--- a/server/repository.py	Mon Jun 24 12:05:35 2013 +0200
+++ b/server/repository.py	Mon Jun 24 12:03:37 2013 +0200
@@ -524,6 +524,7 @@
         with self.internal_session() as session:
             return session.call_service('repo_stats')
 
+    @deprecated("[4.0] use _cw.call_service('repo_gc_stats'")
     def gc_stats(self, nmax=20):
         """Return a dictionary containing some statistics about the repository
         memory usage.
@@ -533,33 +534,8 @@
         nmax is the max number of (most) referenced object returned as
         the 'referenced' result
         """
-
-        from cubicweb._gcdebug import gc_info
-        from cubicweb.appobject import AppObject
-        from cubicweb.rset import ResultSet
-        from cubicweb.dbapi import Connection, Cursor
-        from cubicweb.web.request import CubicWebRequestBase
-        from rql.stmts import Union
-
-        lookupclasses = (AppObject,
-                         Union, ResultSet,
-                         Connection, Cursor,
-                         CubicWebRequestBase)
-        try:
-            from cubicweb.server.session import Session, InternalSession
-            lookupclasses += (InternalSession, Session)
-        except ImportError:
-            pass # no server part installed
-
-        results = {}
-        counters, ocounters, garbage = gc_info(lookupclasses,
-                                               viewreferrersclasses=())
-        values = sorted(counters.iteritems(), key=lambda x: x[1], reverse=True)
-        results['lookupclasses'] = values
-        values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax]
-        results['referenced'] = values
-        results['unreachable'] = len(garbage)
-        return results
+        with self.internal_session() as session:
+            return session.call_service('repo_gc_stats', nmax=nmax)
 
     def get_schema(self):
         """Return the instance schema.
--- a/sobjects/services.py	Mon Jun 24 12:05:35 2013 +0200
+++ b/sobjects/services.py	Mon Jun 24 12:03:37 2013 +0200
@@ -55,3 +55,48 @@
         results['available_cnxsets'] = repo._cnxsets_pool.qsize()
         results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
         return results
+
+class GcStatsService(Service):
+    """Return a dictionary containing some statistics about the repository
+    resources usage.
+    """
+
+    __regid__  = 'repo_gc_stats'
+    __select__ = match_user_groups('managers')
+
+    def call(self, nmax=20):
+        """Return a dictionary containing some statistics about the repository
+        memory usage.
+
+        This is a public method, not requiring a session id.
+
+        nmax is the max number of (most) referenced object returned as
+        the 'referenced' result
+        """
+
+        from cubicweb._gcdebug import gc_info
+        from cubicweb.appobject import AppObject
+        from cubicweb.rset import ResultSet
+        from cubicweb.dbapi import Connection, Cursor
+        from cubicweb.web.request import CubicWebRequestBase
+        from rql.stmts import Union
+
+        lookupclasses = (AppObject,
+                         Union, ResultSet,
+                         Connection, Cursor,
+                         CubicWebRequestBase)
+        try:
+            from cubicweb.server.session import Session, InternalSession
+            lookupclasses += (InternalSession, Session)
+        except ImportError:
+            pass  # no server part installed
+
+        results = {}
+        counters, ocounters, garbage = gc_info(lookupclasses,
+                                               viewreferrersclasses=())
+        values = sorted(counters.iteritems(), key=lambda x: x[1], reverse=True)
+        results['lookupclasses'] = values
+        values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax]
+        results['referenced'] = values
+        results['unreachable'] = len(garbage)
+        return results