|
| 1 | +# Copyright 2024-2025 Wingify Software Pvt. Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Dict, Any, Union |
| 16 | +import sys |
| 17 | +from ..constants.Constants import Constants |
| 18 | +from ..packages.logger.enums.log_level_number_enum import LogLevelNumberEnum |
| 19 | + |
| 20 | + |
| 21 | +class UsageStatsUtil: |
| 22 | + """ |
| 23 | + Manages usage statistics for the SDK. |
| 24 | + Tracks various features and configurations being used by the client. |
| 25 | + Implements Singleton pattern to ensure a single instance. |
| 26 | + """ |
| 27 | + |
| 28 | + _instance = None |
| 29 | + |
| 30 | + def __new__(cls): |
| 31 | + """ |
| 32 | + Ensures only one instance of UsageStatsUtil exists (Singleton pattern). |
| 33 | + """ |
| 34 | + if cls._instance is None: |
| 35 | + cls._instance = super(UsageStatsUtil, cls).__new__(cls) |
| 36 | + cls._instance._usage_stats_data = {} |
| 37 | + return cls._instance |
| 38 | + |
| 39 | + def set_usage_stats(self, options: Dict[str, Any]) -> None: |
| 40 | + """ |
| 41 | + Sets usage statistics based on provided options. |
| 42 | + Maps various SDK features and configurations to boolean flags. |
| 43 | +
|
| 44 | + Args: |
| 45 | + options (Dict[str, Any]): Configuration options for the SDK containing: |
| 46 | + - storage: Storage service configuration |
| 47 | + - logger: Logger configuration |
| 48 | + - batch_events: Event batching configuration |
| 49 | + - integrations: Integrations configuration |
| 50 | + - polling_interval: Polling interval configuration |
| 51 | + - sdk_name: SDK name configuration |
| 52 | + """ |
| 53 | + data: Dict[str, Union[str, int]] = {} |
| 54 | + |
| 55 | + # Map configuration options to usage stats flags |
| 56 | + if options.get("integrations"): |
| 57 | + data["ig"] = 1 # Integration enabled |
| 58 | + if options.get("batch_events"): |
| 59 | + data["eb"] = 1 # Event batching enabled |
| 60 | + |
| 61 | + if options.get("gateway_service"): |
| 62 | + data["gs"] = 1 # Gateway service enabled |
| 63 | + |
| 64 | + # Check for custom logger |
| 65 | + logger = options.get("logger") |
| 66 | + if logger and (logger.get("transport") or logger.get("transports")): |
| 67 | + data["cl"] = 1 |
| 68 | + |
| 69 | + if options.get("storage"): |
| 70 | + data["ss"] = 1 # Storage service configured |
| 71 | + |
| 72 | + if logger and logger.get("level"): |
| 73 | + data["ll"] = getattr( |
| 74 | + LogLevelNumberEnum, logger["level"].upper(), LogLevelNumberEnum.ERROR |
| 75 | + ) # Default to -1 if level is not recognized |
| 76 | + |
| 77 | + if options.get("poll_interval"): |
| 78 | + data["pi"] = 1 # Polling interval configured |
| 79 | + |
| 80 | + # Check for _vwo_meta.ea |
| 81 | + vwo_meta = options.get("_vwo_meta") |
| 82 | + if vwo_meta and vwo_meta.get("ea"): |
| 83 | + data["_ea"] = 1 |
| 84 | + |
| 85 | + # Add Python version if available |
| 86 | + if hasattr(sys, "version"): |
| 87 | + data["lv"] = sys.version |
| 88 | + |
| 89 | + # Check threading configuration |
| 90 | + threading_config = options.get("threading") |
| 91 | + if not threading_config or ( |
| 92 | + threading_config and threading_config.get("enabled") is True |
| 93 | + ): |
| 94 | + data["th"] = 1 |
| 95 | + # Check if max_workers is passed |
| 96 | + if threading_config and threading_config.get("max_workers"): |
| 97 | + data["th_mps"] = threading_config["max_workers"] |
| 98 | + |
| 99 | + self._usage_stats_data = data |
| 100 | + |
| 101 | + def get_usage_stats(self) -> Dict[str, Union[bool, str, int]]: |
| 102 | + """ |
| 103 | + Retrieves the current usage statistics. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + Dict[str, Union[bool, str, int]]: Dictionary containing boolean flags for various SDK features in use |
| 107 | + """ |
| 108 | + return self._usage_stats_data |
| 109 | + |
| 110 | + def clear_usage_stats(self) -> None: |
| 111 | + """ |
| 112 | + Clears the usage statistics data. |
| 113 | + """ |
| 114 | + self._usage_stats_data = {} |
0 commit comments