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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
主入口脚本:执行完整的统计报表流程
|
| 3 |
+
"""
|
| 4 |
+
import argparse
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
# 导入各模块
|
| 9 |
+
from stage_a_stats import StageAStats
|
| 10 |
+
from stage_b_stats import StageBStats
|
| 11 |
+
from repo_meta_scan import RepoMetaScan
|
| 12 |
+
from code_file_stats import CodeFileStats
|
| 13 |
+
from code_file_stats_fast import CodeFileStatsFast # 优化版本
|
| 14 |
+
from visualization import generate_all_visualizations
|
| 15 |
+
from join_insights import JoinInsights
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def main():
|
| 19 |
+
parser = argparse.ArgumentParser(description='生成数据统计报表')
|
| 20 |
+
parser.add_argument('--repos-searched', type=str,
|
| 21 |
+
default='/home/weifengsun/tangou1/domain_code/src/workdir/repos_searched.csv',
|
| 22 |
+
help='repos_searched.csv路径')
|
| 23 |
+
parser.add_argument('--repos-check-history', type=str,
|
| 24 |
+
default='/home/weifengsun/tangou1/domain_code/src/workdir/repos_check_history.csv',
|
| 25 |
+
help='repos_check_history.csv路径')
|
| 26 |
+
parser.add_argument('--repos-filtered', type=str,
|
| 27 |
+
default='/home/weifengsun/tangou1/domain_code/src/workdir/repos_filtered',
|
| 28 |
+
help='repos_filtered目录路径')
|
| 29 |
+
parser.add_argument('--output-dir', type=str,
|
| 30 |
+
default='/home/weifengsun/tangou1/domain_code/src/workdir/reporting',
|
| 31 |
+
help='输出目录')
|
| 32 |
+
parser.add_argument('--top-n', type=int, default=None,
|
| 33 |
+
help='分析的仓库数量(字典序前N个,None表示所有)')
|
| 34 |
+
parser.add_argument('--workers', type=int, default=8,
|
| 35 |
+
help='代码文件统计的并行worker数(默认CPU-1)')
|
| 36 |
+
parser.add_argument('--stage-a', action='store_true',
|
| 37 |
+
help='运行Stage A(搜索阶段统计)')
|
| 38 |
+
parser.add_argument('--stage-b', action='store_true',
|
| 39 |
+
help='运行Stage B(过滤阶段统计)')
|
| 40 |
+
parser.add_argument('--repo-meta', action='store_true',
|
| 41 |
+
help='运行仓库元画像扫描')
|
| 42 |
+
parser.add_argument('--code-stats', action='store_true',
|
| 43 |
+
help='运行代码文件级统计')
|
| 44 |
+
parser.add_argument('--code-stats-fast', action='store_true',
|
| 45 |
+
help='运行代码文件级统计(快速版本,约提速10-20倍)')
|
| 46 |
+
parser.add_argument('--visualization', action='store_true',
|
| 47 |
+
help='生成图表(需要先有stage-a, stage-b, repo-meta, code-stats的数据)')
|
| 48 |
+
parser.add_argument('--insights', action='store_true',
|
| 49 |
+
help='运行关联分析(需要先有stage-a, code-stats, stage-b的数据)')
|
| 50 |
+
|
| 51 |
+
args = parser.parse_args()
|
| 52 |
+
|
| 53 |
+
output_dir = Path(args.output_dir)
|
| 54 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 55 |
+
|
| 56 |
+
print("=" * 80)
|
| 57 |
+
print("数据统计报表生成系统")
|
| 58 |
+
print("=" * 80)
|
| 59 |
+
print(f"输出目录: {output_dir}")
|
| 60 |
+
print(f"分析仓库数: {args.top_n if args.top_n else '所有'}")
|
| 61 |
+
|
| 62 |
+
# 检查是否有指定任何阶段
|
| 63 |
+
has_stage = any([
|
| 64 |
+
args.stage_a, args.stage_b, args.repo_meta,
|
| 65 |
+
args.code_stats, args.code_stats_fast, args.visualization, args.insights
|
| 66 |
+
])
|
| 67 |
+
|
| 68 |
+
if not has_stage:
|
| 69 |
+
print("\n错误: 请至少指定一个要运行的阶段!")
|
| 70 |
+
print("可用选项:")
|
| 71 |
+
print(" --stage-a 运行Stage A(搜索阶段统计)")
|
| 72 |
+
print(" --stage-b 运行Stage B(过滤阶段统计)")
|
| 73 |
+
print(" --repo-meta 运行仓库元画像扫描")
|
| 74 |
+
print(" --code-stats 运行代码文件级统计")
|
| 75 |
+
print(" --code-stats-fast 运行代码文件级统计(快速版本,推荐)")
|
| 76 |
+
print(" --visualization 生成图表")
|
| 77 |
+
print(" --insights 运行关联分析")
|
| 78 |
+
print("\n示例: python main.py --stage-a --stage-b")
|
| 79 |
+
return
|
| 80 |
+
|
| 81 |
+
print()
|
| 82 |
+
|
| 83 |
+
# 定义输出目录路径(即使不运行也需要,因为可能被其他阶段使用)
|
| 84 |
+
stage_a_dir = output_dir / 'stage_a'
|
| 85 |
+
stage_b_dir = output_dir / 'stage_b'
|
| 86 |
+
repo_meta_dir = output_dir / 'repo_meta'
|
| 87 |
+
code_stats_dir = output_dir / 'code_stats'
|
| 88 |
+
|
| 89 |
+
# Stage A: 搜索阶段统计
|
| 90 |
+
if args.stage_a:
|
| 91 |
+
print("\n" + "=" * 80)
|
| 92 |
+
print("Stage A: 搜索阶段统计 (repos_searched.csv)")
|
| 93 |
+
print("=" * 80)
|
| 94 |
+
stage_a_stats = StageAStats(args.repos_searched, stage_a_dir)
|
| 95 |
+
stage_a_stats.run()
|
| 96 |
+
|
| 97 |
+
# Stage B: 过滤阶段统计
|
| 98 |
+
if args.stage_b:
|
| 99 |
+
print("\n" + "=" * 80)
|
| 100 |
+
print("Stage B: 过滤阶段统计 (repos_check_history.csv)")
|
| 101 |
+
print("=" * 80)
|
| 102 |
+
stage_b_stats = StageBStats(args.repos_check_history, stage_b_dir)
|
| 103 |
+
stage_b_stats.run()
|
| 104 |
+
|
| 105 |
+
# 仓库元画像扫描
|
| 106 |
+
if args.repo_meta:
|
| 107 |
+
print("\n" + "=" * 80)
|
| 108 |
+
print("仓库元画像扫描 (repos_filtered)")
|
| 109 |
+
print("=" * 80)
|
| 110 |
+
repo_meta_scanner = RepoMetaScan(args.repos_filtered, repo_meta_dir, top_n=args.top_n)
|
| 111 |
+
repo_meta_scanner.run()
|
| 112 |
+
|
| 113 |
+
# Stage C: 代码文件级统计
|
| 114 |
+
if args.code_stats:
|
| 115 |
+
print("\n" + "=" * 80)
|
| 116 |
+
print("Stage C: 代码文件级统计(原版)")
|
| 117 |
+
print("=" * 80)
|
| 118 |
+
code_stats = CodeFileStats(args.repos_filtered, code_stats_dir,
|
| 119 |
+
top_n=args.top_n)
|
| 120 |
+
code_stats.run(num_workers=args.workers)
|
| 121 |
+
|
| 122 |
+
# Stage C: 代码文件级统计(快速版本)
|
| 123 |
+
if args.code_stats_fast:
|
| 124 |
+
print("\n" + "=" * 80)
|
| 125 |
+
print("Stage C: 代码文件级统计(快速版本)")
|
| 126 |
+
print("=" * 80)
|
| 127 |
+
code_stats_fast = CodeFileStatsFast(
|
| 128 |
+
args.repos_filtered,
|
| 129 |
+
code_stats_dir,
|
| 130 |
+
top_n=args.top_n,
|
| 131 |
+
max_file_size_mb=2,
|
| 132 |
+
max_files_per_repo=500 # 限制每个仓库最多500个文件
|
| 133 |
+
)
|
| 134 |
+
code_stats_fast.run(num_workers=args.workers if args.workers else 48)
|
| 135 |
+
|
| 136 |
+
# 图表生成(需要前面的数据)
|
| 137 |
+
if args.visualization:
|
| 138 |
+
print("\n" + "=" * 80)
|
| 139 |
+
print("生成图表")
|
| 140 |
+
print("=" * 80)
|
| 141 |
+
# 检查必要的数据是否存在
|
| 142 |
+
required_dirs = [stage_a_dir, stage_b_dir, repo_meta_dir, code_stats_dir]
|
| 143 |
+
missing_dirs = [d for d in required_dirs if not d.exists()]
|
| 144 |
+
if missing_dirs:
|
| 145 |
+
print(f"警告: 以下目录不存在,图表生成可能不完整: {[str(d) for d in missing_dirs]}")
|
| 146 |
+
|
| 147 |
+
generate_all_visualizations(
|
| 148 |
+
str(stage_a_dir),
|
| 149 |
+
str(stage_b_dir),
|
| 150 |
+
str(repo_meta_dir),
|
| 151 |
+
str(code_stats_dir),
|
| 152 |
+
args.repos_searched,
|
| 153 |
+
top_n=args.top_n
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# 关联分析(需要前面的数据)
|
| 157 |
+
if args.insights:
|
| 158 |
+
print("\n" + "=" * 80)
|
| 159 |
+
print("关联分析与洞察")
|
| 160 |
+
print("=" * 80)
|
| 161 |
+
# 检查必要的数据是否存在(动态文件名)
|
| 162 |
+
top_n_suffix = f"_top{args.top_n}" if args.top_n else ""
|
| 163 |
+
repo_level_csv = code_stats_dir / f'repo_level_metrics{top_n_suffix}.csv'
|
| 164 |
+
if not repo_level_csv.exists():
|
| 165 |
+
print(f"错误: 代码统计文件不存在: {repo_level_csv}")
|
| 166 |
+
print("请先运行 --code-stats")
|
| 167 |
+
return
|
| 168 |
+
|
| 169 |
+
insights_dir = output_dir / 'insights'
|
| 170 |
+
join_insights = JoinInsights(
|
| 171 |
+
args.repos_searched,
|
| 172 |
+
str(repo_level_csv),
|
| 173 |
+
args.repos_check_history,
|
| 174 |
+
str(insights_dir)
|
| 175 |
+
)
|
| 176 |
+
join_insights.run()
|
| 177 |
+
|
| 178 |
+
print("\n" + "=" * 80)
|
| 179 |
+
print("完成!所有结果已保存到:")
|
| 180 |
+
print(f" - 数据表格: {output_dir}")
|
| 181 |
+
print(f" - 图表: {output_dir / 'figures'}")
|
| 182 |
+
print("=" * 80)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
main()
|
| 187 |
+
|