Viewing file: stats.py (3.57 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
# -*- 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 os 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_features import ALL_OPTIMIZATION_FEATURES from clwpos.feature_suites import get_allowed_modules from clwpos.utils import get_admin_options 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 is_accelerate_wp_icon_enabled(): """ Obtains admin`s options, e.g show_icon """ try: options = get_admin_options() except Exception as e: _logger.error('Error when getting admin options: %s', str(e)) return -42 return options.get('show_icon')
def fill_current_wpos_statistics(): """ Returns current statistics with enabled sites/allowed users counters per module and in total """ is_feature_flag = os.path.isfile('/var/lve/enable-wpos.flag') is_feature_icon_enabled = is_accelerate_wp_icon_enabled() result = { 'allowed_users': dict(), # total: # object_cache: # site_optimization: 'enabled_sites': dict(), # total: # object_cache: # site_optimization: 'is_accelerate_wp_flag_enabled': is_feature_flag, 'is_accelerate_wp_icon_enabled': is_feature_icon_enabled }
result['allowed_users']['total'] = _get_wpos_statistics_total_count( ALL_OPTIMIZATION_FEATURES, 'allowed') result['enabled_sites']['total'] = _get_wpos_statistics_total_count( ALL_OPTIMIZATION_FEATURES, 'enabled')
for module in ALL_OPTIMIZATION_FEATURES: result['allowed_users'][module] = _get_wpos_statistics_total_count( [module], 'allowed') result['enabled_sites'][module] = _get_wpos_statistics_total_count( [module], 'enabled')
return result
|