优化log

This commit is contained in:
zhangkun9038@dingtalk.com 2025-08-19 03:26:04 +08:00
parent 81ae1e7381
commit 36e006a8a0

View File

@ -953,7 +953,7 @@ class FreqaiPrimer(IStrategy):
if nan_count > 0:
logger.warning(f"[{pair}] 列 {col}{nan_count} 个NaN值正在清理...")
# 使用前向填充,然后零填充
dataframe[col] = dataframe[col].fillna(method='ffill').fillna(0)
dataframe[col] = dataframe[col].ffill().fillna(0)
# 最终验证
final_length = len(dataframe)
@ -1142,7 +1142,7 @@ class FreqaiPrimer(IStrategy):
if nan_count > 0:
logger.warning(f"[{pair}] 卖出检查 - 列 {col}{nan_count} 个NaN值正在清理...")
# 使用前向填充,然后零填充
dataframe[col] = dataframe[col].fillna(method='ffill').fillna(0)
dataframe[col] = dataframe[col].ffill().fillna(0)
# 最终验证
final_length = len(dataframe)
@ -1620,7 +1620,8 @@ class FreqaiPrimer(IStrategy):
if cache_key in self._local_cache:
cached_score = self._local_cache[cache_key]
self._local_cache_stats['hits'] += 1
logger.info(f"[{pair}] 🟢 本地缓存命中key={cache_key}, value={cached_score:.2f}")
# 只在调试模式下显示单个缓存命中,正常模式下只统计
logger.debug(f"[{pair}] 🟢 本地缓存命中key={cache_key}, value={cached_score:.2f}")
return cached_score
self._local_cache_stats['misses'] += 1
@ -1637,10 +1638,10 @@ class FreqaiPrimer(IStrategy):
# 同时写入本地缓存,加速后续访问
self._local_cache[cache_key] = score
self._local_cache_stats['redis_hits'] += 1
logger.info(f"[{pair}] 🟡 Redis 缓存命中key={cache_key}, value={score:.2f} (已同步到本地缓存)")
logger.debug(f"[{pair}] 🟡 Redis 缓存命中key={cache_key}, value={score:.2f} (已同步到本地缓存)")
return score
else:
logger.info(f"[{pair}] 🔴 Redis 缓存未命中key={cache_key}")
logger.debug(f"[{pair}] 🔴 Redis 缓存未命中key={cache_key}")
except Exception as e:
logger.error(f"[{pair}] Redis 查询失败key={cache_key}, 错误: {e}")
# Redis 失败时继续运行,降级为本地缓存
@ -1664,13 +1665,15 @@ class FreqaiPrimer(IStrategy):
logger.error(f"[{pair}] Redis 写入失败key={cache_key}, 错误: {e}")
# Redis 失败时继续运行
# 定期打印缓存统计信息
# 定期打印缓存统计信息(更频繁地显示本轮命中统计)
total_requests = sum(self._local_cache_stats.values())
if total_requests % 100 == 0 and total_requests > 0:
hit_rate = (self._local_cache_stats['hits'] + self._local_cache_stats['redis_hits']) / total_requests * 100
logger.info(f"📊 缓存统计 - 本地命中率: {self._local_cache_stats['hits']/total_requests*100:.1f}%, "
f"Redis命中率: {self._local_cache_stats['redis_hits']/total_requests*100:.1f}%, "
f"计算次数: {self._local_cache_stats['computes']}, "
if total_requests % 50 == 0 and total_requests > 0: # 每50次显示一次统计
local_hits = self._local_cache_stats['hits']
redis_hits = self._local_cache_stats['redis_hits']
hit_rate = (local_hits + redis_hits) / total_requests * 100
logger.info(f"📊 本轮缓存统计 - 本地命中: {local_hits}次, "
f"Redis命中: {redis_hits}次, "
f"计算: {self._local_cache_stats['computes']}次, "
f"总命中率: {hit_rate:.1f}%")
return trend_score
@ -1684,24 +1687,33 @@ class FreqaiPrimer(IStrategy):
self._local_cache.clear()
logger.info(f"🧹 已清理本地缓存,清除条目数: {cache_size}")
def get_cache_stats(self) -> dict:
def get_current_round_cache_hits(self) -> int:
"""
获取当前轮次的缓存命中总数本地+Redis
"""
if hasattr(self, '_local_cache_stats'):
return self._local_cache_stats['hits'] + self._local_cache_stats['redis_hits']
return 0
"""
获取缓存统计信息
"""
if hasattr(self, '_local_cache_stats'):
total_requests = sum(self._local_cache_stats.values())
if total_requests > 0:
local_hits = self._local_cache_stats['hits']
redis_hits = self._local_cache_stats['redis_hits']
return {
'local_hits': self._local_cache_stats['hits'],
'redis_hits': self._local_cache_stats['redis_hits'],
'local_hits': local_hits,
'redis_hits': redis_hits,
'misses': self._local_cache_stats['misses'],
'computes': self._local_cache_stats['computes'],
'total_requests': total_requests,
'local_hit_rate': self._local_cache_stats['hits'] / total_requests * 100,
'redis_hit_rate': self._local_cache_stats['redis_hits'] / total_requests * 100,
'overall_hit_rate': (self._local_cache_stats['hits'] + self._local_cache_stats['redis_hits']) / total_requests * 100
'local_hit_rate': local_hits / total_requests * 100,
'redis_hit_rate': redis_hits / total_requests * 100,
'overall_hit_rate': (local_hits + redis_hits) / total_requests * 100,
'current_round_hits': local_hits + redis_hits # 当前轮次总命中数
}
return {'local_hits': 0, 'redis_hits': 0, 'misses': 0, 'computes': 0, 'total_requests': 0}
return {'local_hits': 0, 'redis_hits': 0, 'misses': 0, 'computes': 0, 'total_requests': 0, 'current_round_hits': 0}
def bot_loop_start(self, current_time: datetime, **kwargs) -> None:
"""