%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/self/root/opt/alt/python37/lib/python3.7/site-packages/clwpos/
Upload File :
Create Path :
Current File : //proc/self/root/opt/alt/python37/lib/python3.7/site-packages/clwpos/stats.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT


import pwd
from typing import List

from clcommon import cpapi
from clcommon.clpwd import drop_privileges
from clwpos.logsetup import setup_logging
from clwpos.optimization_modules import (
    get_allowed_modules,
    ALL_OPTIMIZATION_MODULES
)
from clwpos.user.config import UserConfig

_logger = setup_logging('clwpos_statistics')


def get_sites_count_with_enabled_wpos_statistics(user: str, checked_modules: List[str]) -> int:
    """
    Calculates total count of sites with at least one enabled
    module per user
    """
    count = 0
    try:
        with drop_privileges(user):
            uc = UserConfig(user)
            count = uc.get_enabled_sites_count_by_modules(checked_modules)
    except Exception as e:
        _logger.warning('Error while getting info from clwpos user config %s', str(e))
        return count
    return count


def get_users_count_with_allowed_wpos_statistics(user: str, checked_modules: List[str]) -> int:
    """
    Checks if there are any allowed modules for user
    """
    try:
        uid = pwd.getpwnam(user).pw_uid
    except KeyError:
        _logger.debug('Unable to get uid for %s', user)
        return False
    allowed_modules = get_allowed_modules(uid)
    return any(checked_module in allowed_modules for checked_module in checked_modules)


def _get_wpos_statistics_total_count(modules: List[str], kind: str = 'allowed'):
    """
    Returns total count of users with allowed wpos module
    or total count of sites with enabled wpos module
    for all panel users on server
    """
    total_count = 0
    panel_users = cpapi.cpusers()
    for user in panel_users:
        if kind == 'allowed':
            if get_users_count_with_allowed_wpos_statistics(user, modules):
                total_count += 1
        else:
            total_count += get_sites_count_with_enabled_wpos_statistics(user, modules)
    return total_count


def fill_current_wpos_statistics():
    """
    Returns current statistics with enabled sites/allowed users counters
    per module and in total
    """
    result = {
        'allowed_users': dict(),
            # total:
            # object_cache:
            # site_optimization:
        'enabled_sites': dict()
            # total:
            # object_cache:
            # site_optimization:
    }

    result['allowed_users']['total'] = _get_wpos_statistics_total_count(
        ALL_OPTIMIZATION_MODULES,
        'allowed')
    result['enabled_sites']['total'] = _get_wpos_statistics_total_count(
        ALL_OPTIMIZATION_MODULES, 'enabled')

    for module in ALL_OPTIMIZATION_MODULES:
        result['allowed_users'][module] = _get_wpos_statistics_total_count(
            [module], 'allowed')
        result['enabled_sites'][module] = _get_wpos_statistics_total_count(
            [module], 'enabled')

    return result

Zerion Mini Shell 1.0