open-nomad/ui/app/components/allocation-row.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-03-22 21:34:57 +00:00
import Ember from 'ember';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
2018-04-21 01:11:32 +00:00
import { computed } from '@ember/object';
import { computed as overridable } from 'ember-overridable-computed';
import { alias } from '@ember/object/computed';
import { run } from '@ember/runloop';
2018-03-22 21:34:57 +00:00
import { task, timeout } from 'ember-concurrency';
import { lazyClick } from '../helpers/lazy-click';
import AllocationStatsTracker from 'nomad-ui/utils/classes/allocation-stats-tracker';
import classic from 'ember-classic-decorator';
import { classNames, tagName } from '@ember-decorators/component';
2017-09-19 14:47:10 +00:00
@classic
@tagName('tr')
@classNames('allocation-row', 'is-interactive')
export default class AllocationRow extends Component {
@service store;
@service token;
allocation = null;
2017-09-19 14:47:10 +00:00
// Used to determine whether the row should mention the node or the job
context = null;
2017-09-19 14:47:10 +00:00
2018-03-22 21:34:57 +00:00
// Internal state
statsError = false;
2018-03-22 21:34:57 +00:00
@overridable(() => !Ember.testing) enablePolling;
2018-04-21 01:11:32 +00:00
@computed('allocation', 'allocation.isRunning')
get stats() {
if (!this.get('allocation.isRunning')) return undefined;
return AllocationStatsTracker.create({
2019-03-26 07:46:44 +00:00
fetch: url => this.token.authorizedRequest(url),
allocation: this.allocation,
});
}
@alias('stats.cpu.lastObject') cpu;
@alias('stats.memory.lastObject') memory;
onClick() {}
2017-09-19 14:47:10 +00:00
click(event) {
2019-03-26 07:46:44 +00:00
lazyClick([this.onClick, event]);
}
didReceiveAttrs() {
this.updateStatsTracker();
}
updateStatsTracker() {
2019-03-26 07:46:44 +00:00
const allocation = this.allocation;
2018-03-22 21:34:57 +00:00
if (allocation) {
2018-05-02 20:59:28 +00:00
run.scheduleOnce('afterRender', this, qualifyAllocation);
2018-03-22 21:34:57 +00:00
} else {
2019-03-26 07:46:44 +00:00
this.fetchStats.cancelAll();
2018-03-22 21:34:57 +00:00
}
}
2018-03-22 21:34:57 +00:00
@(task(function*() {
2018-03-22 21:34:57 +00:00
do {
2019-03-26 07:46:44 +00:00
if (this.stats) {
try {
yield this.get('stats.poll')
.linked()
.perform();
this.set('statsError', false);
} catch (error) {
this.set('statsError', true);
}
2018-03-22 21:34:57 +00:00
}
yield timeout(500);
2019-03-26 07:46:44 +00:00
} while (this.enablePolling);
}).drop())
fetchStats;
}
async function qualifyAllocation() {
2019-03-26 07:46:44 +00:00
const allocation = this.allocation;
// Make sure the allocation is a complete record and not a partial so we
// can show information such as preemptions and rescheduled allocation.
if (allocation.isPartial) {
await this.store.findRecord('allocation', allocation.id, { backgroundReload: false });
}
2018-05-02 20:59:28 +00:00
if (allocation.get('job.isPending')) {
// Make sure the job is loaded before starting the stats tracker
await allocation.get('job');
} else if (!allocation.get('taskGroup')) {
// Make sure that the job record in the store for this allocation
// is complete and not a partial from the list endpoint
const job = allocation.get('job.content');
if (job) await job.reload();
}
this.fetchStats.perform();
}