1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 19:42:24 +02:00

Add filter by start/end date overall stats in api

This commit is contained in:
Chocobozzz 2022-05-05 14:12:57 +02:00
parent f18a060a83
commit 49f0468d44
No known key found for this signature in database
GPG key ID: 583A612D890159BE
9 changed files with 121 additions and 12 deletions

View file

@ -100,10 +100,28 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
})
}
static async getOverallStats (video: MVideo): Promise<VideoStatsOverall> {
const options = {
static async getOverallStats (options: {
video: MVideo
startDate?: string
endDate?: string
}): Promise<VideoStatsOverall> {
const { video, startDate, endDate } = options
const queryOptions = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { videoId: video.id }
replacements: { videoId: video.id } as any
}
let dateWhere = ''
if (startDate) {
dateWhere += ' AND "localVideoViewer"."startDate" >= :startDate'
queryOptions.replacements.startDate = startDate
}
if (endDate) {
dateWhere += ' AND "localVideoViewer"."endDate" <= :endDate'
queryOptions.replacements.endDate = endDate
}
const watchTimeQuery = `SELECT ` +
@ -111,9 +129,9 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
`AVG("localVideoViewer"."watchTime") AS "averageWatchTime" ` +
`FROM "localVideoViewer" ` +
`INNER JOIN "video" ON "video"."id" = "localVideoViewer"."videoId" ` +
`WHERE "videoId" = :videoId`
`WHERE "videoId" = :videoId ${dateWhere}`
const watchTimePromise = LocalVideoViewerModel.sequelize.query<any>(watchTimeQuery, options)
const watchTimePromise = LocalVideoViewerModel.sequelize.query<any>(watchTimeQuery, queryOptions)
const watchPeakQuery = `WITH "watchPeakValues" AS (
SELECT "startDate" AS "dateBreakpoint", 1 AS "inc"
@ -122,7 +140,7 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
UNION ALL
SELECT "endDate" AS "dateBreakpoint", -1 AS "inc"
FROM "localVideoViewer"
WHERE "videoId" = :videoId
WHERE "videoId" = :videoId ${dateWhere}
)
SELECT "dateBreakpoint", "concurrent"
FROM (
@ -132,14 +150,14 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
) tmp
ORDER BY "concurrent" DESC
FETCH FIRST 1 ROW ONLY`
const watchPeakPromise = LocalVideoViewerModel.sequelize.query<any>(watchPeakQuery, options)
const watchPeakPromise = LocalVideoViewerModel.sequelize.query<any>(watchPeakQuery, queryOptions)
const countriesQuery = `SELECT country, COUNT(country) as viewers ` +
`FROM "localVideoViewer" ` +
`WHERE "videoId" = :videoId AND country IS NOT NULL ` +
`WHERE "videoId" = :videoId AND country IS NOT NULL ${dateWhere} ` +
`GROUP BY country ` +
`ORDER BY viewers DESC`
const countriesPromise = LocalVideoViewerModel.sequelize.query<any>(countriesQuery, options)
const countriesPromise = LocalVideoViewerModel.sequelize.query<any>(countriesQuery, queryOptions)
const [ rowsWatchTime, rowsWatchPeak, rowsCountries ] = await Promise.all([
watchTimePromise,