]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/request/request-video-event-scheduler.ts
Fix upgrade peertube script (bad semver comparison)
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-video-event-scheduler.ts
CommitLineData
69818c93
C
1import * as Sequelize from 'sequelize'
2
e02643f3 3import { database as db } from '../../initializers/database'
15a30294 4import { AbstractRequestScheduler } from './abstract-request-scheduler'
65fcc311
C
5import {
6 REQUESTS_VIDEO_EVENT_LIMIT_PODS,
7 REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
8 REQUEST_VIDEO_EVENT_ENDPOINT
9} from '../../initializers'
6fcd19ba 10import { RequestsVideoEventGrouped } from '../../models'
ee9e7b61 11import { RequestVideoEventType } from '../../../shared'
65fcc311 12
69818c93 13export type RequestVideoEventSchedulerOptions = {
ee9e7b61 14 type: RequestVideoEventType
69818c93
C
15 videoId: string
16 count?: number
17 transaction?: Sequelize.Transaction
18}
19
6fcd19ba 20class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoEventGrouped> {
e4c87ec2
C
21 constructor () {
22 super()
23
24 // We limit the size of the requests
65fcc311
C
25 this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS
26 this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
e4c87ec2
C
27
28 this.description = 'video event requests'
29 }
30
31 getRequestModel () {
32 return db.RequestVideoEvent
33 }
34
35 getRequestToPodModel () {
36 return db.RequestVideoEvent
37 }
38
6fcd19ba 39 buildRequestObjects (eventRequests: RequestsVideoEventGrouped) {
e4c87ec2
C
40 const requestsToMakeGrouped = {}
41
42 /* Example:
43 {
44 pod1: {
45 video1: { views: 4, likes: 5 },
46 video2: { likes: 5 }
47 }
48 }
49 */
50 const eventsPerVideoPerPod = {}
51
52 // We group video events per video and per pod
53 // We add the counts of the same event types
6fcd19ba
C
54 Object.keys(eventRequests).forEach(toPodId => {
55 eventRequests[toPodId].forEach(eventToProcess => {
e4c87ec2
C
56 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
57
58 if (!requestsToMakeGrouped[toPodId]) {
59 requestsToMakeGrouped[toPodId] = {
60 toPod: eventToProcess.pod,
65fcc311 61 endpoint: REQUEST_VIDEO_EVENT_ENDPOINT,
e4c87ec2
C
62 ids: [], // request ids, to delete them from the DB in the future
63 datas: [] // requests data
64 }
65 }
66 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
67
68 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
69 const remoteId = eventToProcess.video.remoteId
70 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
71
72 const events = eventsPerVideo[remoteId]
73 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
74
75 events[eventToProcess.type] += eventToProcess.count
76 })
77 })
78
79 // Now we build our requests array per pod
80 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
81 const eventsForPod = eventsPerVideoPerPod[toPodId]
82
83 Object.keys(eventsForPod).forEach(remoteId => {
84 const eventsForVideo = eventsForPod[remoteId]
85
86 Object.keys(eventsForVideo).forEach(eventType => {
87 requestsToMakeGrouped[toPodId].datas.push({
88 data: {
89 remoteId,
90 eventType,
91 count: eventsForVideo[eventType]
92 }
93 })
94 })
95 })
96 })
97
98 return requestsToMakeGrouped
99 }
100
6fcd19ba 101 createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) {
e4c87ec2
C
102 if (count === undefined) count = 1
103
69818c93 104 const dbRequestOptions: Sequelize.CreateOptions = {}
e4c87ec2
C
105 if (transaction) dbRequestOptions.transaction = transaction
106
107 const createQuery = {
108 type,
109 count,
110 videoId
111 }
112
6fcd19ba 113 return db.RequestVideoEvent.create(createQuery, dbRequestOptions)
e4c87ec2
C
114 }
115}
65fcc311
C
116
117// ---------------------------------------------------------------------------
118
119export {
120 RequestVideoEventScheduler
121}