Merge pull request #4006 from omnivore-app/fix/score-service-api-env

fix/score service api env
This commit is contained in:
Hongbo Wu
2024-05-29 18:31:18 +08:00
committed by GitHub
7 changed files with 72 additions and 50 deletions

View File

@ -1310,7 +1310,7 @@ export type HomeItem = {
likeCount?: Maybe<Scalars['Int']>;
previewContent?: Maybe<Scalars['String']>;
saveCount?: Maybe<Scalars['Int']>;
score: Scalars['Float'];
score?: Maybe<Scalars['Float']>;
seen_at?: Maybe<Scalars['Date']>;
slug?: Maybe<Scalars['String']>;
source?: Maybe<HomeItemSource>;
@ -6103,7 +6103,7 @@ export type HomeItemResolvers<ContextType = ResolverContext, ParentType extends
likeCount?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
previewContent?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
saveCount?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
score?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
score?: Resolver<Maybe<ResolversTypes['Float']>, ParentType, ContextType>;
seen_at?: Resolver<Maybe<ResolversTypes['Date']>, ParentType, ContextType>;
slug?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
source?: Resolver<Maybe<ResolversTypes['HomeItemSource']>, ParentType, ContextType>;

View File

@ -1179,7 +1179,7 @@ type HomeItem {
likeCount: Int
previewContent: String
saveCount: Int
score: Float!
score: Float
seen_at: Date
slug: String
source: HomeItemSource

View File

@ -9,7 +9,7 @@ import { Feature, getScores } from '../services/score'
import { findSubscriptionsByNames } from '../services/subscriptions'
import { findActiveUser } from '../services/user'
import { lanaugeToCode } from '../utils/helpers'
import { logger } from '../utils/logger'
import { logError, logger } from '../utils/logger'
export const UPDATE_HOME_JOB = 'UPDATE_HOME_JOB'
@ -159,6 +159,11 @@ const rankCandidates = async (
userId: string,
candidates: Array<Candidate>
): Promise<Array<Candidate>> => {
if (candidates.length <= 10) {
logger.info('Not enough candidates to rank')
return candidates
}
const unscoredCandidates = candidates.filter((item) => item.score === 0)
const data = {
@ -381,38 +386,56 @@ export const updateHome = async (data: UpdateHomeJobData) => {
const { userId, cursor } = data
logger.info('Updating home for user', data)
const user = await findActiveUser(userId)
if (!user) {
logger.error(`User ${userId} not found`)
return
try {
const user = await findActiveUser(userId)
if (!user) {
logger.error(`User ${userId} not found`)
return
}
logger.info(`Updating home for user ${userId}`)
logger.profile('selecting')
const candidates = await selectCandidates(user)
logger.profile('selecting', {
level: 'info',
message: `Found ${candidates.length} candidates`,
})
if (candidates.length === 0) {
logger.info('No candidates found')
return
}
// TODO: integrity check on candidates
logger.profile('ranking')
const rankedCandidates = await rankCandidates(userId, candidates)
logger.profile('ranking', {
level: 'info',
message: `Ranked ${rankedCandidates.length} candidates`,
})
// TODO: filter candidates
logger.profile('mixing')
const rankedSections = mixHomeItems(rankedCandidates)
logger.profile('mixing', {
level: 'info',
message: `Created ${rankedSections.length} sections`,
})
logger.profile('saving')
await appendSectionsToHome(userId, rankedSections, cursor)
logger.profile('saving', {
level: 'info',
message: 'Sections appended to home',
})
logger.info('Home updated for user', { userId })
} catch (error) {
logError(error)
throw error
}
logger.info(`Updating home for user ${userId}`)
const candidates = await selectCandidates(user)
logger.info(`Found ${candidates.length} candidates`)
if (candidates.length <= 10) {
logger.info('Not enough candidates found')
return
}
// TODO: integrity check on candidates
logger.info('Ranking candidates')
const rankedCandidates = await rankCandidates(userId, candidates)
if (rankedCandidates.length === 0) {
logger.info('No candidates found')
return
}
// TODO: filter candidates
logger.info('Mix home items to create sections')
const rankedSections = mixHomeItems(rankedCandidates)
logger.info(`Created ${rankedSections.length} sections`)
logger.info('Appending sections to home')
await appendSectionsToHome(userId, rankedSections, cursor)
logger.info('Home updated for user', { userId })
}

View File

@ -647,16 +647,13 @@ export const functionResolvers = {
ctx: WithDataSourcesContext
) {
const items = section.items
console.log('items', items)
const libraryItemIds = items
.filter((item) => item.type === 'library_item')
.map((item) => item.id)
console.log('libraryItemIds', libraryItemIds)
const libraryItems = (
await ctx.dataLoaders.libraryItems.loadMany(libraryItemIds)
).filter((libraryItem) => !isError(libraryItem)) as Array<LibraryItem>
console.log('libraryItems', libraryItems)
const publicItemIds = section.items
.filter((item) => item.type === 'public_item')
@ -667,11 +664,9 @@ export const functionResolvers = {
return items
.map((item) => {
console.log('item', item)
const libraryItem = libraryItems.find(
(libraryItem) => item.id === libraryItem.id
)
console.log('libraryItem', libraryItem)
if (libraryItem) {
return {
id: libraryItem.id,

View File

@ -3148,7 +3148,7 @@ const schema = gql`
canShare: Boolean
canArchive: Boolean
canDelete: Boolean
score: Float!
score: Float
}
type HomeSection {

View File

@ -1,3 +1,5 @@
import { env } from '../env'
export interface Feature {
library_item_id?: string
title: string
@ -29,18 +31,12 @@ export type ScoreApiResponse = Record<string, ScoreBody> // item_id -> score
export const getScores = async (
data: ScoreApiRequestBody
): Promise<ScoreApiResponse> => {
const API_URL = 'http://digest-score/batch'
// const token = process.env.SCORE_API_TOKEN
// if (!token) {
// throw new Error('No score API token found')
// }
const API_URL = env.score.apiUrl
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
})

View File

@ -119,6 +119,9 @@ export interface BackendEnv {
clientSecret: string
authUrl: string
}
score: {
apiUrl: string
}
}
const nullableEnvVars = [
@ -175,6 +178,7 @@ const nullableEnvVars = [
'NOTION_CLIENT_ID',
'NOTION_CLIENT_SECRET',
'NOTION_AUTH_URL',
'SCORE_API_URL',
] // Allow some vars to be null/empty
const envParser =
@ -329,6 +333,9 @@ export function getEnv(): BackendEnv {
clientSecret: parse('NOTION_CLIENT_SECRET'),
authUrl: parse('NOTION_AUTH_URL'),
}
const score = {
apiUrl: parse('SCORE_API_URL') || 'http://digest-score/batch',
}
return {
pg,
@ -352,6 +359,7 @@ export function getEnv(): BackendEnv {
subscription,
redis,
notion,
score,
}
}