2017-06-10 15:15:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-07-10 12:43:21 -05:00
|
|
|
import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
REQUESTS_VIDEO_EVENT_LIMIT_PODS,
|
|
|
|
REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
|
|
|
|
REQUEST_VIDEO_EVENT_ENDPOINT
|
|
|
|
} from '../../initializers'
|
2017-07-05 06:26:25 -05:00
|
|
|
import { RequestsVideoEventGrouped } from '../../models'
|
2017-07-10 12:43:21 -05:00
|
|
|
import { RequestVideoEventType, RemoteVideoEventRequest, RemoteVideoEventType } from '../../../shared'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
export type RequestVideoEventSchedulerOptions = {
|
2017-06-16 03:36:18 -05:00
|
|
|
type: RequestVideoEventType
|
2017-07-11 09:01:56 -05:00
|
|
|
videoId: number
|
2017-06-10 15:15:25 -05:00
|
|
|
count?: number
|
|
|
|
transaction?: Sequelize.Transaction
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoEventGrouped> {
|
2017-02-26 11:57:33 -06:00
|
|
|
constructor () {
|
|
|
|
super()
|
|
|
|
|
|
|
|
// We limit the size of the requests
|
2017-05-15 15:22:03 -05:00
|
|
|
this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS
|
|
|
|
this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD
|
2017-02-26 11:57:33 -06:00
|
|
|
|
|
|
|
this.description = 'video event requests'
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequestModel () {
|
|
|
|
return db.RequestVideoEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequestToPodModel () {
|
|
|
|
return db.RequestVideoEvent
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:43:21 -05:00
|
|
|
buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) {
|
|
|
|
const requestsToMakeGrouped: RequestsObjects<RemoteVideoEventRequest> = {}
|
2017-02-26 11:57:33 -06:00
|
|
|
|
|
|
|
/* Example:
|
|
|
|
{
|
|
|
|
pod1: {
|
|
|
|
video1: { views: 4, likes: 5 },
|
|
|
|
video2: { likes: 5 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2017-07-10 12:43:21 -05:00
|
|
|
const eventsPerVideoPerPod: {
|
|
|
|
[ podId: string ]: {
|
2017-07-11 09:01:56 -05:00
|
|
|
[ videoUUID: string ]: {
|
2017-07-10 12:43:21 -05:00
|
|
|
views?: number
|
|
|
|
likes?: number
|
|
|
|
dislikes?: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = {}
|
2017-02-26 11:57:33 -06:00
|
|
|
|
|
|
|
// We group video events per video and per pod
|
|
|
|
// We add the counts of the same event types
|
2017-07-05 06:26:25 -05:00
|
|
|
Object.keys(eventRequests).forEach(toPodId => {
|
|
|
|
eventRequests[toPodId].forEach(eventToProcess => {
|
2017-02-26 11:57:33 -06:00
|
|
|
if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
|
|
|
|
|
|
|
|
if (!requestsToMakeGrouped[toPodId]) {
|
|
|
|
requestsToMakeGrouped[toPodId] = {
|
|
|
|
toPod: eventToProcess.pod,
|
2017-05-15 15:22:03 -05:00
|
|
|
endpoint: REQUEST_VIDEO_EVENT_ENDPOINT,
|
2017-02-26 11:57:33 -06:00
|
|
|
ids: [], // request ids, to delete them from the DB in the future
|
|
|
|
datas: [] // requests data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
requestsToMakeGrouped[toPodId].ids.push(eventToProcess.id)
|
|
|
|
|
|
|
|
const eventsPerVideo = eventsPerVideoPerPod[toPodId]
|
2017-07-11 09:01:56 -05:00
|
|
|
const uuid = eventToProcess.video.uuid
|
|
|
|
if (!eventsPerVideo[uuid]) eventsPerVideo[uuid] = {}
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-11 09:01:56 -05:00
|
|
|
const events = eventsPerVideo[uuid]
|
2017-02-26 11:57:33 -06:00
|
|
|
if (!events[eventToProcess.type]) events[eventToProcess.type] = 0
|
|
|
|
|
|
|
|
events[eventToProcess.type] += eventToProcess.count
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now we build our requests array per pod
|
|
|
|
Object.keys(eventsPerVideoPerPod).forEach(toPodId => {
|
|
|
|
const eventsForPod = eventsPerVideoPerPod[toPodId]
|
|
|
|
|
2017-07-11 09:01:56 -05:00
|
|
|
Object.keys(eventsForPod).forEach(uuid => {
|
|
|
|
const eventsForVideo = eventsForPod[uuid]
|
2017-02-26 11:57:33 -06:00
|
|
|
|
|
|
|
Object.keys(eventsForVideo).forEach(eventType => {
|
|
|
|
requestsToMakeGrouped[toPodId].datas.push({
|
|
|
|
data: {
|
2017-07-11 09:01:56 -05:00
|
|
|
uuid,
|
2017-07-10 12:43:21 -05:00
|
|
|
eventType: eventType as RemoteVideoEventType,
|
|
|
|
count: +eventsForVideo[eventType]
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return requestsToMakeGrouped
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) {
|
2017-02-26 11:57:33 -06:00
|
|
|
if (count === undefined) count = 1
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
const dbRequestOptions: Sequelize.CreateOptions = {}
|
2017-02-26 11:57:33 -06:00
|
|
|
if (transaction) dbRequestOptions.transaction = transaction
|
|
|
|
|
|
|
|
const createQuery = {
|
|
|
|
type,
|
|
|
|
count,
|
|
|
|
videoId
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.RequestVideoEvent.create(createQuery, dbRequestOptions)
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
RequestVideoEventScheduler
|
|
|
|
}
|