@yuants/app-virtual-exchange 0.11.1 → 0.11.3

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 (58) hide show
  1. package/dist/position.js +7 -15
  2. package/dist/position.js.map +1 -1
  3. package/dist/quote/implementations/v1.js +7 -2
  4. package/dist/quote/implementations/v1.js.map +1 -1
  5. package/dist/quote/scheduler.js +244 -0
  6. package/dist/quote/scheduler.js.map +1 -0
  7. package/dist/quote/service.js +4 -67
  8. package/dist/quote/service.js.map +1 -1
  9. package/dist/quote/state.js +1 -0
  10. package/dist/quote/state.js.map +1 -1
  11. package/lib/position.d.ts.map +1 -1
  12. package/lib/position.js +7 -15
  13. package/lib/position.js.map +1 -1
  14. package/lib/quote/implementations/v1.d.ts.map +1 -1
  15. package/lib/quote/implementations/v1.js +7 -2
  16. package/lib/quote/implementations/v1.js.map +1 -1
  17. package/lib/quote/scheduler.d.ts +21 -0
  18. package/lib/quote/scheduler.d.ts.map +1 -0
  19. package/lib/quote/scheduler.js +248 -0
  20. package/lib/quote/scheduler.js.map +1 -0
  21. package/lib/quote/service.js +7 -70
  22. package/lib/quote/service.js.map +1 -1
  23. package/lib/quote/state.d.ts +1 -0
  24. package/lib/quote/state.d.ts.map +1 -1
  25. package/lib/quote/state.js +2 -1
  26. package/lib/quote/state.js.map +1 -1
  27. package/package.json +2 -2
  28. package/temp/package-deps.json +9 -13
  29. package/dist/quote/upstream/executor.js +0 -98
  30. package/dist/quote/upstream/executor.js.map +0 -1
  31. package/dist/quote/upstream/index.js +0 -2
  32. package/dist/quote/upstream/index.js.map +0 -1
  33. package/dist/quote/upstream/prefix-matcher.js +0 -7
  34. package/dist/quote/upstream/prefix-matcher.js.map +0 -1
  35. package/dist/quote/upstream/registry.js +0 -116
  36. package/dist/quote/upstream/registry.js.map +0 -1
  37. package/dist/quote/upstream/router.js +0 -119
  38. package/dist/quote/upstream/router.js.map +0 -1
  39. package/lib/quote/upstream/executor.d.ts +0 -8
  40. package/lib/quote/upstream/executor.d.ts.map +0 -1
  41. package/lib/quote/upstream/executor.js +0 -102
  42. package/lib/quote/upstream/executor.js.map +0 -1
  43. package/lib/quote/upstream/index.d.ts +0 -3
  44. package/lib/quote/upstream/index.d.ts.map +0 -1
  45. package/lib/quote/upstream/index.js +0 -6
  46. package/lib/quote/upstream/index.js.map +0 -1
  47. package/lib/quote/upstream/prefix-matcher.d.ts +0 -8
  48. package/lib/quote/upstream/prefix-matcher.d.ts.map +0 -1
  49. package/lib/quote/upstream/prefix-matcher.js +0 -11
  50. package/lib/quote/upstream/prefix-matcher.js.map +0 -1
  51. package/lib/quote/upstream/registry.d.ts +0 -18
  52. package/lib/quote/upstream/registry.d.ts.map +0 -1
  53. package/lib/quote/upstream/registry.js +0 -120
  54. package/lib/quote/upstream/registry.js.map +0 -1
  55. package/lib/quote/upstream/router.d.ts +0 -27
  56. package/lib/quote/upstream/router.d.ts.map +0 -1
  57. package/lib/quote/upstream/router.js +0 -124
  58. package/lib/quote/upstream/router.js.map +0 -1
package/lib/position.js CHANGED
@@ -3,23 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.polyfillOrders = exports.polyfillPosition = void 0;
4
4
  const cache_1 = require("@yuants/cache");
5
5
  const data_product_1 = require("@yuants/data-product");
6
- const data_quote_1 = require("@yuants/data-quote");
7
6
  const protocol_1 = require("@yuants/protocol");
8
7
  const sql_1 = require("@yuants/sql");
9
8
  const utils_1 = require("@yuants/utils");
