mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 11:25:59 +00:00
SYNC: Merge branch 'feature/dblogs'
This commit is contained in:
committed by
Diflow
parent
4ed437fd4e
commit
ed7605eccd
100
packages/api/src/utility/appLogStore.js
Normal file
100
packages/api/src/utility/appLogStore.js
Normal file
@@ -0,0 +1,100 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { logsdir } = require('./directories');
|
||||
const { format, addDays, startOfDay } = require('date-fns');
|
||||
const JsonLinesDatastore = require('./JsonLinesDatastore');
|
||||
const LineReader = require('./LineReader');
|
||||
const socket = require('./socket');
|
||||
|
||||
async function getLogFiles(timeFrom, timeTo) {
|
||||
const dir = logsdir();
|
||||
const files = await fs.readdir(dir);
|
||||
const startPrefix = format(timeFrom, 'yyyy-MM-dd');
|
||||
const endPrefix = format(addDays(timeTo, 1), 'yyyy-MM-dd');
|
||||
const logFiles = files
|
||||
.filter(file => file.endsWith('.ndjson'))
|
||||
.filter(file => file >= startPrefix && file < endPrefix);
|
||||
return logFiles.sort().map(x => path.join(dir, x));
|
||||
}
|
||||
|
||||
const RECENT_LOG_LIMIT = 1000;
|
||||
|
||||
let recentLogs = null;
|
||||
const beforeRecentLogs = [];
|
||||
|
||||
function adjustRecentLogs() {
|
||||
if (recentLogs.length > RECENT_LOG_LIMIT) {
|
||||
recentLogs.splice(0, recentLogs.length - RECENT_LOG_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyAppLogsIntoFile(timeFrom, timeTo, fileName) {
|
||||
const writeStream = fs.createWriteStream(fileName);
|
||||
|
||||
for (const file of await getLogFiles(timeFrom, timeTo)) {
|
||||
const readStream = fs.createReadStream(file);
|
||||
const reader = new LineReader(readStream);
|
||||
do {
|
||||
const line = await reader.readLine();
|
||||
if (line == null) break;
|
||||
try {
|
||||
const logEntry = JSON.parse(line);
|
||||
if (logEntry.time >= timeFrom && logEntry.time <= timeTo) {
|
||||
writeStream.write(JSON.stringify(logEntry) + '\n');
|
||||
}
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
|
||||
async function initializeRecentLogProvider() {
|
||||
const logs = [];
|
||||
for (const file of await getLogFiles(startOfDay(new Date()), new Date())) {
|
||||
const fileStream = fs.createReadStream(file);
|
||||
const reader = new LineReader(fileStream);
|
||||
do {
|
||||
const line = await reader.readLine();
|
||||
if (line == null) break;
|
||||
try {
|
||||
const logEntry = JSON.parse(line);
|
||||
logs.push(logEntry);
|
||||
if (logs.length > RECENT_LOG_LIMIT) {
|
||||
logs.shift();
|
||||
}
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
recentLogs = logs;
|
||||
recentLogs.push(...beforeRecentLogs);
|
||||
}
|
||||
|
||||
let counter = 0;
|
||||
function pushToRecentLogs(msg) {
|
||||
const finalMsg = {
|
||||
...msg,
|
||||
counter,
|
||||
};
|
||||
counter += 1;
|
||||
if (recentLogs) {
|
||||
recentLogs.push(finalMsg);
|
||||
adjustRecentLogs();
|
||||
socket.emit('applog-event', finalMsg);
|
||||
} else {
|
||||
beforeRecentLogs.push(finalMsg);
|
||||
}
|
||||
}
|
||||
|
||||
function getRecentAppLogRecords() {
|
||||
return recentLogs ?? beforeRecentLogs;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initializeRecentLogProvider,
|
||||
getRecentAppLogRecords,
|
||||
pushToRecentLogs,
|
||||
copyAppLogsIntoFile,
|
||||
};
|
||||
Reference in New Issue
Block a user