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

68 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05: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 09:26:25 -05:00
2022-04-05 08:15:09 -05:00
/**
*
* Aggregate views of all videos federated with our instance
* Mainly used by the trending/hot algorithms
*
*/
2018-08-29 09:26:25 -05:00
@Table({
tableName: 'videoView',
2019-04-26 02:16:43 -05:00
updatedAt: false,
2018-08-29 09:26:25 -05:00
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'startDate' ]
2018-08-29 09:26:25 -05:00
}
]
})
2021-05-12 07:09:04 -05:00
export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> {
2018-08-29 09:26:25 -05:00
@CreatedAt
createdAt: Date
@AllowNull(false)
2021-08-27 07:32:44 -05:00
@Column(DataType.DATE)
2018-08-29 09:26:25 -05:00
startDate: Date
@AllowNull(false)
2021-08-27 07:32:44 -05:00
@Column(DataType.DATE)
2018-08-29 09:26:25 -05: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 10:33:36 -05:00
static removeOldRemoteViewsHistory (beforeDate: string) {
const query = {
where: {
startDate: {
2021-08-27 07:32:44 -05:00
[Op.lt]: beforeDate
2019-04-11 10:33:36 -05:00
},
videoId: {
2021-08-27 07:32:44 -05:00
[Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
2019-04-11 10:33:36 -05:00
}
}
}
return VideoViewModel.destroy(query)
}
2018-08-29 09:26:25 -05:00
}