<commit_before>// eslint-disable no-nested-ternary
import _ from 'lodash';
import moment from 'moment';

function endpointsForSpan(span) {
  const result = [];
  if (span.localEndpoint) result.push(span.localEndpoint);
  if (span.remoteEndpoint) result.push(span.remoteEndpoint);
  return result;
}

// What's the total duration of the spans in this trace?
export function traceDuration(spans) {
  function makeList({timestamp, duration}) {
    if (!timestamp) {
      return [];
    } else if (!duration) {
      return [timestamp];
    } else {
      return [timestamp, timestamp + duration];
    }
  }

  // turns (timestamp, timestamp + duration) into an ordered list
  const timestamps = _(spans).flatMap(makeList).sort().value();

  if (timestamps.length < 2) {
    return 0;
  } else {
    const first = _.head(timestamps);
    const last = _.last(timestamps);
    return last - first;
  }
}

function getServiceNames(span) {
  return _(endpointsForSpan(span))
      .map((ep) => ep.serviceName)
      .filter((name) => name != null && name !== '')
      .uniq().value();
}

export function getGroupedTimestamps(spans) {
  const spanTimestamps = _(spans).flatMap((span) => getServiceNames(span).map((serviceName) => ({
    serviceName,
    timestamp: span.timestamp, // only used by totalDuration
    duration: span.duration
  }))).value();
<commit_msg>Rewrote duration to be more efficient because it was there (#2299)<commit_after>// eslint-disable no-nested-ternary
import _ from 'lodash';
import moment from 'moment';

function endpointsForSpan(span) {
  const result = [];
  if (span.localEndpoint) result.push(span.localEndpoint);
  if (span.remoteEndpoint) result.push(span.remoteEndpoint);
  return result;
}

function addStartEndTimestamps(span, list) {
  if (span.timestamp) list.push(span.timestamp);
  if (!span.duration) return;
  list.push(span.timestamp + span.duration);
}

// What's the total duration of the spans in this trace?
export function traceDuration(spans) {
  const timestamps = [];
  for (let i = 0; i < spans.length; i++) {
    addStartEndTimestamps(spans[i], timestamps);
  }

  if (timestamps.length < 2) return 0; // can't get a duration

  timestamps.sort();
  return timestamps[timestamps.length - 1] - timestamps[0];
}

function getServiceNames(span) {
  return _(endpointsForSpan(span))
      .map((ep) => ep.serviceName)
      .filter((name) => name != null && name !== '')
      .uniq().value();
}

export function getGroupedTimestamps(spans) {
  const spanTimestamps = _(spans).flatMap((span) => getServiceNames(span).map((serviceName) => ({
    serviceName,
    timestamp: span.timestamp, // only used by totalDuration
    duration: span.duration
  }))).value();

{'fraction_non_alphanumeric': 0.14275175212098856, 'fraction_numerical': 0.004057543341940244, 'mean_word_length': 3.895306859205776, 'pattern_counts': {'":': 0, '<': 6, '<?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': '812138', 'n_tokens_mistral': 863, 'n_tokens_neox': 835, 'n_words': 214}