diff options
Diffstat (limited to 'server/tests/api/views/video-views-timeserie-stats.ts')
-rw-r--r-- | server/tests/api/views/video-views-timeserie-stats.ts | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/server/tests/api/views/video-views-timeserie-stats.ts b/server/tests/api/views/video-views-timeserie-stats.ts new file mode 100644 index 000000000..98c041cdf --- /dev/null +++ b/server/tests/api/views/video-views-timeserie-stats.ts | |||
@@ -0,0 +1,109 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import 'mocha' | ||
4 | import * as chai from 'chai' | ||
5 | import { FfmpegCommand } from 'fluent-ffmpeg' | ||
6 | import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared' | ||
7 | import { VideoStatsTimeserie, VideoStatsTimeserieMetric } from '@shared/models' | ||
8 | import { cleanupTests, PeerTubeServer, stopFfmpeg } from '@shared/server-commands' | ||
9 | |||
10 | const expect = chai.expect | ||
11 | |||
12 | describe('Test views timeserie stats', function () { | ||
13 | const availableMetrics: VideoStatsTimeserieMetric[] = [ 'viewers' ] | ||
14 | |||
15 | let servers: PeerTubeServer[] | ||
16 | |||
17 | before(async function () { | ||
18 | this.timeout(120000) | ||
19 | |||
20 | servers = await prepareViewsServers() | ||
21 | }) | ||
22 | |||
23 | describe('Common metric tests', function () { | ||
24 | let vodVideoId: string | ||
25 | |||
26 | before(async function () { | ||
27 | this.timeout(60000); | ||
28 | |||
29 | ({ vodVideoId } = await prepareViewsVideos({ servers, live: false, vod: true })) | ||
30 | }) | ||
31 | |||
32 | it('Should display empty metric stats', async function () { | ||
33 | for (const metric of availableMetrics) { | ||
34 | const { data } = await servers[0].videoStats.getTimeserieStats({ videoId: vodVideoId, metric }) | ||
35 | |||
36 | expect(data).to.have.lengthOf(30) | ||
37 | |||
38 | for (const d of data) { | ||
39 | expect(d.value).to.equal(0) | ||
40 | } | ||
41 | } | ||
42 | }) | ||
43 | }) | ||
44 | |||
45 | describe('Test viewer and watch time metrics on live and VOD', function () { | ||
46 | let vodVideoId: string | ||
47 | let liveVideoId: string | ||
48 | let command: FfmpegCommand | ||
49 | |||
50 | function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { | ||
51 | const { data } = result | ||
52 | expect(data).to.have.lengthOf(30) | ||
53 | |||
54 | const last = data[data.length - 1] | ||
55 | |||
56 | const today = new Date().getDate() | ||
57 | expect(new Date(last.date).getDate()).to.equal(today) | ||
58 | expect(last.value).to.equal(lastValue) | ||
59 | |||
60 | for (let i = 0; i < data.length - 2; i++) { | ||
61 | expect(data[i].value).to.equal(0) | ||
62 | } | ||
63 | } | ||
64 | |||
65 | before(async function () { | ||
66 | this.timeout(60000); | ||
67 | |||
68 | ({ vodVideoId, liveVideoId, ffmpegCommand: command } = await prepareViewsVideos({ servers, live: true, vod: true })) | ||
69 | }) | ||
70 | |||
71 | it('Should display appropriate viewers metrics', async function () { | ||
72 | for (const videoId of [ vodVideoId, liveVideoId ]) { | ||
73 | await servers[0].views.simulateViewer({ id: videoId, currentTimes: [ 0, 3 ] }) | ||
74 | await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 5 ] }) | ||
75 | } | ||
76 | |||
77 | await processViewersStats(servers) | ||
78 | |||
79 | for (const videoId of [ vodVideoId, liveVideoId ]) { | ||
80 | const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' }) | ||
81 | expectTimeserieData(result, 2) | ||
82 | } | ||
83 | }) | ||
84 | |||
85 | it('Should display appropriate watch time metrics', async function () { | ||
86 | for (const videoId of [ vodVideoId, liveVideoId ]) { | ||
87 | const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' }) | ||
88 | expectTimeserieData(result, 8) | ||
89 | |||
90 | await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] }) | ||
91 | } | ||
92 | |||
93 | await processViewersStats(servers) | ||
94 | |||
95 | for (const videoId of [ vodVideoId, liveVideoId ]) { | ||
96 | const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' }) | ||
97 | expectTimeserieData(result, 9) | ||
98 | } | ||
99 | }) | ||
100 | |||
101 | after(async function () { | ||
102 | await stopFfmpeg(command) | ||
103 | }) | ||
104 | }) | ||
105 | |||
106 | after(async function () { | ||
107 | await cleanupTests(servers) | ||
108 | }) | ||
109 | }) | ||