github.com/hernad/nomad@v1.6.112/ui/app/components/primary-metric/node.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import Ember from 'ember'; 7 import Component from '@glimmer/component'; 8 import { task, timeout } from 'ember-concurrency'; 9 import { assert } from '@ember/debug'; 10 import { inject as service } from '@ember/service'; 11 import { action, get } from '@ember/object'; 12 import { 13 formatScheduledBytes, 14 formatScheduledHertz, 15 } from 'nomad-ui/utils/units'; 16 17 export default class NodePrimaryMetric extends Component { 18 @service('stats-trackers-registry') statsTrackersRegistry; 19 20 /** Args 21 node = null; 22 metric null; (one of 'cpu' or 'memory') 23 */ 24 25 get metric() { 26 assert('metric is a required argument', this.args.metric); 27 return this.args.metric; 28 } 29 30 get tracker() { 31 return this.statsTrackersRegistry.getTracker(this.args.node); 32 } 33 34 get data() { 35 if (!this.tracker) return []; 36 return get(this, `tracker.${this.metric}`); 37 } 38 39 get reservedAmount() { 40 if (this.metric === 'cpu') return this.tracker.reservedCPU; 41 if (this.metric === 'memory') return this.tracker.reservedMemory; 42 return null; 43 } 44 45 get chartClass() { 46 if (this.metric === 'cpu') return 'is-info'; 47 if (this.metric === 'memory') return 'is-danger'; 48 return 'is-primary'; 49 } 50 51 get reservedAnnotations() { 52 if (this.metric === 'cpu' && get(this.args.node, 'reserved.cpu')) { 53 const cpu = this.args.node.reserved.cpu; 54 return [ 55 { 56 label: `${formatScheduledHertz(cpu, 'MHz')} reserved`, 57 percent: cpu / this.reservedAmount, 58 }, 59 ]; 60 } 61 62 if (this.metric === 'memory' && get(this.args.node, 'reserved.memory')) { 63 const memory = this.args.node.reserved.memory; 64 return [ 65 { 66 label: `${formatScheduledBytes(memory, 'MiB')} reserved`, 67 percent: memory / this.reservedAmount, 68 }, 69 ]; 70 } 71 72 return []; 73 } 74 75 @task(function* () { 76 do { 77 this.tracker.poll.perform(); 78 yield timeout(100); 79 } while (!Ember.testing); 80 }) 81 poller; 82 83 @action 84 start() { 85 if (this.tracker) this.poller.perform(); 86 } 87 88 willDestroy() { 89 super.willDestroy(...arguments); 90 this.poller.cancelAll(); 91 this.tracker.signalPause.perform(); 92 } 93 }