T-210: M2.3s — Implement MarketDataStore.macroAsOf, crossSection, QuestDB fx('1m'), and copilot 1m routing
This commit is contained in:
parent
c60365237f
commit
5e255d7e30
8 changed files with 587 additions and 37 deletions
|
|
@ -15,12 +15,17 @@
|
|||
|
||||
import {
|
||||
applyM2IntradayDDL,
|
||||
assertNoConflictingQuestDbSsl,
|
||||
openQuestDbPgExecutor,
|
||||
type QuestDbPgExecutorConfig,
|
||||
} from '@stock-bot/questdb';
|
||||
|
||||
function readEnv(primary: string, fallback: string): string | undefined {
|
||||
const value = process.env[primary] ?? process.env[fallback];
|
||||
function readEnv(
|
||||
env: Readonly<Record<string, string | undefined>>,
|
||||
primary: string,
|
||||
fallback: string
|
||||
): string | undefined {
|
||||
const value = env[primary] ?? env[fallback];
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
|
@ -28,34 +33,47 @@ function readEnv(primary: string, fallback: string): string | undefined {
|
|||
return trimmed.length > 0 ? trimmed : undefined;
|
||||
}
|
||||
|
||||
function resolveConfig(): QuestDbPgExecutorConfig {
|
||||
const url = readEnv('QUESTDB_URL', 'RESEARCH_MCP_QUESTDB_URL');
|
||||
const sslRaw = readEnv('QUESTDB_SSL', 'RESEARCH_MCP_QUESTDB_SSL');
|
||||
const ssl =
|
||||
sslRaw === undefined
|
||||
? undefined
|
||||
: sslRaw.toLowerCase() === 'true' || sslRaw === '1'
|
||||
? true
|
||||
: sslRaw.toLowerCase() === 'false' || sslRaw === '0'
|
||||
? false
|
||||
: undefined;
|
||||
function parseSslFlag(raw: string): boolean {
|
||||
const normalized = raw.trim().toLowerCase();
|
||||
if (normalized === 'true' || normalized === '1') {
|
||||
return true;
|
||||
}
|
||||
if (normalized === 'false' || normalized === '0') {
|
||||
return false;
|
||||
}
|
||||
throw new RangeError(
|
||||
'Invalid QuestDB SSL configuration: expected "true", "false", "1", or "0".'
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveQuestDbInitM2Config(
|
||||
env: Readonly<Record<string, string | undefined>> = process.env
|
||||
): QuestDbPgExecutorConfig {
|
||||
const url = readEnv(env, 'QUESTDB_URL', 'RESEARCH_MCP_QUESTDB_URL');
|
||||
const sslRaw = readEnv(env, 'QUESTDB_SSL', 'RESEARCH_MCP_QUESTDB_SSL');
|
||||
const ssl = sslRaw !== undefined ? parseSslFlag(sslRaw) : undefined;
|
||||
|
||||
if (url !== undefined) {
|
||||
if (ssl !== undefined) {
|
||||
assertNoConflictingQuestDbSsl(url, ssl);
|
||||
}
|
||||
return {
|
||||
url,
|
||||
...(ssl !== undefined ? { ssl } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
const host = readEnv('QUESTDB_HOST', 'RESEARCH_MCP_QUESTDB_HOST') ?? 'localhost';
|
||||
const portRaw = readEnv('QUESTDB_PG_PORT', 'RESEARCH_MCP_QUESTDB_PG_PORT');
|
||||
const host = readEnv(env, 'QUESTDB_HOST', 'RESEARCH_MCP_QUESTDB_HOST') ?? 'localhost';
|
||||
const portRaw = readEnv(env, 'QUESTDB_PG_PORT', 'RESEARCH_MCP_QUESTDB_PG_PORT');
|
||||
const port = portRaw !== undefined ? Number(portRaw) : 8812;
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
throw new Error(`Invalid QuestDB PGWire port "${String(portRaw)}". Expected 1..65535.`);
|
||||
throw new RangeError(
|
||||
`Invalid QuestDB PGWire port "${String(portRaw)}". Expected 1..65535.`
|
||||
);
|
||||
}
|
||||
const database = readEnv('QUESTDB_DATABASE', 'RESEARCH_MCP_QUESTDB_DATABASE') ?? 'qdb';
|
||||
const user = readEnv('QUESTDB_USER', 'RESEARCH_MCP_QUESTDB_USER');
|
||||
const password = readEnv('QUESTDB_PASSWORD', 'RESEARCH_MCP_QUESTDB_PASSWORD');
|
||||
const database = readEnv(env, 'QUESTDB_DATABASE', 'RESEARCH_MCP_QUESTDB_DATABASE') ?? 'qdb';
|
||||
const user = readEnv(env, 'QUESTDB_USER', 'RESEARCH_MCP_QUESTDB_USER');
|
||||
const password = readEnv(env, 'QUESTDB_PASSWORD', 'RESEARCH_MCP_QUESTDB_PASSWORD');
|
||||
|
||||
return {
|
||||
host,
|
||||
|
|
@ -67,21 +85,29 @@ function resolveConfig(): QuestDbPgExecutorConfig {
|
|||
};
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const config = resolveConfig();
|
||||
const handle = await openQuestDbPgExecutor(config);
|
||||
export async function runQuestDbInitM2(
|
||||
env: Readonly<Record<string, string | undefined>> = process.env,
|
||||
openExecutor: typeof openQuestDbPgExecutor = openQuestDbPgExecutor
|
||||
): Promise<{ readonly appliedTables: readonly string[] }> {
|
||||
const config = resolveQuestDbInitM2Config(env);
|
||||
const handle = await openExecutor(config);
|
||||
try {
|
||||
const result = await applyM2IntradayDDL(handle.executor);
|
||||
process.stdout.write(
|
||||
`Initialized QuestDB M2 intraday tables: ${result.appliedTables.join(', ')}\n`
|
||||
);
|
||||
return await applyM2IntradayDDL(handle.executor);
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((err: unknown) => {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
process.stderr.write(`questdb-init-m2 failed: ${message}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
if (import.meta.main) {
|
||||
runQuestDbInitM2()
|
||||
.then(result => {
|
||||
process.stdout.write(
|
||||
`Initialized QuestDB M2 intraday tables: ${result.appliedTables.join(', ')}\n`
|
||||
);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
process.stderr.write(`questdb-init-m2 failed: ${message}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
78
scripts/test/questdb-init-m2.test.ts
Normal file
78
scripts/test/questdb-init-m2.test.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { describe, expect, test } from 'bun:test';
|
||||
import { M2_INTRADAY_TABLE_NAMES } from '@stock-bot/questdb';
|
||||
import { resolveQuestDbInitM2Config, runQuestDbInitM2 } from '../questdb-init-m2';
|
||||
|
||||
describe('scripts/questdb-init-m2', () => {
|
||||
test('rejects conflicting URL SSL settings before opening a QuestDB connection', async () => {
|
||||
const secret = 'ddl_init_s3cr3t!';
|
||||
const baseUrl = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
|
||||
|
||||
const conflictingEnvs: Array<Record<string, string>> = [
|
||||
{ QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'true' },
|
||||
{ RESEARCH_MCP_QUESTDB_URL: `${baseUrl}?sslmode=disable`, RESEARCH_MCP_QUESTDB_SSL: 'true' },
|
||||
{ QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'false' },
|
||||
{ QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'true' },
|
||||
{ QUESTDB_URL: `${baseUrl}?sslmode=prefer`, QUESTDB_SSL: 'true' },
|
||||
];
|
||||
|
||||
let openCalls = 0;
|
||||
for (const env of conflictingEnvs) {
|
||||
let caught: unknown;
|
||||
try {
|
||||
await runQuestDbInitM2(env, async () => {
|
||||
openCalls += 1;
|
||||
throw new Error('openExecutor must not be called on conflicting SSL config');
|
||||
});
|
||||
} catch (err) {
|
||||
caught = err;
|
||||
}
|
||||
expect(caught).toBeInstanceOf(RangeError);
|
||||
expect((caught as Error).message).toContain('SSL');
|
||||
expect((caught as Error).message).not.toContain(secret);
|
||||
expect((caught as Error).message).not.toContain(baseUrl);
|
||||
}
|
||||
expect(openCalls).toBe(0);
|
||||
});
|
||||
|
||||
test('rejects invalid SSL flag values and accepts consistent URL + SSL configs', async () => {
|
||||
expect(() =>
|
||||
resolveQuestDbInitM2Config({
|
||||
QUESTDB_HOST: '10.0.0.51',
|
||||
QUESTDB_SSL: 'maybe',
|
||||
})
|
||||
).toThrow(RangeError);
|
||||
|
||||
const resolved = resolveQuestDbInitM2Config({
|
||||
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
|
||||
QUESTDB_SSL: 'true',
|
||||
});
|
||||
expect(resolved).toEqual({
|
||||
url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
|
||||
ssl: true,
|
||||
});
|
||||
|
||||
const executed: string[] = [];
|
||||
let closed = false;
|
||||
const result = await runQuestDbInitM2(
|
||||
{
|
||||
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=disable',
|
||||
QUESTDB_SSL: 'false',
|
||||
},
|
||||
async () => ({
|
||||
executor: {
|
||||
async query(sql: string) {
|
||||
executed.push(sql);
|
||||
return { rows: [] };
|
||||
},
|
||||
},
|
||||
async close() {
|
||||
closed = true;
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
expect(result.appliedTables).toEqual([...M2_INTRADAY_TABLE_NAMES]);
|
||||
expect(executed).toHaveLength(3);
|
||||
expect(closed).toBe(true);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue