batching labels

This commit is contained in:
Hongbo Wu
2024-04-25 22:20:56 +08:00
parent ae8790b545
commit 1445dee933
5 changed files with 41 additions and 18 deletions

View File

@ -20,3 +20,15 @@ webview_gen:
yarn workspace @omnivore/appreader build
cp packages/appreader/build/bundle.js apple/OmnivoreKit/Sources/Views/Resources/bundle.js
cp packages/appreader/build/bundle.js android/Omnivore/app/src/main/assets/bundle.js
api:
yarn workspace @omnivore/api dev
web:
yarn workspace @omnivore/web dev
qp:
yarn workspace @omnivore/api dev_qp
content:
yarn workspace @omnivore/content_fetch start

View File

@ -56,7 +56,7 @@
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"csv-stringify": "^6.4.0",
"dataloader": "^2.0.0",
"dataloader": "^2.2.2",
"diff-match-patch": "^1.0.5",
"dompurify": "^2.0.17",
"dot-case": "^3.0.4",

View File

@ -25,7 +25,7 @@ import {
import { getAISummary } from '../services/ai-summaries'
import { findUserFeatures } from '../services/features'
import { findHighlightsByLibraryItemId } from '../services/highlights'
import { findLabelsByLibraryItemId } from '../services/labels'
import { labelsLoader } from '../services/labels'
import { findRecommendationsByLibraryItemId } from '../services/recommendation'
import { findUploadFileById } from '../services/upload_file'
import {
@ -439,7 +439,7 @@ export const functionResolvers = {
) {
if (article.labels) return article.labels
return findLabelsByLibraryItemId(article.id, ctx.uid)
return labelsLoader.load(article.id)
},
...readingProgressHandlers,
},
@ -505,13 +505,13 @@ export const functionResolvers = {
return item.siteIcon
},
async labels(
item: { id: string; labels?: Label[] },
item: { id: string; labels?: Label[]; labelNames?: string[] },
_: unknown,
ctx: WithDataSourcesContext
) {
if (item.labels) return item.labels
return findLabelsByLibraryItemId(item.id, ctx.uid)
return labelsLoader.load(item.id)
},
async recommendations(
item: {

View File

@ -1,7 +1,9 @@
import DataLoader from 'dataloader'
import { DeepPartial, FindOptionsWhere, In } from 'typeorm'
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'
import { EntityLabel, LabelSource } from '../entity/entity_label'
import { Label } from '../entity/label'
import { LibraryItem } from '../entity/library_item'
import {
createPubSubClient,
EntityEvent,
@ -22,20 +24,24 @@ export type LabelEvent = Merge<
EntityEvent
>
// const batchGetLabelsFromLinkIds = async (
// linkIds: readonly string[]
// ): Promise<Label[][]> => {
// const links = await getRepository(Link).find({
// where: { id: In(linkIds as string[]) },
// relations: ['labels'],
// })
const batchGetLabelsFromLibraryItemIds = async (
libraryItemIds: readonly string[]
): Promise<Label[][]> => {
const libraryItems = await authTrx((tx) =>
tx.getRepository(LibraryItem).find({
where: { id: In(libraryItemIds as string[]) },
relations: ['labels'],
})
)
// return linkIds.map(
// (linkId) => links.find((link) => link.id === linkId)?.labels || []
// )
// }
return libraryItemIds.map(
(libraryItemId) =>
libraryItems.find((libraryItem) => libraryItem.id === libraryItemId)
?.labels || []
)
}
// export const labelsLoader = new DataLoader(batchGetLabelsFromLinkIds)
export const labelsLoader = new DataLoader(batchGetLabelsFromLibraryItemIds)
export const findOrCreateLabels = async (
labels: CreateLabelInput[],

View File

@ -13348,11 +13348,16 @@ data-urls@^2.0.0:
whatwg-mimetype "^2.3.0"
whatwg-url "^8.0.0"
dataloader@2.1.0, dataloader@^2.0.0:
dataloader@2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-2.1.0.tgz#c69c538235e85e7ac6c6c444bae8ecabf5de9df7"
integrity sha512-qTcEYLen3r7ojZNgVUaRggOI+KM7jrKxXeSHhogh/TWxYMeONEMqY+hmkobiYQozsGIyg9OYVzO4ZIfoB4I0pQ==
dataloader@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-2.2.2.tgz#216dc509b5abe39d43a9b9d97e6e5e473dfbe3e0"
integrity sha512-8YnDaaf7N3k/q5HnTJVuzSyLETjoZjVmHc4AeKAzOvKHEFQKcn64OKBfzHYtE9zGjctNM7V9I0MfnUVLpi7M5g==
date-fns@2.x:
version "2.29.3"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8"