<commit_before>import moment from 'moment'
import logger from 'winston'
import * as events from '../model/events'
import { config } from '../config'

config.events = config.get('events')
let normalizationBuffer

if (!config.events) {
  // maybe we're using outdated config
  config.events = config.get('visualizer')
  config.events.normalizationBuffer = config.events.orchestrationTsBufferMillis
}

const enableTSNormalization = config.events.enableTSNormalization != null ? config.events.enableTSNormalization : false
if (enableTSNormalization === true) {
  normalizationBuffer = 100
} else {
  normalizationBuffer = 0
}

const timestampAsMillis = ts => moment(new Date(ts)).valueOf()

// Determine the difference between baseTS and the earliest timestamp
// present in a collection of routes (buffered for normalization)
function calculateEarliestRouteDiff (baseTS, routes) {
  let earliestTS = 0

  for (const route of Array.from(routes)) {
    const ts = timestampAsMillis(route.request.timestamp)
    if (earliestTS < ts) { earliestTS = ts }
  }

  let tsDiff = baseTS - earliestTS
  tsDiff += normalizationBuffer

  return tsDiff
}

function determineStatusType (statusCode) {
  let status = 'success'
  if (statusCode >= 500 && statusCode <= 599) {
    status = 'error'
  }
  return status
}

export function saveEvents (trxEvents, callback) {
  const now = new Date()
  for (const event of Array.from(trxEvents)) { event.created = now }

  // bypass mongoose for quick batch inserts
  return events.EventModel.collection.insert(trxEvents, err => callback(err))
}

function createRouteEvents (dst, transactionId, channel, route, type, tsAdjustment, autoRetryAttempt) {
  if (route != null && route.request != null && route.request.timestamp != null && route.response != null && route.response.timestamp != null) {
    let startTS = timestampAsMillis(route.request.timestamp)
    let endTS = timestampAsMillis(route.response.timestamp)

    if (enableTSNormalization === true) {
      startTS += tsAdjustment<commit_msg>Updated the deprecated mongo function ".insert" with the appropriate alternative .insertMany<commit_after>import moment from 'moment'
import logger from 'winston'
import * as events from '../model/events'
import { config } from '../config'

config.events = config.get('events')
let normalizationBuffer

if (!config.events) {
  // maybe we're using outdated config
  config.events = config.get('visualizer')
  config.events.normalizationBuffer = config.events.orchestrationTsBufferMillis
}

const enableTSNormalization = config.events.enableTSNormalization != null ? config.events.enableTSNormalization : false
if (enableTSNormalization === true) {
  normalizationBuffer = 100
} else {
  normalizationBuffer = 0
}

const timestampAsMillis = ts => moment(new Date(ts)).valueOf()

// Determine the difference between baseTS and the earliest timestamp
// present in a collection of routes (buffered for normalization)
function calculateEarliestRouteDiff (baseTS, routes) {
  let earliestTS = 0

  for (const route of Array.from(routes)) {
    const ts = timestampAsMillis(route.request.timestamp)
    if (earliestTS < ts) { earliestTS = ts }
  }

  let tsDiff = baseTS - earliestTS
  tsDiff += normalizationBuffer

  return tsDiff
}

function determineStatusType (statusCode) {
  let status = 'success'
  if (statusCode >= 500 && statusCode <= 599) {
    status = 'error'
  }
  return status
}

export function saveEvents (trxEvents, callback) {
  const now = new Date()
  for (const event of Array.from(trxEvents)) { event.created = now }

  // bypass mongoose for quick batch inserts
  return events.EventModel.collection.insertMany(trxEvents, err => callback(err))
}

function createRouteEvents (dst, transactionId, channel, route, type, tsAdjustment, autoRetryAttempt) {
  if (route != null && route.request != null && route.request.timestamp != null && route.response != null && route.response.timestamp != null) {
    let startTS = timestampAsMillis(route.request.timestamp)
    let endTS = timestampAsMillis(route.response.timestamp)

    if (enableTSNormalization === true) {
      startTS += tsAdjustment
{'fraction_non_alphanumeric': 0.10544135429262394, 'fraction_numerical': 0.00532043530834341, 'mean_word_length': 4.507323568575233, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4299524', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1142, 'n_words': 347}