数据报告生成器
| 18 | import re |
| 19 | |
| 20 | class DataReportGenerator: |
| 21 | """数据报告生成器""" |
| 22 | |
| 23 | def __init__(self, factors_data: Dict): |
| 24 | self.factors_data = factors_data |
| 25 | self.console = Console() |
| 26 | self.market_type = os.environ.get('CONTEST_TRADE_MARKET', 'CN-Stock') |
| 27 | |
| 28 | def get_text(self, cn_text: str, en_text: str) -> str: |
| 29 | return en_text if self.market_type == 'US-Stock' else cn_text |
| 30 | |
| 31 | def generate_markdown_report(self, save_path: Path) -> str: |
| 32 | """生成数据报告的Markdown格式""" |
| 33 | |
| 34 | # 获取触发时间 |
| 35 | trigger_time = self.factors_data.get('trigger_time', 'N/A') |
| 36 | |
| 37 | # 统计数据源数量和代理数量 |
| 38 | total_agents = len(self.factors_data.get('agents', {})) |
| 39 | |
| 40 | report_content = f"""# ContestTrade {self.get_text('数据分析报告', 'Data Analysis Report')} |
| 41 | |
| 42 | ## 📊 {self.get_text('数据摘要', 'Data Summary')} |
| 43 | |
| 44 | **{self.get_text('分析时间', 'Analysis Time')}**: {trigger_time} |
| 45 | **{self.get_text('分析状态', 'Analysis Status')}**: ✅ {self.get_text('完成', 'Completed')} |
| 46 | **{self.get_text('数据代理数量', 'Data Agent Count')}**: {total_agents} |
| 47 | |
| 48 | --- |
| 49 | |
| 50 | ## 🔍 {self.get_text('数据源分析详情', 'Data Source Analysis Details')} |
| 51 | |
| 52 | """ |
| 53 | |
| 54 | # 遍历每个代理的数据 |
| 55 | for agent_name, agent_data in self.factors_data.get('agents', {}).items(): |
| 56 | report_content += f"### 📈 {agent_name.replace('_', ' ').title()}\n\n" |
| 57 | |
| 58 | # 只获取context_string字段 |
| 59 | context_string = agent_data.get('context_string', '') |
| 60 | |
| 61 | if context_string: |
| 62 | # 清洗掉 [Batch X] 标记 |
| 63 | cleaned_context = re.sub(r'\[Batch \d+\]', '', context_string).strip() |
| 64 | report_content += f"{cleaned_context}\n\n" |
| 65 | else: |
| 66 | report_content += f"**{self.get_text('暂无分析内容', 'No analysis content available')}**\n\n" |
| 67 | |
| 68 | report_content += "---\n\n" |
| 69 | |
| 70 | # 免责声明 |
| 71 | report_content += f"## ⚠️ {self.get_text('免责声明', 'Disclaimer')}\n\n" |
| 72 | report_content += self.get_text("本报告由ContestTrade数据分析系统生成,数据来源于各个数据代理的分析结果,仅供参考。\n\n", "This report is generated by the ContestTrade data analysis system. The data comes from the analysis results of various data agents and is for reference only.\n\n") |
| 73 | report_content += f"**{self.get_text('报告生成时间', 'Report Generation Time')}**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n" |
| 74 | report_content += f"**{self.get_text('系统版本', 'System Version')}**: ContestTrade v1.1.0\n" |
| 75 | |
| 76 | # 保存到文件 |
| 77 | with open(save_path, 'w', encoding='utf-8') as f: |
no outgoing calls
no test coverage detected