aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/request-video-event-scheduler.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-05 17:35:58 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-05 17:35:58 +0200
commit5a976a8c351d2a9e23ceee1e193fca2893b7f12d (patch)
tree9e22c1cf835b68082bc3a12adf6a0a7b40e6ce7f /server/lib/request-video-event-scheduler.js
parent4e284e97b95d1fe93378b90061272e3abc875078 (diff)
downloadPeerTube-5a976a8c351d2a9e23ceee1e193fca2893b7f12d.tar.gz
PeerTube-5a976a8c351d2a9e23ceee1e193fca2893b7f12d.tar.zst
PeerTube-5a976a8c351d2a9e23ceee1e193fca2893b7f12d.zip
Server: move requests lib in their own directory
Diffstat (limited to 'server/lib/request-video-event-scheduler.js')
-rw-r--r--server/lib/request-video-event-scheduler.js108
1 files changed, 0 insertions, 108 deletions
diff --git a/server/lib/request-video-event-scheduler.js b/server/lib/request-video-event-scheduler.js
deleted file mode 100644
index c7631253a..000000000
--- a/server/lib/request-video-event-scheduler.js
+++ /dev/null
@@ -1,108 +0,0 @@
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}