UNPKG

@atlaskit/editor-plugin-collab-edit

Version:

Collab Edit plugin for @atlaskit/editor-core

82 lines (79 loc) 4.83 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.updateNcsSessionStepMetrics = exports.updateNcsActiveSessions = exports.getNcsSessionStepMetrics = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _ncsStepMetrics = require("@atlaskit/editor-common/ncs-step-metrics"); var _frontendUtilities = require("@atlaskit/frontend-utilities"); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var storageClient = new _frontendUtilities.StorageClient(_ncsStepMetrics.NCS_STORAGE.NCS_STORAGE_CLIENT_KEY); /** * Gets the current step session metrics for a given session ID * If the session ID does not exist, it initializes a new metrics object. * It calculates the total size of the steps, the number of steps, * the maximum step size, and the sum of step sizes for P90 calculation. * * @param metrics - The existing metrics object from local storage. * @param sessionId - The session ID for which to get the metrics. * @param steps - The steps to calculate the metrics from. * @returns The updated step session metrics for the given session ID. */ var getNcsSessionStepMetrics = exports.getNcsSessionStepMetrics = function getNcsSessionStepMetrics(metrics, sessionId, steps) { var _metrics$sessionId; var current = (_metrics$sessionId = metrics[sessionId]) !== null && _metrics$sessionId !== void 0 ? _metrics$sessionId : { ncsSessionId: sessionId, totalStepSize: 0, numberOfSteps: 0, maxStepSize: 0, stepSizeSumForP90: [] }; steps.forEach(function (step) { var stepSize = JSON.stringify(step).length; current.totalStepSize += stepSize; current.numberOfSteps += 1; current.maxStepSize = Math.max(current.maxStepSize, stepSize || 0); current.stepSizeSumForP90.push(stepSize || 0); }); return current; }; /** * Gets the current active sessions from local storage * If the session ID does not exist, it initializes a new active session. * * This is used in the ncsStepMetricsPlugin to determine if the session is still active * before sending the ncs steps analytics event. * * @param sessionId - The session ID to check or update in local storage. * @returns void */ var updateNcsActiveSessions = exports.updateNcsActiveSessions = function updateNcsActiveSessions(sessionId) { var currentActiveSessions = JSON.parse(storageClient.getItem(_ncsStepMetrics.NCS_STORAGE.NCS_ACTIVE_SESSIONS) || '{}'); if (!currentActiveSessions[sessionId]) { storageClient.setItemWithExpiry(_ncsStepMetrics.NCS_STORAGE.NCS_ACTIVE_SESSIONS, JSON.stringify(_objectSpread(_objectSpread({}, currentActiveSessions), {}, (0, _defineProperty2.default)({}, sessionId, true)))); } }; // delete this function when cleaning up platform_editor_remove_collab_step_metrics /** * Updates the step session metrics in local storage for a given session ID. * It calculates the metrics based on the provided steps and updates the storage. * * @param api - The API to access the collab edit plugin. * @param steps - The steps to calculate the metrics from. * @return void */ var updateNcsSessionStepMetrics = exports.updateNcsSessionStepMetrics = function updateNcsSessionStepMetrics(_ref) { var _api$collabEdit; var api = _ref.api, steps = _ref.steps; var sessionId = api === null || api === void 0 || (_api$collabEdit = api.collabEdit) === null || _api$collabEdit === void 0 || (_api$collabEdit = _api$collabEdit.sharedState.currentState()) === null || _api$collabEdit === void 0 ? void 0 : _api$collabEdit.sessionId; if (!sessionId) { return; } var existingMetrics = JSON.parse(storageClient.getItem(_ncsStepMetrics.NCS_STORAGE.NCS_SESSION_STEP_METRICS) || '{}'); var ncsSessionStepMetrics = getNcsSessionStepMetrics(existingMetrics, sessionId, steps); storageClient.setItemWithExpiry(_ncsStepMetrics.NCS_STORAGE.NCS_SESSION_STEP_METRICS, JSON.stringify(_objectSpread(_objectSpread({}, existingMetrics), {}, (0, _defineProperty2.default)({}, sessionId, ncsSessionStepMetrics)))); updateNcsActiveSessions(sessionId); };