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 {
12 // We limit the size of the requests
13 this.limitPods = constants.REQUESTS_VIDEO_EVENT_LIMIT_PODS
14 this.limitPerPod = constants.REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
16 this.description = 'video event requests'
20 return db.RequestVideoEvent
23 getRequestToPodModel () {
24 return db.RequestVideoEvent
27 buildRequestObjects (eventsToProcess) {
28 const requestsToMakeGrouped = {}
33 video1: { views: 4, likes: 5 },
38 const eventsPerVideoPerPod = {}
40 // We group video events per video and per pod
41 // We add the counts of the same event types
42 Object.keys(eventsToProcess).forEach(toPodId => {
43 eventsToProcess[toPodId].forEach(eventToProcess => {
44 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
46 if (!requestsToMakeGrouped[toPodId]) {
47 requestsToMakeGrouped[toPodId] = {
48 toPod: eventToProcess.pod,
49 endpoint: constants.REQUEST_ENDPOINTS.EVENT,
50 ids: [], // request ids, to delete them from the DB in the future
51 datas: [] // requests data
54 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
56 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
57 const remoteId = eventToProcess.video.remoteId
58 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
60 const events = eventsPerVideo[remoteId]
61 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
63 events[eventToProcess.type] += eventToProcess.count
67 // Now we build our requests array per pod
68 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
69 const eventsForPod = eventsPerVideoPerPod[toPodId]
71 Object.keys(eventsForPod).forEach(remoteId => {
72 const eventsForVideo = eventsForPod[remoteId]
74 Object.keys(eventsForVideo).forEach(eventType => {
75 requestsToMakeGrouped[toPodId].datas.push({
79 count: eventsForVideo[eventType]
86 return requestsToMakeGrouped
89 // { type, videoId, count?, transaction? }
90 createRequest (options, callback) {
91 const type = options.type
92 const videoId = options.videoId
93 const transaction = options.transaction
94 let count = options.count
96 if (count === undefined) count = 1
98 const dbRequestOptions = {}
99 if (transaction) dbRequestOptions.transaction = transaction
101 const createQuery = {
107 return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)