From fd8710b897a67518d3a61c319e54b6a65ba443ef Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Apr 2019 15:26:33 +0200 Subject: Add logs endpoint --- shared/utils/logs/logs.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 shared/utils/logs/logs.ts (limited to 'shared/utils/logs') diff --git a/shared/utils/logs/logs.ts b/shared/utils/logs/logs.ts new file mode 100644 index 000000000..21adace82 --- /dev/null +++ b/shared/utils/logs/logs.ts @@ -0,0 +1,41 @@ +// Thanks: https://stackoverflow.com/a/37014317 +import { stat } from 'fs-extra' +import { makeGetRequest } from '../requests/requests' +import { LogLevel } from '../../models/server/log-level.type' + +async function mtimeSortFilesDesc (files: string[], basePath: string) { + const promises = [] + const out: { file: string, mtime: number }[] = [] + + for (const file of files) { + const p = stat(basePath + '/' + file) + .then(stats => { + if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) + }) + + promises.push(p) + } + + await Promise.all(promises) + + out.sort((a, b) => b.mtime - a.mtime) + + return out +} + +function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) { + const path = '/api/v1/server/logs' + + return makeGetRequest({ + url, + path, + token: accessToken, + query: { startDate, endDate, level }, + statusCodeExpected: 200 + }) +} + +export { + mtimeSortFilesDesc, + getLogs +} -- cgit v1.2.3