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';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-08-31 21:36:23 +00:00
|
|
|
|
|
|
|
const percent = (numerator, denominator) => {
|
|
|
|
if (!numerator || !denominator) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return numerator / denominator;
|
|
|
|
};
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const empty = (ts) => ({ timestamp: ts, used: null, percent: null });
|
2018-09-17 23:59:09 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
class NodeStatsTracker extends EmberObject.extend(AbstractStatsTracker) {
|
2018-08-31 21:36:23 +00:00
|
|
|
// Set via the stats computed property macro
|
2020-06-10 13:49:16 +00:00
|
|
|
node = null;
|
2018-08-31 21:36:23 +00:00
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('node.id')
|
2020-06-10 13:49:16 +00:00
|
|
|
get url() {
|
2018-08-31 21:36:23 +00:00
|
|
|
return `/v1/client/stats?node_id=${this.get('node.id')}`;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-31 21:36:23 +00:00
|
|
|
|
|
|
|
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;
|
2019-03-26 07:46:44 +00:00
|
|
|
this.cpu.pushObject({
|
2018-09-13 19:25:35 +00:00
|
|
|
timestamp,
|
2018-08-31 21:36:23 +00:00
|
|
|
used: cpuUsed,
|
2019-03-26 07:46:44 +00:00
|
|
|
percent: percent(cpuUsed, this.reservedCPU),
|
2018-08-31 21:36:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const memoryUsed = frame.Memory.Used;
|
2019-03-26 07:46:44 +00:00
|
|
|
this.memory.pushObject({
|
2018-09-13 19:25:35 +00:00
|
|
|
timestamp,
|
2018-08-31 21:36:23 +00:00
|
|
|
used: memoryUsed,
|
2019-03-26 07:46:44 +00:00
|
|
|
percent: percent(memoryUsed / 1024 / 1024, this.reservedMemory),
|
2018-08-31 21:36:23 +00:00
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-31 21:36:23 +00:00
|
|
|
|
2018-09-17 23:59:09 +00:00
|
|
|
pause() {
|
|
|
|
const ts = new Date();
|
2019-03-26 07:46:44 +00:00
|
|
|
this.memory.pushObject(empty(ts));
|
|
|
|
this.cpu.pushObject(empty(ts));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-17 23:59:09 +00:00
|
|
|
|
2018-08-31 21:36:23 +00:00
|
|
|
// Static figures, denominators for stats
|
2020-06-10 13:49:16 +00:00
|
|
|
@alias('node.resources.cpu') reservedCPU;
|
|
|
|
@alias('node.resources.memory') reservedMemory;
|
2018-08-31 21:36:23 +00:00
|
|
|
|
|
|
|
// Dynamic figures, collected over time
|
|
|
|
// []{ timestamp: Date, used: Number, percent: Number }
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('bufferSize', 'node')
|
2020-06-10 13:49:16 +00:00
|
|
|
get cpu() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return RollingArray(this.bufferSize);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('bufferSize', 'node')
|
2020-06-10 13:49:16 +00:00
|
|
|
get memory() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return RollingArray(this.bufferSize);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 21:36:23 +00:00
|
|
|
|
|
|
|
export default NodeStatsTracker;
|
|
|
|
|
|
|
|
export function stats(nodeProp, fetch) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return computed(nodeProp, function () {
|
2018-08-31 21:36:23 +00:00
|
|
|
return NodeStatsTracker.create({
|
|
|
|
fetch: fetch.call(this),
|
|
|
|
node: this.get(nodeProp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|