2017-09-19 14:47:10 +00:00
|
|
|
import Ember from 'ember';
|
|
|
|
import Model from 'ember-data/model';
|
|
|
|
import attr from 'ember-data/attr';
|
|
|
|
import { belongsTo } from 'ember-data/relationships';
|
|
|
|
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
|
|
|
|
import PromiseObject from '../utils/classes/promise-object';
|
|
|
|
import timeout from '../utils/timeout';
|
|
|
|
import shortUUIDProperty from '../utils/properties/short-uuid';
|
|
|
|
|
2017-10-19 02:04:19 +00:00
|
|
|
const { computed, RSVP, inject } = Ember;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2017-10-18 00:48:52 +00:00
|
|
|
const STATUS_ORDER = {
|
|
|
|
pending: 1,
|
|
|
|
running: 2,
|
|
|
|
complete: 3,
|
|
|
|
failed: 4,
|
|
|
|
lost: 5,
|
|
|
|
};
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
export default Model.extend({
|
2017-10-19 02:04:19 +00:00
|
|
|
token: inject.service(),
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
shortId: shortUUIDProperty('id'),
|
|
|
|
job: belongsTo('job'),
|
|
|
|
node: belongsTo('node'),
|
|
|
|
name: attr('string'),
|
|
|
|
taskGroupName: attr('string'),
|
|
|
|
resources: fragment('resources'),
|
|
|
|
modifyIndex: attr('number'),
|
2017-10-18 19:29:33 +00:00
|
|
|
jobVersion: attr('number'),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2017-10-05 22:21:10 +00:00
|
|
|
// TEMPORARY: https://github.com/emberjs/data/issues/5209
|
|
|
|
originalJobId: attr('string'),
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
clientStatus: attr('string'),
|
|
|
|
desiredStatus: attr('string'),
|
2017-10-18 00:48:52 +00:00
|
|
|
statusIndex: computed('clientStatus', function() {
|
|
|
|
return STATUS_ORDER[this.get('clientStatus')] || 100;
|
|
|
|
}),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
taskGroup: computed('taskGroupName', 'job.taskGroups.[]', function() {
|
|
|
|
const taskGroups = this.get('job.taskGroups');
|
|
|
|
return taskGroups && taskGroups.findBy('name', this.get('taskGroupName'));
|
|
|
|
}),
|
|
|
|
|
2017-10-17 01:46:35 +00:00
|
|
|
memoryUsed: computed.readOnly('stats.ResourceUsage.MemoryStats.RSS'),
|
|
|
|
cpuUsed: computed('stats.ResourceUsage.CpuStats.TotalTicks', function() {
|
|
|
|
return Math.floor(this.get('stats.ResourceUsage.CpuStats.TotalTicks'));
|
|
|
|
}),
|
|
|
|
|
|
|
|
percentMemory: computed('taskGroup.reservedMemory', 'memoryUsed', function() {
|
|
|
|
const used = this.get('memoryUsed') / 1024 / 1024;
|
|
|
|
const total = this.get('taskGroup.reservedMemory');
|
|
|
|
if (!total || !used) {
|
|
|
|
return 0;
|
2017-09-19 14:47:10 +00:00
|
|
|
}
|
2017-10-17 01:46:35 +00:00
|
|
|
return used / total;
|
|
|
|
}),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
percentCPU: computed('stats.ResourceUsage.CpuStats.Percent', function() {
|
|
|
|
return this.get('stats.ResourceUsage.CpuStats.Percent') || 0;
|
|
|
|
}),
|
|
|
|
|
|
|
|
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 = `//${this.get('node.httpAddr')}/v1/client/allocation/${this.get('id')}/stats`;
|
|
|
|
return PromiseObject.create({
|
2017-10-19 02:04:19 +00:00
|
|
|
promise: RSVP.Promise.race([
|
|
|
|
this.get('token')
|
|
|
|
.authorizedRequest(url)
|
|
|
|
.then(res => res.json()),
|
|
|
|
timeout(2000),
|
|
|
|
]),
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
|
|
|
states: fragmentArray('task-state'),
|
|
|
|
});
|