Refactor the way allocation stats are fetched

No longer treat it as a property for relationship, just ephemeral
data that can be fetched on demand.
This commit is contained in:
Michael Lange 2018-03-22 14:34:13 -07:00
parent 729365507f
commit 6bf139d094
2 changed files with 44 additions and 44 deletions

View file

@ -1,13 +1,11 @@
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { readOnly } from '@ember/object/computed';
import { computed } from '@ember/object'; import { computed } from '@ember/object';
import RSVP from 'rsvp';
import Model from 'ember-data/model'; import Model from 'ember-data/model';
import attr from 'ember-data/attr'; import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships'; import { belongsTo } from 'ember-data/relationships';
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes'; import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
import PromiseObject from '../utils/classes/promise-object';
import shortUUIDProperty from '../utils/properties/short-uuid'; import shortUUIDProperty from '../utils/properties/short-uuid';
import AllocationStats from '../utils/classes/allocation-stats';
const STATUS_ORDER = { const STATUS_ORDER = {
pending: 1, pending: 1,
@ -56,48 +54,17 @@ export default Model.extend({
return taskGroups && taskGroups.findBy('name', this.get('taskGroupName')); return taskGroups && taskGroups.findBy('name', this.get('taskGroupName'));
}), }),
memoryUsed: readOnly('stats.ResourceUsage.MemoryStats.RSS'), fetchStats() {
cpuUsed: computed('stats.ResourceUsage.CpuStats.TotalTicks', function() { return this.get('token')
return Math.floor(this.get('stats.ResourceUsage.CpuStats.TotalTicks') || 0); .authorizedRequest(`/v1/client/allocation/${this.get('id')}/stats`)
}), .then(res => res.json())
.then(json => {
percentMemory: computed('taskGroup.reservedMemory', 'memoryUsed', function() { return new AllocationStats({
const used = this.get('memoryUsed') / 1024 / 1024; stats: json,
const total = this.get('taskGroup.reservedMemory'); allocation: this,
if (!total || !used) { });
return 0;
}
return used / total;
}),
percentCPU: computed('cpuUsed', 'taskGroup.reservedCPU', function() {
const used = this.get('cpuUsed');
const total = this.get('taskGroup.reservedCPU');
if (!total || !used) {
return 0;
}
return used / total;
}),
stats: computed('node.{isPartial,httpAddr}', function() {
const nodeIsPartial = this.get('node.isPartial');
// If the node doesn't have an httpAddr, it's a partial record.
// Once it reloads, this property will be dirtied and stats will load.
if (nodeIsPartial) {
return PromiseObject.create({
// Never resolve, so the promise object is always in a state of pending
promise: new RSVP.Promise(() => {}),
}); });
} },
const url = `/v1/client/allocation/${this.get('id')}/stats`;
return PromiseObject.create({
promise: this.get('token')
.authorizedRequest(url)
.then(res => res.json()),
});
}),
states: fragmentArray('task-state'), states: fragmentArray('task-state'),
}); });

View file

@ -0,0 +1,33 @@
import EmberObject, { computed } from '@ember/object';
import { alias, readOnly } from '@ember/object/computed';
export default EmberObject.extend({
allocation: null,
stats: null,
reservedMemory: alias('allocation.taskGroup.reservedMemory'),
reservedCPU: alias('allocation.taskGroup.reservedCPU'),
memoryUsed: readOnly('stats.ResourceUsage.MemoryStats.RSS'),
cpuUsed: computed('stats.ResourceUsage.CpuStats.TotalTicks', function() {
return Math.floor(this.get('stats.ResourceUsage.CpuStats.TotalTicks') || 0);
}),
percentMemory: computed('reservedMemory', 'memoryUsed', function() {
const used = this.get('memoryUsed') / 1024 / 1024;
const total = this.get('reservedMemory');
if (!total || !used) {
return 0;
}
return used / total;
}),
percentCPU: computed('reservedCPU', 'cpuUsed', function() {
const used = this.get('cpuUsed');
const total = this.get('reservedCPU');
if (!total || !used) {
return 0;
}
return used / total;
}),
});