aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/request/request-video-event-scheduler.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-10 17:27:49 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit571389d43b8fc8aaf27e77c06f19b320b08dbbc9 (patch)
treee57173bcd0590d939c28952a29258fd02a281e35 /server/lib/request/request-video-event-scheduler.ts
parent38fa2065831b5f55be0d7f30f19a62c967397208 (diff)
downloadPeerTube-571389d43b8fc8aaf27e77c06f19b320b08dbbc9.tar.gz
PeerTube-571389d43b8fc8aaf27e77c06f19b320b08dbbc9.tar.zst
PeerTube-571389d43b8fc8aaf27e77c06f19b320b08dbbc9.zip
Make it compile at least
Diffstat (limited to 'server/lib/request/request-video-event-scheduler.ts')
-rw-r--r--server/lib/request/request-video-event-scheduler.ts129
1 files changed, 0 insertions, 129 deletions
diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts
deleted file mode 100644
index 5f21287f0..000000000
--- a/server/lib/request/request-video-event-scheduler.ts
+++ /dev/null
@@ -1,129 +0,0 @@
1import * as Sequelize from 'sequelize'
2
3import { database as db } from '../../initializers/database'
4import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
5import {
6 REQUESTS_VIDEO_EVENT_LIMIT_PODS,
7 REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
8 REQUEST_VIDEO_EVENT_ENDPOINT
9} from '../../initializers'
10import { RequestsVideoEventGrouped } from '../../models'
11import { RequestVideoEventType, RemoteVideoEventRequest, RemoteVideoEventType } from '../../../shared'
12
13export type RequestVideoEventSchedulerOptions = {
14 type: RequestVideoEventType
15 videoId: number
16 count?: number
17 transaction?: Sequelize.Transaction
18}
19
20class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoEventGrouped> {
21 constructor () {
22 super()
23
24 // We limit the size of the requests
25 this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS
26 this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
27
28 this.description = 'video event requests'
29 }
30
31 getRequestModel () {
32 return db.RequestVideoEvent
33 }
34
35 getRequestToPodModel () {
36 return db.RequestVideoEvent
37 }
38
39 buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) {
40 const requestsToMakeGrouped: RequestsObjects<RemoteVideoEventRequest> = {}
41
42 /* Example:
43 {
44 pod1: {
45 video1: { views: 4, likes: 5 },
46 video2: { likes: 5 }
47 }
48 }
49 */
50 const eventsPerVideoPerPod: {
51 [ podId: string ]: {
52 [ videoUUID: string ]: {
53 views?: number
54 likes?: number
55 dislikes?: number
56 }
57 }
58 } = {}
59
60 // We group video events per video and per pod
61 // We add the counts of the same event types
62 for (const toPodId of Object.keys(eventRequests)) {
63 for (const eventToProcess of eventRequests[toPodId]) {
64 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
65
66 if (!requestsToMakeGrouped[toPodId]) {
67 requestsToMakeGrouped[toPodId] = {
68 toPod: eventToProcess.pod,
69 endpoint: REQUEST_VIDEO_EVENT_ENDPOINT,
70 ids: [], // request ids, to delete them from the DB in the future
71 datas: [] // requests data
72 }
73 }
74 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
75
76 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
77 const uuid = eventToProcess.video.uuid
78 if (!eventsPerVideo[uuid]) eventsPerVideo[uuid] = {}
79
80 const events = eventsPerVideo[uuid]
81 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
82
83 events[eventToProcess.type] += eventToProcess.count
84 }
85 }
86
87 // Now we build our requests array per pod
88 for (const toPodId of Object.keys(eventsPerVideoPerPod)) {
89 const eventsForPod = eventsPerVideoPerPod[toPodId]
90
91 for (const uuid of Object.keys(eventsForPod)) {
92 const eventsForVideo = eventsForPod[uuid]
93
94 for (const eventType of Object.keys(eventsForVideo)) {
95 requestsToMakeGrouped[toPodId].datas.push({
96 data: {
97 uuid,
98 eventType: eventType as RemoteVideoEventType,
99 count: +eventsForVideo[eventType]
100 }
101 })
102 }
103 }
104 }
105
106 return requestsToMakeGrouped
107 }
108
109 createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) {
110 if (count === undefined) count = 1
111
112 const dbRequestOptions: Sequelize.CreateOptions = {}
113 if (transaction) dbRequestOptions.transaction = transaction
114
115 const createQuery = {
116 type,
117 count,
118 videoId
119 }
120
121 return db.RequestVideoEvent.create(createQuery, dbRequestOptions)
122 }
123}
124
125// ---------------------------------------------------------------------------
126
127export {
128 RequestVideoEventScheduler
129}