%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/thread-self/root/opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/
Upload File :
Create Path :
Current File : //proc/thread-self/root/opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/stats.py

# -*- coding: utf-8 -*-
import json
import logging
# 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.constants import CLWPOS_WHMCS_STATS_FILE
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,
    AWPSuite
)
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',
        allowed_by_modules=False,
        allowed_by_suites: List[str] = None,
        per_user=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()

    for user in panel_users:
        if allowed_by_modules and not is_module_allowed_for_user(user, modules):
            continue

        if allowed_by_suites is not None and not is_suite_allowed_for_user(user, allowed_by_suites):
            continue

        if kind == 'allowed':
            total_count += 1
        elif kind == 'visible':
            if is_module_visible_for_user(user, modules):
                total_count += 1
        else:
            if per_user:
                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_premium_potential_upgrades_statistics():
    """

    """
    panel_users = cpapi.cpusers()

    visible_premium = 0
    disallowed_premium = 0
    for user in panel_users:
        # skip users that do not have accelerate wp free enabled
        if not get_sites_count_with_enabled_wpos_statistics(user, AWPSuite.feature_set):
            continue

        if is_module_visible_for_user(user, PremiumSuite.feature_set):
            # if any of the modules are already installed, that is not what we are looking for
            if get_sites_count_with_enabled_wpos_statistics(user, PremiumSuite.feature_set):
                continue
            visible_premium += 1
        else:
            disallowed_premium += 1

    return visible_premium, disallowed_premium


def _get_wpos_statistics_active_count(suite: str, features_by_license=None):
    """
    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

        counted_features = list(ALL_SUITES[suite].primary_features)

        if features_by_license == 'billable':
            counted_features = list(ALL_SUITES[suite].billable_features)
        elif features_by_license == 'non_billable':
            counted_features = list(ALL_SUITES[suite].non_billable_features())

        if get_sites_count_with_enabled_wpos_statistics(
                user, counted_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 _read_whmcs_statistics():
    """
    Reads whmcs statistics file and returns data as dictionary.
    """
    try:
        with open(CLWPOS_WHMCS_STATS_FILE, 'r') as f:
            raw_data = f.read()
    except FileNotFoundError:
        # when file does not exists, it's fine
        return None

    try:
        data = json.loads(raw_data)
    except (json.JSONDecodeError, ValueError):
        logging.error('Malformed whmcs data: %s', raw_data)
        return None

    return data


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()

    disabled_status = {True: 'disabled', False: 'enabled', None: 'none'}

    server_wide_options = get_server_wide_options()

    awp_enabled_visible_premium, awp_enabled_disallowed_premium = (
        _get_premium_potential_upgrades_statistics())
    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,
        'accelerate_wp_suite_enabled_premium_suite_visible': awp_enabled_visible_premium,
        'accelerate_wp_suite_enabled_premium_suite_disallowed': awp_enabled_disallowed_premium,
        'features_visible_by_default': sorted(list(server_wide_options.visible_features)),
        'features_allowed_by_default': sorted(list(server_wide_options.allowed_features)),
        'upgrade_urls': {
            PremiumSuite.name: server_wide_options.upgrade_url,
            CDNSuitePro.name: server_wide_options.upgrade_url_cdn
        },
        'is_smart_advice_notifications_enabled': disabled_status.get(server_wide_options.disable_smart_advice_notifications),
        'is_smart_advice_plugin_installation_enabled': disabled_status.get(server_wide_options.disable_smart_advice_wordpress_plugin),
        'is_smart_advice_reminders_enabled': disabled_status.get(server_wide_options.disable_smart_advice_reminders),
        'is_object_cache_banners_enabled': disabled_status.get(server_wide_options.disable_object_cache_banners),
        'whmcs': _read_whmcs_statistics(),
    }

    result['allowed_users']['total'] = _get_wpos_statistics_total_count(
        ALL_OPTIMIZATION_FEATURES, 'allowed', allowed_by_modules=True)
    result['enabled_sites']['total'] = _get_wpos_statistics_total_count(
        ALL_OPTIMIZATION_FEATURES, 'enabled', allowed_by_modules=True)
    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', allowed_by_modules=True)
        result['enabled_sites'][stats_suite] = _get_wpos_statistics_total_count(
            [module], 'enabled', allowed_by_modules=True)
        result['visible_users'][stats_suite] = _get_wpos_statistics_total_count(
            [module], 'visible')
        result['enabled_users'][stats_suite] = _get_wpos_statistics_total_count(
            [module], 'enabled', allowed_by_modules=True, per_user=True)
    # additional counters for cdn_pro (aka 50gb)
    result['allowed_users']['cdn_pro'] = _get_wpos_statistics_total_count(
        ['cdn'], 'allowed', allowed_by_suites=[CDNSuitePro.name])
    result['enabled_sites']['cdn_pro'] = _get_wpos_statistics_total_count(
        ['cdn'], 'enabled', allowed_by_suites=[CDNSuitePro.name], allowed_by_modules=True)
    result['visible_users']['cdn_pro'] = _get_wpos_statistics_total_count(
        ['cdn'], 'visible', allowed_by_suites=[CDNSuitePro.name])
    result['enabled_users']['cdn_pro'] = _get_wpos_statistics_total_count(
        ['cdn'], 'enabled', allowed_by_suites=[CDNSuitePro.name], allowed_by_modules=True, per_user=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([], 'allowed', allowed_by_suites=[suite])
        result['enabled_suites'][stats_suite] = \
            _get_wpos_statistics_active_count(suite)

        if suite == PremiumSuite.name:
            # fill additional billable / non-billable counters:
            # object_cache is free, image optimization, critical css is billable
            result['enabled_suites'][f'{stats_suite}_billable'] = \
            _get_wpos_statistics_active_count(suite, features_by_license='billable')
            result['enabled_suites'][f'{stats_suite}_non_billable'] =\
                _get_wpos_statistics_active_count(suite, features_by_license='non_billable')

    # cdn_free exclusive of cdn_pro
    _mutually_exclusive_wpos_statistics(
        result,
        ['enabled_suites', 'enabled_sites', 'enabled_users'],
        'cdn_free',
        'cdn_pro'
    )

    return result


def _mutually_exclusive_wpos_statistics(wpos_statistics: dict, statistic_keys: list, from_stat: str, subtract_stat: str):
    """
    Returns mutually exclusive statistics
    """

    from_stat_awp = f'accelerate_wp_{from_stat}'
    subtract_awp = f'accelerate_wp_{subtract_stat}'

    for key, stat in wpos_statistics.items():
        if key in statistic_keys and type(stat) is dict:
            if from_stat in stat and subtract_stat in stat:
                stat[from_stat] -= stat[subtract_stat]

            if from_stat_awp in stat and subtract_awp in stat:
                stat[from_stat_awp] -= stat[subtract_awp]

Zerion Mini Shell 1.0