@yuants/app-virtual-exchange 0.11.2 → 0.11.4

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 (53) hide show
  1. package/dist/quote/implementations/v1.js +7 -2
  2. package/dist/quote/implementations/v1.js.map +1 -1
  3. package/dist/quote/scheduler.js +244 -0
  4. package/dist/quote/scheduler.js.map +1 -0
  5. package/dist/quote/service.js +4 -67
  6. package/dist/quote/service.js.map +1 -1
  7. package/dist/quote/state.js +1 -0
  8. package/dist/quote/state.js.map +1 -1
  9. package/lib/quote/implementations/v1.d.ts.map +1 -1
  10. package/lib/quote/implementations/v1.js +7 -2
  11. package/lib/quote/implementations/v1.js.map +1 -1
  12. package/lib/quote/scheduler.d.ts +21 -0
  13. package/lib/quote/scheduler.d.ts.map +1 -0
  14. package/lib/quote/scheduler.js +248 -0
  15. package/lib/quote/scheduler.js.map +1 -0
  16. package/lib/quote/service.js +7 -70
  17. package/lib/quote/service.js.map +1 -1
  18. package/lib/quote/state.d.ts +1 -0
  19. package/lib/quote/state.d.ts.map +1 -1
  20. package/lib/quote/state.js +2 -1
  21. package/lib/quote/state.js.map +1 -1
  22. package/package.json +2 -2
  23. package/temp/package-deps.json +8 -12
  24. package/dist/quote/upstream/executor.js +0 -98
  25. package/dist/quote/upstream/executor.js.map +0 -1
  26. package/dist/quote/upstream/index.js +0 -2
  27. package/dist/quote/upstream/index.js.map +0 -1
  28. package/dist/quote/upstream/prefix-matcher.js +0 -7
  29. package/dist/quote/upstream/prefix-matcher.js.map +0 -1
  30. package/dist/quote/upstream/registry.js +0 -116
  31. package/dist/quote/upstream/registry.js.map +0 -1
  32. package/dist/quote/upstream/router.js +0 -119
  33. package/dist/quote/upstream/router.js.map +0 -1
  34. package/lib/quote/upstream/executor.d.ts +0 -8
  35. package/lib/quote/upstream/executor.d.ts.map +0 -1
  36. package/lib/quote/upstream/executor.js +0 -102
  37. package/lib/quote/upstream/executor.js.map +0 -1
  38. package/lib/quote/upstream/index.d.ts +0 -3
  39. package/lib/quote/upstream/index.d.ts.map +0 -1
  40. package/lib/quote/upstream/index.js +0 -6
  41. package/lib/quote/upstream/index.js.map +0 -1
  42. package/lib/quote/upstream/prefix-matcher.d.ts +0 -8
  43. package/lib/quote/upstream/prefix-matcher.d.ts.map +0 -1
  44. package/lib/quote/upstream/prefix-matcher.js +0 -11
  45. package/lib/quote/upstream/prefix-matcher.js.map +0 -1
  46. package/lib/quote/upstream/registry.d.ts +0 -18
  47. package/lib/quote/upstream/registry.d.ts.map +0 -1
  48. package/lib/quote/upstream/registry.js +0 -120
  49. package/lib/quote/upstream/registry.js.map +0 -1
  50. package/lib/quote/upstream/router.d.ts +0 -27
  51. package/lib/quote/upstream/router.d.ts.map +0 -1
  52. package/lib/quote/upstream/router.js +0 -124
  53. package/lib/quote/upstream/router.js.map +0 -1
