99d6109bef
build / build (push) Successful in 1m12s
按稳定身份展示零使用人员、个人次数与最近提问时间,直接部门去重计算兼属数量;保留原分支统计、配置和源问答。 验证:396项前端测试及类型检查通过;隔离PG全mirror/org竞态回归通过,个人/别名/同名/零使用/未关联与原部门汇总回归通过。 正式同镜像及上线验证尚未执行。
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package qaopsorg
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"sort"
|
|
"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 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 {
|
|
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{}, People: []DepartmentPersonStatistic{}}
|
|
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{}
|
|
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] {
|
|
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 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
|
|
}
|
|
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
|
|
}
|