2026-04-04 18:17:10 +08:00
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from app.core.database import get_db
|
|
|
|
|
from app.core.deps import get_current_user, require_roles
|
|
|
|
|
from app.models.user import User
|
|
|
|
|
from app.models.setting import SystemSetting
|
|
|
|
|
from app.services.audit import log_audit
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/settings", tags=["系统设置"])
|
|
|
|
|
|
|
|
|
|
# Default settings — used when keys are missing from DB
|
|
|
|
|
DEFAULTS: dict[str, str] = {
|
|
|
|
|
"platform_name": "天普零碳园区智慧能源管理平台",
|
|
|
|
|
"data_retention_days": "365",
|
|
|
|
|
"alarm_auto_resolve_minutes": "30",
|
|
|
|
|
"simulator_interval_seconds": "15",
|
|
|
|
|
"notification_email_enabled": "false",
|
|
|
|
|
"notification_email_smtp": "",
|
|
|
|
|
"report_auto_schedule_enabled": "false",
|
|
|
|
|
"timezone": "Asia/Shanghai",
|
feat: v2.0 — maintenance module, AI analysis, station power fix
- Add full 检修维护中心 (6.4): 3-type work orders (消缺/巡检/抄表),
asset management, warehouse, work plans, billing settlement
- Add AI智能分析 tab with LLM-powered diagnostics (StepFun + ZhipuAI)
- Add AI模型配置 settings page (provider, temperature, prompts)
- Fix station power accuracy: use API station total (station_power)
instead of inverter-level computation — eliminates timing gaps
- Add 7 new DB models, 4 new API routers, 5 new frontend pages
- Migrations: 009 (maintenance expansion) + 010 (AI analysis)
- Version bump: 1.6.1 → 2.0.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:16:03 +08:00
|
|
|
# AI Model Settings
|
|
|
|
|
"ai_enabled": "false",
|
|
|
|
|
"ai_provider": "stepfun", # stepfun, zhipu
|
|
|
|
|
"ai_api_base_url": "https://api.stepfun.com/step_plan/v1",
|
|
|
|
|
"ai_api_key": "1UVGFlMG9zaGrRvRATpBNdjKotLio6x9t6lKRKdxwYD3mEkLU2Itb30yb1rvzWRGs",
|
|
|
|
|
"ai_model_name": "step-2-16k",
|
|
|
|
|
"ai_temperature": "0.7",
|
|
|
|
|
"ai_max_tokens": "2000",
|
|
|
|
|
"ai_context_length": "8000",
|
|
|
|
|
"ai_fallback_enabled": "true",
|
|
|
|
|
"ai_fallback_provider": "zhipu",
|
|
|
|
|
"ai_fallback_api_base_url": "https://open.bigmodel.cn/api/coding/paas/v4",
|
|
|
|
|
"ai_fallback_api_key": "0b5fe625dfd64836bfd42cc9608aed42.wnQngOvi7EkAWjyn",
|
|
|
|
|
"ai_fallback_model_name": "codegeex-4",
|
|
|
|
|
"ai_system_prompt": "你是一个专业的光伏电站智能运维助手。你的任务是分析光伏电站的设备运行数据、告警信息和历史趋势,提供专业的诊断分析和运维建议。请用中文回答,结构清晰,重点突出。",
|
|
|
|
|
"ai_diagnostic_prompt": "请分析以下光伏设备的运行数据,给出诊断报告:\n\n设备信息:{device_info}\n运行数据:{metrics}\n告警记录:{alarms}\n\n请按以下结构输出:\n## 运行概况\n## 问题诊断\n## 建议措施\n## 风险预警",
|
|
|
|
|
"ai_insight_prompt": "请根据以下电站运行数据,生成运营洞察报告:\n\n电站概况:{station_info}\n关键指标:{kpis}\n近期告警:{recent_alarms}\n\n请给出3-5条关键洞察和建议。",
|
2026-04-04 18:17:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SettingsUpdate(BaseModel):
|
|
|
|
|
platform_name: str | None = None
|
|
|
|
|
data_retention_days: int | None = None
|
|
|
|
|
alarm_auto_resolve_minutes: int | None = None
|
|
|
|
|
simulator_interval_seconds: int | None = None
|
|
|
|
|
notification_email_enabled: bool | None = None
|
|
|
|
|
notification_email_smtp: str | None = None
|
|
|
|
|
report_auto_schedule_enabled: bool | None = None
|
|
|
|
|
timezone: str | None = None
|
feat: v2.0 — maintenance module, AI analysis, station power fix
- Add full 检修维护中心 (6.4): 3-type work orders (消缺/巡检/抄表),
asset management, warehouse, work plans, billing settlement
- Add AI智能分析 tab with LLM-powered diagnostics (StepFun + ZhipuAI)
- Add AI模型配置 settings page (provider, temperature, prompts)
- Fix station power accuracy: use API station total (station_power)
instead of inverter-level computation — eliminates timing gaps
- Add 7 new DB models, 4 new API routers, 5 new frontend pages
- Migrations: 009 (maintenance expansion) + 010 (AI analysis)
- Version bump: 1.6.1 → 2.0.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:16:03 +08:00
|
|
|
ai_enabled: bool | None = None
|
|
|
|
|
ai_provider: str | None = None
|
|
|
|
|
ai_api_base_url: str | None = None
|
|
|
|
|
ai_api_key: str | None = None
|
|
|
|
|
ai_model_name: str | None = None
|
|
|
|
|
ai_temperature: float | None = None
|
|
|
|
|
ai_max_tokens: int | None = None
|
|
|
|
|
ai_context_length: int | None = None
|
|
|
|
|
ai_fallback_enabled: bool | None = None
|
|
|
|
|
ai_fallback_provider: str | None = None
|
|
|
|
|
ai_fallback_api_base_url: str | None = None
|
|
|
|
|
ai_fallback_api_key: str | None = None
|
|
|
|
|
ai_fallback_model_name: str | None = None
|
|
|
|
|
ai_system_prompt: str | None = None
|
|
|
|
|
ai_diagnostic_prompt: str | None = None
|
|
|
|
|
ai_insight_prompt: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _mask_key(key: str) -> str:
|
|
|
|
|
if not key or len(key) < 8:
|
|
|
|
|
return "****"
|
|
|
|
|
return "*" * (len(key) - 4) + key[-4:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _get_raw_settings(db: AsyncSession) -> dict[str, str]:
|
|
|
|
|
"""Return merged settings dict WITHOUT masking (for internal use)."""
|
|
|
|
|
result = await db.execute(select(SystemSetting))
|
|
|
|
|
db_settings = {s.key: s.value for s in result.scalars().all()}
|
|
|
|
|
return {**DEFAULTS, **db_settings}
|
2026-04-04 18:17:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("")
|
|
|
|
|
async def get_settings(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
user: User = Depends(get_current_user),
|
|
|
|
|
):
|
|
|
|
|
"""Return all platform settings as a flat dict."""
|
feat: v2.0 — maintenance module, AI analysis, station power fix
- Add full 检修维护中心 (6.4): 3-type work orders (消缺/巡检/抄表),
asset management, warehouse, work plans, billing settlement
- Add AI智能分析 tab with LLM-powered diagnostics (StepFun + ZhipuAI)
- Add AI模型配置 settings page (provider, temperature, prompts)
- Fix station power accuracy: use API station total (station_power)
instead of inverter-level computation — eliminates timing gaps
- Add 7 new DB models, 4 new API routers, 5 new frontend pages
- Migrations: 009 (maintenance expansion) + 010 (AI analysis)
- Version bump: 1.6.1 → 2.0.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:16:03 +08:00
|
|
|
merged = await _get_raw_settings(db)
|
2026-04-04 18:17:10 +08:00
|
|
|
# Cast types for frontend
|
|
|
|
|
return {
|
|
|
|
|
"platform_name": merged["platform_name"],
|
|
|
|
|
"data_retention_days": int(merged["data_retention_days"]),
|
|
|
|
|
"alarm_auto_resolve_minutes": int(merged["alarm_auto_resolve_minutes"]),
|
|
|
|
|
"simulator_interval_seconds": int(merged["simulator_interval_seconds"]),
|
|
|
|
|
"notification_email_enabled": merged["notification_email_enabled"] == "true",
|
|
|
|
|
"notification_email_smtp": merged["notification_email_smtp"],
|
|
|
|
|
"report_auto_schedule_enabled": merged["report_auto_schedule_enabled"] == "true",
|
|
|
|
|
"timezone": merged["timezone"],
|
feat: v2.0 — maintenance module, AI analysis, station power fix
- Add full 检修维护中心 (6.4): 3-type work orders (消缺/巡检/抄表),
asset management, warehouse, work plans, billing settlement
- Add AI智能分析 tab with LLM-powered diagnostics (StepFun + ZhipuAI)
- Add AI模型配置 settings page (provider, temperature, prompts)
- Fix station power accuracy: use API station total (station_power)
instead of inverter-level computation — eliminates timing gaps
- Add 7 new DB models, 4 new API routers, 5 new frontend pages
- Migrations: 009 (maintenance expansion) + 010 (AI analysis)
- Version bump: 1.6.1 → 2.0.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:16:03 +08:00
|
|
|
# AI settings
|
|
|
|
|
"ai_enabled": merged["ai_enabled"] == "true",
|
|
|
|
|
"ai_provider": merged["ai_provider"],
|
|
|
|
|
"ai_api_base_url": merged["ai_api_base_url"],
|
|
|
|
|
"ai_api_key": _mask_key(merged["ai_api_key"]),
|
|
|
|
|
"ai_model_name": merged["ai_model_name"],
|
|
|
|
|
"ai_temperature": float(merged["ai_temperature"]),
|
|
|
|
|
"ai_max_tokens": int(merged["ai_max_tokens"]),
|
|
|
|
|
"ai_context_length": int(merged["ai_context_length"]),
|
|
|
|
|
"ai_fallback_enabled": merged["ai_fallback_enabled"] == "true",
|
|
|
|
|
"ai_fallback_provider": merged["ai_fallback_provider"],
|
|
|
|
|
"ai_fallback_api_base_url": merged["ai_fallback_api_base_url"],
|
|
|
|
|
"ai_fallback_api_key": _mask_key(merged["ai_fallback_api_key"]),
|
|
|
|
|
"ai_fallback_model_name": merged["ai_fallback_model_name"],
|
|
|
|
|
"ai_system_prompt": merged["ai_system_prompt"],
|
|
|
|
|
"ai_diagnostic_prompt": merged["ai_diagnostic_prompt"],
|
|
|
|
|
"ai_insight_prompt": merged["ai_insight_prompt"],
|
2026-04-04 18:17:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("")
|
|
|
|
|
async def update_settings(
|
|
|
|
|
data: SettingsUpdate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
user: User = Depends(require_roles("admin")),
|
|
|
|
|
):
|
|
|
|
|
"""Update platform settings (admin only)."""
|
|
|
|
|
updates = data.model_dump(exclude_unset=True)
|
|
|
|
|
changed_keys = []
|
|
|
|
|
for key, value in updates.items():
|
|
|
|
|
str_value = str(value).lower() if isinstance(value, bool) else str(value)
|
|
|
|
|
result = await db.execute(select(SystemSetting).where(SystemSetting.key == key))
|
|
|
|
|
setting = result.scalar_one_or_none()
|
|
|
|
|
if setting:
|
|
|
|
|
setting.value = str_value
|
|
|
|
|
else:
|
|
|
|
|
db.add(SystemSetting(key=key, value=str_value))
|
|
|
|
|
changed_keys.append(key)
|
|
|
|
|
|
|
|
|
|
await log_audit(
|
|
|
|
|
db, user.id, "update", "system",
|
|
|
|
|
detail=f"更新系统设置: {', '.join(changed_keys)}",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {"message": "设置已更新"}
|
feat: v2.0 — maintenance module, AI analysis, station power fix
- Add full 检修维护中心 (6.4): 3-type work orders (消缺/巡检/抄表),
asset management, warehouse, work plans, billing settlement
- Add AI智能分析 tab with LLM-powered diagnostics (StepFun + ZhipuAI)
- Add AI模型配置 settings page (provider, temperature, prompts)
- Fix station power accuracy: use API station total (station_power)
instead of inverter-level computation — eliminates timing gaps
- Add 7 new DB models, 4 new API routers, 5 new frontend pages
- Migrations: 009 (maintenance expansion) + 010 (AI analysis)
- Version bump: 1.6.1 → 2.0.0
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:16:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/test-ai")
|
|
|
|
|
async def test_ai_connection(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
user: User = Depends(require_roles("admin")),
|
|
|
|
|
):
|
|
|
|
|
"""Test AI model connection."""
|
|
|
|
|
from app.services.llm_service import test_connection
|
|
|
|
|
settings = await _get_raw_settings(db)
|
|
|
|
|
result = await test_connection(settings)
|
|
|
|
|
return result
|