From 901bcf5c188ea79350fecd499ad76460b866617b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 7 Apr 2022 10:53:35 +0200 Subject: Add ability to set start/end date to timeserie --- server/tests/api/check-params/views.ts | 48 +++++++++++ .../tests/api/views/video-views-timeserie-stats.ts | 97 +++++++++++++++++++++- 2 files changed, 141 insertions(+), 4 deletions(-) (limited to 'server/tests/api') diff --git a/server/tests/api/check-params/views.ts b/server/tests/api/check-params/views.ts index ca4752345..3dba2a42e 100644 --- a/server/tests/api/check-params/views.ts +++ b/server/tests/api/check-params/views.ts @@ -112,6 +112,54 @@ describe('Test videos views', function () { await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'hello' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) + it('Should fail with an invalid start date', async function () { + await servers[0].videoStats.getTimeserieStats({ + videoId, + metric: 'viewers', + startDate: 'fake' as any, + endDate: new Date(), + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) + }) + + it('Should fail with an invalid end date', async function () { + await servers[0].videoStats.getTimeserieStats({ + videoId, + metric: 'viewers', + startDate: new Date(), + endDate: 'fake' as any, + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) + }) + + it('Should fail if start date is specified but not end date', async function () { + await servers[0].videoStats.getTimeserieStats({ + videoId, + metric: 'viewers', + startDate: new Date(), + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) + }) + + it('Should fail if end date is specified but not start date', async function () { + await servers[0].videoStats.getTimeserieStats({ + videoId, + metric: 'viewers', + endDate: new Date(), + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) + }) + + it('Should fail with a too big interval', async function () { + await servers[0].videoStats.getTimeserieStats({ + videoId, + metric: 'viewers', + startDate: new Date('2021-04-07T08:31:57.126Z'), + endDate: new Date(), + expectedStatus: HttpStatusCode.BAD_REQUEST_400 + }) + }) + it('Should succeed with the correct parameters', async function () { await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' }) }) diff --git a/server/tests/api/views/video-views-timeserie-stats.ts b/server/tests/api/views/video-views-timeserie-stats.ts index 858edeff7..4db76fe89 100644 --- a/server/tests/api/views/video-views-timeserie-stats.ts +++ b/server/tests/api/views/video-views-timeserie-stats.ts @@ -47,21 +47,31 @@ describe('Test views timeserie stats', function () { let liveVideoId: string let command: FfmpegCommand - function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { + function expectTodayLastValue (result: VideoStatsTimeserie, lastValue: number) { const { data } = result - expect(data).to.have.lengthOf(30) const last = data[data.length - 1] - const today = new Date().getDate() expect(new Date(last.date).getDate()).to.equal(today) - expect(last.value).to.equal(lastValue) + } + + function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { + const { data } = result + expect(data).to.have.lengthOf(30) + + expectTodayLastValue(result, lastValue) for (let i = 0; i < data.length - 2; i++) { expect(data[i].value).to.equal(0) } } + function expectInterval (result: VideoStatsTimeserie, intervalMs: number) { + const first = result.data[0] + const second = result.data[1] + expect(new Date(second.date).getTime() - new Date(first.date).getTime()).to.equal(intervalMs) + } + before(async function () { this.timeout(120000); @@ -98,6 +108,85 @@ 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 result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: tenDaysAgo, + endDate: now + }) + + expect(result.groupInterval).to.equal('one_day') + expect(result.data).to.have.lengthOf(10) + + const first = result.data[0] + expect(new Date(first.date).toLocaleDateString()).to.equal(tenDaysAgo.toLocaleDateString()) + + expectInterval(result, 24 * 3600 * 1000) + expectTodayLastValue(result, 9) + }) + + it('Should automatically group by hours', async function () { + const now = new Date() + const twoDaysAgo = new Date() + twoDaysAgo.setDate(twoDaysAgo.getDate() - 1) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: twoDaysAgo, + endDate: now + }) + + expect(result.groupInterval).to.equal('one_hour') + expect(result.data).to.have.length.above(24).and.below(50) + + expectInterval(result, 3600 * 1000) + expectTodayLastValue(result, 9) + }) + + it('Should automatically group by ten minutes', async function () { + const now = new Date() + const twoHoursAgo = new Date() + twoHoursAgo.setHours(twoHoursAgo.getHours() - 1) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: twoHoursAgo, + endDate: now + }) + + expect(result.groupInterval).to.equal('ten_minutes') + expect(result.data).to.have.length.above(6).and.below(18) + + expectInterval(result, 60 * 10 * 1000) + expectTodayLastValue(result, 9) + }) + + it('Should automatically group by one minute', async function () { + const now = new Date() + const thirtyAgo = new Date() + thirtyAgo.setMinutes(thirtyAgo.getMinutes() - 30) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: thirtyAgo, + endDate: now + }) + + expect(result.groupInterval).to.equal('one_minute') + expect(result.data).to.have.length.above(20).and.below(40) + + expectInterval(result, 60 * 1000) + expectTodayLastValue(result, 9) + }) + after(async function () { await stopFfmpeg(command) }) -- cgit v1.2.3