perf: 筛选统计去掉 JSON 兜底,并补上 orders 覆盖索引

去掉 work_orders 堆访问后,JOIN 会把 orders 整表扫进 Hash Right Join。
领取页在快照新鲜时不再打上游;快手出网统一 10 秒超时。倒计时改为自适应间隔。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-10-01 15:05:38 +08:00
co-authored by Cursor
parent f4c405d51a
commit 1239393fc2
20 changed files with 404 additions and 65 deletions
@@ -2,8 +2,11 @@ import test from 'node:test'
import assert from 'node:assert/strict'
import {
AFFILIATE_DASH_BIND_REFRESH_FRESH_MS,
AFFILIATE_DASH_ORDER_SYNC_FRESH_MS,
canReuseAffiliateDashBindSnapshot,
isAffiliateDashBindSnapshotStale,
isAffiliateDashSnapshotFresh,
shouldRefreshAffiliateDashBindStateForSnapshot,
} from './kuaishou-cloud-claim-service.js'
@@ -138,6 +141,84 @@ test('canReuseAffiliateDashBindSnapshot refuses mismatched bound account', () =>
)
})
test('isAffiliateDashSnapshotFresh treats missing or future timestamps as stale', () => {
const now = Date.parse('2026-10-01T06:00:00.000Z')
assert.equal(isAffiliateDashSnapshotFresh(null, AFFILIATE_DASH_ORDER_SYNC_FRESH_MS, now), false)
assert.equal(isAffiliateDashSnapshotFresh('', AFFILIATE_DASH_ORDER_SYNC_FRESH_MS, now), false)
assert.equal(
isAffiliateDashSnapshotFresh('not-a-date', AFFILIATE_DASH_ORDER_SYNC_FRESH_MS, now),
false,
)
assert.equal(
isAffiliateDashSnapshotFresh(
'2026-10-01T06:00:01.000Z',
AFFILIATE_DASH_ORDER_SYNC_FRESH_MS,
now,
),
false,
)
})
test('order snapshot stays fresh for 45s and bind snapshot only for 15s', () => {
const now = Date.parse('2026-10-01T06:00:45.000Z')
assert.equal(
isAffiliateDashSnapshotFresh(
'2026-10-01T06:00:00.000Z',
AFFILIATE_DASH_ORDER_SYNC_FRESH_MS,
now,
),
false,
)
assert.equal(
isAffiliateDashSnapshotFresh(
'2026-10-01T06:00:01.000Z',
AFFILIATE_DASH_ORDER_SYNC_FRESH_MS,
now,
),
true,
)
assert.equal(
isAffiliateDashSnapshotFresh(
'2026-10-01T06:00:30.000Z',
AFFILIATE_DASH_BIND_REFRESH_FRESH_MS,
now,
),
false,
)
assert.equal(
isAffiliateDashSnapshotFresh(
'2026-10-01T06:00:31.000Z',
AFFILIATE_DASH_BIND_REFRESH_FRESH_MS,
now,
),
true,
)
})
test('claim detail skips affiliate-dash sync while the snapshot is fresh', async () => {
const { readFile } = await import('node:fs/promises')
const src = await readFile(new URL('./kuaishou-cloud-claim-service.ts', import.meta.url), 'utf8')
const detail = src.slice(
src.indexOf('export async function getKuaishouCloudClaimDetail'),
src.indexOf('export async function submitClaimUid'),
)
assert.ok(detail.includes('flow.mock?.enabled'))
assert.ok(detail.includes('isAffiliateDashSnapshotFresh(flow.lastSyncedAt'))
assert.ok(detail.includes('bindFlow.lastBindSyncedAt'))
assert.ok(
!detail.includes('snapshotOnly'),
'新鲜快照必须直接不调用同步;snapshotOnly 仍会 updateTask',
)
const refresh = src.slice(
src.indexOf('async function refreshAffiliateDashBindState'),
src.indexOf('function normalizeUid'),
)
assert.ok(
refresh.includes('lastBindSyncedAt: now'),
'成功拉取绑定结果后才记时间,失败不能挡住下一次',
)
})
test('shouldRefreshAffiliateDashBindStateForSnapshot keeps polling before submit', () => {
assert.equal(
shouldRefreshAffiliateDashBindStateForSnapshot({
@@ -26,6 +26,26 @@ type ClaimDetailPayload = ReturnType<typeof buildClaimDetailPayload>
const RETIRED_EXECUTOR_KEYS = new Set(['kuaishou_feifei', 'kuaishou_ct_assisted'])
/**
* 订单状态轮询的新鲜窗口。
* 发货状态以 affiliate-dash notify 回调为主(notify-service 会调 syncAffiliateDashTaskStatus)。
* 领取页详情只在回调没到时做备份:窗口内不打上游、也不写库。
* 代价是回调丢失时,「已发货」最多晚这么久才出现在领取页上。
*/
export const AFFILIATE_DASH_ORDER_SYNC_FRESH_MS = 45_000
/** 绑定结果没有回调,扫码过程仍要靠轮询,但必须长于上游超时,避免 8 秒一轮必超时。 */
export const AFFILIATE_DASH_BIND_REFRESH_FRESH_MS = 15_000
export function isAffiliateDashSnapshotFresh(
syncedAt: unknown,
freshForMs: number,
now = Date.now(),
) {
const parsed = Date.parse(String(syncedAt ?? ''))
if (!Number.isFinite(parsed)) return false
return now - parsed >= 0 && now - parsed < freshForMs
}
function isRetiredExecutor(executorKey: unknown) {
return RETIRED_EXECUTOR_KEYS.has(String(executorKey || '').trim())
}
@@ -46,9 +66,25 @@ export async function getKuaishouCloudClaimDetail(token: unknown): Promise<Claim
}
if (executorKey === 'affiliate_dash') {
task = (await syncAffiliateDashTaskStatus(task)) || task
const flow = normalizeAffiliateDashFlow(parseTaskContextValue(task).affiliateDash)
// 新鲜则整段不调用。走同步函数的本地分支仍会 updateTask,8 秒一轮照样写 WAL。
// dev-mock 不打上游,但任务状态要靠这次调用从快照落下来,不能被新鲜窗口跳过。
if (
flow.mock?.enabled ||
!isAffiliateDashSnapshotFresh(flow.lastSyncedAt, AFFILIATE_DASH_ORDER_SYNC_FRESH_MS)
) {
task = (await syncAffiliateDashTaskStatus(task)) || task
}
if (shouldRefreshAffiliateDashBindState(task)) {
task = (await refreshAffiliateDashBindState(task)) || task
const bindFlow = normalizeAffiliateDashFlow(parseTaskContextValue(task).affiliateDash)
if (
!isAffiliateDashSnapshotFresh(
bindFlow.lastBindSyncedAt,
AFFILIATE_DASH_BIND_REFRESH_FRESH_MS,
)
) {
task = (await refreshAffiliateDashBindState(task)) || task
}
}
}
@@ -564,6 +600,7 @@ async function refreshAffiliateDashBindState(task: TaskRow): Promise<TaskRow | n
expectedGameAccount &&
normalizeUid(boundAccount) !== normalizeUid(expectedGameAccount),
)
const now = nowIso()
const nextFlow = {
...flow,
bindUuid: result.bindUuid || flow.bindUuid,
@@ -580,8 +617,8 @@ async function refreshAffiliateDashBindState(task: TaskRow): Promise<TaskRow | n
? String(result.gameChannel || flow.gameChannel || '').trim()
: flow.gameChannel,
bindMismatch: Boolean(result.mismatch) || localMismatch,
lastBindSyncedAt: now,
}
const now = nowIso()
return (
(await updateTask(task.id, {