From f40712abbbb74e51f06037ef02757c42736bccf8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 6 May 2022 14:23:02 +0200 Subject: Add ability to filter overall video stats by date --- .../tests/api/views/video-views-overall-stats.ts | 26 +++++++- .../tests/api/views/video-views-timeserie-stats.ts | 74 +++++++++++++++++++--- 2 files changed, 90 insertions(+), 10 deletions(-) (limited to 'server/tests/api/views') diff --git a/server/tests/api/views/video-views-overall-stats.ts b/server/tests/api/views/video-views-overall-stats.ts index 53b8f0d4b..02012388d 100644 --- a/server/tests/api/views/video-views-overall-stats.ts +++ b/server/tests/api/views/video-views-overall-stats.ts @@ -169,6 +169,7 @@ describe('Test views overall stats', function () { describe('Test watchers peak stats of local videos on VOD', function () { let videoUUID: string + let before2Watchers: Date before(async function () { this.timeout(120000); @@ -201,7 +202,7 @@ describe('Test views overall stats', function () { it('Should have watcher peak with 2 watchers', async function () { this.timeout(60000) - const before = new Date() + before2Watchers = new Date() await servers[0].views.view({ id: videoUUID, currentTime: 0 }) await servers[1].views.view({ id: videoUUID, currentTime: 0 }) await servers[0].views.view({ id: videoUUID, currentTime: 2 }) @@ -213,11 +214,26 @@ describe('Test views overall stats', function () { const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID }) expect(stats.viewersPeak).to.equal(2) - expect(new Date(stats.viewersPeakDate)).to.be.above(before).and.below(after) + expect(new Date(stats.viewersPeakDate)).to.be.above(before2Watchers).and.below(after) + }) + + it('Should filter peak viewers stats by date', async function () { + { + const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() }) + expect(stats.viewersPeak).to.equal(0) + expect(stats.viewersPeakDate).to.not.exist + } + + { + const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate: before2Watchers.toISOString() }) + expect(stats.viewersPeak).to.equal(1) + expect(new Date(stats.viewersPeakDate)).to.be.below(before2Watchers) + } }) }) describe('Test countries', function () { + let videoUUID: string it('Should not report countries if geoip is disabled', async function () { this.timeout(120000) @@ -237,6 +253,7 @@ describe('Test views overall stats', function () { this.timeout(240000) const { uuid } = await servers[0].videos.quickUpload({ name: 'video' }) + videoUUID = uuid await waitJobs(servers) await Promise.all([ @@ -265,6 +282,11 @@ describe('Test views overall stats', function () { expect(stats.countries[1].isoCode).to.equal('FR') expect(stats.countries[1].viewers).to.equal(1) }) + + it('Should filter countries stats by date', async function () { + const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() }) + expect(stats.countries).to.have.lengthOf(0) + }) }) after(async function () { diff --git a/server/tests/api/views/video-views-timeserie-stats.ts b/server/tests/api/views/video-views-timeserie-stats.ts index fd3aba188..6f03b0e82 100644 --- a/server/tests/api/views/video-views-timeserie-stats.ts +++ b/server/tests/api/views/video-views-timeserie-stats.ts @@ -9,6 +9,15 @@ import { cleanupTests, PeerTubeServer, stopFfmpeg } from '@shared/server-command const expect = chai.expect +function buildOneMonthAgo () { + const monthAgo = new Date() + monthAgo.setHours(0, 0, 0, 0) + + monthAgo.setDate(monthAgo.getDate() - 29) + + return monthAgo +} + describe('Test views timeserie stats', function () { const availableMetrics: VideoStatsTimeserieMetric[] = [ 'viewers' ] @@ -33,7 +42,7 @@ describe('Test views timeserie stats', function () { for (const metric of availableMetrics) { const { data } = await servers[0].videoStats.getTimeserieStats({ videoId: vodVideoId, metric }) - expect(data).to.have.lengthOf(30) + expect(data).to.have.length.at.least(1) for (const d of data) { expect(d.value).to.equal(0) @@ -47,17 +56,19 @@ describe('Test views timeserie stats', function () { let liveVideoId: string let command: FfmpegCommand - function expectTodayLastValue (result: VideoStatsTimeserie, lastValue: number) { + function expectTodayLastValue (result: VideoStatsTimeserie, lastValue?: number) { const { data } = result const last = data[data.length - 1] const today = new Date().getDate() expect(new Date(last.date).getDate()).to.equal(today) + + if (lastValue) expect(last.value).to.equal(lastValue) } function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { const { data } = result - expect(data).to.have.lengthOf(30) + expect(data).to.have.length.at.least(25) expectTodayLastValue(result, lastValue) @@ -87,14 +98,24 @@ describe('Test views timeserie stats', function () { await processViewersStats(servers) for (const videoId of [ vodVideoId, liveVideoId ]) { - const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' }) + const result = await servers[0].videoStats.getTimeserieStats({ + videoId, + startDate: buildOneMonthAgo(), + endDate: new Date(), + metric: 'viewers' + }) expectTimeserieData(result, 2) } }) it('Should display appropriate watch time metrics', async function () { for (const videoId of [ vodVideoId, liveVideoId ]) { - const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' }) + const result = await servers[0].videoStats.getTimeserieStats({ + videoId, + startDate: buildOneMonthAgo(), + endDate: new Date(), + metric: 'aggregateWatchTime' + }) expectTimeserieData(result, 8) await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] }) @@ -103,7 +124,12 @@ describe('Test views timeserie stats', function () { await processViewersStats(servers) for (const videoId of [ vodVideoId, liveVideoId ]) { - const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' }) + const result = await servers[0].videoStats.getTimeserieStats({ + videoId, + startDate: buildOneMonthAgo(), + endDate: new Date(), + metric: 'aggregateWatchTime' + }) expectTimeserieData(result, 9) } }) @@ -130,6 +156,38 @@ describe('Test views timeserie stats', function () { expectTodayLastValue(result, 9) }) + it('Should automatically group by months', async function () { + const now = new Date() + const heightYearsAgo = new Date() + heightYearsAgo.setFullYear(heightYearsAgo.getFullYear() - 7) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: heightYearsAgo, + endDate: now + }) + + expect(result.groupInterval).to.equal('6 months') + expect(result.data).to.have.length.above(10).and.below(200) + }) + + it('Should automatically group by days', async function () { + const now = new Date() + const threeMonthsAgo = new Date() + threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3) + + const result = await servers[0].videoStats.getTimeserieStats({ + videoId: vodVideoId, + metric: 'aggregateWatchTime', + startDate: threeMonthsAgo, + endDate: now + }) + + expect(result.groupInterval).to.equal('2 days') + expect(result.data).to.have.length.above(10).and.below(200) + }) + it('Should automatically group by hours', async function () { const now = new Date() const twoDaysAgo = new Date() @@ -165,7 +223,7 @@ describe('Test views timeserie stats', function () { expect(result.data).to.have.length.above(20).and.below(30) expectInterval(result, 60 * 10 * 1000) - expectTodayLastValue(result, 9) + expectTodayLastValue(result) }) it('Should automatically group by one minute', async function () { @@ -184,7 +242,7 @@ describe('Test views timeserie stats', function () { expect(result.data).to.have.length.above(20).and.below(40) expectInterval(result, 60 * 1000) - expectTodayLastValue(result, 9) + expectTodayLastValue(result) }) after(async function () { -- cgit v1.2.3