区域配置入口改为区域统计,五列表按当前通讯录整支人员去重;调岗依当前归属、零分母未知、最近真实提问时间和父行收起不重算。仅读治理正式总表,后台单并发/请求合并/60秒缓存,目录与配置版本隔离,客户端失败不伪零。 保留区域自定义目录与预览/双版本锁保存,父部门勾选联选全后代,取消子支清除祖先整支选择,避免隐式覆盖未选子级;允许删除无引用区域,已有引用不静默孤立。统计页与设置共享选择,统计支持累计与北京时间自然月;不新增迁移,不改DobotAI生产、代理、原问答、历史趋势或已有AI任务。 验证已执行:完整make verify退出0,705 Go/340前端通过、120项声明的可选集成跳过且必需0跳过;镜像全量隔离PG race退出0;新增去重/调岗/空分母、后台缓存/并发、HTTP授权/严格请求、真PG目录与配置失效、树选择/收起、五列与删除保护均先红后绿。旧导航测试按新名称更新后全量通过。上线前治理库备份与真实恢复72表/29张QA内容哈希一致,匹配独立主密钥受限备份。 当前尚待精确SHA CI、同镜像真实数据浏览器与正式读回,不把源码验证称为已上线。公司真实区域映射不在本轮擅改;只在隔离库验证保存。
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package qaopsorg
|
||||
|
||||
import "time"
|
||||
|
||||
type DepartmentActivity struct {
|
||||
StaffID, UnionID, Asker, Trigger string
|
||||
Visits int64
|
||||
LastAskedAt time.Time
|
||||
}
|
||||
type DepartmentStatistic struct {
|
||||
ID string `json:"id"`
|
||||
ParentID string `json:"parent_id"`
|
||||
Name string `json:"name"`
|
||||
Region string `json:"region"`
|
||||
Visitors int `json:"visitors"`
|
||||
TotalPeople int `json:"total_people"`
|
||||
Ratio *float64 `json:"ratio"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func AggregateDepartmentStatistics(p *Attribution, activity []DepartmentActivity) (DepartmentStatistics, error) {
|
||||
out := DepartmentStatistics{Rows: []DepartmentStatistic{}}
|
||||
if p == nil || p.Metadata.State != "ready" {
|
||||
return out, ErrUnavailable
|
||||
}
|
||||
indices := map[string]int{}
|
||||
nodes := map[string]DepartmentView{}
|
||||
visitors := map[string]map[string]bool{}
|
||||
for _, d := range p.Metadata.View.Departments {
|
||||
indices[d.ID] = len(out.Rows)
|
||||
nodes[d.ID] = d
|
||||
visitors[d.ID] = map[string]bool{}
|
||||
out.Rows = append(out.Rows, DepartmentStatistic{ID: d.ID, ParentID: d.ParentID, Name: d.Name, Region: d.Region})
|
||||
}
|
||||
branches := map[string][]string{}
|
||||
for _, person := range p.Members() {
|
||||
seen := map[string]bool{}
|
||||
for _, id := range person.DepartmentIDs {
|
||||
for id != "" && !seen[id] {
|
||||
node, ok := nodes[id]
|
||||
if !ok {
|
||||
return out, ErrUnavailable
|
||||
}
|
||||
seen[id] = true
|
||||
id = node.ParentID
|
||||
}
|
||||
}
|
||||
for id := range seen {
|
||||
out.Rows[indices[id]].TotalPeople++
|
||||
branches[person.PersonID] = append(branches[person.PersonID], id)
|
||||
}
|
||||
out.TotalPeople++
|
||||
}
|
||||
for _, a := range activity {
|
||||
if a.Visits <= 0 || a.LastAskedAt.IsZero() {
|
||||
return out, ErrInvalid
|
||||
}
|
||||
out.TotalVisits += a.Visits
|
||||
person := p.Resolve(a.StaffID, a.UnionID, a.Asker, a.Trigger)
|
||||
if len(branches[person.PersonID]) == 0 {
|
||||
out.UnmappedVisits += a.Visits
|
||||
continue
|
||||
}
|
||||
for _, id := range branches[person.PersonID] {
|
||||
row := &out.Rows[indices[id]]
|
||||
visitors[id][person.PersonID] = true
|
||||
row.Visits += a.Visits
|
||||
if row.LastAskedAt == nil || a.LastAskedAt.After(*row.LastAskedAt) {
|
||||
at := a.LastAskedAt
|
||||
row.LastAskedAt = &at
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range out.Rows {
|
||||
row := &out.Rows[i]
|
||||
row.Visitors = len(visitors[row.ID])
|
||||
if row.TotalPeople > 0 {
|
||||
percent := 100.0 * float64(row.Visitors) / float64(row.TotalPeople)
|
||||
row.Ratio = &percent
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package qaopsorg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDepartmentStatisticsDeduplicateSubtreePeopleAndUseCurrentMembership(t *testing.T) {
|
||||
directory := Directory{Departments: []Department{{ID: "root", Name: "Root"}, {ID: "child", ParentID: "root", Name: "Child"}, {ID: "former", ParentID: "root", Name: "Former"}, {ID: "current", ParentID: "root", Name: "Current"}, {ID: "empty", ParentID: "root", Name: "Empty"}}, People: []Person{{ID: "one", UnionID: "union-one", Name: "Fixture One", DepartmentIDs: []string{"root", "child"}}, {ID: "moved", Name: "Moved Fixture", DepartmentIDs: []string{"current"}}, {ID: "never", DepartmentIDs: []string{"child"}}}}
|
||||
p, err := BuildAttribution(State{State: "ready", DirectoryID: "fixture"}, directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
earlier := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
|
||||
later := earlier.Add(time.Hour)
|
||||
out, err := AggregateDepartmentStatistics(p, []DepartmentActivity{{StaffID: "one", Visits: 2, LastAskedAt: earlier}, {UnionID: "union-one", Visits: 1, LastAskedAt: later}, {Asker: "Moved Fixture", Visits: 4, LastAskedAt: earlier}, {StaffID: "not-in-current-directory", Visits: 5, LastAskedAt: later}})
|
||||
if err != nil {
|
||||
t.Fatal("current-roster subtree aggregation is missing", err)
|
||||
}
|
||||
rows := map[string]DepartmentStatistic{}
|
||||
for _, row := range out.Rows {
|
||||
rows[row.ID] = row
|
||||
}
|
||||
root, child, current, former, empty := rows["root"], rows["child"], rows["current"], rows["former"], rows["empty"]
|
||||
if root.TotalPeople != 3 || root.Visitors != 2 || root.Visits != 7 || root.Ratio == nil || *root.Ratio != 100.0*2/3 || root.LastAskedAt == nil || !root.LastAskedAt.Equal(later) {
|
||||
t.Fatalf("subtree parent duplicated or event counts lost: %+v", root)
|
||||
}
|
||||
if child.TotalPeople != 2 || child.Visitors != 1 || child.Visits != 3 || child.Ratio == nil || *child.Ratio != 50 {
|
||||
t.Fatalf("parent/child alias not deduplicated: %+v", child)
|
||||
}
|
||||
if current.Visitors != 1 || current.Visits != 4 || former.Visitors != 0 || former.Visits != 0 {
|
||||
t.Fatal("historical events were attributed to an old department")
|
||||
}
|
||||
if empty.TotalPeople != 0 || empty.Ratio != nil || empty.LastAskedAt != nil {
|
||||
t.Fatal("zero denominator fabricated a percentage/time")
|
||||
}
|
||||
if out.TotalVisits != 12 || out.UnmappedVisits != 5 || out.TotalPeople != 3 {
|
||||
t.Fatalf("unmatched events hidden or fake membership: %+v", out)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user