6e67034fbd
build / build (push) Successful in 1m10s
以治理库单一只读事务分开聚合累计、自然周、截止月及当前通讯录使用分母;浏览器不再读取全量长答案来构建看板。 新增迁移52与普通worker周质量冻结,保留真实冻结时点、规则和标量贡献;缺少当时快照留空。提交后冻结头与贡献不可修改、删除或追加;只读接口保留管理员、固定租户、严格JSON与输入冲突保护。 已验证:make verify退出0,686个Go用例和305个前端用例通过,必需跳过0;镜像与组织隔离PostgreSQL竞态回归退出0。真实治理副本浏览器核对第41周累计1885与新增471、十一项和完整PNG下载,390px无外溢、JS异常0;明确模拟503后不伪零且重试恢复。没有修改DobotAI原始问答、真实区域规则或生产发布门禁。
216 lines
7.4 KiB
Go
216 lines
7.4 KiB
Go
package qaopsmirror
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/jackc/pgx/v5"
|
|
"knowledge-governance/internal/dobotaiops"
|
|
"knowledge-governance/internal/qaopsorg"
|
|
"knowledge-governance/internal/qaopsquality"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
type dashboardRecord struct {
|
|
id, hash string
|
|
at time.Time
|
|
identity dobotaiops.ActorIdentity
|
|
}
|
|
|
|
func dashboardRate(n, d int) qaopsquality.Rate {
|
|
r := qaopsquality.Rate{Numerator: n, Denominator: d}
|
|
if d > 0 {
|
|
p := 100 * float64(n) / float64(d)
|
|
r.Percent = &p
|
|
}
|
|
return r
|
|
}
|
|
func dashboardCategories(counts map[string]int) []DashboardCount {
|
|
out := []DashboardCount{}
|
|
for label, count := range counts {
|
|
out = append(out, DashboardCount{Label: label, Count: count})
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].Count == out[j].Count {
|
|
return out[i].Label < out[j].Label
|
|
}
|
|
return out[i].Count > out[j].Count
|
|
})
|
|
return out
|
|
}
|
|
func dashboardCategory(value string) string {
|
|
if value = strings.TrimSpace(value); value != "" {
|
|
return value
|
|
}
|
|
return "未分类"
|
|
}
|
|
|
|
// ReadDashboard never fetches answer payloads or opens a source connection. All
|
|
// scopes, quality inputs and organizational attribution use ONE local snapshot.
|
|
func (s *Store) ReadDashboard(ctx context.Context, q dobotaiops.RecordQuery) (Dashboard, error) {
|
|
out := Dashboard{Start: q.Window.Start, End: q.Window.End, Keyword: q.Keyword, IssueTypes: []DashboardCount{}, Products: []DashboardCount{}, Daily: []DashboardDaily{}, Regions: []DashboardRegion{}, FrozenWeeks: []DashboardFrozen{}}
|
|
if !s.valid() {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
if q.Window.TenantKey != s.tenant || !utf8.ValidString(q.Keyword) || utf8.RuneCountInString(q.Keyword) > 200 || strings.ContainsRune(q.Keyword, 0) {
|
|
return out, dobotaiops.ErrInvalidQuery
|
|
}
|
|
if _, e := dobotaiops.NewWindow(s.tenant, q.Window.Start.Format(time.RFC3339Nano), q.Window.End.Format(time.RFC3339Nano), 90*24*time.Hour, time.Now()); e != nil {
|
|
return out, e
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
|
defer cancel()
|
|
tx, e := s.pool.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
|
|
if e != nil {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
defer func() { _ = tx.Rollback(context.Background()) }()
|
|
state, e := s.readState(ctx, txQuery{tx})
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
if state.Blocked {
|
|
return out, dobotaiops.ErrNotReadOnly
|
|
}
|
|
if !state.Ready || state.SourceObservedAt == nil {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
out.AsOf = *state.SourceObservedAt
|
|
out.LocalRevision = state.Revision
|
|
attribution, e := qaopsorg.NewStore(s.pool, s.tenant).GetAttributionInTx(ctx, tx)
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
meta := attribution.Metadata
|
|
out.Organization = dobotaiops.OrganizationVersion{State: meta.State, DirectoryID: meta.DirectoryID, DirectorySource: meta.DirectorySource, SyncedAt: meta.DirectorySyncedAt, RevisionID: meta.RevisionID, Basis: "current_directory", RegionNames: meta.RegionLabels()}
|
|
out.MonthStart, out.WeekStart = dashboardPeriods(q.Window.End)
|
|
var first *time.Time
|
|
if e = tx.QueryRow(ctx, `SELECT count(*),LEAST((SELECT min(asked_at) FROM qa_ops_mirror_records WHERE tenant_key=$1 AND asked_at<$2),(SELECT first_data_at FROM qa_ops_weekly_quality_tracking WHERE tenant_key=$1),(SELECT min(week_start) FROM qa_ops_weekly_quality_snapshots WHERE tenant_key=$1 AND week_start<$2)) FROM qa_ops_mirror_records WHERE tenant_key=$1 AND asked_at<$2 AND ($3='' OR position(lower($3) IN lower(question))>0)`, s.tenant, q.Window.End, q.Keyword).Scan(&out.TotalIssues, &first); e != nil {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
lower := q.Window.Start
|
|
if out.MonthStart.Before(lower) {
|
|
lower = out.MonthStart
|
|
}
|
|
if out.WeekStart.Before(lower) {
|
|
lower = out.WeekStart
|
|
}
|
|
rows, e := tx.Query(ctx, `SELECT id,asked_at,identity,quality_hash FROM qa_ops_mirror_records WHERE tenant_key=$1 AND asked_at>=$2 AND asked_at<$3 AND ($4='' OR position(lower($4) IN lower(question))>0) ORDER BY id LIMIT 100001`, s.tenant, lower, q.Window.End, q.Keyword)
|
|
if e != nil {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
records := []dashboardRecord{}
|
|
keys := []dobotaiops.QualityKey{}
|
|
for rows.Next() {
|
|
var r dashboardRecord
|
|
var identity []byte
|
|
if rows.Scan(&r.id, &r.at, &identity, &r.hash) != nil || json.Unmarshal(identity, &r.identity) != nil {
|
|
rows.Close()
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
records = append(records, r)
|
|
if len(records) > dobotaiops.MaxMirrorRecords {
|
|
rows.Close()
|
|
return out, dobotaiops.ErrInvalidQuery
|
|
}
|
|
if !r.at.Before(q.Window.Start) {
|
|
keys = append(keys, dobotaiops.QualityKey{ID: r.id, InputHash: r.hash})
|
|
}
|
|
}
|
|
e = rows.Err()
|
|
rows.Close()
|
|
if e != nil {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
views := map[string]qaopsquality.View{}
|
|
quality := qaopsquality.NewStore(s.pool, s.tenant)
|
|
for begin := 0; begin < len(keys); begin += 5000 {
|
|
end := begin + 5000
|
|
if end > len(keys) {
|
|
end = len(keys)
|
|
}
|
|
batch, err := quality.ReadViewsInTx(ctx, tx, keys[begin:end])
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
for id, v := range batch {
|
|
views[id] = v
|
|
}
|
|
}
|
|
out.Quality = qaopsquality.Summarize(views)
|
|
products, types, daily := map[string]int{}, map[string]int{}, map[string]int{}
|
|
for day := out.WeekStart; day.Before(q.Window.End); day = day.AddDate(0, 0, 1) {
|
|
daily[day.Format("2006-01-02")] = 0
|
|
}
|
|
regionIndex := map[string]int{}
|
|
users := map[string]map[string]bool{}
|
|
for _, region := range meta.RegionDefinitions() {
|
|
r := DashboardRegion{ID: region.ID, Name: region.Name}
|
|
if meta.State == "ready" {
|
|
n := meta.View.Regions[region.ID]
|
|
r.RosterPeople = &n
|
|
r.Users = new(int)
|
|
}
|
|
regionIndex[region.Name] = len(out.Regions)
|
|
users[region.Name] = map[string]bool{}
|
|
out.Regions = append(out.Regions, r)
|
|
}
|
|
for _, r := range records {
|
|
if !r.at.Before(out.WeekStart) {
|
|
out.NewThisWeek++
|
|
daily[r.at.In(dashboardLocation).Format("2006-01-02")]++
|
|
}
|
|
if !r.at.Before(q.Window.Start) {
|
|
v, ok := views[r.id]
|
|
if !ok {
|
|
return out, qaopsquality.ErrUnavailable
|
|
}
|
|
products[dashboardCategory(v.Effective.ProductCategory)]++
|
|
types[dashboardCategory(v.Effective.ProblemType)]++
|
|
}
|
|
if !r.at.Before(out.MonthStart) {
|
|
person := attribution.Resolve(r.identity.StaffID, r.identity.UnionID)
|
|
if index, ok := regionIndex[person.Region]; ok && person.PersonID != "" {
|
|
out.Regions[index].Questions++
|
|
users[person.Region][person.PersonID] = true
|
|
}
|
|
}
|
|
}
|
|
out.Products = dashboardCategories(products)
|
|
out.IssueTypes = dashboardCategories(types)
|
|
for day := out.WeekStart; day.Before(q.Window.End); day = day.AddDate(0, 0, 1) {
|
|
date := day.Format("2006-01-02")
|
|
out.Daily = append(out.Daily, DashboardDaily{Date: date, Count: daily[date]})
|
|
}
|
|
for i := range out.Regions {
|
|
r := &out.Regions[i]
|
|
if r.Users != nil {
|
|
*r.Users = len(users[r.Name])
|
|
if *r.Users > *r.RosterPeople {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
r.UsageCoverage = dashboardRate(*r.Users, *r.RosterPeople)
|
|
if *r.RosterPeople > 0 {
|
|
frequency := float64(r.Questions) / float64(*r.RosterPeople)
|
|
r.Frequency = &frequency
|
|
}
|
|
}
|
|
}
|
|
if first != nil {
|
|
snapshots, err := s.readFrozenDashboard(ctx, tx, q.Keyword, q.Window.End)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.FrozenWeeks = dashboardFrozenSeries(*first, q.Window.End, snapshots)
|
|
if len(out.FrozenWeeks) > 0 && out.FrozenWeeks[len(out.FrozenWeeks)-1].WeekEnd.Before(q.Window.End) {
|
|
return out, dobotaiops.ErrInvalidQuery
|
|
}
|
|
}
|
|
if tx.Commit(ctx) != nil {
|
|
return out, dobotaiops.ErrUnavailable
|
|
}
|
|
return out, nil
|
|
}
|