Fix stats time metric

This commit is contained in:
Chocobozzz 2022-05-06 14:49:42 +02:00
parent f40712abbb
commit 67ad83d0fa
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 11 additions and 1 deletions

View File

@ -236,6 +236,16 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
aggregateWatchTime: 'SUM("localVideoViewer"."watchTime")' aggregateWatchTime: 'SUM("localVideoViewer"."watchTime")'
} }
const intervalWhere: { [ id in VideoStatsTimeserieMetric ]: string } = {
// Viewer is still in the interval. Overlap algorithm
viewers: '"localVideoViewer"."startDate" <= "intervals"."endDate" ' +
'AND "localVideoViewer"."endDate" >= "intervals"."startDate"',
// We do an aggregation, so only sum things once. Arbitrary we use the end date for that purpose
aggregateWatchTime: '"localVideoViewer"."endDate" >= "intervals"."startDate" ' +
'AND "localVideoViewer"."endDate" <= "intervals"."endDate"'
}
const query = `WITH "intervals" AS ( const query = `WITH "intervals" AS (
SELECT SELECT
"time" AS "startDate", "time" + :groupInterval::interval as "endDate" "time" AS "startDate", "time" + :groupInterval::interval as "endDate"
@ -246,7 +256,7 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
FROM FROM
intervals intervals
LEFT JOIN "localVideoViewer" ON "localVideoViewer"."videoId" = :videoId LEFT JOIN "localVideoViewer" ON "localVideoViewer"."videoId" = :videoId
AND "localVideoViewer"."startDate" >= "intervals"."startDate" AND "localVideoViewer"."startDate" <= "intervals"."endDate" AND ${intervalWhere[metric]}
GROUP BY GROUP BY
"intervals"."startDate" "intervals"."startDate"
ORDER BY ORDER BY