155 lines
4.8 KiB
Go
155 lines
4.8 KiB
Go
package qaopsorg
|
|
|
|
import (
|
|
"errors"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
var ErrInvalid = errors.New("invalid organization data or region rules")
|
|
var RegionNames = []string{"国内工业", "APAC", "EMEA", "America"}
|
|
|
|
type Department struct {
|
|
ID string `json:"id"`
|
|
ParentID string `json:"parent_id"`
|
|
Name string `json:"name"`
|
|
}
|
|
type Person struct {
|
|
Name string `json:"name,omitempty"`
|
|
ID string `json:"id"`
|
|
UnionID string `json:"union_id,omitempty"`
|
|
DepartmentIDs []string `json:"department_ids"`
|
|
}
|
|
type Directory struct {
|
|
Departments []Department `json:"departments"`
|
|
People []Person `json:"people"`
|
|
}
|
|
type Rule struct {
|
|
DepartmentID string `json:"department_id"`
|
|
Region string `json:"region"`
|
|
}
|
|
type DepartmentView struct {
|
|
Department
|
|
Path string `json:"path"`
|
|
Region string `json:"region"`
|
|
Explicit bool `json:"explicit"`
|
|
InheritedFrom string `json:"inherited_from"`
|
|
}
|
|
type PreviewResult struct {
|
|
Departments []DepartmentView `json:"departments"`
|
|
Regions map[string]int `json:"regions"`
|
|
TotalPeople int `json:"total_people"`
|
|
UnassignedPeople int `json:"unassigned_people"`
|
|
ConflictingPeople int `json:"conflicting_people"`
|
|
MissingDepartments []string `json:"missing_departments"`
|
|
}
|
|
|
|
// Identity is department ID. Names and legacy keyword rules never decide ownership.
|
|
func Preview(directory Directory, rules []Rule) (PreviewResult, error) {
|
|
return PreviewRegions(directory, rules, DefaultRegions())
|
|
}
|
|
func PreviewRegions(directory Directory, rules []Rule, regions []RegionDefinition) (PreviewResult, error) {
|
|
result := PreviewResult{Departments: []DepartmentView{}, Regions: map[string]int{}, MissingDepartments: []string{}, TotalPeople: len(directory.People)}
|
|
if len(directory.Departments) == 0 || len(directory.Departments) > 20000 || len(directory.People) == 0 || len(directory.People) > 100000 || len(rules) > 20000 {
|
|
return result, ErrInvalid
|
|
}
|
|
if !validRegions(regions) {
|
|
return result, ErrInvalid
|
|
}
|
|
for _, region := range regions {
|
|
result.Regions[region.ID] = 0
|
|
}
|
|
departments := make(map[string]Department, len(directory.Departments))
|
|
for _, d := range directory.Departments {
|
|
if !validID(d.ID) || d.Name == "" || len(d.Name) > 1000 || strings.ContainsRune(d.Name, 0) {
|
|
return result, ErrInvalid
|
|
}
|
|
if _, ok := departments[d.ID]; ok {
|
|
return result, ErrInvalid
|
|
}
|
|
departments[d.ID] = d
|
|
}
|
|
direct := map[string]string{}
|
|
for _, rule := range rules {
|
|
if !validID(rule.DepartmentID) {
|
|
return result, ErrInvalid
|
|
}
|
|
if _, ok := result.Regions[rule.Region]; !ok {
|
|
return result, ErrInvalid
|
|
}
|
|
if _, ok := direct[rule.DepartmentID]; ok {
|
|
return result, ErrInvalid
|
|
}
|
|
direct[rule.DepartmentID] = rule.Region
|
|
if _, ok := departments[rule.DepartmentID]; !ok {
|
|
result.MissingDepartments = append(result.MissingDepartments, rule.DepartmentID)
|
|
}
|
|
}
|
|
resolved := map[string]DepartmentView{}
|
|
for _, d := range directory.Departments {
|
|
seen := map[string]bool{}
|
|
chain := []string{}
|
|
region, from := "", ""
|
|
for cur := d.ID; cur != ""; {
|
|
node, ok := departments[cur]
|
|
if !ok || seen[cur] || len(chain) > 100 {
|
|
return result, ErrInvalid
|
|
}
|
|
seen[cur] = true
|
|
chain = append(chain, node.Name)
|
|
if region == "" && direct[cur] != "" {
|
|
region, from = direct[cur], cur
|
|
}
|
|
cur = node.ParentID
|
|
}
|
|
for left, right := 0, len(chain)-1; left < right; left, right = left+1, right-1 {
|
|
chain[left], chain[right] = chain[right], chain[left]
|
|
}
|
|
view := DepartmentView{Department: d, Path: strings.Join(chain, "/"), Region: region, Explicit: direct[d.ID] != ""}
|
|
if from != d.ID {
|
|
view.InheritedFrom = from
|
|
}
|
|
resolved[d.ID] = view
|
|
result.Departments = append(result.Departments, view)
|
|
}
|
|
people := map[string]bool{}
|
|
for _, person := range directory.People {
|
|
if !validID(person.ID) || people[person.ID] || len(person.DepartmentIDs) > 1000 || len(person.Name) > 1000 || strings.ContainsRune(person.Name, 0) {
|
|
return result, ErrInvalid
|
|
}
|
|
people[person.ID] = true
|
|
regions := map[string]bool{}
|
|
for _, id := range person.DepartmentIDs {
|
|
department, ok := resolved[id]
|
|
if !ok {
|
|
return result, ErrInvalid
|
|
}
|
|
if department.Region != "" {
|
|
regions[department.Region] = true
|
|
}
|
|
}
|
|
switch len(regions) {
|
|
case 0:
|
|
result.UnassignedPeople++
|
|
case 1:
|
|
for region := range regions {
|
|
result.Regions[region]++
|
|
}
|
|
default:
|
|
result.ConflictingPeople++
|
|
}
|
|
}
|
|
sort.Slice(result.Departments, func(i, j int) bool {
|
|
a, b := result.Departments[i], result.Departments[j]
|
|
if a.Path == b.Path {
|
|
return a.ID < b.ID
|
|
}
|
|
return a.Path < b.Path
|
|
})
|
|
sort.Strings(result.MissingDepartments)
|
|
return result, nil
|
|
}
|
|
func validID(value string) bool {
|
|
return value != "" && len(value) <= 256 && strings.TrimSpace(value) == value && !strings.ContainsAny(value, "\x00\r\n")
|
|
}
|