Files
knowledge-governance/internal/qaopsorg/attribution.go
T
姚章浩 6e67034fbd
build / build (push) Successful in 1m10s
feat(qa-ops): 交付十一项统计看板与完整图片导出
以治理库单一只读事务分开聚合累计、自然周、截止月及当前通讯录使用分母;浏览器不再读取全量长答案来构建看板。

新增迁移52与普通worker周质量冻结,保留真实冻结时点、规则和标量贡献;缺少当时快照留空。提交后冻结头与贡献不可修改、删除或追加;只读接口保留管理员、固定租户、严格JSON与输入冲突保护。

已验证:make verify退出0,686个Go用例和305个前端用例通过,必需跳过0;镜像与组织隔离PostgreSQL竞态回归退出0。真实治理副本浏览器核对第41周累计1885与新增471、十一项和完整PNG下载,390px无外溢、JS异常0;明确模拟503后不伪零且重试恢复。没有修改DobotAI原始问答、真实区域规则或生产发布门禁。
2026-10-18 23:26:55 +08:00

135 lines
3.5 KiB
Go

package qaopsorg
import (
"context"
"github.com/jackc/pgx/v5"
"sort"
)
type PersonAttribution struct {
Name string `json:"name"`
DepartmentIDs []string `json:"-"`
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 (p *Attribution) Members() []PersonAttribution {
result := []PersonAttribution{}
if p == nil {
return result
}
for _, person := range p.people {
result = append(result, person)
}
sort.Slice(result, func(i, j int) bool { return result[i].PersonID < result[j].PersonID })
return result
}
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 := PreviewRegions(directory, state.Rules, state.RegionDefinitions())
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{Name: person.Name, DepartmentIDs: append([]string{}, person.DepartmentIDs...), 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 = state.RegionLabel(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) GetAttributionInTx(ctx context.Context, tx pgx.Tx) (*Attribution, error) {
if !s.available() {
return nil, ErrUnavailable
}
state, directory, err := s.read(ctx, tx, false)
if err != nil {
return nil, err
}
return BuildAttribution(state, directory)
}
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)
}