aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views/video-views-timeserie-stats.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-24 13:36:47 +0100
committerChocobozzz <chocobozzz@cpy.re>2022-04-15 09:49:35 +0200
commitb211106695bb82f6c32e53306081b5262c3d109d (patch)
treefa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/tests/api/views/video-views-timeserie-stats.ts
parent69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff)
downloadPeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.gz
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.zst
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.zip
Support video views/viewers stats in server
* Add "currentTime" and "event" body params to view endpoint * Merge watching and view endpoints * Introduce WatchAction AP activity * Add tables to store viewer information of local videos * Add endpoints to fetch video views/viewers stats of local videos * Refactor views/viewers handlers * Support "views" and "viewers" counters for both VOD and live videos
Diffstat (limited to 'server/tests/api/views/video-views-timeserie-stats.ts')
-rw-r--r--server/tests/api/views/video-views-timeserie-stats.ts109
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
3import 'mocha'
4import * as chai from 'chai'
5import { FfmpegCommand } from 'fluent-ffmpeg'
6import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
7import { VideoStatsTimeserie, VideoStatsTimeserieMetric } from '@shared/models'
8import { cleanupTests, PeerTubeServer, stopFfmpeg } from '@shared/server-commands'
9
10const expect = chai.expect
11
12describe('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})