From 80344e3c5f99584ec72edc5a7343465dbdf480a4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 21 May 2024 18:15:17 -0500 Subject: [PATCH] Fix trends cache --- src/app.ts | 8 ++--- src/controllers/api/trends.ts | 68 +++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/app.ts b/src/app.ts index be2ce840..9b82bee2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -194,12 +194,8 @@ app.get('/api/v2/search', searchController); app.get('/api/pleroma/frontend_configurations', frontendConfigController); -app.get( - '/api/v1/trends/tags', - cacheMiddleware({ cacheName: 'web', expires: Time.minutes(15) }), - trendingTagsController, -); -app.get('/api/v1/trends', cacheMiddleware({ cacheName: 'web', expires: Time.minutes(15) }), trendingTagsController); +app.get('/api/v1/trends/tags', trendingTagsController); +app.get('/api/v1/trends', trendingTagsController); app.get('/api/v1/suggestions', suggestionsV1Controller); app.get('/api/v2/suggestions', suggestionsV2Controller); diff --git a/src/controllers/api/trends.ts b/src/controllers/api/trends.ts index 1a47d8cd..64b3019a 100644 --- a/src/controllers/api/trends.ts +++ b/src/controllers/api/trends.ts @@ -10,10 +10,20 @@ await TrendsWorker.open('data/trends.sqlite3'); const limitSchema = z.coerce.number().catch(10).transform((value) => Math.min(Math.max(value, 0), 20)); +let cache = getTrends(); + +Deno.cron('update trends cache', { minute: { every: 15 } }, async () => { + const trends = await getTrends(); + cache = Promise.resolve(trends); +}); + const trendingTagsController: AppController = async (c) => { const limit = limitSchema.parse(c.req.query('limit')); - if (limit < 1) return c.json([]); + const trends = await cache; + return c.json(trends.slice(0, limit)); +}; +async function getTrends() { const now = new Date(); const yesterday = new Date(now.getTime() - Time.days(1)); const lastWeek = new Date(now.getTime() - Time.days(7)); @@ -22,36 +32,34 @@ const trendingTagsController: AppController = async (c) => { const tags = await TrendsWorker.getTrendingTags({ since: yesterday, until: now, - limit, + limit: 20, }); - return c.json( - await Promise.all(tags.map(async ({ tag, uses, accounts }) => ({ - name: tag, - url: Conf.local(`/tags/${tag}`), - history: [ - // Use the full 24h query for the current day. Then use `offset: 1` to adjust for this below. - // This result is more accurate than what Mastodon returns. - { - day: String(Math.floor(stripTime(now).getTime() / 1000)), - accounts: String(accounts), - uses: String(uses), - }, - ...(await TrendsWorker.getTagHistory({ - tag, - since: lastWeek, - until: now, - limit: 6, - offset: 1, - })).map((history) => ({ - // For some reason, Mastodon wants these to be strings... oh well. - day: String(Math.floor(history.day.getTime() / 1000)), - accounts: String(history.accounts), - uses: String(history.uses), - })), - ], - }))), - ); -}; + return Promise.all(tags.map(async ({ tag, uses, accounts }) => ({ + name: tag, + url: Conf.local(`/tags/${tag}`), + history: [ + // Use the full 24h query for the current day. Then use `offset: 1` to adjust for this below. + // This result is more accurate than what Mastodon returns. + { + day: String(Math.floor(stripTime(now).getTime() / 1000)), + accounts: String(accounts), + uses: String(uses), + }, + ...(await TrendsWorker.getTagHistory({ + tag, + since: lastWeek, + until: now, + limit: 6, + offset: 1, + })).map((history) => ({ + // For some reason, Mastodon wants these to be strings... oh well. + day: String(Math.floor(history.day.getTime() / 1000)), + accounts: String(history.accounts), + uses: String(history.uses), + })), + ], + }))); +} export { trendingTagsController };