aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/view/video-view.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/models/view/video-view.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/models/view/video-view.ts')
-rw-r--r--server/models/view/video-view.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/server/models/view/video-view.ts b/server/models/view/video-view.ts
new file mode 100644
index 000000000..df462e631
--- /dev/null
+++ b/server/models/view/video-view.ts
@@ -0,0 +1,60 @@
1import { literal, Op } from 'sequelize'
2import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'
3import { AttributesOnly } from '@shared/typescript-utils'
4import { VideoModel } from '../video/video'
5
6@Table({
7 tableName: 'videoView',
8 updatedAt: false,
9 indexes: [
10 {
11 fields: [ 'videoId' ]
12 },
13 {
14 fields: [ 'startDate' ]
15 }
16 ]
17})
18export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> {
19 @CreatedAt
20 createdAt: Date
21
22 @AllowNull(false)
23 @Column(DataType.DATE)
24 startDate: Date
25
26 @AllowNull(false)
27 @Column(DataType.DATE)
28 endDate: Date
29
30 @AllowNull(false)
31 @Column
32 views: number
33
34 @ForeignKey(() => VideoModel)
35 @Column
36 videoId: number
37
38 @BelongsTo(() => VideoModel, {
39 foreignKey: {
40 allowNull: false
41 },
42 onDelete: 'CASCADE'
43 })
44 Video: VideoModel
45
46 static removeOldRemoteViewsHistory (beforeDate: string) {
47 const query = {
48 where: {
49 startDate: {
50 [Op.lt]: beforeDate
51 },
52 videoId: {
53 [Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
54 }
55 }
56 }
57
58 return VideoViewModel.destroy(query)
59 }
60}