From 8ed658382f952cfbb48b38e56eba406e1d3eb685 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 11 Sep 2026 18:23:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(analytics):=20=E4=BD=BF=E7=94=A8=E5=88=86?= =?UTF-8?q?=E6=9E=90=E9=9D=A2=E6=9D=BF=20+=20schema=20=E5=B1=82=E5=AE=A1?= =?UTF-8?q?=E8=AE=A1=20+=20access=5Flog=20target/=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - access_log 中间件按路由模板映射 target(修复 :name/:id 参数名导致 /object-types/:name 等路由 target 长期为空);401/4xx 兜底记 deny; action 简化为 read/write/delete/manage;新增 90 天保留策略 (ACCESS_LOG_RETENTION_DAYS 覆盖,0 关闭) - schema 审计:对象类型/属性/链接类型/链接属性/动作类型/分级配置的 增删改写入 ontology_audit_log(target_pk 落库,update/delete 带 before 快照); 存量库补 target_pk 列(幂等 ALTER) - 新增 /api/v1/analytics/{overview,types,users,sharing}(仅 manager): 总体读写比/活跃主体/来源分布/按天趋势、分项热度、用户活跃、共享授权使用量 - 前端新增「使用分析」tab(AnalyticsView) --- server/access_log.go | 124 +++++++-- server/classification.go | 8 + server/config.go | 2 + server/db.go | 2 + server/handlers_analytics.go | 479 ++++++++++++++++++++++++++++++++ server/handlers_ontology.go | 55 ++++ server/main.go | 11 + server/schema_audit.go | 123 ++++++++ web/src/App.vue | 2 + web/src/api.js | 6 + web/src/views/AnalyticsView.vue | 212 ++++++++++++++ 11 files changed, 1008 insertions(+), 16 deletions(-) create mode 100644 server/handlers_analytics.go create mode 100644 server/schema_audit.go create mode 100644 web/src/views/AnalyticsView.vue diff --git a/server/access_log.go b/server/access_log.go index af1ee52..3774dd7 100644 --- a/server/access_log.go +++ b/server/access_log.go @@ -17,6 +17,7 @@ import ( "encoding/json" "log" "strconv" + "strings" "sync" "time" @@ -110,20 +111,24 @@ func (a *app) accessLogMiddleware() gin.HandlerFunc { } // 解析 principal p := a.principalFromRequest(c) - // 提取目标 (type/pk) - typ := c.Param("type") - pk := c.Param("pk") - if typ == "" { - typ = c.Query("type") - } - if pk == "" { - pk = c.Query("pk") + // 提取目标 (type/pk)。三层来源: + // 1) handler 通过 c.Set("audit_target_pk", ...) 精确指定(create 类目标名在 body 里) + // 2) 按路由模板映射(targetFromRoute,修复 :name/:id 参数名导致 target 为空) + // 3) query 参数兜底(老行为) + typ, pk := targetFromRoute(c) + if v := c.GetString("audit_target_pk"); v != "" { + pk = v } // 决定(默认 allow;若 handler 已通过 c.Set("authz_decision", ...) 标记,则用它) decision := c.GetString("authz_decision") if decision == "" { decision = "allow" } + // 修复(2026-09-11):鉴权/业务层直接 401/403/404 的请求此前被记成 allow, + // 导致"拒绝"统计失真 —— 按最终响应状态码兜底标记。 + if decision == "allow" && c.Writer.Status() >= 400 { + decision = "deny" + } // 命中 grant(可选:handler 通过 c.Set("authz_grants", [...]) 写入) var matchedGrants []int64 if v, ok := c.Get("authz_grants"); ok { @@ -153,18 +158,77 @@ func (a *app) accessLogMiddleware() gin.HandlerFunc { } } +// targetFromRoute 按路由模板(c.FullPath())把请求映射到 (target_type, target_pk)。 +// 此前只认 :type/:pk 两个参数名,而 /object-types/:name、/properties/:id 等路由的 +// 参数名是 name/id,导致本体类型层操作的 target 长期为空,分项统计无从聚合。 +// 约定:实例类路由(/objects/:type/:pk、/connector/object[s]/…)的 target_type +// 就是对象类型 api_name;元原语路由用固定类型名(object_type/link_type/...)。 +func targetFromRoute(c *gin.Context) (string, string) { + // 实例路由::type(+:pk)参数天然就是目标 + if t := c.Param("type"); t != "" { + return t, c.Param("pk") + } + // query 兜底(老行为保留) + if t := c.Query("type"); t != "" { + return t, c.Query("pk") + } + fp := c.FullPath() + if fp == "" { + return "", "" + } + name := c.Param("name") + id := c.Param("id") + switch { + case strings.Contains(fp, "/object-types/:name/properties"): + return "object_property", name // 具体属性名在 body,schemaAudit 会记全 + case strings.Contains(fp, "/object-types/:name"): + return "object_type", name + case strings.Contains(fp, "/object-types"): + return "object_type", "" // create:名字在 body + case strings.Contains(fp, "/properties/:id"): + return "object_property", id + case strings.Contains(fp, "/link-types/:name/properties"): + return "link_property", name + case strings.Contains(fp, "/link-types/:name"): + return "link_type", name + case strings.Contains(fp, "/link-types"): + return "link_type", "" + case strings.Contains(fp, "/link-properties/:id"): + return "link_property", id + case strings.Contains(fp, "/action-types"): + return "action_type", "" + case strings.Contains(fp, "/function-types/:name"): + return "function_type", name + case strings.Contains(fp, "/function-types"): + return "function_type", "" + case strings.Contains(fp, "/interface-types/:name"): + return "interface_type", name + case strings.Contains(fp, "/interface-types"): + return "interface_type", "" + case strings.Contains(fp, "/materializations/:name"): + return "materialization", name + case strings.Contains(fp, "/backing-sources/:name"): + return "backing_source", name + case strings.Contains(fp, "/principals/:name"): + return "principal", name + case strings.Contains(fp, "/grants/:id"): + return "grant", id + case strings.Contains(fp, "/field-policies/:id"): + return "field_policy", id + } + return "", "" +} + func classifyAction(method, path string) string { switch { - case contains(path, "/objects/") && method == "GET": - return "read" - case contains(path, "/objects") && (method == "POST" || method == "PUT"): - return "write" - case contains(path, "/objects/") && method == "DELETE": - return "delete" - case contains(path, "/connector/") && method == "GET": - return "read" case contains(path, "/access/"): return "manage" + case method == "GET": + return "read" + case method == "DELETE": + return "delete" + case method == "POST" || method == "PUT": + return "write" default: return method + " " + path } @@ -262,5 +326,33 @@ func (a *app) accessLogStats(c *gin.Context) { c.JSON(200, gin.H{"stats": out}) } +// ---------- 保留策略 ---------- + +// startAccessLogRetention 定期清理过期 access_log,防止审计表无限膨胀。 +// 默认保留 90 天(ACCESS_LOG_RETENTION_DAYS 覆盖,0 = 关闭清理)。 +func startAccessLogRetention(p dbExec, days int) { + if days <= 0 { + log.Printf("[access_log] 保留策略已关闭(ACCESS_LOG_RETENTION_DAYS=0)") + return + } + go func() { + clean := func() { + tag, err := p.Exec(context.Background(), + `DELETE FROM access_log WHERE created_at < now() - make_interval(days => $1)`, days) + if err != nil { + log.Printf("[access_log] 过期清理失败: %v", err) + } else if tag.RowsAffected() > 0 { + log.Printf("[access_log] 清理过期审计日志 %d 行(保留 %d 天)", tag.RowsAffected(), days) + } + } + clean() // 启动时清一次 + t := time.NewTicker(24 * time.Hour) + defer t.Stop() + for range t.C { + clean() + } + }() +} + // strconv_Atoi 占位(已用标准库,保留符号避免引用残留) var _ = strconv.Atoi \ No newline at end of file diff --git a/server/classification.go b/server/classification.go index 8077abf..24fcc36 100644 --- a/server/classification.go +++ b/server/classification.go @@ -614,6 +614,14 @@ func (a *app) setObjectTypeClassification(c *gin.Context) { c.JSON(400, gin.H{"error": "body 必须是合法 JSON: " + err.Error()}) return } + // before 快照(审计用) + before := a.objectTypeSnapshot(c, name) + // 分级配置也是 schema 变更,统一走 ontology_audit_log(仅在成功响应时记录) + defer func() { + if c.Writer.Status() < 400 { + a.schemaAudit(c, "object_type_classification", "object_type", name, map[string]any{"before": before, "after": in}) + } + }() // 关掉分级(property=空)允许 levels=[] 与 default="" if in.Property == "" { // property=空直接落库:levels 与 default 都以「空」处理(不会去校验词汇) diff --git a/server/config.go b/server/config.go index 2cc5080..7803b7b 100644 --- a/server/config.go +++ b/server/config.go @@ -17,6 +17,7 @@ type Config struct { AllowCORS bool SeedOnStart bool // 首次启动若本体为空则灌入极简占位种子 ArchiveDir string // 知识文件根目录(archive),文件管理功能使用 + AccessLogRetentionDays int // access_log 保留天数(0=不清理),默认 90 } func loadConfig() *Config { @@ -30,6 +31,7 @@ func loadConfig() *Config { AllowCORS: getEnvBool("ALLOW_CORS", true), SeedOnStart: getEnvBool("SEED_ON_START", true), ArchiveDir: getEnv("ARCHIVE_DIR", "archive"), + AccessLogRetentionDays: getEnvInt("ACCESS_LOG_RETENTION_DAYS", 90), } return c } diff --git a/server/db.go b/server/db.go index ccc1831..cc68e22 100644 --- a/server/db.go +++ b/server/db.go @@ -327,6 +327,8 @@ CREATE TABLE IF NOT EXISTS ontology_audit_log ( ); CREATE INDEX IF NOT EXISTS idx_audit_log_created ON ontology_audit_log(created_at); CREATE INDEX IF NOT EXISTS idx_audit_log_actor ON ontology_audit_log(actor); +-- 兼容存量库(2026-09-11):schema 审计按 target_pk 精确落库 +ALTER TABLE ontology_audit_log ADD COLUMN IF NOT EXISTS target_pk TEXT DEFAULT ''; -- 兼容存量库:补 P1-4 Interface 共享 properties + 多态 (2026-08-24) ALTER TABLE interface_types ADD COLUMN IF NOT EXISTS shared_properties JSONB DEFAULT '[]'::jsonb; diff --git a/server/handlers_analytics.go b/server/handlers_analytics.go new file mode 100644 index 0000000..d85376f --- /dev/null +++ b/server/handlers_analytics.go @@ -0,0 +1,479 @@ +package main + +// 使用分析(2026-09-11) +// +// 基于既有数据现算,不建新表: +// - access_log → 请求量 / 浏览(read) / 修改(write,delete) / 活跃用户 / 来源分布 +// - object_changes → 实例级增删改(含字段 diff 的量) +// - ontology_audit_log → schema 层变更(object_type/link_type/property/...) +// - grants + principals → 共享关系矩阵 + 关联使用量 +// +// 时间口径:days 参数(默认 30,1..365);按天分组用 Asia/Shanghai 时区。 + +import ( + "encoding/json" + "net/http" + "sort" + "strconv" + "time" + + "github.com/gin-gonic/gin" +) + +const tzExpr = "AT TIME ZONE 'Asia/Shanghai'" + +func daysParam(c *gin.Context) int { + d, err := strconv.Atoi(c.Query("days")) + if err != nil || d <= 0 || d > 365 { + return 30 + } + return d +} + +// ---------- 总体 ---------- + +// analyticsOverview 总体:请求量 / 读写分布 / 活跃用户 / 来源分布 / 按天趋势 / 最近 schema 变更 +// GET /api/v1/analytics/overview?days=30 +func (a *app) analyticsOverview(c *gin.Context) { + days := daysParam(c) + since := time.Now().AddDate(0, 0, -days) + + var total, reads, writes, deletes, denies, mcpCnt, connCnt, kmCnt, users int64 + err := a.pool.QueryRow(c, ` + SELECT count(*), + count(*) FILTER (WHERE action='read'), + count(*) FILTER (WHERE action='write'), + count(*) FILTER (WHERE action='delete'), + count(*) FILTER (WHERE decision<>'allow'), + count(*) FILTER (WHERE path LIKE '/api/v1/mcp%'), + count(*) FILTER (WHERE path LIKE '/api/v1/connector/%'), + count(*) FILTER (WHERE path LIKE '/api/v1/km/%'), + count(DISTINCT principal) FILTER (WHERE principal <> '') + FROM access_log WHERE created_at >= $1`, since).Scan( + &total, &reads, &writes, &deletes, &denies, &mcpCnt, &connCnt, &kmCnt, &users) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + // 按天趋势 + type dayRow struct { + Day string `json:"day"` + Requests int64 `json:"requests"` + Reads int64 `json:"reads"` + Writes int64 `json:"writes"` + Users int64 `json:"users"` + } + dayRows, err := a.pool.Query(c, ` + SELECT to_char(created_at `+tzExpr+`, 'YYYY-MM-DD') AS day, + count(*) AS requests, + count(*) FILTER (WHERE action='read') AS reads, + count(*) FILTER (WHERE action IN ('write','delete')) AS writes, + count(DISTINCT principal) FILTER (WHERE principal<>'') AS users + FROM access_log WHERE created_at >= $1 + GROUP BY day ORDER BY day`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + defer dayRows.Close() + byDay := []dayRow{} + for dayRows.Next() { + var r dayRow + if dayRows.Scan(&r.Day, &r.Requests, &r.Reads, &r.Writes, &r.Users) == nil { + byDay = append(byDay, r) + } + } + + // 热门目标(读) + type topRow struct { + TargetType string `json:"target_type"` + TargetPK string `json:"target_pk"` + Count int64 `json:"count"` + } + topRows, err := a.pool.Query(c, ` + SELECT target_type, target_pk, count(*) AS c + FROM access_log + WHERE created_at >= $1 AND method='GET' AND target_type <> '' + GROUP BY 1,2 ORDER BY c DESC LIMIT 10`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + defer topRows.Close() + topTargets := []topRow{} + for topRows.Next() { + var r topRow + if topRows.Scan(&r.TargetType, &r.TargetPK, &r.Count) == nil { + topTargets = append(topTargets, r) + } + } + + // 最近 schema 变更(本体编辑记录) + type auditRow struct { + CreatedAt string `json:"created_at"` + Actor string `json:"actor"` + Action string `json:"action"` + TargetType string `json:"target_type"` + TargetPK string `json:"target_pk"` + Details map[string]any `json:"details"` + } + auditRows, err := a.pool.Query(c, ` + SELECT to_char(created_at `+tzExpr+`, 'YYYY-MM-DD HH24:MI'), actor, action, target_type, target_pk, details + FROM ontology_audit_log ORDER BY id DESC LIMIT 15`) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + defer auditRows.Close() + recentSchema := []auditRow{} + for auditRows.Next() { + var r auditRow + var details []byte + if auditRows.Scan(&r.CreatedAt, &r.Actor, &r.Action, &r.TargetType, &r.TargetPK, &details) == nil { + _ = json.Unmarshal(details, &r.Details) + recentSchema = append(recentSchema, r) + } + } + + c.JSON(http.StatusOK, gin.H{ + "days": days, + "total": total, + "reads": reads, + "writes": writes, + "deletes": deletes, + "denies": denies, + "mcp_requests": mcpCnt, + "conn_requests": connCnt, + "km_requests": kmCnt, + "active_users": users, + "by_day": byDay, + "top_targets": topTargets, + "recent_schema": recentSchema, + }) +} + +// ---------- 分项(按对象类型) ---------- + +// analyticsTypes 每个对象类型:本体定义浏览量 / 实例浏览量 / 实例修改量 / 最近时间 +// GET /api/v1/analytics/types?days=30 +func (a *app) analyticsTypes(c *gin.Context) { + days := daysParam(c) + since := time.Now().AddDate(0, 0, -days) + + type typeStat struct { + Name string `json:"name"` + DisplayName string `json:"display_name"` + Group string `json:"group"` + Instances int64 `json:"instances"` + OntViews int64 `json:"ontology_views"` // 本体定义/编辑页浏览 + InstReads int64 `json:"instance_reads"` // 实例浏览(管理台 + 连接器 + MCP 读) + InstWrites int64 `json:"instance_writes"` // 实例增删改(object_changes) + SchemaChanges int64 `json:"schema_changes"` // 类型/属性定义变更(ontology_audit_log) + LastRead string `json:"last_read"` + LastWrite string `json:"last_write"` + } + stats := map[string]*typeStat{} + + // 1) 全部对象类型打底 + rows, err := a.pool.Query(c, ` + SELECT ot.api_name, ot.display_name, COALESCE(ot.group_name,''), + (SELECT count(*) FROM objects o WHERE o.object_type_id = ot.id) + FROM object_types ot`) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + for rows.Next() { + var n, dn, g string + var cnt int64 + if rows.Scan(&n, &dn, &g, &cnt) == nil { + stats[n] = &typeStat{Name: n, DisplayName: dn, Group: g, Instances: cnt} + } + } + rows.Close() + + get := func(n string) *typeStat { + s, ok := stats[n] + if !ok { + s = &typeStat{Name: n} + stats[n] = s + } + return s + } + + // 2) 本体定义浏览(target_type='object_type' 的 GET) + rows, err = a.pool.Query(c, ` + SELECT target_pk, count(*), COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'') + FROM access_log + WHERE created_at >= $1 AND method='GET' AND target_type='object_type' AND target_pk <> '' + GROUP BY 1`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + for rows.Next() { + var pk, last string + var cnt int64 + if rows.Scan(&pk, &cnt, &last) == nil { + s := get(pk) + s.OntViews = cnt + s.LastRead = last + } + } + rows.Close() + + // 3) 实例浏览(/objects/:type/... 与 /connector/object[s]/... 的 GET) + rows, err = a.pool.Query(c, ` + SELECT target_type, count(*), COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'') + FROM access_log + WHERE created_at >= $1 AND method='GET' AND target_type <> '' AND target_type <> 'object_type' + AND (path LIKE '/api/v1/objects/%' OR path LIKE '/api/v1/connector/object%' OR path='/api/v1/mcp') + GROUP BY 1`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + for rows.Next() { + var tn, last string + var cnt int64 + if rows.Scan(&tn, &cnt, &last) == nil { + s := get(tn) + s.InstReads = cnt + if last > s.LastRead { + s.LastRead = last + } + } + } + rows.Close() + + // 4) 实例修改(object_changes:create/update/delete/restore) + rows, err = a.pool.Query(c, ` + SELECT object_type, count(*), COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'') + FROM object_changes WHERE created_at >= $1 GROUP BY 1`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + for rows.Next() { + var tn, last string + var cnt int64 + if rows.Scan(&tn, &cnt, &last) == nil { + s := get(tn) + s.InstWrites = cnt + s.LastWrite = last + } + } + rows.Close() + + // 5) schema 变更(ontology_audit_log 中 object_type / object_property 按 target_pk 前缀归属类型) + rows, err = a.pool.Query(c, ` + SELECT target_type, target_pk, count(*) + FROM ontology_audit_log + WHERE created_at >= $1 AND target_type IN ('object_type','object_property') + GROUP BY 1,2`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + for rows.Next() { + var tt, tp string + var cnt int64 + if rows.Scan(&tt, &tp, &cnt) == nil { + name := tp + // object_property 的 target_pk 是 "类型.属性名" 或 "id:N"(历史记录),取前缀归属 + if tt == "object_property" { + if i := lastIndexByte(name, '.'); i > 0 { + name = name[:i] + } else { + continue // id:N 无法归属到类型,跳过 + } + } + get(name).SchemaChanges += cnt + } + } + rows.Close() + + out := make([]typeStat, 0, len(stats)) + for _, s := range stats { + out = append(out, *s) + } + // 按热度排序:实例读 > 定义浏览 > 实例写 > 实例数 + sort.Slice(out, func(i, j int) bool { + si, sj := out[i], out[j] + ki, kj := si.InstReads+si.OntViews*2+si.InstWrites*3, sj.InstReads+sj.OntViews*2+sj.InstWrites*3 + if ki != kj { + return ki > kj + } + return si.Instances > sj.Instances + }) + c.JSON(http.StatusOK, gin.H{"days": days, "types": out}) +} + +func lastIndexByte(s string, b byte) int { + for i := len(s) - 1; i >= 0; i-- { + if s[i] == b { + return i + } + } + return -1 +} + +// ---------- 用户活跃 ---------- + +// analyticsUsers 每个主体:请求量 / 读 / 写 / 删 / 拒绝 / 触达目标数 / 最近活跃 +// GET /api/v1/analytics/users?days=30 +func (a *app) analyticsUsers(c *gin.Context) { + days := daysParam(c) + since := time.Now().AddDate(0, 0, -days) + + type userStat struct { + Principal string `json:"principal"` + Requests int64 `json:"requests"` + Reads int64 `json:"reads"` + Writes int64 `json:"writes"` + Deletes int64 `json:"deletes"` + Denies int64 `json:"denies"` + Targets int64 `json:"targets"` + LastActive string `json:"last_active"` + Active bool `json:"active"` + } + stats := map[string]*userStat{} + + rows, err := a.pool.Query(c, ` + SELECT principal, count(*), + count(*) FILTER (WHERE action='read'), + count(*) FILTER (WHERE action='write'), + count(*) FILTER (WHERE action='delete'), + count(*) FILTER (WHERE decision<>'allow'), + count(DISTINCT (target_type || '/' || target_pk)) FILTER (WHERE target_type<>''), + COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'') + FROM access_log + WHERE created_at >= $1 AND principal <> '' + GROUP BY principal ORDER BY count(*) DESC`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + for rows.Next() { + var u userStat + if rows.Scan(&u.Principal, &u.Requests, &u.Reads, &u.Writes, &u.Deletes, &u.Denies, &u.Targets, &u.LastActive) == nil { + u.Active = true + stats[u.Principal] = &u + } + } + rows.Close() + + // principals 表打底:窗口期内没动静的主体标 active=false + rows, err = a.pool.Query(c, `SELECT name, COALESCE(description,'') FROM principals ORDER BY name`) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + type pInfo struct { + Desc string `json:"description"` + } + principals := map[string]pInfo{} + for rows.Next() { + var n, d string + if rows.Scan(&n, &d) == nil { + principals[n] = pInfo{Desc: d} + if _, ok := stats[n]; !ok { + stats[n] = &userStat{Principal: n} + } + } + } + rows.Close() + + out := make([]userStat, 0, len(stats)) + for _, s := range stats { + out = append(out, *s) + } + sort.Slice(out, func(i, j int) bool { return out[i].Requests > out[j].Requests }) + c.JSON(http.StatusOK, gin.H{"days": days, "users": out, "principals": principals}) +} + +// ---------- 共享 ---------- + +// analyticsSharing 授权(grant)矩阵 + 每条授权在窗口期内的实际使用量 +// GET /api/v1/analytics/sharing?days=30 +func (a *app) analyticsSharing(c *gin.Context) { + days := daysParam(c) + since := time.Now().AddDate(0, 0, -days) + + type grantRow struct { + ID int64 `json:"id"` + Principal string `json:"principal"` + Action string `json:"action"` + ScopeType string `json:"scope_type"` + ScopePK string `json:"scope_pk"` + Effect string `json:"effect"` + Used int64 `json:"used"` // 窗口期内该主体落在该 scope 范围内的请求数 + } + rows, err := a.pool.Query(c, ` + SELECT g.id, g.principal, g.action, g.scope_type, g.scope_pk, g.effect, + (SELECT count(*) FROM access_log al + WHERE al.created_at >= $1 AND al.principal = g.principal + AND (g.scope_type = '*' OR al.target_type = g.scope_type) + AND (g.scope_pk = '*' OR al.target_pk = g.scope_pk)) AS used + FROM grants g ORDER BY g.principal, g.id`, since) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + defer rows.Close() + grants := []grantRow{} + sharedPrincipals := map[string]bool{} + for rows.Next() { + var r grantRow + if rows.Scan(&r.ID, &r.Principal, &r.Action, &r.ScopeType, &r.ScopePK, &r.Effect, &r.Used) == nil { + grants = append(grants, r) + sharedPrincipals[r.Principal] = true + } + } + + // 每个被共享主体的使用总量(读/写) + type pUsage struct { + Principal string `json:"principal"` + Grants int64 `json:"grants"` + Requests int64 `json:"requests"` + Reads int64 `json:"reads"` + Writes int64 `json:"writes"` + } + usageRows, err := a.pool.Query(c, ` + SELECT al.principal, count(*), + count(*) FILTER (WHERE al.action='read'), + count(*) FILTER (WHERE al.action IN ('write','delete')) + FROM access_log al + WHERE al.created_at >= $1 AND al.principal = ANY($2::text[]) + GROUP BY al.principal ORDER BY count(*) DESC`, since, mapKeys(sharedPrincipals)) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + defer usageRows.Close() + usage := []pUsage{} + for usageRows.Next() { + var u pUsage + if usageRows.Scan(&u.Principal, &u.Requests, &u.Reads, &u.Writes) == nil { + usage = append(usage, u) + } + } + for i := range usage { + for _, g := range grants { + if g.Principal == usage[i].Principal { + usage[i].Grants++ + } + } + } + + c.JSON(http.StatusOK, gin.H{"days": days, "grants": grants, "usage": usage}) +} + +func mapKeys(m map[string]bool) []string { + out := make([]string, 0, len(m)) + for k := range m { + out = append(out, k) + } + return out +} diff --git a/server/handlers_ontology.go b/server/handlers_ontology.go index 216463e..5c5c351 100644 --- a/server/handlers_ontology.go +++ b/server/handlers_ontology.go @@ -110,6 +110,9 @@ func (a *app) createObjectType(c *gin.Context) { return } } + // schema 审计(2026-09-11):create 记录输入快照 + c.Set("audit_target_pk", in.APIName) + a.schemaAudit(c, "object_type_create", "object_type", in.APIName, map[string]any{"after": in}) c.JSON(201, gin.H{"id": id, "api_name": in.APIName}) } @@ -147,6 +150,8 @@ func (a *app) updateObjectType(c *gin.Context) { return } defer invalidateClassCache() + // before 快照(更新前,审计用) + before := a.objectTypeSnapshot(c, name) // 分级三件套允许显式置空:传空串即关闭(property)/清空(levels,default) lvJSON := mustJSON(in.ClassificationLevels) clearProp := in.ClassificationProperty != "" @@ -188,6 +193,9 @@ func (a *app) updateObjectType(c *gin.Context) { return } } + // schema 审计(2026-09-11):before/after + c.Set("audit_target_pk", name) + a.schemaAudit(c, "object_type_update", "object_type", name, map[string]any{"before": before, "after": in}) c.JSON(200, gin.H{"ok": true}) } @@ -238,6 +246,8 @@ func (a *app) upsertObjectTypeProperties(c *gin.Context, oid int64, props []prop func (a *app) deleteObjectType(c *gin.Context) { name := c.Param("name") + // before 快照(删除前,审计 + 误删可查) + before := a.objectTypeSnapshot(c, name) tag, err := a.pool.Exec(c, `DELETE FROM object_types WHERE api_name=$1`, name) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) @@ -247,6 +257,7 @@ func (a *app) deleteObjectType(c *gin.Context) { c.JSON(404, gin.H{"error": "对象类型不存在"}) return } + a.schemaAudit(c, "object_type_delete", "object_type", name, map[string]any{"before": before}) c.JSON(200, gin.H{"ok": true}) } @@ -343,6 +354,7 @@ func (a *app) addProperty(c *gin.Context) { return } invalidateClassCache() + a.schemaAudit(c, "property_add", "object_property", name+"."+in.APIName, map[string]any{"after": in}) c.JSON(201, gin.H{"ok": true}) } @@ -353,6 +365,8 @@ func (a *app) updateProperty(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } + // before 快照(更新前,审计用) + before := a.propertySnapshot(c, id) enum := "[]" if len(in.EnumValues) > 0 { enum = mustJSON(in.EnumValues) @@ -380,11 +394,21 @@ func (a *app) updateProperty(c *gin.Context) { return } invalidateClassCache() + // schema 审计(2026-09-11):target_pk 用 类型.属性名(before 里拿得到时),否则退回 id + pk := fmt.Sprintf("id:%d", id) + if before != nil { + if ot, ok := before["object_type"].(string); ok && ot != "" { + pk = ot + "." + before["api_name"].(string) + } + } + a.schemaAudit(c, "property_update", "object_property", pk, map[string]any{"before": before, "after": in}) c.JSON(200, gin.H{"ok": true}) } func (a *app) deleteProperty(c *gin.Context) { id, _ := strconv.ParseInt(c.Param("id"), 10, 64) + // before 快照(删除前,审计 + 误删可查) + before := a.propertySnapshot(c, id) tag, err := a.pool.Exec(c, `DELETE FROM properties WHERE id=$1`, id) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) @@ -395,6 +419,13 @@ func (a *app) deleteProperty(c *gin.Context) { return } invalidateClassCache() + pk := fmt.Sprintf("id:%d", id) + if before != nil { + if ot, ok := before["object_type"].(string); ok && ot != "" { + pk = ot + "." + before["api_name"].(string) + } + } + a.schemaAudit(c, "property_delete", "object_property", pk, map[string]any{"before": before}) c.JSON(200, gin.H{"ok": true}) } @@ -448,6 +479,9 @@ func (a *app) createLinkType(c *gin.Context) { if in.Cardinality == "" { in.Cardinality = "many_to_many" } + // upsert 语义:区分 create / update 供审计归类 + var existed int + isUpdate := a.pool.QueryRow(c, `SELECT 1 FROM link_types WHERE api_name=$1`, in.APIName).Scan(&existed) == nil _, err := a.pool.Exec(c, `INSERT INTO link_types(api_name,display_name,source_type,target_type,cardinality,description, directional,source_property,target_property) @@ -466,11 +500,19 @@ func (a *app) createLinkType(c *gin.Context) { c.JSON(500, gin.H{"error": err.Error()}) return } + c.Set("audit_target_pk", in.APIName) + action := "link_type_create" + if isUpdate { + action = "link_type_update" + } + a.schemaAudit(c, action, "link_type", in.APIName, map[string]any{"after": in}) c.JSON(201, gin.H{"ok": true}) } func (a *app) deleteLinkType(c *gin.Context) { name := c.Param("name") + // before 快照(删除前,审计 + 误删可查) + before := a.linkTypeSnapshot(c, name) tag, err := a.pool.Exec(c, `DELETE FROM link_types WHERE api_name=$1`, name) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) @@ -480,6 +522,7 @@ func (a *app) deleteLinkType(c *gin.Context) { c.JSON(404, gin.H{"error": "链接类型不存在"}) return } + a.schemaAudit(c, "link_type_delete", "link_type", name, map[string]any{"before": before}) c.JSON(200, gin.H{"ok": true}) } @@ -565,11 +608,14 @@ func (a *app) addLinkProperty(c *gin.Context) { c.JSON(500, gin.H{"error": err.Error()}) return } + a.schemaAudit(c, "link_property_add", "link_property", name+"."+in.APIName, map[string]any{"after": in}) c.JSON(201, gin.H{"ok": true}) } func (a *app) deleteLinkProperty(c *gin.Context) { id, _ := strconv.ParseInt(c.Param("id"), 10, 64) + // before 快照(删除前,审计 + 误删可查) + before := a.linkPropertySnapshot(c, id) tag, err := a.pool.Exec(c, `DELETE FROM link_properties WHERE id=$1`, id) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) @@ -579,6 +625,13 @@ func (a *app) deleteLinkProperty(c *gin.Context) { c.JSON(404, gin.H{"error": "链接属性不存在"}) return } + pk := fmt.Sprintf("id:%d", id) + if before != nil { + if lt, ok := before["link_type"].(string); ok && lt != "" { + pk = lt + "." + before["api_name"].(string) + } + } + a.schemaAudit(c, "link_property_delete", "link_property", pk, map[string]any{"before": before}) c.JSON(200, gin.H{"ok": true}) } @@ -692,6 +745,8 @@ func (a *app) createActionType(c *gin.Context) { c.JSON(500, gin.H{"error": err.Error()}) return } + c.Set("audit_target_pk", in.APIName) + a.schemaAudit(c, "action_type_upsert", "action_type", in.APIName, map[string]any{"after": in}) c.JSON(201, gin.H{"ok": true}) } diff --git a/server/main.go b/server/main.go index 1742239..63bfbd0 100644 --- a/server/main.go +++ b/server/main.go @@ -109,6 +109,8 @@ func run() error { // Dynamic Security 审计日志中间件(自动记录每次请求的访问决策) r.Use(a.accessLogMiddleware()) startAccessLogFlusher(pool) + // access_log 保留策略(2026-09-11):默认 90 天,ACCESS_LOG_RETENTION_DAYS 覆盖 + startAccessLogRetention(pool, cfg.AccessLogRetentionDays) // 物化层调度器(P2-1):后台 ticker 定期刷新 interval 物化 startMaterializationScheduler(context.Background(), a) @@ -337,6 +339,15 @@ func registerRoutes(r *gin.Engine, a *app) { acc.GET("/log/stats", a.accessLogStats) } + // 使用分析(2026-09-11):本体总体/分项使用、用户活跃、共享情况(仅 manager) + ana := mgmt.Group("/analytics") + { + ana.GET("/overview", a.analyticsOverview) + ana.GET("/types", a.analyticsTypes) + ana.GET("/users", a.analyticsUsers) + ana.GET("/sharing", a.analyticsSharing) + } + // 数据备份(v1.3.0):仅 manager 主体可访问,含 create/inspect/list/download/diff/delete // 不提供 restore 到线上——MCP 客户端拿到 dump 后在另一实例验证恢复路径 bkp := mgmt.Group("/backups") diff --git a/server/schema_audit.go b/server/schema_audit.go new file mode 100644 index 0000000..86ce589 --- /dev/null +++ b/server/schema_audit.go @@ -0,0 +1,123 @@ +package main + +// 本体 schema 层审计(2026-09-11) +// +// 实例级变更已有 object_changes(字段级 diff + 快照),但 schema 层 +// (对象类型 / 属性 / 链接类型 / 链接属性 / 动作类型)此前只有 access_log 的 +// method+path 间接痕迹,没有 before/after 内容 —— 本文件补齐: +// - 全部 schema 增删改写入 ontology_audit_log(target_pk 落库) +// - update/delete 记录变更前快照(before),create 记录输入(after) +// 失败只打日志,不阻断主流程(与 recordChange 同策略)。 + +import ( + "context" + "encoding/json" + "log" + + "github.com/gin-gonic/gin" +) + +// schemaAudit 写一条 schema 层审计记录。 +func (a *app) schemaAudit(c *gin.Context, action, targetType, targetPK string, details map[string]any) { + actor := a.principalFromRequest(c) + if actor == "" { + actor = "anonymous" + } + if details == nil { + details = map[string]any{} + } + b, _ := json.Marshal(details) + if _, err := a.pool.Exec(context.Background(), + `INSERT INTO ontology_audit_log(actor, action, target_type, target_pk, details, affected_rows) + VALUES($1,$2,$3,$4,$5::jsonb,$6)`, + actor, action, targetType, targetPK, string(b), 1); err != nil { + log.Printf("[schema_audit] 写入失败 %s %s/%s: %v", action, targetType, targetPK, err) + } +} + +// toMap 结构体 → map(审计 details 用,nil 安全)。 +func toMap(v any) map[string]any { + if v == nil { + return nil + } + b, err := json.Marshal(v) + if err != nil { + return nil + } + var m map[string]any + _ = json.Unmarshal(b, &m) + return m +} + +// objectTypeSnapshot 读取对象类型(含属性)当前状态,作 update/delete 的 before 快照。 +func (a *app) objectTypeSnapshot(c *gin.Context, name string) map[string]any { + o, err := loadObjectType(c, a.pool, name) + if err != nil { + return nil + } + return toMap(o) +} + +// propertySnapshot 按 properties.id 读取属性当前状态(含所属类型 api_name)。 +func (a *app) propertySnapshot(c *gin.Context, id int64) map[string]any { + var p Property + var enum, defVal []byte + var typeName string + err := a.pool.QueryRow(c, ` + SELECT p.id, p.object_type_id, p.api_name, p.display_name, p.data_type, p.required, + p.enum_values, p.description, p.format, p.default_value, p.indexable, + p.vector_index, p.backing_column, COALESCE(p.sensitivity,''), ot.api_name + FROM properties p JOIN object_types ot ON ot.id = p.object_type_id + WHERE p.id=$1`, id). + Scan(&p.ID, &p.ObjectID, &p.APIName, &p.DisplayName, &p.DataType, &p.Required, + &enum, &p.Description, &p.Format, &defVal, &p.Indexable, + &p.VectorIndex, &p.BackingColumn, &p.Sensitivity, &typeName) + if err != nil { + return nil + } + p.EnumValues = parseStringArray(enum) + p.DefaultValue = jsonRaw(defVal) + m := toMap(p) + if m != nil { + m["object_type"] = typeName + } + return m +} + +// linkTypeSnapshot 读取链接类型当前状态。 +func (a *app) linkTypeSnapshot(c *gin.Context, name string) map[string]any { + var l LinkType + err := a.pool.QueryRow(c, ` + SELECT id, api_name, display_name, source_type, target_type, cardinality, + COALESCE(description,''), directional, COALESCE(source_property,''), COALESCE(target_property,'') + FROM link_types WHERE api_name=$1`, name). + Scan(&l.ID, &l.APIName, &l.DisplayName, &l.SourceType, &l.TargetType, &l.Cardinality, + &l.Description, &l.Directional, &l.SourceProperty, &l.TargetProperty) + if err != nil { + return nil + } + return toMap(l) +} + +// linkPropertySnapshot 按 link_properties.id 读取链接属性当前状态(含所属链接类型 api_name)。 +func (a *app) linkPropertySnapshot(c *gin.Context, id int64) map[string]any { + var p LinkProperty + var enum []byte + var ltName string + err := a.pool.QueryRow(c, ` + SELECT p.id, p.link_type_id, p.api_name, p.display_name, p.data_type, p.required, + p.enum_values, COALESCE(p.description,''), lt.api_name + FROM link_properties p JOIN link_types lt ON lt.id = p.link_type_id + WHERE p.id=$1`, id). + Scan(&p.ID, &p.LinkTypeID, &p.APIName, &p.DisplayName, &p.DataType, &p.Required, + &enum, &p.Description, <Name) + if err != nil { + return nil + } + p.EnumValues = parseStringArray(enum) + m := toMap(p) + if m != nil { + m["link_type"] = ltName + } + return m +} diff --git a/web/src/App.vue b/web/src/App.vue index 0e38771..2eab25d 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -31,6 +31,7 @@ + @@ -78,6 +79,7 @@ import { ref, reactive, onMounted, watch } from 'vue' import { ElMessage, ElMessageBox } from 'element-plus' import OverviewView from './views/OverviewView.vue' +import AnalyticsView from './views/AnalyticsView.vue' import OntologyView from './views/OntologyView.vue' import InstanceView from './views/InstanceView.vue' import AIView from './views/AIView.vue' diff --git a/web/src/api.js b/web/src/api.js index d17bbe9..77230f7 100644 --- a/web/src/api.js +++ b/web/src/api.js @@ -171,6 +171,12 @@ export const api = { }, accessLogStats: () => request('GET', '/access/log/stats'), + // 使用分析(2026-09-11):本体总体/分项使用、用户活跃、共享情况 + analyticsOverview: (days = 30) => request('GET', `/analytics/overview?days=${days}`), + analyticsTypes: (days = 30) => request('GET', `/analytics/types?days=${days}`), + analyticsUsers: (days = 30) => request('GET', `/analytics/users?days=${days}`), + analyticsSharing: (days = 30) => request('GET', `/analytics/sharing?days=${days}`), + // ---- 账号密码登录与会话 ---- // dev 模式下也支持登录(与 X-API-Key 并存)。登录后浏览器自动带 cookie; // 后端 principalFromRequest 优先识别 cookie → 命名主体。 diff --git a/web/src/views/AnalyticsView.vue b/web/src/views/AnalyticsView.vue new file mode 100644 index 0000000..1ed29cb --- /dev/null +++ b/web/src/views/AnalyticsView.vue @@ -0,0 +1,212 @@ + + + + +