Skip to content

Commit

Permalink
feat(MetricsAdapter): add submitMetrics method
Browse files Browse the repository at this point in the history
  • Loading branch information
kponmuth authored and lalli-flores committed Mar 17, 2022
1 parent eec4234 commit 6e0b215
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/MetricsAdapter.js
Original file line number Diff line number Diff line change
@@ -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.<Metric>} Observable stream that emits metric data
* @memberof MetricsAdapter
*/
submitMetrics(metric, preLoginID) {
return throwError(
new Error('submitMetrics(metric, preLoginID) must be defined in MetricsAdapter'),
);
}
}
40 changes: 40 additions & 0 deletions src/MetricsAdapter.test.js
Original file line number Diff line number Diff line change
@@ -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();
},
);
});
});
});

0 comments on commit 6e0b215

Please sign in to comment.