diff options
Diffstat (limited to 'shared/utils/logs')
-rw-r--r-- | shared/utils/logs/logs.ts | 41 |
1 files changed, 41 insertions, 0 deletions
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 @@ | |||
1 | // Thanks: https://stackoverflow.com/a/37014317 | ||
2 | import { stat } from 'fs-extra' | ||
3 | import { makeGetRequest } from '../requests/requests' | ||
4 | import { LogLevel } from '../../models/server/log-level.type' | ||
5 | |||
6 | async function mtimeSortFilesDesc (files: string[], basePath: string) { | ||
7 | const promises = [] | ||
8 | const out: { file: string, mtime: number }[] = [] | ||
9 | |||
10 | for (const file of files) { | ||
11 | const p = stat(basePath + '/' + file) | ||
12 | .then(stats => { | ||
13 | if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) | ||
14 | }) | ||
15 | |||
16 | promises.push(p) | ||
17 | } | ||
18 | |||
19 | await Promise.all(promises) | ||
20 | |||
21 | out.sort((a, b) => b.mtime - a.mtime) | ||
22 | |||
23 | return out | ||
24 | } | ||
25 | |||
26 | function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) { | ||
27 | const path = '/api/v1/server/logs' | ||
28 | |||
29 | return makeGetRequest({ | ||
30 | url, | ||
31 | path, | ||
32 | token: accessToken, | ||
33 | query: { startDate, endDate, level }, | ||
34 | statusCodeExpected: 200 | ||
35 | }) | ||
36 | } | ||
37 | |||
38 | export { | ||
39 | mtimeSortFilesDesc, | ||
40 | getLogs | ||
41 | } | ||