2018-08-31 21:36:23 +00:00
|
|
|
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,
|
|
|
|
|
|
|
|
url: computed('node', function() {
|
|
|
|
return `/v1/client/stats?node_id=${this.get('node.id')}`;
|
|
|
|
}),
|
|
|
|
|
|
|
|
append(frame) {
|
2018-09-13 19:25:35 +00:00
|
|
|
const timestamp = new Date(Math.floor(frame.Timestamp / 1000000));
|
|
|
|
|
2018-08-31 21:36:23 +00:00
|
|
|
const cpuUsed = Math.floor(frame.CPUTicksConsumed) || 0;
|
2018-09-14 17:21:01 +00:00
|
|
|
this.get('cpu').pushObject({
|
2018-09-13 19:25:35 +00:00
|
|
|
timestamp,
|
2018-08-31 21:36:23 +00:00
|
|
|
used: cpuUsed,
|
|
|
|
percent: percent(cpuUsed, this.get('reservedCPU')),
|
|
|
|
});
|
|
|
|
|
|
|
|
const memoryUsed = frame.Memory.Used;
|
2018-09-14 17:21:01 +00:00
|
|
|
this.get('memory').pushObject({
|
2018-09-13 19:25:35 +00:00
|
|
|
timestamp,
|
2018-08-31 21:36:23 +00:00
|
|
|
used: memoryUsed,
|
|
|
|
percent: percent(memoryUsed / 1024 / 1024, this.get('reservedMemory')),
|
|
|
|
});
|
2018-09-13 19:25:35 +00:00
|
|
|
|
|
|
|
// this.notifyPropertyChange('cpu');
|
|
|
|
// this.notifyPropertyChange('memory');
|
2018-08-31 21:36:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|