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

79 lines
1.9 KiB
JavaScript
Raw Normal View History

import Ember from 'ember';
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import { task, timeout } from 'ember-concurrency';
import { lazyClick } from '../helpers/lazy-click';
import { classNames, tagName } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
@classic
@tagName('tr')
@classNames('task-row', 'is-interactive')
export default class TaskRow extends Component {
@service store;
@service token;
@service('stats-trackers-registry') statsTrackersRegistry;
task = null;
// Internal state
statsError = false;
@computed
get enablePolling() {
return !Ember.testing;
}
// Since all tasks for an allocation share the same tracker, use the registry
@computed('task', 'task.isRunning')
get stats() {
if (!this.get('task.isRunning')) return undefined;
2019-03-26 07:46:44 +00:00
return this.statsTrackersRegistry.getTracker(this.get('task.allocation'));
}
@computed('task.name', 'stats.tasks.[]')
get taskStats() {
if (!this.stats) return undefined;
return this.get('stats.tasks').findBy('task', this.get('task.name'));
}
@alias('taskStats.cpu.lastObject') cpu;
@alias('taskStats.memory.lastObject') memory;
onClick() {}
click(event) {
2019-03-26 07:46:44 +00:00
lazyClick([this.onClick, event]);
}
@(task(function*() {
do {
2019-03-26 07:46:44 +00:00
if (this.stats) {
try {
yield this.get('stats.poll').perform();
this.set('statsError', false);
} catch (error) {
this.set('statsError', true);
}
}
yield timeout(500);
2019-03-26 07:46:44 +00:00
} while (this.enablePolling);
}).drop())
fetchStats;
didReceiveAttrs() {
const allocation = this.get('task.allocation');
if (allocation) {
2019-03-26 07:46:44 +00:00
this.fetchStats.perform();
} else {
2019-03-26 07:46:44 +00:00
this.fetchStats.cancelAll();
}
}
}