@@ -113,8 +113,13 @@ export const createQuoteStateV1 = () => {
113
113
  for (const product_id of product_ids) {
114
114
  result[product_id] = {};
115
115
  for (const field of fields) {
116
- const tuple = getValueTuple(product_id, field);
117
- result[product_id][field] = tuple ? tuple[0] : '';
116
+ const offset = getFieldOffset(product_id, field);
117
+ if (offset === undefined) {
118
+ result[product_id][field] = '';
119
+ }
120
+ else {
121
+ result[product_id][field] = data[offset] || '';
122
+ }
118
123
  }
119
124
  }
120
125
  return result;
@@ -1 +1 @@
1
- {"version":3,"file":"v1.js","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,wBAAwB;AACxB,MAAM,MAAM,GAAG,CAAC,CAAC,CAAiC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAiB,CAAC,CAAC;IAC3F,mDAAmD;IACnD,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb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AM,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;YACxB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;gBAC1B,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;gBAC/C,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE;oBACnC,MAAM,CAAC,UAAU,CAAE,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;iBACpC;aACF;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,MAAM,YAAY,GAAG,CACnB,WAAqB,EACrB,MAAW,EACwB,EAAE;QACrC,MAAM,MAAM,GAAsC,EAAE,CAAC;QACrD,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;YACpC,MAAM,CAAC,UAAU,CAAC,GAAG,EAAuB,CAAC;YAC7C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;gBAC1B,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;gBAC/C,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;aACnD;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AACvE,CAAC,CAAC","sourcesContent":["import { newError } from '@yuants/utils';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n// TRICK: 固定字段顺序,方便计算偏移量\nconst FIELDS = ((x: { [key in IQuoteKey]: number }) => Object.keys(x).sort() as IQuoteKey[])({\n // TS TRICK: 强制运行时数组具有 IQuoteKey 的所有字段。不重不漏,味道真是好极了\n last_price: 0,\n ask_price: 0,\n ask_volume: 0,\n bid_volume: 0,\n bid_price: 0,\n interest_rate_short: 0,\n open_interest: 0,\n interest_rate_prev_settled_at: 0,\n interest_rate_next_settled_at: 0,\n interest_rate_long: 0,\n});\n\nconst FIELD_COUNT = FIELDS.length;\nconst mapFieldNameToOffset = Object.fromEntries(FIELDS.map((field, index) => [field, index * 2]));\n\n/**\n * 高效的行情状态管理器\n * 内部使用扁平化数组存储数据,避免内存碎片化和过多的 Map/对象开销\n * 提供高效的读写接口,支持按需更新和查询\n */\nexport const createQuoteStateV1 = (): IQuoteState => {\n // 内部数据结构的设计需要考虑高效的读写性能,防止内存碎片化\n const data: (string | number)[] = [];\n const products: string[] = [];\n const mapProductIdToIndex = new Map<string, number>();\n // 0~20 (10 fields * 2 (value, updated_at))\n const getFieldOffset = (product_id: string, field: string): number | undefined => {\n let baseIndex = mapProductIdToIndex.get(product_id);\n if (baseIndex === undefined) {\n return undefined;\n }\n const fieldOffset = mapFieldNameToOffset[field];\n if (fieldOffset === undefined) throw newError('INVALID_FIELD_NAME', { field, available_fields: FIELDS });\n return baseIndex + fieldOffset;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) return undefined;\n const value = data[offset] as string;\n if (value === undefined) return undefined;\n const updated_at = data[offset + 1] as number;\n return [value, updated_at];\n };\n\n const setValueTuple = (product_id: string, field: IQuoteKey, value: string, updated_at: number) => {\n let offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n const baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;\n products.push(product_id);\n mapProductIdToIndex.set(product_id, baseIndex);\n offset = baseIndex + mapFieldNameToOffset[field];\n }\n data[offset] = value;\n data[offset + 1] = updated_at;\n };\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const oldTuple = getValueTuple(product_id, field);\n if (oldTuple === undefined || updated_at >= oldTuple[1]) {\n setValueTuple(product_id, field, value, updated_at);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of products) {\n result[product_id] = {};\n }\n for (let i = 0; i < data.length; i += 2) {\n const value = data[i] as string;\n if (value === undefined) continue;\n const updated_at = data[i + 1] as number;\n\n const productIndex = Math.floor(i / (FIELD_COUNT * 2));\n const product_id = products[productIndex];\n\n const fieldIndex = (i % (FIELD_COUNT * 2)) / 2;\n const field_name = FIELDS[fieldIndex];\n\n result[product_id][field_name] = [value, updated_at];\n }\n return result;\n };\n\n /**\n * 过滤状态,返回指定 product_id 列表和字段列表中,且更新时间不早于指定时间的字段数据\n * @param product_ids - 需要过滤的 product_id 列表\n * @param fields - 需要过滤的字段列表\n * @param updated_at - 需要过滤的更新时间阈值 (仅返回更新时间不早于该值的字段)\n * @returns 过滤后的数据\n */\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): 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 = getValueTuple(product_id, field);\n if (tuple && tuple[1] >= updated_at) {\n result[product_id]![field] = tuple;\n }\n }\n }\n return result;\n };\n\n const filterValues = <K extends IQuoteKey>(\n product_ids: string[],\n fields: K[],\n ): Record<string, Record<K, string>> => {\n const result: Record<string, Record<K, string>> = {};\n for (const product_id of product_ids) {\n result[product_id] = {} as Record<K, string>;\n for (const field of fields) {\n const tuple = getValueTuple(product_id, field);\n result[product_id][field] = tuple ? tuple[0] : '';\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter, filterValues };\n};\n"]}
1
+ {"version":3,"file":"v1.js","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,wBAAwB;AACxB,MAAM,MAAM,GAAG,CAAC,CAAC,CAAiC,EAAE,EAAE,CAAC,M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import { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n// TRICK: 固定字段顺序,方便计算偏移量\nconst FIELDS = ((x: { [key in IQuoteKey]: number }) => Object.keys(x).sort() as IQuoteKey[])({\n // TS TRICK: 强制运行时数组具有 IQuoteKey 的所有字段。不重不漏,味道真是好极了\n last_price: 0,\n ask_price: 0,\n ask_volume: 0,\n bid_volume: 0,\n bid_price: 0,\n interest_rate_short: 0,\n open_interest: 0,\n interest_rate_prev_settled_at: 0,\n interest_rate_next_settled_at: 0,\n interest_rate_long: 0,\n});\n\nconst FIELD_COUNT = FIELDS.length;\nconst mapFieldNameToOffset = Object.fromEntries(FIELDS.map((field, index) => [field, index * 2]));\n\n/**\n * 高效的行情状态管理器\n * 内部使用扁平化数组存储数据,避免内存碎片化和过多的 Map/对象开销\n * 提供高效的读写接口,支持按需更新和查询\n */\nexport const createQuoteStateV1 = (): IQuoteState => {\n // 内部数据结构的设计需要考虑高效的读写性能,防止内存碎片化\n const data: (string | number)[] = [];\n const products: string[] = [];\n const mapProductIdToIndex = new Map<string, number>();\n // 0~20 (10 fields * 2 (value, updated_at))\n const getFieldOffset = (product_id: string, field: string): number | undefined => {\n let baseIndex = mapProductIdToIndex.get(product_id);\n if (baseIndex === undefined) {\n return undefined;\n }\n const fieldOffset = mapFieldNameToOffset[field];\n if (fieldOffset === undefined) throw newError('INVALID_FIELD_NAME', { field, available_fields: FIELDS });\n return baseIndex + fieldOffset;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) return undefined;\n const value = data[offset] as string;\n if (value === undefined) return undefined;\n const updated_at = data[offset + 1] as number;\n return [value, updated_at];\n };\n\n const setValueTuple = (product_id: string, field: IQuoteKey, value: string, updated_at: number) => {\n let offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n const baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;\n products.push(product_id);\n mapProductIdToIndex.set(product_id, baseIndex);\n offset = baseIndex + mapFieldNameToOffset[field];\n }\n data[offset] = value;\n data[offset + 1] = updated_at;\n };\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const oldTuple = getValueTuple(product_id, field);\n if (oldTuple === undefined || updated_at >= oldTuple[1]) {\n setValueTuple(product_id, field, value, updated_at);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of products) {\n result[product_id] = {};\n }\n for (let i = 0; i < data.length; i += 2) {\n const value = data[i] as string;\n if (value === undefined) continue;\n const updated_at = data[i + 1] as number;\n\n const productIndex = Math.floor(i / (FIELD_COUNT * 2));\n const product_id = products[productIndex];\n\n const fieldIndex = (i % (FIELD_COUNT * 2)) / 2;\n const field_name = FIELDS[fieldIndex];\n\n result[product_id][field_name] = [value, updated_at];\n }\n return result;\n };\n\n /**\n * 过滤状态,返回指定 product_id 列表和字段列表中,且更新时间不早于指定时间的字段数据\n * @param product_ids - 需要过滤的 product_id 列表\n * @param fields - 需要过滤的字段列表\n * @param updated_at - 需要过滤的更新时间阈值 (仅返回更新时间不早于该值的字段)\n * @returns 过滤后的数据\n */\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): 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 = getValueTuple(product_id, field);\n if (tuple && tuple[1] >= updated_at) {\n result[product_id]![field] = tuple;\n }\n }\n }\n return result;\n };\n\n const filterValues = <K extends IQuoteKey>(\n product_ids: string[],\n fields: K[],\n ): Record<string, Record<K, string>> => {\n const result: Record<string, Record<K, string>> = {};\n for (const product_id of product_ids) {\n result[product_id] = {} as Record<K, string>;\n for (const field of fields) {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n result[product_id][field] = '';\n } else {\n result[product_id][field] = (data[offset] as string) || '';\n }\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter, filterValues };\n};\n"]}
@@ -0,0 +1,244 @@
1
+ import { parseQuoteServiceMetadataFromSchema } from '@yuants/exchange';
2
+ import { Terminal } from '@yuants/protocol';
3
+ import { encodePath, formatTime } from '@yuants/utils';
4
+ import { filter, from, map, mergeMap, tap, toArray } from 'rxjs';
5
+ import { quoteState } from './state';
6
+ const createFifoQueue = () => ({ items: [], head: 0 });
7
+ const fifoEnqueue = (queue, value) => {
8
+ queue.items.push(value);
9
+ };
10
+ const fifoDequeue = (queue) => {
11
+ if (queue.head >= queue.items.length)
12
+ return undefined;
13
+ const value = queue.items[queue.head++];
14
+ if (queue.head > 1024 && queue.head * 2 > queue.items.length) {
15
+ queue.items = queue.items.slice(queue.head);
16
+ queue.head = 0;
17
+ }
18
+ return value;
19
+ };
20
+ const fifoSize = (queue) => queue.items.length - queue.head;
21
+ const isTraceEnabled = process.env.VEX_QUOTE_UPSTREAM_REFINE_TRACE === '1';
22
+ // Query -> Make Cell Dirty -> Trigger Dirty Check
23
+ // Dirty Check():
24
+ // Acquire dirtyCheck lock
25
+ // Foreach Dirty cell in parallel:
26
+ //. Routing the dirty cell to ServiceID
27
+ //. Acquire Service Lock
28
+ //. if success:
29
+ // request the service and wait for response
30
+ //. release service lock
31
+ // clean cells
32
+ //. if failed: do nothing.
33
+ // Release dirtyCheck lock
34
+ // if any cell dirty remaining, call dirtyCheck()
35
+ // (service_id, service_group_id) -> sync func state (hash func)
36
+ const services = [];
37
+ const mapGroupIdToServices = new Map();
38
+ // (product_id, field, service_group_id, is_dirty = true, is_fetching)
39
+ // use-case 1: find unique product_ids (not is_fetching) by service_group_id
40
+ // use-case 2: mark
41
+ /**
42
+ * Use cases:
43
+ * - upsert items (when queryQuotes)
44
+ * - find unique product_ids (is_dirty = true and is_fetching = false) by service_group_id
45
+ * - when service fired, update is_fetching = true for some items (product_id, field)
46
+ * - when service failed, update is_fetching = false for some items (product_id, field)
47
+ * - when service success, update is_fetching = false and is_dirty = false for some items (product_id, field)
48
+ */
49
+ export const cells = [];
50
+ const mapCellKeyToCell = new Map();
51
+ const mapGroupIdToState = new Map();
52
+ const getOrCreateGroupState = (group_id) => {
53
+ const existing = mapGroupIdToState.get(group_id);
54
+ if (existing)
55
+ return existing;
56
+ const next = {
57
+ productQueue: createFifoQueue(),
58
+ inQueue: new Set(),
59
+ fetchingProducts: new Set(),
60
+ dirtyFieldsByProduct: new Map(),
61
+ };
62
+ mapGroupIdToState.set(group_id, next);
63
+ return next;
64
+ };
65
+ const route = (product_id, field) => {
66
+ for (const service of services) {
67
+ if (!product_id.startsWith(service.meta.product_id_prefix))
68
+ continue;
69
+ if (!service.metaFieldsSet.has(field))
70
+ continue;
71
+ return service.service_group_id;
72
+ }
73
+ return '';
74
+ };
75
+ const enqueueDirty = (params) => {
76
+ var _a;
77
+ const { product_id, field, service_group_id } = params;
78
+ if (!service_group_id)
79
+ return;
80
+ const groupState = getOrCreateGroupState(service_group_id);
81
+ const dirtyFields = (_a = groupState.dirtyFieldsByProduct.get(product_id)) !== null && _a !== void 0 ? _a : (() => {
82
+ const next = new Set();
83
+ groupState.dirtyFieldsByProduct.set(product_id, next);
84
+ return next;
85
+ })();
86
+ dirtyFields.add(field);
87
+ if (groupState.fetchingProducts.has(product_id))
88
+ return;
89
+ if (groupState.inQueue.has(product_id))
90
+ return;
91
+ groupState.inQueue.add(product_id);
92
+ fifoEnqueue(groupState.productQueue, product_id);
93
+ };
94
+ export const markDirty = (product_id, field) => {
95
+ const service_group_id = route(product_id, field);
96
+ const cellKey = encodePath(product_id, field);
97
+ const existing = mapCellKeyToCell.get(cellKey);
98
+ if (!existing) {
99
+ const cell = {
100
+ product_id,
101
+ field,
102
+ service_group_id,
103
+ is_dirty: true,
104
+ is_fetching: false,
105
+ round: 0,
106
+ };
107
+ mapCellKeyToCell.set(cellKey, cell);
108
+ cells.push(cell);
109
+ }
110
+ else {
111
+ existing.service_group_id = service_group_id;
112
+ existing.is_dirty = true;
113
+ }
114
+ enqueueDirty({ product_id, field, service_group_id });
115
+ scheduleServiceGroupId(service_group_id);
116
+ };
117
+ const isServiceIdRunning = new Set();
118
+ const scheduleServiceGroupId = (serviceGroupId) => {
119
+ if (!serviceGroupId)
120
+ return;
121
+ const serviceList = mapGroupIdToServices.get(serviceGroupId);
122
+ if (!serviceList || serviceList.length === 0)
123
+ return;
124
+ for (const service of serviceList) {
125
+ if (isServiceIdRunning.has(service.service_id))
126
+ continue;
127
+ isServiceIdRunning.add(service.service_id);
128
+ handleService(service).finally(() => {
129
+ isServiceIdRunning.delete(service.service_id);
130
+ });
131
+ }
132
+ };
133
+ const handleService = async (service) => {
134
+ var _a;
135
+ const groupState = mapGroupIdToState.get(service.service_group_id);
136
+ if (!groupState)
137
+ return;
138
+ while (true) {
139
+ const maxProducts = (_a = service.meta.max_products_per_request) !== null && _a !== void 0 ? _a : Infinity;
140
+ const fetchedFieldsByProduct = new Map();
141
+ const productsToFetch = [];
142
+ const cellsToFetch = [];
143
+ while (productsToFetch.length < maxProducts && fifoSize(groupState.productQueue) > 0) {
144
+ const product_id = fifoDequeue(groupState.productQueue);
145
+ if (!product_id)
146
+ break;
147
+ groupState.inQueue.delete(product_id);
148
+ if (groupState.fetchingProducts.has(product_id))
149
+ continue;
150
+ const dirtyFields = groupState.dirtyFieldsByProduct.get(product_id);
151
+ if (!dirtyFields || dirtyFields.size === 0)
152
+ continue;
153
+ const matchedFields = [];
154
+ for (const f of dirtyFields) {
155
+ if (!service.metaFieldsSet.has(f))
156
+ continue;
157
+ const cell = mapCellKeyToCell.get(encodePath(product_id, f));
158
+ if (!cell)
159
+ continue;
160
+ if (cell.service_group_id !== service.service_group_id)
161
+ continue;
162
+ if (!cell.is_dirty)
163
+ continue;
164
+ if (cell.is_fetching)
165
+ continue;
166
+ matchedFields.push(f);
167
+ cellsToFetch.push(cell);
168
+ }
169
+ if (matchedFields.length === 0)
170
+ continue;
171
+ productsToFetch.push(product_id);
172
+ groupState.fetchingProducts.add(product_id);
173
+ fetchedFieldsByProduct.set(product_id, new Set(matchedFields));
174
+ }
175
+ if (productsToFetch.length === 0)
176
+ return;
177
+ for (const cell of cellsToFetch) {
178
+ cell.round++;
179
+ cell.is_fetching = true;
180
+ }
181
+ const res = await makeRequest(service, productsToFetch).catch(() => ({}));
182
+ quoteState.update(res);
183
+ for (const cell of cellsToFetch) {
184
+ cell.is_fetching = false;
185
+ cell.is_dirty = false;
186
+ }
187
+ for (const product_id of productsToFetch) {
188
+ groupState.fetchingProducts.delete(product_id);
189
+ const dirtyFields = groupState.dirtyFieldsByProduct.get(product_id);
190
+ const fetchedFields = fetchedFieldsByProduct.get(product_id);
191
+ if (!dirtyFields || !fetchedFields)
192
+ continue;
193
+ for (const f of fetchedFields) {
194
+ dirtyFields.delete(f);
195
+ }
196
+ if (dirtyFields.size === 0) {
197
+ groupState.dirtyFieldsByProduct.delete(product_id);
198
+ continue;
199
+ }
200
+ if (!groupState.inQueue.has(product_id)) {
201
+ groupState.inQueue.add(product_id);
202
+ fifoEnqueue(groupState.productQueue, product_id);
203
+ }
204
+ }
205
+ }
206
+ };
207
+ const terminal = Terminal.fromNodeEnv();
208
+ const makeRequest = async (service, product_ids) => {
209
+ const res = await terminal.client.requestForResponseData('GetQuotes', {
210
+ product_ids,
211
+ fields: service.meta.fields,
212
+ });
213
+ if (isTraceEnabled) {
214
+ console.info(formatTime(Date.now()), `[VEX][Quote][Refine]GetQuotes`, `service_id=${service.service_id}`, `group=${service.service_group_id}`, `products=${product_ids.length}`);
215
+ }
216
+ return res;
217
+ };
218
+ terminal.terminalInfos$
219
+ .pipe(mergeMap((terminalInfos) => from(terminalInfos).pipe(mergeMap((terminalInfo) => from(Object.values(terminalInfo.serviceInfo || {})).pipe(filter((serviceInfo) => serviceInfo.method === 'GetQuotes'), map((serviceInfo) => {
220
+ var _a;
221
+ try {
222
+ const meta = parseQuoteServiceMetadataFromSchema(serviceInfo.schema);
223
+ const service_group_id = encodePath(meta.product_id_prefix, meta.fields.join(','), (_a = meta.max_products_per_request) !== null && _a !== void 0 ? _a : '');
224
+ return {
225
+ service_id: serviceInfo.service_id,
226
+ service_group_id,
227
+ meta,
228
+ metaFieldsSet: new Set(meta.fields),
229
+ };
230
+ }
231
+ catch (_b) { }
232
+ }), filter((x) => !!x))), toArray(), tap((x) => {
233
+ services.length = 0;
234
+ mapGroupIdToServices.clear();
235
+ x.forEach((service) => {
236
+ var _a;
237
+ services.push(service);
238
+ const list = (_a = mapGroupIdToServices.get(service.service_group_id)) !== null && _a !== void 0 ? _a : [];
239
+ list.push(service);
240
+ mapGroupIdToServices.set(service.service_group_id, list);
241
+ });
242
+ }))))
243
+ .subscribe();
244
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../../src/quote/scheduler.ts"],"names":[],"mappings":"AACA,OAAO,EAAsC,mCAAmC,EAAE,MAAM,kBAAkB,CAAC;AAC3G,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAuBrC,MAAM,eAAe,GAAG,GAAqB,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;AAEzE,MAAM,WAAW,GAAG,CAAI,KAAoB,EAAE,KAAQ,EAAE,EAAE;IACxD,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAI,KAAoB,EAAiB,EAAE;IAC7D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AI,CAAC,MAAM,CAAC;SACpC,CAAC;KACH;IAAC,WAAM,GAAE;AACZ,CAAC,CAAC,EACF,MAAM,CAAC,CAAC,CAAC,EAAqC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CACtD,CACF,EACD,OAAO,EAAE,EACT,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;IACR,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IACpB,oBAAoB,CAAC,KAAK,EAAE,CAAC;IAC7B,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;;QACpB,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,MAAM,IAAI,GAAG,MAAA,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,mCAAI,EAAE,CAAC;QACtE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACnB,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CACH,CACF,CACF;KACA,SAAS,EAAE,CAAC","sourcesContent":["import { IQuoteUpdateAction } from '@yuants/data-quote';\nimport { IQuoteField, IQuoteServiceMetadata, parseQuoteServiceMetadataFromSchema } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath, formatTime } from '@yuants/utils';\nimport { filter, from, map, mergeMap, tap, toArray } from 'rxjs';\nimport { quoteState } from './state';\n\ninterface IQuoteService {\n service_id: string;\n service_group_id: string;\n meta: IQuoteServiceMetadata;\n metaFieldsSet: Set<IQuoteField>;\n}\n\ninterface ICellState {\n product_id: string;\n field: IQuoteField;\n service_group_id: string;\n is_dirty: boolean;\n is_fetching: boolean;\n round: number;\n}\n\ntype IFifoQueue<T> = {\n items: T[];\n head: number;\n};\n\nconst createFifoQueue = <T>(): IFifoQueue<T> => ({ items: [], head: 0 });\n\nconst fifoEnqueue = <T>(queue: IFifoQueue<T>, value: T) => {\n queue.items.push(value);\n};\n\nconst fifoDequeue = <T>(queue: IFifoQueue<T>): T | undefined => {\n if (queue.head >= queue.items.length) return undefined;\n const value = queue.items[queue.head++];\n if (queue.head > 1024 && queue.head * 2 > queue.items.length) {\n queue.items = queue.items.slice(queue.head);\n queue.head = 0;\n }\n return value;\n};\n\nconst fifoSize = <T>(queue: IFifoQueue<T>): number => queue.items.length - queue.head;\n\nconst isTraceEnabled = process.env.VEX_QUOTE_UPSTREAM_REFINE_TRACE === '1';\n\n// Query -> Make Cell Dirty -> Trigger Dirty Check\n\n// Dirty Check():\n// Acquire dirtyCheck lock\n// Foreach Dirty cell in parallel:\n//. Routing the dirty cell to ServiceID\n//. Acquire Service Lock\n//. if success:\n// request the service and wait for response\n//. release service lock\n// clean cells\n//. if failed: do nothing.\n// Release dirtyCheck lock\n// if any cell dirty remaining, call dirtyCheck()\n\n// (service_id, service_group_id) -> sync func state (hash func)\nconst services: Array<IQuoteService> = [];\nconst mapGroupIdToServices = new Map<string, IQuoteService[]>();\n\n// (product_id, field, service_group_id, is_dirty = true, is_fetching)\n// use-case 1: find unique product_ids (not is_fetching) by service_group_id\n// use-case 2: mark\n/**\n * Use cases:\n * - upsert items (when queryQuotes)\n * - find unique product_ids (is_dirty = true and is_fetching = false) by service_group_id\n * - when service fired, update is_fetching = true for some items (product_id, field)\n * - when service failed, update is_fetching = false for some items (product_id, field)\n * - when service success, update is_fetching = false and is_dirty = false for some items (product_id, field)\n */\nexport const cells: Array<ICellState> = [];\nconst mapCellKeyToCell = new Map<string, ICellState>();\n\ntype IGroupState = {\n productQueue: IFifoQueue<string>;\n inQueue: Set<string>;\n fetchingProducts: Set<string>;\n dirtyFieldsByProduct: Map<string, Set<IQuoteField>>;\n};\n\nconst mapGroupIdToState = new Map<string, IGroupState>();\n\nconst getOrCreateGroupState = (group_id: string): IGroupState => {\n const existing = mapGroupIdToState.get(group_id);\n if (existing) return existing;\n const next: IGroupState = {\n productQueue: createFifoQueue(),\n inQueue: new Set<string>(),\n fetchingProducts: new Set<string>(),\n dirtyFieldsByProduct: new Map<string, Set<IQuoteField>>(),\n };\n mapGroupIdToState.set(group_id, next);\n return next;\n};\n\nconst route = (product_id: string, field: IQuoteField) => {\n for (const service of services) {\n if (!product_id.startsWith(service.meta.product_id_prefix)) continue;\n if (!service.metaFieldsSet.has(field)) continue;\n return service.service_group_id;\n }\n return '';\n};\n\nconst enqueueDirty = (params: { product_id: string; field: IQuoteField; service_group_id: string }) => {\n const { product_id, field, service_group_id } = params;\n if (!service_group_id) return;\n const groupState = getOrCreateGroupState(service_group_id);\n const dirtyFields =\n groupState.dirtyFieldsByProduct.get(product_id) ??\n (() => {\n const next = new Set<IQuoteField>();\n groupState.dirtyFieldsByProduct.set(product_id, next);\n return next;\n })();\n dirtyFields.add(field);\n\n if (groupState.fetchingProducts.has(product_id)) return;\n if (groupState.inQueue.has(product_id)) return;\n groupState.inQueue.add(product_id);\n fifoEnqueue(groupState.productQueue, product_id);\n};\n\nexport const markDirty = (product_id: string, field: IQuoteField) => {\n const service_group_id = route(product_id, field);\n\n const cellKey = encodePath(product_id, field);\n const existing = mapCellKeyToCell.get(cellKey);\n if (!existing) {\n const cell: ICellState = {\n product_id,\n field,\n service_group_id,\n is_dirty: true,\n is_fetching: false,\n round: 0,\n };\n mapCellKeyToCell.set(cellKey, cell);\n cells.push(cell);\n } else {\n existing.service_group_id = service_group_id;\n existing.is_dirty = true;\n }\n\n enqueueDirty({ product_id, field, service_group_id });\n scheduleServiceGroupId(service_group_id);\n};\n\nconst isServiceIdRunning = new Set<string>();\n\nconst scheduleServiceGroupId = (serviceGroupId: string): void => {\n if (!serviceGroupId) return;\n const serviceList = mapGroupIdToServices.get(serviceGroupId);\n if (!serviceList || serviceList.length === 0) return;\n\n for (const service of serviceList) {\n if (isServiceIdRunning.has(service.service_id)) continue;\n isServiceIdRunning.add(service.service_id);\n handleService(service).finally(() => {\n isServiceIdRunning.delete(service.service_id);\n });\n }\n};\n\nconst handleService = async (service: IQuoteService) => {\n const groupState = mapGroupIdToState.get(service.service_group_id);\n if (!groupState) return;\n\n while (true) {\n const maxProducts = service.meta.max_products_per_request ?? Infinity;\n const fetchedFieldsByProduct = new Map<string, Set<IQuoteField>>();\n const productsToFetch: string[] = [];\n const cellsToFetch: ICellState[] = [];\n\n while (productsToFetch.length < maxProducts && fifoSize(groupState.productQueue) > 0) {\n const product_id = fifoDequeue(groupState.productQueue);\n if (!product_id) break;\n groupState.inQueue.delete(product_id);\n\n if (groupState.fetchingProducts.has(product_id)) continue;\n const dirtyFields = groupState.dirtyFieldsByProduct.get(product_id);\n if (!dirtyFields || dirtyFields.size === 0) continue;\n\n const matchedFields: IQuoteField[] = [];\n for (const f of dirtyFields) {\n if (!service.metaFieldsSet.has(f)) continue;\n const cell = mapCellKeyToCell.get(encodePath(product_id, f));\n if (!cell) continue;\n if (cell.service_group_id !== service.service_group_id) continue;\n if (!cell.is_dirty) continue;\n if (cell.is_fetching) continue;\n matchedFields.push(f);\n cellsToFetch.push(cell);\n }\n\n if (matchedFields.length === 0) continue;\n productsToFetch.push(product_id);\n groupState.fetchingProducts.add(product_id);\n fetchedFieldsByProduct.set(product_id, new Set(matchedFields));\n }\n\n if (productsToFetch.length === 0) return;\n\n for (const cell of cellsToFetch) {\n cell.round++;\n cell.is_fetching = true;\n }\n\n const res = await makeRequest(service, productsToFetch).catch(() => ({} as IQuoteUpdateAction));\n quoteState.update(res);\n\n for (const cell of cellsToFetch) {\n cell.is_fetching = false;\n cell.is_dirty = false;\n }\n\n for (const product_id of productsToFetch) {\n groupState.fetchingProducts.delete(product_id);\n const dirtyFields = groupState.dirtyFieldsByProduct.get(product_id);\n const fetchedFields = fetchedFieldsByProduct.get(product_id);\n if (!dirtyFields || !fetchedFields) continue;\n for (const f of fetchedFields) {\n dirtyFields.delete(f);\n }\n if (dirtyFields.size === 0) {\n groupState.dirtyFieldsByProduct.delete(product_id);\n continue;\n }\n if (!groupState.inQueue.has(product_id)) {\n groupState.inQueue.add(product_id);\n fifoEnqueue(groupState.productQueue, product_id);\n }\n }\n }\n};\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst makeRequest = async (service: IQuoteService, product_ids: string[]): Promise<IQuoteUpdateAction> => {\n const res = await terminal.client.requestForResponseData<{}, IQuoteUpdateAction>('GetQuotes', {\n product_ids,\n fields: service.meta.fields,\n });\n if (isTraceEnabled) {\n console.info(\n formatTime(Date.now()),\n `[VEX][Quote][Refine]GetQuotes`,\n `service_id=${service.service_id}`,\n `group=${service.service_group_id}`,\n `products=${product_ids.length}`,\n );\n }\n return res;\n};\n\nterminal.terminalInfos$\n .pipe(\n mergeMap((terminalInfos) =>\n from(terminalInfos).pipe(\n mergeMap((terminalInfo) =>\n from(Object.values(terminalInfo.serviceInfo || {})).pipe(\n filter((serviceInfo) => serviceInfo.method === 'GetQuotes'),\n map((serviceInfo): IQuoteService | undefined => {\n try {\n const meta = parseQuoteServiceMetadataFromSchema(serviceInfo.schema);\n const service_group_id = encodePath(\n meta.product_id_prefix,\n (meta.fields as any[]).join(','),\n meta.max_products_per_request ?? '',\n );\n return {\n service_id: serviceInfo.service_id,\n service_group_id,\n meta,\n metaFieldsSet: new Set(meta.fields),\n };\n } catch {}\n }),\n filter((x): x is Exclude<typeof x, undefined> => !!x),\n ),\n ),\n toArray(),\n tap((x) => {\n services.length = 0;\n mapGroupIdToServices.clear();\n x.forEach((service) => {\n services.push(service);\n const list = mapGroupIdToServices.get(service.service_group_id) ?? [];\n list.push(service);\n mapGroupIdToServices.set(service.service_group_id, list);\n });\n }),\n ),\n ),\n )\n .subscribe();\n"]}
@@ -1,11 +1,7 @@
1
1
  import { Terminal } from '@yuants/protocol';
