- 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)
480 lines
14 KiB
Go
480 lines
14 KiB
Go
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
|
||
}
|