import { reactive } from 'vue'; import type { QualityOverviewData, ObservationState } from '@/api/datagov/quality-rule/observations'; import type { CatalogOverviewData } from '@/api/datagov/asset-catalog/overview'; export const aggregateScenario = reactive({ fail: false, empty: false, reads: 0 }); const names = ['订单主键唯一性', '客户编号完整性', '订单金额取值范围', '每日订单量', '商品编号唯一性', '支付时间完整性', '退款金额范围', '渠道编码完整性', '客户维度重复校验', '库存数量范围', '物流单号完整性', '商品日快照行数']; export async function getQualityOverview(params: { from?: string; to?: string; after?: string }) { aggregateScenario.reads++; if (aggregateScenario.fail) { aggregateScenario.fail = false; throw new Error('合成场景:读取失败,请刷新重试'); } const from = params.from || '2026-10-08', to = params.to || '2026-10-21'; const base = params.after && params.after !== '0' ? 12 : 0; const rules = aggregateScenario.empty ? [] : (base ? ['会员等级完整性', '成本中心唯一性', '营销转化范围'] : names).map((ruleName, i) => ({ ruleId: String(9007199254740993n + BigInt(base + i)), ruleName, currentTarget: ['dwd.fact_orders', 'dim.customer', 'dwd.fact_payment', 'dws.inventory_daily'][i % 4], currentDataSourceId: '9007199254741001', currentColumn: i === 3 ? undefined : 'id', version: 3 })); const cells: QualityOverviewData['cells'] = []; rules.forEach((rule, i) => { const start = Date.parse(from + 'T00:00:00Z'), end = Date.parse(to + 'T00:00:00Z'); for (let time = start, d = 0; time <= end && d < 31; time += 86400000, d++) { if ((i === 10 && d < 5) || (i === 11 && d > 10)) continue; const state: ObservationState = i === 1 && d === 13 ? 'UNKNOWN' : i === 8 && d < 4 ? 'LEGACY' : i === 5 && d === 12 ? 'EXECUTION_FAILED' : (i === 2 || i === 6) && d > 8 ? 'THRESHOLD_FAILED' : 'PASSED'; const date = new Date(time).toISOString().slice(0, 10); const latest = { validationId: String(100000 + i * 100 + d), snapshotId: '9007199254741999', configSha256: (d > 8 ? 'a' : 'b').repeat(64), state, attempts: d === 13 ? 3 : 1, method: i === 3 ? 'ROW_COUNT' : 'NOT_NULL', unit: i === 3 ? '行数' : '违规记录数', value: state === 'UNKNOWN' || state === 'EXECUTION_FAILED' ? undefined : i === 3 ? 12843 : state === 'THRESHOLD_FAILED' ? 27 : 0, evidenceRef: state === 'UNKNOWN' ? undefined : 'fixture/validation/semantics/v1', errorCode: state === 'THRESHOLD_FAILED' ? 'QUALITY_RULE_THRESHOLD_NOT_MET' : state === 'EXECUTION_FAILED' ? 'SOURCE_UNAVAILABLE' : undefined, observedAt: state === 'UNKNOWN' ? undefined : date + ' 09:30:00', requestedAt: date + ' 09:29:00' }; if (i === 8 && d < 4) { latest.value = undefined; latest.method = 'UNKNOWN'; latest.unit = '单位未声明'; latest.evidenceRef = 'fixture/legacy'; } const configurations = [latest]; if (d === 9) configurations.push({ ...latest, validationId: String(80000 + i), snapshotId: '9007199254741998', configSha256: 'b'.repeat(64), state: 'PASSED', attempts: 2, value: 0, errorCode: undefined, observedAt: date + ' 08:30:00', requestedAt: date + ' 08:29:00' }); const unverified = i === 8 && d < 4, historical = i === 0 && d < 8; const cell: QualityOverviewData['cells'][number] = { ...latest, ruleId: rule.ruleId, date, attempts: configurations.reduce((sum, item) => sum + item.attempts, 0), versionChanged: d === 9, targetKey: unverified ? 'unverified' : historical ? 'old-orders' : 'current-' + i, targetStatus: unverified ? 'UNVERIFIED' : 'VERIFIED', target: unverified ? undefined : historical ? 'ods.order_archive' : rule.currentTarget, targetDataSourceId: unverified ? undefined : rule.currentDataSourceId, targetColumn: unverified ? undefined : rule.currentColumn, configurations }; cells.push(cell); if (i === 0 && d === 8) { const prior = { ...latest, validationId: '79999', snapshotId: '9007199254741997', state: 'THRESHOLD_FAILED' as const, value: 18, observedAt: date + ' 08:00:00', requestedAt: date + ' 07:59:00', errorCode: 'QUALITY_RULE_THRESHOLD_NOT_MET' }; cells.push({ ...cell, ...prior, targetKey: 'old-orders', target: 'ods.order_archive', configurations: [prior] }); } } }); return { data: { scopeHash: 'c'.repeat(64), from, to, dateBasis: 'OBSERVED_AT_ELSE_REQUESTED_AT', timezone: 'UNDECLARED', aggregationBasis: 'RULE_FROZEN_TARGET_DAY_V1', readAt: '2026-10-22T02:00:00Z', rules, cells, nextCursor: base || aggregateScenario.empty ? undefined : rules.at(-1)?.ruleId, truncated: !base && !aggregateScenario.empty } satisfies QualityOverviewData }; } export async function getCatalogOverview() { aggregateScenario.reads++; if (aggregateScenario.fail) { aggregateScenario.fail = false; throw new Error('合成场景:读取失败,请刷新重试'); } const groups = aggregateScenario.empty ? [] : ['交易域', '客户域', '商品域', '财务域', '营销域', '供应链域', '运营指标', '共享维度', '待建设目录'].map((name, i) => ({ catalogId: String(9007199254740993n + BigInt(i)), name, status: i < 6 ? 'PUBLISHED' : 'DRAFT', types: i === 8 ? [] : ['TABLE', 'DATASET', 'FILE', 'REPORT', 'API', 'MODEL'].map((type, j) => ({ type, reliable: Math.max(0, 140 - i * 14 - j * 18), uncertain: (i + j) % 4 === 0 ? 8 : 0 })) })); return { data: { scopeHash: 'd'.repeat(64), readAt: '2026-10-21T02:00:00Z', lastRegistrationUpdate: '2026-10-21 09:42:00', countKind: 'REGISTRATION', truncated: false, groups } satisfies CatalogOverviewData }; }