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