commit 2 refactor
This commit is contained in:
parent
588f666943
commit
32e43c9243
310 changed files with 33318 additions and 31189 deletions
|
|
@ -1,4 +1,6 @@
|
|||
import type { PoolConfig, QueryResultRow } from 'pg';
|
||||
import { Pool } from 'pg';
|
||||
import type { LoggerLike } from '@stock-bot/logger';
|
||||
import { QuestDBHealthMonitor } from './health';
|
||||
import { QuestDBInfluxWriter } from './influx-writer';
|
||||
import { QuestDBQueryBuilder } from './query-builder';
|
||||
|
|
@ -20,13 +22,17 @@ export class QuestDBClient {
|
|||
private pgPool: Pool | null = null;
|
||||
private readonly config: QuestDBClientConfig;
|
||||
private readonly options: QuestDBConnectionOptions;
|
||||
private readonly logger: any;
|
||||
private readonly logger: LoggerLike;
|
||||
private readonly healthMonitor: QuestDBHealthMonitor;
|
||||
private readonly influxWriter: QuestDBInfluxWriter;
|
||||
private readonly schemaManager: QuestDBSchemaManager;
|
||||
private isConnected = false;
|
||||
|
||||
constructor(config: QuestDBClientConfig, logger?: any, options?: QuestDBConnectionOptions) {
|
||||
constructor(
|
||||
config: QuestDBClientConfig,
|
||||
logger?: LoggerLike,
|
||||
options?: QuestDBConnectionOptions
|
||||
) {
|
||||
this.config = config;
|
||||
this.logger = logger || console;
|
||||
this.options = {
|
||||
|
|
@ -128,7 +134,7 @@ export class QuestDBClient {
|
|||
/**
|
||||
* Execute a SQL query
|
||||
*/
|
||||
async query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>> {
|
||||
async query<T = QueryResultRow>(sql: string, params?: unknown[]): Promise<QueryResult<T>> {
|
||||
if (!this.pgPool) {
|
||||
throw new Error('QuestDB client not connected');
|
||||
}
|
||||
|
|
@ -150,7 +156,7 @@ export class QuestDBClient {
|
|||
executionTime,
|
||||
metadata: {
|
||||
columns:
|
||||
result.fields?.map((field: any) => ({
|
||||
result.fields?.map(field => ({
|
||||
name: field.name,
|
||||
type: this.mapDataType(field.dataTypeID),
|
||||
})) || [],
|
||||
|
|
@ -227,13 +233,13 @@ export class QuestDBClient {
|
|||
/**
|
||||
* Execute a time-series specific query with SAMPLE BY
|
||||
*/
|
||||
async sampleBy<T = any>(
|
||||
async sampleBy<T = QueryResultRow>(
|
||||
table: TableNames,
|
||||
columns: string[],
|
||||
interval: string,
|
||||
_timeColumn: string = 'timestamp',
|
||||
where?: string,
|
||||
params?: any[]
|
||||
params?: unknown[]
|
||||
): Promise<QueryResult<T>> {
|
||||
const columnsStr = columns.join(', ');
|
||||
const whereClause = where ? `WHERE ${where}` : '';
|
||||
|
|
@ -252,7 +258,7 @@ export class QuestDBClient {
|
|||
/**
|
||||
* Get latest values by symbol using LATEST BY
|
||||
*/
|
||||
async latestBy<T = any>(
|
||||
async latestBy<T = QueryResultRow>(
|
||||
table: TableNames,
|
||||
columns: string | string[] = '*',
|
||||
keyColumns: string | string[] = 'symbol'
|
||||
|
|
@ -272,13 +278,13 @@ export class QuestDBClient {
|
|||
/**
|
||||
* Execute ASOF JOIN for time-series correlation
|
||||
*/
|
||||
async asofJoin<T = any>(
|
||||
async asofJoin<T = QueryResultRow>(
|
||||
leftTable: TableNames,
|
||||
rightTable: TableNames,
|
||||
joinCondition: string,
|
||||
columns?: string[],
|
||||
where?: string,
|
||||
params?: any[]
|
||||
params?: unknown[]
|
||||
): Promise<QueryResult<T>> {
|
||||
const columnsStr = columns ? columns.join(', ') : '*';
|
||||
const whereClause = where ? `WHERE ${where}` : '';
|
||||
|
|
@ -296,7 +302,7 @@ export class QuestDBClient {
|
|||
/**
|
||||
* Get database statistics
|
||||
*/
|
||||
async getStats(): Promise<any> {
|
||||
async getStats(): Promise<QueryResultRow[]> {
|
||||
const result = await this.query(`
|
||||
SELECT
|
||||
table_name,
|
||||
|
|
@ -313,7 +319,7 @@ export class QuestDBClient {
|
|||
/**
|
||||
* Get table information
|
||||
*/
|
||||
async getTableInfo(tableName: string): Promise<any> {
|
||||
async getTableInfo(tableName: string): Promise<QueryResultRow[]> {
|
||||
const result = await this.query(`SELECT * FROM table_columns WHERE table_name = ?`, [
|
||||
tableName,
|
||||
]);
|
||||
|
|
@ -412,8 +418,8 @@ export class QuestDBClient {
|
|||
return { ...this.config };
|
||||
}
|
||||
|
||||
private buildPgPoolConfig(): any {
|
||||
const config: any = {
|
||||
private buildPgPoolConfig(): PoolConfig {
|
||||
const config: PoolConfig = {
|
||||
host: this.config.host,
|
||||
port: this.config.pgPort,
|
||||
database: this.config.database,
|
||||
|
|
@ -430,7 +436,7 @@ export class QuestDBClient {
|
|||
|
||||
// Only add user/password if they are provided
|
||||
if (this.config.user) {
|
||||
this.logger.debug('Adding user to QuestDB pool config:', this.config.user);
|
||||
this.logger.debug('Adding user to QuestDB pool config', { user: this.config.user });
|
||||
config.user = this.config.user;
|
||||
} else {
|
||||
this.logger.debug('No user provided for QuestDB connection');
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import type { QueryResultRow } from 'pg';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { HealthStatus, PerformanceMetrics, QueryResult } from './types';
|
||||
|
||||
// Interface to avoid circular dependency
|
||||
interface QuestDBClientInterface {
|
||||
query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>;
|
||||
query<T = QueryResultRow>(sql: string, params?: unknown[]): Promise<QueryResult<T>>;
|
||||
isPgPoolHealthy(): boolean;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import type { QueryResultRow } from 'pg';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { QueryResult, TableNames, TimeRange } from './types';
|
||||
|
||||
// Interface to avoid circular dependency
|
||||
interface QuestDBClientInterface {
|
||||
query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>;
|
||||
query<T = QueryResultRow>(sql: string, params?: unknown[]): Promise<QueryResult<T>>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -190,7 +191,7 @@ export class QuestDBQueryBuilder {
|
|||
/**
|
||||
* Build and execute the query
|
||||
*/
|
||||
public async execute<T = any>(): Promise<QueryResult<T>> {
|
||||
public async execute<T = QueryResultRow>(): Promise<QueryResult<T>> {
|
||||
const sql = this.build();
|
||||
this.logger.debug('Executing query', { sql });
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
import type { QueryResultRow } from 'pg';
|
||||
import { at } from '@stock-bot/invariants';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { QueryResult, TableSchema } from './types';
|
||||
|
||||
// Interface to avoid circular dependency
|
||||
interface QuestDBClientInterface {
|
||||
query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>;
|
||||
query<T = QueryResultRow>(sql: string, params?: unknown[]): Promise<QueryResult<T>>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -215,7 +217,7 @@ export class QuestDBSchemaManager {
|
|||
WHERE table_name = '${tableName}'
|
||||
`);
|
||||
|
||||
return result.rows.length > 0 && result.rows[0].count > 0;
|
||||
return result.rows.length > 0 && at(result.rows, 0).count > 0;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error checking if table exists: ${tableName}`, error);
|
||||
return false;
|
||||
|
|
@ -266,7 +268,7 @@ export class QuestDBSchemaManager {
|
|||
/**
|
||||
* Get table statistics
|
||||
*/
|
||||
public async getTableStats(tableName: string): Promise<any> {
|
||||
public async getTableStats(tableName: string): Promise<QueryResultRow | undefined> {
|
||||
try {
|
||||
const result = await this.client.query(`
|
||||
SELECT
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import type { QueryResultRow } from 'pg';
|
||||
|
||||
/**
|
||||
* QuestDB Client Configuration and Types
|
||||
*/
|
||||
|
|
@ -101,7 +103,7 @@ export interface QuoteData extends BaseTimeSeriesData {
|
|||
export interface IndicatorData extends BaseTimeSeriesData {
|
||||
indicator_name: string;
|
||||
value: number;
|
||||
parameters?: Record<string, any>;
|
||||
parameters?: Record<string, unknown>;
|
||||
timeframe: string;
|
||||
}
|
||||
|
||||
|
|
@ -127,7 +129,7 @@ export interface RiskMetrics extends BaseTimeSeriesData {
|
|||
/**
|
||||
* Query Result Types
|
||||
*/
|
||||
export interface QueryResult<T = any> {
|
||||
export interface QueryResult<T = QueryResultRow> {
|
||||
rows: T[];
|
||||
rowCount: number;
|
||||
executionTime: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue