/*
 * @copyright Copyright (c) Sematext Group, Inc. - All Rights Reserved
 *
 * @licence SPM for Docker is free-to-use, proprietary software.
 * THIS IS PROPRIETARY SOURCE CODE OF Sematext Group, Inc. (Sematext)
 * This source code may not be copied, reverse engineered, or altered for any purpose.
 * This source code is to be used exclusively by users and customers of Sematext.
 * Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
 */
'use strict'
var Measured = require('measured')
var Aggregator = function (metricCallback) {
  this.metrics = {}
  this.lastValues = {}
  this.metricCallback = metricCallback
  this.lastUpdate = Date.now()
}

Aggregator.prototype.update = function (timestamp, name, value, calcDiff) {
  this.lastUpdate = Date.now()
  if (isNaN(value)) {
    return
  }
  if (this.metrics[name] === undefined) {
    this.metrics[name] = new Measured.Histogram()
  }
  if (!this.lastValues[name]) {
    this.lastValues[name] = {value: value, ts: timestamp}
  }
  if (calcDiff === true) {
    var diff = value - this.lastValues[name].value
    this.metrics[name].update(diff, timestamp)
    this.lastValues[name] = {value: value, ts: timestamp}
  } else {
    this.metrics[name].update(value, timestamp)
  }
}

Aggregator.prototype.get = function (name) {
  if (this.metrics[name] !== undefined) {
    var rv = this.metrics[name].toJSON()
    rv.name = name
    this.metrics[name].reset()
    return rv
  } else {
    return {count: 0, sum: 0, mean: 0, err: 'no metrics object for ' + name}
  }
}

Aggregator.prototype.reset = function () {
  this.metrics = null
  this.metrics = {}
}

module.exports = Aggregator

/*
 * @copyright Copyright (c) Sematext Group, Inc. - All Rights Reserved
 *
 * @licence SPM for Docker is free-to-use, proprietary software.
 * THIS IS PROPRIETARY SOURCE CODE OF Sematext Group, Inc. (Sematext)
 * This source code may not be copied, reverse engineered, or altered for any purpose.
 * This source code is to be used exclusively by users and customers of Sematext.
 * Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
 */
'use strict'
var Measured = require('measured')
var Aggregator = function (metricCallback, id) {
  this.metrics = {}
  this.id = id
  this.lastValues = {}
  this.metricCallback = metricCallback
  this.lastUpdate = Date.now()
}

Aggregator.prototype.update = function (timestamp, name, value, calcDiff) {
  this.lastUpdate = Date.now()
  if (isNaN(value)) {
    return
  }
  if (this.metrics[name] === undefined) {
    this.metrics[name] = new Measured.Histogram()
  }
  if (!this.lastValues[name]) {
    this.lastValues[name] = {value: value, ts: timestamp}
  }

  if (calcDiff === true) {
    var diff = value - this.lastValues[name].value
    if (diff < 0) {
      // container stop might have caused a reset of metric value.
      this.lastValues[name].value = 0
    } else {
      this.metrics[name].update(diff, timestamp)
      this.lastValues[name] = {value: value, ts: timestamp}
    }
  } else {
    this.metrics[name].update(value, timestamp)
  }
}

Aggregator.prototype.get = function (name) {
  if (this.metrics[name] !== undefined) {
    var rv = this.metrics[name].toJSON()
    rv.name = name
    this.metrics[name].reset()
    return rv
  } else {
    return {count: 0, sum: 0, mean: 0, err: 'no metrics object for ' + name}
  }
}

Aggregator.prototype.reset = function () {
  this.metrics = null
  this.metrics = {}
}

module.exports = Aggregator

fix negative metric value after 'docker stop'