diff --git a/packages/@webex/internal-plugin-metrics/src/new-metrics.ts b/packages/@webex/internal-plugin-metrics/src/new-metrics.ts index e771fc9cddb..2e58834516a 100644 --- a/packages/@webex/internal-plugin-metrics/src/new-metrics.ts +++ b/packages/@webex/internal-plugin-metrics/src/new-metrics.ts @@ -9,6 +9,7 @@ import CallDiagnosticMetrics from './call-diagnostic/call-diagnostic-metrics'; import BehavioralMetrics from './behavioral-metrics'; import OperationalMetrics from './operational-metrics'; import BusinessMetrics from './business-metrics'; +import PreLoginMetrics from './prelogin-metrics'; import { RecursivePartial, MetricEventProduct, @@ -45,6 +46,7 @@ class Metrics extends WebexPlugin { behavioralMetrics: BehavioralMetrics; operationalMetrics: OperationalMetrics; businessMetrics: BusinessMetrics; + preLoginMetrics: PreLoginMetrics; isReady = false; /** @@ -87,6 +89,8 @@ class Metrics extends WebexPlugin { this.webex.once('ready', () => { // @ts-ignore this.callDiagnosticMetrics = new CallDiagnosticMetrics({}, {parent: this.webex}); + // @ts-ignore + this.preLoginMetrics = new PreLoginMetrics({}, {parent: this.webex}); this.isReady = true; this.setDelaySubmitClientEvents({ shouldDelay: this.delaySubmitClientEvents, @@ -251,6 +255,38 @@ class Metrics extends WebexPlugin { return this.businessMetrics.submitBusinessEvent({name, payload, table, metadata}); } + /** + * Call Analyzer: Pre-Login Event + * @param args + */ + SubmitPreLoginEvent({ + name, + preLoginId, + payload, + metadata, + }: { + name: string; + preLoginId: string; + payload: EventPayload; + metadata?: EventPayload; + }): Promise { + if (!this.isReady) { + // @ts-ignore + this.webex.logger.log( + `NewMetrics: @submitPreLoginEvent. Attempted to submit before webex.ready: ${name}` + ); + + return Promise.resolve(); + } + + return this.preLoginMetrics.submitPreLoginEvent({ + name, + preLoginId, + payload, + metadata, + }); + } + /** * Call Analyzer: Media Quality Event * @param args diff --git a/packages/@webex/internal-plugin-metrics/src/prelogin-metrics.ts b/packages/@webex/internal-plugin-metrics/src/prelogin-metrics.ts new file mode 100644 index 00000000000..05da0577800 --- /dev/null +++ b/packages/@webex/internal-plugin-metrics/src/prelogin-metrics.ts @@ -0,0 +1,90 @@ +import GenericMetrics from './generic-metrics'; +import {EventPayload, Table} from './metrics.types'; +import PreLoginMetricsBatcher from './prelogin-metrics-batcher'; + +/** + * Builds a formatted event object for metrics submission. + * @param {string} name - Metric name + * @param {string} preLoginId - Pre-login user identifier + * @param {EventPayload} payload - Metric payload data + * @param {EventPayload} metadata - Additional metadata to include in the event + * @returns {object} Formatted metrics event object with type, eventPayload, and timestamp + */ +function buildEvent( + name: string, + preLoginId: string, + payload: EventPayload, + metadata: EventPayload +) { + const payloadWithPreLoginId = {...payload, preLoginId}; + + return { + type: ['business'], + eventPayload: { + key: name, + client_timestamp: new Date().toISOString(), + preLoginId, + ...metadata, + value: payloadWithPreLoginId, + }, + }; +} + +/** + * @description Util class to handle PreLogin Metrics + * @export + * @class PreLoginMetrics + */ +export default class PreLoginMetrics extends GenericMetrics { + // @ts-ignore + private preLoginMetricsBatcher: PreLoginMetricsBatcher; + + /** + * Constructor + * @param {any[]} args - Constructor arguments + * @constructor + */ + constructor(...args) { + super(...args); + // @ts-ignore + this.logger = this.webex.logger; + // @ts-ignore + this.preLoginMetricsBatcher = new PreLoginMetricsBatcher({}, {parent: this.webex}); + } + + /** + * Submit a business metric to our metrics endpoint. + * Routes to the correct table with the correct schema payload by table. + * @see https://confluence-eng-gpk2.cisco.com/conf/display/WAP/Business+metrics++-%3E+ROMA + * @param {Object} options - The options object + * @param {string} options.name - Name of the metric + * @param {string} options.preLoginId - ID to identify pre-login user + * @param {EventPayload} options.payload - User payload of the metric + * @param {EventPayload} [options.metadata] - Optional metadata to include outside of eventPayload.value + * @returns {Promise} Promise that resolves when the metric is submitted + */ + public submitPreLoginEvent({ + name, + preLoginId, + payload, + metadata, + }: { + name: string; + preLoginId: string; + payload: EventPayload; + metadata?: EventPayload; + }): Promise { + if (!metadata) { + metadata = {}; + } + if (!metadata.appType) { + metadata.appType = 'Web Client'; + } + + const finalEvent = buildEvent(name, preLoginId, payload, metadata); + + this.preLoginMetricsBatcher.savePreLoginId(preLoginId); + + return this.preLoginMetricsBatcher.request(finalEvent); + } +}