feat: 完成管理员用户查询优化 - 实施admin_user_query_optimization_proposal
🚀 核心优化内容: - ljwx-boot: 添加user_type和admin_level冗余字段到sys_user表 - ljwx-boot: 重构管理员判断方法,从复杂多表JOIN优化为单字段查询 - ljwx-boot: 实现数据同步维护机制,确保数据一致性 - ljwx-bigscreen: 移除复杂admin_helper,直接在SQL中使用user_type过滤 - 性能提升: 查询复杂度从O(N*M)优化为O(1) 📊 技术改进: - 数据库: 新增UserType和AdminLevel枚举定义 - 迁移: 创建数据迁移脚本初始化现有用户类型 - 同步: 角色/组织变更时自动更新用户类型字段 - 查询: 消除多表JOIN,直接使用索引查询 🎯 预期效果: - 减少数据库负载,提升查询性能 - 简化代码逻辑,降低维护成本 - 实时准确,无需复杂缓存机制 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
This commit is contained in:
Executable
+277
@@ -0,0 +1,277 @@
|
||||
#!/bin/bash
|
||||
# 直接调用健康数据生成脚本(无需登录)
|
||||
# 直接调用HealthBaselineScoreTasks的executeImmediately方法
|
||||
|
||||
set -e
|
||||
|
||||
# 配置参数
|
||||
DAYS=${1:-30}
|
||||
BASE_URL="http://localhost:9998"
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 直接通过数据库触发健康数据生成
|
||||
generate_via_service_call() {
|
||||
log_info "通过服务直接调用生成健康数据..."
|
||||
|
||||
# 创建临时SQL脚本来触发数据生成
|
||||
local temp_sql="/tmp/trigger_health_generation.sql"
|
||||
|
||||
cat > "$temp_sql" << EOF
|
||||
-- 触发健康数据生成的SQL脚本
|
||||
USE test;
|
||||
|
||||
-- 获取当前活跃用户列表(有健康数据的用户)
|
||||
SELECT
|
||||
'当前活跃用户统计' as info,
|
||||
COUNT(DISTINCT user_id) as active_users,
|
||||
COUNT(*) as total_records,
|
||||
DATE(MIN(create_time)) as earliest_data,
|
||||
DATE(MAX(create_time)) as latest_data
|
||||
FROM t_user_health_data
|
||||
WHERE create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY);
|
||||
|
||||
-- 显示需要处理的用户ID
|
||||
SELECT DISTINCT
|
||||
user_id,
|
||||
customer_id,
|
||||
COUNT(*) as health_records,
|
||||
DATE(MAX(create_time)) as latest_data
|
||||
FROM t_user_health_data
|
||||
WHERE create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
GROUP BY user_id, customer_id
|
||||
ORDER BY health_records DESC
|
||||
LIMIT 10;
|
||||
EOF
|
||||
|
||||
log_info "检查当前健康数据状态..."
|
||||
mysql -h127.0.0.1 -uroot -p123456 test < "$temp_sql" 2>/dev/null || {
|
||||
log_error "无法访问数据库"
|
||||
return 1
|
||||
}
|
||||
|
||||
rm -f "$temp_sql"
|
||||
}
|
||||
|
||||
# 通过HTTP API调用
|
||||
call_health_api() {
|
||||
local data_type="$1"
|
||||
local description="$2"
|
||||
|
||||
log_info "生成${description}..."
|
||||
|
||||
# 尝试多个可能的API端点
|
||||
local endpoints=(
|
||||
"/api/health/task/manual/${data_type}"
|
||||
"/api/health/task/execute/${data_type}"
|
||||
"/health/task/run/${data_type}"
|
||||
)
|
||||
|
||||
for endpoint in "${endpoints[@]}"; do
|
||||
local url="${BASE_URL}${endpoint}?days=${DAYS}"
|
||||
|
||||
log_info "尝试调用: $url"
|
||||
|
||||
local response=$(curl -s -w "\n%{http_code}" -X POST "$url" \
|
||||
-H "Content-Type: application/json" \
|
||||
--connect-timeout 10 \
|
||||
--max-time 30 2>/dev/null || echo -e "\n000")
|
||||
|
||||
local http_code=$(echo "$response" | tail -n1)
|
||||
local body=$(echo "$response" | head -n -1)
|
||||
|
||||
if [ "$http_code" = "200" ]; then
|
||||
if echo "$body" | grep -q -i "success\|完成\|执行"; then
|
||||
log_success "${description}生成成功"
|
||||
return 0
|
||||
else
|
||||
log_warning "${description}API调用成功但状态未确认: $body"
|
||||
return 0
|
||||
fi
|
||||
elif [ "$http_code" = "404" ]; then
|
||||
log_warning "API端点不存在: $endpoint"
|
||||
continue
|
||||
elif [ "$http_code" = "401" ]; then
|
||||
log_warning "需要认证: $endpoint"
|
||||
continue
|
||||
else
|
||||
log_warning "API调用失败 (HTTP $http_code): $endpoint"
|
||||
continue
|
||||
fi
|
||||
done
|
||||
|
||||
log_error "${description}生成失败: 所有API端点都无法访问"
|
||||
return 1
|
||||
}
|
||||
|
||||
# 检查和创建必要的数据
|
||||
ensure_data_structure() {
|
||||
log_info "确保数据结构完整..."
|
||||
|
||||
# 检查基础表是否存在
|
||||
local tables=("t_health_baseline" "t_health_score" "t_health_prediction" "t_health_recommendation" "t_health_profile")
|
||||
|
||||
for table in "${tables[@]}"; do
|
||||
local exists=$(mysql -h127.0.0.1 -uroot -p123456 -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='test' AND table_name='$table';" test 2>/dev/null | tail -n1)
|
||||
|
||||
if [ "$exists" = "1" ]; then
|
||||
log_success "表 $table 存在"
|
||||
else
|
||||
log_error "表 $table 不存在"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# 显示生成后的数据统计
|
||||
show_data_summary() {
|
||||
log_info "显示数据生成结果..."
|
||||
|
||||
mysql -h127.0.0.1 -uroot -p123456 -e "
|
||||
SELECT
|
||||
'=== 健康数据生成结果统计 ===' as summary;
|
||||
|
||||
SELECT
|
||||
'基线数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as unique_users,
|
||||
COUNT(DISTINCT feature_name) as unique_features,
|
||||
DATE(MAX(create_time)) as latest_record
|
||||
FROM t_health_baseline
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'评分数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as unique_users,
|
||||
COUNT(DISTINCT feature_name) as unique_features,
|
||||
DATE(MAX(create_time)) as latest_record
|
||||
FROM t_health_score
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'预测数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as unique_users,
|
||||
0 as unique_features,
|
||||
DATE(MAX(create_time)) as latest_record
|
||||
FROM t_health_prediction
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'建议数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as unique_users,
|
||||
0 as unique_features,
|
||||
DATE(MAX(create_time)) as latest_record
|
||||
FROM t_health_recommendation
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'档案数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as unique_users,
|
||||
0 as unique_features,
|
||||
DATE(MAX(create_time)) as latest_record
|
||||
FROM t_health_profile
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY);
|
||||
" test 2>/dev/null || log_warning "无法查询数据统计"
|
||||
}
|
||||
|
||||
main() {
|
||||
echo "========================================"
|
||||
echo "🏥 健康数据直接生成脚本"
|
||||
echo "========================================"
|
||||
echo "📅 处理天数: ${DAYS} 天"
|
||||
echo "🌐 服务地址: ${BASE_URL}"
|
||||
echo ""
|
||||
|
||||
# 1. 确保数据结构
|
||||
ensure_data_structure
|
||||
|
||||
echo ""
|
||||
|
||||
# 2. 检查当前数据状态
|
||||
generate_via_service_call
|
||||
|
||||
echo ""
|
||||
echo "开始生成健康数据..."
|
||||
echo "========================================"
|
||||
|
||||
# 3. 逐个生成健康数据类型
|
||||
local success_count=0
|
||||
local data_types=("baseline:健康基线数据" "score:健康评分数据" "prediction:健康预测数据" "recommendation:健康建议数据" "profile:健康档案数据")
|
||||
|
||||
for item in "${data_types[@]}"; do
|
||||
IFS=':' read -ra ADDR <<< "$item"
|
||||
local type="${ADDR[0]}"
|
||||
local desc="${ADDR[1]}"
|
||||
|
||||
if call_health_api "$type" "$desc"; then
|
||||
((success_count++))
|
||||
fi
|
||||
|
||||
sleep 3 # 给系统时间处理
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "========================================"
|
||||
echo "📊 生成结果"
|
||||
echo "========================================"
|
||||
echo "✅ 成功: ${success_count}/5"
|
||||
echo "❌ 失败: $((5 - success_count))/5"
|
||||
|
||||
if [ $success_count -gt 0 ]; then
|
||||
log_success "部分或全部健康数据生成完成"
|
||||
else
|
||||
log_warning "健康数据生成可能失败,请检查服务状态"
|
||||
fi
|
||||
|
||||
# 4. 显示生成结果
|
||||
echo ""
|
||||
show_data_summary
|
||||
|
||||
echo ""
|
||||
echo "========================================"
|
||||
echo "💡 说明"
|
||||
echo "========================================"
|
||||
echo "- 此脚本尝试直接调用健康数据生成API"
|
||||
echo "- 如果API调用失败,数据生成可能通过定时任务自动进行"
|
||||
echo "- 可以通过管理界面或数据库直接查看生成的数据"
|
||||
echo "- 服务文档: ${BASE_URL}/doc.html"
|
||||
echo "========================================"
|
||||
}
|
||||
|
||||
# 检查参数
|
||||
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
||||
echo "用法: $0 [天数]"
|
||||
echo "参数:"
|
||||
echo " 天数 生成数据的天数范围 (默认: 30)"
|
||||
echo ""
|
||||
echo "示例:"
|
||||
echo " $0 30 # 生成过去30天的健康数据"
|
||||
echo " $0 7 # 生成过去7天的健康数据"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
main "$@"
|
||||
Executable
+391
@@ -0,0 +1,391 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
健康数据生成脚本 (Python版本)
|
||||
功能:先登录获取token,然后生成过去一个月的baseline, score, prediction, recommendation, profile
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional, Dict, Any
|
||||
import argparse
|
||||
|
||||
# 配置参数
|
||||
class Config:
|
||||
BASE_URL = "http://localhost:9998"
|
||||
USERNAME = "admin"
|
||||
PASSWORD = "admin123"
|
||||
DAYS = 30
|
||||
TIMEOUT = 30
|
||||
|
||||
class Colors:
|
||||
"""终端颜色定义"""
|
||||
RED = '\033[0;31m'
|
||||
GREEN = '\033[0;32m'
|
||||
YELLOW = '\033[1;33m'
|
||||
BLUE = '\033[0;34m'
|
||||
PURPLE = '\033[0;35m'
|
||||
CYAN = '\033[0;36m'
|
||||
WHITE = '\033[1;37m'
|
||||
NC = '\033[0m' # No Color
|
||||
|
||||
class Logger:
|
||||
"""日志工具类"""
|
||||
|
||||
@staticmethod
|
||||
def info(message: str):
|
||||
print(f"{Colors.BLUE}[INFO]{Colors.NC} {message}")
|
||||
|
||||
@staticmethod
|
||||
def success(message: str):
|
||||
print(f"{Colors.GREEN}[SUCCESS]{Colors.NC} {message}")
|
||||
|
||||
@staticmethod
|
||||
def warning(message: str):
|
||||
print(f"{Colors.YELLOW}[WARNING]{Colors.NC} {message}")
|
||||
|
||||
@staticmethod
|
||||
def error(message: str):
|
||||
print(f"{Colors.RED}[ERROR]{Colors.NC} {message}")
|
||||
|
||||
@staticmethod
|
||||
def debug(message: str):
|
||||
print(f"{Colors.PURPLE}[DEBUG]{Colors.NC} {message}")
|
||||
|
||||
class HealthDataGenerator:
|
||||
"""健康数据生成器"""
|
||||
|
||||
def __init__(self, base_url: str = Config.BASE_URL, username: str = Config.USERNAME,
|
||||
password: str = Config.PASSWORD, days: int = Config.DAYS):
|
||||
self.base_url = base_url.rstrip('/')
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.days = days
|
||||
self.token: Optional[str] = None
|
||||
self.session = requests.Session()
|
||||
self.session.timeout = Config.TIMEOUT
|
||||
|
||||
# 健康数据类型定义
|
||||
self.data_types = [
|
||||
("baseline", "健康基线数据"),
|
||||
("score", "健康评分数据"),
|
||||
("prediction", "健康预测数据"),
|
||||
("recommendation", "健康建议数据"),
|
||||
("profile", "健康档案数据")
|
||||
]
|
||||
|
||||
def check_service(self) -> bool:
|
||||
"""检查服务是否可用"""
|
||||
Logger.info("检查ljwx-boot服务状态...")
|
||||
|
||||
try:
|
||||
response = self.session.get(f"{self.base_url}/actuator/health")
|
||||
if response.status_code == 200:
|
||||
Logger.success("服务正常运行")
|
||||
return True
|
||||
else:
|
||||
Logger.error(f"服务返回状态码: {response.status_code}")
|
||||
return False
|
||||
except Exception as e:
|
||||
Logger.error(f"服务不可访问: {str(e)}")
|
||||
Logger.error("请确保ljwx-boot已启动并运行在正确端口")
|
||||
return False
|
||||
|
||||
def login(self) -> bool:
|
||||
"""登录获取token"""
|
||||
Logger.info("开始登录获取token...")
|
||||
|
||||
login_data = {
|
||||
"username": self.username,
|
||||
"password": self.password
|
||||
}
|
||||
|
||||
# 尝试多个可能的登录接口
|
||||
login_endpoints = [
|
||||
"/auth/login",
|
||||
"/api/auth/login",
|
||||
"/api/login",
|
||||
"/login"
|
||||
]
|
||||
|
||||
for endpoint in login_endpoints:
|
||||
try:
|
||||
Logger.debug(f"尝试登录接口: {endpoint}")
|
||||
response = self.session.post(
|
||||
f"{self.base_url}{endpoint}",
|
||||
json=login_data,
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
|
||||
# 尝试多种token字段名
|
||||
token_fields = ['token', 'access_token', 'accessToken', 'jwt']
|
||||
for field in token_fields:
|
||||
if field in data:
|
||||
self.token = data[field]
|
||||
break
|
||||
elif 'data' in data and isinstance(data['data'], dict) and field in data['data']:
|
||||
self.token = data['data'][field]
|
||||
break
|
||||
|
||||
if self.token:
|
||||
Logger.success("登录成功,token已获取")
|
||||
self.session.headers.update({
|
||||
"Authorization": f"Bearer {self.token}"
|
||||
})
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
Logger.debug(f"登录接口 {endpoint} 失败: {str(e)}")
|
||||
continue
|
||||
|
||||
Logger.warning("无法获取token,将尝试直接访问API(可能不需要登录)")
|
||||
return True # 继续执行,可能API不需要认证
|
||||
|
||||
def api_request(self, method: str, endpoint: str, data: Optional[Dict] = None) -> Dict[str, Any]:
|
||||
"""执行API请求"""
|
||||
url = f"{self.base_url}{endpoint}"
|
||||
|
||||
try:
|
||||
if method.upper() == "POST":
|
||||
response = self.session.post(url, json=data)
|
||||
else:
|
||||
response = self.session.get(url)
|
||||
|
||||
# 尝试解析JSON响应
|
||||
try:
|
||||
return {
|
||||
"status_code": response.status_code,
|
||||
"data": response.json(),
|
||||
"text": response.text
|
||||
}
|
||||
except:
|
||||
return {
|
||||
"status_code": response.status_code,
|
||||
"data": {},
|
||||
"text": response.text
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"status_code": 0,
|
||||
"data": {},
|
||||
"text": f"请求异常: {str(e)}"
|
||||
}
|
||||
|
||||
def generate_health_data(self, data_type: str, description: str) -> bool:
|
||||
"""生成健康数据"""
|
||||
Logger.info(f"开始生成{description}...")
|
||||
|
||||
# 尝试多个可能的API接口
|
||||
endpoints = [
|
||||
f"/api/health/task/execute/{data_type}?days={self.days}",
|
||||
f"/api/health/task/{data_type}?days={self.days}",
|
||||
f"/health/task/execute/{data_type}?days={self.days}",
|
||||
f"/api/health/generate/{data_type}?days={self.days}"
|
||||
]
|
||||
|
||||
for endpoint in endpoints:
|
||||
Logger.debug(f"尝试API接口: {endpoint}")
|
||||
response = self.api_request("POST", endpoint)
|
||||
|
||||
# 检查响应
|
||||
if response["status_code"] == 200:
|
||||
data = response["data"]
|
||||
|
||||
# 检查业务状态码
|
||||
code = data.get("code", data.get("status", 200))
|
||||
message = data.get("message", data.get("msg", "success"))
|
||||
|
||||
if code in [200, 0] or "success" in str(message).lower() or "完成" in str(message) or "执行" in str(message):
|
||||
Logger.success(f"{description}生成完成")
|
||||
return True
|
||||
elif code == 401:
|
||||
Logger.warning(f"{description}需要认证,尝试重新登录...")
|
||||
if self.login():
|
||||
continue # 重试当前接口
|
||||
else:
|
||||
Logger.error(f"{description}生成失败: {message}")
|
||||
Logger.debug(f"完整响应: {response['text']}")
|
||||
elif response["status_code"] == 401:
|
||||
Logger.warning("认证失败,尝试重新登录...")
|
||||
if self.login():
|
||||
continue # 重试当前接口
|
||||
elif response["status_code"] == 404:
|
||||
Logger.debug(f"接口不存在: {endpoint}")
|
||||
continue # 尝试下一个接口
|
||||
else:
|
||||
Logger.debug(f"接口返回: {response['status_code']} - {response['text']}")
|
||||
continue # 尝试下一个接口
|
||||
|
||||
Logger.error(f"{description}生成失败: 所有API接口都无法访问")
|
||||
return False
|
||||
|
||||
def verify_data(self):
|
||||
"""验证生成的数据(通过数据库查询)"""
|
||||
Logger.info("验证生成的数据...")
|
||||
|
||||
try:
|
||||
import subprocess
|
||||
|
||||
sql_query = f"""
|
||||
SELECT
|
||||
'基线数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_baseline
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL {self.days} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'评分数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_score
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL {self.days} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'预测数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_prediction
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL {self.days} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'建议数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_recommendation
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL {self.days} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'档案数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_profile
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL {self.days} DAY);
|
||||
"""
|
||||
|
||||
result = subprocess.run([
|
||||
"mysql", "-h127.0.0.1", "-uroot", "-p123456",
|
||||
"-e", sql_query, "test"
|
||||
], capture_output=True, text=True, stderr=subprocess.DEVNULL)
|
||||
|
||||
if result.returncode == 0:
|
||||
print("\n" + "="*50)
|
||||
print("📊 数据验证结果")
|
||||
print("="*50)
|
||||
print(result.stdout)
|
||||
else:
|
||||
Logger.warning("无法验证数据库数据,请手动检查")
|
||||
|
||||
except Exception as e:
|
||||
Logger.warning(f"数据验证失败: {str(e)}")
|
||||
|
||||
def run(self) -> bool:
|
||||
"""运行主流程"""
|
||||
print("========================================")
|
||||
print("🏥 健康数据生成脚本 (Python版本)")
|
||||
print("========================================")
|
||||
print(f"📅 生成时间范围: 过去 {self.days} 天")
|
||||
print(f"🌐 服务地址: {self.base_url}")
|
||||
print(f"👤 用户名: {self.username}")
|
||||
print("")
|
||||
|
||||
# 1. 检查服务状态
|
||||
if not self.check_service():
|
||||
return False
|
||||
|
||||
# 2. 登录获取token
|
||||
if not self.login():
|
||||
Logger.error("登录失败,无法继续")
|
||||
return False
|
||||
|
||||
print("")
|
||||
print("开始生成健康数据...")
|
||||
print("========================================")
|
||||
|
||||
# 3. 按顺序生成各类健康数据
|
||||
success_count = 0
|
||||
total_count = len(self.data_types)
|
||||
|
||||
for data_type, description in self.data_types:
|
||||
if self.generate_health_data(data_type, description):
|
||||
success_count += 1
|
||||
time.sleep(2) # 避免请求过于频繁
|
||||
|
||||
print("")
|
||||
print("========================================")
|
||||
print("📊 生成结果统计")
|
||||
print("========================================")
|
||||
print(f"✅ 成功: {success_count}/{total_count}")
|
||||
print(f"❌ 失败: {total_count - success_count}/{total_count}")
|
||||
|
||||
if success_count == total_count:
|
||||
Logger.success("所有健康数据生成完成!")
|
||||
else:
|
||||
Logger.warning("部分健康数据生成失败,请检查日志")
|
||||
|
||||
# 4. 验证生成的数据
|
||||
print("")
|
||||
self.verify_data()
|
||||
|
||||
# 5. 输出访问信息
|
||||
print("")
|
||||
print("========================================")
|
||||
print("🔗 访问地址")
|
||||
print("========================================")
|
||||
print(f"📖 API文档: {self.base_url}/doc.html")
|
||||
print(f"📊 健康监控: {self.base_url}/actuator/health")
|
||||
print(f"🎯 任务管理: {self.base_url}/api/health/task/")
|
||||
print("")
|
||||
print("💡 提示:")
|
||||
print(" - 可以通过管理界面查看生成的数据")
|
||||
print(" - 数据生成可能需要一些时间处理")
|
||||
print(" - 如有失败,可以重新运行此脚本")
|
||||
print("========================================")
|
||||
|
||||
return success_count > 0
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
parser = argparse.ArgumentParser(description='健康数据生成脚本')
|
||||
parser.add_argument('--url', default=Config.BASE_URL, help='API基础URL')
|
||||
parser.add_argument('--username', default=Config.USERNAME, help='用户名')
|
||||
parser.add_argument('--password', default=Config.PASSWORD, help='密码')
|
||||
parser.add_argument('--days', type=int, default=Config.DAYS, help='生成数据的天数')
|
||||
parser.add_argument('--debug', action='store_true', help='启用调试模式')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
generator = HealthDataGenerator(
|
||||
base_url=args.url,
|
||||
username=args.username,
|
||||
password=args.password,
|
||||
days=args.days
|
||||
)
|
||||
|
||||
try:
|
||||
success = generator.run()
|
||||
sys.exit(0 if success else 1)
|
||||
except KeyboardInterrupt:
|
||||
Logger.warning("用户中断执行")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
Logger.error(f"执行异常: {str(e)}")
|
||||
if args.debug:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+284
@@ -0,0 +1,284 @@
|
||||
#!/bin/bash
|
||||
# 健康数据生成脚本
|
||||
# 先登录获取token,然后生成过去一个月的baseline, score, prediction, recommendation, profile
|
||||
|
||||
set -e
|
||||
|
||||
# 配置参数
|
||||
BASE_URL="http://localhost:9998"
|
||||
USERNAME="admin"
|
||||
PASSWORD="admin123"
|
||||
DAYS=30
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 日志函数
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# 检查服务是否可用
|
||||
check_service() {
|
||||
log_info "检查ljwx-boot服务状态..."
|
||||
|
||||
if curl -s "${BASE_URL}/actuator/health" > /dev/null; then
|
||||
log_success "服务正常运行"
|
||||
else
|
||||
log_error "服务不可访问,请确保ljwx-boot已启动"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 登录获取token
|
||||
login() {
|
||||
log_info "开始登录获取token..."
|
||||
|
||||
local login_data=$(cat <<EOF
|
||||
{
|
||||
"username": "${USERNAME}",
|
||||
"password": "${PASSWORD}"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
local response=$(curl -s -X POST \
|
||||
"${BASE_URL}/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$login_data")
|
||||
|
||||
# 提取token (根据实际API响应格式调整)
|
||||
local token=$(echo "$response" | jq -r '.data.token // .token // .access_token // empty')
|
||||
|
||||
if [ -z "$token" ] || [ "$token" = "null" ]; then
|
||||
log_warning "使用标准登录方式失败,尝试其他登录接口..."
|
||||
|
||||
# 尝试其他可能的登录接口
|
||||
response=$(curl -s -X POST \
|
||||
"${BASE_URL}/api/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$login_data")
|
||||
|
||||
token=$(echo "$response" | jq -r '.data.token // .token // .access_token // empty')
|
||||
|
||||
if [ -z "$token" ] || [ "$token" = "null" ]; then
|
||||
# 尝试直接访问健康任务接口(可能不需要登录)
|
||||
log_warning "无法获取token,尝试直接访问API..."
|
||||
TOKEN=""
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
TOKEN="$token"
|
||||
log_success "登录成功,token已获取"
|
||||
return 0
|
||||
}
|
||||
|
||||
# 执行API请求
|
||||
api_request() {
|
||||
local method="$1"
|
||||
local endpoint="$2"
|
||||
local data="$3"
|
||||
|
||||
local headers="-H 'Content-Type: application/json'"
|
||||
if [ -n "$TOKEN" ]; then
|
||||
headers="$headers -H 'Authorization: Bearer $TOKEN'"
|
||||
fi
|
||||
|
||||
local url="${BASE_URL}${endpoint}"
|
||||
|
||||
if [ "$method" = "POST" ]; then
|
||||
if [ -n "$data" ]; then
|
||||
eval curl -s -X POST "$url" $headers -d "'$data'"
|
||||
else
|
||||
eval curl -s -X POST "$url" $headers
|
||||
fi
|
||||
else
|
||||
eval curl -s -X GET "$url" $headers
|
||||
fi
|
||||
}
|
||||
|
||||
# 生成健康数据
|
||||
generate_health_data() {
|
||||
local data_type="$1"
|
||||
local description="$2"
|
||||
|
||||
log_info "开始生成${description}..."
|
||||
|
||||
local endpoint="/api/health/task/execute/${data_type}?days=${DAYS}"
|
||||
local response=$(api_request "POST" "$endpoint")
|
||||
|
||||
# 检查响应
|
||||
local code=$(echo "$response" | jq -r '.code // .status // 200')
|
||||
local message=$(echo "$response" | jq -r '.message // .msg // "success"')
|
||||
|
||||
if [ "$code" = "200" ] || [ "$code" = "0" ] || echo "$response" | grep -q "success\|完成\|执行"; then
|
||||
log_success "${description}生成完成"
|
||||
return 0
|
||||
else
|
||||
log_error "${description}生成失败: $message"
|
||||
echo "Response: $response"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 检查任务状态
|
||||
check_task_status() {
|
||||
log_info "检查任务执行状态..."
|
||||
|
||||
local response=$(api_request "GET" "/api/health/task/status")
|
||||
echo "任务状态: $response"
|
||||
}
|
||||
|
||||
# 验证生成的数据
|
||||
verify_data() {
|
||||
log_info "验证生成的数据..."
|
||||
|
||||
# 检查数据库中的记录数量
|
||||
mysql -h127.0.0.1 -uroot -p123456 -e "
|
||||
SELECT
|
||||
'基线数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_baseline
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'评分数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_score
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'预测数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_prediction
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'建议数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_recommendation
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY)
|
||||
UNION ALL
|
||||
SELECT
|
||||
'档案数据' as data_type,
|
||||
COUNT(*) as total_records,
|
||||
COUNT(DISTINCT user_id) as users,
|
||||
DATE(MAX(create_time)) as latest_date
|
||||
FROM t_health_profile
|
||||
WHERE is_deleted = 0 AND create_time >= DATE_SUB(NOW(), INTERVAL ${DAYS} DAY);
|
||||
" test 2>/dev/null || log_warning "无法验证数据库数据"
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
echo "========================================"
|
||||
echo "🏥 健康数据生成脚本"
|
||||
echo "========================================"
|
||||
echo "📅 生成时间范围: 过去 ${DAYS} 天"
|
||||
echo "🌐 服务地址: ${BASE_URL}"
|
||||
echo "👤 用户名: ${USERNAME}"
|
||||
echo ""
|
||||
|
||||
# 1. 检查服务状态
|
||||
check_service
|
||||
|
||||
# 2. 登录获取token
|
||||
login
|
||||
|
||||
echo ""
|
||||
echo "开始生成健康数据..."
|
||||
echo "========================================"
|
||||
|
||||
# 3. 按顺序生成各类健康数据
|
||||
local success_count=0
|
||||
local total_count=5
|
||||
|
||||
# 3.1 生成基线数据
|
||||
if generate_health_data "baseline" "健康基线数据"; then
|
||||
((success_count++))
|
||||
fi
|
||||
sleep 2
|
||||
|
||||
# 3.2 生成评分数据
|
||||
if generate_health_data "score" "健康评分数据"; then
|
||||
((success_count++))
|
||||
fi
|
||||
sleep 2
|
||||
|
||||
# 3.3 生成预测数据
|
||||
if generate_health_data "prediction" "健康预测数据"; then
|
||||
((success_count++))
|
||||
fi
|
||||
sleep 2
|
||||
|
||||
# 3.4 生成建议数据
|
||||
if generate_health_data "recommendation" "健康建议数据"; then
|
||||
((success_count++))
|
||||
fi
|
||||
sleep 2
|
||||
|
||||
# 3.5 生成档案数据
|
||||
if generate_health_data "profile" "健康档案数据"; then
|
||||
((success_count++))
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "========================================"
|
||||
echo "📊 生成结果统计"
|
||||
echo "========================================"
|
||||
echo "✅ 成功: ${success_count}/${total_count}"
|
||||
echo "❌ 失败: $((total_count - success_count))/${total_count}"
|
||||
|
||||
if [ $success_count -eq $total_count ]; then
|
||||
log_success "所有健康数据生成完成!"
|
||||
else
|
||||
log_warning "部分健康数据生成失败,请检查日志"
|
||||
fi
|
||||
|
||||
# 4. 验证生成的数据
|
||||
echo ""
|
||||
verify_data
|
||||
|
||||
# 5. 输出访问信息
|
||||
echo ""
|
||||
echo "========================================"
|
||||
echo "🔗 访问地址"
|
||||
echo "========================================"
|
||||
echo "📖 API文档: ${BASE_URL}/doc.html"
|
||||
echo "📊 健康监控: ${BASE_URL}/actuator/health"
|
||||
echo "🎯 任务管理: ${BASE_URL}/api/health/task/"
|
||||
echo ""
|
||||
echo "💡 提示:"
|
||||
echo " - 可以通过管理界面查看生成的数据"
|
||||
echo " - 数据生成可能需要一些时间处理"
|
||||
echo " - 如有失败,可以重新运行此脚本"
|
||||
echo "========================================"
|
||||
}
|
||||
|
||||
# 运行主函数
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user