aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-04-07 10:53:35 +0200
committerChocobozzz <chocobozzz@cpy.re>2022-04-15 09:49:35 +0200
commit901bcf5c188ea79350fecd499ad76460b866617b (patch)
tree1e79f26cc3f2b952371d31bfa9b94a2b150be38a /server/tests/api/views
parentac907dc7c158056e9b6a5cb58acd27df5c7c2670 (diff)
downloadPeerTube-901bcf5c188ea79350fecd499ad76460b866617b.tar.gz
PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.tar.zst
PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.zip
Add ability to set start/end date to timeserie
Diffstat (limited to 'server/tests/api/views')
-rw-r--r--server/tests/api/views/video-views-timeserie-stats.ts97
1 files changed, 93 insertions, 4 deletions
diff --git a/server/tests/api/views/video-views-timeserie-stats.ts b/server/tests/api/views/video-views-timeserie-stats.ts
index 858edeff7..4db76fe89 100644
--- a/server/tests/api/views/video-views-timeserie-stats.ts
+++ b/server/tests/api/views/video-views-timeserie-stats.ts
@@ -47,21 +47,31 @@ describe('Test views timeserie stats', function () {
47 let liveVideoId: string 47 let liveVideoId: string
48 let command: FfmpegCommand 48 let command: FfmpegCommand
49 49
50 function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) { 50 function expectTodayLastValue (result: VideoStatsTimeserie, lastValue: number) {
51 const { data } = result 51 const { data } = result
52 expect(data).to.have.lengthOf(30)
53 52
54 const last = data[data.length - 1] 53 const last = data[data.length - 1]
55
56 const today = new Date().getDate() 54 const today = new Date().getDate()
57 expect(new Date(last.date).getDate()).to.equal(today) 55 expect(new Date(last.date).getDate()).to.equal(today)
58 expect(last.value).to.equal(lastValue) 56 }
57
58 function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) {
59 const { data } = result
60 expect(data).to.have.lengthOf(30)
61
62 expectTodayLastValue(result, lastValue)
59 63
60 for (let i = 0; i < data.length - 2; i++) { 64 for (let i = 0; i < data.length - 2; i++) {
61 expect(data[i].value).to.equal(0) 65 expect(data[i].value).to.equal(0)
62 } 66 }
63 } 67 }
64 68
69 function expectInterval (result: VideoStatsTimeserie, intervalMs: number) {
70 const first = result.data[0]
71 const second = result.data[1]
72 expect(new Date(second.date).getTime() - new Date(first.date).getTime()).to.equal(intervalMs)
73 }
74
65 before(async function () { 75 before(async function () {
66 this.timeout(120000); 76 this.timeout(120000);
67 77
@@ -98,6 +108,85 @@ describe('Test views timeserie stats', function () {
98 } 108 }
99 }) 109 })
100 110
111 it('Should use a custom start/end date', async function () {
112 const now = new Date()
113 const tenDaysAgo = new Date()
114 tenDaysAgo.setDate(tenDaysAgo.getDate() - 9)
115
116 const result = await servers[0].videoStats.getTimeserieStats({
117 videoId: vodVideoId,
118 metric: 'aggregateWatchTime',
119 startDate: tenDaysAgo,
120 endDate: now
121 })
122
123 expect(result.groupInterval).to.equal('one_day')
124 expect(result.data).to.have.lengthOf(10)
125
126 const first = result.data[0]
127 expect(new Date(first.date).toLocaleDateString()).to.equal(tenDaysAgo.toLocaleDateString())
128
129 expectInterval(result, 24 * 3600 * 1000)
130 expectTodayLastValue(result, 9)
131 })
132
133 it('Should automatically group by hours', async function () {
134 const now = new Date()
135 const twoDaysAgo = new Date()
136 twoDaysAgo.setDate(twoDaysAgo.getDate() - 1)
137
138 const result = await servers[0].videoStats.getTimeserieStats({
139 videoId: vodVideoId,
140 metric: 'aggregateWatchTime',
141 startDate: twoDaysAgo,
142 endDate: now
143 })
144
145 expect(result.groupInterval).to.equal('one_hour')
146 expect(result.data).to.have.length.above(24).and.below(50)
147
148 expectInterval(result, 3600 * 1000)
149 expectTodayLastValue(result, 9)
150 })
151
152 it('Should automatically group by ten minutes', async function () {
153 const now = new Date()
154 const twoHoursAgo = new Date()
155 twoHoursAgo.setHours(twoHoursAgo.getHours() - 1)
156
157 const result = await servers[0].videoStats.getTimeserieStats({
158 videoId: vodVideoId,
159 metric: 'aggregateWatchTime',
160 startDate: twoHoursAgo,
161 endDate: now
162 })
163
164 expect(result.groupInterval).to.equal('ten_minutes')
165 expect(result.data).to.have.length.above(6).and.below(18)
166
167 expectInterval(result, 60 * 10 * 1000)
168 expectTodayLastValue(result, 9)
169 })
170
171 it('Should automatically group by one minute', async function () {
172 const now = new Date()
173 const thirtyAgo = new Date()
174 thirtyAgo.setMinutes(thirtyAgo.getMinutes() - 30)
175
176 const result = await servers[0].videoStats.getTimeserieStats({
177 videoId: vodVideoId,
178 metric: 'aggregateWatchTime',
179 startDate: thirtyAgo,
180 endDate: now
181 })
182
183 expect(result.groupInterval).to.equal('one_minute')
184 expect(result.data).to.have.length.above(20).and.below(40)
185
186 expectInterval(result, 60 * 1000)
187 expectTodayLastValue(result, 9)
188 })
189
101 after(async function () { 190 after(async function () {
102 await stopFfmpeg(command) 191 await stopFfmpeg(command)
103 }) 192 })