feat(qaops): 区域统计展示直属人员与多部门兼属
build / build (push) Successful in 1m12s

按稳定身份展示零使用人员、个人次数与最近提问时间,直接部门去重计算兼属数量;保留原分支统计、配置和源问答。

验证:396项前端测试及类型检查通过;隔离PG全mirror/org竞态回归通过,个人/别名/同名/零使用/未关联与原部门汇总回归通过。
正式同镜像及上线验证尚未执行。
This commit is contained in:
姚章浩
2026-10-21 17:17:08 +08:00
parent cb9f0c389f
commit 99d6109bef
11 changed files with 519 additions and 67 deletions
@@ -0,0 +1,71 @@
package qaopsorg
import (
"encoding/json"
"strings"
"testing"
"time"
)
func TestDepartmentStatisticsExposeDirectPeopleWithoutDuplicatingBranchTotals(t *testing.T) {
d := Directory{Departments: []Department{{ID: "root", Name: "Root"}, {ID: "child", ParentID: "root", Name: "Child"}, {ID: "peer", ParentID: "root", Name: "Peer"}}, People: []Person{
{ID: "private-first", UnionID: "private-union", Name: "同名样例", DepartmentIDs: []string{"root", "child", "peer", "child"}},
{ID: "private-second", Name: "同名样例", DepartmentIDs: []string{"child"}},
{ID: "private-zero", DepartmentIDs: []string{"peer"}},
{ID: "private-unassigned", Name: "未关联样例", DepartmentIDs: []string{}},
}}
p, err := BuildAttribution(State{State: "ready", DirectoryID: "fixture"}, d)
if err != nil {
t.Fatal(err)
}
at := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
got, err := AggregateDepartmentStatistics(p, []DepartmentActivity{{StaffID: "private-first", Visits: 2, LastAskedAt: at}, {UnionID: "private-union", Visits: 3, LastAskedAt: at.Add(time.Hour)}, {StaffID: "private-second", Visits: 4, LastAskedAt: at}, {Asker: "同名样例", Visits: 7, LastAskedAt: at}, {StaffID: "private-unassigned", Visits: 1, LastAskedAt: at}})
if err != nil {
t.Fatal(err)
}
raw, err := json.Marshal(got)
if err != nil {
t.Fatal(err)
}
var wire struct {
People []struct {
Key string `json:"key"`
Name string `json:"name"`
DepartmentIDs []string `json:"department_ids"`
Visits int64 `json:"visits"`
Last *time.Time `json:"last_asked_at"`
} `json:"people"`
}
if json.Unmarshal(raw, &wire) != nil || len(wire.People) != 4 {
t.Fatal("department statistics must include the full unique roster, including zero-use and unassigned people")
}
visits := map[int64]int{}
keys := map[string]bool{}
for _, person := range wire.People {
if !strings.HasPrefix(person.Key, "person-") || len(person.Key) != 71 || keys[person.Key] {
t.Fatal("missing stable private row key or same-name people merged")
}
keys[person.Key] = true
visits[person.Visits]++
if person.Visits == 5 && (len(person.DepartmentIDs) != 3 || person.Last == nil || !person.Last.Equal(at.Add(time.Hour))) {
t.Fatal("direct memberships, alias aggregation or most recent personal activity lost")
}
if person.Visits == 0 && person.Last != nil {
t.Fatal("zero-use person received invented activity")
}
}
if visits[5] != 1 || visits[4] != 1 || visits[0] != 1 || visits[1] != 1 {
t.Fatal("one person's activity was multiplied by departments or a same-name record was misattributed")
}
if strings.Contains(string(raw), "private-") || strings.Contains(string(raw), "union_id") || strings.Contains(string(raw), "staff_id") {
t.Fatal("raw identity leaked")
}
if got.TotalPeople != 4 || got.TotalVisits != 17 || got.UnmappedVisits != 8 {
t.Fatal("adding personal rows changed existing totals")
}
for _, row := range got.Rows {
if row.ID == "root" && (row.TotalPeople != 3 || row.Visits != 9 || row.Visitors != 2) {
t.Fatal("expanded personal rows changed deduplicated parent totals")
}
}
}
+41 -6
View File
@@ -1,6 +1,11 @@
package qaopsorg
import "time"
import (
"crypto/sha256"
"encoding/hex"
"sort"
"time"
)
type DepartmentActivity struct {
StaffID, UnionID, Asker, Trigger string
@@ -18,15 +23,23 @@ type DepartmentStatistic struct {
Visits int64 `json:"visits"`
LastAskedAt *time.Time `json:"last_asked_at"`
}
type DepartmentPersonStatistic struct {
Key string `json:"key"`
Name string `json:"name"`
DepartmentIDs []string `json:"department_ids"`
Visits int64 `json:"visits"`
LastAskedAt *time.Time `json:"last_asked_at"`
}
type DepartmentStatistics struct {
Rows []DepartmentStatistic `json:"rows"`
TotalVisits int64 `json:"total_visits"`
UnmappedVisits int64 `json:"unmapped_visits"`
TotalPeople int `json:"total_people"`
People []DepartmentPersonStatistic `json:"people"`
Rows []DepartmentStatistic `json:"rows"`
TotalVisits int64 `json:"total_visits"`
UnmappedVisits int64 `json:"unmapped_visits"`
TotalPeople int `json:"total_people"`
}
func AggregateDepartmentStatistics(p *Attribution, activity []DepartmentActivity) (DepartmentStatistics, error) {
out := DepartmentStatistics{Rows: []DepartmentStatistic{}}
out := DepartmentStatistics{Rows: []DepartmentStatistic{}, People: []DepartmentPersonStatistic{}}
if p == nil || p.Metadata.State != "ready" {
return out, ErrUnavailable
}
@@ -40,7 +53,21 @@ func AggregateDepartmentStatistics(p *Attribution, activity []DepartmentActivity
out.Rows = append(out.Rows, DepartmentStatistic{ID: d.ID, ParentID: d.ParentID, Name: d.Name, Region: d.Region})
}
branches := map[string][]string{}
personIndices := map[string]int{}
for _, person := range p.Members() {
direct := map[string]bool{}
for _, id := range person.DepartmentIDs {
direct[id] = true
}
ids := []string{}
for id := range direct {
ids = append(ids, id)
}
sort.Strings(ids)
// Match the monthly roster's opaque row-key convention, not a raw staff/union ID.
sum := sha256.Sum256([]byte(p.Metadata.DirectoryID + "\x00" + person.PersonID))
personIndices[person.PersonID] = len(out.People)
out.People = append(out.People, DepartmentPersonStatistic{Key: "person-" + hex.EncodeToString(sum[:]), Name: person.Name, DepartmentIDs: ids})
seen := map[string]bool{}
for _, id := range person.DepartmentIDs {
for id != "" && !seen[id] {
@@ -64,6 +91,14 @@ func AggregateDepartmentStatistics(p *Attribution, activity []DepartmentActivity
}
out.TotalVisits += a.Visits
person := p.Resolve(a.StaffID, a.UnionID, a.Asker, a.Trigger)
if i, ok := personIndices[person.PersonID]; ok {
row := &out.People[i]
row.Visits += a.Visits
if row.LastAskedAt == nil || a.LastAskedAt.After(*row.LastAskedAt) {
at := a.LastAskedAt
row.LastAskedAt = &at
}
}
if len(branches[person.PersonID]) == 0 {
out.UnmappedVisits += a.Visits
continue