feat(qaops): 接入问答组织补全与区域使用统计

This commit is contained in:
姚章浩
2026-10-17 19:02:13 +08:00
parent cc4906b6a9
commit 56b8cabed9
23 changed files with 1182 additions and 71 deletions
+109
View File
@@ -0,0 +1,109 @@
package qaopsorg
import (
"context"
"sort"
)
type PersonAttribution struct {
State string `json:"state"`
PersonID string `json:"-"`
Departments []string `json:"departments"`
Region string `json:"region"`
}
type Attribution struct {
Metadata State
people map[string]PersonAttribution
unions map[string]string
}
func BuildAttribution(state State, directory Directory) (*Attribution, error) {
p := &Attribution{Metadata: state, people: map[string]PersonAttribution{}, unions: map[string]string{}}
if state.State == "empty" {
return p, nil
}
if state.State != "ready" || state.DirectoryID == "" {
return nil, ErrUnavailable
}
view, err := Preview(directory, state.Rules)
if err != nil {
return nil, ErrUnavailable
}
p.Metadata.View = view
departments := map[string]DepartmentView{}
for _, d := range view.Departments {
departments[d.ID] = d
}
for _, person := range directory.People {
paths, regions := map[string]bool{}, map[string]bool{}
for _, id := range person.DepartmentIDs {
d := departments[id]
paths[d.Path] = true
if d.Region != "" {
regions[d.Region] = true
}
}
item := PersonAttribution{State: "unassigned", PersonID: person.ID, Departments: []string{}}
for path := range paths {
item.Departments = append(item.Departments, path)
}
sort.Strings(item.Departments)
if len(regions) == 1 {
item.State = "matched"
for region := range regions {
item.Region = region
}
} else if len(regions) > 1 {
item.State = "cross_region"
}
p.people[person.ID] = item
if person.UnionID != "" {
if !validID(person.UnionID) {
return nil, ErrUnavailable
}
if _, ok := p.unions[person.UnionID]; ok {
p.unions[person.UnionID] = ""
} else {
p.unions[person.UnionID] = person.ID
}
}
}
return p, nil
}
func (p *Attribution) Resolve(staff, union string) PersonAttribution {
none := PersonAttribution{State: "unmatched", Departments: []string{}}
if p == nil || p.Metadata.State != "ready" {
none.State = "directory_missing"
return none
}
if staff == "" && union == "" {
none.State = "no_identity"
return none
}
byUnion, unionKnown := p.unions[union]
if person, ok := p.people[staff]; ok {
if unionKnown && (byUnion == "" || byUnion != staff) {
none.State = "identity_conflict"
return none
}
return person
}
if unionKnown {
if byUnion == "" {
none.State = "identity_conflict"
return none
}
return p.people[byUnion]
}
return none
}
func (s *Store) GetAttribution(ctx context.Context) (*Attribution, error) {
if !s.available() {
return nil, ErrUnavailable
}
state, directory, err := s.read(ctx, s.pool, false)
if err != nil {
return nil, err
}
return BuildAttribution(state, directory)
}
+46
View File
@@ -0,0 +1,46 @@
package qaopsorg
import (
"context"
"errors"
"testing"
)
func TestAttributionUsesStableIdentityAndKeepsAmbiguitiesExplicit(t *testing.T) {
d := Directory{Departments: []Department{{ID: "root", Name: "fixture"}, {ID: "a", ParentID: "root", Name: "one"}, {ID: "b", ParentID: "root", Name: "two"}}, People: []Person{{ID: "staff-a", UnionID: "union-a", DepartmentIDs: []string{"a", "a"}}, {ID: "staff-cross", UnionID: "union-cross", DepartmentIDs: []string{"a", "b"}}, {ID: "duplicate-one", UnionID: "duplicate", DepartmentIDs: []string{"a"}}, {ID: "duplicate-two", UnionID: "duplicate", DepartmentIDs: []string{"b"}}}}
p, err := BuildAttribution(State{State: "ready", DirectoryID: "d", RevisionID: "r", Rules: []Rule{{DepartmentID: "root", Region: "国内工业"}, {DepartmentID: "b", Region: "APAC"}}}, d)
if err != nil {
t.Fatal(err)
}
for _, key := range [][2]string{{"staff-a", ""}, {"", "union-a"}} {
got := p.Resolve(key[0], key[1])
if got.State != "matched" || got.Region != "国内工业" || len(got.Departments) != 1 || got.PersonID != "staff-a" {
t.Fatalf("stable identity not resolved/deduplicated: %+v", got)
}
}
if got := p.Resolve("staff-cross", ""); got.State != "cross_region" || got.Region != "" || len(got.Departments) != 2 {
t.Fatalf("invented primary region: %+v", got)
}
if got := p.Resolve("", "duplicate"); got.State != "identity_conflict" {
t.Fatalf("duplicate union selected first person: %+v", got)
}
if got := p.Resolve("staff-a", "union-cross"); got.State != "identity_conflict" {
t.Fatalf("contradictory identifiers silently preferred: %+v", got)
}
if got := p.Resolve("absent", ""); got.State != "unmatched" {
t.Fatalf("unknown identity inferred: %+v", got)
}
if got := p.Resolve("", ""); got.State != "no_identity" {
t.Fatalf("anonymous identity inferred: %+v", got)
}
}
func TestAttributionDoesNotTreatMissingDirectoryAsPermissionSuccess(t *testing.T) {
p, err := BuildAttribution(State{State: "empty"}, Directory{})
if err != nil || p.Resolve("staff", "").State != "directory_missing" {
t.Fatal("missing directory not explicit")
}
var store *Store
if _, err = store.GetAttribution(context.Background()); !errors.Is(err, ErrUnavailable) {
t.Fatal("unavailable store returned an empty directory")
}
}
@@ -62,6 +62,10 @@ func TestOrganizationStoreRoundTripAndOptimisticConflict(t *testing.T) {
if err != nil || read.RevisionID != saved.RevisionID || read.View.Regions["国内工业"] != 1 {
t.Fatal("saved rules not consumed by read path")
}
projection, projectionErr := store.GetAttribution(ctx)
if projectionErr != nil || projection.Metadata.DirectoryID != read.DirectoryID || projection.Metadata.RevisionID != read.RevisionID || projection.Resolve("u2", "").Region != "APAC" {
t.Fatal("attribution did not consume the saved directory/region revision")
}
if _, err = store.SaveRules(ctx, request, "stale-admin"); !errors.Is(err, qaopsorg.ErrConflict) {
t.Fatalf("stale update overwrote newer rules: %v", err)
}