Files
deerflow-code/offline-backend-20260512/backend/app/gateway/goview_builtin_templates.py
T

116 lines
3.7 KiB
Python

"""Versioned builtin GoView templates.
Missing ids are inserted. Builtin canvas JSON is refreshed in place so an
administrator rename of the template name, description, or cover is kept.
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
from app.gateway.goview_demo_projects import COVER_URL
_SEED_DIR = Path(__file__).with_name("goview_template_seeds")
_SEEDS: tuple[dict[str, Any], ...] = (
{
"id": "builtin-dark-cockpit",
"name": "深色通用数据驾驶舱",
"description": "深色背景的通用指标驾驶舱,适合总览类大屏。",
"sort_order": 10,
"file": "dark-cockpit.json",
},
{
"id": "builtin-map-overview",
"name": "地图基础概览大屏",
"description": "带离线瓦片地图背景的概览大屏。",
"sort_order": 20,
"file": "map-overview.json",
},
{
"id": "builtin-metrics-trend",
"name": "指标总览与趋势分析大屏",
"description": "指标卡片与趋势分析的总览布局。",
"sort_order": 30,
"file": "metrics-trend.json",
},
{
"id": "builtin-timeline-progress",
"name": "时间轴与进度概览大屏",
"description": "时间轴和进度概览布局。",
"sort_order": 40,
"file": "timeline-progress.json",
},
{
"id": "builtin-ops-watch",
"name": "运行监测总览",
"description": "节点、告警与响应时长的监测大屏。",
"sort_order": 50,
"file": "ops-watch.json",
},
{
"id": "builtin-command-brief",
"name": "指挥研判简报",
"description": "今日结论、关注方向与待办计数。",
"sort_order": 60,
"file": "command-brief.json",
},
{
"id": "builtin-resource-board",
"name": "资源态势看板",
"description": "人员、装备、仓储与在途资源。",
"sort_order": 70,
"file": "resource-board.json",
},
{
"id": "builtin-compare-analysis",
"name": "对比分析大屏",
"description": "本期与上期的分类对照和差值。",
"sort_order": 80,
"file": "compare-board.json",
},
)
def load_builtin_templates() -> list[dict[str, Any]]:
items: list[dict[str, Any]] = []
for seed in _SEEDS:
content = (_SEED_DIR / seed["file"]).read_text(encoding="utf-8")
json.loads(content)
items.append(
{
"id": seed["id"],
"name": seed["name"],
"description": seed["description"],
# The bundled local asset is the same high-fidelity dashboard
# preview used by the sample projects. It works without any
# external image service and is copied into the runtime asset
# directory at startup.
"cover_image": COVER_URL,
"content": content,
"owner_id": None,
"scope": "market",
"is_builtin": True,
"sort_order": seed["sort_order"],
}
)
return items
async def ensure_builtin_templates(store) -> list[str]:
"""Insert missing builtin templates and refresh their canvas JSON.
Returns only the newly inserted ids. A later seed does not rename a
template an administrator has already edited.
"""
inserted: list[str] = []
for item in load_builtin_templates():
if await store.insert_if_absent(item):
inserted.append(item["id"])
else:
await store.refresh_builtin_content(item["id"], item["content"])
return inserted