3 const BaseRequestScheduler
= require('./base-request-scheduler')
4 const constants
= require('../../initializers/constants')
5 const db
= require('../../initializers/database')
7 module
.exports
= class RequestVideoEventScheduler
extends BaseRequestScheduler
{
11 // We limit the size of the requests
12 this.limitPods
= constants
.REQUESTS_VIDEO_EVENT_LIMIT_PODS
13 this.limitPerPod
= constants
.REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
15 this.description
= 'video event requests'
19 return db
.RequestVideoEvent
22 getRequestToPodModel () {
23 return db
.RequestVideoEvent
26 buildRequestObjects (eventsToProcess
) {
27 const requestsToMakeGrouped
= {}
32 video1: { views: 4, likes: 5 },
37 const eventsPerVideoPerPod
= {}
39 // We group video events per video and per pod
40 // We add the counts of the same event types
41 Object
.keys(eventsToProcess
).forEach(toPodId
=> {
42 eventsToProcess
[toPodId
].forEach(eventToProcess
=> {
43 if (!eventsPerVideoPerPod
[toPodId
]) eventsPerVideoPerPod
[toPodId
] = {}
45 if (!requestsToMakeGrouped
[toPodId
]) {
46 requestsToMakeGrouped
[toPodId
] = {
47 toPod: eventToProcess
.pod
,
48 endpoint: constants
.REQUEST_VIDEO_EVENT_ENDPOINT
,
49 ids: [], // request ids, to delete them from the DB in the future
50 datas: [] // requests data
53 requestsToMakeGrouped
[toPodId
].ids
.push(eventToProcess
.id
)
55 const eventsPerVideo
= eventsPerVideoPerPod
[toPodId
]
56 const remoteId
= eventToProcess
.video
.remoteId
57 if (!eventsPerVideo
[remoteId
]) eventsPerVideo
[remoteId
] = {}
59 const events
= eventsPerVideo
[remoteId
]
60 if (!events
[eventToProcess
.type
]) events
[eventToProcess
.type
] = 0
62 events
[eventToProcess
.type
] += eventToProcess
.count
66 // Now we build our requests array per pod
67 Object
.keys(eventsPerVideoPerPod
).forEach(toPodId
=> {
68 const eventsForPod
= eventsPerVideoPerPod
[toPodId
]
70 Object
.keys(eventsForPod
).forEach(remoteId
=> {
71 const eventsForVideo
= eventsForPod
[remoteId
]
73 Object
.keys(eventsForVideo
).forEach(eventType
=> {
74 requestsToMakeGrouped
[toPodId
].datas
.push({
78 count: eventsForVideo
[eventType
]
85 return requestsToMakeGrouped
88 // { type, videoId, count?, transaction? }
89 createRequest (options
, callback
) {
90 const type
= options
.type
91 const videoId
= options
.videoId
92 const transaction
= options
.transaction
93 let count
= options
.count
95 if (count
=== undefined) count
= 1
97 const dbRequestOptions
= {}
98 if (transaction
) dbRequestOptions
.transaction
= transaction
100 const createQuery
= {
106 return db
.RequestVideoEvent
.create(createQuery
, dbRequestOptions
).asCallback(callback
)