T-210: M2.3s — Implement MarketDataStore.macroAsOf, crossSection, QuestDB fx('1m'), and copilot 1m routing
This commit is contained in:
parent
a3a31f7562
commit
0c33d8ae19
23 changed files with 2440 additions and 82 deletions
|
|
@ -27,12 +27,22 @@ export {
|
|||
type IlpV2EncodeErrorKind,
|
||||
} from './ilp-v2-writer';
|
||||
export {
|
||||
applyM2IntradayDDL,
|
||||
formatCreateTableSQL,
|
||||
getM2IntradayDDL,
|
||||
M2_INTRADAY_SCHEMAS,
|
||||
M2_INTRADAY_TABLE_NAMES,
|
||||
QuestDBSchemaManager,
|
||||
type ApplyM2IntradayDDLOptions,
|
||||
type ApplyM2IntradayDDLResult,
|
||||
type M2IntradayTableName,
|
||||
} from './schema';
|
||||
export {
|
||||
openQuestDbPgExecutor,
|
||||
type QuestDbPgExecutorConfig,
|
||||
type QuestDbPgExecutorHandle,
|
||||
type QuestDbPgQueryExecutor,
|
||||
} from './pg-executor';
|
||||
|
||||
// Types
|
||||
export type {
|
||||
|
|
|
|||
80
libs/data/questdb/src/pg-executor.ts
Normal file
80
libs/data/questdb/src/pg-executor.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import { Pool, type PoolConfig } from 'pg';
|
||||
|
||||
export interface QuestDbPgExecutorConfig {
|
||||
readonly url?: string;
|
||||
readonly host?: string;
|
||||
readonly port?: number;
|
||||
readonly database?: string;
|
||||
readonly user?: string;
|
||||
readonly password?: string;
|
||||
readonly ssl?: boolean;
|
||||
readonly max?: number;
|
||||
readonly connectionTimeoutMillis?: number;
|
||||
readonly idleTimeoutMillis?: number;
|
||||
}
|
||||
|
||||
export interface QuestDbPgQueryExecutor {
|
||||
query(
|
||||
sql: string,
|
||||
params?: readonly unknown[]
|
||||
): Promise<{ readonly rows: readonly Record<string, unknown>[] }>;
|
||||
}
|
||||
|
||||
export interface QuestDbPgExecutorHandle {
|
||||
readonly executor: QuestDbPgQueryExecutor;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a lightweight PGWire `pg.Pool` executor for QuestDB read queries or
|
||||
* opt-in DDL without creating legacy tables or starting background health timers.
|
||||
*/
|
||||
export async function openQuestDbPgExecutor(
|
||||
config: QuestDbPgExecutorConfig
|
||||
): Promise<QuestDbPgExecutorHandle> {
|
||||
const poolConfig: PoolConfig = {
|
||||
max: config.max ?? 4,
|
||||
connectionTimeoutMillis: config.connectionTimeoutMillis ?? 5_000,
|
||||
idleTimeoutMillis: config.idleTimeoutMillis ?? 30_000,
|
||||
...(config.url !== undefined
|
||||
? { connectionString: config.url }
|
||||
: {
|
||||
host: config.host ?? 'localhost',
|
||||
port: config.port ?? 8812,
|
||||
database: config.database ?? 'qdb',
|
||||
...(config.user !== undefined ? { user: config.user } : {}),
|
||||
...(config.password !== undefined ? { password: config.password } : {}),
|
||||
}),
|
||||
...(config.ssl !== undefined ? { ssl: config.ssl } : {}),
|
||||
};
|
||||
|
||||
const pool = new Pool(poolConfig);
|
||||
try {
|
||||
await pool.query('SELECT 1');
|
||||
} catch (err: unknown) {
|
||||
await pool.end().catch(() => undefined);
|
||||
throw err;
|
||||
}
|
||||
|
||||
let closePromise: Promise<void> | null = null;
|
||||
const close = (): Promise<void> => {
|
||||
if (closePromise === null) {
|
||||
closePromise = pool.end();
|
||||
}
|
||||
return closePromise;
|
||||
};
|
||||
|
||||
const executor: QuestDbPgQueryExecutor = {
|
||||
async query(sql: string, params?: readonly unknown[]) {
|
||||
const result =
|
||||
params !== undefined && params.length > 0
|
||||
? await pool.query(sql, [...params])
|
||||
: await pool.query(sql);
|
||||
return {
|
||||
rows: result.rows as readonly Record<string, unknown>[],
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
return { executor, close };
|
||||
}
|
||||
|
|
@ -6,6 +6,14 @@ import type { ColumnSchema, QuestDBQueryable, TableSchema } from './types';
|
|||
|
||||
export type M2IntradayTableName = 'bars_1m' | 'fx_1m' | 'bar_revisions';
|
||||
|
||||
export const M2_INTRADAY_TABLE_NAMES = [
|
||||
'bars_1m',
|
||||
'fx_1m',
|
||||
'bar_revisions',
|
||||
] as const satisfies readonly M2IntradayTableName[];
|
||||
|
||||
const M2_INTRADAY_TABLE_NAME_SET: ReadonlySet<string> = new Set<string>(M2_INTRADAY_TABLE_NAMES);
|
||||
|
||||
/**
|
||||
* Contract schemas for M2 QuestDB 10.2.0 intraday tables (`design/DATA-CONTRACTS.md` §3.B).
|
||||
* Kept opt-in (outside `QuestDBSchemaManager.initializeDatabase()`).
|
||||
|
|
@ -144,6 +152,68 @@ export function getM2IntradayDDL(): Record<M2IntradayTableName, string> {
|
|||
};
|
||||
}
|
||||
|
||||
export interface ApplyM2IntradayDDLOptions {
|
||||
/** Subset of M2 intraday tables to initialize. Defaults to all three (`bars_1m`, `fx_1m`, `bar_revisions`). */
|
||||
readonly tables?: readonly M2IntradayTableName[];
|
||||
}
|
||||
|
||||
export interface ApplyM2IntradayDDLResult {
|
||||
readonly appliedTables: readonly M2IntradayTableName[];
|
||||
readonly statements: readonly string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Opt-in initializer that executes `CREATE TABLE IF NOT EXISTS` DDL from
|
||||
* {@link getM2IntradayDDL} for `bars_1m`, `fx_1m`, and `bar_revisions`.
|
||||
*
|
||||
* Kept strictly opt-in and never invoked by `QuestDBSchemaManager.initializeDatabase()`
|
||||
* or `QuestDBClient.connect()`.
|
||||
*/
|
||||
export async function applyM2IntradayDDL(
|
||||
client: { query(sql: string, params?: readonly unknown[]): Promise<unknown> },
|
||||
options?: ApplyM2IntradayDDLOptions
|
||||
): Promise<ApplyM2IntradayDDLResult> {
|
||||
if (typeof client !== 'object' || client === null || typeof client.query !== 'function') {
|
||||
throw new Error('Expected a QuestDB queryable client with a query(sql) method.');
|
||||
}
|
||||
|
||||
let tables: M2IntradayTableName[];
|
||||
if (options?.tables === undefined) {
|
||||
tables = [...M2_INTRADAY_TABLE_NAMES];
|
||||
} else {
|
||||
if (!Array.isArray(options.tables) || options.tables.length === 0) {
|
||||
throw new Error('Expected a non-empty array of M2 intraday table names.');
|
||||
}
|
||||
const seen = new Set<M2IntradayTableName>();
|
||||
tables = [];
|
||||
for (const rawName of options.tables) {
|
||||
if (typeof rawName !== 'string' || !M2_INTRADAY_TABLE_NAME_SET.has(rawName)) {
|
||||
throw new Error(
|
||||
`Unknown M2 intraday table "${String(rawName)}". Expected one of: ${M2_INTRADAY_TABLE_NAMES.join(', ')}.`
|
||||
);
|
||||
}
|
||||
const name = rawName as M2IntradayTableName;
|
||||
if (!seen.has(name)) {
|
||||
seen.add(name);
|
||||
tables.push(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ddl = getM2IntradayDDL();
|
||||
const statements: string[] = [];
|
||||
for (const table of tables) {
|
||||
const sql = ddl[table];
|
||||
await client.query(sql);
|
||||
statements.push(sql);
|
||||
}
|
||||
|
||||
return {
|
||||
appliedTables: tables,
|
||||
statements,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* QuestDB Schema Manager
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue