import {InfluxDB} from 'influx';
import config from 'config';
import log from '../log';

const THRESHOLD_IN_MS = 60000;

function setup() {
  const influxConnection = config.get("metrics.influx.dsn");
  const influx = new InfluxDB(influxConnection);
  log('debug', `Connecting influx to ${influxConnection}`);

  let queue = [];
  let lastWrite = new Date();
  let lastPrintedError;
  let timeout;

  function drainQueue() {
    const queueToSend = queue;
    queue = [];
    lastWrite = new Date();
    if (queue.length > 0) {
      log('debug', 'Draining queue for influx');
      influx.writePoints(queueToSend).catch(err => {
        // limit an error to once every 30 seconds
        if (!lastPrintedError || (new Date() - lastPrintedError) > 30) {
          log('error', `Error saving data to InfluxDB! ${err.stack}`);
        }
        lastPrintedError = new Date();
      });
    }
  }

  process.on('SIGINT', drainQueue);
  process.on('SIGTERM', drainQueue);
  process.on('SIGQUIT', drainQueue);
  process.on('SIGABRT', drainQueue);

  return {
    write(metric) {
      const metricPoint = metric.get();
      const influxMetric = {
        measurement: 'response_times',
        tags: Object.assign({}, metricPoint.tags, {request_type: metricPoint.type}),
        fields: Object.assign({}, metricPoint.fields, {duration: metricPoint.duration})
      };
      queue.push(influxMetric);

      // Now determine whether to actually fire a write (every 250 items or THRESHOLD_IN_MS)
      if (queue.length > 250 || new Date() - lastWrite > THRESHOLD_IN_MS) {
        drainQueue();
      } else {
        // For the case somebody receives little traffic
        const innerLast = lastWrite;
        timeout = setTimeout(() => {
          if (innerLast !== lastWrite) {
            log('debug', 'No need to write, another write was performed in the meantime');
            return;
          }
          drainQueue();
        }, THRESHOLD_IN_MS);
      }
    }
  };
}

export default setup;

@@ -498,32 +498,38 @@
 );%0A    if (queue
+ToSend
 .length %3E 0) %7B%0A 
@@ -538,25 +538,24 @@
    log('
-debug', '
+info', %60
 Draining
@@ -571,17 +571,56 @@
 r influx
-'
+. Writing $%7BqueueToSend.length%7D points.%60
 );%0A     
@@ -1391,16 +1391,48 @@
 ration%7D)
+,%0A        timestamp: new Date(),
 %0A      %7D

Write to influx correctly