Support interactive video stats graph

This commit is contained in:
Chocobozzz
2022-04-15 09:49:35 +02:00
committed by Chocobozzz
parent 901bcf5c18
commit 3eda9b775a
12 changed files with 268 additions and 64 deletions
+9 -15
View File
@@ -1,24 +1,17 @@
import { logger } from '@server/helpers/logger'
import { VideoStatsTimeserieGroupInterval } from '@shared/models'
function buildGroupByAndBoundaries (startDateString: string, endDateString: string) {
const startDate = new Date(startDateString)
const endDate = new Date(endDateString)
const groupByMatrix: { [ id in VideoStatsTimeserieGroupInterval ]: string } = {
one_day: '1 day',
one_hour: '1 hour',
ten_minutes: '10 minutes',
one_minute: '1 minute'
}
const groupInterval = buildGroupInterval(startDate, endDate)
logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate })
// Remove parts of the date we don't need
if (groupInterval === 'one_day') {
if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) {
startDate.setHours(0, 0, 0, 0)
} else if (groupInterval === 'one_hour') {
} else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) {
startDate.setMinutes(0, 0, 0)
} else {
startDate.setSeconds(0, 0)
@@ -26,7 +19,6 @@ function buildGroupByAndBoundaries (startDateString: string, endDateString: stri
return {
groupInterval,
sqlInterval: groupByMatrix[groupInterval],
startDate,
endDate
}
@@ -40,16 +32,18 @@ export {
// ---------------------------------------------------------------------------
function buildGroupInterval (startDate: Date, endDate: Date): VideoStatsTimeserieGroupInterval {
function buildGroupInterval (startDate: Date, endDate: Date): string {
const aDay = 86400
const anHour = 3600
const aMinute = 60
const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
if (diffSeconds >= 6 * aDay) return 'one_day'
if (diffSeconds >= 6 * anHour) return 'one_hour'
if (diffSeconds >= 60 * aMinute) return 'ten_minutes'
if (diffSeconds >= 15 * aDay) return '1 day'
if (diffSeconds >= 8 * aDay) return '12 hours'
if (diffSeconds >= 4 * aDay) return '6 hours'
if (diffSeconds >= 15 * anHour) return '1 hour'
if (diffSeconds >= 180 * aMinute) return '10 minutes'
return 'one_minute'
return '1 minute'
}
+4 -4
View File
@@ -221,7 +221,7 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
}): Promise<VideoStatsTimeserie> {
const { video, metric } = options
const { groupInterval, sqlInterval, startDate, endDate } = buildGroupByAndBoundaries(options.startDate, options.endDate)
const { groupInterval, startDate, endDate } = buildGroupByAndBoundaries(options.startDate, options.endDate)
const selectMetrics: { [ id in VideoStatsTimeserieMetric ]: string } = {
viewers: 'COUNT("localVideoViewer"."id")',
@@ -230,9 +230,9 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
const query = `WITH "intervals" AS (
SELECT
"time" AS "startDate", "time" + :sqlInterval::interval as "endDate"
"time" AS "startDate", "time" + :groupInterval::interval as "endDate"
FROM
generate_series(:startDate::timestamptz, :endDate::timestamptz, :sqlInterval::interval) serie("time")
generate_series(:startDate::timestamptz, :endDate::timestamptz, :groupInterval::interval) serie("time")
)
SELECT "intervals"."startDate" as "date", COALESCE(${selectMetrics[metric]}, 0) AS value
FROM
@@ -249,7 +249,7 @@ export class LocalVideoViewerModel extends Model<Partial<AttributesOnly<LocalVid
replacements: {
startDate,
endDate,
sqlInterval,
groupInterval,
videoId: video.id
}
}
@@ -110,21 +110,21 @@ describe('Test views timeserie stats', function () {
it('Should use a custom start/end date', async function () {
const now = new Date()
const tenDaysAgo = new Date()
tenDaysAgo.setDate(tenDaysAgo.getDate() - 9)
const twentyDaysAgo = new Date()
twentyDaysAgo.setDate(twentyDaysAgo.getDate() - 19)
const result = await servers[0].videoStats.getTimeserieStats({
videoId: vodVideoId,
metric: 'aggregateWatchTime',
startDate: tenDaysAgo,
startDate: twentyDaysAgo,
endDate: now
})
expect(result.groupInterval).to.equal('one_day')
expect(result.data).to.have.lengthOf(10)
expect(result.groupInterval).to.equal('1 day')
expect(result.data).to.have.lengthOf(20)
const first = result.data[0]
expect(new Date(first.date).toLocaleDateString()).to.equal(tenDaysAgo.toLocaleDateString())
expect(new Date(first.date).toLocaleDateString()).to.equal(twentyDaysAgo.toLocaleDateString())
expectInterval(result, 24 * 3600 * 1000)
expectTodayLastValue(result, 9)
@@ -142,7 +142,7 @@ describe('Test views timeserie stats', function () {
endDate: now
})
expect(result.groupInterval).to.equal('one_hour')
expect(result.groupInterval).to.equal('1 hour')
expect(result.data).to.have.length.above(24).and.below(50)
expectInterval(result, 3600 * 1000)
@@ -152,7 +152,7 @@ describe('Test views timeserie stats', function () {
it('Should automatically group by ten minutes', async function () {
const now = new Date()
const twoHoursAgo = new Date()
twoHoursAgo.setHours(twoHoursAgo.getHours() - 1)
twoHoursAgo.setHours(twoHoursAgo.getHours() - 4)
const result = await servers[0].videoStats.getTimeserieStats({
videoId: vodVideoId,
@@ -161,8 +161,8 @@ describe('Test views timeserie stats', function () {
endDate: now
})
expect(result.groupInterval).to.equal('ten_minutes')
expect(result.data).to.have.length.above(6).and.below(18)
expect(result.groupInterval).to.equal('10 minutes')
expect(result.data).to.have.length.above(20).and.below(30)
expectInterval(result, 60 * 10 * 1000)
expectTodayLastValue(result, 9)
@@ -180,7 +180,7 @@ describe('Test views timeserie stats', function () {
endDate: now
})
expect(result.groupInterval).to.equal('one_minute')
expect(result.groupInterval).to.equal('1 minute')
expect(result.data).to.have.length.above(20).and.below(40)
expectInterval(result, 60 * 1000)