完整功能
This commit is contained in:
43
utils/helpers.py
Normal file
43
utils/helpers.py
Normal file
@ -0,0 +1,43 @@
|
||||
"""工具函数模块"""
|
||||
|
||||
|
||||
def get_f_block_positions(artifact_list):
|
||||
"""获取artifact_list中F块的位置"""
|
||||
positions = []
|
||||
for i, artifact in enumerate(artifact_list):
|
||||
if artifact.get("BlockNumber") == "F":
|
||||
positions.append(i)
|
||||
return positions
|
||||
|
||||
|
||||
def cleanup_old_timestamps(artifact_timestamps, current_artifact_ids=None, max_age_hours=24):
|
||||
"""
|
||||
清理过期的时间戳记录
|
||||
|
||||
Args:
|
||||
artifact_timestamps: 时间戳字典
|
||||
current_artifact_ids: 当前活跃的artifact_id集合,可选
|
||||
max_age_hours: 时间戳最大保留时间(小时),默认24小时
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
current_time = datetime.now()
|
||||
expired_ids = []
|
||||
|
||||
# 遍历所有存储的时间戳记录
|
||||
for artifact_id, timestamp in artifact_timestamps.items():
|
||||
# 如果提供了当前活跃ID列表,且该ID不在当前活跃列表中,则检查是否过期
|
||||
if current_artifact_ids is None or artifact_id not in current_artifact_ids:
|
||||
age = current_time - timestamp
|
||||
# 如果超过最大保留时间,则标记为过期
|
||||
if age.total_seconds() > max_age_hours * 3600:
|
||||
expired_ids.append(artifact_id)
|
||||
|
||||
# 删除过期的时间戳记录
|
||||
for artifact_id in expired_ids:
|
||||
del artifact_timestamps[artifact_id]
|
||||
|
||||
if expired_ids:
|
||||
print(f"清理了 {len(expired_ids)} 个过期的时间戳记录: {expired_ids}")
|
||||
|
||||
return len(expired_ids)
|
||||
Reference in New Issue
Block a user