2
- import { formatTime } from '@yuants/utils';
3
- import { Subject, concatMap, defer } from 'rxjs';
4
- import { createQuoteState } from './state';
5
- import { createQuoteProviderRegistry } from './upstream';
2
+ import { quoteState } from './state';
3
+ import { markDirty } from './scheduler';
6
4
  const terminal = Terminal.fromNodeEnv();
7
- const quoteState = createQuoteState();
8
- const quoteProviderRegistry = createQuoteProviderRegistry(terminal);
9
5
  const normalizeStrings = (values) => [...new Set(values)].sort();
10
6
  const normalizeFields = (values) => [...new Set(values)].sort();
11
7
  const analyzeRequestedQuotes = (quoteState, product_ids, fields, updated_at) => {
@@ -24,60 +20,6 @@ const analyzeRequestedQuotes = (quoteState, product_ids, fields, updated_at) =>
24
20
  }
25
21
  return { needUpdate };
26
22
  };
27
- const updateQueue$ = new Subject();
28
- const updateQueueStats = {
29
- queued_total: 0,
30
- started_total: 0,
31
- processed_total: 0,
32
- };
33
- const getQueueStatus = () => {
34
- const pending = updateQueueStats.queued_total - updateQueueStats.started_total;
35
- const in_flight = updateQueueStats.started_total - updateQueueStats.processed_total;
36
- return Object.assign({ pending,
37
- in_flight }, updateQueueStats);
38
- };
39
- const enqueueUpdateTask = (task) => {
40
- updateQueueStats.queued_total++;
41
- updateQueueStats.last_enqueued_at = Date.now();
42
- updateQueue$.next(task);
43
- };
44
- const summarizeError = (error) => {
45
- if (typeof error === 'object' && error !== null) {
46
- const code = 'code' in error ? error.code : undefined;
47
- const message = 'message' in error ? error.message : undefined;
48
- return {
49
- code: typeof code === 'string' ? code : undefined,
50
- message: typeof message === 'string' ? message : undefined,
51
- };
52
- }
53
- return {};
54
- };
55
- const processUpdateTask = async (task) => {
56
- const { needUpdate } = analyzeRequestedQuotes(quoteState, task.product_ids, task.fields, task.updated_at);
57
- await quoteProviderRegistry.fillQuoteStateFromUpstream({
58
- quoteState,
59
- cacheMissed: needUpdate,
60
- updated_at: task.updated_at,
61
- });
62
- };
63
- updateQueue$
64
- .pipe(concatMap((task) => defer(async () => {
65
- updateQueueStats.started_total++;
66
- updateQueueStats.last_started_at = Date.now();
67
- try {
68
- await processUpdateTask(task);
69
- }
70
- catch (error) {
71
- const summary = summarizeError(error);
72
- updateQueueStats.last_error = Object.assign({ at: Date.now() }, summary);
73
- console.info(formatTime(Date.now()), `[VEX][Quote]UpdateQueueTaskFailed`, `product_ids=${task.product_ids.length} fields=${task.fields.length} updated_at=${task.updated_at}`, JSON.stringify(summary));
74
- }
75
- finally {
76
- updateQueueStats.processed_total++;
77
- updateQueueStats.last_processed_at = Date.now();
78
- }
79
- })))
80
- .subscribe();
81
23
  terminal.server.provideService('VEX/UpdateQuotes', {}, async (msg) => {
82
24
  quoteState.update(msg.req);
83
25
  return { res: { code: 0, message: 'OK' } };
@@ -103,16 +45,11 @@ terminal.server.provideService('VEX/QueryQuotes', {
103
45
  const product_ids = normalizeStrings(msg.req.product_ids);
104
46
  const fields = normalizeFields(msg.req.fields);
105
47
  const { updated_at } = msg.req;
106
- // SWR strategy: if we have stale or missing data, enqueue an update task,
107
- // but still return the current data immediately.
108
48
  const { needUpdate } = analyzeRequestedQuotes(quoteState, product_ids, fields, updated_at);
109
- if (needUpdate.length > 0) {
110
- enqueueUpdateTask({ product_ids, fields, updated_at });
49
+ for (const { product_id, field } of needUpdate) {
50
+ markDirty(product_id, field);
111
51
  }
112
52
  const data = quoteState.filterValues(product_ids, fields);
113
53
  return { res: { code: 0, message: 'OK', data } };
114
54
  });
115
- terminal.server.provideService('VEX/QuoteUpdateQueueStatus', {}, async () => {
116
- return { res: { code: 0, message: 'OK', data: getQueueStatus() } };
117
- });
118
55
  //# sourceMappingURL=service.js.map
@@ -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,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,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,UAA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AAE,MAAM,CAAC,CAAC;IAC1D,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 } 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): { needUpdate: 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 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 { needUpdate };\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 Record<string, Partial<Record<IQuoteKey, string>>>\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 = normalizeStrings(msg.req.product_ids);\n const fields = normalizeFields(msg.req.fields);\n const { updated_at } = msg.req;\n\n // SWR strategy: if we have stale or missing data, enqueue an update task,\n // but still return the current data immediately.\n const { 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 = quoteState.filterValues(product_ids, fields);\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
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../src/quote/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,gBAAgB,GAAG,CAAC,MAAgB,EAAE,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,E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import { quoteState } from './state';\nimport { IQuoteKey, IQuoteRequire, IQuoteState, IQuoteUpdateAction } from './types';\nimport { markDirty } from './scheduler';\n\nconst terminal = Terminal.fromNodeEnv();\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): { needUpdate: 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 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 { needUpdate };\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\nterminal.server.provideService<\n { product_ids: string[]; fields: IQuoteKey[]; updated_at: number },\n Record<string, Partial<Record<IQuoteKey, string>>>\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 = normalizeStrings(msg.req.product_ids);\n const fields = normalizeFields(msg.req.fields);\n const { updated_at } = msg.req;\n\n const { needUpdate } = analyzeRequestedQuotes(quoteState, product_ids, fields, updated_at);\n for (const { product_id, field } of needUpdate) {\n markDirty(product_id, field as any as IQuoteField);\n }\n\n const data = quoteState.filterValues(product_ids, fields);\n return { res: { code: 0, message: 'OK', data } };\n },\n);\n"]}
@@ -1,4 +1,5 @@
1
1
  import { implementations } from './implementations';
