PeerTube/server/models/view/video-view.ts

68 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import { literal, Op } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'
import { AttributesOnly } from '@shared/typescript-utils'
import { VideoModel } from '../video/video'
2018-08-29 16:26:25 +02:00
2022-04-05 15:15:09 +02:00
/**
*
* Aggregate views of all videos federated with our instance
* Mainly used by the trending/hot algorithms
*
*/
2018-08-29 16:26:25 +02:00
@Table({
tableName: 'videoView',
2019-04-26 09:16:43 +02:00
updatedAt: false,
2018-08-29 16:26:25 +02:00
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'startDate' ]
2018-08-29 16:26:25 +02:00
}
]
})
2021-05-12 14:09:04 +02:00
export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> {
2018-08-29 16:26:25 +02:00
@CreatedAt
createdAt: Date
@AllowNull(false)
2021-08-27 14:32:44 +02:00
@Column(DataType.DATE)
2018-08-29 16:26:25 +02:00
startDate: Date
@AllowNull(false)
2021-08-27 14:32:44 +02:00
@Column(DataType.DATE)
2018-08-29 16:26:25 +02:00
endDate: Date
@AllowNull(false)
@Column
views: number
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: VideoModel
2019-04-11 17:33:36 +02:00
static removeOldRemoteViewsHistory (beforeDate: string) {
const query = {
where: {
startDate: {
2021-08-27 14:32:44 +02:00
[Op.lt]: beforeDate
2019-04-11 17:33:36 +02:00
},
videoId: {
2021-08-27 14:32:44 +02:00
[Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
2019-04-11 17:33:36 +02:00
}
}
}
return VideoViewModel.destroy(query)
}
2018-08-29 16:26:25 +02:00
}