import { EventPayload } from '@webex/internal-plugin-metrics/src/metrics.types'; import { WebexSDK } from '../types'; import { METRIC_EVENT_NAMES } from './constants'; import { Failure } from '../services/core/GlobalTypes'; export type MetricsType = 'behavioral' | 'operational' | 'business'; /** * @class MetricsManager * @classdesc Manages the collection, batching, and submission of behavioral, operational, and business metrics for the Webex SDK. * Implements a singleton pattern to ensure a single instance throughout the application lifecycle. * * @remarks * This class is responsible for tracking, batching, and submitting various types of metric events. * It also provides utility methods for extracting common tracking fields from AQM responses. * @ignore */ export default class MetricsManager { /** * The Webex SDK instance used for submitting metrics. * @private */ private webex; /** * Stores currently running timed events. * @private */ private readonly runningEvents; /** * Queue for pending behavioral events. * @private */ private pendingBehavioralEvents; /** * Queue for pending operational events. * @private */ private pendingOperationalEvents; /** * Queue for pending business events. * @private */ private pendingBusinessEvents; /** * Indicates if the manager is ready to submit events. * @private */ private readyToSubmitEvents; /** * Lock to prevent concurrent submissions. * @private */ private submittingEvents; /** * Singleton instance of MetricsManager. * @private */ private static instance; /** * Flag to disable metrics collection. * @private */ private metricsDisabled; /** * Private constructor to enforce singleton pattern. * @private */ private constructor(); /** * Marks the manager as ready to submit events and triggers submission. * @private */ private setReadyToSubmitEvents; /** * Submits all pending events if not already submitting. * @private */ private submitPendingEvents; /** * Submits all pending behavioral events if ready. * @private */ private submitPendingBehavioralEvents; /** * Submits all pending operational events if ready. * @private */ private submitPendingOperationalEvents; /** * Submits all pending business events if ready. * @private */ private submitPendingBusinessEvents; /** * Adds a duration property to the event payload if the event was timed. * @param eventName - The name of the event. * @param options - Optional event payload. * @returns The event payload with duration if applicable. * @private */ private addDurationIfTimed; /** * Converts spaces in a string to underscores. * @param str - The input string. * @returns The string with spaces replaced by underscores. * @public * @example * MetricsManager.spacesToUnderscore('my event name'); // 'my_event_name' */ static spacesToUnderscore(str: string): string; /** * Prepares the event payload by removing empty or undefined fields and adding common metadata. * @param obj - The original event payload. * @returns The cleaned and enriched event payload. * @private */ private static preparePayload; /** * Checks if metrics collection is currently disabled. * @returns True if metrics are disabled, false otherwise. * @private */ private isMetricsDisabled; /** * Enables or disables metrics collection. Clears pending events if disabled. * @param disabled - Whether to disable metrics. * @public * @example * MetricsManager.getInstance().setMetricsDisabled(true); */ setMetricsDisabled(disabled: boolean): void; /** * Clears all pending events from the queues. * @private */ private clearPendingEvents; /** * Tracks a behavioral event and submits it if possible. * @param name - The metric event name. * @param options - Optional event payload. * @public * @example * MetricsManager.getInstance().trackBehavioralEvent('AGENT_LOGIN', {agentId: '123'}); */ trackBehavioralEvent(name: METRIC_EVENT_NAMES, options?: EventPayload): void; /** * Tracks an operational event and submits it if possible. * @param name - The metric event name. * @param options - Optional event payload. * @public * @example * MetricsManager.getInstance().trackOperationalEvent('AGENT_LOGOUT', {agentId: '123'}); */ trackOperationalEvent(name: METRIC_EVENT_NAMES, options?: EventPayload): void; /** * Tracks a business event and submits it if possible. * @param name - The metric event name. * @param options - Optional event payload. * @public * @example * MetricsManager.getInstance().trackBusinessEvent('AGENT_TRANSFER', {agentId: '123'}); */ trackBusinessEvent(name: METRIC_EVENT_NAMES, options?: EventPayload): void; /** * Tracks an event across one or more metric services. * @param name - The metric event name. * @param payload - Optional event payload. * @param metricServices - Array of metric types to track (default: ['behavioral']). * @public * @example * MetricsManager.getInstance().trackEvent('AGENT_LOGIN', {agentId: '123'}, ['behavioral', 'operational']); */ trackEvent(name: METRIC_EVENT_NAMES, payload?: EventPayload, metricServices?: MetricsType[]): void; /** * Starts timing for one or more event keys. * @param keys - A string or array of strings representing event keys. * @public * @example * MetricsManager.getInstance().timeEvent('AGENT_LOGIN'); * MetricsManager.getInstance().timeEvent(['AGENT_LOGIN', 'AGENT_LOGOUT']); */ timeEvent(keys: string | string[]): void; /** * Sets the Webex SDK instance and marks the manager as ready when the SDK is ready. * @param webex - The Webex SDK instance. * @private */ private setWebex; /** * Returns the singleton instance of MetricsManager, initializing it if necessary. * @param options - Optional object containing the Webex SDK instance. * @returns The singleton MetricsManager instance. * @public * @example * const metrics = MetricsManager.getInstance({webex}); */ static getInstance(options?: { webex: WebexSDK; }): MetricsManager; /** * Resets the singleton instance of MetricsManager. Useful for testing. * @public * @example * MetricsManager.resetInstance(); */ static resetInstance(): void; /** * Extracts common tracking fields from an AQM response object. * @param response - The AQM response object. * @returns An object containing common tracking fields. * @public * @example * const fields = MetricsManager.getCommonTrackingFieldForAQMResponse(response); */ static getCommonTrackingFieldForAQMResponse(response: any): Record; /** * Extracts common tracking fields from an AQM failure response object. * @param failureResponse - The AQM failure response object. * @returns An object containing common tracking fields for failures. * @public * @example * const fields = MetricsManager.getCommonTrackingFieldForAQMResponseFailed(failureResponse); */ static getCommonTrackingFieldForAQMResponseFailed(failureResponse: Failure): Record; }