import EmberObject, { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import RollingArray from 'nomad-ui/utils/classes/rolling-array';
import AbstractStatsTracker from 'nomad-ui/utils/classes/abstract-stats-tracker';

const percent = (numerator, denominator) => {
  if (!numerator || !denominator) {
    return 0;
  }
  return numerator / denominator;
};

const NodeStatsTracker = EmberObject.extend(AbstractStatsTracker, {
  // Set via the stats computed property macro
  node: null,

  bufferSize: 100,

  url: computed('node', function() {
    return `/v1/client/stats?node_id=${this.get('node.id')}`;
  }),

  append(frame) {
    const cpuUsed = Math.floor(frame.CPUTicksConsumed) || 0;
    this.get('cpu').push({
      timestamp: frame.Timestamp,
      used: cpuUsed,
      percent: percent(cpuUsed, this.get('reservedCPU')),
    });

    const memoryUsed = frame.Memory.Used;
    this.get('memory').push({
      timestamp: frame.Timestamp,
      used: memoryUsed,
      percent: percent(memoryUsed / 1024 / 1024, this.get('reservedMemory')),
    });
  },

  // Static figures, denominators for stats
  reservedCPU: alias('node.resources.cpu'),
  reservedMemory: alias('node.resources.memory'),

  // Dynamic figures, collected over time
  // []{ timestamp: Date, used: Number, percent: Number }
  cpu: computed('node', function() {
    return RollingArray(this.get('bufferSize'));
  }),
  memory: computed('node', function() {
    return RollingArray(this.get('bufferSize'));
  }),
});

export default NodeStatsTracker;

export function stats(nodeProp, fetch) {
  return computed(nodeProp, function() {
    return NodeStatsTracker.create({
      fetch: fetch.call(this),
      node: this.get(nodeProp),
    });
  });
}

@@ -652,24 +652,96 @@
 nd(frame) %7B%0A
+    const timestamp = new Date(Math.floor(frame.Timestamp / 1000000));%0A%0A
     const cp
@@ -809,20 +809,25 @@
 ('cpu').
-push
+addObject
 (%7B%0A     
@@ -836,33 +836,16 @@
 imestamp
-: frame.Timestamp
 ,%0A      
@@ -995,12 +995,17 @@
 y').
-push
+addObject
 (%7B%0A 
@@ -1022,25 +1022,8 @@
 tamp
-: frame.Timestamp
 ,%0A  
@@ -1122,23 +1122,109 @@
 ory')),%0A
-
     %7D);
+%0A%0A    // this.notifyPropertyChange('cpu');%0A    // this.notifyPropertyChange('memory');
 %0A  %7D,%0A%0A 

Use addObject to get kvo behaviors