2020-06-13 06:50:56 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-06-13 05:12:56 +00:00
|
|
|
import Component from '@ember/component';
|
2020-06-13 06:50:56 +00:00
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { assert } from '@ember/debug';
|
2020-06-13 05:12:56 +00:00
|
|
|
import { tagName } from '@ember-decorators/component';
|
2020-06-17 21:56:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2020-06-13 21:37:57 +00:00
|
|
|
import Log from 'nomad-ui/utils/classes/log';
|
2020-06-13 05:12:56 +00:00
|
|
|
|
2020-06-13 20:05:45 +00:00
|
|
|
const LEVELS = ['error', 'warn', 'info', 'debug', 'trace'];
|
2020-06-13 05:12:56 +00:00
|
|
|
|
2020-06-17 21:56:16 +00:00
|
|
|
@classic
|
2020-06-13 05:12:56 +00:00
|
|
|
@tagName('')
|
|
|
|
export default class AgentMonitor extends Component {
|
2020-06-13 06:50:56 +00:00
|
|
|
@service token;
|
|
|
|
|
2020-06-13 05:12:56 +00:00
|
|
|
client = null;
|
|
|
|
server = null;
|
2020-06-13 20:05:45 +00:00
|
|
|
level = LEVELS[2];
|
2020-06-13 05:12:56 +00:00
|
|
|
onLevelChange() {}
|
|
|
|
|
2020-06-13 20:05:45 +00:00
|
|
|
levels = LEVELS;
|
2020-06-13 06:50:56 +00:00
|
|
|
monitorUrl = '/v1/agent/monitor';
|
|
|
|
isStreaming = true;
|
2020-06-13 21:37:57 +00:00
|
|
|
logger = null;
|
2020-06-13 06:50:56 +00:00
|
|
|
|
|
|
|
@computed('level', 'client.id', 'server.id')
|
|
|
|
get monitorParams() {
|
|
|
|
assert(
|
|
|
|
'Provide a client OR a server to AgentMonitor, not both.',
|
|
|
|
this.server != null || this.client != null
|
|
|
|
);
|
|
|
|
|
|
|
|
const type = this.server ? 'server_id' : 'client_id';
|
|
|
|
const id = this.server ? this.server.id : this.client && this.client.id;
|
|
|
|
|
|
|
|
return {
|
|
|
|
log_level: this.level,
|
|
|
|
[type]: id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-13 21:37:57 +00:00
|
|
|
didInsertElement() {
|
|
|
|
this.updateLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLogger() {
|
|
|
|
let currentTail = this.logger ? this.logger.tail : '';
|
|
|
|
if (currentTail) {
|
|
|
|
currentTail += `\n...changing log level to ${this.level}...\n\n`;
|
|
|
|
}
|
|
|
|
this.set(
|
|
|
|
'logger',
|
|
|
|
Log.create({
|
|
|
|
logFetch: url => this.token.authorizedRequest(url),
|
|
|
|
params: this.monitorParams,
|
|
|
|
url: this.monitorUrl,
|
|
|
|
tail: currentTail,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-06-13 06:50:56 +00:00
|
|
|
|
2020-06-13 05:12:56 +00:00
|
|
|
setLevel(level) {
|
2020-06-13 06:50:56 +00:00
|
|
|
this.logger.stop();
|
2020-06-13 20:05:45 +00:00
|
|
|
this.set('level', level);
|
2020-06-13 05:12:56 +00:00
|
|
|
this.onLevelChange(level);
|
2020-06-13 21:37:57 +00:00
|
|
|
this.updateLogger();
|
2020-06-13 05:12:56 +00:00
|
|
|
}
|
2020-06-13 06:50:56 +00:00
|
|
|
|
|
|
|
toggleStream() {
|
|
|
|
this.set('streamMode', 'streaming');
|
|
|
|
this.toggleProperty('isStreaming');
|
|
|
|
}
|
2020-06-13 05:12:56 +00:00
|
|
|
}
|