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

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

73 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2021-03-19 21:34:00 +00:00
import Ember from 'ember';
import Component from '@glimmer/component';
2021-03-22 07:46:37 +00:00
import { tracked } from '@glimmer/tracking';
2021-03-19 21:34:00 +00:00
import { task, timeout } from 'ember-concurrency';
import { assert } from '@ember/debug';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class TaskPrimaryMetric extends Component {
@service('stats-trackers-registry') statsTrackersRegistry;
/** Args
taskState = null;
metric null; (one of 'cpu' or 'memory'
*/
2021-03-22 07:46:37 +00:00
@tracked tracker = null;
@tracked taskState = null;
2021-03-19 21:34:00 +00:00
get metric() {
assert('metric is a required argument', this.args.metric);
return this.args.metric;
}
get data() {
if (!this.tracker) return [];
2021-03-22 07:46:37 +00:00
const task = this.tracker.tasks.findBy('task', this.taskState.name);
2021-03-19 21:34:00 +00:00
return task && task[this.metric];
}
get reservedAmount() {
2021-03-22 07:46:37 +00:00
if (!this.tracker) return null;
const task = this.tracker.tasks.findBy('task', this.taskState.name);
if (this.metric === 'cpu') return task.reservedCPU;
if (this.metric === 'memory') return task.reservedMemory;
return null;
2021-03-19 21:34:00 +00:00
}
get chartClass() {
if (this.metric === 'cpu') return 'is-info';
if (this.metric === 'memory') return 'is-danger';
return 'is-primary';
}
2021-12-28 14:45:20 +00:00
@task(function* () {
2021-03-19 21:34:00 +00:00
do {
this.tracker.poll.perform();
yield timeout(100);
} while (!Ember.testing);
})
poller;
@action
start() {
2021-03-22 07:46:37 +00:00
this.taskState = this.args.taskState;
2021-12-28 16:08:12 +00:00
this.tracker = this.statsTrackersRegistry.getTracker(
this.args.taskState.allocation
);
2021-03-22 07:46:37 +00:00
this.poller.perform();
2021-03-19 21:34:00 +00:00
}
willDestroy() {
2021-12-28 14:45:20 +00:00
super.willDestroy(...arguments);
2021-03-19 21:34:00 +00:00
this.poller.cancelAll();
this.tracker.signalPause.perform();
}
}