2018-09-13 23:32:29 +00:00
|
|
|
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';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { classNames } from '@ember-decorators/component';
|
|
|
|
import classic from 'ember-classic-decorator';
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@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
|
|
|
|
2018-09-13 23:32:29 +00:00
|
|
|
// One of Node, Allocation, or TaskState
|
2020-06-10 13:49:16 +00:00
|
|
|
resource = null;
|
2018-09-13 23:32:29 +00:00
|
|
|
|
|
|
|
// cpu or memory
|
2020-06-10 13:49:16 +00:00
|
|
|
metric = null;
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
'data-test-primary-metric' = true;
|
2018-09-19 23:33:51 +00:00
|
|
|
|
2018-09-13 23:32:29 +00:00
|
|
|
// An instance of a StatsTracker. An alternative interface to resource
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('trackedResource', 'type')
|
|
|
|
get tracker() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const resource = this.trackedResource;
|
|
|
|
return this.statsTrackersRegistry.getTracker(resource);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('resource')
|
|
|
|
get type() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const resource = this.resource;
|
2018-09-13 23:32:29 +00:00
|
|
|
return resource && resource.constructor.modelName;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('resource', 'type')
|
|
|
|
get trackedResource() {
|
2018-09-13 23:32:29 +00:00
|
|
|
// TaskStates use the allocation stats tracker
|
2020-06-10 13:49:16 +00:00
|
|
|
return this.type === 'task-state' ? this.get('resource.allocation') : this.resource;
|
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('metric')
|
|
|
|
get metricLabel() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const metric = this.metric;
|
2018-09-13 23:32:29 +00:00
|
|
|
const mappings = {
|
|
|
|
cpu: 'CPU',
|
|
|
|
memory: 'Memory',
|
|
|
|
};
|
|
|
|
return mappings[metric] || metric;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('resource', 'metric', 'type')
|
|
|
|
get data() {
|
2019-03-26 07:46:44 +00:00
|
|
|
if (!this.tracker) return [];
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2019-03-26 07:46:44 +00:00
|
|
|
const metric = this.metric;
|
|
|
|
if (this.type === 'task-state') {
|
2018-09-13 23:32:29 +00:00
|
|
|
// 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}`);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('resource', 'metric', 'type')
|
|
|
|
get reservedAmount() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const metricProperty = this.metric === 'cpu' ? 'reservedCPU' : 'reservedMemory';
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.type === 'task-state') {
|
2018-09-13 23:32:29 +00:00
|
|
|
const task = this.get('tracker.tasks').findBy('task', this.get('resource.name'));
|
|
|
|
return task[metricProperty];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.get(`tracker.${metricProperty}`);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('metric')
|
|
|
|
get chartClass() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const metric = this.metric;
|
2018-09-13 23:32:29 +00:00
|
|
|
const mappings = {
|
|
|
|
cpu: 'is-info',
|
|
|
|
memory: 'is-danger',
|
|
|
|
};
|
|
|
|
|
|
|
|
return mappings[metric] || 'is-primary';
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@task(function*() {
|
2018-09-13 23:32:29 +00:00
|
|
|
do {
|
2018-09-17 23:59:09 +00:00
|
|
|
this.get('tracker.poll').perform();
|
|
|
|
yield timeout(100);
|
2018-09-13 23:32:29 +00:00
|
|
|
} while (!Ember.testing);
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
poller;
|
2018-09-13 23:32:29 +00:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.tracker) {
|
|
|
|
this.poller.perform();
|
2018-09-13 23:32:29 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-09-13 23:32:29 +00:00
|
|
|
|
|
|
|
willDestroy() {
|
2019-03-26 07:46:44 +00:00
|
|
|
this.poller.cancelAll();
|
2018-09-17 23:59:09 +00:00
|
|
|
this.get('tracker.signalPause').perform();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|