aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views/video-views-timeserie-stats.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/views/video-views-timeserie-stats.ts')
-rw-r--r--server/tests/api/views/video-views-timeserie-stats.ts74
1 files changed, 66 insertions, 8 deletions
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
9 9
10const expect = chai.expect 10const expect = chai.expect
11 11
12function buildOneMonthAgo () {
13 const monthAgo = new Date()
14 monthAgo.setHours(0, 0, 0, 0)
15
16 monthAgo.setDate(monthAgo.getDate() - 29)
17
18 return monthAgo
19}
20
12describe('Test views timeserie stats', function () { 21describe('Test views timeserie stats', function () {
13 const availableMetrics: VideoStatsTimeserieMetric[] = [ 'viewers' ] 22 const availableMetrics: VideoStatsTimeserieMetric[] = [ 'viewers' ]
14 23
@@ -33,7 +42,7 @@ describe('Test views timeserie stats', function () {
33 for (const metric of availableMetrics) { 42 for (const metric of availableMetrics) {
34 const { data } = await servers[0].videoStats.getTimeserieStats({ videoId: vodVideoId, metric }) 43 const { data } = await servers[0].videoStats.getTimeserieStats({ videoId: vodVideoId, metric })
35 44
36 expect(data).to.have.lengthOf(30) 45 expect(data).to.have.length.at.least(1)
37 46
38 for (const d of data) { 47 for (const d of data) {
39 expect(d.value).to.equal(0) 48 expect(d.value).to.equal(0)
@@ -47,17 +56,19 @@ describe('Test views timeserie stats', function () {
47 let liveVideoId: string 56 let liveVideoId: string
48 let command: FfmpegCommand 57 let command: FfmpegCommand
49 58
50 function expectTodayLastValue (result: VideoStatsTimeserie, lastValue: number) { 59 function expectTodayLastValue (result: VideoStatsTimeserie, lastValue?: number) {
51 const { data } = result 60 const { data } = result
52 61
53 const last = data[data.length - 1] 62 const last = data[data.length - 1]
54 const today = new Date().getDate() 63 const today = new Date().getDate()
55 expect(new Date(last.date).getDate()).to.equal(today) 64 expect(new Date(last.date).getDate()).to.equal(today)
65
66 if (lastValue) expect(last.value).to.equal(lastValue)
56 } 67 }
57 68
58 function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { 69 function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) {
59 const { data } = result 70 const { data } = result
60 expect(data).to.have.lengthOf(30) 71 expect(data).to.have.length.at.least(25)
61 72
62 expectTodayLastValue(result, lastValue) 73 expectTodayLastValue(result, lastValue)
63 74
@@ -87,14 +98,24 @@ describe('Test views timeserie stats', function () {
87 await processViewersStats(servers) 98 await processViewersStats(servers)
88 99
89 for (const videoId of [ vodVideoId, liveVideoId ]) { 100 for (const videoId of [ vodVideoId, liveVideoId ]) {
90 const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' }) 101 const result = await servers[0].videoStats.getTimeserieStats({
102 videoId,
103 startDate: buildOneMonthAgo(),
104 endDate: new Date(),
105 metric: 'viewers'
106 })
91 expectTimeserieData(result, 2) 107 expectTimeserieData(result, 2)
92 } 108 }
93 }) 109 })
94 110
95 it('Should display appropriate watch time metrics', async function () { 111 it('Should display appropriate watch time metrics', async function () {
96 for (const videoId of [ vodVideoId, liveVideoId ]) { 112 for (const videoId of [ vodVideoId, liveVideoId ]) {
97 const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' }) 113 const result = await servers[0].videoStats.getTimeserieStats({
114 videoId,
115 startDate: buildOneMonthAgo(),
116 endDate: new Date(),
117 metric: 'aggregateWatchTime'
118 })
98 expectTimeserieData(result, 8) 119 expectTimeserieData(result, 8)
99 120
100 await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] }) 121 await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] })
@@ -103,7 +124,12 @@ describe('Test views timeserie stats', function () {
103 await processViewersStats(servers) 124 await processViewersStats(servers)
104 125
105 for (const videoId of [ vodVideoId, liveVideoId ]) { 126 for (const videoId of [ vodVideoId, liveVideoId ]) {
106 const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' }) 127 const result = await servers[0].videoStats.getTimeserieStats({
128 videoId,
129 startDate: buildOneMonthAgo(),
130 endDate: new Date(),
131 metric: 'aggregateWatchTime'
132 })
107 expectTimeserieData(result, 9) 133 expectTimeserieData(result, 9)
108 } 134 }
109 }) 135 })
@@ -130,6 +156,38 @@ describe('Test views timeserie stats', function () {
130 expectTodayLastValue(result, 9) 156 expectTodayLastValue(result, 9)
131 }) 157 })
132 158
159 it('Should automatically group by months', async function () {
160 const now = new Date()
161 const heightYearsAgo = new Date()
162 heightYearsAgo.setFullYear(heightYearsAgo.getFullYear() - 7)
163
164 const result = await servers[0].videoStats.getTimeserieStats({
165 videoId: vodVideoId,
166 metric: 'aggregateWatchTime',
167 startDate: heightYearsAgo,
168 endDate: now
169 })
170
171 expect(result.groupInterval).to.equal('6 months')
172 expect(result.data).to.have.length.above(10).and.below(200)
173 })
174
175 it('Should automatically group by days', async function () {
176 const now = new Date()
177 const threeMonthsAgo = new Date()
178 threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3)
179
180 const result = await servers[0].videoStats.getTimeserieStats({
181 videoId: vodVideoId,
182 metric: 'aggregateWatchTime',
183 startDate: threeMonthsAgo,
184 endDate: now
185 })
186
187 expect(result.groupInterval).to.equal('2 days')
188 expect(result.data).to.have.length.above(10).and.below(200)
189 })
190
133 it('Should automatically group by hours', async function () { 191 it('Should automatically group by hours', async function () {
134 const now = new Date() 192 const now = new Date()
135 const twoDaysAgo = new Date() 193 const twoDaysAgo = new Date()
@@ -165,7 +223,7 @@ describe('Test views timeserie stats', function () {
165 expect(result.data).to.have.length.above(20).and.below(30) 223 expect(result.data).to.have.length.above(20).and.below(30)
166 224
167 expectInterval(result, 60 * 10 * 1000) 225 expectInterval(result, 60 * 10 * 1000)
168 expectTodayLastValue(result, 9) 226 expectTodayLastValue(result)
169 }) 227 })
170 228
171 it('Should automatically group by one minute', async function () { 229 it('Should automatically group by one minute', async function () {
@@ -184,7 +242,7 @@ describe('Test views timeserie stats', function () {
184 expect(result.data).to.have.length.above(20).and.below(40) 242 expect(result.data).to.have.length.above(20).and.below(40)
185 243
186 expectInterval(result, 60 * 1000) 244 expectInterval(result, 60 * 1000)
187 expectTodayLastValue(result, 9) 245 expectTodayLastValue(result)
188 }) 246 })
189 247
190 after(async function () { 248 after(async function () {