stub score client
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { findLibraryItemById } from '../services/library_item'
|
||||
import { Feature, getScores } from '../services/score'
|
||||
import { Feature, scoreClient } from '../services/score'
|
||||
import { enqueueUpdateHomeJob } from '../utils/createTask'
|
||||
import { lanaugeToCode } from '../utils/helpers'
|
||||
import { logger } from '../utils/logger'
|
||||
@ -57,7 +57,7 @@ export const scoreLibraryItem = async (
|
||||
} as Feature,
|
||||
}
|
||||
|
||||
const scores = await getScores({
|
||||
const scores = await scoreClient.getScores({
|
||||
user_id: userId,
|
||||
items: itemFeatures,
|
||||
})
|
||||
|
||||
@ -7,7 +7,7 @@ import { registerMetric } from '../prometheus'
|
||||
import { redisDataSource } from '../redis_data_source'
|
||||
import { findUnseenPublicItems } from '../services/home'
|
||||
import { searchLibraryItems } from '../services/library_item'
|
||||
import { Feature, getScores } from '../services/score'
|
||||
import { Feature, scoreClient } from '../services/score'
|
||||
import { findSubscriptionsByNames } from '../services/subscriptions'
|
||||
import { findActiveUser } from '../services/user'
|
||||
import { lanaugeToCode } from '../utils/helpers'
|
||||
@ -227,7 +227,7 @@ const rankCandidates = async (
|
||||
}, {} as Record<string, Feature>),
|
||||
}
|
||||
|
||||
const scores = await getScores(data)
|
||||
const scores = await scoreClient.getScores(data)
|
||||
// update scores for candidates
|
||||
candidates.forEach((item) => {
|
||||
item.score = scores[item.id]['score'] || 0
|
||||
|
||||
@ -27,24 +27,46 @@ export type ScoreBody = {
|
||||
}
|
||||
|
||||
export type ScoreApiResponse = Record<string, ScoreBody> // item_id -> score
|
||||
interface ScoreClient {
|
||||
getScores(data: ScoreApiRequestBody): Promise<ScoreApiResponse>
|
||||
}
|
||||
|
||||
export const getScores = async (
|
||||
data: ScoreApiRequestBody
|
||||
): Promise<ScoreApiResponse> => {
|
||||
const API_URL = env.score.apiUrl
|
||||
class StubScoreClientImpl implements ScoreClient {
|
||||
async getScores(data: ScoreApiRequestBody): Promise<ScoreApiResponse> {
|
||||
const stubScore = 1.0
|
||||
|
||||
const response = await fetch(API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
const stubScores: ScoreApiResponse = {}
|
||||
for (const itemId in data.items) {
|
||||
stubScores[itemId] = { score: stubScore }
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to score candidates: ${response.statusText}`)
|
||||
return Promise.resolve(stubScores)
|
||||
}
|
||||
}
|
||||
|
||||
class ScoreClientImpl implements ScoreClient {
|
||||
private apiUrl: string
|
||||
|
||||
constructor(apiUrl = env.score.apiUrl) {
|
||||
this.apiUrl = apiUrl
|
||||
}
|
||||
|
||||
const scores = (await response.json()) as ScoreApiResponse
|
||||
return scores
|
||||
async getScores(data: ScoreApiRequestBody): Promise<ScoreApiResponse> {
|
||||
const response = await fetch(this.apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
export const scoreClient = new StubScoreClientImpl()
|
||||
|
||||
Reference in New Issue
Block a user