perf(pipeline): 订单项管道改用水位游标并只扫缺失
- 新增 watermark-store:统一读写游标/日水位/失败原因 - 渠道判定、商品归一、成本快照从上次游标继续,到表尾后回绕重试 - 成本快照只处理没有有效快照的订单项,避免每 5 分钟全量重扫
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { query } from '../../db/client.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
import { resolveExceptionsForItems, upsertAggregatedException } from './cost-exception-service.js'
|
||||
import { readWatermarkItemId } from '../watermark-store.js'
|
||||
|
||||
/**
|
||||
* 管道 C1:成本快照(唯一入口)。
|
||||
@@ -157,6 +158,7 @@ export type SyncCostSnapshotsResult = {
|
||||
created: number
|
||||
missing: number
|
||||
lastId: number
|
||||
nextCursor: number
|
||||
}
|
||||
|
||||
export async function syncChannelCostSnapshots(
|
||||
@@ -164,14 +166,24 @@ export async function syncChannelCostSnapshots(
|
||||
): Promise<SyncCostSnapshotsResult> {
|
||||
const batchSize = Math.min(5000, Math.max(1, Math.trunc(options.batchSize || 500)))
|
||||
const maxBatches = Math.min(200, Math.max(1, Math.trunc(options.maxBatches || 20)))
|
||||
let cursor = Math.max(0, Math.trunc(options.afterId || 0))
|
||||
let cursor =
|
||||
options.afterId != null
|
||||
? Math.max(0, Math.trunc(options.afterId))
|
||||
: await readWatermarkItemId('cost_snapshot')
|
||||
let reachedEnd = false
|
||||
|
||||
const costItems = await loadCostItems()
|
||||
const versions = await loadActiveVersions()
|
||||
const pricingModeByItemId = new Map<number, string>()
|
||||
for (const item of costItems.values()) pricingModeByItemId.set(item.id, item.pricing_mode)
|
||||
|
||||
const result: SyncCostSnapshotsResult = { scanned: 0, created: 0, missing: 0, lastId: cursor }
|
||||
const result: SyncCostSnapshotsResult = {
|
||||
scanned: 0,
|
||||
created: 0,
|
||||
missing: 0,
|
||||
lastId: cursor,
|
||||
nextCursor: cursor,
|
||||
}
|
||||
|
||||
for (let batch = 0; batch < maxBatches; batch += 1) {
|
||||
const rows = await query<CostCandidateItem>(
|
||||
@@ -198,6 +210,11 @@ export async function syncChannelCostSnapshots(
|
||||
oi.canonical_product_id IS NOT NULL
|
||||
OR (oi.fulfillment_channel = 'work_order' AND wo.product_rule_id IS NOT NULL)
|
||||
)
|
||||
-- 只处理还没有有效快照的订单项,避免每轮重复解析整张表。
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM cost_snapshots cs
|
||||
WHERE cs.order_item_id = oi.id AND cs.superseded_at IS NULL
|
||||
)
|
||||
AND oi.id > $1
|
||||
ORDER BY oi.id
|
||||
LIMIT $2
|
||||
@@ -205,7 +222,11 @@ export async function syncChannelCostSnapshots(
|
||||
[cursor, batchSize],
|
||||
)
|
||||
|
||||
if (rows.rows.length === 0) break
|
||||
if (rows.rows.length === 0) {
|
||||
reachedEnd = true
|
||||
break
|
||||
}
|
||||
if (rows.rows.length < batchSize) reachedEnd = true
|
||||
result.scanned += rows.rows.length
|
||||
cursor = Number(rows.rows[rows.rows.length - 1]!.id)
|
||||
result.lastId = cursor
|
||||
@@ -236,6 +257,7 @@ export async function syncChannelCostSnapshots(
|
||||
await resolveExceptionsForItems(inserts.map((row) => row.item.id))
|
||||
}
|
||||
|
||||
result.nextCursor = reachedEnd ? 0 : cursor
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { query } from '../../db/client.js'
|
||||
import { resolveExceptionsForItems, upsertAggregatedException } from './cost-exception-service.js'
|
||||
import { readWatermarkItemId } from '../watermark-store.js'
|
||||
|
||||
/**
|
||||
* 管道 B:商品归一。
|
||||
@@ -171,6 +172,7 @@ export type NormalizeProductsResult = {
|
||||
matched: number
|
||||
unmatched: number
|
||||
lastId: number
|
||||
nextCursor: number
|
||||
}
|
||||
|
||||
export async function normalizeOrderItemProducts(
|
||||
@@ -178,10 +180,20 @@ export async function normalizeOrderItemProducts(
|
||||
): Promise<NormalizeProductsResult> {
|
||||
const batchSize = Math.min(5000, Math.max(1, Math.trunc(options.batchSize || 500)))
|
||||
const maxBatches = Math.min(200, Math.max(1, Math.trunc(options.maxBatches || 20)))
|
||||
let cursor = Math.max(0, Math.trunc(options.afterId || 0))
|
||||
let cursor =
|
||||
options.afterId != null
|
||||
? Math.max(0, Math.trunc(options.afterId))
|
||||
: await readWatermarkItemId('product_normalize')
|
||||
let reachedEnd = false
|
||||
|
||||
const index = await loadMappingIndex()
|
||||
const result: NormalizeProductsResult = { scanned: 0, matched: 0, unmatched: 0, lastId: cursor }
|
||||
const result: NormalizeProductsResult = {
|
||||
scanned: 0,
|
||||
matched: 0,
|
||||
unmatched: 0,
|
||||
lastId: cursor,
|
||||
nextCursor: cursor,
|
||||
}
|
||||
|
||||
for (let batch = 0; batch < maxBatches; batch += 1) {
|
||||
const rows = await query<ItemRow>(
|
||||
@@ -211,7 +223,11 @@ export async function normalizeOrderItemProducts(
|
||||
[cursor, batchSize],
|
||||
)
|
||||
|
||||
if (rows.rows.length === 0) break
|
||||
if (rows.rows.length === 0) {
|
||||
reachedEnd = true
|
||||
break
|
||||
}
|
||||
if (rows.rows.length < batchSize) reachedEnd = true
|
||||
result.scanned += rows.rows.length
|
||||
cursor = Number(rows.rows[rows.rows.length - 1]!.id)
|
||||
result.lastId = cursor
|
||||
@@ -249,6 +265,7 @@ export async function normalizeOrderItemProducts(
|
||||
}
|
||||
}
|
||||
|
||||
result.nextCursor = reachedEnd ? 0 : cursor
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user