<commit_before>import moment from 'moment';
import { isPresent, isBlank } from "@ember/utils";
import { getWithDefault } from '@ember/object';
import { buildDateEod } from 'thirdeye-frontend/utils/utils';
import { getFormatedDuration } from 'thirdeye-frontend/utils/anomaly';
import floatToPercent from 'thirdeye-frontend/utils/float-to-percent';

/**
 * Handles types and defaults returned from eval/projected endpoints
 * @param {Number|String} metric - number or string like 'NaN', 'Infinity'
 * @param {Boolean} isPercentage - shall we treat this as a % or a whole number?
 * @returns {Object}
 */
export function formatEvalMetric(metric, isPercentage = false) {
  const isWhole = Number.isInteger(Number(metric));
  let shown = (metric === 'Infinity') ? metric : 'N/A';
  const multiplier = isPercentage ? 100 : 1;
  const convertedNum = metric * multiplier;
  const formattedNum = isWhole ? convertedNum : convertedNum.toFixed(1);
  let displayNum = isFinite(metric) ? formattedNum : shown;
  // Prevent meaninglessly large numbers
  return (Number(displayNum) > 10000) ? 'N/A' : displayNum;
}

/**
 * Pluralizes and formats the anomaly range duration string
 * @param {Number} time
 * @param {String} unit
 * @returns {String}
 */
export function pluralizeTime(time, unit) {
  const unitStr = time > 1 ? unit + 's' : unit;
  return time ? time + ' ' + unitStr : '';
}

/**
 * Performs a case-insensitive sort of a flat array or array of objects
 * by property. If sorting flat array, pass "null" for targetProperty.
 * @param {Array} targetArray
 * @param {String} targetProperty
 * @returns {Array}
 */
export function powerSort(targetArray, targetProperty) {
  const cleanArray = [];
  // Make sure we have a valid array
  targetArray.forEach((item) => {
    cleanArray.push(isBlank(item) ? 'undefined' : item);
  });
  // Do case-insensitive sort
  const sortedArray = cleanArray.sort((a, b) => {
    if (targetProperty) {
      a = a[targetProperty];
      b = b[targetProperty];
    }
    return a.toLowerCase().trim().localeCompare(b.toLowerCase().trim());
  });<commit_msg>[TE] aaronucsd/fix for the live alerts not showing bug (#3710)<commit_after>import moment from 'moment';
import { isPresent, isBlank } from "@ember/utils";
import { getWithDefault } from '@ember/object';
import { buildDateEod } from 'thirdeye-frontend/utils/utils';
import { getFormatedDuration } from 'thirdeye-frontend/utils/anomaly';
import floatToPercent from 'thirdeye-frontend/utils/float-to-percent';

/**
 * Handles types and defaults returned from eval/projected endpoints
 * @param {Number|String} metric - number or string like 'NaN', 'Infinity'
 * @param {Boolean} isPercentage - shall we treat this as a % or a whole number?
 * @returns {Object}
 */
export function formatEvalMetric(metric, isPercentage = false) {
  const isWhole = Number.isInteger(Number(metric));
  let shown = (metric === 'Infinity') ? metric : 'N/A';
  const multiplier = isPercentage ? 100 : 1;
  const convertedNum = metric * multiplier;
  const formattedNum = isWhole ? convertedNum : convertedNum.toFixed(1);
  let displayNum = isFinite(metric) ? formattedNum : shown;
  // Prevent meaninglessly large numbers
  return (Number(displayNum) > 10000) ? 'N/A' : displayNum;
}

/**
 * Pluralizes and formats the anomaly range duration string
 * @param {Number} time
 * @param {String} unit
 * @returns {String}
 */
export function pluralizeTime(time, unit) {
  const unitStr = time > 1 ? unit + 's' : unit;
  return time ? time + ' ' + unitStr : '';
}

/**
 * Performs a case-insensitive sort of a flat array or array of objects
 * by property. If sorting flat array, pass "null" for targetProperty.
 * @param {Array} targetArray
 * @param {String} targetProperty
 * @returns {Array}
 */
export function powerSort(targetArray, targetProperty) {
  const cleanArray = [];
  // Make sure we have a valid array
  targetArray.forEach((item) => {
    cleanArray.push(isBlank(item) ? 'undefined' : item);
  });
  // Do case-insensitive sort
  const sortedArray = cleanArray.sort((a, b) => {
    if (targetProperty) {
      a = a[targetProperty];
      b = b[targetProperty];
    }
    return (a || '').toLowerCase().trim().localeCompare((b || '').toLowerCase().trim());
  });
{'fraction_non_alphanumeric': 0.1325784383109224, 'fraction_numerical': 0.006133522057088936, 'mean_word_length': 4.189718482252142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '414742', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1266, 'n_words': 395}