ok
Direktori : /proc/thread-self/root/proc/self/root/opt/alt/python37/lib/python3.7/site-packages/clwpos/ |
Current File : //proc/thread-self/root/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 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, get_visible_modules, get_allowed_suites, PremiumSuite, ALL_SUITES ) from clwpos.utils import get_server_wide_options from clwpos.user.config import UserConfig from clwpos.feature_suites import CDNSuitePro _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 is_module_allowed_for_user(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 is_module_visible_for_user(user: str, checked_modules: List[str]) -> int: """ Checks if there are any visible modules for user """ try: uid = pwd.getpwnam(user).pw_uid except KeyError: _logger.debug('Unable to get uid for %s', user) return False visible_modules = get_visible_modules(uid) return any(checked_module in visible_modules for checked_module in checked_modules) def is_suite_allowed_for_user(user: str, suites: List[str]) -> int: try: uid = pwd.getpwnam(user).pw_uid except KeyError: _logger.debug('Unable to get uid for %s', user) return False allowed_suites = get_allowed_suites(uid) return any(checked_suite in allowed_suites for checked_suite in suites) def _get_wpos_statistics_total_count( modules: List[str], kind: str = 'allowed', by_suites=False, by_users=False ): """ 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() is_allowed_method = is_suite_allowed_for_user if by_suites else is_module_allowed_for_user for user in panel_users: if kind == 'allowed': if is_allowed_method(user, modules): total_count += 1 elif kind == 'visible': if is_module_visible_for_user(user, modules): total_count += 1 else: if by_users: total_count += bool(get_sites_count_with_enabled_wpos_statistics(user, modules)) else: total_count += get_sites_count_with_enabled_wpos_statistics(user, modules) return total_count def _get_wpos_statistics_active_count(suite: str): """ Returns total count of users with active wpos module. """ total_count = 0 panel_users = cpapi.cpusers() for user in panel_users: if not is_suite_allowed_for_user(user, [suite]): continue if get_sites_count_with_enabled_wpos_statistics( user, list(ALL_SUITES[suite].primary_features)): total_count += 1 return total_count def is_accelerate_wp_icon_enabled(): """ Obtains admin`s options, e.g show_icon """ try: options = get_server_wide_options() except Exception as e: _logger.error('Error when getting admin options: %s', str(e)) return -42 return options.show_icon def fill_current_wpos_statistics(): """ Returns current statistics with enabled sites/allowed users counters per module and in total """ # this flag is kept only for backwards compatibility # and does not mean anything specific anymore is_feature_flag = os.path.isfile('/var/lve/enable-wpos.flag') is_feature_icon_enabled = is_accelerate_wp_icon_enabled() server_wide_options = get_server_wide_options() result = { 'allowed_users': dict(), # total: # object_cache: # site_optimization: 'allowed_suites': dict(), # total: # premium: # cdn # cdn_pro: 'enabled_suites': dict(), # total: # premium: # cdn # cdn_pro: 'visible_users': dict(), # total: # object_cache: # site_optimization: 'enabled_sites': dict(), # total: # object_cache: # site_optimization: 'enabled_users': dict(), # total: # object_cache: # site_optimization: 'is_accelerate_wp_flag_enabled': is_feature_flag, 'is_accelerate_wp_icon_enabled': is_feature_icon_enabled, 'features_visible_by_default': sorted(list(server_wide_options.visible_features)), 'features_allowed_by_default': sorted(list(server_wide_options.allowed_features)) } 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') result['visible_users']['total'] = _get_wpos_statistics_total_count( ALL_OPTIMIZATION_FEATURES, 'visible') for module in ALL_OPTIMIZATION_FEATURES: # rename it for more obvious stats if module == 'cdn': stats_suite = 'cdn_free' else: stats_suite = module result['allowed_users'][stats_suite] = _get_wpos_statistics_total_count( [module], 'allowed') result['enabled_sites'][stats_suite] = _get_wpos_statistics_total_count( [module], 'enabled') result['visible_users'][stats_suite] = _get_wpos_statistics_total_count( [module], 'visible') result['enabled_users'][stats_suite] = _get_wpos_statistics_total_count( [module], 'enabled', by_users=True) # additional counter how many users allowed to cdn_pro (aka 50gb) result['allowed_users']['cdn_pro'] = _get_wpos_statistics_total_count( [CDNSuitePro.name], 'allowed', by_suites=True) for suite in ALL_SUITES: # rename it for more obvious stats if suite == 'accelerate_wp_cdn': stats_suite = 'accelerate_wp_cdn_free' else: stats_suite = suite # additional counter how many users allowed to cdn_pro (aka 50gb) result['allowed_suites'][stats_suite] = \ _get_wpos_statistics_total_count([suite], 'allowed', by_suites=True) result['enabled_suites'][stats_suite] = \ _get_wpos_statistics_active_count(suite) return result