aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/request/request-video-event-scheduler.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/request/request-video-event-scheduler.js')
-rw-r--r--server/lib/request/request-video-event-scheduler.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/server/lib/request/request-video-event-scheduler.js b/server/lib/request/request-video-event-scheduler.js
new file mode 100644
index 000000000..e54d34f4a
--- /dev/null
+++ b/server/lib/request/request-video-event-scheduler.js
@@ -0,0 +1,108 @@
1'use strict'
2
3const BaseRequestScheduler = require('./base-request-scheduler')
4const constants = require('../../initializers/constants')
5const db = require('../../initializers/database')
6
7module.exports = class RequestVideoEventScheduler extends BaseRequestScheduler {
8 constructor () {
9 super()
10
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
14
15 this.description = 'video event requests'
16 }
17
18 getRequestModel () {
19 return db.RequestVideoEvent
20 }
21
22 getRequestToPodModel () {
23 return db.RequestVideoEvent
24 }
25
26 buildRequestObjects (eventsToProcess) {
27 const requestsToMakeGrouped = {}
28
29 /* Example:
30 {
31 pod1: {
32 video1: { views: 4, likes: 5 },
33 video2: { likes: 5 }
34 }
35 }
36 */
37 const eventsPerVideoPerPod = {}
38
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] = {}
44
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
51 }
52 }
53 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
54
55 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
56 const remoteId = eventToProcess.video.remoteId
57 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
58
59 const events = eventsPerVideo[remoteId]
60 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
61
62 events[eventToProcess.type] += eventToProcess.count
63 })
64 })
65
66 // Now we build our requests array per pod
67 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
68 const eventsForPod = eventsPerVideoPerPod[toPodId]
69
70 Object.keys(eventsForPod).forEach(remoteId => {
71 const eventsForVideo = eventsForPod[remoteId]
72
73 Object.keys(eventsForVideo).forEach(eventType => {
74 requestsToMakeGrouped[toPodId].datas.push({
75 data: {
76 remoteId,
77 eventType,
78 count: eventsForVideo[eventType]
79 }
80 })
81 })
82 })
83 })
84
85 return requestsToMakeGrouped
86 }
87
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
94
95 if (count === undefined) count = 1
96
97 const dbRequestOptions = {}
98 if (transaction) dbRequestOptions.transaction = transaction
99
100 const createQuery = {
101 type,
102 count,
103 videoId
104 }
105
106 return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)
107 }
108}