@yuants/app-virtual-exchange 0.9.4 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/position.js +17 -0
  2. package/dist/position.js.map +1 -1
  3. package/dist/quote/__tests__/implementations.test.js +218 -0
  4. package/dist/quote/__tests__/implementations.test.js.map +1 -0
  5. package/dist/quote/implementations/v0.js +7 -9
  6. package/dist/quote/implementations/v0.js.map +1 -1
  7. package/dist/quote/implementations/v1.js +10 -4
  8. package/dist/quote/implementations/v1.js.map +1 -1
  9. package/dist/quote/service.js +103 -30
  10. package/dist/quote/service.js.map +1 -1
  11. package/dist/quote/types.js.map +1 -1
  12. package/dist/quote/upstream/executor.js +98 -0
  13. package/dist/quote/upstream/executor.js.map +1 -0
  14. package/dist/quote/upstream/index.js +2 -0
  15. package/dist/quote/upstream/index.js.map +1 -0
  16. package/dist/quote/upstream/prefix-matcher.js.map +1 -0
  17. package/dist/quote/upstream/registry.js +116 -0
  18. package/dist/quote/upstream/registry.js.map +1 -0
  19. package/dist/quote/upstream/router.js +119 -0
  20. package/dist/quote/upstream/router.js.map +1 -0
  21. package/lib/position.d.ts.map +1 -1
  22. package/lib/position.js +17 -0
  23. package/lib/position.js.map +1 -1
  24. package/lib/quote/__tests__/implementations.test.d.ts +2 -0
  25. package/lib/quote/__tests__/implementations.test.d.ts.map +1 -0
  26. package/lib/quote/__tests__/implementations.test.js +220 -0
  27. package/lib/quote/__tests__/implementations.test.js.map +1 -0
  28. package/lib/quote/implementations/v0.d.ts.map +1 -1
  29. package/lib/quote/implementations/v0.js +7 -9
  30. package/lib/quote/implementations/v0.js.map +1 -1
  31. package/lib/quote/implementations/v1.d.ts.map +1 -1
  32. package/lib/quote/implementations/v1.js +10 -4
  33. package/lib/quote/implementations/v1.js.map +1 -1
  34. package/lib/quote/service.js +102 -29
  35. package/lib/quote/service.js.map +1 -1
  36. package/lib/quote/types.d.ts +18 -0
  37. package/lib/quote/types.d.ts.map +1 -1
  38. package/lib/quote/types.js.map +1 -1
  39. package/lib/quote/upstream/executor.d.ts +8 -0
  40. package/lib/quote/upstream/executor.d.ts.map +1 -0
  41. package/lib/quote/upstream/executor.js +102 -0
  42. package/lib/quote/upstream/executor.js.map +1 -0
  43. package/lib/quote/upstream/index.d.ts +3 -0
  44. package/lib/quote/upstream/index.d.ts.map +1 -0
  45. package/lib/quote/upstream/index.js +6 -0
  46. package/lib/quote/upstream/index.js.map +1 -0
  47. package/lib/quote/upstream/prefix-matcher.d.ts.map +1 -0
  48. package/lib/quote/upstream/prefix-matcher.js.map +1 -0
  49. package/lib/quote/upstream/registry.d.ts +18 -0
  50. package/lib/quote/upstream/registry.d.ts.map +1 -0
  51. package/lib/quote/upstream/registry.js +120 -0
  52. package/lib/quote/upstream/registry.js.map +1 -0
  53. package/lib/quote/upstream/router.d.ts +27 -0
  54. package/lib/quote/upstream/router.d.ts.map +1 -0
  55. package/lib/quote/upstream/router.js +124 -0
  56. package/lib/quote/upstream/router.js.map +1 -0
  57. package/package.json +3 -3
  58. package/temp/package-deps.json +17 -13
  59. package/dist/quote/prefix-matcher.js.map +0 -1
  60. package/dist/quote/request-key.js +0 -20
  61. package/dist/quote/request-key.js.map +0 -1
  62. package/dist/quote/upstream-routing.js +0 -300
  63. package/dist/quote/upstream-routing.js.map +0 -1
  64. package/lib/quote/prefix-matcher.d.ts.map +0 -1
  65. package/lib/quote/prefix-matcher.js.map +0 -1
  66. package/lib/quote/request-key.d.ts +0 -2
  67. package/lib/quote/request-key.d.ts.map +0 -1
  68. package/lib/quote/request-key.js +0 -24
  69. package/lib/quote/request-key.js.map +0 -1
  70. package/lib/quote/upstream-routing.d.ts +0 -15
  71. package/lib/quote/upstream-routing.d.ts.map +0 -1
  72. package/lib/quote/upstream-routing.js +0 -304
  73. package/lib/quote/upstream-routing.js.map +0 -1
  74. /package/dist/quote/{prefix-matcher.js → upstream/prefix-matcher.js} +0 -0
  75. /package/lib/quote/{prefix-matcher.d.ts → upstream/prefix-matcher.d.ts} +0 -0
  76. /package/lib/quote/{prefix-matcher.js → upstream/prefix-matcher.js} +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/quote/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,0BAA0B,EAAc,MAAM,oBAAoB,CAAC;AAE5E,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;AAEtC,MAAM,wBAAwB,GAAG,CAC/B,IAAwB,EACxB,MAA0E,EAC1E,EAAE;;IACF,OAAO,CAAC,IAAI,CACV,kEAAkE,EAClE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EACtB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CACrB,CAAC;IACF,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IACnD,MAAM,WAAW,GAAoD,EAAE,CAAC;IACxE,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;QACpC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;YAC1B,IAAI,CAAC,CAAA,MAAA,IAAI,CAAC,UAAU,CAAC,0CAAG,KAAK,CAAC,CAAA,EAAE;gBAC9B,WAAW,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;aACzC;SACF;KACF;IACD,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE;QAC1B,MAAM,QAAQ,CAAC,mCAAmC,EAAE;YAClD,UAAU;YACV,MAAM,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;YACjC,YA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,EAAE,YAAY,CAAC;IACjD,UAAU,EAAE;QACV,WAAW,EAAE;YACX,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC1B;QACD,MAAM,EAAE;YACN,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC1B;QACD,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC/B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC;IAEpD,MAAM,WAAW,GAAG,kBAAkB,CAAC,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;IACpF,MAAM,0BAA0B,CAAC,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;IAEpF,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;IAChE,wBAAwB,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;AACnD,CAAC,CACF,CAAC","sourcesContent":["import { Terminal } from '@yuants/protocol';\nimport { newError } from '@yuants/utils';\nimport { createQuoteState } from './state';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from './types';\nimport { fillQuoteStateFromUpstream, IQuoteMiss } from './upstream-routing';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst quoteState = createQuoteState();\n\nconst assertFreshnessSatisfied = (\n data: IQuoteUpdateAction,\n params: { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n) => {\n console.info(\n '[VEX][Quote] Asserting freshness satisfied for requested quotes.',\n JSON.stringify(params),\n JSON.stringify(data),\n );\n const { product_ids, fields, updated_at } = params;\n const stillMissed: Array<{ product_id: string; field: IQuoteKey }> = [];\n for (const product_id of product_ids) {\n for (const field of fields) {\n if (!data[product_id]?.[field]) {\n stillMissed.push({ product_id, field });\n }\n }\n }\n if (stillMissed.length > 0) {\n throw newError('VEX_QUOTE_FRESHNESS_NOT_SATISFIED', {\n updated_at,\n missed: stillMissed.slice(0, 200),\n missed_total: stillMissed.length,\n });\n }\n};\n\nterminal.server.provideService<IQuoteUpdateAction>('VEX/UpdateQuotes', {}, async (msg) => {\n quoteState.update(msg.req);\n return { res: { code: 0, message: 'OK' } };\n});\n\nterminal.server.provideService<{}, IQuoteUpdateAction>('VEX/DumpQuoteState', {}, async () => {\n return { res: { code: 0, message: 'OK', data: quoteState.dumpAsObject() } };\n});\n\nconst computeCacheMissed = (\n quoteState: IQuoteState,\n product_ids: string[],\n fields: IQuoteKey[],\n updated_at: number,\n): IQuoteMiss[] => {\n const cacheMissed: IQuoteMiss[] = [];\n for (const product_id of product_ids) {\n for (const field of fields) {\n const tuple = quoteState.getValueTuple(product_id, field);\n if (tuple === undefined || tuple[1] < updated_at) {\n cacheMissed.push({ product_id, field });\n }\n }\n }\n return cacheMissed;\n};\n\nterminal.server.provideService<\n { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n IQuoteUpdateAction\n>(\n 'VEX/QueryQuotes',\n {\n type: 'object',\n required: ['product_ids', 'fields', 'updated_at'],\n properties: {\n product_ids: {\n type: 'array',\n items: { type: 'string' },\n },\n fields: {\n type: 'array',\n items: { type: 'string' },\n },\n updated_at: { type: 'number' },\n },\n },\n async (msg) => {\n const { product_ids, fields, updated_at } = msg.req;\n\n const cacheMissed = computeCacheMissed(quoteState, product_ids, fields, updated_at);\n await fillQuoteStateFromUpstream({ terminal, quoteState, cacheMissed, updated_at });\n\n const data = quoteState.filter(product_ids, fields, updated_at);\n assertFreshnessSatisfied(data, { product_ids, fields, updated_at });\n return { res: { code: 0, message: 'OK', data } };\n },\n);\n"]}
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/quote/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAEzD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;AACtC,MAAM,qBAAqB,GAAG,2BAA2B,CAAC,QAAQ,CAAC,CAAC;AAgBpE,MAAM,gBAAgB,GAAG,CAAC,MAAgB,EAAE,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AAC3E,MAAM,eAAe,GAAG,CAAC,MAAmB,EAAE,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AAE7E,MAAM,sBAAsB,GAAG,CAC7B,UAAuB,EACvB,WAAqB,EACrB,MAAmB,EACnB,UAAkB,EACyC,EAAE;IAC7D,MAAM,OAAO,GAAoB,EAAE,CAAC;IACpC,MAAM,UAAU,GAAoB,EAAE,CAAC;IACvC,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;QACpC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;YAC1B,MAAM,KAAK,GAAG,UAAU,CAAC,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;YAC1D,IAAI,KAAK,KAAK,SAAS,EAAE;gBACvB,OAAO,CAAC,IAAI,CAA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E;QACzB,iBAAiB,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;KACxD;IAED,MAAM,IAAI,GAAG,YAAY,CAAC,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC;IAC3D,gBAAgB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;IACtC,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC;AACnD,CAAC,CACF,CAAC;AAEF,QAAQ,CAAC,MAAM,CAAC,cAAc,CAA8B,4BAA4B,EAAE,EAAE,EAAE,KAAK,IAAI,EAAE;IACvG,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC;AACrE,CAAC,CAAC,CAAC","sourcesContent":["import { Terminal } from '@yuants/protocol';\nimport { formatTime, newError } from '@yuants/utils';\nimport { Subject, concatMap, defer } from 'rxjs';\nimport { createQuoteState } from './state';\nimport { IQuoteKey, IQuoteRequire, IQuoteState, IQuoteUpdateAction } from './types';\nimport { createQuoteProviderRegistry } from './upstream';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst quoteState = createQuoteState();\nconst quoteProviderRegistry = createQuoteProviderRegistry(terminal);\n\ntype UpdateTask = { product_ids: string[]; fields: IQuoteKey[]; updated_at: number };\n\ntype IQuoteUpdateQueueStatus = {\n pending: number;\n in_flight: number;\n queued_total: number;\n started_total: number;\n processed_total: number;\n last_enqueued_at?: number;\n last_started_at?: number;\n last_processed_at?: number;\n last_error?: { at: number; code?: string; message?: string };\n};\n\nconst normalizeStrings = (values: string[]) => [...new Set(values)].sort();\nconst normalizeFields = (values: IQuoteKey[]) => [...new Set(values)].sort();\n\nconst analyzeRequestedQuotes = (\n quoteState: IQuoteState,\n product_ids: string[],\n fields: IQuoteKey[],\n updated_at: number,\n): { missing: IQuoteRequire[]; needUpdate: IQuoteRequire[] } => {\n const missing: IQuoteRequire[] = [];\n const needUpdate: IQuoteRequire[] = [];\n for (const product_id of product_ids) {\n for (const field of fields) {\n const tuple = quoteState.getValueTuple(product_id, field);\n if (tuple === undefined) {\n missing.push({ product_id, field });\n needUpdate.push({ product_id, field });\n continue;\n }\n if (tuple[1] < updated_at) {\n needUpdate.push({ product_id, field });\n }\n }\n }\n return { missing, needUpdate };\n};\n\nconst filterLatest = (\n quoteState: IQuoteState,\n product_ids: string[],\n fields: IQuoteKey[],\n): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of product_ids) {\n result[product_id] = {};\n for (const field of fields) {\n const tuple = quoteState.getValueTuple(product_id, field);\n if (tuple) {\n result[product_id][field] = tuple;\n }\n }\n }\n return result;\n};\n\nconst assertNotMissing = (missing: IQuoteRequire[], updated_at: number) => {\n if (missing.length > 0) {\n throw newError('VEX_QUOTE_FRESHNESS_NOT_SATISFIED', {\n updated_at,\n missed: missing.slice(0, 200),\n missed_total: missing.length,\n });\n }\n};\n\nconst updateQueue$ = new Subject<UpdateTask>();\nconst updateQueueStats: Omit<IQuoteUpdateQueueStatus, 'pending' | 'in_flight'> = {\n queued_total: 0,\n started_total: 0,\n processed_total: 0,\n};\n\nconst getQueueStatus = (): IQuoteUpdateQueueStatus => {\n const pending = updateQueueStats.queued_total - updateQueueStats.started_total;\n const in_flight = updateQueueStats.started_total - updateQueueStats.processed_total;\n return {\n pending,\n in_flight,\n ...updateQueueStats,\n };\n};\n\nconst enqueueUpdateTask = (task: UpdateTask) => {\n updateQueueStats.queued_total++;\n updateQueueStats.last_enqueued_at = Date.now();\n updateQueue$.next(task);\n};\n\nconst summarizeError = (error: unknown): { code?: string; message?: string } => {\n if (typeof error === 'object' && error !== null) {\n const code = 'code' in error ? (error as any).code : undefined;\n const message = 'message' in error ? (error as any).message : undefined;\n return {\n code: typeof code === 'string' ? code : undefined,\n message: typeof message === 'string' ? message : undefined,\n };\n }\n return {};\n};\n\nconst processUpdateTask = async (task: UpdateTask) => {\n const { needUpdate } = analyzeRequestedQuotes(quoteState, task.product_ids, task.fields, task.updated_at);\n await quoteProviderRegistry.fillQuoteStateFromUpstream({\n quoteState,\n cacheMissed: needUpdate,\n updated_at: task.updated_at,\n });\n};\n\nupdateQueue$\n .pipe(\n concatMap((task) =>\n defer(async () => {\n updateQueueStats.started_total++;\n updateQueueStats.last_started_at = Date.now();\n\n try {\n await processUpdateTask(task);\n } catch (error) {\n const summary = summarizeError(error);\n updateQueueStats.last_error = { at: Date.now(), ...summary };\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote]UpdateQueueTaskFailed`,\n `product_ids=${task.product_ids.length} fields=${task.fields.length} updated_at=${task.updated_at}`,\n JSON.stringify(summary),\n );\n } finally {\n updateQueueStats.processed_total++;\n updateQueueStats.last_processed_at = Date.now();\n }\n }),\n ),\n )\n .subscribe();\n\nterminal.server.provideService<IQuoteUpdateAction>('VEX/UpdateQuotes', {}, async (msg) => {\n quoteState.update(msg.req);\n return { res: { code: 0, message: 'OK' } };\n});\n\nterminal.server.provideService<{}, IQuoteUpdateAction>('VEX/DumpQuoteState', {}, async () => {\n return { res: { code: 0, message: 'OK', data: quoteState.dumpAsObject() } };\n});\n\nterminal.server.provideService<\n { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n IQuoteUpdateAction\n>(\n 'VEX/QueryQuotes/C1',\n {\n type: 'object',\n required: ['product_ids', 'fields', 'updated_at'],\n properties: {\n product_ids: {\n type: 'array',\n items: { type: 'string' },\n },\n fields: {\n type: 'array',\n items: { type: 'string' },\n },\n updated_at: { type: 'number' },\n },\n },\n async (msg) => {\n const product_ids = normalizeStrings(msg.req.product_ids);\n const fields = normalizeFields(msg.req.fields);\n const { updated_at } = msg.req;\n\n const { missing, needUpdate } = analyzeRequestedQuotes(quoteState, product_ids, fields, updated_at);\n if (needUpdate.length > 0) {\n enqueueUpdateTask({ product_ids, fields, updated_at });\n }\n\n const data = filterLatest(quoteState, product_ids, fields);\n assertNotMissing(missing, updated_at);\n return { res: { code: 0, message: 'OK', data } };\n },\n);\n\nterminal.server.provideService<{}, IQuoteUpdateQueueStatus>('VEX/QuoteUpdateQueueStatus', {}, async () => {\n return { res: { code: 0, message: 'OK', data: getQueueStatus() } };\n});\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quote/types.ts"],"names":[],"mappings":"","sourcesContent":["import { IQuote } from '@yuants/data-quote';\n\nexport type IQuoteKey = Exclude<keyof IQuote, 'datasource_id' | 'product_id' | 'updated_at'>;\n\n/**\n * 用于批量更新的数据结构\n * 结构为:\n * product_id -> field_name (keyof IQuote) -> [value: string, updated_at: number]\n * 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间\n *\n * 例如:\n * ```json\n * {\n * \"product_123\": {\n * \"last_price\": [\"100.5\", 1627890123456],\n * \"volume\": [\"1500\", 1627890123456]\n * },\n * \"product_456\": {\n * \"last_price\": [\"200.0\", 1627890123456]\n * }\n * }\n * ```\n * @public\n */\nexport type IQuoteUpdateAction = Record<\n string,\n Partial<Record<IQuoteKey, [value: string, updated_at: number]>>\n>;\nexport interface IQuoteState {\n update: (action: IQuoteUpdateAction) => void;\n dumpAsObject: () => IQuoteUpdateAction;\n getValueTuple: (product_id: string, field: IQuoteKey) => [string, number] | undefined;\n filter: (product_ids: string[], fields: IQuoteKey[], updated_at: number) => IQuoteUpdateAction;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quote/types.ts"],"names":[],"mappings":"","sourcesContent":["import { IQuote } from '@yuants/data-quote';\nimport { IQuoteServiceMetadata } from '@yuants/exchange';\n\nexport type IQuoteKey = Exclude<keyof IQuote, 'datasource_id' | 'product_id' | 'updated_at'>;\n\n/**\n * 用于批量更新的数据结构\n * 结构为:\n * product_id -> field_name (keyof IQuote) -> [value: string, updated_at: number]\n * 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间\n *\n * 例如:\n * ```json\n * {\n * \"product_123\": {\n * \"last_price\": [\"100.5\", 1627890123456],\n * \"volume\": [\"1500\", 1627890123456]\n * },\n * \"product_456\": {\n * \"last_price\": [\"200.0\", 1627890123456]\n * }\n * }\n * ```\n * @public\n */\nexport type IQuoteUpdateAction = Record<\n string,\n Partial<Record<IQuoteKey, [value: string, updated_at: number]>>\n>;\nexport interface IQuoteState {\n update: (action: IQuoteUpdateAction) => void;\n dumpAsObject: () => IQuoteUpdateAction;\n getValueTuple: (product_id: string, field: IQuoteKey) => [string, number] | undefined;\n filter: (product_ids: string[], fields: IQuoteKey[], updated_at: number) => IQuoteUpdateAction;\n}\n\nexport interface IQuoteProviderInstance {\n terminal_id: string;\n service_id: string;\n}\n\n/**\n * A \"provider group\" is a capability signature of `GetQuotes`.\n * Multiple vendor terminals may provide the same capability; VEX load-balances across instances.\n */\nexport interface IQuoteProviderGroup {\n group_id: string;\n meta: IQuoteServiceMetadata;\n mapTerminalIdToInstance: Map<string, IQuoteProviderInstance>;\n}\n\nexport interface IQuoteRequire {\n product_id: string;\n field: IQuoteKey;\n}\n"]}
@@ -0,0 +1,98 @@
1
+ import { newError } from '@yuants/utils';
2
+ import { filter, firstValueFrom, map } from 'rxjs';
3
+ const createConcurrencyLimiter = (concurrency) => {
4
+ const queue = [];
5
+ let active = 0;
6
+ const next = () => {
7
+ if (active >= concurrency)
8
+ return;
9
+ const task = queue.shift();
10
+ if (!task)
11
+ return;
12
+ active++;
13
+ task();
14
+ };
15
+ return async (fn) => await new Promise((resolve, reject) => {
16
+ queue.push(async () => {
17
+ try {
18
+ resolve(await fn());
19
+ }
20
+ catch (e) {
21
+ reject(e);
22
+ }
23
+ finally {
24
+ active--;
25
+ next();
26
+ }
27
+ });
28
+ next();
29
+ });
30
+ };
31
+ const pickInstance = (params) => {
32
+ var _a;
33
+ const { group_id, instances, mapGroupIdToRoundRobinIndex } = params;
34
+ if (instances.length === 0)
35
+ throw newError('VEX_QUOTE_PROVIDER_INSTANCE_EMPTY', { group_id });
36
+ const nextIndex = ((_a = mapGroupIdToRoundRobinIndex.get(group_id)) !== null && _a !== void 0 ? _a : 0) % instances.length;
37
+ mapGroupIdToRoundRobinIndex.set(group_id, nextIndex + 1);
38
+ return instances[nextIndex];
39
+ };
40
+ const requestGetQuotes = async (params) => {
41
+ const { terminal, instance, req } = params;
42
+ const res = await firstValueFrom(terminal.client
43
+ .request('GetQuotes', instance.terminal_id, req, instance.service_id)
44
+ .pipe(map((msg) => msg.res), filter((v) => v !== undefined)));
45
+ if (res.code !== 0)
46
+ throw newError('VEX_QUOTE_PROVIDER_ERROR', { instance, res });
47
+ if (res.data === undefined)
48
+ throw newError('VEX_QUOTE_PROVIDER_DATA_MISSING', { instance, res });
49
+ return res.data;
50
+ };
51
+ const runWithProviderGroupConcurrencyLimit1 = async (params) => {
52
+ var _a;
53
+ const { group_id, mapGroupIdToTailPromise, fn } = params;
54
+ const prev = (_a = mapGroupIdToTailPromise.get(group_id)) !== null && _a !== void 0 ? _a : Promise.resolve();
55
+ let resolveCurrent = () => { };
56
+ const current = new Promise((resolve) => {
57
+ resolveCurrent = resolve;
58
+ });
59
+ mapGroupIdToTailPromise.set(group_id, prev.then(() => current));
60
+ await prev;
61
+ try {
62
+ return await fn();
63
+ }
64
+ finally {
65
+ resolveCurrent();
66
+ }
67
+ };
68
+ export const createGetQuotesExecutor = (terminal) => {
69
+ const mapGroupIdToRoundRobinIndex = new Map();
70
+ const mapGroupIdToTailPromise = new Map();
71
+ const limitGetQuotes = createConcurrencyLimiter(32);
72
+ const mapKeyToInFlightGetQuotesPromise = new Map();
73
+ const requestGetQuotesInFlight = (key, planned) => {
74
+ const existing = mapKeyToInFlightGetQuotesPromise.get(key);
75
+ if (existing)
76
+ return existing;
77
+ const promise = limitGetQuotes(() => runWithProviderGroupConcurrencyLimit1({
78
+ group_id: planned.group_id,
79
+ mapGroupIdToTailPromise,
80
+ fn: async () => {
81
+ const instance = pickInstance({
82
+ group_id: planned.group_id,
83
+ instances: planned.instances,
84
+ mapGroupIdToRoundRobinIndex,
85
+ });
86
+ return await requestGetQuotes({ terminal, instance, req: planned.req });
87
+ },
88
+ })).finally(() => {
89
+ mapKeyToInFlightGetQuotesPromise.delete(key);
90
+ });
91
+ mapKeyToInFlightGetQuotesPromise.set(key, promise);
92
+ return promise;
93
+ };
94
+ return {
95
+ execute: async (requests) => await Promise.all(requests.map(async ({ key, planned }) => await requestGetQuotesInFlight(key, planned))),
96
+ };
97
+ };
98
+ //# sourceMappingURL=executor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.js","sourceRoot":"","sources":["../../../src/quote/upstream/executor.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAMnD,MAAM,wBAAwB,GAAG,CAAC,WAAmB,EAAE,EAAE;IACvD,MAAM,KAAK,GAAsB,EAAE,CAAC;IACpC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,MAAM,IAAI,GAAG,GAAG,EAAE;QAChB,IAAI,MAAM,IAAI,WAAW;YAAE,OAAO;QAClC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;QAC3B,IAAI,CAAC,IAAI;YAAE,OAAO;QAClB,MAAM,EAAE,CAAC;QACT,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;IACF,OAAO,KAAK,EAAK,EAAoB,EAAc,EAAE,CACnD,MAAM,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACvC,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE;YACpB,IAAI;gBACF,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;aACrB;YAAC,OAAO,CAAC,EAAE;gBACV,MAAM,CAAC,CAAC,CAAC,CAAC;aACX;oBAAS;gBACR,MAAM,EAAE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IAAI,QAAQ;YAAE,OAAO,QAAQ,CAAC;QAC9B,MAAM,OAAO,GAAG,cAAc,CAAC,GAAG,EAAE,CAClC,qCAAqC,CAAC;YACpC,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,uBAAuB;YACvB,EAAE,EAAE,KAAK,IAAI,EAAE;gBACb,MAAM,QAAQ,GAAG,YAAY,CAAC;oBAC5B,QAAQ,EAAE,OAAO,CAAC,QAAQ;oBAC1B,SAAS,EAAE,OAAO,CAAC,SAAS;oBAC5B,2BAA2B;iBAC5B,CAAC,CAAC;gBACH,OAAO,MAAM,gBAAgB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;YAC1E,CAAC;SACF,CAAC,CACH,CAAC,OAAO,CAAC,GAAG,EAAE;YACb,gCAAgC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC/C,CAAC,CAAC,CAAC;QACH,gCAAgC,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACnD,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAEF,OAAO;QACL,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,CAC1B,MAAM,OAAO,CAAC,GAAG,CACf,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,MAAM,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CACvF;KACJ,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {\n IQuoteUpdateAction as IExchangeQuoteUpdateAction,\n IQuoteServiceRequestByVEX,\n} from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { newError } from '@yuants/utils';\nimport { filter, firstValueFrom, map } from 'rxjs';\nimport { IQuoteProviderInstance } from '../types';\nimport { IPlannedRequestWithKey } from './router';\n\ntype IPlannedRequest = IPlannedRequestWithKey['planned'];\n\nconst createConcurrencyLimiter = (concurrency: number) => {\n const queue: Array<() => void> = [];\n let active = 0;\n const next = () => {\n if (active >= concurrency) return;\n const task = queue.shift();\n if (!task) return;\n active++;\n task();\n };\n return async <T>(fn: () => Promise<T>): Promise<T> =>\n await new Promise<T>((resolve, reject) => {\n queue.push(async () => {\n try {\n resolve(await fn());\n } catch (e) {\n reject(e);\n } finally {\n active--;\n next();\n }\n });\n next();\n });\n};\n\nconst pickInstance = (params: {\n group_id: string;\n instances: IQuoteProviderInstance[];\n mapGroupIdToRoundRobinIndex: Map<string, number>;\n}): IQuoteProviderInstance => {\n const { group_id, instances, mapGroupIdToRoundRobinIndex } = params;\n if (instances.length === 0) throw newError('VEX_QUOTE_PROVIDER_INSTANCE_EMPTY', { group_id });\n const nextIndex = (mapGroupIdToRoundRobinIndex.get(group_id) ?? 0) % instances.length;\n mapGroupIdToRoundRobinIndex.set(group_id, nextIndex + 1);\n return instances[nextIndex];\n};\n\nconst requestGetQuotes = async (params: {\n terminal: Terminal;\n instance: IQuoteProviderInstance;\n req: IQuoteServiceRequestByVEX;\n}): Promise<IExchangeQuoteUpdateAction> => {\n const { terminal, instance, req } = params;\n const res = await firstValueFrom(\n terminal.client\n .request<IQuoteServiceRequestByVEX, IExchangeQuoteUpdateAction>(\n 'GetQuotes',\n instance.terminal_id,\n req,\n instance.service_id,\n )\n .pipe(\n map((msg) => msg.res),\n filter((v): v is Exclude<typeof v, undefined> => v !== undefined),\n ),\n );\n if (res.code !== 0) throw newError('VEX_QUOTE_PROVIDER_ERROR', { instance, res });\n if (res.data === undefined) throw newError('VEX_QUOTE_PROVIDER_DATA_MISSING', { instance, res });\n return res.data as any;\n};\n\nconst runWithProviderGroupConcurrencyLimit1 = async <T>(params: {\n group_id: string;\n mapGroupIdToTailPromise: Map<string, Promise<void>>;\n fn: () => Promise<T>;\n}): Promise<T> => {\n const { group_id, mapGroupIdToTailPromise, fn } = params;\n const prev = mapGroupIdToTailPromise.get(group_id) ?? Promise.resolve();\n let resolveCurrent: () => void = () => {};\n const current = new Promise<void>((resolve) => {\n resolveCurrent = resolve;\n });\n mapGroupIdToTailPromise.set(\n group_id,\n prev.then(() => current),\n );\n await prev;\n try {\n return await fn();\n } finally {\n resolveCurrent();\n }\n};\n\nexport interface IGetQuotesExecutor {\n execute: (requests: IPlannedRequestWithKey[]) => Promise<IExchangeQuoteUpdateAction[]>;\n}\n\nexport const createGetQuotesExecutor = (terminal: Terminal): IGetQuotesExecutor => {\n const mapGroupIdToRoundRobinIndex = new Map<string, number>();\n const mapGroupIdToTailPromise = new Map<string, Promise<void>>();\n const limitGetQuotes = createConcurrencyLimiter(32);\n\n const mapKeyToInFlightGetQuotesPromise = new Map<string, Promise<IExchangeQuoteUpdateAction>>();\n const requestGetQuotesInFlight = (key: string, planned: IPlannedRequest) => {\n const existing = mapKeyToInFlightGetQuotesPromise.get(key);\n if (existing) return existing;\n const promise = limitGetQuotes(() =>\n runWithProviderGroupConcurrencyLimit1({\n group_id: planned.group_id,\n mapGroupIdToTailPromise,\n fn: async () => {\n const instance = pickInstance({\n group_id: planned.group_id,\n instances: planned.instances,\n mapGroupIdToRoundRobinIndex,\n });\n return await requestGetQuotes({ terminal, instance, req: planned.req });\n },\n }),\n ).finally(() => {\n mapKeyToInFlightGetQuotesPromise.delete(key);\n });\n mapKeyToInFlightGetQuotesPromise.set(key, promise);\n return promise;\n };\n\n return {\n execute: async (requests) =>\n await Promise.all(\n requests.map(async ({ key, planned }) => await requestGetQuotesInFlight(key, planned)),\n ),\n };\n};\n"]}
@@ -0,0 +1,2 @@
1
+ export { createQuoteProviderRegistry } from './registry';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/quote/upstream/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { createQuoteProviderRegistry } from './registry';\nexport type { IQuoteProviderRegistry } from './registry';\n"]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix-matcher.js","sourceRoot":"","sources":["../../../src/quote/upstream/prefix-matcher.ts"],"names":[],"mappings":"AAIA,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,OAA4C,EACzB,EAAE;IACrB,MAAM,MAAM,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9E,OAAO;QACL,KAAK,EAAE,CAAC,KAAa,EAAO,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;KACpG,CAAC;AACJ,CAAC,CAAC","sourcesContent":["export interface IPrefixMatcher<T> {\n match: (value: string) => T[];\n}\n\nexport const createSortedPrefixMatcher = <T>(\n entries: Array<{ prefix: string; value: T }>,\n): IPrefixMatcher<T> => {\n const sorted = [...entries].sort((a, b) => b.prefix.length - a.prefix.length);\n return {\n match: (value: string): T[] => sorted.filter((x) => value.startsWith(x.prefix)).map((x) => x.value),\n };\n};\n"]}
@@ -0,0 +1,116 @@
1
+ import { parseQuoteServiceMetadataFromSchema } from '@yuants/exchange';
2
+ import { encodePath, formatTime, listWatch, newError } from '@yuants/utils';
3
+ import { EMPTY, map, of, tap } from 'rxjs';
4
+ import { createGetQuotesExecutor } from './executor';
5
+ import { buildProviderIndices, createQuoteRouter, } from './router';
6
+ const normalizeMetadataForGroup = (metadata) => {
7
+ const fields = [...metadata.fields].sort();
8
+ return Object.assign(Object.assign({}, metadata), { fields });
9
+ };
10
+ export const createQuoteProviderRegistry = (terminal) => {
11
+ const router = createQuoteRouter();
12
+ const executor = createGetQuotesExecutor(terminal);
13
+ const mapGroupIdToGroup = new Map();
14
+ let version = 0;
15
+ let cachedIndices;
16
+ const quoteServiceInfos$ = terminal.terminalInfos$.pipe(map((infos) => infos.flatMap((info) => {
17
+ var _a;
18
+ return Object.values((_a = info.serviceInfo) !== null && _a !== void 0 ? _a : {})
19
+ .filter((serviceInfo) => serviceInfo.method === 'GetQuotes')
20
+ .map((serviceInfo) => ({ terminal_id: info.terminal_id, serviceInfo }));
21
+ })));
22
+ quoteServiceInfos$
23
+ .pipe(listWatch((v) => v.serviceInfo.service_id, (v) => {
24
+ var _a, _b;
25
+ console.info(formatTime(Date.now()), `[VEX][Quote]DiscoveringGetQuotesProvider...`, `from terminal ${v.terminal_id}`, `service ${v.serviceInfo.service_id}`, `schema: ${JSON.stringify(v.serviceInfo.schema)}`);
26
+ try {
27
+ const metadata = normalizeMetadataForGroup(parseQuoteServiceMetadataFromSchema(v.serviceInfo.schema));
28
+ const group_id = encodePath(metadata.product_id_prefix, metadata.fields.join(','), (_a = metadata.max_products_per_request) !== null && _a !== void 0 ? _a : '');
29
+ const provider = {
30
+ terminal_id: v.terminal_id,
31
+ service_id: v.serviceInfo.service_id || v.serviceInfo.method,
32
+ };
33
+ const group = (_b = mapGroupIdToGroup.get(group_id)) !== null && _b !== void 0 ? _b : (() => {
34
+ const next = {
35
+ group_id,
36
+ meta: metadata,
37
+ mapTerminalIdToInstance: new Map(),
38
+ };
39
+ mapGroupIdToGroup.set(group_id, next);
40
+ return next;
41
+ })();
42
+ group.mapTerminalIdToInstance.set(provider.terminal_id, provider);
43
+ version++;
44
+ return of(void 0).pipe(tap({
45
+ unsubscribe: () => {
46
+ var _a, _b;
47
+ (_a = mapGroupIdToGroup.get(group_id)) === null || _a === void 0 ? void 0 : _a.mapTerminalIdToInstance.delete(v.terminal_id);
48
+ if (((_b = mapGroupIdToGroup.get(group_id)) === null || _b === void 0 ? void 0 : _b.mapTerminalIdToInstance.size) === 0) {
49
+ mapGroupIdToGroup.delete(group_id);
50
+ }
51
+ version++;
52
+ },
53
+ }));
54
+ }
55
+ catch (_c) {
56
+ console.info(formatTime(Date.now()), `[VEX][Quote]IgnoredGetQuotesProvider`, `Ignored GetQuotes provider from terminal ${v.terminal_id}`, `service ${v.serviceInfo.service_id} due to invalid schema.`);
57
+ return EMPTY;
58
+ }
59
+ }))
60
+ .subscribe();
61
+ const snapshot = () => {
62
+ const groups = Array.from(mapGroupIdToGroup.values());
63
+ if (!cachedIndices || cachedIndices.version !== version) {
64
+ cachedIndices = { version, indices: buildProviderIndices(groups) };
65
+ }
66
+ return { groups, indices: cachedIndices.indices };
67
+ };
68
+ const planOrThrow = (misses, updated_at) => {
69
+ const { groups, indices } = snapshot();
70
+ if (groups.length === 0)
71
+ throw newError('VEX_QUOTE_PROVIDER_NOT_FOUND', { method: 'GetQuotes' });
72
+ return router.planOrThrow(misses, indices, updated_at);
73
+ };
74
+ const execute = async (requests) => (await executor.execute(requests));
75
+ const fillQuoteStateFromUpstream = async (params) => {
76
+ const { quoteState, cacheMissed, updated_at } = params;
77
+ if (cacheMissed.length === 0)
78
+ return;
79
+ const { groups } = snapshot();
80
+ console.info(formatTime(Date.now()), `[VEX][Quote]UpstreamProviderDiscovery`, ` Discovered ${groups.length} GetQuotes provider groups from terminal infos.`, JSON.stringify(groups));
81
+ const plan = planOrThrow(cacheMissed, updated_at);
82
+ const mapGroupIdToProductIds = new Map();
83
+ for (const { planned } of plan.requests) {
84
+ let productIds = mapGroupIdToProductIds.get(planned.group_id);
85
+ if (!productIds) {
86
+ productIds = new Set();
87
+ mapGroupIdToProductIds.set(planned.group_id, productIds);
88
+ }
89
+ for (const product_id of planned.req.product_ids)
90
+ productIds.add(product_id);
91
+ }
92
+ console.info(formatTime(Date.now()), `[VEX][Quote]RouteDispatched`, ` Routed ${cacheMissed.length} missed quotes to ${mapGroupIdToProductIds.size} provider groups.`, JSON.stringify({
93
+ productsByGroupId: [...mapGroupIdToProductIds.entries()].map(([group_id, productIds]) => ({
94
+ group_id,
95
+ product_ids: [...productIds],
96
+ })),
97
+ default_action: plan.defaultAction,
98
+ }));
99
+ if (Object.keys(plan.defaultAction).length > 0) {
100
+ quoteState.update(plan.defaultAction);
101
+ }
102
+ console.info(formatTime(Date.now()), `[VEX][Quote]RequestPlanned`, `Planned ${plan.requests.length} upstream GetQuotes requests.`, JSON.stringify(plan.requests.map(({ key, planned }) => ({
103
+ key,
104
+ group_id: planned.group_id,
105
+ product_ids: planned.req.product_ids,
106
+ fields: planned.req.fields,
107
+ }))));
108
+ const actions = await execute(plan.requests);
109
+ console.debug(formatTime(Date.now()), `[VEX][Quote]RequestReceived`, `Received ${actions.length} upstream GetQuotes responses.`, JSON.stringify(actions));
110
+ for (const action of actions) {
111
+ quoteState.update(action);
112
+ }
113
+ };
114
+ return { snapshot, planOrThrow, execute, fillQuoteStateFromUpstream };
115
+ };
116
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../../src/quote/upstream/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,mCAAmC,EAAE,MAAM,kBAAkB,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAS3C,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EACL,oBAAoB,EACpB,iBAAiB,GAIlB,MAAM,UAAU,CAAC;AAalB,MAAM,yBAAyB,GAAG,CAAC,QAA+B,EAAyB,EAAE;IAC3F,MAAM,MAAM,GAAG,CAAC,GAAI,QAAQ,CAAC,MAAiC,CAAC,CAAC,IAAI,EAAE,CAAC;IACvE,uCAAY,QAAQ,KAAE,MAAM,IAAG;AACjC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,2BAA2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minal } from '@yuants/protocol';\nimport { encodePath, formatTime, listWatch, newError } from '@yuants/utils';\nimport { EMPTY, map, of, tap } from 'rxjs';\nimport {\n IQuoteKey,\n IQuoteProviderGroup,\n IQuoteProviderInstance,\n IQuoteRequire,\n IQuoteState,\n IQuoteUpdateAction,\n} from '../types';\nimport { createGetQuotesExecutor } from './executor';\nimport {\n buildProviderIndices,\n createQuoteRouter,\n IProviderIndices,\n IPlannedRequestWithKey,\n QuoteUpstreamPlan,\n} from './router';\n\nexport interface IQuoteProviderRegistry {\n snapshot: () => { groups: IQuoteProviderGroup[]; indices: IProviderIndices };\n planOrThrow: (misses: IQuoteRequire[], updated_at: number) => QuoteUpstreamPlan;\n execute: (requests: IPlannedRequestWithKey[]) => Promise<IQuoteUpdateAction[]>;\n fillQuoteStateFromUpstream: (params: {\n quoteState: IQuoteState;\n cacheMissed: IQuoteRequire[];\n updated_at: number;\n }) => Promise<void>;\n}\n\nconst normalizeMetadataForGroup = (metadata: IQuoteServiceMetadata): IQuoteServiceMetadata => {\n const fields = [...(metadata.fields as unknown as IQuoteKey[])].sort();\n return { ...metadata, fields };\n};\n\nexport const createQuoteProviderRegistry = (terminal: Terminal): IQuoteProviderRegistry => {\n const router = createQuoteRouter();\n const executor = createGetQuotesExecutor(terminal);\n\n const mapGroupIdToGroup = new Map<string, IQuoteProviderGroup>();\n let version = 0;\n let cachedIndices: { version: number; indices: IProviderIndices } | undefined;\n\n const quoteServiceInfos$ = terminal.terminalInfos$.pipe(\n map((infos) =>\n infos.flatMap((info) =>\n Object.values(info.serviceInfo ?? {})\n .filter((serviceInfo) => serviceInfo.method === 'GetQuotes')\n .map((serviceInfo) => ({ terminal_id: info.terminal_id, serviceInfo })),\n ),\n ),\n );\n\n quoteServiceInfos$\n .pipe(\n listWatch(\n (v) => v.serviceInfo.service_id,\n (v) => {\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote]DiscoveringGetQuotesProvider...`,\n `from terminal ${v.terminal_id}`,\n `service ${v.serviceInfo.service_id}`,\n `schema: ${JSON.stringify(v.serviceInfo.schema)}`,\n );\n try {\n const metadata = normalizeMetadataForGroup(\n parseQuoteServiceMetadataFromSchema(v.serviceInfo.schema),\n );\n const group_id = encodePath(\n metadata.product_id_prefix,\n (metadata.fields as any[]).join(','),\n metadata.max_products_per_request ?? '',\n );\n const provider: IQuoteProviderInstance = {\n terminal_id: v.terminal_id,\n service_id: v.serviceInfo.service_id || v.serviceInfo.method,\n };\n const group =\n mapGroupIdToGroup.get(group_id) ??\n (() => {\n const next: IQuoteProviderGroup = {\n group_id,\n meta: metadata,\n mapTerminalIdToInstance: new Map<string, IQuoteProviderInstance>(),\n };\n mapGroupIdToGroup.set(group_id, next);\n return next;\n })();\n group.mapTerminalIdToInstance.set(provider.terminal_id, provider);\n version++;\n return of(void 0).pipe(\n tap({\n unsubscribe: () => {\n mapGroupIdToGroup.get(group_id)?.mapTerminalIdToInstance.delete(v.terminal_id);\n if (mapGroupIdToGroup.get(group_id)?.mapTerminalIdToInstance.size === 0) {\n mapGroupIdToGroup.delete(group_id);\n }\n version++;\n },\n }),\n );\n } catch {\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote]IgnoredGetQuotesProvider`,\n `Ignored GetQuotes provider from terminal ${v.terminal_id}`,\n `service ${v.serviceInfo.service_id} due to invalid schema.`,\n );\n return EMPTY;\n }\n },\n ),\n )\n .subscribe();\n\n const snapshot = () => {\n const groups = Array.from(mapGroupIdToGroup.values());\n if (!cachedIndices || cachedIndices.version !== version) {\n cachedIndices = { version, indices: buildProviderIndices(groups) };\n }\n return { groups, indices: cachedIndices.indices };\n };\n\n const planOrThrow = (misses: IQuoteRequire[], updated_at: number): QuoteUpstreamPlan => {\n const { groups, indices } = snapshot();\n if (groups.length === 0) throw newError('VEX_QUOTE_PROVIDER_NOT_FOUND', { method: 'GetQuotes' });\n return router.planOrThrow(misses, indices, updated_at);\n };\n\n const execute = async (requests: IPlannedRequestWithKey[]): Promise<IQuoteUpdateAction[]> =>\n (await executor.execute(requests)) as any;\n\n const fillQuoteStateFromUpstream: IQuoteProviderRegistry['fillQuoteStateFromUpstream'] = async (params) => {\n const { quoteState, cacheMissed, updated_at } = params;\n if (cacheMissed.length === 0) return;\n\n const { groups } = snapshot();\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote]UpstreamProviderDiscovery`,\n ` Discovered ${groups.length} GetQuotes provider groups from terminal infos.`,\n JSON.stringify(groups),\n );\n\n const plan = planOrThrow(cacheMissed, updated_at);\n\n const mapGroupIdToProductIds = new Map<string, Set<string>>();\n for (const { planned } of plan.requests) {\n let productIds = mapGroupIdToProductIds.get(planned.group_id);\n if (!productIds) {\n productIds = new Set<string>();\n mapGroupIdToProductIds.set(planned.group_id, productIds);\n }\n for (const product_id of planned.req.product_ids) productIds.add(product_id);\n }\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote]RouteDispatched`,\n ` Routed ${cacheMissed.length} missed quotes to ${mapGroupIdToProductIds.size} provider groups.`,\n JSON.stringify({\n productsByGroupId: [...mapGroupIdToProductIds.entries()].map(([group_id, productIds]) => ({\n group_id,\n product_ids: [...productIds],\n })),\n default_action: plan.defaultAction,\n }),\n );\n\n if (Object.keys(plan.defaultAction).length > 0) {\n quoteState.update(plan.defaultAction);\n }\n\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote]RequestPlanned`,\n `Planned ${plan.requests.length} upstream GetQuotes requests.`,\n JSON.stringify(\n plan.requests.map(({ key, planned }) => ({\n key,\n group_id: planned.group_id,\n product_ids: planned.req.product_ids,\n fields: planned.req.fields,\n })),\n ),\n );\n\n const actions = await execute(plan.requests);\n console.debug(\n formatTime(Date.now()),\n `[VEX][Quote]RequestReceived`,\n `Received ${actions.length} upstream GetQuotes responses.`,\n JSON.stringify(actions),\n );\n\n for (const action of actions) {\n quoteState.update(action);\n }\n };\n\n return { snapshot, planOrThrow, execute, fillQuoteStateFromUpstream };\n};\n"]}
@@ -0,0 +1,119 @@
1
+ import { TextEncoder } from 'util';
2
+ import { encodePath, fnv1a64Hex, newError } from '@yuants/utils';
3
+ import { createSortedPrefixMatcher } from './prefix-matcher';
4
+ const SEP_BYTE = new Uint8Array([0xff]);
5
+ const encodeStrings = (parts) => {
6
+ const buffers = [];
7
+ for (const part of parts) {
8
+ buffers.push(new TextEncoder().encode(part));
9
+ buffers.push(SEP_BYTE);
10
+ }
11
+ const totalLength = buffers.reduce((sum, b) => sum + b.length, 0);
12
+ const result = new Uint8Array(totalLength);
13
+ let offset = 0;
14
+ for (const b of buffers) {
15
+ result.set(b, offset);
16
+ offset += b.length;
17
+ }
18
+ return result;
19
+ };
20
+ const fnv1a64HexFromStrings = (parts) => fnv1a64Hex(encodeStrings(parts));
21
+ export const buildProviderIndices = (groups) => {
22
+ const mapGroupIdToGroup = new Map(groups.map((x) => [x.group_id, x]));
23
+ const prefixMatcher = createSortedPrefixMatcher(groups.map((group) => ({ prefix: group.meta.product_id_prefix, value: group.group_id })));
24
+ const mapFieldToGroupIds = new Map();
25
+ for (const group of groups) {
26
+ for (const field of group.meta.fields) {
27
+ let groupIds = mapFieldToGroupIds.get(field);
28
+ if (!groupIds) {
29
+ groupIds = new Set();
30
+ mapFieldToGroupIds.set(field, groupIds);
31
+ }
32
+ groupIds.add(group.group_id);
33
+ }
34
+ }
35
+ return { mapGroupIdToGroup, prefixMatcher, mapFieldToGroupIds };
36
+ };
37
+ const createRequestKey = (group_id, batchProductIds) => encodePath(group_id, fnv1a64HexFromStrings(batchProductIds));
38
+ const planRequests = (params) => {
39
+ var _a;
40
+ const { productsByGroupId, mapGroupIdToGroup } = params;
41
+ const plannedRequests = [];
42
+ for (const [group_id, productIdSet] of productsByGroupId) {
43
+ const group = mapGroupIdToGroup.get(group_id);
44
+ if (!group)
45
+ continue;
46
+ const sortedProductIds = [...productIdSet].sort();
47
+ const max = (_a = group.meta.max_products_per_request) !== null && _a !== void 0 ? _a : sortedProductIds.length;
48
+ for (let i = 0; i < sortedProductIds.length; i += max) {
49
+ const batchProductIds = sortedProductIds.slice(i, i + max);
50
+ const key = createRequestKey(group_id, batchProductIds);
51
+ plannedRequests.push({
52
+ key,
53
+ planned: {
54
+ group_id,
55
+ instances: Array.from(group.mapTerminalIdToInstance.values()),
56
+ req: { product_ids: batchProductIds, fields: group.meta.fields },
57
+ },
58
+ });
59
+ }
60
+ }
61
+ return plannedRequests;
62
+ };
63
+ export const createQuoteRouter = () => ({
64
+ planOrThrow: (misses, indices, updated_at) => {
65
+ const { prefixMatcher, mapFieldToGroupIds, mapGroupIdToGroup } = indices;
66
+ const productsByGroupId = new Map();
67
+ const defaultAction = {};
68
+ const unroutableProducts = new Set();
69
+ const mapProductIdToGroupIds = new Map();
70
+ for (const miss of misses) {
71
+ const { product_id, field } = miss;
72
+ let productGroupIds = mapProductIdToGroupIds.get(product_id);
73
+ if (!productGroupIds) {
74
+ productGroupIds = prefixMatcher.match(product_id);
75
+ mapProductIdToGroupIds.set(product_id, productGroupIds);
76
+ }
77
+ if (productGroupIds.length === 0) {
78
+ unroutableProducts.add(product_id);
79
+ continue;
80
+ }
81
+ const fieldGroupIds = mapFieldToGroupIds.get(field);
82
+ if (!fieldGroupIds) {
83
+ if (!defaultAction[product_id])
84
+ defaultAction[product_id] = {};
85
+ defaultAction[product_id][field] = ['', updated_at];
86
+ continue;
87
+ }
88
+ let matched = false;
89
+ for (const group_id of productGroupIds) {
90
+ if (!fieldGroupIds.has(group_id))
91
+ continue;
92
+ matched = true;
93
+ let productIds = productsByGroupId.get(group_id);
94
+ if (!productIds) {
95
+ productIds = new Set();
96
+ productsByGroupId.set(group_id, productIds);
97
+ }
98
+ productIds.add(product_id);
99
+ }
100
+ if (!matched) {
101
+ if (!defaultAction[product_id])
102
+ defaultAction[product_id] = {};
103
+ defaultAction[product_id][field] = ['', updated_at];
104
+ }
105
+ }
106
+ if (unroutableProducts.size !== 0) {
107
+ throw newError('VEX_QUOTE_PRODUCT_UNROUTABLE', {
108
+ updated_at,
109
+ unroutable_products: [...unroutableProducts].slice(0, 200),
110
+ unroutable_products_total: unroutableProducts.size,
111
+ });
112
+ }
113
+ return {
114
+ defaultAction,
115
+ requests: planRequests({ productsByGroupId, mapGroupIdToGroup }),
116
+ };
117
+ },
118
+ });
119
+ //# sourceMappingURL=router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.js","sourceRoot":"","sources":["../../../src/quote/upstream/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAGnC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,yBAAyB,EAAkB,MAAM,kBAAkB,CAAC;AAS7E,MAAM,QAAQ,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;AAExC,MAAM,aAAa,GAAG,CAAC,KAAe,EAAc,EAAE;IACpD,MAAM,OAAO,GAAiB,EAAE,CAAC;IACjC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,OAAO,CAAC,IAAI,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;QAC7C,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;KACxB;IACD,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;IAC3C,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE;QACvB,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QACtB,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC;KACpB;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAAC,K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CAAC;gBACjD,IAAI,CAAC,UAAU,EAAE;oBACf,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;oBAC/B,iBAAiB,CAAC,GAAG,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;iBAC7C;gBACD,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;aAC5B;YAED,IAAI,CAAC,OAAO,EAAE;gBACZ,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC;oBAAE,aAAa,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;gBAC/D,aAAa,CAAC,UAAU,CAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;aACtD;SACF;QAED,IAAI,kBAAkB,CAAC,IAAI,KAAK,CAAC,EAAE;YACjC,MAAM,QAAQ,CAAC,8BAA8B,EAAE;gBAC7C,UAAU;gBACV,mBAAmB,EAAE,CAAC,GAAG,kBAAkB,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;gBAC1D,yBAAyB,EAAE,kBAAkB,CAAC,IAAI;aACnD,CAAC,CAAC;SACJ;QAED,OAAO;YACL,aAAa;YACb,QAAQ,EAAE,YAAY,CAAC,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,CAAC;SACjE,CAAC;IACJ,CAAC;CACF,CAAC,CAAC","sourcesContent":["import { TextEncoder } from 'util';\n\nimport { IQuoteServiceRequestByVEX } from '@yuants/exchange';\nimport { encodePath, fnv1a64Hex, newError } from '@yuants/utils';\nimport { createSortedPrefixMatcher, IPrefixMatcher } from './prefix-matcher';\nimport {\n IQuoteKey,\n IQuoteProviderGroup,\n IQuoteProviderInstance,\n IQuoteRequire,\n IQuoteUpdateAction,\n} from '../types';\n\nconst SEP_BYTE = new Uint8Array([0xff]);\n\nconst encodeStrings = (parts: string[]): Uint8Array => {\n const buffers: Uint8Array[] = [];\n for (const part of parts) {\n buffers.push(new TextEncoder().encode(part));\n buffers.push(SEP_BYTE);\n }\n const totalLength = buffers.reduce((sum, b) => sum + b.length, 0);\n const result = new Uint8Array(totalLength);\n let offset = 0;\n for (const b of buffers) {\n result.set(b, offset);\n offset += b.length;\n }\n return result;\n};\n\nconst fnv1a64HexFromStrings = (parts: string[]): string => fnv1a64Hex(encodeStrings(parts));\n\nexport type IPlannedRequest = {\n group_id: string;\n instances: IQuoteProviderInstance[];\n req: IQuoteServiceRequestByVEX;\n};\n\nexport type IPlannedRequestWithKey = { key: string; planned: IPlannedRequest };\n\nexport type QuoteUpstreamPlan = {\n defaultAction: IQuoteUpdateAction;\n requests: IPlannedRequestWithKey[];\n};\n\nexport type IProviderIndices = {\n mapGroupIdToGroup: Map<string, IQuoteProviderGroup>;\n prefixMatcher: IPrefixMatcher<string>;\n mapFieldToGroupIds: Map<IQuoteKey, Set<string>>;\n};\n\nexport const buildProviderIndices = (groups: IQuoteProviderGroup[]): IProviderIndices => {\n const mapGroupIdToGroup = new Map(groups.map((x) => [x.group_id, x] as const));\n const prefixMatcher = createSortedPrefixMatcher(\n groups.map((group) => ({ prefix: group.meta.product_id_prefix, value: group.group_id })),\n );\n const mapFieldToGroupIds = new Map<IQuoteKey, Set<string>>();\n for (const group of groups) {\n for (const field of group.meta.fields as IQuoteKey[]) {\n let groupIds = mapFieldToGroupIds.get(field);\n if (!groupIds) {\n groupIds = new Set<string>();\n mapFieldToGroupIds.set(field, groupIds);\n }\n groupIds.add(group.group_id);\n }\n }\n return { mapGroupIdToGroup, prefixMatcher, mapFieldToGroupIds };\n};\n\nconst createRequestKey = (group_id: string, batchProductIds: string[]) =>\n encodePath(group_id, fnv1a64HexFromStrings(batchProductIds));\n\nconst planRequests = (params: {\n productsByGroupId: Map<string, Set<string>>;\n mapGroupIdToGroup: Map<string, IQuoteProviderGroup>;\n}): IPlannedRequestWithKey[] => {\n const { productsByGroupId, mapGroupIdToGroup } = params;\n const plannedRequests: IPlannedRequestWithKey[] = [];\n for (const [group_id, productIdSet] of productsByGroupId) {\n const group = mapGroupIdToGroup.get(group_id);\n if (!group) continue;\n const sortedProductIds = [...productIdSet].sort();\n const max = group.meta.max_products_per_request ?? sortedProductIds.length;\n for (let i = 0; i < sortedProductIds.length; i += max) {\n const batchProductIds = sortedProductIds.slice(i, i + max);\n const key = createRequestKey(group_id, batchProductIds);\n plannedRequests.push({\n key,\n planned: {\n group_id,\n instances: Array.from(group.mapTerminalIdToInstance.values()),\n req: { product_ids: batchProductIds, fields: group.meta.fields as any },\n },\n });\n }\n }\n return plannedRequests;\n};\n\nexport interface IQuoteRouter {\n planOrThrow: (misses: IQuoteRequire[], indices: IProviderIndices, updated_at: number) => QuoteUpstreamPlan;\n}\n\nexport const createQuoteRouter = (): IQuoteRouter => ({\n planOrThrow: (misses, indices, updated_at) => {\n const { prefixMatcher, mapFieldToGroupIds, mapGroupIdToGroup } = indices;\n const productsByGroupId = new Map<string, Set<string>>();\n const defaultAction: IQuoteUpdateAction = {};\n const unroutableProducts = new Set<string>();\n\n const mapProductIdToGroupIds = new Map<string, string[]>();\n\n for (const miss of misses) {\n const { product_id, field } = miss;\n\n let productGroupIds = mapProductIdToGroupIds.get(product_id);\n if (!productGroupIds) {\n productGroupIds = prefixMatcher.match(product_id);\n mapProductIdToGroupIds.set(product_id, productGroupIds);\n }\n if (productGroupIds.length === 0) {\n unroutableProducts.add(product_id);\n continue;\n }\n\n const fieldGroupIds = mapFieldToGroupIds.get(field);\n if (!fieldGroupIds) {\n if (!defaultAction[product_id]) defaultAction[product_id] = {};\n defaultAction[product_id]![field] = ['', updated_at];\n continue;\n }\n\n let matched = false;\n for (const group_id of productGroupIds) {\n if (!fieldGroupIds.has(group_id)) continue;\n matched = true;\n let productIds = productsByGroupId.get(group_id);\n if (!productIds) {\n productIds = new Set<string>();\n productsByGroupId.set(group_id, productIds);\n }\n productIds.add(product_id);\n }\n\n if (!matched) {\n if (!defaultAction[product_id]) defaultAction[product_id] = {};\n defaultAction[product_id]![field] = ['', updated_at];\n }\n }\n\n if (unroutableProducts.size !== 0) {\n throw newError('VEX_QUOTE_PRODUCT_UNROUTABLE', {\n updated_at,\n unroutable_products: [...unroutableProducts].slice(0, 200),\n unroutable_products_total: unroutableProducts.size,\n });\n }\n\n return {\n defaultAction,\n requests: planRequests({ productsByGroupId, mapGroupIdToGroup }),\n };\n },\n});\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAqC5C,eAAO,MAAM,gBAAgB,cAAqB,SAAS,EAAE,KAAG,QAAQ,SAAS,EAAE,CA+DlF,CAAC;AAEF,eAAO,MAAM,cAAc,WAAkB,MAAM,EAAE,KAAG,QAAQ,MAAM,EAAE,CA4BvE,CAAC"}
1
+ {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAqC5C,eAAO,MAAM,gBAAgB,cAAqB,SAAS,EAAE,KAAG,QAAQ,SAAS,EAAE,CAiFlF,CAAC;AAEF,eAAO,MAAM,cAAc,WAAkB,MAAM,EAAE,KAAG,QAAQ,MAAM,EAAE,CA4BvE,CAAC"}
package/lib/position.js CHANGED
@@ -52,6 +52,23 @@ const polyfillPosition = async (positions) => {
52
52
  }
53
53
  pos.valuation = Math.abs((theProduct.value_scale || 1) * pos.volume * pos.closable_price);
54
54
  }
55
+ if (quote && pos.size) {
56
+ const sizeNum = +pos.size;
57
+ if (pos.current_price === undefined) {
58
+ if (sizeNum > 0) {
59
+ // 多头头寸使用买一价作为可平仓价格,如果没有买一价则使用最新价
60
+ pos.current_price = (quote.ask_price || quote.last_price) + '';
61
+ }
62
+ else {
63
+ // 空头头寸使用卖一价作为可平仓价格,如果没有卖一价则使用最新价
64
+ pos.current_price = (quote.bid_price || quote.last_price) + '';
65
+ }
66
+ }
67
+ // 计算名义价值
68
+ if (pos.notional === undefined) {
69
+ pos.notional = sizeNum * (+pos.current_price || 0) + '';
70
+ }
71
+ }
55
72
  // 利率相关信息的追加
56
73
  if (quote) {
57
74
  if (quote.interest_rate_next_settled_at !== null) {
@@ -1 +1 @@
1
- {"version":3,"file":"position.js","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAG5C,uDAAgE;AAEhE,+CAA4C;AAC5C,qCAAoD;AACpD,yCAAyC;AAEzC,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AACxC,MAAM,YAAY,GAAG,IAAA,uCAAwB,EAAC,QAAQ,CAAC,CAAC;AACxD,MAAM,UAAU,GAAG,IAAA,mBAAW,EAC5B,KAAK,EAAE,UAAU,EAAE,EAAE;IACnB,MAAM,GAAG,GAAG,0CAA0C,IAAA,eAAS,EAAC,UAAU,CAAC,EAAE,CAAC;IAC9E,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,IAAA,gBAAU,EAAW,QAAQ,EAAE,GAAG,CAAC,CAAC;IAC1D,OAAO,KAAK,CAAC;AACf,CAAC,EACD,EAAE,MAAM,EAAE,KAAM,EAAE,CACnB,CAAC;AAEF,MAAM,yBAAyB,GAAG,IAAA,mBAAW,EAC3C,KAAK,EAAE,UAAkB,EAAE,EAAE;IAC3B,MAAM,GAAG,GAAG,0DAA0D,IAAA,eAAS,EAC7E,UAAU,CACX,mCAAmC,CAAC;IACrC,MAAM,KAAK,GAAG,MAAM,IAAA,gBAAU,EAA2B,QAAQ,EAAE,GAAG,CAAC,CAAC;IACxE,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IACvC,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC;IACrD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AO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC;gBAC5F,qCAAqC;gBACrC,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,KAAK,EAAE;oBACzE,MAAM,IAAA,gBAAQ,EAAC,8CAA8C,EAAE;wBAC7D,KAAK;wBACL,QAAQ;wBACR,OAAO;wBACP,OAAO,EAAE,UAAU;qBACpB,CAAC,CAAC;iBACJ;gBAED,IAAI,OAAO,IAAI,CAAC,EAAE;oBAChB,KAAK,CAAC,eAAe,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,WAAW,CAAC;iBACtE;qBAAM;oBACL,KAAK,CAAC,eAAe,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC;iBACtE;gBACD,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC;aAC3D;SACF;KACF;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AA5BW,QAAA,cAAc,kBA4BzB","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { createClientProductCache } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '@yuants/utils';\n\nconst terminal = Terminal.fromNodeEnv();\nconst productCache = createClientProductCache(terminal);\nconst quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n);\n\nconst interestRateIntervalCache = createCache(\n async (product_id: string) => {\n const sql = `select created_at from interest_rate where series_id = ${escapeSQL(\n product_id,\n )} order by created_at desc limit 2`;\n const rates = await requestSQL<{ created_at: string }[]>(terminal, sql);\n if (rates.length < 2) return undefined;\n const prev = new Date(rates[0].created_at).getTime();\n const prevOfPrev = new Date(rates[1].created_at).getTime();\n const interval = prev - prevOfPrev;\n return {\n prev,\n prevOfPrev,\n interval,\n };\n },\n { swrAfter: 60_000, expire: 3600_000 },\n);\n\nexport const polyfillPosition = async (positions: IPosition[]): Promise<IPosition[]> => {\n // TODO: 使用 batch query SQL 优化 product / quote 查询性能\n for (const pos of positions) {\n const [theProduct, quote, interestRateInterval] = await Promise.all([\n productCache.query(pos.product_id),\n quoteCache.query(pos.product_id),\n interestRateIntervalCache.query(pos.product_id),\n ]);\n\n // 估值 = value_scale * volume * closable_price\n if (theProduct) {\n if (theProduct.base_currency) {\n pos.base_currency = theProduct.base_currency;\n }\n if (theProduct.quote_currency) {\n pos.quote_currency = theProduct.quote_currency;\n }\n if (pos.size === undefined && pos.volume !== undefined && pos.direction !== undefined) {\n pos.size = (pos.direction === 'LONG' ? 1 : -1) * pos.volume * (theProduct.value_scale || 1) + '';\n }\n if (pos.free_size === undefined && pos.free_volume !== undefined && pos.direction !== undefined) {\n pos.free_size =\n (pos.direction === 'LONG' ? 1 : -1) * pos.free_volume * (theProduct.value_scale || 1) + '';\n }\n pos.valuation = Math.abs((theProduct.value_scale || 1) * pos.volume * pos.closable_price);\n }\n\n // 利率相关信息的追加\n if (quote) {\n if (quote.interest_rate_next_settled_at !== null) {\n const nextSettledAt = new Date(quote.interest_rate_next_settled_at).getTime();\n // 优先使用行情数据中的下一个结算时间\n pos.settlement_scheduled_at = nextSettledAt;\n // 优先使用下一个结算时间推算结算间隔\n if (interestRateInterval !== undefined) {\n const interval = nextSettledAt - interestRateInterval.prev;\n pos.settlement_interval = interval;\n }\n } else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {\n // 估算下一个结算时间\n // 找到 prev + k * interval > now 的最小 k,则下一个结算时间为 prev + k * interval\n const k = Math.ceil((Date.now() - interestRateInterval.prev) / interestRateInterval.interval);\n pos.settlement_scheduled_at = interestRateInterval.prev + k * interestRateInterval.interval;\n }\n\n // 如果还没有结算间隔,则使用 interest rate 表的时间间隔作为结算间隔\n if (pos.settlement_interval === undefined && interestRateInterval) {\n pos.settlement_interval = interestRateInterval.interval;\n }\n\n if (pos.direction === 'LONG') {\n if (quote.interest_rate_long !== null) {\n pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;\n }\n }\n if (pos.direction === 'SHORT') {\n if (quote.interest_rate_short !== null) {\n pos.interest_to_settle = +quote.interest_rate_short * pos.valuation;\n }\n }\n }\n }\n return positions;\n};\n\nexport const polyfillOrders = async (orders: IOrder[]): Promise<IOrder[]> => {\n for (const order of orders) {\n const theProduct = await productCache.query(order.product_id);\n if (theProduct) {\n if (order.size !== undefined) {\n const sizeNum = +order.size;\n const sizeStep = theProduct.volume_step * theProduct.value_scale;\n if (!(sizeStep > 0)) throw newError('INVALID_SIZE_STEP', { product: theProduct, sizeStep });\n // check size is multiple of sizeStep\n if (Math.abs(sizeNum - Math.round(sizeNum / sizeStep) * sizeStep) > 1e-16) {\n throw newError('INVALID_ORDER_SIZE_NOT_MULTIPLE_OF_SIZE_STEP', {\n order,\n sizeStep,\n sizeNum,\n product: theProduct,\n });\n }\n\n if (sizeNum >= 0) {\n order.order_direction = order.is_close ? 'CLOSE_SHORT' : 'OPEN_LONG';\n } else {\n order.order_direction = order.is_close ? 'CLOSE_LONG' : 'OPEN_SHORT';\n }\n order.volume = Math.abs(sizeNum) / theProduct.value_scale;\n }\n }\n }\n return orders;\n};\n"]}
1
+ {"version":3,"file":"position.js","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAG5C,uDAAgE;AAEhE,+CAA4C;AAC5C,qCAAoD;AACpD,yCAAyC;AAEzC,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE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,CAAC,KAAK,CAAC,IAAI,CAAC;gBAC5B,MAAM,QAAQ,GAAG,UAAU,CAAC,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC;gBACjE,IAAI,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;oBAAE,MAAM,IAAA,gBAAQ,EAAC,mBAAmB,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC;gBAC5F,qCAAqC;gBACrC,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,KAAK,EAAE;oBACzE,MAAM,IAAA,gBAAQ,EAAC,8CAA8C,EAAE;wBAC7D,KAAK;wBACL,QAAQ;wBACR,OAAO;wBACP,OAAO,EAAE,UAAU;qBACpB,CAAC,CAAC;iBACJ;gBAED,IAAI,OAAO,IAAI,CAAC,EAAE;oBAChB,KAAK,CAAC,eAAe,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,WAAW,CAAC;iBACtE;qBAAM;oBACL,KAAK,CAAC,eAAe,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC;iBACtE;gBACD,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC;aAC3D;SACF;KACF;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AA5BW,QAAA,cAAc,kBA4BzB","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { createClientProductCache } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '@yuants/utils';\n\nconst terminal = Terminal.fromNodeEnv();\nconst productCache = createClientProductCache(terminal);\nconst quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n);\n\nconst interestRateIntervalCache = createCache(\n async (product_id: string) => {\n const sql = `select created_at from interest_rate where series_id = ${escapeSQL(\n product_id,\n )} order by created_at desc limit 2`;\n const rates = await requestSQL<{ created_at: string }[]>(terminal, sql);\n if (rates.length < 2) return undefined;\n const prev = new Date(rates[0].created_at).getTime();\n const prevOfPrev = new Date(rates[1].created_at).getTime();\n const interval = prev - prevOfPrev;\n return {\n prev,\n prevOfPrev,\n interval,\n };\n },\n { swrAfter: 60_000, expire: 3600_000 },\n);\n\nexport const polyfillPosition = async (positions: IPosition[]): Promise<IPosition[]> => {\n // TODO: 使用 batch query SQL 优化 product / quote 查询性能\n for (const pos of positions) {\n const [theProduct, quote, interestRateInterval] = await Promise.all([\n productCache.query(pos.product_id),\n quoteCache.query(pos.product_id),\n interestRateIntervalCache.query(pos.product_id),\n ]);\n\n // 估值 = value_scale * volume * closable_price\n if (theProduct) {\n if (theProduct.base_currency) {\n pos.base_currency = theProduct.base_currency;\n }\n if (theProduct.quote_currency) {\n pos.quote_currency = theProduct.quote_currency;\n }\n if (pos.size === undefined && pos.volume !== undefined && pos.direction !== undefined) {\n pos.size = (pos.direction === 'LONG' ? 1 : -1) * pos.volume * (theProduct.value_scale || 1) + '';\n }\n if (pos.free_size === undefined && pos.free_volume !== undefined && pos.direction !== undefined) {\n pos.free_size =\n (pos.direction === 'LONG' ? 1 : -1) * pos.free_volume * (theProduct.value_scale || 1) + '';\n }\n pos.valuation = Math.abs((theProduct.value_scale || 1) * pos.volume * pos.closable_price);\n }\n\n if (quote && pos.size) {\n const sizeNum = +pos.size;\n if (pos.current_price === undefined) {\n if (sizeNum > 0) {\n // 多头头寸使用买一价作为可平仓价格,如果没有买一价则使用最新价\n pos.current_price = (quote.ask_price || quote.last_price) + '';\n } else {\n // 空头头寸使用卖一价作为可平仓价格,如果没有卖一价则使用最新价\n pos.current_price = (quote.bid_price || quote.last_price) + '';\n }\n }\n\n // 计算名义价值\n if (pos.notional === undefined) {\n pos.notional = sizeNum * (+pos.current_price || 0) + '';\n }\n }\n\n // 利率相关信息的追加\n if (quote) {\n if (quote.interest_rate_next_settled_at !== null) {\n const nextSettledAt = new Date(quote.interest_rate_next_settled_at).getTime();\n // 优先使用行情数据中的下一个结算时间\n pos.settlement_scheduled_at = nextSettledAt;\n // 优先使用下一个结算时间推算结算间隔\n if (interestRateInterval !== undefined) {\n const interval = nextSettledAt - interestRateInterval.prev;\n pos.settlement_interval = interval;\n }\n } else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {\n // 估算下一个结算时间\n // 找到 prev + k * interval > now 的最小 k,则下一个结算时间为 prev + k * interval\n const k = Math.ceil((Date.now() - interestRateInterval.prev) / interestRateInterval.interval);\n pos.settlement_scheduled_at = interestRateInterval.prev + k * interestRateInterval.interval;\n }\n\n // 如果还没有结算间隔,则使用 interest rate 表的时间间隔作为结算间隔\n if (pos.settlement_interval === undefined && interestRateInterval) {\n pos.settlement_interval = interestRateInterval.interval;\n }\n\n if (pos.direction === 'LONG') {\n if (quote.interest_rate_long !== null) {\n pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;\n }\n }\n if (pos.direction === 'SHORT') {\n if (quote.interest_rate_short !== null) {\n pos.interest_to_settle = +quote.interest_rate_short * pos.valuation;\n }\n }\n }\n }\n return positions;\n};\n\nexport const polyfillOrders = async (orders: IOrder[]): Promise<IOrder[]> => {\n for (const order of orders) {\n const theProduct = await productCache.query(order.product_id);\n if (theProduct) {\n if (order.size !== undefined) {\n const sizeNum = +order.size;\n const sizeStep = theProduct.volume_step * theProduct.value_scale;\n if (!(sizeStep > 0)) throw newError('INVALID_SIZE_STEP', { product: theProduct, sizeStep });\n // check size is multiple of sizeStep\n if (Math.abs(sizeNum - Math.round(sizeNum / sizeStep) * sizeStep) > 1e-16) {\n throw newError('INVALID_ORDER_SIZE_NOT_MULTIPLE_OF_SIZE_STEP', {\n order,\n sizeStep,\n sizeNum,\n product: theProduct,\n });\n }\n\n if (sizeNum >= 0) {\n order.order_direction = order.is_close ? 'CLOSE_SHORT' : 'OPEN_LONG';\n } else {\n order.order_direction = order.is_close ? 'CLOSE_LONG' : 'OPEN_SHORT';\n }\n order.volume = Math.abs(sizeNum) / theProduct.value_scale;\n }\n }\n }\n return orders;\n};\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=implementations.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"implementations.test.d.ts","sourceRoot":"","sources":["../../../src/quote/__tests__/implementations.test.ts"],"names":[],"mappings":""}