aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/request/request-video-event-scheduler.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/lib/request/request-video-event-scheduler.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/lib/request/request-video-event-scheduler.ts')
-rw-r--r--server/lib/request/request-video-event-scheduler.ts116
1 files changed, 116 insertions, 0 deletions
diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts
new file mode 100644
index 000000000..6e5306c7d
--- /dev/null
+++ b/server/lib/request/request-video-event-scheduler.ts
@@ -0,0 +1,116 @@
1const db = require('../../initializers/database')
2import { BaseRequestScheduler } from './base-request-scheduler'
3import {
4 REQUESTS_VIDEO_EVENT_LIMIT_PODS,
5 REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
6 REQUEST_VIDEO_EVENT_ENDPOINT
7} from '../../initializers'
8
9class RequestVideoEventScheduler extends BaseRequestScheduler {
10 constructor () {
11 super()
12
13 // We limit the size of the requests
14 this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS
15 this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
16
17 this.description = 'video event requests'
18 }
19
20 getRequestModel () {
21 return db.RequestVideoEvent
22 }
23
24 getRequestToPodModel () {
25 return db.RequestVideoEvent
26 }
27
28 buildRequestObjects (eventsToProcess) {
29 const requestsToMakeGrouped = {}
30
31 /* Example:
32 {
33 pod1: {
34 video1: { views: 4, likes: 5 },
35 video2: { likes: 5 }
36 }
37 }
38 */
39 const eventsPerVideoPerPod = {}
40
41 // We group video events per video and per pod
42 // We add the counts of the same event types
43 Object.keys(eventsToProcess).forEach(toPodId => {
44 eventsToProcess[toPodId].forEach(eventToProcess => {
45 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
46
47 if (!requestsToMakeGrouped[toPodId]) {
48 requestsToMakeGrouped[toPodId] = {
49 toPod: eventToProcess.pod,
50 endpoint: REQUEST_VIDEO_EVENT_ENDPOINT,
51 ids: [], // request ids, to delete them from the DB in the future
52 datas: [] // requests data
53 }
54 }
55 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
56
57 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
58 const remoteId = eventToProcess.video.remoteId
59 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
60
61 const events = eventsPerVideo[remoteId]
62 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
63
64 events[eventToProcess.type] += eventToProcess.count
65 })
66 })
67
68 // Now we build our requests array per pod
69 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
70 const eventsForPod = eventsPerVideoPerPod[toPodId]
71
72 Object.keys(eventsForPod).forEach(remoteId => {
73 const eventsForVideo = eventsForPod[remoteId]
74
75 Object.keys(eventsForVideo).forEach(eventType => {
76 requestsToMakeGrouped[toPodId].datas.push({
77 data: {
78 remoteId,
79 eventType,
80 count: eventsForVideo[eventType]
81 }
82 })
83 })
84 })
85 })
86
87 return requestsToMakeGrouped
88 }
89
90 // { type, videoId, count?, transaction? }
91 createRequest (options, callback) {
92 const type = options.type
93 const videoId = options.videoId
94 const transaction = options.transaction
95 let count = options.count
96
97 if (count === undefined) count = 1
98
99 const dbRequestOptions: { transaction?: any } = {}
100 if (transaction) dbRequestOptions.transaction = transaction
101
102 const createQuery = {
103 type,
104 count,
105 videoId
106 }
107
108 return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)
109 }
110}
111
112// ---------------------------------------------------------------------------
113
114export {
115 RequestVideoEventScheduler
116}