Skip to content

Commit 66f6580

Browse files
committed
feat(tools): 增强数据合并功能支持进度回调和中断控制
1 parent 47a7dee commit 66f6580

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

hikyuu/tools/merge.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def merge_files_by_market(data_dir, dest_dir, market, data_type, table_desc):
586586
pass
587587

588588

589-
def merge_files_by_market_with_progress(data_dir, dest_dir, market, data_type, table_desc, stop_flag=None):
589+
def merge_files_by_market_with_progress(data_dir, dest_dir, market, data_type, table_desc, stop_flag=None, progress_callback=None, market_index=0, total_markets=1):
590590
"""
591591
合并特定市场和数据类型的所有年份文件(支持进度更新和可中断)
592592
@@ -597,6 +597,9 @@ def merge_files_by_market_with_progress(data_dir, dest_dir, market, data_type, t
597597
data_type: 数据类型
598598
table_desc: 表结构描述类
599599
stop_flag: 停止标志检查函数
600+
progress_callback: 进度回调函数,接收 (current, total, message) 参数
601+
market_index: 当前市场索引(用于计算总体进度)
602+
total_markets: 总市场数量
600603
601604
返回:
602605
bool: 是否成功完成
@@ -659,6 +662,16 @@ def merge_files_by_market_with_progress(data_dir, dest_dir, market, data_type, t
659662
progress.update(i + 1, len(year_files),
660663
sub_description=f"{os.path.basename(year_file)}")
661664

665+
# 调用进度回调,更新 GUI 进度
666+
if progress_callback:
667+
# 计算总体进度:市场进度 + 年份进度
668+
market_progress = market_index / total_markets * 100
669+
year_progress = (i + 1) / len(year_files) / total_markets * 100
670+
overall_progress = int(market_progress + year_progress)
671+
672+
progress_callback(overall_progress, 100,
673+
f"处理年份 {i+1}/{len(year_files)} - {os.path.basename(year_file)}")
674+
662675
try:
663676
src_hdf5 = tables.open_file(year_file, mode='r')
664677

@@ -907,27 +920,36 @@ def merge_all_data_from_split(src_dir, dest_dir, data_types, stop_flag=None, pro
907920
success_count = 0
908921
task_index = 0
909922

910-
for (market, data_type), year_files in merge_tasks.items():
923+
# 将 merge_tasks 转换为列表以便获取索引
924+
tasks_list = list(merge_tasks.items())
925+
total_tasks = len(tasks_list)
926+
927+
print(f"\n总计:{total_tasks} 个合并任务")
928+
929+
for market_index, ((market, data_type), year_files) in enumerate(tasks_list):
911930
task_index += 1
912931

913932
# 检查停止标志
914933
if stop_flag and stop_flag():
915934
print(f"\n用户取消处理,已完成 {task_index-1}/{total_tasks} 个任务")
916935
break
917936

918-
# 更新进度
919-
progress = int((task_index - 1) / total_tasks * 100)
937+
# 更新进度(使用市场索引计算总体进度)
938+
progress = int(market_index / total_tasks * 100)
920939
if progress_callback:
921-
progress_callback(progress, total_tasks, f"处理中:{task_index}/{total_tasks} - {market}_{data_type}")
940+
progress_callback(progress, total_tasks, f"准备处理:{market_index+1}/{total_tasks} - {market}_{data_type}")
922941

923942
print(f"\n[{task_index}/{total_tasks}] 合并 {market} 市场的 {data_type} 数据")
924943

925944
try:
926945
table_desc = get_table_desc(data_type)
927946

928-
# 使用增强版的合并函数,支持增量合并和停止检查
947+
# 使用增强版的合并函数,支持增量合并、停止检查和进度回调
929948
if merge_files_by_market_with_progress(src_dir, dest_dir, market, data_type, table_desc,
930-
stop_flag=stop_flag):
949+
stop_flag=stop_flag,
950+
progress_callback=progress_callback,
951+
market_index=market_index,
952+
total_markets=total_tasks):
931953
success_count += 1
932954

933955
except Exception as e:

0 commit comments

Comments
 (0)