2017-11-14 18:54:36 +00:00
|
|
|
import Ember from 'ember';
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import TextDecoder from 'nomad-ui/utils/classes/text-decoder';
|
2017-11-21 01:09:28 +00:00
|
|
|
import AbstractLogger from './abstract-logger';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
const { Object: EmberObject, computed } = Ember;
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
export default EmberObject.extend(AbstractLogger, {
|
2017-11-14 18:54:36 +00:00
|
|
|
reader: null,
|
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
additionalParams: computed(() => ({
|
|
|
|
follow: true,
|
|
|
|
})),
|
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
start() {
|
|
|
|
return this.get('poll').perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
const reader = this.get('reader');
|
|
|
|
if (reader) {
|
|
|
|
reader.cancel();
|
|
|
|
}
|
|
|
|
return this.get('poll').cancelAll();
|
|
|
|
},
|
|
|
|
|
|
|
|
poll: task(function*() {
|
2017-11-21 01:09:28 +00:00
|
|
|
const url = this.get('fullUrl');
|
2017-11-14 18:54:36 +00:00
|
|
|
const logFetch = this.get('logFetch');
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
let streamClosed = false;
|
2017-11-21 01:09:28 +00:00
|
|
|
let buffer = '';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
const decoder = new TextDecoder();
|
2017-11-14 18:54:36 +00:00
|
|
|
const reader = yield logFetch(url).then(res => res.body.getReader());
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
this.set('reader', reader);
|
|
|
|
|
|
|
|
while (!streamClosed) {
|
|
|
|
yield reader.read().then(({ value, done }) => {
|
|
|
|
streamClosed = done;
|
2017-11-21 01:09:28 +00:00
|
|
|
|
|
|
|
// There is no guarantee that value will be a complete JSON object,
|
|
|
|
// so it needs to be buffered.
|
|
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
|
|
|
|
// Only when the buffer contains a close bracket can we be sure the buffer
|
|
|
|
// is in a complete state
|
|
|
|
if (buffer.indexOf('}') !== -1) {
|
|
|
|
// The buffer can be one or more complete frames with additional text for the
|
|
|
|
// next frame
|
|
|
|
const [, chunk, newBuffer] = buffer.match(/(.*\})(.*)$/);
|
|
|
|
|
|
|
|
// Peel chunk off the front of the buffer (since it represents complete frames)
|
|
|
|
// and set the buffer to be the remainder
|
|
|
|
buffer = newBuffer;
|
|
|
|
|
|
|
|
// Assuming the logs endpoint never returns nested JSON (it shouldn't), at this
|
|
|
|
// point chunk is a series of valid JSON objects with no delimiter.
|
|
|
|
const lines = chunk.replace(/\}\{/g, '}\n{').split('\n');
|
|
|
|
const frames = lines.map(line => JSON.parse(line)).filter(frame => frame.Data);
|
|
|
|
|
|
|
|
if (frames.length) {
|
|
|
|
frames.forEach(frame => (frame.Data = window.atob(frame.Data)));
|
|
|
|
this.set('endOffset', frames[frames.length - 1].Offset);
|
|
|
|
this.get('write')(frames.mapBy('Data').join(''));
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
});
|