1
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import List, Dict, Optional, Any
|
||||
from loguru import logger
|
||||
|
||||
@@ -29,3 +30,85 @@ def safe_str(self, value: Any, default: str = '') -> str:
|
||||
except Exception as e:
|
||||
logger.error(f"safe_str error: {e}")
|
||||
return ""
|
||||
|
||||
def convert_timestamp(timestamp, return_type='str', format_str='%Y-%m-%d %H:%M:%S'):
|
||||
"""
|
||||
时间戳转换函数,支持多种返回格式
|
||||
|
||||
Args:
|
||||
timestamp: 时间戳(支持整数、浮点数、字符串)
|
||||
return_type: 返回类型 'str'/'datetime'/'dict'
|
||||
format_str: 当return_type='str'时的格式字符串
|
||||
|
||||
Returns:
|
||||
根据return_type返回不同格式的数据
|
||||
"""
|
||||
try:
|
||||
# 转换为浮点数
|
||||
if isinstance(timestamp, str):
|
||||
timestamp = float(timestamp)
|
||||
else:
|
||||
timestamp = float(timestamp)
|
||||
|
||||
# 判断时间戳精度
|
||||
original_timestamp = timestamp
|
||||
|
||||
# 精确判断逻辑
|
||||
if timestamp > 4102444800000: # 2100-01-01 的毫秒级时间戳
|
||||
# 可能是微秒级,转换为秒级
|
||||
timestamp = timestamp / 1000000
|
||||
precision = 'microseconds'
|
||||
elif timestamp > 4102444800: # 2100-01-01 的秒级时间戳
|
||||
# 毫秒级时间戳
|
||||
timestamp = timestamp / 1000
|
||||
precision = 'milliseconds'
|
||||
else:
|
||||
# 秒级时间戳
|
||||
precision = 'seconds'
|
||||
|
||||
# 转换为 datetime 对象
|
||||
dt = datetime.fromtimestamp(timestamp)
|
||||
|
||||
# 根据返回类型返回不同格式
|
||||
if return_type == 'datetime':
|
||||
return dt
|
||||
elif return_type == 'dict':
|
||||
return {
|
||||
'datetime': dt,
|
||||
'formatted': dt.strftime(format_str),
|
||||
'precision': precision,
|
||||
'original_timestamp': original_timestamp,
|
||||
'converted_timestamp': timestamp
|
||||
}
|
||||
else: # 默认返回字符串
|
||||
return dt.strftime(format_str)
|
||||
|
||||
except (ValueError, TypeError, OSError) as e:
|
||||
logger.error(f"时间戳转换失败: {timestamp}, 错误: {e}")
|
||||
return None
|
||||
|
||||
def timestamp(unit='seconds'):
|
||||
"""
|
||||
简化版时间戳获取
|
||||
|
||||
Args:
|
||||
unit: 's'/'ms'/'us' 或 'seconds'/'milliseconds'/'microseconds'
|
||||
|
||||
Returns:
|
||||
int: 时间戳
|
||||
"""
|
||||
current = time.time()
|
||||
|
||||
unit_map = {
|
||||
's': 1, 'seconds': 1,
|
||||
'ms': 1000, 'milliseconds': 1000,
|
||||
'us': 1000000, 'microseconds': 1000000
|
||||
}
|
||||
|
||||
multiplier = unit_map.get(unit.lower(), 1)
|
||||
return int(current * multiplier)
|
||||
|
||||
# 别名函数
|
||||
def ts(unit='seconds'):
|
||||
"""timestamp的别名"""
|
||||
return timestamp(unit)
|
||||
@@ -361,9 +361,10 @@ class RedisClient:
|
||||
for account_info in account_list:
|
||||
k_id = int(account_info['k_id'])
|
||||
st_id = account_info.get('st_id', 0)
|
||||
add_time = account_info.get('add_time', 0)
|
||||
|
||||
# 从Redis获取账户信息数据
|
||||
account_data = await self._get_account_info_from_redis(k_id, st_id, exchange_id)
|
||||
account_data = await self._get_account_info_from_redis(k_id, st_id, exchange_id, add_time)
|
||||
account_data_list.extend(account_data)
|
||||
|
||||
logger.debug(f"交易所 {exchange_id}: 收集到 {len(account_data_list)} 条账户信息")
|
||||
@@ -373,7 +374,7 @@ class RedisClient:
|
||||
|
||||
return account_data_list
|
||||
|
||||
async def _get_account_info_from_redis(self, k_id: int, st_id: int, exchange_id: str) -> List[Dict]:
|
||||
async def _get_account_info_from_redis(self, k_id: int, st_id: int, exchange_id: str, add_time: int) -> List[Dict]:
|
||||
"""从Redis获取账户信息数据(批量优化版本)"""
|
||||
try:
|
||||
redis_key = f"{exchange_id}:balance:{k_id}"
|
||||
@@ -383,7 +384,7 @@ class RedisClient:
|
||||
return []
|
||||
|
||||
# 按天统计数据
|
||||
recent_days = SYNC_CONFIG['recent_days']
|
||||
recent_days = SYNC_CONFIG['recent_days_account']
|
||||
|
||||
today = datetime.now()
|
||||
date_stats = {}
|
||||
@@ -394,14 +395,18 @@ class RedisClient:
|
||||
fund_data = json.loads(fund_json)
|
||||
date_str = fund_data.get('lz_time', '')
|
||||
lz_type = fund_data.get('lz_type', '')
|
||||
add_time_str = helpers.convert_timestamp(add_time,format_str='%Y-%m-%d')
|
||||
if date_str < add_time_str:
|
||||
continue
|
||||
|
||||
if not date_str or lz_type not in ['lz_balance', 'deposit', 'withdrawal']:
|
||||
continue
|
||||
|
||||
# 只处理最近N天的数据
|
||||
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
|
||||
if (today - date_obj).days > recent_days:
|
||||
continue
|
||||
if recent_days != 0:
|
||||
if (today - date_obj).days > recent_days:
|
||||
continue
|
||||
|
||||
if date_str not in date_stats:
|
||||
date_stats[date_str] = {
|
||||
@@ -554,7 +559,7 @@ class RedisClient:
|
||||
"""从Redis获取最近N天的订单数据"""
|
||||
try:
|
||||
redis_key = f"{exchange_id}:orders:{k_id}"
|
||||
recent_days = SYNC_CONFIG['recent_days']
|
||||
recent_days = SYNC_CONFIG['recent_days_order']
|
||||
# 计算最近N天的日期
|
||||
today = datetime.now()
|
||||
recent_dates = []
|
||||
|
||||
Reference in New Issue
Block a user