X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Frequest%2Frequest-video-event-scheduler.ts;h=8a008c51bbae9d3c15bb0bfc5152285c28c6c4e5;hb=4771e0008dd26eadbb7eaff64255a6ec914fdadb;hp=bde50b1d33dfc3ae9c619758f249c89d54257f5c;hpb=e02643f32e4c97ca307f8fc5b69be79c40d70a3b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts index bde50b1d3..8a008c51b 100644 --- a/server/lib/request/request-video-event-scheduler.ts +++ b/server/lib/request/request-video-event-scheduler.ts @@ -1,12 +1,23 @@ +import * as Sequelize from 'sequelize' + import { database as db } from '../../initializers/database' -import { BaseRequestScheduler } from './base-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: RequestVideoEventType + videoId: string + count?: number + transaction?: Sequelize.Transaction +} -class RequestVideoEventScheduler extends BaseRequestScheduler { +class RequestVideoEventScheduler extends AbstractRequestScheduler { constructor () { super() @@ -25,8 +36,8 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { return db.RequestVideoEvent } - buildRequestObjects (eventsToProcess) { - const requestsToMakeGrouped = {} + buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) { + const requestsToMakeGrouped: RequestsObjects = {} /* Example: { @@ -36,12 +47,20 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { } } */ - 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]) { @@ -76,8 +95,8 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { requestsToMakeGrouped[toPodId].datas.push({ data: { remoteId, - eventType, - count: eventsForVideo[eventType] + eventType: eventType as RemoteVideoEventType, + count: +eventsForVideo[eventType] } }) }) @@ -87,16 +106,10 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { return requestsToMakeGrouped } - // { type, videoId, count?, transaction? } - createRequest (options, callback) { - const type = options.type - const videoId = options.videoId - const transaction = options.transaction - let count = options.count - + createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions) { if (count === undefined) count = 1 - const dbRequestOptions: { transaction?: any } = {} + const dbRequestOptions: Sequelize.CreateOptions = {} if (transaction) dbRequestOptions.transaction = transaction const createQuery = { @@ -105,7 +118,7 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { videoId } - return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback) + return db.RequestVideoEvent.create(createQuery, dbRequestOptions) } }