open-nomad/ui/app/models/node.js

132 lines
4.2 KiB
JavaScript
Raw Normal View History

import { computed } from '@ember/object';
import { equal } from '@ember/object/computed';
2017-09-19 14:47:10 +00:00
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
import RSVP from 'rsvp';
2017-09-19 14:47:10 +00:00
import shortUUIDProperty from '../utils/properties/short-uuid';
import ipParts from '../utils/ip-parts';
export default Model.extend({
// Available from list response
name: attr('string'),
datacenter: attr('string'),
2019-02-12 04:01:14 +00:00
nodeClass: attr('string'),
2017-09-19 14:47:10 +00:00
isDraining: attr('boolean'),
schedulingEligibility: attr('string'),
2017-09-19 14:47:10 +00:00
status: attr('string'),
statusDescription: attr('string'),
shortId: shortUUIDProperty('id'),
modifyIndex: attr('number'),
// Available from single response
httpAddr: attr('string'),
tlsEnabled: attr('boolean'),
attributes: fragment('node-attributes'),
meta: fragment('node-attributes'),
2017-09-19 14:47:10 +00:00
resources: fragment('resources'),
reserved: fragment('resources'),
drainStrategy: fragment('drain-strategy'),
2017-09-19 14:47:10 +00:00
isEligible: equal('schedulingEligibility', 'eligible'),
2017-09-19 14:47:10 +00:00
address: computed('httpAddr', function() {
2019-03-26 07:46:44 +00:00
return ipParts(this.httpAddr).address;
2017-09-19 14:47:10 +00:00
}),
port: computed('httpAddr', function() {
2019-03-26 07:46:44 +00:00
return ipParts(this.httpAddr).port;
2017-09-19 14:47:10 +00:00
}),
isPartial: computed('httpAddr', function() {
2019-03-26 07:46:44 +00:00
return this.httpAddr == null;
2017-09-19 14:47:10 +00:00
}),
2018-04-25 22:50:15 +00:00
allocations: hasMany('allocations', { inverse: 'node' }),
completeAllocations: computed('allocations.@each.clientStatus', function() {
return this.allocations.filterBy('clientStatus', 'complete');
}),
runningAllocations: computed('allocations.@each.isRunning', function() {
return this.allocations.filterBy('isRunning');
}),
migratingAllocations: computed('allocations.@each.{isMigrating,isRunning}', function() {
return this.allocations.filter(alloc => alloc.isRunning && alloc.isMigrating);
}),
lastMigrateTime: computed('allocations.@each.{isMigrating,isRunning,modifyTime}', function() {
const allocation = this.allocations
.filterBy('isRunning', false)
.filterBy('isMigrating')
.sortBy('modifyTime')
.reverse()[0];
if (allocation) {
return allocation.modifyTime;
}
}),
drivers: fragmentArray('node-driver'),
events: fragmentArray('node-event'),
2020-02-11 00:19:28 +00:00
hostVolumes: fragmentArray('host-volume'),
detectedDrivers: computed('drivers.@each.detected', function() {
2019-03-26 07:46:44 +00:00
return this.drivers.filterBy('detected');
}),
unhealthyDrivers: computed('detectedDrivers.@each.healthy', function() {
2019-03-26 07:46:44 +00:00
return this.detectedDrivers.filterBy('healthy', false);
}),
unhealthyDriverNames: computed('unhealthyDrivers.@each.name', function() {
2019-03-26 07:46:44 +00:00
return this.unhealthyDrivers.mapBy('name');
}),
2018-05-29 17:27:24 +00:00
// A status attribute that includes states not included in node status.
// Useful for coloring and sorting nodes
compositeStatus: computed('isDraining', 'isEligible', 'status', function() {
if (this.isDraining) {
return 'draining';
} else if (!this.isEligible) {
return 'ineligible';
} else {
return this.status;
}
2018-05-29 17:27:24 +00:00
}),
2020-01-24 01:59:21 +00:00
compositeStatusIcon: computed('isDraining', 'isEligible', 'status', function() {
if (this.isDraining || !this.isEligible) {
return 'alert-circle-fill';
} else if (this.status === 'down') {
2020-01-31 20:51:22 +00:00
return 'cancel-circle-fill';
2020-01-24 01:59:21 +00:00
} else if (this.status === 'initializing') {
2020-01-31 20:51:22 +00:00
return 'node-init-circle-fill';
2020-01-24 01:59:21 +00:00
}
2020-01-31 20:51:22 +00:00
return 'check-circle-fill';
2020-01-24 01:59:21 +00:00
}),
setEligible() {
if (this.isEligible) return RSVP.resolve();
2019-11-02 07:55:01 +00:00
// Optimistically update schedulingEligibility for immediate feedback
this.set('schedulingEligibility', 'eligible');
return this.store.adapterFor('node').setEligible(this);
},
setIneligible() {
if (!this.isEligible) return RSVP.resolve();
2019-11-02 07:55:01 +00:00
// Optimistically update schedulingEligibility for immediate feedback
this.set('schedulingEligibility', 'ineligible');
return this.store.adapterFor('node').setIneligible(this);
},
2019-10-25 02:58:18 +00:00
drain(drainSpec) {
return this.store.adapterFor('node').drain(this, drainSpec);
},
forceDrain(drainSpec) {
return this.store.adapterFor('node').forceDrain(this, drainSpec);
},
cancelDrain() {
return this.store.adapterFor('node').cancelDrain(this);
},
2017-09-19 14:47:10 +00:00
});