aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-14 11:52:23 +0200
committerChocobozzz <me@florianbigard.com>2018-09-14 11:52:23 +0200
commit7348b1fd84dee869b3c36554aea6797f09d4ceed (patch)
tree46f6800a92f659dd989d0f38c1b682a61fd2315a /server/controllers/api
parent2b62cccd75e9025fb66148bcb1feea2a458ee8e4 (diff)
downloadPeerTube-7348b1fd84dee869b3c36554aea6797f09d4ceed.tar.gz
PeerTube-7348b1fd84dee869b3c36554aea6797f09d4ceed.tar.zst
PeerTube-7348b1fd84dee869b3c36554aea6797f09d4ceed.zip
Speed up overviews route
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/overviews.ts26
1 files changed, 18 insertions, 8 deletions
diff --git a/server/controllers/api/overviews.ts b/server/controllers/api/overviews.ts
index da941c0ac..cc3cc54a7 100644
--- a/server/controllers/api/overviews.ts
+++ b/server/controllers/api/overviews.ts
@@ -4,8 +4,9 @@ import { VideoModel } from '../../models/video/video'
4import { asyncMiddleware } from '../../middlewares' 4import { asyncMiddleware } from '../../middlewares'
5import { TagModel } from '../../models/video/tag' 5import { TagModel } from '../../models/video/tag'
6import { VideosOverview } from '../../../shared/models/overviews' 6import { VideosOverview } from '../../../shared/models/overviews'
7import { OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers' 7import { MEMOIZE_TTL, OVERVIEWS, ROUTE_CACHE_LIFETIME } from '../../initializers'
8import { cacheRoute } from '../../middlewares/cache' 8import { cacheRoute } from '../../middlewares/cache'
9import * as memoizee from 'memoizee'
9 10
10const overviewsRouter = express.Router() 11const overviewsRouter = express.Router()
11 12
@@ -23,10 +24,17 @@ export { overviewsRouter }
23// This endpoint could be quite long, but we cache it 24// This endpoint could be quite long, but we cache it
24async function getVideosOverview (req: express.Request, res: express.Response) { 25async function getVideosOverview (req: express.Request, res: express.Response) {
25 const attributes = await buildSamples() 26 const attributes = await buildSamples()
27
28 const [ categories, channels, tags ] = await Promise.all([
29 Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))),
30 Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))),
31 Promise.all(attributes.tags.map(t => getVideosByTag(t, res)))
32 ])
33
26 const result: VideosOverview = { 34 const result: VideosOverview = {
27 categories: await Promise.all(attributes.categories.map(c => getVideosByCategory(c, res))), 35 categories,
28 channels: await Promise.all(attributes.channels.map(c => getVideosByChannel(c, res))), 36 channels,
29 tags: await Promise.all(attributes.tags.map(t => getVideosByTag(t, res))) 37 tags
30 } 38 }
31 39
32 // Cleanup our object 40 // Cleanup our object
@@ -37,7 +45,7 @@ async function getVideosOverview (req: express.Request, res: express.Response) {
37 return res.json(result) 45 return res.json(result)
38} 46}
39 47
40async function buildSamples () { 48const buildSamples = memoizee(async function () {
41 const [ categories, channels, tags ] = await Promise.all([ 49 const [ categories, channels, tags ] = await Promise.all([
42 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT), 50 VideoModel.getRandomFieldSamples('category', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD, OVERVIEWS.VIDEOS.SAMPLES_COUNT),
43 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT), 51 VideoModel.getRandomFieldSamples('channelId', OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD ,OVERVIEWS.VIDEOS.SAMPLES_COUNT),
@@ -45,7 +53,7 @@ async function buildSamples () {
45 ]) 53 ])
46 54
47 return { categories, channels, tags } 55 return { categories, channels, tags }
48} 56}, { maxAge: MEMOIZE_TTL.OVERVIEWS_SAMPLE })
49 57
50async function getVideosByTag (tag: string, res: express.Response) { 58async function getVideosByTag (tag: string, res: express.Response) {
51 const videos = await getVideos(res, { tagsOneOf: [ tag ] }) 59 const videos = await getVideos(res, { tagsOneOf: [ tag ] })
@@ -84,14 +92,16 @@ async function getVideos (
84 res: express.Response, 92 res: express.Response,
85 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] } 93 where: { videoChannelId?: number, tagsOneOf?: string[], categoryOneOf?: number[] }
86) { 94) {
87 const { data } = await VideoModel.listForApi(Object.assign({ 95 const query = Object.assign({
88 start: 0, 96 start: 0,
89 count: 10, 97 count: 10,
90 sort: '-createdAt', 98 sort: '-createdAt',
91 includeLocalVideos: true, 99 includeLocalVideos: true,
92 nsfw: buildNSFWFilter(res), 100 nsfw: buildNSFWFilter(res),
93 withFiles: false 101 withFiles: false
94 }, where)) 102 }, where)
103
104 const { data } = await VideoModel.listForApi(query, false)
95 105
96 return data.map(d => d.toFormattedJSON()) 106 return data.map(d => d.toFormattedJSON())
97} 107}