From b24d8ca41328fb9e7b3dec3bb1786c4b8b4c2963 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 27 May 2024 17:55:19 +0800 Subject: [PATCH] show long read items before quick links --- .../api/src/jobs/update_just_read_feed.ts | 31 +++++------ packages/api/src/services/score.ts | 54 +++++++++---------- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/packages/api/src/jobs/update_just_read_feed.ts b/packages/api/src/jobs/update_just_read_feed.ts index 267dfcfb3..5f2cecc59 100644 --- a/packages/api/src/jobs/update_just_read_feed.ts +++ b/packages/api/src/jobs/update_just_read_feed.ts @@ -221,7 +221,7 @@ export const getJustReadFeedSections = async ( const key = redisKey(userId) // get feed items from redis sorted set in descending order - // with score greater than minScore + // with score smalled than maxScore // limit to the first `limit` items // response is an array of [member1, score1, member2, score2, ...] const results = await redisClient.zrevrangebyscore( @@ -335,34 +335,29 @@ const mixFeedItems = (rankedFeedItems: Array): Array
=> { } } - // distribute longer items first and then shorter items - distributeItems(longItems, batches) + // distribute quick link items first distributeItems(shortItems, batches) + distributeItems(longItems, batches) // convert batches to sections const sections = [] for (const batch of batches) { - // create a section for each long item - for (let i = 0; i < 5; i++) { - const section: Section = { - items: [ - { - id: batch[i].id, - type: batch[i].type, - }, - ], - layout: 'long', - } - sections.push(section) - } - // create a section for short items + // create a section for all quick links sections.push({ - items: batch.slice(5).map((item) => ({ + items: batch.slice(0, 5).map((item) => ({ id: item.id, type: item.type, })), layout: 'quick links', }) + + // create a section for each long item + sections.push( + ...batch.slice(5).map((item) => ({ + items: [{ id: item.id, type: item.type }], + layout: 'long', + })) + ) } return sections diff --git a/packages/api/src/services/score.ts b/packages/api/src/services/score.ts index e2dc8d1ca..cd9cf9dc1 100644 --- a/packages/api/src/services/score.ts +++ b/packages/api/src/services/score.ts @@ -23,33 +23,33 @@ export type ScoreApiResponse = Record // item_id -> score export const getScores = async ( data: ScoreApiRequestBody ): Promise => { - // const API_URL = 'http://127.0.0.1:5000/predictions' - // const token = process.env.SCORE_API_TOKEN + const API_URL = 'http://127.0.0.1:5000/predictions' + const token = process.env.SCORE_API_TOKEN - // if (!token) { - // throw new Error('No score API token found') - // } - - // const response = await fetch(API_URL, { - // method: 'POST', - // headers: { - // 'Content-Type': 'application/json', - // Authorization: `Bearer ${token}`, - // }, - // body: JSON.stringify(data), - // }) - - // if (!response.ok) { - // throw new Error(`Failed to score candidates: ${response.statusText}`) - // } - - // const scores = (await response.json()) as ScoreApiResponse - // return scores - - // fake random scores - const scores: ScoreApiResponse = {} - for (const itemId of Object.keys(data.item_features)) { - scores[itemId] = Math.random() + if (!token) { + throw new Error('No score API token found') } - return Promise.resolve(scores) + + const response = await fetch(API_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(data), + }) + + if (!response.ok) { + throw new Error(`Failed to score candidates: ${response.statusText}`) + } + + const scores = (await response.json()) as ScoreApiResponse + return scores + + // // fake random scores + // const scores: ScoreApiResponse = {} + // for (const itemId of Object.keys(data.item_features)) { + // scores[itemId] = Math.random() + // } + // return Promise.resolve(scores) }