10
9
  const terminal = protocol_1.Terminal.fromNodeEnv();
11
10
  const productCache = (0, data_product_1.createClientProductCache)(terminal);
12
11
  const quoteCache = (0, cache_1.createCache)(async (product_id) => {
13
- const quoteRecord = await (0, data_quote_1.queryQuotes)(terminal, [product_id], [
14
- 'ask_price',
15
- 'bid_price',
16
- 'last_price',
17
- 'interest_rate_long',
18
- 'interest_rate_short',
19
- 'interest_rate_prev_settled_at',
20
- 'interest_rate_next_settled_at',
21
- ], Date.now());
22
- return quoteRecord[product_id];
12
+ const sql = `select * from quote where product_id = ${(0, sql_1.escapeSQL)(product_id)}`;
13
+ const [quote] = await (0, sql_1.requestSQL)(terminal, sql);
14
+ return quote;
23
15
  }, { expire: 30000 });
24
16
  const interestRateIntervalCache = (0, cache_1.createCache)(async (product_id) => {
25
17
  const sql = `select created_at from interest_rate where series_id = ${(0, sql_1.escapeSQL)(product_id)} order by created_at desc limit 2`;
@@ -79,7 +71,7 @@ const polyfillPosition = async (positions) => {
79
71
  }
80
72
  // 利率相关信息的追加
81
73
  if (quote) {
82
- if (quote.interest_rate_next_settled_at != null) {
74
+ if (quote.interest_rate_next_settled_at !== null) {
83
75
  const nextSettledAt = new Date(quote.interest_rate_next_settled_at).getTime();
84
76
  // 优先使用行情数据中的下一个结算时间
85
77
  pos.settlement_scheduled_at = nextSettledAt;
@@ -89,7 +81,7 @@ const polyfillPosition = async (positions) => {
89
81
  pos.settlement_interval = interval;
90
82
  }
91
83
  }
92
- else if (quote.interest_rate_next_settled_at == null && interestRateInterval !== undefined) {
84
+ else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {
93
85
  // 估算下一个结算时间
94
86
  // 找到 prev + k * interval > now 的最小 k,则下一个结算时间为 prev + k * interval
95
87
  const k = Math.ceil((Date.now() - interestRateInterval.prev) / interestRateInterval.interval);
@@ -100,12 +92,12 @@ const polyfillPosition = async (positions) => {
100
92
  pos.settlement_interval = interestRateInterval.interval;
101
93
  }
102
94
  if (pos.direction === 'LONG') {
103
- if (quote.interest_rate_long != null) {
95
+ if (quote.interest_rate_long !== null) {
104
96
  pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;
105
97
  }
106
98
  }
107
99
  if (pos.direction === 'SHORT') {
108
- if (quote.interest_rate_short != null) {
100
+ if (quote.interest_rate_short !== null) {
109
101
  pos.interest_to_settle = +quote.interest_rate_short * pos.valuation;
110
102
  }
111
103
  }
@@ -1 +1 @@
1
- {"version":3,"file":"position.js","sourceRoot":"","sources":["../src/position.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","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, queryQuotes } 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);\n\nconst quoteCache = createCache<Partial<IQuote> | undefined>(\n async (product_id) => {\n const quoteRecord = await queryQuotes(\n terminal,\n [product_id],\n [\n 'ask_price',\n 'bid_price',\n 'last_price',\n 'interest_rate_long',\n 'interest_rate_short',\n 'interest_rate_prev_settled_at',\n 'interest_rate_next_settled_at',\n ],\n Date.now(),\n );\n return quoteRecord[product_id];\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"]}
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,E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,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"]}
@@ -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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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;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,CAA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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,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"}
@@ -0,0 +1,248 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.markDirty = exports.cells = void 0;
4
+ const exchange_1 = require("@yuants/exchange");
5
+ const protocol_1 = require("@yuants/protocol");
6
+ const utils_1 = require("@yuants/utils");
7
+ const rxjs_1 = require("rxjs");
8
+ const state_1 = require("./state");
9
+ const createFifoQueue = () => ({ items: [], head: 0 });
10
+ const fifoEnqueue = (queue, value) => {
11
+ queue.items.push(value);
12
+ };
13
+ const fifoDequeue = (queue) => {
14
+ if (queue.head >= queue.items.length)
15
+ return undefined;
16
+ const value = queue.items[queue.head++];
17
+ if (queue.head > 1024 && queue.head * 2 > queue.items.length) {
18
+ queue.items = queue.items.slice(queue.head);
19
+ queue.head = 0;
20
+ }
21
+ return value;
22
+ };
23
+ const fifoSize = (queue) => queue.items.length - queue.head;
24
+ const isTraceEnabled = process.env.VEX_QUOTE_UPSTREAM_REFINE_TRACE === '1';
25
+ // Query -> Make Cell Dirty -> Trigger Dirty Check
26
+ // Dirty Check():
27
+ // Acquire dirtyCheck lock
28
+ // Foreach Dirty cell in parallel:
29
+ //. Routing the dirty cell to ServiceID
30
+ //. Acquire Service Lock
31
+ //. if success:
32
+ // request the service and wait for response
33
+ //. release service lock
34
+ // clean cells
35
+ //. if failed: do nothing.
36
+ // Release dirtyCheck lock
37
+ // if any cell dirty remaining, call dirtyCheck()
38
+ // (service_id, service_group_id) -> sync func state (hash func)
39
+ const services = [];
40
+ const mapGroupIdToServices = new Map();
41
+ // (product_id, field, service_group_id, is_dirty = true, is_fetching)
42
+ // use-case 1: find unique product_ids (not is_fetching) by service_group_id
43
+ // use-case 2: mark
44
+ /**
45
+ * Use cases:
46
+ * - upsert items (when queryQuotes)
47
+ * - find unique product_ids (is_dirty = true and is_fetching = false) by service_group_id
48
+ * - when service fired, update is_fetching = true for some items (product_id, field)
49
+ * - when service failed, update is_fetching = false for some items (product_id, field)
50
+ * - when service success, update is_fetching = false and is_dirty = false for some items (product_id, field)
51
+ */
52
+ exports.cells = [];
53
+ const mapCellKeyToCell = new Map();
54
+ const mapGroupIdToState = new Map();
55
+ const getOrCreateGroupState = (group_id) => {
56
+ const existing = mapGroupIdToState.get(group_id);
57
+ if (existing)
58
+ return existing;
59
+ const next = {
60
+ productQueue: createFifoQueue(),
61
+ inQueue: new Set(),
62
+ fetchingProducts: new Set(),
63
+ dirtyFieldsByProduct: new Map(),
64
+ };
65
+ mapGroupIdToState.set(group_id, next);
66
+ return next;
67
+ };
68
+ const route = (product_id, field) => {
69
+ for (const service of services) {
70
+ if (!product_id.startsWith(service.meta.product_id_prefix))
71
+ continue;
72
+ if (!service.metaFieldsSet.has(field))
73
+ continue;
74
+ return service.service_group_id;
75
+ }
76
+ return '';
77
+ };
78
+ const enqueueDirty = (params) => {
79
+ var _a;
80
+ const { product_id, field, service_group_id } = params;
81
+ if (!service_group_id)
82
+ return;
83
+ const groupState = getOrCreateGroupState(service_group_id);
84
+ const dirtyFields = (_a = groupState.dirtyFieldsByProduct.get(product_id)) !== null && _a !== void 0 ? _a : (() => {
85
+ const next = new Set();
86
+ groupState.dirtyFieldsByProduct.set(product_id, next);
87
+ return next;
88
+ })();
89
+ dirtyFields.add(field);
90
+ if (groupState.fetchingProducts.has(product_id))
91
+ return;
92
+ if (groupState.inQueue.has(product_id))
93
+ return;
94
+ groupState.inQueue.add(product_id);
95
+ fifoEnqueue(groupState.productQueue, product_id);
96
+ };
97
+ const markDirty = (product_id, field) => {
98
+ const service_group_id = route(product_id, field);
99
+ const cellKey = (0, utils_1.encodePath)(product_id, field);
100
+ const existing = mapCellKeyToCell.get(cellKey);
101
+ if (!existing) {
102
+ const cell = {
103
+ product_id,
104
+ field,
105
+ service_group_id,
106
+ is_dirty: true,
107
+ is_fetching: false,
108
+ round: 0,
109
+ };
110
+ mapCellKeyToCell.set(cellKey, cell);
111
+ exports.cells.push(cell);
112
+ }
113
+ else {
114
+ existing.service_group_id = service_group_id;
115
+ existing.is_dirty = true;
116
+ }
117
+ enqueueDirty({ product_id, field, service_group_id });
118
+ scheduleServiceGroupId(service_group_id);
119
+ };
120
+ exports.markDirty = markDirty;
121
+ const isServiceIdRunning = new Set();
122
+ const scheduleServiceGroupId = (serviceGroupId) => {
123
+ if (!serviceGroupId)
124
+ return;
125
+ const serviceList = mapGroupIdToServices.get(serviceGroupId);
126
+ if (!serviceList || serviceList.length === 0)
127
+ return;
128
+ for (const service of serviceList) {
129
+ if (isServiceIdRunning.has(service.service_id))
130
+ continue;
131
+ isServiceIdRunning.add(service.service_id);
132
+ handleService(service).finally(() => {
133
+ isServiceIdRunning.delete(service.service_id);
134
+ });
135
+ }
136
+ };
137
+ const handleService = async (service) => {
138
+ var _a;
139
+ const groupState = mapGroupIdToState.get(service.service_group_id);
140
+ if (!groupState)
141
+ return;
142
+ while (true) {
143
+ const maxProducts = (_a = service.meta.max_products_per_request) !== null && _a !== void 0 ? _a : Infinity;
144
+ const fetchedFieldsByProduct = new Map();
145
+ const productsToFetch = [];
146
+ const cellsToFetch = [];
147
+ while (productsToFetch.length < maxProducts && fifoSize(groupState.productQueue) > 0) {
148
+ const product_id = fifoDequeue(groupState.productQueue);
149
+ if (!product_id)
150
+ break;
151
+ groupState.inQueue.delete(product_id);
152
+ if (groupState.fetchingProducts.has(product_id))
153
+ continue;
154
+ const dirtyFields = groupState.dirtyFieldsByProduct.get(product_id);
155
+ if (!dirtyFields || dirtyFields.size === 0)
156
+ continue;
157
+ const matchedFields = [];
158
+ for (const f of dirtyFields) {
159
+ if (!service.metaFieldsSet.has(f))
160
+ continue;
161
+ const cell = mapCellKeyToCell.get((0, utils_1.encodePath)(product_id, f));
162
+ if (!cell)
163
+ continue;
164
+ if (cell.service_group_id !== service.service_group_id)
165
+ continue;
166
+ if (!cell.is_dirty)
167
+ continue;
168
+ if (cell.is_fetching)
169
+ continue;
170
+ matchedFields.push(f);
171
+ cellsToFetch.push(cell);
172
+ }
173
+ if (matchedFields.length === 0)
174
+ continue;
175
+ productsToFetch.push(product_id);
176
+ groupState.fetchingProducts.add(product_id);
177
+ fetchedFieldsByProduct.set(product_id, new Set(matchedFields));
178
+ }
179
+ if (productsToFetch.length === 0)
180
+ return;
181
+ for (const cell of cellsToFetch) {
182
+ cell.round++;
183
+ cell.is_fetching = true;
184
+ }
185
+ const res = await makeRequest(service, productsToFetch).catch(() => ({}));
186
+ state_1.quoteState.update(res);
187
+ for (const cell of cellsToFetch) {
188
+ cell.is_fetching = false;
189
+ cell.is_dirty = false;
190
+ }
191
+ for (const product_id of productsToFetch) {
192
+ groupState.fetchingProducts.delete(product_id);
193
+ const dirtyFields = groupState.dirtyFieldsByProduct.get(product_id);
194
+ const fetchedFields = fetchedFieldsByProduct.get(product_id);
195
+ if (!dirtyFields || !fetchedFields)
196
+ continue;
197
+ for (const f of fetchedFields) {
198
+ dirtyFields.delete(f);
199
+ }
200
+ if (dirtyFields.size === 0) {
201
+ groupState.dirtyFieldsByProduct.delete(product_id);
202
+ continue;
203
+ }
204
+ if (!groupState.inQueue.has(product_id)) {
205
+ groupState.inQueue.add(product_id);
206
+ fifoEnqueue(groupState.productQueue, product_id);
207
+ }
208
+ }
209
+ }
210
+ };
211
+ const terminal = protocol_1.Terminal.fromNodeEnv();
212
+ const makeRequest = async (service, product_ids) => {
213
+ const res = await terminal.client.requestForResponseData('GetQuotes', {
214
+ product_ids,
215
+ fields: service.meta.fields,
216
+ });
217
+ if (isTraceEnabled) {
218
+ console.info((0, utils_1.formatTime)(Date.now()), `[VEX][Quote][Refine]GetQuotes`, `service_id=${service.service_id}`, `group=${service.service_group_id}`, `products=${product_ids.length}`);
219
+ }
220
+ return res;
221
+ };
222
+ terminal.terminalInfos$
223
+ .pipe((0, rxjs_1.mergeMap)((terminalInfos) => (0, rxjs_1.from)(terminalInfos).pipe((0, rxjs_1.mergeMap)((terminalInfo) => (0, rxjs_1.from)(Object.values(terminalInfo.serviceInfo || {})).pipe((0, rxjs_1.filter)((serviceInfo) => serviceInfo.method === 'GetQuotes'), (0, rxjs_1.map)((serviceInfo) => {
224
+ var _a;
225
+ try {
226
+ const meta = (0, exchange_1.parseQuoteServiceMetadataFromSchema)(serviceInfo.schema);
227
+ const service_group_id = (0, utils_1.encodePath)(meta.product_id_prefix, meta.fields.join(','), (_a = meta.max_products_per_request) !== null && _a !== void 0 ? _a : '');
228
+ return {
229
+ service_id: serviceInfo.service_id,
230
+ service_group_id,
231
+ meta,
232
+ metaFieldsSet: new Set(meta.fields),
233
+ };
234
+ }
235
+ catch (_b) { }
236
+ }), (0, rxjs_1.filter)((x) => !!x))), (0, rxjs_1.toArray)(), (0, rxjs_1.tap)((x) => {
237
+ services.length = 0;
238
+ mapGroupIdToServices.clear();
239
+ x.forEach((service) => {
240
+ var _a;
241
+ services.push(service);
242
+ const list = (_a = mapGroupIdToServices.get(service.service_group_id)) !== null && _a !== void 0 ? _a : [];
243
+ list.push(service);
244
+ mapGroupIdToServices.set(service.service_group_id, list);
245
+ });
246
+ }))))
247
+ .subscribe();
248
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../../src/quote/scheduler.ts"],"names":[],"mappings":";;;AACA,+CAA2G;AAC3G,+CAA4C;AAC5C,yCAAuD;AACvD,+BAAiE;AACjE,mCAAqC;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,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM;QAAE,OAAO,SAAS,CAAC;IACvD,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IACxC,IAAI,KAAK,CAAC,IAAI,GAAG,IAAI,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE;QAC5D,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5C,KAAK,CAAC,IAAI,GAAG,CAAC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AAC,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,13 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const protocol_1 = require("@yuants/protocol");
4
- const utils_1 = require("@yuants/utils");
5
- const rxjs_1 = require("rxjs");
6
4
  const state_1 = require("./state");
7
- const upstream_1 = require("./upstream");
5
+ const scheduler_1 = require("./scheduler");
8
6
  const terminal = protocol_1.Terminal.fromNodeEnv();
9
- const quoteState = (0, state_1.createQuoteState)();
10
- const quoteProviderRegistry = (0, upstream_1.createQuoteProviderRegistry)(terminal);
11
7
  const normalizeStrings = (values) => [...new Set(values)].sort();
12
8
  const normalizeFields = (values) => [...new Set(values)].sort();
13
9
  const analyzeRequestedQuotes = (quoteState, product_ids, fields, updated_at) => {
@@ -26,66 +22,12 @@ const analyzeRequestedQuotes = (quoteState, product_ids, fields, updated_at) =>
26
22
  }
27
23
  return { needUpdate };
28
24
  };
29
- const updateQueue$ = new rxjs_1.Subject();
30
- const updateQueueStats = {
31
- queued_total: 0,
32
- started_total: 0,
33
- processed_total: 0,
34
- };
35
- const getQueueStatus = () => {
36
- const pending = updateQueueStats.queued_total - updateQueueStats.started_total;
37
- const in_flight = updateQueueStats.started_total - updateQueueStats.processed_total;
38
- return Object.assign({ pending,
39
- in_flight }, updateQueueStats);
40
- };
41
- const enqueueUpdateTask = (task) => {
42
- updateQueueStats.queued_total++;
43
- updateQueueStats.last_enqueued_at = Date.now();
44
- updateQueue$.next(task);
45
- };
46
- const summarizeError = (error) => {
47
- if (typeof error === 'object' && error !== null) {
48
- const code = 'code' in error ? error.code : undefined;
49
- const message = 'message' in error ? error.message : undefined;
50
- return {
51
- code: typeof code === 'string' ? code : undefined,
52
- message: typeof message === 'string' ? message : undefined,
53
- };
54
- }
55
- return {};
56
- };
57
- const processUpdateTask = async (task) => {
58
- const { needUpdate } = analyzeRequestedQuotes(quoteState, task.product_ids, task.fields, task.updated_at);
59
- await quoteProviderRegistry.fillQuoteStateFromUpstream({
60
- quoteState,
61
- cacheMissed: needUpdate,
62
- updated_at: task.updated_at,
63
- });
64
- };
65
- updateQueue$
66
- .pipe((0, rxjs_1.concatMap)((task) => (0, rxjs_1.defer)(async () => {
67
- updateQueueStats.started_total++;
68
- updateQueueStats.last_started_at = Date.now();
69
- try {
70
- await processUpdateTask(task);
71
- }
72
- catch (error) {
73
- const summary = summarizeError(error);
74
- updateQueueStats.last_error = Object.assign({ at: Date.now() }, summary);
75
- console.info((0, utils_1.formatTime)(Date.now()), `[VEX][Quote]UpdateQueueTaskFailed`, `product_ids=${task.product_ids.length} fields=${task.fields.length} updated_at=${task.updated_at}`, JSON.stringify(summary));
76
- }
77
- finally {
78
- updateQueueStats.processed_total++;
79
- updateQueueStats.last_processed_at = Date.now();
80
- }
81
- })))
82
- .subscribe();
83
25
  terminal.server.provideService('VEX/UpdateQuotes', {}, async (msg) => {
84
- quoteState.update(msg.req);
26
+ state_1.quoteState.update(msg.req);
85
27
  return { res: { code: 0, message: 'OK' } };
86
28
  });
87
29
  terminal.server.provideService('VEX/DumpQuoteState', {}, async () => {
88
- return { res: { code: 0, message: 'OK', data: quoteState.dumpAsObject() } };
30
+ return { res: { code: 0, message: 'OK', data: state_1.quoteState.dumpAsObject() } };
89
31
  });
90
32
  terminal.server.provideService('VEX/QueryQuotes', {
91
33
  type: 'object',
@@ -105,16 +47,11 @@ terminal.server.provideService('VEX/QueryQuotes', {
105
47
  const product_ids = normalizeStrings(msg.req.product_ids);
106
48
  const fields = normalizeFields(msg.req.fields);
107
49
  const { updated_at } = msg.req;
108
- // SWR strategy: if we have stale or missing data, enqueue an update task,
109
- // but still return the current data immediately.
110
- const { needUpdate } = analyzeRequestedQuotes(quoteState, product_ids, fields, updated_at);
111
- if (needUpdate.length > 0) {
112
- enqueueUpdateTask({ product_ids, fields, updated_at });
50
+ const { needUpdate } = analyzeRequestedQuotes(state_1.quoteState, product_ids, fields, updated_at);
51
+ for (const { product_id, field } of needUpdate) {
52
+ (0, scheduler_1.markDirty)(product_id, field);
113
53
  }
114
- const data = quoteState.filterValues(product_ids, fields);
54
+ const data = state_1.quoteState.filterValues(product_ids, fields);
115
55
  return { res: { code: 0, message: 'OK', data } };
116
56
  });
117
- terminal.server.provideService('VEX/QuoteUpdateQueueStatus', {}, async () => {
118
- return { res: { code: 0, message: 'OK', data: getQueueStatus() } };
119
- });
120
57
  //# sourceMappingURL=service.js.map