Files
cloudron-box/dashboard/src/models/LogsModel.js
T

97 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-07-12 13:21:21 +02:00
2025-03-16 11:12:49 +01:00
import moment from 'moment-timezone';
2023-07-12 13:21:21 +02:00
import { ansiToHtml } from 'anser';
2025-03-03 11:22:56 +01:00
import { API_ORIGIN } from '../constants.js';
2023-07-12 13:21:21 +02:00
// https://github.com/janl/mustache.js/blob/master/mustache.js#L60
const entityMap = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;',
'/': '&#x2F;',
'`': '&#x60;',
'=': '&#x3D;'
};
function escapeHtml(string) {
return String(string).replace(/[&<>"'`=/]/g, function fromEntityMap (s) {
return entityMap[s];
});
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
2025-01-31 21:02:48 +01:00
export function create(type, id) {
const accessToken = localStorage.token;
2024-07-23 17:25:13 +02:00
const INITIAL_STREAM_LINES = 100;
2023-07-12 13:21:21 +02:00
let eventSource = null;
let streamApi = '';
let downloadApi = '';
if (type === 'platform') {
2023-08-04 13:41:13 +05:30
streamApi = '/api/v1/system/logstream/box';
downloadApi = '/api/v1/system/logs/box';
2023-07-12 13:21:21 +02:00
} else if (type === 'crash') {
2023-08-04 13:41:13 +05:30
streamApi = `/api/v1/system/logstream/crash-${id}`;
downloadApi = `/api/v1/system/logs/crash-${id}`;
2023-07-12 13:21:21 +02:00
} else if (type === 'app') {
streamApi = `/api/v1/apps/${id}/logstream`;
downloadApi = `/api/v1/apps/${id}/logs`;
} else if (type === 'service') {
streamApi = `/api/v1/services/${id}/logstream`;
downloadApi = `/api/v1/services/${id}/logs`;
} else if (type === 'task') {
streamApi = `/api/v1/tasks/${id}/logstream`;
downloadApi = `/api/v1/tasks/${id}/logs`;
} else {
console.error('unsupported logs type', type);
}
return {
name: 'LogsModel',
stream(lineHandler, errorHandler) {
2025-03-03 11:22:56 +01:00
eventSource = new EventSource(`${API_ORIGIN}${streamApi}?lines=${INITIAL_STREAM_LINES}&access_token=${accessToken}`);
2024-09-12 10:31:31 +02:00
eventSource._lastMessage = null;
eventSource.onerror = function ( /* uselessError */) {
if (eventSource.readyState === EventSource.CLOSED) {
// eventSource does not give us the HTTP error code. We have to resort to message count check and guess the reason
const msg = eventSource._lastMessage === null ? `Logs unavailable. Maybe the logs were logrotated.` : `Connection closed.`;
const e = new Error(msg);
e.time = moment().format('MMM DD HH:mm:ss');
e.html = ansiToHtml(e.message);
errorHandler(e);
}
};
2023-07-12 13:21:21 +02:00
// eventSource.onopen = function () { console.log('stream is open'); };
eventSource.onmessage = function (message) {
var data;
try {
data = JSON.parse(message.data);
} catch (e) {
return console.error(e);
}
const time = data.realtimeTimestamp ? moment(data.realtimeTimestamp/1000).format('MMM DD HH:mm:ss') : '';
const html = ansiToHtml(escapeHtml(typeof data.message === 'string' ? data.message : ab2str(data.message)));
2024-09-12 10:31:31 +02:00
eventSource._lastMessage = { time, html };
2024-02-22 18:08:32 +01:00
lineHandler(time, html);
2023-07-12 13:21:21 +02:00
};
},
getDownloadUrl() {
2025-03-03 11:22:56 +01:00
return `${API_ORIGIN}${downloadApi}?access_token=${accessToken}&format=short&lines=-1`;
2023-07-12 13:21:21 +02:00
}
};
}
export default {
create
};