68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
import requests
|
|
import json
|
|
|
|
# 测试预测API的响应格式
|
|
def test_prediction_api():
|
|
url = "http://localhost:5000/api/prediction"
|
|
|
|
# 测试数据
|
|
payload = {
|
|
"product_id": "P001",
|
|
"model_type": "tcn",
|
|
# "version": "v1", # 不指定版本,使用默认
|
|
"future_days": 7,
|
|
"start_date": "2024-01-15",
|
|
"include_visualization": True
|
|
}
|
|
|
|
print("发送预测请求...")
|
|
print(f"请求数据: {json.dumps(payload, indent=2)}")
|
|
|
|
try:
|
|
response = requests.post(url, json=payload)
|
|
print(f"响应状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
print(f"响应数据结构:")
|
|
print(f"顶级键: {list(data.keys())}")
|
|
|
|
# 检查前端期望的字段
|
|
required_fields = ['data', 'history_data', 'prediction_data']
|
|
for field in required_fields:
|
|
if field in data:
|
|
print(f"✅ {field}: 存在")
|
|
if isinstance(data[field], list):
|
|
print(f" 长度: {len(data[field])}")
|
|
elif isinstance(data[field], dict):
|
|
print(f" 子键: {list(data[field].keys())}")
|
|
else:
|
|
print(f"❌ {field}: 不存在")
|
|
|
|
# 特别检查data字段
|
|
if 'data' in data and data['data']:
|
|
print(f"data字段详情:")
|
|
if isinstance(data['data'], dict):
|
|
print(f" 是字典类型,键: {list(data['data'].keys())}")
|
|
else:
|
|
print(f" 类型: {type(data['data'])}")
|
|
|
|
# 如果有错误状态,显示
|
|
if 'status' in data:
|
|
print(f"状态: {data['status']}")
|
|
|
|
# 打印部分数据示例
|
|
if 'history_data' in data and data['history_data']:
|
|
print(f"history_data示例: {data['history_data'][:2]}")
|
|
|
|
if 'prediction_data' in data and data['prediction_data']:
|
|
print(f"prediction_data示例: {data['prediction_data'][:2]}")
|
|
|
|
else:
|
|
print(f"请求失败: {response.text}")
|
|
|
|
except Exception as e:
|
|
print(f"测试失败: {str(e)}")
|
|
|
|
if __name__ == "__main__":
|
|
test_prediction_api() |