From 6e0b215df86b6f722ddee58fe0ee9e5314a8486c Mon Sep 17 00:00:00 2001 From: Kishore Ponmuthuramalingam Date: Thu, 17 Mar 2022 08:40:13 +0530 Subject: [PATCH] feat(MetricsAdapter): add submitMetrics method --- src/MetricsAdapter.js | 49 ++++++++++++++++++++++++++++++++++++++ src/MetricsAdapter.test.js | 40 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/MetricsAdapter.js create mode 100644 src/MetricsAdapter.test.js diff --git a/src/MetricsAdapter.js b/src/MetricsAdapter.js new file mode 100644 index 0000000..ec9ebd1 --- /dev/null +++ b/src/MetricsAdapter.js @@ -0,0 +1,49 @@ +import {throwError} from 'rxjs'; + +import WebexAdapter from './WebexAdapter'; + +/** + * A metric sent to webex. + * + * @typedef {object} Metric + * @property {MetricType} type The type of metric to be captured + * @property {string} name The metric name + * @property {object} fields The data to be sent in metric + * @property {object} tags Tags for categorization + * @property {object} eventPayload Business metric payload + */ + +/** + * All potential types of a metric. + * + * @readonly + * @enum {string} + */ +export const MetricType = { + BEHAVIORAL: 'behavioral', + BUSINESS: 'business', + OPERATIONAL: 'operational', +}; + +/** + * This is a base class that defines the interface for sending metrics. + * Developers that want to extend `MetricsAdapter` must implement all of its methods, + * adhering to the exact parameters and structure of the returned objects. + * + * @interface + */ +export default class MetricsAdapter extends WebexAdapter { + /** + * submit metrics to metric service. + * + * @param {Metric} metric metric object containing type, fields, tags + * @param {string} [preLoginID] ID of person during onboarding + * @returns {external:Observable.} Observable stream that emits metric data + * @memberof MetricsAdapter + */ + submitMetrics(metric, preLoginID) { + return throwError( + new Error('submitMetrics(metric, preLoginID) must be defined in MetricsAdapter'), + ); + } +} diff --git a/src/MetricsAdapter.test.js b/src/MetricsAdapter.test.js new file mode 100644 index 0000000..99c2f6c --- /dev/null +++ b/src/MetricsAdapter.test.js @@ -0,0 +1,40 @@ +import MetricsAdapter from './MetricsAdapter'; + +describe('Metrics Adapter Interface', () => { + let metricsAdapter; + + beforeEach(() => { + metricsAdapter = new MetricsAdapter(); + }); + + afterEach(() => { + metricsAdapter = null; + }); + + describe('submitMetrics()', () => { + const eventName = 'test_event'; + const mockMetric = { + fields: { + testField: 123, + }, + tags: { + testTag: 'tag value', + }, + metricName: eventName, + type: 'behavioral', + eventPayload: {value: 'splunk business metric payload'}, + }; + + test('errors because it needs to be defined', (done) => { + const message = 'submitMetrics(metric, preLoginID) must be defined in MetricsAdapter'; + + metricsAdapter.submitMetrics(eventName, mockMetric).subscribe( + () => {}, + (error) => { + expect(error.message).toBe(message); + done(); + }, + ); + }); + }); +});