open-nomad/ui/app/components/primary-metric.js

108 lines
2.6 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 { task, timeout } from 'ember-concurrency';
import { classNames } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
@classic
@classNames('primary-metric')
export default class PrimaryMetric extends Component {
@service token;
@service('stats-trackers-registry') statsTrackersRegistry;
2018-09-14 17:20:33 +00:00
// One of Node, Allocation, or TaskState
resource = null;
// cpu or memory
metric = null;
'data-test-primary-metric' = true;
// An instance of a StatsTracker. An alternative interface to resource
@computed('trackedResource', 'type')
get tracker() {
2019-03-26 07:46:44 +00:00
const resource = this.trackedResource;
return this.statsTrackersRegistry.getTracker(resource);
}
@computed('resource')
get type() {
2019-03-26 07:46:44 +00:00
const resource = this.resource;
return resource && resource.constructor.modelName;
}
@computed('resource', 'type')
get trackedResource() {
// TaskStates use the allocation stats tracker
return this.type === 'task-state' ? this.get('resource.allocation') : this.resource;
}
@computed('metric')
get metricLabel() {
2019-03-26 07:46:44 +00:00
const metric = this.metric;
const mappings = {
cpu: 'CPU',
memory: 'Memory',
};
return mappings[metric] || metric;
}
@computed('resource', 'metric', 'type')
get data() {
2019-03-26 07:46:44 +00:00
if (!this.tracker) return [];
2019-03-26 07:46:44 +00:00
const metric = this.metric;
if (this.type === 'task-state') {
// handle getting the right task out of the tracker
const task = this.get('tracker.tasks').findBy('task', this.get('resource.name'));
return task && task[metric];
}
return this.get(`tracker.${metric}`);
}
@computed('resource', 'metric', 'type')
get reservedAmount() {
2019-03-26 07:46:44 +00:00
const metricProperty = this.metric === 'cpu' ? 'reservedCPU' : 'reservedMemory';
2019-03-26 07:46:44 +00:00
if (this.type === 'task-state') {
const task = this.get('tracker.tasks').findBy('task', this.get('resource.name'));
return task[metricProperty];
}
return this.get(`tracker.${metricProperty}`);
}
@computed('metric')
get chartClass() {
2019-03-26 07:46:44 +00:00
const metric = this.metric;
const mappings = {
cpu: 'is-info',
memory: 'is-danger',
};
return mappings[metric] || 'is-primary';
}
@task(function*() {
do {
this.get('tracker.poll').perform();
yield timeout(100);
} while (!Ember.testing);
})
poller;
didReceiveAttrs() {
2019-03-26 07:46:44 +00:00
if (this.tracker) {
this.poller.perform();
}
}
willDestroy() {
2019-03-26 07:46:44 +00:00
this.poller.cancelAll();
this.get('tracker.signalPause').perform();
}
}