<commit_before>const METRIC_NS = `ciscospark_widget_`;
const LOAD_START = `widget:ready`;

const metrics = {};
let metricsQueue = [];
const metricsCount = {};
/**
 * Temporarily save metric to sessionStorage
 * @param {string} key
 * @param  {any} data
 * @returns {null}
 */
export function saveTempMetric(key, data) {
  metrics[key] = data;
}

export function getTempMetric(key) {
  const data = metrics[key];
  return {key, data};
}

// Helpers for saving metrics to queue before Spark is ready to send metrics

export function saveMetricToQueue(key, data) {
  metricsQueue.push({key, data});
}

export function saveElapsedTimeToQueue(key) {
  const elapsedTime = getElapsedTime();
  saveMetricToQueue(key, elapsedTime);
}

export function sendMetricsInQueue(spark) {
  metricsQueue.forEach(({key, data}) => {
    sendPerfMetric({key, data}, spark);
  });
  metricsQueue = [];
}

/**
 * Immediately send metric
 * @param  {object} metric
 * @param  {string} metric.key
 * @param  {any} metric.data
 * @param  {object} spark
 * @returns {Promise}
 */
export function sendPerfMetric({key, data, fields, tags}, spark) {
  try {
    const [name, action] = key.split(`:`);

    const count = metricsCount[key];
    if (count) {
      metricsCount[key] = count + 1;
    }
    else {
      metricsCount[key] = 1;
    }

    const formattedData = {
      type: [`operational`],
      tags: Object.assign({
        action
      }, tags),
      fields: Object.assign({
        protocol: window.location.protocol,
        hostname: window.location.hostname,
        pathname: window.location.pathname,
        widgetVersion: process.env.REACT_CISCOSPARK_VERSION,
        jsSdkVersion: spark.version,
        iteration: metricsCount[key],
        data
      }, fields)
    };
    return spark.metrics.submitClientMetrics(`${METRIC_NS}${name}`, formattedData);
  }
  catch (e) {
    return console.warn(`clientMetrics: ${key} metrics was not sent`, e); // eslint-disable-line no-console
  }
}

/**
 * Send metric stored in session
 * @param  {string} key
 * @param  {object} spark
 * @returns {Promise}
 */
export function sendTempMetric(key, spark) {
  let metric = getTempMetric(key);
  metric = isNaN(metric) ? metric : Number(metric);
  return sendPerfMetric(metric, spark);
}

/**
 * Save start time
 * @param  {object} spark
 * @returns {null}
 */
export function saveAndSendLoadStart(spark) {<commit_msg>fix(react-component-utils): Disable metrics<commit_after>const METRIC_NS = `ciscospark_widget_`; // eslint-disable-line no-unused-vars
const LOAD_START = `widget:ready`;

const metrics = {};
let metricsQueue = [];
const metricsCount = {}; // eslint-disable-line no-unused-vars
/**
 * Temporarily save metric to sessionStorage
 * @param {string} key
 * @param  {any} data
 * @returns {null}
 */
export function saveTempMetric(key, data) {
  metrics[key] = data;
}

export function getTempMetric(key) {
  const data = metrics[key];
  return {key, data};
}

// Helpers for saving metrics to queue before Spark is ready to send metrics

export function saveMetricToQueue(key, data) {
  metricsQueue.push({key, data});
}

export function saveElapsedTimeToQueue(key) {
  const elapsedTime = getElapsedTime();
  saveMetricToQueue(key, elapsedTime);
}

export function sendMetricsInQueue(spark) {
  metricsQueue.forEach(({key, data}) => {
    sendPerfMetric({key, data}, spark);
  });
  metricsQueue = [];
}

/**
 * Immediately send metric
 * @param  {object} metric
 * @param  {string} metric.key
 * @param  {any} metric.data
 * @param  {object} spark
 * @returns {Promise}
 */
export function sendPerfMetric({key, data, fields, tags}, spark) { // eslint-disable-line no-unused-vars
  // try {
  //   const [name, action] = key.split(`:`);
  //
  //   const count = metricsCount[key];
  //   if (count) {
  //     metricsCount[key] = count + 1;
  //   }
  //   else {
  //     metricsCount[key] = 1;
  //   }
  //
  //   const formattedData = {
  //     type: [`operational`],
  //     tags: Object.assign({
  //       action
  //     }, tags),
  //     fields: Object.assign({
  //       protocol: window.location.protocol,
  //       hostname: window.location.hostname,
  //       pathname: window.location.pathname,
  //       widgetVersion: process.env.REACT_CISCOSPARK_VERSION,
  //       jsSdkVersion: spark.version,
  //       iteration: metricsCount[key],
  //       data
  //     }, fields)
  //   };
  //   return spark.metrics.submitClientMetrics(`${METRIC_NS}${name}`, formattedData);
  // }
  // catch (e) {
  //   return console.warn(`clientMetrics: ${key} metrics was not sent`, e); // eslint-disable-line no-console
  // }
  return false;
}

/**
 * Send metric stored in session
 * @param  {string} key
 * @param  {object} spark
 * @returns {Promise}
 */
export function sendTempMetric(key, spark) {
  let metric = getTempMetric(key);
  metric = isNaN(metric) ? metric : Number(metric);
  return sendPerfMetric(metric, spark);
}

/**
 * Save start time
 * @param  {object} spark
 * @returns {null}
 */
export function saveAndSendLoadStart(spark) {
{'fraction_non_alphanumeric': 0.1455408344868499, 'fraction_numerical': 0.0007909827961241843, 'mean_word_length': 3.261162594776748, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6817517', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1610, 'n_words': 396}