2
2
  // 使用 v1 作为生产版本
3
3
  export const createQuoteState = implementations.v1;
4
+ export const quoteState = createQuoteState();
4
5
  //# sourceMappingURL=state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/quote/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,eAAe;AACf,MAAM,CAAC,MAAM,gBAAgB,GAAG,eAAe,CAAC,EAAE,CAAC","sourcesContent":["import { implementations } from './implementations';\n\n// 使用 v1 作为生产版本\nexport const createQuoteState = implementations.v1;\n"]}
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/quote/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,eAAe;AACf,MAAM,CAAC,MAAM,gBAAgB,GAAG,eAAe,CAAC,EAAE,CAAC;AAEnD,MAAM,CAAC,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC","sourcesContent":["import { implementations } from './implementations';\n\n// 使用 v1 作为生产版本\nexport const createQuoteState = implementations.v1;\n\nexport const quoteState = createQuoteState();\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"v1.d.ts","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,WAAW,EAAsB,MAAM,UAAU,CAAC;AAoBtE;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,QAAO,WA6GrC,CAAC"}
1
+ {"version":3,"file":"v1.d.ts","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,WAAW,EAAsB,MAAM,UAAU,CAAC;AAoBtE;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,QAAO,WAiHrC,CAAC"}
@@ -116,8 +116,13 @@ const createQuoteStateV1 = () => {
116
116
  for (const product_id of product_ids) {
117
117
  result[product_id] = {};
118
118
  for (const field of fields) {
119
- const tuple = getValueTuple(product_id, field);
120
- result[product_id][field] = tuple ? tuple[0] : '';
119
+ const offset = getFieldOffset(product_id, field);
120
+ if (offset === undefined) {
121
+ result[product_id][field] = '';
122
+ }
123
+ else {
124
+ result[product_id][field] = data[offset] || '';
125
+ }
121
126
  }
122
127
  }
123
128
  return result;
@@ -1 +1 @@
1
- {"version":3,"file":"v1.js","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":";;;AAAA,yCAAyC;AAGzC,wBAAwB;AACxB,MAAM,MAAM,GAAG,CAAC,CAAC,CAAiC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAiB,CAAC,CAAC;IAC3F,mDAAmD;IACnD,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,mBAAmB,EAAE,CAAC;IACtB,aAAa,EAAE,CAAC;IAChB,6BAA6B,EAAE,CAAC;IAChC,6BAA6B,EAAE,CAAC;IAChC,kBAAkB,EAAE,CAAC;CACtB,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC;AAClC,MAAM,oBAAoB,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AAElG;;;;GAIG;AACI,MAAM,kBAAkB,GAAG,GAAgB,EAAE;IAClD,+BAA+B;IAC/B,MAAM,IAAI,GAAwB,EAAE,CAAC;IACrC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtD,2CAA2C;IAC3C,MAAM,cAAc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,KAAK,CAAC,CAAC;gBAC/C,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;aACnD;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AACvE,CAAC,CAAC;AA7GW,QAAA,kBAAkB,sBA6G7B","sourcesContent":["import { newError } from '@yuants/utils';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n// TRICK: 固定字段顺序,方便计算偏移量\nconst FIELDS = ((x: { [key in IQuoteKey]: number }) => Object.keys(x).sort() as IQuoteKey[])({\n // TS TRICK: 强制运行时数组具有 IQuoteKey 的所有字段。不重不漏,味道真是好极了\n last_price: 0,\n ask_price: 0,\n ask_volume: 0,\n bid_volume: 0,\n bid_price: 0,\n interest_rate_short: 0,\n open_interest: 0,\n interest_rate_prev_settled_at: 0,\n interest_rate_next_settled_at: 0,\n interest_rate_long: 0,\n});\n\nconst FIELD_COUNT = FIELDS.length;\nconst mapFieldNameToOffset = Object.fromEntries(FIELDS.map((field, index) => [field, index * 2]));\n\n/**\n * 高效的行情状态管理器\n * 内部使用扁平化数组存储数据,避免内存碎片化和过多的 Map/对象开销\n * 提供高效的读写接口,支持按需更新和查询\n */\nexport const createQuoteStateV1 = (): IQuoteState => {\n // 内部数据结构的设计需要考虑高效的读写性能,防止内存碎片化\n const data: (string | number)[] = [];\n const products: string[] = [];\n const mapProductIdToIndex = new Map<string, number>();\n // 0~20 (10 fields * 2 (value, updated_at))\n const getFieldOffset = (product_id: string, field: string): number | undefined => {\n let baseIndex = mapProductIdToIndex.get(product_id);\n if (baseIndex === undefined) {\n return undefined;\n }\n const fieldOffset = mapFieldNameToOffset[field];\n if (fieldOffset === undefined) throw newError('INVALID_FIELD_NAME', { field, available_fields: FIELDS });\n return baseIndex + fieldOffset;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) return undefined;\n const value = data[offset] as string;\n if (value === undefined) return undefined;\n const updated_at = data[offset + 1] as number;\n return [value, updated_at];\n };\n\n const setValueTuple = (product_id: string, field: IQuoteKey, value: string, updated_at: number) => {\n let offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n const baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;\n products.push(product_id);\n mapProductIdToIndex.set(product_id, baseIndex);\n offset = baseIndex + mapFieldNameToOffset[field];\n }\n data[offset] = value;\n data[offset + 1] = updated_at;\n };\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const oldTuple = getValueTuple(product_id, field);\n if (oldTuple === undefined || updated_at >= oldTuple[1]) {\n setValueTuple(product_id, field, value, updated_at);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of products) {\n result[product_id] = {};\n }\n for (let i = 0; i < data.length; i += 2) {\n const value = data[i] as string;\n if (value === undefined) continue;\n const updated_at = data[i + 1] as number;\n\n const productIndex = Math.floor(i / (FIELD_COUNT * 2));\n const product_id = products[productIndex];\n\n const fieldIndex = (i % (FIELD_COUNT * 2)) / 2;\n const field_name = FIELDS[fieldIndex];\n\n result[product_id][field_name] = [value, updated_at];\n }\n return result;\n };\n\n /**\n * 过滤状态,返回指定 product_id 列表和字段列表中,且更新时间不早于指定时间的字段数据\n * @param product_ids - 需要过滤的 product_id 列表\n * @param fields - 需要过滤的字段列表\n * @param updated_at - 需要过滤的更新时间阈值 (仅返回更新时间不早于该值的字段)\n * @returns 过滤后的数据\n */\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): 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 = getValueTuple(product_id, field);\n if (tuple && tuple[1] >= updated_at) {\n result[product_id]![field] = tuple;\n }\n }\n }\n return result;\n };\n\n const filterValues = <K extends IQuoteKey>(\n product_ids: string[],\n fields: K[],\n ): Record<string, Record<K, string>> => {\n const result: Record<string, Record<K, string>> = {};\n for (const product_id of product_ids) {\n result[product_id] = {} as Record<K, string>;\n for (const field of fields) {\n const tuple = getValueTuple(product_id, field);\n result[product_id][field] = tuple ? tuple[0] : '';\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter, filterValues };\n};\n"]}
1
+ {"version":3,"file":"v1.js","sourceRoot":"","sources":["../../../src/quote/implementations/v1.ts"],"names":[],"mappings":";;;AAAA,yCAAyC;AAGzC,wBAAwB;AACxB,MAAM,MAAM,GAAG,CAAC,CAAC,CAAiC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAiB,CAAC,CAAC;IAC3F,mDAAmD;IACnD,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,SAAS,EAAE,CAAC;IACZ,mBAAmB,EAAE,CAAC;IACtB,aAAa,EAAE,CAAC;IAChB,6BAA6B,EAAE,CAAC;IAChC,6BAA6B,EAAE,CAAC;IAChC,kBAAkB,EAAE,CAAC;CACtB,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC;AAClC,MAAM,oBAAoB,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AAElG;;;;GAIG;AACI,MAAM,kBAAkB,GAAG,GAAgB,EAAE;IA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,MAAM,UAAU,IAAI,WAAW,EAAE;YACpC,MAAM,CAAC,UAAU,CAAC,GAAG,EAAuB,CAAC;YAC7C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;gBAC1B,MAAM,MAAM,GAAG,cAAc,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;gBACjD,IAAI,MAAM,KAAK,SAAS,EAAE;oBACxB,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;iBAChC;qBAAM;oBACL,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAI,IAAI,CAAC,MAAM,CAAY,IAAI,EAAE,CAAC;iBAC5D;aACF;SACF;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AACvE,CAAC,CAAC;AAjHW,QAAA,kBAAkB,sBAiH7B","sourcesContent":["import { newError } from '@yuants/utils';\nimport { IQuoteKey, IQuoteState, IQuoteUpdateAction } from '../types';\n\n// TRICK: 固定字段顺序,方便计算偏移量\nconst FIELDS = ((x: { [key in IQuoteKey]: number }) => Object.keys(x).sort() as IQuoteKey[])({\n // TS TRICK: 强制运行时数组具有 IQuoteKey 的所有字段。不重不漏,味道真是好极了\n last_price: 0,\n ask_price: 0,\n ask_volume: 0,\n bid_volume: 0,\n bid_price: 0,\n interest_rate_short: 0,\n open_interest: 0,\n interest_rate_prev_settled_at: 0,\n interest_rate_next_settled_at: 0,\n interest_rate_long: 0,\n});\n\nconst FIELD_COUNT = FIELDS.length;\nconst mapFieldNameToOffset = Object.fromEntries(FIELDS.map((field, index) => [field, index * 2]));\n\n/**\n * 高效的行情状态管理器\n * 内部使用扁平化数组存储数据,避免内存碎片化和过多的 Map/对象开销\n * 提供高效的读写接口,支持按需更新和查询\n */\nexport const createQuoteStateV1 = (): IQuoteState => {\n // 内部数据结构的设计需要考虑高效的读写性能,防止内存碎片化\n const data: (string | number)[] = [];\n const products: string[] = [];\n const mapProductIdToIndex = new Map<string, number>();\n // 0~20 (10 fields * 2 (value, updated_at))\n const getFieldOffset = (product_id: string, field: string): number | undefined => {\n let baseIndex = mapProductIdToIndex.get(product_id);\n if (baseIndex === undefined) {\n return undefined;\n }\n const fieldOffset = mapFieldNameToOffset[field];\n if (fieldOffset === undefined) throw newError('INVALID_FIELD_NAME', { field, available_fields: FIELDS });\n return baseIndex + fieldOffset;\n };\n\n const getValueTuple = (product_id: string, field: IQuoteKey): [string, number] | undefined => {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) return undefined;\n const value = data[offset] as string;\n if (value === undefined) return undefined;\n const updated_at = data[offset + 1] as number;\n return [value, updated_at];\n };\n\n const setValueTuple = (product_id: string, field: IQuoteKey, value: string, updated_at: number) => {\n let offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n const baseIndex = mapProductIdToIndex.size * FIELD_COUNT * 2;\n products.push(product_id);\n mapProductIdToIndex.set(product_id, baseIndex);\n offset = baseIndex + mapFieldNameToOffset[field];\n }\n data[offset] = value;\n data[offset + 1] = updated_at;\n };\n\n const update = (action: IQuoteUpdateAction) => {\n for (const product_id in action) {\n const fields = action[product_id];\n for (const field_name in fields) {\n const field = field_name as IQuoteKey;\n const [value, updated_at] = fields[field]!;\n const oldTuple = getValueTuple(product_id, field);\n if (oldTuple === undefined || updated_at >= oldTuple[1]) {\n setValueTuple(product_id, field, value, updated_at);\n }\n }\n }\n };\n\n const dumpAsObject = (): IQuoteUpdateAction => {\n const result: IQuoteUpdateAction = {};\n for (const product_id of products) {\n result[product_id] = {};\n }\n for (let i = 0; i < data.length; i += 2) {\n const value = data[i] as string;\n if (value === undefined) continue;\n const updated_at = data[i + 1] as number;\n\n const productIndex = Math.floor(i / (FIELD_COUNT * 2));\n const product_id = products[productIndex];\n\n const fieldIndex = (i % (FIELD_COUNT * 2)) / 2;\n const field_name = FIELDS[fieldIndex];\n\n result[product_id][field_name] = [value, updated_at];\n }\n return result;\n };\n\n /**\n * 过滤状态,返回指定 product_id 列表和字段列表中,且更新时间不早于指定时间的字段数据\n * @param product_ids - 需要过滤的 product_id 列表\n * @param fields - 需要过滤的字段列表\n * @param updated_at - 需要过滤的更新时间阈值 (仅返回更新时间不早于该值的字段)\n * @returns 过滤后的数据\n */\n const filter = (product_ids: string[], fields: IQuoteKey[], updated_at: number): 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 = getValueTuple(product_id, field);\n if (tuple && tuple[1] >= updated_at) {\n result[product_id]![field] = tuple;\n }\n }\n }\n return result;\n };\n\n const filterValues = <K extends IQuoteKey>(\n product_ids: string[],\n fields: K[],\n ): Record<string, Record<K, string>> => {\n const result: Record<string, Record<K, string>> = {};\n for (const product_id of product_ids) {\n result[product_id] = {} as Record<K, string>;\n for (const field of fields) {\n const offset = getFieldOffset(product_id, field);\n if (offset === undefined) {\n result[product_id][field] = '';\n } else {\n result[product_id][field] = (data[offset] as string) || '';\n }\n }\n }\n return result;\n };\n\n return { update, dumpAsObject, getValueTuple, filter, filterValues };\n};\n"]}
@@ -0,0 +1,21 @@
1
+ import { IQuoteField } from '@yuants/exchange';
2
+ interface ICellState {
3
+ product_id: string;
4
+ field: IQuoteField;
5
+ service_group_id: string;
6
+ is_dirty: boolean;
7
+ is_fetching: boolean;
8
+ round: number;
9
+ }
10
+ /**
11
+ * Use cases:
12
+ * - upsert items (when queryQuotes)
13
+ * - find unique product_ids (is_dirty = true and is_fetching = false) by service_group_id
14
+ * - when service fired, update is_fetching = true for some items (product_id, field)
15
+ * - when service failed, update is_fetching = false for some items (product_id, field)
16
+ * - when service success, update is_fetching = false and is_dirty = false for some items (product_id, field)
17
+ */
18
+ export declare const cells: Array<ICellState>;
19
+ export declare const markDirty: (product_id: string, field: IQuoteField) => void;
20
+ export {};
21
+ //# sourceMappingURL=scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../src/quote/scheduler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAA8D,MAAM,kBAAkB,CAAC;AAa3G,UAAU,UAAU;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,WAAW,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,OAAO,CAAC;IAClB,WAAW,EAAE,OAAO,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;CACf;AAiDD;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,UAAU,CAAM,CAAC;AAqD3C,eAAO,MAAM,SAAS,eAAgB,MAAM,SAAS,WAAW,SAuB/D,CAAC"}