rag/wwwroot/knowledge_bases_list/storage_stats.dspy
ymq 789960a1cf refactor(schema): rag 模块全部数据表加 rag_ 前缀(模块跨应用复用防冲突)
- models/ 重建为正确表定义格式(summary+fields+indexes),覆盖全部 11 个表
  修正原 models/ 误放 CRUD 格式副本(缺 summary)导致 json2ddl 100% 失败被静默忽略
- json/ 6 个 CRUD 定义 tblname 加前缀 + 文件改名
- 129 处 SQL/sor 表名加 rag_ 前缀(URL 路径 knowledge_bases_list 保持不变)
- 新增 init/migrate_rag_prefix.sql 幂等 RENAME TABLE 迁移
2026-08-25 14:24:37 +08:00

43 lines
1.9 KiB
Plaintext

ns = params_kw.copy()
env = request._run_ns
userorgid = await env.get_userorgid()
# Per-org metering: documents.file_size by org_id (auto-decreases on delete)
used = 0
limit = 104857600
docs = 0
async with get_sor_context(env, 'rag') as sor:
rec = await sor.sqlExe(
"SELECT COALESCE(SUM(d.file_size),0) AS used, COUNT(*) AS docs "
"FROM rag_documents d JOIN rag_knowledge_bases k ON d.kb_id=k.id "
"WHERE k.org_id=${org_id}$",
{"org_id": userorgid})
if rec:
used = int(rec[0].used)
docs = int(rec[0].docs)
lim = await sor.sqlExe("SELECT limit_bytes FROM rag_org_storage_limits WHERE org_id=${org_id}$", {"org_id": userorgid})
if lim:
limit = int(lim[0].limit_bytes)
def fmt(sz):
if sz < 1024: return str(sz) + 'B'
elif sz < 1024*1024: return str(round(sz/1024,1)) + 'KB'
else: return str(round(sz/(1024*1024),1)) + 'MB'
pct = min(round(used / max(limit, 1) * 100, 1), 100) if used > 0 else 0
bar_color = "#f56c6c" if pct >= 90 else ("#e6a23c" if pct >= 70 else "#4a90d9")
return json.dumps({
"widgettype": "VBox",
"options": {"width": "100%", "bgcolor": "#f8f9fa", "padding": "16px", "css": "card", "spacing": "4px"},
"subwidgets": [
{"widgettype": "HBox", "options": {"width": "100%", "justifyContent": "space-between"}, "subwidgets": [
{"widgettype": "Text", "options": {"text": "💾 存储容量(本机构)", "cfontsize": 14, "fontWeight": "bold", "color": "#333"}},
{"widgettype": "Text", "options": {"text": "已用 " + fmt(used) + " / 限额 " + fmt(limit) + " | " + str(docs) + " 文档", "cfontsize": 12, "color": "#888"}}
]},
{"widgettype": "VBox", "options": {"width": "100%", "cheight": 0.5, "bgcolor": "#e0e0e0"}, "subwidgets": [
{"widgettype": "VBox", "options": {"cwidth": pct, "cheight": 0.5, "bgcolor": bar_color}}
]}
]
}, ensure_ascii=False, default=str)