# HG changeset patch # User Pierre-Yves David # Date 1372068335 -7200 # Node ID cc3442054e48c95b796c11bd7dadbe9c64a2cd73 # Parent 614bf73cc12659ec02fb8e8529b3a4d879ad2e61 [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. diff -r 614bf73cc126 -r cc3442054e48 doc/4.0.rst --- 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 ---------------------- diff -r 614bf73cc126 -r cc3442054e48 server/__init__.py --- 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 diff -r 614bf73cc126 -r cc3442054e48 server/repository.py --- 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 diff -r 614bf73cc126 -r cc3442054e48 sobjects/services.py --- /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 . +"""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 diff -r 614bf73cc126 -r cc3442054e48 web/views/debug.py --- 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'

%s

' % _('Repository')) w(u'

%s

' % _('resources usage')) w(u'') - stats = repo.stats() + stats = self._cw.call_service('repo_stats') for element in sorted(stats): w(u'' % (element, xml_escape(unicode(stats[element])), diff -r 614bf73cc126 -r cc3442054e48 web/views/management.py --- 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]))
%s%s %s