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