Skip to content

Commit

Permalink
feat(SignIn): emit operational metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sumit12690 committed May 30, 2022
1 parent d760f49 commit 7db12ed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/components/SignIn/SignIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import {Button} from '../generic';
import Spinner from '../generic/Spinner/Spinner';
import webexComponentClasses from '../helpers';
import {useMetrics} from '../hooks';

/**
* Performs OAuth 2.0 Authorization
Expand Down Expand Up @@ -36,12 +37,13 @@ export default function SignIn({
}) {
const [isAuthenticating, setIsAuthenticating] = useState(false);
const [cssClasses] = webexComponentClasses('sign-in', className);
const [emitMetrics] = useMetrics();

const openAuthUrl = () => {
const arr = new Uint8Array(4);
const state = window.crypto.getRandomValues(arr);
const fullAuthUrl = `${authUrl}?client_id=${clientID}&response_type=code&redirect_uri=${encodeURI(redirectUri)}${scope !== '' ? `&scope=${encodeURI(scope)}` : ''}&state=${state}`;

const startTime = window.performance.now();
const newWindow = window.open(fullAuthUrl, 'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=700');

setIsAuthenticating(true);
Expand All @@ -50,6 +52,18 @@ export default function SignIn({
const timer = setInterval(() => {
if (newWindow.closed) {
clearInterval(timer);
let endTime = window.performance.now();

emitMetrics({
fields: {
startTime,
endTime,
totalTime: endTime - startTime,
},
metricName: 'windowTime',
type: 'operational',
}, 'loginId');

getAccessToken().then((accessToken) => {
if (accessToken) {
if (Object.keys(tokenStoragePolicy).length !== 0) {
Expand Down Expand Up @@ -80,6 +94,16 @@ export default function SignIn({
signInResponse(clientID, Error('Failed to fetch access token: ', err));
});
setIsAuthenticating(false);
endTime = window.performance.now();
emitMetrics({
fields: {
startTime,
endTime,
totalTime: endTime - startTime,
},
metricName: 'authenticationTime',
type: 'operational',
}, 'loginId');
}
}, 500);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export {default as useRef} from './useRef';
export {default as useRoom} from './useRoom';
export {default as useStream} from './useStream';
export {default as useSpeakers} from './useSpeakers';
export {default as useMetrics} from './useMetrics';
21 changes: 21 additions & 0 deletions src/components/hooks/useMetrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useContext} from 'react';

import {AdapterContext} from './contexts';

/**
* A Webex metric.
*
* @external Metric
* @see {@link https://github.com/webex/component-adapter-interfaces/blob/master/src/MetricsAdapter.js#L6}
*/

/**
* Custom hook that returns an function for submitting metrics
*
* @returns {Function>} The submit data function that will emit the metrics
*/
export default function useMetrics() {
const {metricsAdapter} = useContext(AdapterContext);

return [(metric, loginId) => metricsAdapter.submitMetrics(metric, loginId)];
}

0 comments on commit 7db12ed

Please sign in to comment.