ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
1.3 KiB

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