aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/core-utils/logs/logs.ts25
-rw-r--r--shared/utils/logs/logs.ts23
-rw-r--r--shared/utils/miscs/sql.ts15
3 files changed, 40 insertions, 23 deletions
diff --git a/shared/core-utils/logs/logs.ts b/shared/core-utils/logs/logs.ts
new file mode 100644
index 000000000..d0996cf55
--- /dev/null
+++ b/shared/core-utils/logs/logs.ts
@@ -0,0 +1,25 @@
1import { stat } from 'fs-extra'
2
3async function mtimeSortFilesDesc (files: string[], basePath: string) {
4 const promises = []
5 const out: { file: string, mtime: number }[] = []
6
7 for (const file of files) {
8 const p = stat(basePath + '/' + file)
9 .then(stats => {
10 if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
11 })
12
13 promises.push(p)
14 }
15
16 await Promise.all(promises)
17
18 out.sort((a, b) => b.mtime - a.mtime)
19
20 return out
21}
22
23export {
24 mtimeSortFilesDesc
25}
diff --git a/shared/utils/logs/logs.ts b/shared/utils/logs/logs.ts
index 21adace82..cbb1afb93 100644
--- a/shared/utils/logs/logs.ts
+++ b/shared/utils/logs/logs.ts
@@ -1,28 +1,6 @@
1// Thanks: https://stackoverflow.com/a/37014317
2import { stat } from 'fs-extra'
3import { makeGetRequest } from '../requests/requests' 1import { makeGetRequest } from '../requests/requests'
4import { LogLevel } from '../../models/server/log-level.type' 2import { LogLevel } from '../../models/server/log-level.type'
5 3
6async 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
26function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) { 4function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) {
27 const path = '/api/v1/server/logs' 5 const path = '/api/v1/server/logs'
28 6
@@ -36,6 +14,5 @@ function getLogs (url: string, accessToken: string, startDate: Date, endDate?: D
36} 14}
37 15
38export { 16export {
39 mtimeSortFilesDesc,
40 getLogs 17 getLogs
41} 18}
diff --git a/shared/utils/miscs/sql.ts b/shared/utils/miscs/sql.ts
index 1ce3d801a..b281471ce 100644
--- a/shared/utils/miscs/sql.ts
+++ b/shared/utils/miscs/sql.ts
@@ -48,6 +48,20 @@ function setPlaylistField (serverNumber: number, uuid: string, field: string, va
48 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) 48 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
49} 49}
50 50
51async function countVideoViewsOf (serverNumber: number, uuid: string) {
52 const seq = getSequelize(serverNumber)
53
54 // tslint:disable
55 const query = `SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
56
57 const options = { type: Sequelize.QueryTypes.SELECT }
58 const [ { total } ] = await seq.query(query, options)
59
60 if (!total) return 0
61
62 return parseInt(total, 10)
63}
64
51async function closeAllSequelize (servers: any[]) { 65async function closeAllSequelize (servers: any[]) {
52 for (let i = 1; i <= servers.length; i++) { 66 for (let i = 1; i <= servers.length; i++) {
53 if (sequelizes[ i ]) { 67 if (sequelizes[ i ]) {
@@ -61,5 +75,6 @@ export {
61 setVideoField, 75 setVideoField,
62 setPlaylistField, 76 setPlaylistField,
63 setActorField, 77 setActorField,
78 countVideoViewsOf,
64 closeAllSequelize 79 closeAllSequelize
65} 80}