2017-12-15 21:39:18 +00:00
|
|
|
import { alias } from '@ember/object/computed';
|
|
|
|
import { assert } from '@ember/debug';
|
2019-04-27 15:21:26 +00:00
|
|
|
import { htmlSafe } from '@ember/template';
|
2017-12-15 21:39:18 +00:00
|
|
|
import Evented from '@ember/object/evented';
|
|
|
|
import EmberObject, { computed } from '@ember/object';
|
2019-10-15 18:32:58 +00:00
|
|
|
import { computed as overridable } from 'ember-overridable-computed';
|
2017-12-15 21:39:18 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
2018-07-06 20:38:57 +00:00
|
|
|
import queryString from 'query-string';
|
2017-11-14 18:54:36 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import StreamLogger from 'nomad-ui/utils/classes/stream-logger';
|
|
|
|
import PollLogger from 'nomad-ui/utils/classes/poll-logger';
|
2019-06-26 21:52:41 +00:00
|
|
|
import { decode } from 'nomad-ui/utils/stream-frames';
|
2019-04-27 14:52:24 +00:00
|
|
|
import Anser from 'anser';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-21 23:05:28 +00:00
|
|
|
const MAX_OUTPUT_LENGTH = 50000;
|
|
|
|
|
2019-03-26 05:27:47 +00:00
|
|
|
// eslint-disable-next-line
|
2021-12-28 14:45:20 +00:00
|
|
|
export const fetchFailure = (url) => () => console.warn(`LOG FETCH: Couldn't connect to ${url}`);
|
2018-02-26 20:23:01 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
class Log extends EmberObject.extend(Evented) {
|
2017-11-14 18:54:36 +00:00
|
|
|
// Parameters
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
url = '';
|
|
|
|
|
|
|
|
@overridable(() => ({}))
|
|
|
|
params;
|
|
|
|
|
|
|
|
plainText = false;
|
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
logFetch() {
|
2017-12-15 21:39:18 +00:00
|
|
|
assert('Log objects need a logFetch method, which should have an interface like window.fetch');
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
// Read-only state
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@alias('logStreamer.poll.isRunning')
|
|
|
|
isStreaming;
|
|
|
|
|
|
|
|
logPointer = null;
|
|
|
|
logStreamer = null;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
// The top of the log
|
2020-06-10 13:49:16 +00:00
|
|
|
head = '';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
// The bottom of the log
|
2020-06-10 13:49:16 +00:00
|
|
|
tail = '';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
// The top or bottom of the log, depending on whether
|
|
|
|
// the logPointer is pointed at head or tail
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('logPointer', 'head', 'tail')
|
|
|
|
get output() {
|
2019-04-27 14:52:24 +00:00
|
|
|
let logs = this.logPointer === 'head' ? this.head : this.tail;
|
2019-07-20 18:46:54 +00:00
|
|
|
logs = logs.replace(/</g, '<').replace(/>/g, '>');
|
2019-04-27 15:21:26 +00:00
|
|
|
let colouredLogs = Anser.ansiToHtml(logs);
|
|
|
|
return htmlSafe(colouredLogs);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
init() {
|
2020-06-10 13:49:16 +00:00
|
|
|
super.init();
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
const args = this.getProperties('url', 'params', 'logFetch');
|
2021-12-28 14:45:20 +00:00
|
|
|
args.write = (chunk) => {
|
2019-03-26 07:46:44 +00:00
|
|
|
let newTail = this.tail + chunk;
|
2017-11-21 23:05:28 +00:00
|
|
|
if (newTail.length > MAX_OUTPUT_LENGTH) {
|
|
|
|
newTail = newTail.substr(newTail.length - MAX_OUTPUT_LENGTH);
|
2017-11-14 18:54:36 +00:00
|
|
|
}
|
|
|
|
this.set('tail', newTail);
|
2017-11-15 17:13:17 +00:00
|
|
|
this.trigger('tick', chunk);
|
2017-11-14 18:54:36 +00:00
|
|
|
};
|
|
|
|
|
2017-11-21 22:57:19 +00:00
|
|
|
if (StreamLogger.isSupported) {
|
2017-11-14 18:54:36 +00:00
|
|
|
this.set('logStreamer', StreamLogger.create(args));
|
|
|
|
} else {
|
|
|
|
this.set('logStreamer', PollLogger.create(args));
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.stop();
|
2020-06-10 13:49:16 +00:00
|
|
|
super.destroy();
|
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2019-03-26 07:46:44 +00:00
|
|
|
const logFetch = this.logFetch;
|
2017-11-14 18:54:36 +00:00
|
|
|
const queryParams = queryString.stringify(
|
2019-07-21 04:01:39 +00:00
|
|
|
assign(
|
|
|
|
{
|
|
|
|
origin: 'start',
|
|
|
|
offset: 0,
|
|
|
|
},
|
|
|
|
this.params
|
|
|
|
)
|
2017-11-14 18:54:36 +00:00
|
|
|
);
|
2019-03-26 07:46:44 +00:00
|
|
|
const url = `${this.url}?${queryParams}`;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
this.stop();
|
2021-12-28 14:45:20 +00:00
|
|
|
const response = yield logFetch(url).then((res) => res.text(), fetchFailure(url));
|
2019-07-02 22:36:38 +00:00
|
|
|
let text = this.plainText ? response : decode(response).message;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2018-02-26 20:23:01 +00:00
|
|
|
if (text && text.length > MAX_OUTPUT_LENGTH) {
|
2017-11-21 23:05:28 +00:00
|
|
|
text = text.substr(0, MAX_OUTPUT_LENGTH);
|
2017-11-14 18:54:36 +00:00
|
|
|
text += '\n\n---------- TRUNCATED: Click "tail" to view the bottom of the log ----------';
|
|
|
|
}
|
|
|
|
this.set('head', text);
|
|
|
|
this.set('logPointer', 'head');
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
gotoHead;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2019-03-26 07:46:44 +00:00
|
|
|
const logFetch = this.logFetch;
|
2017-11-14 18:54:36 +00:00
|
|
|
const queryParams = queryString.stringify(
|
2019-07-21 04:01:39 +00:00
|
|
|
assign(
|
|
|
|
{
|
|
|
|
origin: 'end',
|
|
|
|
offset: MAX_OUTPUT_LENGTH,
|
|
|
|
},
|
|
|
|
this.params
|
|
|
|
)
|
2017-11-14 18:54:36 +00:00
|
|
|
);
|
2019-03-26 07:46:44 +00:00
|
|
|
const url = `${this.url}?${queryParams}`;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
this.stop();
|
2021-12-28 14:45:20 +00:00
|
|
|
const response = yield logFetch(url).then((res) => res.text(), fetchFailure(url));
|
2019-07-02 22:36:38 +00:00
|
|
|
let text = this.plainText ? response : decode(response).message;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
this.set('tail', text);
|
|
|
|
this.set('logPointer', 'tail');
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
gotoTail;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
startStreaming() {
|
|
|
|
this.set('logPointer', 'tail');
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.logStreamer.start();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
stop() {
|
2019-03-26 07:46:44 +00:00
|
|
|
this.logStreamer.stop();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
|
|
|
|
export default Log;
|
|
|
|
|
|
|
|
export function logger(urlProp, params, logFetch) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return computed(urlProp, params, function () {
|
2017-11-14 18:54:36 +00:00
|
|
|
return Log.create({
|
|
|
|
logFetch: logFetch.call(this),
|
|
|
|
params: this.get(params),
|
|
|
|
url: this.get(urlProp),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|