diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-07-10 19:43:21 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-07-10 19:43:21 +0200 |
commit | 4771e0008dd26eadbb7eaff64255a6ec914fdadb (patch) | |
tree | 4fd58f8a3f3c2d674b936c99817b4f5fb958c5d8 /server/lib/request | |
parent | 7a214f746bf420defbf17fa218d90d6233551bf8 (diff) | |
download | PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.tar.gz PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.tar.zst PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.zip |
Better typescript typing for a better world
Diffstat (limited to 'server/lib/request')
-rw-r--r-- | server/lib/request/abstract-request-scheduler.ts | 22 | ||||
-rw-r--r-- | server/lib/request/request-scheduler.ts | 8 | ||||
-rw-r--r-- | server/lib/request/request-video-event-scheduler.ts | 22 | ||||
-rw-r--r-- | server/lib/request/request-video-qadu-scheduler.ts | 31 |
4 files changed, 60 insertions, 23 deletions
diff --git a/server/lib/request/abstract-request-scheduler.ts b/server/lib/request/abstract-request-scheduler.ts index 0a9ff65d5..ce4e2ffd2 100644 --- a/server/lib/request/abstract-request-scheduler.ts +++ b/server/lib/request/abstract-request-scheduler.ts | |||
@@ -10,6 +10,15 @@ import { | |||
10 | REQUESTS_INTERVAL | 10 | REQUESTS_INTERVAL |
11 | } from '../../initializers' | 11 | } from '../../initializers' |
12 | 12 | ||
13 | interface RequestsObjects<U> { | ||
14 | [ id: string ]: { | ||
15 | toPod: PodInstance | ||
16 | endpoint: string | ||
17 | ids: number[] // ids | ||
18 | datas: U[] | ||
19 | } | ||
20 | } | ||
21 | |||
13 | abstract class AbstractRequestScheduler <T> { | 22 | abstract class AbstractRequestScheduler <T> { |
14 | requestInterval: number | 23 | requestInterval: number |
15 | limitPods: number | 24 | limitPods: number |
@@ -27,7 +36,7 @@ abstract class AbstractRequestScheduler <T> { | |||
27 | 36 | ||
28 | abstract getRequestModel (): AbstractRequestClass<T> | 37 | abstract getRequestModel (): AbstractRequestClass<T> |
29 | abstract getRequestToPodModel (): AbstractRequestToPodClass | 38 | abstract getRequestToPodModel (): AbstractRequestToPodClass |
30 | abstract buildRequestObjects (requestsGrouped: T): {} | 39 | abstract buildRequestsObjects (requestsGrouped: T): RequestsObjects<any> |
31 | 40 | ||
32 | activate () { | 41 | activate () { |
33 | logger.info('Requests scheduler activated.') | 42 | logger.info('Requests scheduler activated.') |
@@ -67,7 +76,7 @@ abstract class AbstractRequestScheduler <T> { | |||
67 | // --------------------------------------------------------------------------- | 76 | // --------------------------------------------------------------------------- |
68 | 77 | ||
69 | // Make a requests to friends of a certain type | 78 | // Make a requests to friends of a certain type |
70 | protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object) { | 79 | protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: any) { |
71 | const params = { | 80 | const params = { |
72 | toPod: toPod, | 81 | toPod: toPod, |
73 | method: 'POST' as 'POST', | 82 | method: 'POST' as 'POST', |
@@ -95,7 +104,7 @@ abstract class AbstractRequestScheduler <T> { | |||
95 | return this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod) | 104 | return this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod) |
96 | .then((requestsGrouped: T) => { | 105 | .then((requestsGrouped: T) => { |
97 | // We want to group requests by destinations pod and endpoint | 106 | // We want to group requests by destinations pod and endpoint |
98 | const requestsToMake = this.buildRequestObjects(requestsGrouped) | 107 | const requestsToMake = this.buildRequestsObjects(requestsGrouped) |
99 | 108 | ||
100 | // If there are no requests, abort | 109 | // If there are no requests, abort |
101 | if (isEmpty(requestsToMake) === true) { | 110 | if (isEmpty(requestsToMake) === true) { |
@@ -105,8 +114,8 @@ abstract class AbstractRequestScheduler <T> { | |||
105 | 114 | ||
106 | logger.info('Making "%s" to friends.', this.description) | 115 | logger.info('Making "%s" to friends.', this.description) |
107 | 116 | ||
108 | const goodPods = [] | 117 | const goodPods: number[] = [] |
109 | const badPods = [] | 118 | const badPods: number[] = [] |
110 | 119 | ||
111 | return Promise.map(Object.keys(requestsToMake), hashKey => { | 120 | return Promise.map(Object.keys(requestsToMake), hashKey => { |
112 | const requestToMake = requestsToMake[hashKey] | 121 | const requestToMake = requestsToMake[hashKey] |
@@ -149,5 +158,6 @@ abstract class AbstractRequestScheduler <T> { | |||
149 | // --------------------------------------------------------------------------- | 158 | // --------------------------------------------------------------------------- |
150 | 159 | ||
151 | export { | 160 | export { |
152 | AbstractRequestScheduler | 161 | AbstractRequestScheduler, |
162 | RequestsObjects | ||
153 | } | 163 | } |
diff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.ts index 3945ace20..696875dcf 100644 --- a/server/lib/request/request-scheduler.ts +++ b/server/lib/request/request-scheduler.ts | |||
@@ -1,11 +1,11 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | 2 | ||
3 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
4 | import { AbstractRequestScheduler } from './abstract-request-scheduler' | 4 | import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler' |
5 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
6 | import { REQUESTS_LIMIT_PODS, REQUESTS_LIMIT_PER_POD } from '../../initializers' | 6 | import { REQUESTS_LIMIT_PODS, REQUESTS_LIMIT_PER_POD } from '../../initializers' |
7 | import { RequestsGrouped } from '../../models' | 7 | import { RequestsGrouped } from '../../models' |
8 | import { RequestEndpoint } from '../../../shared' | 8 | import { RequestEndpoint, RemoteVideoRequest } from '../../../shared' |
9 | 9 | ||
10 | export type RequestSchedulerOptions = { | 10 | export type RequestSchedulerOptions = { |
11 | type: string | 11 | type: string |
@@ -34,8 +34,8 @@ class RequestScheduler extends AbstractRequestScheduler<RequestsGrouped> { | |||
34 | return db.RequestToPod | 34 | return db.RequestToPod |
35 | } | 35 | } |
36 | 36 | ||
37 | buildRequestObjects (requestsGrouped: RequestsGrouped) { | 37 | buildRequestsObjects (requestsGrouped: RequestsGrouped) { |
38 | const requestsToMakeGrouped = {} | 38 | const requestsToMakeGrouped: RequestsObjects<RemoteVideoRequest> = {} |
39 | 39 | ||
40 | Object.keys(requestsGrouped).forEach(toPodId => { | 40 | Object.keys(requestsGrouped).forEach(toPodId => { |
41 | requestsGrouped[toPodId].forEach(data => { | 41 | requestsGrouped[toPodId].forEach(data => { |
diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts index d4d714c02..8a008c51b 100644 --- a/server/lib/request/request-video-event-scheduler.ts +++ b/server/lib/request/request-video-event-scheduler.ts | |||
@@ -1,14 +1,14 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | 2 | ||
3 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
4 | import { AbstractRequestScheduler } from './abstract-request-scheduler' | 4 | import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler' |
5 | import { | 5 | import { |
6 | REQUESTS_VIDEO_EVENT_LIMIT_PODS, | 6 | REQUESTS_VIDEO_EVENT_LIMIT_PODS, |
7 | REQUESTS_VIDEO_EVENT_LIMIT_PER_POD, | 7 | REQUESTS_VIDEO_EVENT_LIMIT_PER_POD, |
8 | REQUEST_VIDEO_EVENT_ENDPOINT | 8 | REQUEST_VIDEO_EVENT_ENDPOINT |
9 | } from '../../initializers' | 9 | } from '../../initializers' |
10 | import { RequestsVideoEventGrouped } from '../../models' | 10 | import { RequestsVideoEventGrouped } from '../../models' |
11 | import { RequestVideoEventType } from '../../../shared' | 11 | import { RequestVideoEventType, RemoteVideoEventRequest, RemoteVideoEventType } from '../../../shared' |
12 | 12 | ||
13 | export type RequestVideoEventSchedulerOptions = { | 13 | export type RequestVideoEventSchedulerOptions = { |
14 | type: RequestVideoEventType | 14 | type: RequestVideoEventType |
@@ -36,8 +36,8 @@ class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoE | |||
36 | return db.RequestVideoEvent | 36 | return db.RequestVideoEvent |
37 | } | 37 | } |
38 | 38 | ||
39 | buildRequestObjects (eventRequests: RequestsVideoEventGrouped) { | 39 | buildRequestsObjects (eventRequests: RequestsVideoEventGrouped) { |
40 | const requestsToMakeGrouped = {} | 40 | const requestsToMakeGrouped: RequestsObjects<RemoteVideoEventRequest> = {} |
41 | 41 | ||
42 | /* Example: | 42 | /* Example: |
43 | { | 43 | { |
@@ -47,7 +47,15 @@ class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoE | |||
47 | } | 47 | } |
48 | } | 48 | } |
49 | */ | 49 | */ |
50 | const eventsPerVideoPerPod = {} | 50 | const eventsPerVideoPerPod: { |
51 | [ podId: string ]: { | ||
52 | [ videoRemoteId: string ]: { | ||
53 | views?: number | ||
54 | likes?: number | ||
55 | dislikes?: number | ||
56 | } | ||
57 | } | ||
58 | } = {} | ||
51 | 59 | ||
52 | // We group video events per video and per pod | 60 | // We group video events per video and per pod |
53 | // We add the counts of the same event types | 61 | // We add the counts of the same event types |
@@ -87,8 +95,8 @@ class RequestVideoEventScheduler extends AbstractRequestScheduler<RequestsVideoE | |||
87 | requestsToMakeGrouped[toPodId].datas.push({ | 95 | requestsToMakeGrouped[toPodId].datas.push({ |
88 | data: { | 96 | data: { |
89 | remoteId, | 97 | remoteId, |
90 | eventType, | 98 | eventType: eventType as RemoteVideoEventType, |
91 | count: eventsForVideo[eventType] | 99 | count: +eventsForVideo[eventType] |
92 | } | 100 | } |
93 | }) | 101 | }) |
94 | }) | 102 | }) |
diff --git a/server/lib/request/request-video-qadu-scheduler.ts b/server/lib/request/request-video-qadu-scheduler.ts index 5ec7de9c2..988165170 100644 --- a/server/lib/request/request-video-qadu-scheduler.ts +++ b/server/lib/request/request-video-qadu-scheduler.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | 2 | ||
3 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
4 | import { AbstractRequestScheduler } from './abstract-request-scheduler' | 4 | import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler' |
5 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
6 | import { | 6 | import { |
7 | REQUESTS_VIDEO_QADU_LIMIT_PODS, | 7 | REQUESTS_VIDEO_QADU_LIMIT_PODS, |
@@ -9,8 +9,27 @@ import { | |||
9 | REQUEST_VIDEO_QADU_ENDPOINT, | 9 | REQUEST_VIDEO_QADU_ENDPOINT, |
10 | REQUEST_VIDEO_QADU_TYPES | 10 | REQUEST_VIDEO_QADU_TYPES |
11 | } from '../../initializers' | 11 | } from '../../initializers' |
12 | import { RequestsVideoQaduGrouped } from '../../models' | 12 | import { RequestsVideoQaduGrouped, PodInstance } from '../../models' |
13 | import { RequestVideoQaduType } from '../../../shared' | 13 | import { RemoteQaduVideoRequest, RequestVideoQaduType } from '../../../shared' |
14 | |||
15 | // We create a custom interface because we need "videos" attribute for our computations | ||
16 | interface RequestsObjectsCustom<U> extends RequestsObjects<U> { | ||
17 | [ id: string ]: { | ||
18 | toPod: PodInstance | ||
19 | endpoint: string | ||
20 | ids: number[] // ids | ||
21 | datas: U[] | ||
22 | |||
23 | videos: { | ||
24 | [ id: string ]: { | ||
25 | remoteId: string | ||
26 | likes?: number | ||
27 | dislikes?: number | ||
28 | views?: number | ||
29 | } | ||
30 | } | ||
31 | } | ||
32 | } | ||
14 | 33 | ||
15 | export type RequestVideoQaduSchedulerOptions = { | 34 | export type RequestVideoQaduSchedulerOptions = { |
16 | type: RequestVideoQaduType | 35 | type: RequestVideoQaduType |
@@ -37,8 +56,8 @@ class RequestVideoQaduScheduler extends AbstractRequestScheduler<RequestsVideoQa | |||
37 | return db.RequestVideoQadu | 56 | return db.RequestVideoQadu |
38 | } | 57 | } |
39 | 58 | ||
40 | buildRequestObjects (requests: RequestsVideoQaduGrouped) { | 59 | buildRequestsObjects (requests: RequestsVideoQaduGrouped) { |
41 | const requestsToMakeGrouped = {} | 60 | const requestsToMakeGrouped: RequestsObjectsCustom<RemoteQaduVideoRequest> = {} |
42 | 61 | ||
43 | Object.keys(requests).forEach(toPodId => { | 62 | Object.keys(requests).forEach(toPodId => { |
44 | requests[toPodId].forEach(data => { | 63 | requests[toPodId].forEach(data => { |
@@ -59,7 +78,7 @@ class RequestVideoQaduScheduler extends AbstractRequestScheduler<RequestsVideoQa | |||
59 | 78 | ||
60 | // Maybe another attribute was filled for this video | 79 | // Maybe another attribute was filled for this video |
61 | let videoData = requestsToMakeGrouped[hashKey].videos[video.id] | 80 | let videoData = requestsToMakeGrouped[hashKey].videos[video.id] |
62 | if (!videoData) videoData = {} | 81 | if (!videoData) videoData = { remoteId: null } |
63 | 82 | ||
64 | switch (request.type) { | 83 | switch (request.type) { |
65 | case REQUEST_VIDEO_QADU_TYPES.LIKES: | 84 | case REQUEST_VIDEO_QADU_TYPES.LIKES: |