aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils/logs/logs.ts
blob: 21adace82f6d9faa9171e394c25d3e4531cfdaf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
}