]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/request/request-video-event-scheduler.ts
BaseRequestScheduler -> AbstractRequestScheduler
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-video-event-scheduler.ts
1 import * as Sequelize from 'sequelize'
2
3 import { database as db } from '../../initializers/database'
4 import { AbstractRequestScheduler } from './abstract-request-scheduler'
5 import {
6 REQUESTS_VIDEO_EVENT_LIMIT_PODS,
7 REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
8 REQUEST_VIDEO_EVENT_ENDPOINT
9 } from '../../initializers'
10
11 export type RequestVideoEventSchedulerOptions = {
12 type: string
13 videoId: string
14 count?: number
15 transaction?: Sequelize.Transaction
16 }
17
18 class RequestVideoEventScheduler extends AbstractRequestScheduler {
19 constructor () {
20 super()
21
22 // We limit the size of the requests
23 this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS
24 this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
25
26 this.description = 'video event requests'
27 }
28
29 getRequestModel () {
30 return db.RequestVideoEvent
31 }
32
33 getRequestToPodModel () {
34 return db.RequestVideoEvent
35 }
36
37 buildRequestObjects (eventsToProcess: { [ toPodId: number ]: any }[]) {
38 const requestsToMakeGrouped = {}
39
40 /* Example:
41 {
42 pod1: {
43 video1: { views: 4, likes: 5 },
44 video2: { likes: 5 }
45 }
46 }
47 */
48 const eventsPerVideoPerPod = {}
49
50 // We group video events per video and per pod
51 // We add the counts of the same event types
52 Object.keys(eventsToProcess).forEach(toPodId => {
53 eventsToProcess[toPodId].forEach(eventToProcess => {
54 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
55
56 if (!requestsToMakeGrouped[toPodId]) {
57 requestsToMakeGrouped[toPodId] = {
58 toPod: eventToProcess.pod,
59 endpoint: REQUEST_VIDEO_EVENT_ENDPOINT,
60 ids: [], // request ids, to delete them from the DB in the future
61 datas: [] // requests data
62 }
63 }
64 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
65
66 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
67 const remoteId = eventToProcess.video.remoteId
68 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
69
70 const events = eventsPerVideo[remoteId]
71 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
72
73 events[eventToProcess.type] += eventToProcess.count
74 })
75 })
76
77 // Now we build our requests array per pod
78 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
79 const eventsForPod = eventsPerVideoPerPod[toPodId]
80
81 Object.keys(eventsForPod).forEach(remoteId => {
82 const eventsForVideo = eventsForPod[remoteId]
83
84 Object.keys(eventsForVideo).forEach(eventType => {
85 requestsToMakeGrouped[toPodId].datas.push({
86 data: {
87 remoteId,
88 eventType,
89 count: eventsForVideo[eventType]
90 }
91 })
92 })
93 })
94 })
95
96 return requestsToMakeGrouped
97 }
98
99 createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) {
100 if (count === undefined) count = 1
101
102 const dbRequestOptions: Sequelize.CreateOptions = {}
103 if (transaction) dbRequestOptions.transaction = transaction
104
105 const createQuery = {
106 type,
107 count,
108 videoId
109 }
110
111 return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)
112 }
113 }
114
115 // ---------------------------------------------------------------------------
116
117 export {
118 RequestVideoEventScheduler
119 }