open-nomad/ui/app/utils/classes/poll-logger.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

import EmberObject from '@ember/object';
import { task, timeout } from 'ember-concurrency';
import AbstractLogger from './abstract-logger';
import { fetchFailure } from './log';
export default EmberObject.extend(AbstractLogger, {
interval: 1000,
start() {
2019-03-26 07:46:44 +00:00
return this.poll
.linked()
.perform();
},
stop() {
2019-03-26 07:46:44 +00:00
return this.poll.cancelAll();
},
poll: task(function*() {
2019-03-26 07:46:44 +00:00
const { interval, logFetch } = this;
while (true) {
2019-03-26 07:46:44 +00:00
const url = this.fullUrl;
let response = yield logFetch(url).then(res => res, fetchFailure(url));
if (!response) {
return;
}
let text = yield response.text();
if (text) {
const lines = text.replace(/\}\{/g, '}\n{').split('\n');
const frames = lines
.map(line => JSON.parse(line))
.filter(frame => frame.Data != null && frame.Offset != null);
if (frames.length) {
frames.forEach(frame => (frame.Data = window.atob(frame.Data)));
this.set('endOffset', frames[frames.length - 1].Offset);
2019-03-26 07:46:44 +00:00
this.write(frames.mapBy('Data').join(''));
}
}
yield timeout(interval);
}
}),
});