You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
9.7 KiB
234 lines
9.7 KiB
#!/usr/bin/env python3
|
|
"""
|
|
前端集成示例
|
|
演示前端如何调用API接口展示中间处理过程
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
from datetime import datetime
|
|
|
|
API_BASE = "http://127.0.0.1:8080/api"
|
|
|
|
class FrontendIntegrationExample:
|
|
"""模拟前端调用后端API的示例"""
|
|
|
|
def __init__(self):
|
|
self.processing_steps = [
|
|
{
|
|
'name': '原始数据',
|
|
'api': 'original-data',
|
|
'description': '文件上传后的原始ATC对话',
|
|
'icon': '📤'
|
|
},
|
|
{
|
|
'name': '数据预处理',
|
|
'api': 'preprocessed-data',
|
|
'description': '清理格式、去除多余空格',
|
|
'icon': '🧹'
|
|
},
|
|
{
|
|
'name': '格式合并',
|
|
'api': 'merged-data',
|
|
'description': '统一航空术语格式',
|
|
'icon': '📋'
|
|
},
|
|
{
|
|
'name': '单词纠错',
|
|
'api': 'corrected-data',
|
|
'description': '修正拼写、语音字母转换',
|
|
'icon': '✏️'
|
|
},
|
|
{
|
|
'name': 'AI信息提取',
|
|
'api': 'processed-data',
|
|
'description': 'AI提取结构化飞行信息',
|
|
'icon': '🤖'
|
|
},
|
|
{
|
|
'name': '最终验证',
|
|
'api': 'final-data',
|
|
'description': '验证通过的有效数据',
|
|
'icon': '✅'
|
|
}
|
|
]
|
|
|
|
def simulate_step_by_step_processing(self):
|
|
"""模拟前端分步处理模式"""
|
|
print("🎯 前端集成示例 - 分步处理模式")
|
|
print("="*70)
|
|
print("📝 模拟前端界面展示中间处理过程的数据")
|
|
print()
|
|
|
|
# 模拟处理步骤
|
|
processing_apis = [
|
|
('preprocess', '数据预处理'),
|
|
('merge', '格式合并'),
|
|
('correct', '单词纠错'),
|
|
('analyze', 'AI分析')
|
|
]
|
|
|
|
print("🔄 执行处理步骤:")
|
|
for api, name in processing_apis:
|
|
print(f" 🔧 执行{name}...")
|
|
try:
|
|
response = requests.post(f"{API_BASE}/{api}/", json={}, timeout=30)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print(f" ✅ {name}完成: {result['message']}")
|
|
else:
|
|
print(f" ❌ {name}失败: {response.status_code}")
|
|
except Exception as e:
|
|
print(f" ❌ {name}异常: {e}")
|
|
|
|
print()
|
|
|
|
# 模拟前端展示各步骤数据
|
|
print("📊 前端界面展示 - 中间处理过程数据:")
|
|
print("="*70)
|
|
|
|
for i, step in enumerate(self.processing_steps, 1):
|
|
print(f"\\n{step['icon']} 步骤{i}: {step['name']}")
|
|
print(f" 📝 {step['description']}")
|
|
|
|
try:
|
|
response = requests.get(f"{API_BASE}/{step['api']}/", timeout=10)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
count = data['count']
|
|
records = data.get('data', [])
|
|
|
|
print(f" 📊 数据量: {count} 条记录")
|
|
|
|
# 显示数据样例(模拟前端表格展示)
|
|
if records and len(records) > 0:
|
|
print(f" 📋 数据样例:")
|
|
|
|
# 根据不同步骤显示不同的关键字段
|
|
if step['api'] == 'original-data':
|
|
for j, record in enumerate(records[:2], 1):
|
|
print(f" {j}. ID: {record.get('id', 'N/A')} | 原文: {record.get('text', 'N/A')[:50]}...")
|
|
|
|
elif step['api'] == 'preprocessed-data':
|
|
for j, record in enumerate(records[:2], 1):
|
|
print(f" {j}. ID: {record.get('id', 'N/A')} | 清理操作: {record.get('cleaning_operations', 'N/A')}")
|
|
|
|
elif step['api'] == 'merged-data':
|
|
for j, record in enumerate(records[:2], 1):
|
|
print(f" {j}. ID: {record.get('id', 'N/A')} | 格式操作: {record.get('format_operations', 'N/A')}")
|
|
|
|
elif step['api'] == 'corrected-data':
|
|
for j, record in enumerate(records[:2], 1):
|
|
corrections = record.get('corrections_made', 'N/A')
|
|
count_corr = record.get('correction_count', 0)
|
|
print(f" {j}. ID: {record.get('id', 'N/A')} | 纠错: {corrections} ({count_corr}处)")
|
|
|
|
elif step['api'] == 'processed-data':
|
|
for j, record in enumerate(records[:2], 1):
|
|
call_sign = record.get('call_sign', 'N/A')
|
|
behavior = record.get('behavior', 'N/A')
|
|
print(f" {j}. ID: {record.get('id', 'N/A')} | 呼号: {call_sign} | 行为: {behavior}")
|
|
|
|
elif step['api'] == 'final-data':
|
|
for j, record in enumerate(records[:2], 1):
|
|
call_sign = record.get('Call Sign', 'N/A')
|
|
behavior = record.get('Behavior', 'N/A')
|
|
print(f" {j}. ID: {record.get('id', 'N/A')} | 呼号: {call_sign} | 行为: {behavior}")
|
|
|
|
else:
|
|
print(f" 📋 暂无数据")
|
|
|
|
else:
|
|
print(f" ❌ 数据获取失败: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f" ❌ 数据获取异常: {e}")
|
|
|
|
# 显示统计信息
|
|
print(f"\\n📈 处理统计信息:")
|
|
print("="*30)
|
|
|
|
try:
|
|
response = requests.get(f"{API_BASE}/statistics/", timeout=10)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
stats = data['statistics']
|
|
|
|
print(f" 📊 数据流向: {stats.get('original_count', 0)} → {stats.get('extracted_count', 0)} → {stats.get('valid_count', 0)}")
|
|
print(f" 🎯 提取效率: {stats.get('extraction_rate', 0)}%")
|
|
print(f" ✅ 验证通过率: {stats.get('validation_rate', 0)}%")
|
|
print(f" ❌ 错误率: {stats.get('error_rate', 0)}%")
|
|
except Exception as e:
|
|
print(f" ❌ 统计信息获取失败: {e}")
|
|
|
|
def generate_frontend_api_guide(self):
|
|
"""生成前端API调用指南"""
|
|
print(f"\\n📚 前端API调用指南")
|
|
print("="*50)
|
|
|
|
print(f"\\n🔄 分步处理模式 (推荐用于前端展示):")
|
|
print(f"```javascript")
|
|
print(f"// 1. 文件上传")
|
|
print(f"const uploadResponse = await fetch('/api/upload/', {{")
|
|
print(f" method: 'POST',")
|
|
print(f" body: formData")
|
|
print(f"}});")
|
|
print(f"")
|
|
print(f"// 2. 执行处理步骤并展示结果")
|
|
print(f"const steps = ['preprocess', 'merge', 'correct', 'analyze'];")
|
|
print(f"for (const step of steps) {{")
|
|
print(f" // 执行处理")
|
|
print(f" await fetch(`/api/${{step}}/`, {{ method: 'POST' }});")
|
|
print(f" ")
|
|
print(f" // 获取结果数据")
|
|
print(f" const dataEndpoint = getDataEndpoint(step);")
|
|
print(f" const result = await fetch(`/api/${{dataEndpoint}}/`);")
|
|
print(f" const data = await result.json();")
|
|
print(f" ")
|
|
print(f" // 更新界面显示")
|
|
print(f" updateUI(step, data);")
|
|
print(f"}}")
|
|
print(f"```")
|
|
|
|
print(f"\\n📊 数据获取API映射:")
|
|
for i, step in enumerate(self.processing_steps, 1):
|
|
print(f" {i}. {step['name']}: GET /api/{step['api']}/")
|
|
|
|
print(f"\\n🎯 前端界面建议:")
|
|
print(f" 1. 设计进度条显示处理步骤")
|
|
print(f" 2. 每个步骤完成后展示数据表格")
|
|
print(f" 3. 支持切换查看不同步骤的数据")
|
|
print(f" 4. 显示数据转换的统计信息")
|
|
print(f" 5. 提供数据导出功能")
|
|
|
|
print(f"\\n⚡ 性能优化建议:")
|
|
print(f" 1. 使用分页加载大量数据")
|
|
print(f" 2. 缓存中间步骤的结果")
|
|
print(f" 3. 支持异步处理模式")
|
|
print(f" 4. 添加处理进度指示器")
|
|
|
|
def run_example(self):
|
|
"""运行完整示例"""
|
|
print(f"🚀 前端集成示例演示")
|
|
print(f"📅 时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
print()
|
|
|
|
# 执行分步处理演示
|
|
self.simulate_step_by_step_processing()
|
|
|
|
# 生成API调用指南
|
|
self.generate_frontend_api_guide()
|
|
|
|
print(f"\\n🎉 前端集成示例演示完成!")
|
|
print(f"💡 现在前端可以完整展示数据处理的中间过程")
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
example = FrontendIntegrationExample()
|
|
example.run_example()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
exit(main()) |