|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
// TQAPI数据源实现 - 使用Python服务
|
|
|
|
|
import { DataSource } from './DataSource';
|
|
|
|
|
import { logger } from '../../utils/logger';
|
|
|
|
|
|
|
|
|
|
// 简化的HTTP客户端
|
|
|
|
|
class HttpClient {
|
|
|
|
|
@ -16,18 +17,18 @@ class HttpClient {
|
|
|
|
|
url += `?${queryString}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('发送GET请求:', url);
|
|
|
|
|
logger.log('发送GET请求:', url);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 设置20秒超时
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
console.error('GET请求超时,正在中止请求...');
|
|
|
|
|
logger.error('GET请求超时,正在中止请求...');
|
|
|
|
|
controller.abort();
|
|
|
|
|
}, 20000);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('正在发送GET请求...');
|
|
|
|
|
logger.log('正在发送GET请求...');
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
@ -37,44 +38,44 @@ class HttpClient {
|
|
|
|
|
signal: controller.signal
|
|
|
|
|
});
|
|
|
|
|
const end = Date.now();
|
|
|
|
|
console.log(`GET请求完成,耗时: ${end - start}ms`);
|
|
|
|
|
console.log('GET请求响应状态:', response.status);
|
|
|
|
|
logger.log(`GET请求完成,耗时: ${end - start}ms`);
|
|
|
|
|
logger.log('GET请求响应状态:', response.status);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const errorText = await response.text();
|
|
|
|
|
console.error('GET请求失败:', errorText);
|
|
|
|
|
logger.error('GET请求失败:', errorText);
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('正在解析GET响应数据...');
|
|
|
|
|
logger.log('正在解析GET响应数据...');
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
console.log('GET请求响应数据:', data);
|
|
|
|
|
logger.log('GET请求响应数据:', data);
|
|
|
|
|
return data as T;
|
|
|
|
|
} finally {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('GET请求网络错误:', error.message || error);
|
|
|
|
|
console.error('错误详情:', error);
|
|
|
|
|
console.error('错误堆栈:', error.stack);
|
|
|
|
|
logger.error('GET请求网络错误:', error.message || error);
|
|
|
|
|
logger.error('错误详情:', error);
|
|
|
|
|
logger.error('错误堆栈:', error.stack);
|
|
|
|
|
throw new Error(`网络请求失败: ${error.message || error}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async post<T>(endpoint: string, data?: any): Promise<T> {
|
|
|
|
|
const url = `${this.baseUrl}${endpoint}`;
|
|
|
|
|
console.log('发送POST请求:', url, '数据:', data);
|
|
|
|
|
logger.log('发送POST请求:', url, '数据:', data);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 设置20秒超时
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
console.error('POST请求超时,正在中止请求...');
|
|
|
|
|
logger.error('POST请求超时,正在中止请求...');
|
|
|
|
|
controller.abort();
|
|
|
|
|
}, 20000);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('正在发送请求...');
|
|
|
|
|
logger.log('正在发送请求...');
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
@ -85,26 +86,26 @@ class HttpClient {
|
|
|
|
|
signal: controller.signal
|
|
|
|
|
});
|
|
|
|
|
const end = Date.now();
|
|
|
|
|
console.log(`请求完成,耗时: ${end - start}ms`);
|
|
|
|
|
console.log('POST请求响应状态:', response.status);
|
|
|
|
|
logger.log(`请求完成,耗时: ${end - start}ms`);
|
|
|
|
|
logger.log('POST请求响应状态:', response.status);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const errorText = await response.text();
|
|
|
|
|
console.error('POST请求失败:', errorText);
|
|
|
|
|
logger.error('POST请求失败:', errorText);
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('正在解析响应数据...');
|
|
|
|
|
logger.log('正在解析响应数据...');
|
|
|
|
|
const responseData = await response.json();
|
|
|
|
|
console.log('POST请求响应数据:', responseData);
|
|
|
|
|
logger.log('POST请求响应数据:', responseData);
|
|
|
|
|
return responseData as T;
|
|
|
|
|
} finally {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('POST请求网络错误:', error.message || error);
|
|
|
|
|
console.error('错误详情:', error);
|
|
|
|
|
console.error('错误堆栈:', error.stack);
|
|
|
|
|
logger.error('POST请求网络错误:', error.message || error);
|
|
|
|
|
logger.error('错误详情:', error);
|
|
|
|
|
logger.error('错误堆栈:', error.stack);
|
|
|
|
|
throw new Error(`网络请求失败: ${error.message || error}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -123,7 +124,7 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor(config: any = {}) {
|
|
|
|
|
console.log('使用TQAPI数据源初始化...');
|
|
|
|
|
logger.log('使用TQAPI数据源初始化...');
|
|
|
|
|
// 从配置中读取端口,默认8001
|
|
|
|
|
const port = config.pythonPort || 8001;
|
|
|
|
|
this.config = {
|
|
|
|
|
@ -134,26 +135,26 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
maxConnections: config.maxConnections || 5,
|
|
|
|
|
pythonServiceUrl: config.pythonServiceUrl || `http://127.0.0.1:${port}/api`
|
|
|
|
|
};
|
|
|
|
|
console.log('TQAPI数据源配置:', this.config);
|
|
|
|
|
logger.log('TQAPI数据源配置:', this.config);
|
|
|
|
|
// 测试Python服务URL是否正确
|
|
|
|
|
console.log('测试Python服务URL:', this.config.pythonServiceUrl);
|
|
|
|
|
logger.log('测试Python服务URL:', this.config.pythonServiceUrl);
|
|
|
|
|
this.httpClient = new HttpClient(this.config.pythonServiceUrl!);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async initialize(): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
console.log('开始初始化TQAPI数据源...');
|
|
|
|
|
console.log('Python服务URL:', this.config.pythonServiceUrl);
|
|
|
|
|
console.log('连接参数:', {
|
|
|
|
|
logger.log('开始初始化TQAPI数据源...');
|
|
|
|
|
logger.log('Python服务URL:', this.config.pythonServiceUrl);
|
|
|
|
|
logger.log('连接参数:', {
|
|
|
|
|
username: this.config.username ? '***' : '',
|
|
|
|
|
password: this.config.password ? '***' : ''
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 先测试Python服务是否可达
|
|
|
|
|
console.log('测试Python服务是否可达...');
|
|
|
|
|
logger.log('测试Python服务是否可达...');
|
|
|
|
|
try {
|
|
|
|
|
const testUrl = `${this.config.pythonServiceUrl}/../health`;
|
|
|
|
|
console.log('测试URL:', testUrl);
|
|
|
|
|
logger.log('测试URL:', testUrl);
|
|
|
|
|
// 使用AbortController设置超时
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
|
|
|
|
@ -162,21 +163,21 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
signal: controller.signal
|
|
|
|
|
});
|
|
|
|
|
console.log('Python服务健康检查响应:', testResponse.status);
|
|
|
|
|
logger.log('Python服务健康检查响应:', testResponse.status);
|
|
|
|
|
if (testResponse.ok) {
|
|
|
|
|
console.log('Python服务可达');
|
|
|
|
|
logger.log('Python服务可达');
|
|
|
|
|
} else {
|
|
|
|
|
console.error('Python服务不可达,状态码:', testResponse.status);
|
|
|
|
|
logger.error('Python服务不可达,状态码:', testResponse.status);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('Python服务健康检查失败:', error.message || error);
|
|
|
|
|
logger.error('Python服务健康检查失败:', error.message || error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 连接到天勤服务器
|
|
|
|
|
console.log('连接到天勤服务器...');
|
|
|
|
|
logger.log('连接到天勤服务器...');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await this.httpClient.post<any>('/connect', {
|
|
|
|
|
@ -184,24 +185,24 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
password: this.config.password
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('连接响应:', response);
|
|
|
|
|
logger.log('连接响应:', response);
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
console.log('TQAPI连接成功');
|
|
|
|
|
logger.log('TQAPI连接成功');
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
console.error('TQAPI连接失败:', response.message);
|
|
|
|
|
logger.error('TQAPI连接失败:', response.message);
|
|
|
|
|
this.initialized = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('连接请求失败:', error.message || error);
|
|
|
|
|
console.error('错误详情:', error);
|
|
|
|
|
logger.error('连接请求失败:', error.message || error);
|
|
|
|
|
logger.error('错误详情:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('TQAPI数据源初始化失败:', error);
|
|
|
|
|
logger.error('TQAPI数据源初始化失败:', error);
|
|
|
|
|
this.initialized = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
@ -213,18 +214,18 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('获取合约列表...');
|
|
|
|
|
logger.log('获取合约列表...');
|
|
|
|
|
const response = await this.httpClient.get<any>('/contracts');
|
|
|
|
|
|
|
|
|
|
if (response.success && Array.isArray(response.data)) {
|
|
|
|
|
console.log('获取合约列表成功,数量:', response.data.length);
|
|
|
|
|
logger.log('获取合约列表成功,数量:', response.data.length);
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
console.error('获取合约列表失败:', response.message);
|
|
|
|
|
logger.error('获取合约列表失败:', response.message);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取合约列表失败:', error);
|
|
|
|
|
logger.error('获取合约列表失败:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -235,18 +236,18 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log(`获取合约${symbol}详情...`);
|
|
|
|
|
logger.log(`获取合约${symbol}详情...`);
|
|
|
|
|
const response = await this.httpClient.get<any>(`/contract/${symbol}`);
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
console.log(`获取合约${symbol}详情成功`);
|
|
|
|
|
logger.log(`获取合约${symbol}详情成功`);
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
console.error(`获取合约${symbol}详情失败:`, response.message);
|
|
|
|
|
logger.error(`获取合约${symbol}详情失败:`, response.message);
|
|
|
|
|
throw new Error(`获取合约${symbol}详情失败`);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`获取合约${symbol}详情失败:`, error);
|
|
|
|
|
logger.error(`获取合约${symbol}详情失败:`, error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -257,7 +258,7 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('开始获取K线数据:', {
|
|
|
|
|
logger.log('开始获取K线数据:', {
|
|
|
|
|
symbol: symbol,
|
|
|
|
|
period: period,
|
|
|
|
|
count: count
|
|
|
|
|
@ -267,10 +268,10 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
let contractSymbol = symbol;
|
|
|
|
|
if (!contractSymbol.includes('.')) {
|
|
|
|
|
// 如果没有交易所前缀,尝试添加默认交易所
|
|
|
|
|
console.warn('合约代码缺少交易所前缀,尝试添加默认交易所');
|
|
|
|
|
logger.warn('合约代码缺少交易所前缀,尝试添加默认交易所');
|
|
|
|
|
contractSymbol = `SHFE.${contractSymbol}`;
|
|
|
|
|
}
|
|
|
|
|
console.log('使用的合约代码:', contractSymbol);
|
|
|
|
|
logger.log('使用的合约代码:', contractSymbol);
|
|
|
|
|
|
|
|
|
|
const response = await this.httpClient.get<any>('/klines/' + contractSymbol, {
|
|
|
|
|
period: period,
|
|
|
|
|
@ -278,14 +279,14 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.success && Array.isArray(response.data)) {
|
|
|
|
|
console.log('获取K线数据成功,长度:', response.data.length);
|
|
|
|
|
logger.log('获取K线数据成功,长度:', response.data.length);
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
console.error('获取K线数据失败:', response.message);
|
|
|
|
|
logger.error('获取K线数据失败:', response.message);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`获取合约${symbol}K线数据失败:`, error);
|
|
|
|
|
logger.error(`获取合约${symbol}K线数据失败:`, error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -296,33 +297,33 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log(`获取合约${symbol}实时行情数据...`);
|
|
|
|
|
logger.log(`获取合约${symbol}实时行情数据...`);
|
|
|
|
|
|
|
|
|
|
// 确保合约代码格式正确
|
|
|
|
|
let contractSymbol = symbol;
|
|
|
|
|
if (!contractSymbol.includes('.')) {
|
|
|
|
|
// 如果没有交易所前缀,尝试添加默认交易所
|
|
|
|
|
console.warn('合约代码缺少交易所前缀,尝试添加默认交易所');
|
|
|
|
|
logger.warn('合约代码缺少交易所前缀,尝试添加默认交易所');
|
|
|
|
|
contractSymbol = `SHFE.${contractSymbol}`;
|
|
|
|
|
}
|
|
|
|
|
console.log('使用的合约代码:', contractSymbol);
|
|
|
|
|
logger.log('使用的合约代码:', contractSymbol);
|
|
|
|
|
|
|
|
|
|
console.log('正在发送GET请求到:', `/tick/${contractSymbol}`);
|
|
|
|
|
logger.log('正在发送GET请求到:', `/tick/${contractSymbol}`);
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
const response = await this.httpClient.get<any>(`/tick/${contractSymbol}`);
|
|
|
|
|
const end = Date.now();
|
|
|
|
|
console.log(`GET请求完成,耗时: ${end - start}ms`);
|
|
|
|
|
console.log('响应数据:', response);
|
|
|
|
|
logger.log(`GET请求完成,耗时: ${end - start}ms`);
|
|
|
|
|
logger.log('响应数据:', response);
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
console.log(`获取合约${symbol}实时行情数据成功`);
|
|
|
|
|
logger.log(`获取合约${symbol}实时行情数据成功`);
|
|
|
|
|
// 计算价格变化
|
|
|
|
|
const tickData = response.data;
|
|
|
|
|
tickData.price_change = tickData.last_price - (tickData.pre_close || 0);
|
|
|
|
|
console.log('计算价格变化后的数据:', tickData);
|
|
|
|
|
logger.log('计算价格变化后的数据:', tickData);
|
|
|
|
|
return tickData;
|
|
|
|
|
} else {
|
|
|
|
|
console.error(`获取合约${symbol}实时行情数据失败:`, response.message);
|
|
|
|
|
logger.error(`获取合约${symbol}实时行情数据失败:`, response.message);
|
|
|
|
|
// 返回默认数据,避免整个请求失败
|
|
|
|
|
return {
|
|
|
|
|
last_price: 0,
|
|
|
|
|
@ -341,7 +342,7 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`获取合约${symbol}实时行情数据失败:`, error);
|
|
|
|
|
logger.error(`获取合约${symbol}实时行情数据失败:`, error);
|
|
|
|
|
// 返回默认数据,避免整个请求失败
|
|
|
|
|
return {
|
|
|
|
|
last_price: 0,
|
|
|
|
|
@ -393,12 +394,12 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
open_interest: open_interest
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`获取合约${contract.symbol}行情失败:`, error);
|
|
|
|
|
logger.error(`获取合约${contract.symbol}行情失败:`, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return overview;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取市场概览失败:', error);
|
|
|
|
|
logger.error('获取市场概览失败:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -410,29 +411,29 @@ export class TQDataSource implements DataSource {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 目前Python服务未实现此功能
|
|
|
|
|
console.warn('TQAPI服务暂未实现历史成交数据获取功能');
|
|
|
|
|
logger.warn('TQAPI服务暂未实现历史成交数据获取功能');
|
|
|
|
|
return [];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`获取合约${symbol}历史成交数据失败:`, error);
|
|
|
|
|
logger.error(`获取合约${symbol}历史成交数据失败:`, error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
console.log('关闭TQAPI连接...');
|
|
|
|
|
logger.log('关闭TQAPI连接...');
|
|
|
|
|
const response = await this.httpClient.post<any>('/disconnect');
|
|
|
|
|
|
|
|
|
|
if (response.success) {
|
|
|
|
|
console.log('TQAPI连接已关闭');
|
|
|
|
|
logger.log('TQAPI连接已关闭');
|
|
|
|
|
} else {
|
|
|
|
|
console.error('关闭TQAPI连接失败:', response.message);
|
|
|
|
|
logger.error('关闭TQAPI连接失败:', response.message);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('关闭TQAPI连接失败:', error);
|
|
|
|
|
logger.error('关闭TQAPI连接失败:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
this.initialized = false;
|
|
|
|
|
console.log('TQAPI数据源已关闭');
|
|
|
|
|
logger.log('TQAPI数据源已关闭');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|