1 import * as Sequelize from 'sequelize'
3 import { database as db } from '../../initializers/database'
4 import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
6 REQUESTS_VIDEO_EVENT_LIMIT_PODS,
7 REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
8 REQUEST_VIDEO_EVENT_ENDPOINT
9 } from '../../initializers'
10 import { RequestsVideoEventGrouped } from '../../models'
11 import { RequestVideoEventType, RemoteVideoEventRequest, RemoteVideoEventType } from '../../../shared'
13 export type RequestVideoEventSchedulerOptions = {
14 type: RequestVideoEventType
17 transaction?: Sequelize.Transaction
20 class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoEventGrouped> {
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
28 this.description = 'video event requests'
32 return db.RequestVideoEvent
35 getRequestToPodModel () {
36 return db.RequestVideoEvent
39 buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) {
40 const requestsToMakeGrouped: RequestsObjects<RemoteVideoEventRequest> = {}
45 video1: { views: 4, likes: 5 },
50 const eventsPerVideoPerPod: {
52 [ videoRemoteId: string ]: {
60 // We group video events per video and per pod
61 // We add the counts of the same event types
62 Object.keys(eventRequests).forEach(toPodId => {
63 eventRequests[toPodId].forEach(eventToProcess => {
64 if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
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
74 requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
76 const eventsPerVideo = eventsPerVideoPerPod[toPodId]
77 const remoteId = eventToProcess.video.remoteId
78 if (!eventsPerVideo[remoteId]) eventsPerVideo[remoteId] = {}
80 const events = eventsPerVideo[remoteId]
81 if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
83 events[eventToProcess.type] += eventToProcess.count
87 // Now we build our requests array per pod
88 Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
89 const eventsForPod = eventsPerVideoPerPod[toPodId]
91 Object.keys(eventsForPod).forEach(remoteId => {
92 const eventsForVideo = eventsForPod[remoteId]
94 Object.keys(eventsForVideo).forEach(eventType => {
95 requestsToMakeGrouped[toPodId].datas.push({
98 eventType: eventType as RemoteVideoEventType,
99 count: +eventsForVideo[eventType]
106 return requestsToMakeGrouped
109 createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) {
110 if (count === undefined) count = 1
112 const dbRequestOptions: Sequelize.CreateOptions = {}
113 if (transaction) dbRequestOptions.transaction = transaction
115 const createQuery = {
121 return db.RequestVideoEvent.create(createQuery, dbRequestOptions)
125 // ---------------------------------------------------------------------------
128 RequestVideoEventScheduler