import * as Sequelize from 'sequelize'
import { database as db } from '../../initializers/database'
-import { AbstractRequestScheduler } from './abstract-request-scheduler'
+import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
import {
REQUESTS_VIDEO_EVENT_LIMIT_PODS,
REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
REQUEST_VIDEO_EVENT_ENDPOINT
} from '../../initializers'
+import { RequestsVideoEventGrouped } from '../../models'
+import { RequestVideoEventType, RemoteVideoEventRequest, RemoteVideoEventType } from '../../../shared'
export type RequestVideoEventSchedulerOptions = {
- type: string
+ type: RequestVideoEventType
videoId: string
count?: number
transaction?: Sequelize.Transaction
}
-class RequestVideoEventScheduler extends AbstractRequestScheduler {
+class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoEventGrouped> {
constructor () {
super()
return db.RequestVideoEvent
}
- buildRequestObjects (eventsToProcess: { [ toPodId: number ]: any }[]) {
- const requestsToMakeGrouped = {}
+ buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) {
+ const requestsToMakeGrouped: RequestsObjects<RemoteVideoEventRequest> = {}
/* Example:
{
}
}
*/
- const eventsPerVideoPerPod = {}
+ const eventsPerVideoPerPod: {
+ [ podId: string ]: {
+ [ videoRemoteId: string ]: {
+ views?: number
+ likes?: number
+ dislikes?: number
+ }
+ }
+ } = {}
// We group video events per video and per pod
// We add the counts of the same event types
- Object.keys(eventsToProcess).forEach(toPodId => {
- eventsToProcess[toPodId].forEach(eventToProcess => {
+ Object.keys(eventRequests).forEach(toPodId => {
+ eventRequests[toPodId].forEach(eventToProcess => {
if (!eventsPerVideoPerPod[toPodId]) eventsPerVideoPerPod[toPodId] = {}
if (!requestsToMakeGrouped[toPodId]) {
requestsToMakeGrouped[toPodId].datas.push({
data: {
remoteId,
- eventType,
- count: eventsForVideo[eventType]
+ eventType: eventType as RemoteVideoEventType,
+ count: +eventsForVideo[eventType]
}
})
})
return requestsToMakeGrouped
}
- createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) {
+ createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) {
if (count === undefined) count = 1
const dbRequestOptions: Sequelize.CreateOptions = {}
videoId
}
- return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback)
+ return db.RequestVideoEvent.create(createQuery, dbRequestOptions)
}
}