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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

176 lines
4.8 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { computed } from '@ember/object';
import { equal } from '@ember/object/computed';
import Model from '@ember-data/model';
import { attr } from '@ember-data/model';
import { hasMany } from '@ember-data/model';
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';
import classic from 'ember-classic-decorator';
2017-09-19 14:47:10 +00:00
@classic
export default class Node extends Model {
2017-09-19 14:47:10 +00:00
// Available from list response
@attr('string') name;
@attr('string') datacenter;
@attr('string') nodeClass;
@attr('boolean') isDraining;
@attr('string') schedulingEligibility;
@attr('string') status;
@attr('string') statusDescription;
@shortUUIDProperty('id') shortId;
@attr('number') modifyIndex;
@attr('string') version;
@attr('string') nodePool;
2017-09-19 14:47:10 +00:00
// Available from single response
@attr('string') httpAddr;
@attr('boolean') tlsEnabled;
2021-10-12 20:36:10 +00:00
@fragment('structured-attributes') attributes;
@fragment('structured-attributes') meta;
@fragment('resources') resources;
@fragment('resources') reserved;
@fragment('drain-strategy') drainStrategy;
@equal('schedulingEligibility', 'eligible') isEligible;
@computed('httpAddr')
get address() {
2019-03-26 07:46:44 +00:00
return ipParts(this.httpAddr).address;
}
2017-09-19 14:47:10 +00:00
@computed('httpAddr')
get port() {
2019-03-26 07:46:44 +00:00
return ipParts(this.httpAddr).port;
}
2017-09-19 14:47:10 +00:00
@computed('httpAddr')
get isPartial() {
2019-03-26 07:46:44 +00:00
return this.httpAddr == null;
}
@hasMany('allocations', { inverse: 'node' }) allocations;
2017-09-19 14:47:10 +00:00
@computed('allocations.@each.clientStatus')
get completeAllocations() {
return this.allocations.filterBy('clientStatus', 'complete');
}
@computed('allocations.@each.isRunning')
get runningAllocations() {
return this.allocations.filterBy('isRunning');
}
@computed('allocations.@each.{isMigrating,isRunning}')
get migratingAllocations() {
2021-12-28 16:08:12 +00:00
return this.allocations.filter(
(alloc) => alloc.isRunning && alloc.isMigrating
);
}
@computed('allocations.@each.{isMigrating,isRunning,modifyTime}')
get lastMigrateTime() {
const allocation = this.allocations
.filterBy('isRunning', false)
.filterBy('isMigrating')
.sortBy('modifyTime')
.reverse()[0];
if (allocation) {
return allocation.modifyTime;
}
return undefined;
}
@fragmentArray('node-driver') drivers;
@fragmentArray('node-event') events;
@fragmentArray('host-volume') hostVolumes;
@computed('drivers.@each.detected')
get detectedDrivers() {
2019-03-26 07:46:44 +00:00
return this.drivers.filterBy('detected');
}
@computed('detectedDrivers.@each.healthy')
get unhealthyDrivers() {
2019-03-26 07:46:44 +00:00
return this.detectedDrivers.filterBy('healthy', false);
}
@computed('unhealthyDrivers.@each.name')
get unhealthyDriverNames() {
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
@computed('isDraining', 'isEligible', 'status')
get compositeStatus() {
if (this.status === 'down') {
return 'down';
} else if (this.isDraining) {
return 'draining';
} else if (!this.isEligible) {
return 'ineligible';
} else {
return this.status;
}
}
@computed('isDraining', 'isEligible', 'status')
get compositeStatusIcon() {
2020-01-24 01:59:21 +00:00
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);
}
2019-10-25 02:58:18 +00:00
forceDrain(drainSpec) {
return this.store.adapterFor('node').forceDrain(this, drainSpec);
}
cancelDrain() {
return this.store.adapterFor('node').cancelDrain(this);
}
async addMeta(newMeta) {
let metaResponse = await this.store
.adapterFor('node')
.addMeta(this, newMeta);
if (!this.meta) {
this.set('meta', this.store.createFragment('structured-attributes'));
}
this.meta.recomputeRawProperties(metaResponse.Meta);
return metaResponse;
}
}