]>
Commit | Line | Data |
---|---|---|
69818c93 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
e02643f3 | 3 | import { database as db } from '../../initializers/database' |
65fcc311 C |
4 | import { BaseRequestScheduler } from './base-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 | ||
69818c93 C |
11 | export type RequestVideoEventSchedulerOptions = { |
12 | type: string | |
13 | videoId: string | |
14 | count?: number | |
15 | transaction?: Sequelize.Transaction | |
16 | } | |
17 | ||
65fcc311 | 18 | class RequestVideoEventScheduler extends BaseRequestScheduler { |
e4c87ec2 C |
19 | constructor () { |
20 | super() | |
21 | ||
22 | // We limit the size of the requests | |
65fcc311 C |
23 | this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS |
24 | this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD | |
e4c87ec2 C |
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 | ||
69818c93 | 37 | buildRequestObjects (eventsToProcess: { [ toPodId: number ]: any }[]) { |
e4c87ec2 C |
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, | |
65fcc311 | 59 | endpoint: REQUEST_VIDEO_EVENT_ENDPOINT, |
e4c87ec2 C |
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 | ||
69818c93 | 99 | createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) { |
e4c87ec2 C |
100 | if (count === undefined) count = 1 |
101 | ||
69818c93 | 102 | const dbRequestOptions: Sequelize.CreateOptions = {} |
e4c87ec2 C |
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 | } | |
65fcc311 C |
114 | |
115 | // --------------------------------------------------------------------------- | |
116 | ||
117 | export { | |
118 | RequestVideoEventScheduler | |
119 | } |