<commit_before>var Transaction = require('./transaction')
var request = require('../lib/transport')

var Instrumentation = function () {
  this._queue = []
}

Instrumentation.prototype.add = function (transaction) {
  this._queue.push(transaction)
  this._send()
}

Instrumentation.prototype.startTransaction = function (name, type) {
  return new Transaction(this, name, type)
}

Instrumentation.prototype._send = function () {
  var formattedTransactions = this._formatTransactions()

  request.sendTransaction(formattedTransactions)
    .then(this._flush.bind(this))
}

Instrumentation.prototype._flush = function () {
  this._queue = []
}

Instrumentation.prototype._formatTransactions = function () {
  var transactions = groupTransactions(this._queue)

  var traces = [].concat.apply([], this._queue.map(function (trans) {
    return trans.traces
  }))

  var groupedTraces = groupTraces(traces)
  var groupedTracesTimings = getRawGroupedTracesTimings(traces, groupedTraces);

  return {
    transactions: transactions,
    traces: {
      groups: groupedTraces,
      raw: groupedTracesTimings
    }
  }
}

function groupTransactions (transactions) {
  var groups = grouper(transactions, transactionGroupingKey)

  return Object.keys(groups).map(function (key) {
    var trans = groups[key][0]
    var durations = groups[key].map(function (trans) {
      return trans.duration()
    })
    return {
      transaction: trans.name,
      result: trans.result,
      kind: trans.type,
      timestamp: groupingTs(trans._startStamp).toISOString(),
      durations: durations
    }
  })
}

function getRawGroupedTracesTimings (traces, groupedTraces) {

  var getTraceGroupIndex = function (col, item) {
    var index = 0
    var targetGroup = traceGroupingKey(item)

    col.forEach(function (item, i) {
      if(item._group === targetGroup) {
        index = i
      }
    })

    return index
  }

  var groupedByTransaction = grouper(traces, function(trace) {
    return trace.transaction.name
  })

  return Object.keys(groupedByTransaction).map(function (key) {
      var traces = groupedByTransaction[key]
      var transaction = traces[0].transaction

      var data = [transaction.duration()]

      traces.forEach(function(trace) {
        var groupIndex = getTraceGroupIndex(groupedTraces, trace)
        data.push([groupIndex, trace._start - transaction._start, trace.duration()])
      })

      return data;
  });

}
<commit_msg>Add scheduler to dispatch finished transactions every 10 sec <commit_after>var Transaction = require('./transaction')
var request = require('../lib/transport')
var logger = require('../lib/logger')

var Instrumentation = function () {
  this._queue = []
  this.scheduler = setInterval(this._dispatch.bind(this), 10000)
}

Instrumentation.prototype.add = function (transaction) {
  this._queue.push(transaction)
}

Instrumentation.prototype.startTransaction = function (name, type) {
  return new Transaction(this, name, type)
}

Instrumentation.prototype._flush = function () {
  this._queue = []
}

Instrumentation.prototype._dispatch = function() {

  logger.log('Instrumentation.scheduler._dispatch', this._queue.length)

  if(!this._queue.length) {
    return 
  }

  var transactions = this._formatTransactions()
  var flush = this._flush.bind(this)

  request.sendTransaction(transactions).then(flush)
}

Instrumentation.prototype._formatTransactions = function () {
  var transactions = groupTransactions(this._queue)

  var traces = [].concat.apply([], this._queue.map(function (trans) {
    return trans.traces
  }))

  var groupedTraces = groupTraces(traces)
  var groupedTracesTimings = getRawGroupedTracesTimings(traces, groupedTraces);

  return {
    transactions: transactions,
    traces: {
      groups: groupedTraces,
      raw: groupedTracesTimings
    }
  }
}

function groupTransactions (transactions) {
  var groups = grouper(transactions, transactionGroupingKey)

  return Object.keys(groups).map(function (key) {
    var trans = groups[key][0]
    var durations = groups[key].map(function (trans) {
      return trans.duration()
    })
    return {
      transaction: trans.name,
      result: trans.result,
      kind: trans.type,
      timestamp: groupingTs(trans._startStamp).toISOString(),
      durations: durations
    }
  })
}

function getRawGroupedTracesTimings (traces, groupedTraces) {

  var getTraceGroupIndex = function (col, item) {
    var index = 0
    var targetGroup = traceGroupingKey(item)

    col.forEach(function (item, i) {
      if(item._group === targetGroup) {
        index = i
      }
    })

    return index
  }

  var groupedByTransaction = grouper(traces, function(trace) {
    return trace.transaction.name
  })

  return Object.keys(groupedByTransaction).map(function (key) {
    var traces = groupedByTransaction[key]
    var transaction = traces[0].transaction

    var data = [transaction.duration()]

    traces.forEach(function(trace) {
      var groupIndex = getTraceGroupIndex(groupedTraces, trace)
      data.push([groupIndex, trace._start - transaction._start, trace.duration()])
    })

    return data;
  });

}

{'fraction_non_alphanumeric': 0.11605227228398673, 'fraction_numerical': 0.0025355958650282815, 'mean_word_length': 3.721915285451197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7561698', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1494, 'n_words': 308}