X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fviews%2Fvideo-views-timeserie-stats.ts;h=fd3aba188a33e8f74da18e760056be451c377898;hb=9295c68b74fe1f1e2e9f72009205d7f0379844c5;hp=98c041cdfae1b2f74fedc175d0bdf2cb595b550f;hpb=b211106695bb82f6c32e53306081b5262c3d109d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/views/video-views-timeserie-stats.ts b/server/tests/api/views/video-views-timeserie-stats.ts index 98c041cdf..fd3aba188 100644 --- a/server/tests/api/views/video-views-timeserie-stats.ts +++ b/server/tests/api/views/video-views-timeserie-stats.ts @@ -24,7 +24,7 @@ describe('Test views timeserie stats', function () { let vodVideoId: string before(async function () { - this.timeout(60000); + this.timeout(120000); ({ vodVideoId } = await prepareViewsVideos({ servers, live: false, vod: true })) }) @@ -47,23 +47,33 @@ 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(60000); + this.timeout(120000); ({ vodVideoId, liveVideoId, ffmpegCommand: command } = await prepareViewsVideos({ servers, live: true, vod: true })) }) @@ -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 twentyDaysAgo = new Date() + twentyDaysAgo.setDate(twentyDaysAgo.getDate() - 19) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: twentyDaysAgo, + endDate: now + }) + + 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(twentyDaysAgo.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('1 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() - 4) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: twoHoursAgo, + endDate: now + }) + + 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) + }) + + 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('1 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) })