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
+185
View File
@@ -0,0 +1,185 @@
package httpapi
import (
"context"
"errors"
"knowledge-governance/internal/dobotaiops"
"knowledge-governance/internal/qaopsorg"
"net/http"
"strings"
"time"
)
var errQAOrganization = errors.New("organization attribution unavailable")
type qaAttributionProvider interface {
GetAttribution(context.Context) (*qaopsorg.Attribution, error)
}
func recordAttribution(ctx context.Context, config Config) (*qaopsorg.Attribution, error) {
if config.QAOrg == nil {
return qaopsorg.BuildAttribution(qaopsorg.State{State: "empty"}, qaopsorg.Directory{})
}
provider, ok := config.QAOrg.(qaAttributionProvider)
if !ok {
return nil, errQAOrganization
}
p, err := provider.GetAttribution(ctx)
if err != nil || p == nil {
return nil, errQAOrganization
}
return p, nil
}
func attributionVersion(p *qaopsorg.Attribution) dobotaiops.OrganizationVersion {
return dobotaiops.OrganizationVersion{State: p.Metadata.State, DirectoryID: p.Metadata.DirectoryID, DirectorySource: p.Metadata.DirectorySource, SyncedAt: p.Metadata.DirectorySyncedAt, RevisionID: p.Metadata.RevisionID, Basis: "current_directory"}
}
func enrichQARecord(item *dobotaiops.Record, p *qaopsorg.Attribution) {
person := p.Resolve(item.Identity.StaffID, item.Identity.UnionID)
o := &dobotaiops.RecordOrganization{Version: attributionVersion(p), State: person.State, Departments: person.Departments, Region: person.Region, SourceDepartment: item.Department, SourceRegion: item.Region, DepartmentOrigin: "unavailable", RegionOrigin: "unavailable"}
if item.Department != "" {
o.DepartmentOrigin = "source_snapshot"
} else if len(person.Departments) > 0 {
item.Department = strings.Join(person.Departments, ";")
o.DepartmentOrigin = "current_directory"
}
if item.Region != "" {
o.RegionOrigin = "source_snapshot"
} else if person.Region != "" {
item.Region = person.Region
o.RegionOrigin = "current_directory"
}
item.Organization = o
}
func enrichQAPage(page *dobotaiops.RecordPage, p *qaopsorg.Attribution) {
version := attributionVersion(p)
page.Organization = &version
for i := range page.Items {
enrichQARecord(&page.Items[i], p)
}
for _, actor := range page.ScopeActors {
person := p.Resolve(actor.StaffID, actor.UnionID)
if len(person.Departments) > 0 {
dobotaiops.AddRecordColumns(page, "department")
}
if person.Region != "" {
dobotaiops.AddRecordColumns(page, "region")
}
}
}
type qaUsageReader interface {
ReadUsageGroups(context.Context, dobotaiops.RecordQuery) (dobotaiops.UsageGroups, error)
}
type qaUsageRow struct {
Key string `json:"key"`
Questions int64 `json:"questions"`
Succeeded int64 `json:"succeeded"`
Failed int64 `json:"failed"`
Other int64 `json:"other"`
Users *int `json:"users"`
RosterPeople *int `json:"roster_people"`
}
type qaUsage struct {
Organization dobotaiops.OrganizationVersion `json:"organization"`
AsOf time.Time `json:"as_of"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Keyword string `json:"keyword"`
Questions int64 `json:"questions"`
IdentifiedUsers int `json:"identified_users"`
Rows []qaUsageRow `json:"rows"`
}
func summarizeQAUsage(groups dobotaiops.UsageGroups, p *qaopsorg.Attribution, query dobotaiops.RecordQuery) qaUsage {
result := qaUsage{Organization: attributionVersion(p), AsOf: groups.AsOf, Start: query.Window.Start, End: query.Window.End, Keyword: query.Keyword, Rows: []qaUsageRow{}}
keys := append(append([]string{}, qaopsorg.RegionNames...), "unassigned", "cross_region", "unmatched", "identity_conflict", "no_identity", "directory_missing")
rows := map[string]*qaUsageRow{}
members := map[string]map[string]bool{}
all := map[string]bool{}
for _, key := range keys {
row := &qaUsageRow{Key: key}
if p.Metadata.State == "ready" {
var n int
valid := true
switch key {
case "unassigned":
n = p.Metadata.View.UnassignedPeople
case "cross_region":
n = p.Metadata.View.ConflictingPeople
default:
var ok bool
n, ok = p.Metadata.View.Regions[key]
valid = ok
}
if valid {
row.RosterPeople = &n
row.Users = new(int)
}
}
rows[key] = row
members[key] = map[string]bool{}
}
for _, group := range groups.Groups {
person := p.Resolve(group.Identity.StaffID, group.Identity.UnionID)
key := person.State
if person.Region != "" {
key = person.Region
}
row := rows[key]
if row == nil {
continue
}
row.Questions += group.Total
row.Succeeded += group.Succeeded
row.Failed += group.Failed
row.Other += group.Total - group.Succeeded - group.Failed
result.Questions += group.Total
if person.PersonID != "" {
members[key][person.PersonID] = true
all[person.PersonID] = true
}
}
for _, key := range keys {
row := rows[key]
if row.Users != nil {
*row.Users = len(members[key])
}
result.Rows = append(result.Rows, *row)
}
result.IdentifiedUsers = len(all)
return result
}
func qaOpsUsage(w http.ResponseWriter, r *http.Request, config Config) {
if !authorizeQAOps(w, r) {
return
}
reader, ok := config.QAOps.(qaUsageReader)
if !ok || config.QAOpsTenantKey == "" {
qaOpsError(w, dobotaiops.ErrUnavailable)
return
}
values := r.URL.Query()
for key, items := range values {
if (key != "start" && key != "end" && key != "keyword") || len(items) != 1 {
qaOpsError(w, dobotaiops.ErrInvalidQuery)
return
}
}
window, err := dobotaiops.NewWindow(config.QAOpsTenantKey, values.Get("start"), values.Get("end"), 90*24*time.Hour, config.Now())
if err != nil {
qaOpsError(w, err)
return
}
p, err := recordAttribution(r.Context(), config)
if err != nil {
qaOpsError(w, err)
return
}
query := dobotaiops.RecordQuery{Window: window, Keyword: values.Get("keyword")}
groups, err := reader.ReadUsageGroups(r.Context(), query)
if err != nil {
qaOpsError(w, err)
return
}
writeJSON(w, 200, map[string]any{"data": summarizeQAUsage(groups, p, query)})
}