[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.
# 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"""importthreadingfromcubicweb.serverimportServicefromcubicweb.predicatesimportmatch_user_groupsclassStatsService(Service):"""Return a dictionary containing some statistics about the repository resources usage. """__regid__='repo_stats'__select__=match_user_groups('managers')defcall(self):repo=self._cw.repo# Service are repo side only.results={}querier=repo.queriersource=repo.system_sourceforsize,maxsize,hits,misses,titlein((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]=hitsresults['%s_cache_miss'%title]=missesresults['%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_cacheresults['nb_open_sessions']=len(repo._sessions)results['nb_active_threads']=threading.activeCount()looping_tasks=repo._tasks_manager._looping_tasksresults['looping_tasks']=', '.join(str(t)fortinlooping_tasks)results['available_cnxsets']=repo._cnxsets_pool.qsize()results['threads']=', '.join(sorted(str(t)fortinthreading.enumerate()))returnresults