From cda03765fe366f028897e9b02dd4a0a19af3c935 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 11 Apr 2019 17:33:36 +0200 Subject: Add ability to delete old remote views --- shared/core-utils/logs/logs.ts | 25 +++++++++++++++++++++++++ shared/utils/logs/logs.ts | 23 ----------------------- shared/utils/miscs/sql.ts | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 shared/core-utils/logs/logs.ts (limited to 'shared') 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 @@ +import { stat } from 'fs-extra' + +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 +} + +export { + mtimeSortFilesDesc +} 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 @@ -// 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' @@ -36,6 +14,5 @@ function getLogs (url: string, accessToken: string, startDate: Date, endDate?: D } export { - mtimeSortFilesDesc, getLogs } 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 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) } +async function countVideoViewsOf (serverNumber: number, uuid: string) { + const seq = getSequelize(serverNumber) + + // tslint:disable + const query = `SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` + + const options = { type: Sequelize.QueryTypes.SELECT } + const [ { total } ] = await seq.query(query, options) + + if (!total) return 0 + + return parseInt(total, 10) +} + async function closeAllSequelize (servers: any[]) { for (let i = 1; i <= servers.length; i++) { if (sequelizes[ i ]) { @@ -61,5 +75,6 @@ export { setVideoField, setPlaylistField, setActorField, + countVideoViewsOf, closeAllSequelize } -- cgit v1.2.3