]>
Commit | Line | Data |
---|---|---|
41fb13c3 C |
1 | import { literal, Op } from 'sequelize' |
2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript' | |
6b5f72be | 3 | import { AttributesOnly } from '@shared/typescript-utils' |
b2111066 | 4 | import { VideoModel } from '../video/video' |
6b616860 | 5 | |
dfbcefc2 C |
6 | /** |
7 | * | |
8 | * Aggregate views of all videos federated with our instance | |
9 | * Mainly used by the trending/hot algorithms | |
10 | * | |
11 | */ | |
12 | ||
6b616860 C |
13 | @Table({ |
14 | tableName: 'videoView', | |
4f0f2ab2 | 15 | updatedAt: false, |
6b616860 C |
16 | indexes: [ |
17 | { | |
18 | fields: [ 'videoId' ] | |
0926af7a C |
19 | }, |
20 | { | |
21 | fields: [ 'startDate' ] | |
6b616860 C |
22 | } |
23 | ] | |
24 | }) | |
16c016e8 | 25 | export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> { |
6b616860 C |
26 | @CreatedAt |
27 | createdAt: Date | |
28 | ||
29 | @AllowNull(false) | |
41fb13c3 | 30 | @Column(DataType.DATE) |
6b616860 C |
31 | startDate: Date |
32 | ||
33 | @AllowNull(false) | |
41fb13c3 | 34 | @Column(DataType.DATE) |
6b616860 C |
35 | endDate: Date |
36 | ||
37 | @AllowNull(false) | |
38 | @Column | |
39 | views: number | |
40 | ||
41 | @ForeignKey(() => VideoModel) | |
42 | @Column | |
43 | videoId: number | |
44 | ||
45 | @BelongsTo(() => VideoModel, { | |
46 | foreignKey: { | |
47 | allowNull: false | |
48 | }, | |
49 | onDelete: 'CASCADE' | |
50 | }) | |
51 | Video: VideoModel | |
52 | ||
cda03765 C |
53 | static removeOldRemoteViewsHistory (beforeDate: string) { |
54 | const query = { | |
55 | where: { | |
56 | startDate: { | |
41fb13c3 | 57 | [Op.lt]: beforeDate |
cda03765 C |
58 | }, |
59 | videoId: { | |
41fb13c3 | 60 | [Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)') |
cda03765 C |
61 | } |
62 | } | |
63 | } | |
64 | ||
65 | return VideoViewModel.destroy(query) | |
66 | } | |
6b616860 | 67 | } |