@yuants/exchange 0.8.1 → 0.8.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.
- package/dist/interest_rate.js +24 -9
- package/dist/interest_rate.js.map +1 -1
- package/dist/ohlc.js +26 -13
- package/dist/ohlc.js.map +1 -1
- package/lib/interest_rate.d.ts.map +1 -1
- package/lib/interest_rate.js +24 -9
- package/lib/interest_rate.js.map +1 -1
- package/lib/ohlc.d.ts.map +1 -1
- package/lib/ohlc.js +25 -12
- package/lib/ohlc.js.map +1 -1
- package/package.json +11 -11
- package/temp/package-deps.json +15 -15
package/dist/interest_rate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { encodeInterestRateSeriesId } from '@yuants/data-interest-rate';
|
|
1
2
|
import { createValidator } from '@yuants/protocol/lib/schema';
|
|
2
3
|
import { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';
|
|
3
|
-
import { encodePath } from '@yuants/utils';
|
|
4
4
|
import { newError } from '../../utils/lib';
|
|
5
5
|
const schemaValidator = createValidator({
|
|
6
6
|
type: 'object',
|
|
@@ -94,18 +94,17 @@ export const provideInterestRateService = (terminal, metadata, fetchPage, servic
|
|
|
94
94
|
},
|
|
95
95
|
}, async (msg) => {
|
|
96
96
|
try {
|
|
97
|
-
const series_id =
|
|
97
|
+
const series_id = encodeInterestRateSeriesId(msg.req.product_id);
|
|
98
98
|
const items = await fetchPage(Object.assign(Object.assign({}, msg.req), { series_id }));
|
|
99
99
|
const normalized = items.map((x) => (Object.assign(Object.assign({}, x), { series_id, datasource_id: '', product_id: msg.req.product_id })));
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
const range = computeInterestRatePageRange(normalized);
|
|
101
|
+
// Atomic write: data rows + series_data_range in the same statement.
|
|
102
|
+
if (normalized.length > 0 && range) {
|
|
103
|
+
const writeInterestRate = `${buildInsertManyIntoTableSQL(normalized, 'interest_rate', {
|
|
102
104
|
columns: INTEREST_RATE_INSERT_COLUMNS,
|
|
103
105
|
conflictKeys: ['series_id', 'created_at'],
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
const range = computeInterestRatePageRange(normalized);
|
|
107
|
-
if (range) {
|
|
108
|
-
await requestSQL(terminal, buildInsertManyIntoTableSQL([
|
|
106
|
+
})} RETURNING 1`;
|
|
107
|
+
const writeRange = `${buildInsertManyIntoTableSQL([
|
|
109
108
|
{
|
|
110
109
|
series_id,
|
|
111
110
|
table_name: 'interest_rate',
|
|
@@ -115,6 +114,22 @@ export const provideInterestRateService = (terminal, metadata, fetchPage, servic
|
|
|
115
114
|
], 'series_data_range', {
|
|
116
115
|
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
117
116
|
ignoreConflict: true,
|
|
117
|
+
})} RETURNING 1`;
|
|
118
|
+
await requestSQL(terminal, `
|
|
119
|
+
WITH
|
|
120
|
+
write_interest_rate AS (
|
|
121
|
+
${writeInterestRate}
|
|
122
|
+
),
|
|
123
|
+
write_range AS (
|
|
124
|
+
${writeRange}
|
|
125
|
+
)
|
|
126
|
+
SELECT 1 as ok;
|
|
127
|
+
`);
|
|
128
|
+
}
|
|
129
|
+
else if (normalized.length > 0) {
|
|
130
|
+
await requestSQL(terminal, buildInsertManyIntoTableSQL(normalized, 'interest_rate', {
|
|
131
|
+
columns: INTEREST_RATE_INSERT_COLUMNS,
|
|
132
|
+
conflictKeys: ['series_id', 'created_at'],
|
|
118
133
|
}));
|
|
119
134
|
}
|
|
120
135
|
return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interest_rate.js","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe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YAEJ,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;gBACzB,MAAM,UAAU,CACd,QAAQ,EACR,2BAA2B,CAAC,UAAU,EAAE,eAAe,EAAE;oBACvD,OAAO,EAAE,4BAA4B;oBACrC,YAAY,EAAE,CAAC,WAAW,EAAE,YAAY,CAAC;iBAC1C,CAAC,CACH,CAAC;aACH;YAED,MAAM,KAAK,GAAG,4BAA4B,CAAC,UAAU,CAAC,CAAC;YACvD,IAAI,KAAK,EAAE;gBACT,MAAM,UAAU,CACd,QAAQ,EACR,2BAA2B,CACzB;oBACE;wBACE,SAAS;wBACT,UAAU,EAAE,eAAe;wBAC3B,UAAU,EAAE,KAAK,CAAC,UAAU;wBAC5B,QAAQ,EAAE,KAAK,CAAC,QAAQ;qBACzB;iBACF,EACD,mBAAmB,EACnB;oBACE,OAAO,EAAE,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC;oBAC9D,cAAc,EAAE,IAAI;iBACrB,CACF,CACF,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IInterestRate } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { encodePath } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: IInterestRate[],\n): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {\n end = item;\n endMs = createdAtMs;\n }\n }\n\n return { start_time: start.created_at, end_time: end.created_at };\n};\n\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodePath(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n const range = computeInterestRatePageRange(normalized);\n if (range) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n ),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };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ACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { encodeInterestRateSeriesId, IInterestRate } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: IInterestRate[],\n): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {\n end = item;\n endMs = createdAtMs;\n }\n }\n\n return { start_time: start.created_at, end_time: end.created_at };\n};\n\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeInterestRateSeriesId(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n const range = computeInterestRatePageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n )} RETURNING 1`;\n\n await requestSQL(\n terminal,\n `\n WITH\n write_interest_rate AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
package/dist/ohlc.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { encodeOHLCSeriesId } from '@yuants/data-ohlc';
|
|
1
2
|
import { createValidator } from '@yuants/protocol/lib/schema';
|
|
2
3
|
import { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';
|
|
3
|
-
import {
|
|
4
|
+
import { formatTime } from '@yuants/utils';
|
|
4
5
|
import { newError } from '../../utils/lib';
|
|
5
6
|
const schemaValidator = createValidator({
|
|
6
7
|
type: 'object',
|
|
@@ -63,9 +64,6 @@ export const parseOHLCServiceMetadataFromSchema = (schema) => {
|
|
|
63
64
|
const OHLC_INSERT_COLUMNS = [
|
|
64
65
|
'series_id',
|
|
65
66
|
'created_at',
|
|
66
|
-
'datasource_id',
|
|
67
|
-
'product_id',
|
|
68
|
-
'duration',
|
|
69
67
|
'closed_at',
|
|
70
68
|
'open',
|
|
71
69
|
'high',
|
|
@@ -114,31 +112,46 @@ export const provideOHLCService = (terminal, metadata, fetchPage, serviceOptions
|
|
|
114
112
|
},
|
|
115
113
|
}, async (msg) => {
|
|
116
114
|
try {
|
|
117
|
-
const series_id =
|
|
115
|
+
const series_id = encodeOHLCSeriesId(msg.req.product_id, msg.req.duration);
|
|
118
116
|
const items = await fetchPage(Object.assign(Object.assign({}, msg.req), { series_id }));
|
|
119
117
|
console.info(formatTime(Date.now()), 'IngestOHLC', msg.req.product_id, msg.req.duration, 'fetched', items.length, 'bars');
|
|
120
118
|
const normalized = items.map((x) => {
|
|
121
119
|
var _a;
|
|
122
120
|
return (Object.assign(Object.assign({}, x), { series_id, datasource_id: '', product_id: msg.req.product_id, duration: msg.req.duration, open_interest: (_a = x.open_interest) !== null && _a !== void 0 ? _a : '0' }));
|
|
123
121
|
});
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
const range = computeOHLCPageRange(normalized);
|
|
123
|
+
// Atomic write: data rows + series_data_range in the same statement.
|
|
124
|
+
if (normalized.length > 0 && range) {
|
|
125
|
+
const writeOHLC = `${buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {
|
|
126
126
|
columns: OHLC_INSERT_COLUMNS,
|
|
127
127
|
conflictKeys: ['series_id', 'created_at'],
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
const range = computeOHLCPageRange(normalized);
|
|
131
|
-
if (range) {
|
|
132
|
-
await requestSQL(terminal, buildInsertManyIntoTableSQL([
|
|
128
|
+
})} RETURNING 1`;
|
|
129
|
+
const writeRange = `${buildInsertManyIntoTableSQL([
|
|
133
130
|
{
|
|
134
131
|
series_id,
|
|
135
|
-
table_name: '
|
|
132
|
+
table_name: 'ohlc_v2',
|
|
136
133
|
start_time: range.start_time,
|
|
137
134
|
end_time: range.end_time,
|
|
138
135
|
},
|
|
139
136
|
], 'series_data_range', {
|
|
140
137
|
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
141
138
|
ignoreConflict: true,
|
|
139
|
+
})} RETURNING 1`;
|
|
140
|
+
await requestSQL(terminal, `
|
|
141
|
+
WITH
|
|
142
|
+
write_ohlc AS (
|
|
143
|
+
${writeOHLC}
|
|
144
|
+
),
|
|
145
|
+
write_range AS (
|
|
146
|
+
${writeRange}
|
|
147
|
+
)
|
|
148
|
+
SELECT 1 as ok;
|
|
149
|
+
`);
|
|
150
|
+
}
|
|
151
|
+
else if (normalized.length > 0) {
|
|
152
|
+
await requestSQL(terminal, buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {
|
|
153
|
+
columns: OHLC_INSERT_COLUMNS,
|
|
154
|
+
conflictKeys: ['series_id', 'created_at'],
|
|
142
155
|
}));
|
|
143
156
|
}
|
|
144
157
|
return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };
|
package/dist/ohlc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAwB3C,MAAM,eAAe,GAAG,eAAe,CAAC;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC;IAChC,UAAU,EAAE;QACV,IAAI,EAAE,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BAAoB,CAAC,UAAU,CAAC,CAAC;YAC/C,IAAI,KAAK,EAAE;gBACT,MAAM,UAAU,CACd,QAAQ,EACR,2BAA2B,CACzB;oBACE;wBACE,SAAS;wBACT,UAAU,EAAE,MAAM;wBAClB,UAAU,EAAE,KAAK,CAAC,UAAU;wBAC5B,QAAQ,EAAE,KAAK,CAAC,QAAQ;qBACzB;iBACF,EACD,mBAAmB,EACnB;oBACE,OAAO,EAAE,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC;oBAC9D,cAAc,EAAE,IAAI;iBACrB,CACF,CACF,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAClE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { encodePath, formatTime } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'duration',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.closed_at) || Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodePath(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n console.info(\n formatTime(Date.now()),\n 'IngestOHLC',\n msg.req.product_id,\n msg.req.duration,\n 'fetched',\n items.length,\n 'bars',\n );\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'ohlc', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n const range = computeOHLCPageRange(normalized);\n if (range) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n ),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n console.error(formatTime(Date.now()), 'IngestOHLC error:', error);\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
|
1
|
+
{"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAS,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAwB3C,MAAM,eAAe,GAAG,eAAe,CAAC;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC;IAChC,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;QACzC,QAAQ,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,EAAE;QACnF,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC;YACzD,UAAU,EAAE;gBACV,UAAU,EAAE;oBACV,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,MAAM,EAA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,SAAS;;;kBAGT,UAAU;;;aAGf,CACF,CAAC;aACH;iBAAM,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;gBAChC,MAAM,UAAU,CACd,QAAQ,EACR,2BAA2B,CAAC,UAAU,EAAE,SAAS,EAAE;oBACjD,OAAO,EAAE,mBAAmB;oBAC5B,YAAY,EAAE,CAAC,WAAW,EAAE,YAAY,CAAC;iBAC1C,CAAC,CACH,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAClE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { encodeOHLCSeriesId, IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { formatTime } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.closed_at) || Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeOHLCSeriesId(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n console.info(\n formatTime(Date.now()),\n 'IngestOHLC',\n msg.req.product_id,\n msg.req.duration,\n 'fetched',\n items.length,\n 'bars',\n );\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n const range = computeOHLCPageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeOHLC = `${buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc_v2',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n )} RETURNING 1`;\n\n await requestSQL(\n terminal,\n `\n WITH\n write_ohlc AS (\n ${writeOHLC}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n console.error(formatTime(Date.now()), 'IngestOHLC error:', error);\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interest_rate.d.ts","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"interest_rate.d.ts","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,aAAa,EAAE,MAAM,4BAA4B,CAAC;AACvF,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAI7D,OAAO,EAAuB,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpE;;;GAGG;AACH,MAAM,WAAW,4BAA4B;IAC3C,iBAAiB,EAAE,MAAM,CAAC;IAC1B,SAAS,EAAE,oBAAoB,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,oBAAoB,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC;CACd;AAuCD;;GAEG;AACH,eAAO,MAAM,0CAA0C,WAAY,GAAG,KAAG,4BAOxE,CAAC;AAqCF;;GAEG;AACH,eAAO,MAAM,0BAA0B,aAC3B,QAAQ,YACR,4BAA4B,uBACjB,0BAA0B,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,KAAK,QAAQ,aAAa,EAAE,CAAC,mBACnF,eAAe;;CAkFjC,CAAC"}
|
package/lib/interest_rate.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.provideInterestRateService = exports.parseInterestRateServiceMetadataFromSchema = void 0;
|
|
4
|
+
const data_interest_rate_1 = require("@yuants/data-interest-rate");
|
|
4
5
|
const schema_1 = require("@yuants/protocol/lib/schema");
|
|
5
6
|
const sql_1 = require("@yuants/sql");
|
|
6
|
-
const utils_1 = require("@yuants/utils");
|
|
7
7
|
const lib_1 = require("../../utils/lib");
|
|
8
8
|
const schemaValidator = (0, schema_1.createValidator)({
|
|
9
9
|
type: 'object',
|
|
@@ -98,18 +98,17 @@ const provideInterestRateService = (terminal, metadata, fetchPage, serviceOption
|
|
|
98
98
|
},
|
|
99
99
|
}, async (msg) => {
|
|
100
100
|
try {
|
|
101
|
-
const series_id = (0,
|
|
101
|
+
const series_id = (0, data_interest_rate_1.encodeInterestRateSeriesId)(msg.req.product_id);
|
|
102
102
|
const items = await fetchPage(Object.assign(Object.assign({}, msg.req), { series_id }));
|
|
103
103
|
const normalized = items.map((x) => (Object.assign(Object.assign({}, x), { series_id, datasource_id: '', product_id: msg.req.product_id })));
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
const range = computeInterestRatePageRange(normalized);
|
|
105
|
+
// Atomic write: data rows + series_data_range in the same statement.
|
|
106
|
+
if (normalized.length > 0 && range) {
|
|
107
|
+
const writeInterestRate = `${(0, sql_1.buildInsertManyIntoTableSQL)(normalized, 'interest_rate', {
|
|
106
108
|
columns: INTEREST_RATE_INSERT_COLUMNS,
|
|
107
109
|
conflictKeys: ['series_id', 'created_at'],
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
const range = computeInterestRatePageRange(normalized);
|
|
111
|
-
if (range) {
|
|
112
|
-
await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)([
|
|
110
|
+
})} RETURNING 1`;
|
|
111
|
+
const writeRange = `${(0, sql_1.buildInsertManyIntoTableSQL)([
|
|
113
112
|
{
|
|
114
113
|
series_id,
|
|
115
114
|
table_name: 'interest_rate',
|
|
@@ -119,6 +118,22 @@ const provideInterestRateService = (terminal, metadata, fetchPage, serviceOption
|
|
|
119
118
|
], 'series_data_range', {
|
|
120
119
|
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
121
120
|
ignoreConflict: true,
|
|
121
|
+
})} RETURNING 1`;
|
|
122
|
+
await (0, sql_1.requestSQL)(terminal, `
|
|
123
|
+
WITH
|
|
124
|
+
write_interest_rate AS (
|
|
125
|
+
${writeInterestRate}
|
|
126
|
+
),
|
|
127
|
+
write_range AS (
|
|
128
|
+
${writeRange}
|
|
129
|
+
)
|
|
130
|
+
SELECT 1 as ok;
|
|
131
|
+
`);
|
|
132
|
+
}
|
|
133
|
+
else if (normalized.length > 0) {
|
|
134
|
+
await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)(normalized, 'interest_rate', {
|
|
135
|
+
columns: INTEREST_RATE_INSERT_COLUMNS,
|
|
136
|
+
conflictKeys: ['series_id', 'created_at'],
|
|
122
137
|
}));
|
|
123
138
|
}
|
|
124
139
|
return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };
|
package/lib/interest_rate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interest_rate.js","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":";;;AAEA,wDAA8D;AAC9D,qCAAsE;AACtE,yCAA2C;AAC3C,yCAA2C;AAsB3C,MAAM,eAAe,GAAG,IAAA,wBAAe,EAAC;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,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,EAAE,eAAe,EAAE;oBACvD,OAAO,EAAE,4BAA4B;oBACrC,YAAY,EAAE,CAAC,WAAW,EAAE,YAAY,CAAC;iBAC1C,CAAC,CACH,CAAC;aACH;YAED,MAAM,KAAK,GAAG,4BAA4B,CAAC,UAAU,CAAC,CAAC;YACvD,IAAI,KAAK,EAAE;gBACT,MAAM,IAAA,gBAAU,EACd,QAAQ,EACR,IAAA,iCAA2B,EACzB;oBACE;wBACE,SAAS;wBACT,UAAU,EAAE,eAAe;wBAC3B,UAAU,EAAE,KAAK,CAAC,UAAU;wBAC5B,QAAQ,EAAE,KAAK,CAAC,QAAQ;qBACzB;iBACF,EACD,mBAAmB,EACnB;oBACE,OAAO,EAAE,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC;oBAC9D,cAAc,EAAE,IAAI;iBACrB,CACF,CACF,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC;AAtEW,QAAA,0BAA0B,8BAsErC","sourcesContent":["import { IInterestRate } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { encodePath } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: IInterestRate[],\n): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {\n end = item;\n endMs = createdAtMs;\n }\n }\n\n return { start_time: start.created_at, end_time: end.created_at };\n};\n\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodePath(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n const range = computeInterestRatePageRange(normalized);\n if (range) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n ),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
|
1
|
+
{"version":3,"file":"interest_r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,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC;AAtFW,QAAA,0BAA0B,8BAsFrC","sourcesContent":["import { encodeInterestRateSeriesId, IInterestRate } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: IInterestRate[],\n): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {\n end = item;\n endMs = createdAtMs;\n }\n }\n\n return { start_time: start.created_at, end_time: end.created_at };\n};\n\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeInterestRateSeriesId(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n const range = computeInterestRatePageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n )} RETURNING 1`;\n\n await requestSQL(\n terminal,\n `\n WITH\n write_interest_rate AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
package/lib/ohlc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ohlc.d.ts","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ohlc.d.ts","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAK7D,OAAO,EAAuB,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpE;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,SAAS,EAAE,oBAAoB,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,oBAAoB,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC;CACd;AA+CD;;GAEG;AACH,eAAO,MAAM,kCAAkC,WAAY,GAAG,KAAG,oBAQhE,CAAC;AA0CF;;GAEG;AACH,eAAO,MAAM,kBAAkB,aACnB,QAAQ,YACR,oBAAoB,uBACT,kBAAkB,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,KAAK,QAAQ,KAAK,EAAE,CAAC,mBACnE,eAAe;;CAgGjC,CAAC"}
|
package/lib/ohlc.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.provideOHLCService = exports.parseOHLCServiceMetadataFromSchema = void 0;
|
|
4
|
+
const data_ohlc_1 = require("@yuants/data-ohlc");
|
|
4
5
|
const schema_1 = require("@yuants/protocol/lib/schema");
|
|
5
6
|
const sql_1 = require("@yuants/sql");
|
|
6
7
|
const utils_1 = require("@yuants/utils");
|
|
@@ -67,9 +68,6 @@ exports.parseOHLCServiceMetadataFromSchema = parseOHLCServiceMetadataFromSchema;
|
|
|
67
68
|
const OHLC_INSERT_COLUMNS = [
|
|
68
69
|
'series_id',
|
|
69
70
|
'created_at',
|
|
70
|
-
'datasource_id',
|
|
71
|
-
'product_id',
|
|
72
|
-
'duration',
|
|
73
71
|
'closed_at',
|
|
74
72
|
'open',
|
|
75
73
|
'high',
|
|
@@ -118,31 +116,46 @@ const provideOHLCService = (terminal, metadata, fetchPage, serviceOptions) => {
|
|
|
118
116
|
},
|
|
119
117
|
}, async (msg) => {
|
|
120
118
|
try {
|
|
121
|
-
const series_id = (0,
|
|
119
|
+
const series_id = (0, data_ohlc_1.encodeOHLCSeriesId)(msg.req.product_id, msg.req.duration);
|
|
122
120
|
const items = await fetchPage(Object.assign(Object.assign({}, msg.req), { series_id }));
|
|
123
121
|
console.info((0, utils_1.formatTime)(Date.now()), 'IngestOHLC', msg.req.product_id, msg.req.duration, 'fetched', items.length, 'bars');
|
|
124
122
|
const normalized = items.map((x) => {
|
|
125
123
|
var _a;
|
|
126
124
|
return (Object.assign(Object.assign({}, x), { series_id, datasource_id: '', product_id: msg.req.product_id, duration: msg.req.duration, open_interest: (_a = x.open_interest) !== null && _a !== void 0 ? _a : '0' }));
|
|
127
125
|
});
|
|
128
|
-
|
|
129
|
-
|
|
126
|
+
const range = computeOHLCPageRange(normalized);
|
|
127
|
+
// Atomic write: data rows + series_data_range in the same statement.
|
|
128
|
+
if (normalized.length > 0 && range) {
|
|
129
|
+
const writeOHLC = `${(0, sql_1.buildInsertManyIntoTableSQL)(normalized, 'ohlc_v2', {
|
|
130
130
|
columns: OHLC_INSERT_COLUMNS,
|
|
131
131
|
conflictKeys: ['series_id', 'created_at'],
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
const range = computeOHLCPageRange(normalized);
|
|
135
|
-
if (range) {
|
|
136
|
-
await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)([
|
|
132
|
+
})} RETURNING 1`;
|
|
133
|
+
const writeRange = `${(0, sql_1.buildInsertManyIntoTableSQL)([
|
|
137
134
|
{
|
|
138
135
|
series_id,
|
|
139
|
-
table_name: '
|
|
136
|
+
table_name: 'ohlc_v2',
|
|
140
137
|
start_time: range.start_time,
|
|
141
138
|
end_time: range.end_time,
|
|
142
139
|
},
|
|
143
140
|
], 'series_data_range', {
|
|
144
141
|
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
145
142
|
ignoreConflict: true,
|
|
143
|
+
})} RETURNING 1`;
|
|
144
|
+
await (0, sql_1.requestSQL)(terminal, `
|
|
145
|
+
WITH
|
|
146
|
+
write_ohlc AS (
|
|
147
|
+
${writeOHLC}
|
|
148
|
+
),
|
|
149
|
+
write_range AS (
|
|
150
|
+
${writeRange}
|
|
151
|
+
)
|
|
152
|
+
SELECT 1 as ok;
|
|
153
|
+
`);
|
|
154
|
+
}
|
|
155
|
+
else if (normalized.length > 0) {
|
|
156
|
+
await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)(normalized, 'ohlc_v2', {
|
|
157
|
+
columns: OHLC_INSERT_COLUMNS,
|
|
158
|
+
conflictKeys: ['series_id', 'created_at'],
|
|
146
159
|
}));
|
|
147
160
|
}
|
|
148
161
|
return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };
|
package/lib/ohlc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;AAEA,wDAA8D;AAC9D,qCAAsE;AACtE,yCAAuD;AACvD,yCAA2C;AAwB3C,MAAM,eAAe,GAAG,IAAA,wBAAe,EAAC;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC;IAChC,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;QACzC,QAAQ,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,EAAE;QACnF,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC;YACzD,UAAU,EAAE;gBACV,UAAU,EAAE;oBACV,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;oBAC7B,UAAU,EAAE;wBACV,I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,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,CAAC,KAAK,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAClE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC;AApFW,QAAA,kBAAkB,sBAoF7B","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { encodePath, formatTime } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'duration',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.closed_at) || Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodePath(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n console.info(\n formatTime(Date.now()),\n 'IngestOHLC',\n msg.req.product_id,\n msg.req.duration,\n 'fetched',\n items.length,\n 'bars',\n );\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'ohlc', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n const range = computeOHLCPageRange(normalized);\n if (range) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n ),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n console.error(formatTime(Date.now()), 'IngestOHLC error:', error);\n return { res: { code: 1, message } };\n }\n },\n serviceOptions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C,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,CAAC,KAAK,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAClE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC;AApGW,QAAA,kBAAkB,sBAoG7B","sourcesContent":["import { encodeOHLCSeriesId, IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { formatTime } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.closed_at) || Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeOHLCSeriesId(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n console.info(\n formatTime(Date.now()),\n 'IngestOHLC',\n msg.req.product_id,\n msg.req.duration,\n 'fetched',\n items.length,\n 'bars',\n );\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n const range = computeOHLCPageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeOHLC = `${buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc_v2',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n )} RETURNING 1`;\n\n await requestSQL(\n terminal,\n `\n WITH\n write_ohlc AS (\n ${writeOHLC}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n console.error(formatTime(Date.now()), 'IngestOHLC error:', error);\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuants/exchange",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"typescript": "~4.7.4"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@yuants/protocol": "0.53.
|
|
28
|
-
"@yuants/data-account": "0.11.
|
|
29
|
-
"@yuants/data-order": "0.7.
|
|
30
|
-
"@yuants/data-product": "0.5.
|
|
31
|
-
"@yuants/data-quote": "0.4.
|
|
32
|
-
"@yuants/data-ohlc": "0.
|
|
33
|
-
"@yuants/data-interest-rate": "0.1
|
|
34
|
-
"@yuants/cache": "0.3.
|
|
35
|
-
"@yuants/sql": "0.9.
|
|
36
|
-
"@yuants/utils": "0.
|
|
27
|
+
"@yuants/protocol": "0.53.5",
|
|
28
|
+
"@yuants/data-account": "0.11.2",
|
|
29
|
+
"@yuants/data-order": "0.7.3",
|
|
30
|
+
"@yuants/data-product": "0.5.3",
|
|
31
|
+
"@yuants/data-quote": "0.4.2",
|
|
32
|
+
"@yuants/data-ohlc": "0.5.1",
|
|
33
|
+
"@yuants/data-interest-rate": "0.2.1",
|
|
34
|
+
"@yuants/cache": "0.3.6",
|
|
35
|
+
"@yuants/sql": "0.9.33",
|
|
36
|
+
"@yuants/utils": "0.17.0",
|
|
37
37
|
"rxjs": "~7.5.6"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
package/temp/package-deps.json
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
"libraries/exchange/CHANGELOG.json": "
|
|
3
|
-
"libraries/exchange/CHANGELOG.md": "
|
|
2
|
+
"libraries/exchange/CHANGELOG.json": "5e6593ce398f3f85f9db34be8ab6437cdfd4fea0",
|
|
3
|
+
"libraries/exchange/CHANGELOG.md": "c30615709466184bdc3dab9f4fae0fb8317aaf11",
|
|
4
4
|
"libraries/exchange/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
|
|
5
5
|
"libraries/exchange/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
|
|
6
6
|
"libraries/exchange/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
|
|
7
7
|
"libraries/exchange/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
|
|
8
8
|
"libraries/exchange/etc/exchange.api.md": "5cbfbfb185ea230234d254b34a778ee25073ac3c",
|
|
9
|
-
"libraries/exchange/package.json": "
|
|
9
|
+
"libraries/exchange/package.json": "f4a468733c9eeb0e36f0f5b7aa0c01d09db78b81",
|
|
10
10
|
"libraries/exchange/src/index.ts": "3eb2b464a88063b8730b8d7f671553bfdfc88ce6",
|
|
11
11
|
"libraries/exchange/src/interest_rate.test.ts": "9197fe2ace778d3dd6ef6043926d776abf21ab89",
|
|
12
|
-
"libraries/exchange/src/interest_rate.ts": "
|
|
12
|
+
"libraries/exchange/src/interest_rate.ts": "f8eb61acc74cb16f30c98ecd382b877eb74f49f3",
|
|
13
13
|
"libraries/exchange/src/ohlc.test.ts": "975b880372c2c608d09944a37e294244bd6e488b",
|
|
14
|
-
"libraries/exchange/src/ohlc.ts": "
|
|
14
|
+
"libraries/exchange/src/ohlc.ts": "d1158eb491144c7dca01df70c86dfce18bb7f08e",
|
|
15
15
|
"libraries/exchange/src/quote.test.ts": "2d50be70e6a924498719ad4fe940e0b95d0dfa09",
|
|
16
16
|
"libraries/exchange/src/quote.ts": "663393e8f22b1e11f675e05f6d3c19d06862366e",
|
|
17
17
|
"libraries/exchange/src/types.ts": "302d5383c38c053ac2ea8fac028b9d4921f1abe0",
|
|
18
18
|
"libraries/exchange/tsconfig.json": "22f94ca28b507f8ddcc21b9053158eefd3f726a9",
|
|
19
19
|
"libraries/exchange/.rush/temp/shrinkwrap-deps.json": "75a385701243e189b795559fc82d660f1a355805",
|
|
20
|
-
"libraries/protocol/temp/package-deps.json": "
|
|
21
|
-
"libraries/data-account/temp/package-deps.json": "
|
|
22
|
-
"libraries/data-order/temp/package-deps.json": "
|
|
23
|
-
"libraries/data-product/temp/package-deps.json": "
|
|
24
|
-
"libraries/data-quote/temp/package-deps.json": "
|
|
25
|
-
"libraries/data-ohlc/temp/package-deps.json": "
|
|
26
|
-
"libraries/data-interest-rate/temp/package-deps.json": "
|
|
27
|
-
"libraries/cache/temp/package-deps.json": "
|
|
28
|
-
"libraries/sql/temp/package-deps.json": "
|
|
29
|
-
"libraries/utils/temp/package-deps.json": "
|
|
20
|
+
"libraries/protocol/temp/package-deps.json": "359ac43a7382bafa1f91541e084256baec86d40b",
|
|
21
|
+
"libraries/data-account/temp/package-deps.json": "a94782e226c47a0f5198a5cca619ae7815055f4a",
|
|
22
|
+
"libraries/data-order/temp/package-deps.json": "e89dd8f11cdbe84eb8bdf474105abccdf51abfaa",
|
|
23
|
+
"libraries/data-product/temp/package-deps.json": "00c23f21d7977f38a942ed06f4812e5ddc2b6248",
|
|
24
|
+
"libraries/data-quote/temp/package-deps.json": "6ee7c2a4c28dd7947039fd9561ff045400848bde",
|
|
25
|
+
"libraries/data-ohlc/temp/package-deps.json": "919692801e80ce34e9af7470712ac4d1c0086af7",
|
|
26
|
+
"libraries/data-interest-rate/temp/package-deps.json": "5f955d9b9c04936964ba20a37d5e00816fb33f67",
|
|
27
|
+
"libraries/cache/temp/package-deps.json": "6f7f290f9a9c2e42e937d8ce3b55a6b541d2e835",
|
|
28
|
+
"libraries/sql/temp/package-deps.json": "43f7034c83d7ac4b6c4224b8e916ac39fd78057d",
|
|
29
|
+
"libraries/utils/temp/package-deps.json": "acd039ba8f5f93e664764ad2c8474878bd105c3b",
|
|
30
30
|
"tools/toolkit/temp/package-deps.json": "23e053490eb8feade23e4d45de4e54883e322711"
|
|
31
31
|
}
|