github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/app/components/task-log.js (about) 1 import Ember from 'ember'; 2 import { task } from 'ember-concurrency'; 3 import { logger } from 'nomad-ui/utils/classes/log'; 4 import WindowResizable from 'nomad-ui/mixins/window-resizable'; 5 6 const { Component, computed, inject, run } = Ember; 7 8 export default Component.extend(WindowResizable, { 9 token: inject.service(), 10 11 classNames: ['boxed-section', 'task-log'], 12 13 allocation: null, 14 task: null, 15 16 didReceiveAttrs() { 17 if (this.get('allocation') && this.get('task')) { 18 this.send('toggleStream'); 19 } 20 }, 21 22 didInsertElement() { 23 this.fillAvailableHeight(); 24 }, 25 26 windowResizeHandler() { 27 run.once(this, this.fillAvailableHeight); 28 }, 29 30 fillAvailableHeight() { 31 // This math is arbitrary and far from bulletproof, but the UX 32 // of having the log window fill available height is worth the hack. 33 const cliWindow = this.$('.cli-window'); 34 cliWindow.height(window.innerHeight - cliWindow.offset().top - 25); 35 }, 36 37 mode: 'stdout', 38 39 logUrl: computed('allocation.id', 'allocation.node.httpAddr', function() { 40 const address = this.get('allocation.node.httpAddr'); 41 const allocation = this.get('allocation.id'); 42 43 return `//${address}/v1/client/fs/logs/${allocation}`; 44 }), 45 46 logParams: computed('task', 'mode', function() { 47 return { 48 task: this.get('task'), 49 type: this.get('mode'), 50 }; 51 }), 52 53 logger: logger('logUrl', 'logParams', function() { 54 const token = this.get('token'); 55 return token.authorizedRequest.bind(token); 56 }), 57 58 head: task(function*() { 59 yield this.get('logger.gotoHead').perform(); 60 run.scheduleOnce('afterRender', () => { 61 this.$('.cli-window').scrollTop(0); 62 }); 63 }), 64 65 tail: task(function*() { 66 yield this.get('logger.gotoTail').perform(); 67 run.scheduleOnce('afterRender', () => { 68 const cliWindow = this.$('.cli-window'); 69 cliWindow.scrollTop(cliWindow[0].scrollHeight); 70 }); 71 }), 72 73 stream: task(function*() { 74 this.get('logger').on('tick', () => { 75 run.scheduleOnce('afterRender', () => { 76 const cliWindow = this.$('.cli-window'); 77 cliWindow.scrollTop(cliWindow[0].scrollHeight); 78 }); 79 }); 80 81 yield this.get('logger').startStreaming(); 82 this.get('logger').off('tick'); 83 }), 84 85 willDestroy() { 86 this.get('logger').stop(); 87 }, 88 89 actions: { 90 setMode(mode) { 91 this.get('logger').stop(); 92 this.set('mode', mode); 93 this.get('stream').perform(); 94 }, 95 toggleStream() { 96 if (this.get('logger.isStreaming')) { 97 this.get('logger').stop(); 98 } else { 99 this.get('stream').perform(); 100 } 101 }, 102 }, 103 });