sobjects/services.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 05 Mar 2014 12:35:37 +0100
changeset 9576 5d4f662f5e31
parent 9035 63f3d25bab14
child 9687 00c2356faba7
permissions -rw-r--r--
[devtools] make get_repo_and_cnx return a repoapi ClientConnection And adjust callers accordingly (they need to use it as a context manager to open/close it properly).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9034
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     3
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     5
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     9
# any later version.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    10
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    14
# details.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    15
#
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    18
"""Define server side service provided by cubicweb"""
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    19
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    20
import threading
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    21
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    22
from cubicweb.server import Service
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    23
from cubicweb.predicates import match_user_groups
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    24
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    25
class StatsService(Service):
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    26
    """Return a dictionary containing some statistics about the repository
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    27
    resources usage.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    28
    """
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    29
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    30
    __regid__  = 'repo_stats'
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    31
    __select__ = match_user_groups('managers')
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    32
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    33
    def call(self):
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    34
        repo = self._cw.repo # Service are repo side only.
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    35
        results = {}
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    36
        querier = repo.querier
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    37
        source = repo.system_source
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    38
        for size, maxsize, hits, misses, title in (
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
            (len(querier._rql_cache), repo.config['rql-cache-size'],
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    40
            querier.cache_hit, querier.cache_miss, 'rqlt_st'),
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
            (len(source._cache), repo.config['rql-cache-size'],
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
            source.cache_hit, source.cache_miss, 'sql'),
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    43
            ):
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    44
            results['%s_cache_size' % title] = '%s / %s' % (size, maxsize)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    45
            results['%s_cache_hit' % title] = hits
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
            results['%s_cache_miss' % title] = misses
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    47
            results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    48
        results['type_source_cache_size'] = len(repo._type_source_cache)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    49
        results['extid_cache_size'] = len(repo._extid_cache)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    50
        results['sql_no_cache'] = repo.system_source.no_cache
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    51
        results['nb_open_sessions'] = len(repo._sessions)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    52
        results['nb_active_threads'] = threading.activeCount()
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    53
        looping_tasks = repo._tasks_manager._looping_tasks
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    54
        results['looping_tasks'] = ', '.join(str(t) for t in looping_tasks)
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    55
        results['available_cnxsets'] = repo._cnxsets_pool.qsize()
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    56
        results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate()))
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    57
        return results
9035
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    58
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    59
class GcStatsService(Service):
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    60
    """Return a dictionary containing some statistics about the repository
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    61
    resources usage.
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    62
    """
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    63
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    64
    __regid__  = 'repo_gc_stats'
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    65
    __select__ = match_user_groups('managers')
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    66
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    67
    def call(self, nmax=20):
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    68
        """Return a dictionary containing some statistics about the repository
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    69
        memory usage.
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    70
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    71
        This is a public method, not requiring a session id.
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    72
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    73
        nmax is the max number of (most) referenced object returned as
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    74
        the 'referenced' result
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    75
        """
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    76
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    77
        from cubicweb._gcdebug import gc_info
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    78
        from cubicweb.appobject import AppObject
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    79
        from cubicweb.rset import ResultSet
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    80
        from cubicweb.dbapi import Connection, Cursor
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    81
        from cubicweb.web.request import CubicWebRequestBase
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    82
        from rql.stmts import Union
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    83
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    84
        lookupclasses = (AppObject,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    85
                         Union, ResultSet,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    86
                         Connection, Cursor,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    87
                         CubicWebRequestBase)
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    88
        try:
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    89
            from cubicweb.server.session import Session, InternalSession
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    90
            lookupclasses += (InternalSession, Session)
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    91
        except ImportError:
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    92
            pass  # no server part installed
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    93
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    94
        results = {}
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    95
        counters, ocounters, garbage = gc_info(lookupclasses,
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    96
                                               viewreferrersclasses=())
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    97
        values = sorted(counters.iteritems(), key=lambda x: x[1], reverse=True)
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    98
        results['lookupclasses'] = values
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
    99
        values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax]
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   100
        results['referenced'] = values
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   101
        results['unreachable'] = len(garbage)
63f3d25bab14 [repo] move repo.gc_stats to Service API (closes #2951068)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9034
diff changeset
   102
        return results