feat(qaops): 完成区域统计与部门绑定六项联动
build / build (push) Successful in 1m20s

新增北京时间自然周校验与缓存隔离,默认展开到业务二级部门。
选中部门在统计页就地绑定;区域名称列表原位增删改,双版本锁、自动预览、独立读回及删除确认。
筛选和两张区域看板使用同一已保存目录,删除失效筛选回全部。

验证:完整 make verify、全 mirror 隔离 PostgreSQL race、自然周边界与缓存键负例通过;真实恢复库327部门逐行对拍及六项浏览器验收通过。
schema57无迁移,不改真实公司区域绑定或原始问答,不连接DobotAI生产源库;PNG导出本轮不冒充已验证。
This commit is contained in:
姚章浩
2026-10-19 23:41:01 +08:00
parent 9470a127f7
commit 154b0b62ad
18 changed files with 933 additions and 560 deletions
@@ -11,6 +11,7 @@ import (
type DepartmentStatisticsQuery struct {
Period string `json:"period"`
Month string `json:"month,omitempty"`
Week string `json:"week,omitempty"`
}
type DepartmentStatisticsSnapshot struct {
Regions []qaopsorg.RegionDefinition `json:"regions"`
@@ -18,6 +19,7 @@ type DepartmentStatisticsSnapshot struct {
Organization dobotaiops.OrganizationVersion `json:"organization"`
Period string `json:"period"`
Month string `json:"month"`
Week string `json:"week"`
Start *time.Time `json:"start"`
End time.Time `json:"end"`
GeneratedAt time.Time `json:"generated_at"`
@@ -8,14 +8,36 @@ import (
"github.com/jackc/pgx/v5"
"knowledge-governance/internal/dobotaiops"
"knowledge-governance/internal/qaopsorg"
"strconv"
"time"
)
func (q DepartmentStatisticsQuery) Bounds(now time.Time) (*time.Time, time.Time, error) {
if q.Period == "all" && q.Month == "" {
if q.Period == "all" && q.Month == "" && q.Week == "" {
return nil, now, nil
}
if q.Period != "month" {
if q.Period == "week" && q.Month == "" {
if len(q.Week) != 8 || q.Week[4:6] != "-W" || q.Week[6] < '0' || q.Week[6] > '9' || q.Week[7] < '0' || q.Week[7] > '9' {
return nil, time.Time{}, dobotaiops.ErrInvalidQuery
}
year, ey := strconv.Atoi(q.Week[:4])
week, ew := strconv.Atoi(q.Week[6:])
if ey != nil || ew != nil || year < 2000 || year > 9998 || week < 1 || week > 53 {
return nil, time.Time{}, dobotaiops.ErrInvalidQuery
}
jan4 := time.Date(year, time.January, 4, 0, 0, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600))
start := jan4.AddDate(0, 0, -(int(jan4.Weekday())+6)%7+7*(week-1))
actualYear, actualWeek := start.ISOWeek()
if actualYear != year || actualWeek != week || start.After(now) {
return nil, time.Time{}, dobotaiops.ErrInvalidQuery
}
end := start.AddDate(0, 0, 7)
if end.After(now) {
end = now
}
return &start, end, nil
}
if q.Period != "month" || q.Week != "" {
return nil, time.Time{}, dobotaiops.ErrInvalidQuery
}
zone := time.FixedZone("Asia/Shanghai", 8*3600)
@@ -42,7 +64,7 @@ func (s *Store) ReadDepartmentStatistics(ctx context.Context, q DepartmentStatis
if err != nil || directory == "" {
return DepartmentStatisticsResult{}, dobotaiops.ErrUnavailable
}
raw, _ := json.Marshal([]string{s.tenant, directory, revision, q.Period, q.Month})
raw, _ := json.Marshal([]string{s.tenant, directory, revision, q.Period, q.Month, q.Week})
digest := sha256.Sum256(raw)
key := hex.EncodeToString(digest[:])
return s.departmentStats.get(key, now, func(background context.Context) (DepartmentStatisticsSnapshot, error) {
@@ -50,7 +72,7 @@ func (s *Store) ReadDepartmentStatistics(ctx context.Context, q DepartmentStatis
})
}
func (s *Store) loadDepartmentStatistics(ctx context.Context, q DepartmentStatisticsQuery, start *time.Time, end time.Time, directory, revision string) (DepartmentStatisticsSnapshot, error) {
out := DepartmentStatisticsSnapshot{Period: q.Period, Month: q.Month, Start: start, End: end}
out := DepartmentStatisticsSnapshot{Period: q.Period, Month: q.Month, Week: q.Week, Start: start, End: end}
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
if err != nil {
return out, dobotaiops.ErrUnavailable
@@ -0,0 +1,56 @@
package qaopsmirror
import (
"context"
"encoding/json"
"knowledge-governance/internal/qaopsorg"
"testing"
"time"
)
func TestDepartmentStatisticsNaturalWeekBoundaryAndCacheIsolation(t *testing.T) {
p := mirrorFixture(t)
s := NewStore(p, "fixture-department-natural-week")
now, _ := time.Parse(time.RFC3339, "2026-10-21T11:00:00+08:00")
ctx := context.Background()
dir := qaopsorg.Directory{Departments: []qaopsorg.Department{{ID: "root", Name: "Fixture center"}, {ID: "child", ParentID: "root", Name: "Fixture department"}}, People: []qaopsorg.Person{{ID: "p1", Name: "Fixture person", DepartmentIDs: []string{"root", "child"}}}}
if _, err := qaopsorg.NewStore(p, s.tenant).ReplaceDirectory(ctx, qaopsorg.Import{Source: "weekly_report_snapshot", SyncedAt: time.Now(), Complete: true, Directory: dir}); err != nil {
t.Fatal(err)
}
for i, stamp := range []string{"2026-10-11T23:59:59+08:00", "2026-10-12T00:00:00+08:00", "2026-10-18T23:59:59+08:00", "2026-10-19T00:00:00+08:00"} {
_, err := p.Exec(ctx, `INSERT INTO qa_ops_master_records(tenant_key,id,event_key,origin,asked_at,question,status,identity,quality_hash,payload)VALUES($1,$2,repeat('a',64),'dobotai_2_0',$3,'non-business boundary fixture','succeeded','{"staff_id":"p1"}',repeat('b',64),jsonb_build_object('id',$2::text))`, s.tenant, "boundary-"+time.Date(2000, 1, i+1, 0, 0, 0, 0, time.UTC).Format("02"), stamp)
if err != nil {
t.Fatal(err)
}
}
read := func(week string) DepartmentStatisticsResult {
var q DepartmentStatisticsQuery
raw, _ := json.Marshal(map[string]string{"period": "week", "week": week})
_ = json.Unmarshal(raw, &q)
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
r, err := s.ReadDepartmentStatistics(ctx, q, now)
if err != nil {
t.Fatal(err)
}
if r.State == "ready" {
return r
}
time.Sleep(time.Millisecond)
}
t.Fatal("background natural week did not finish")
return DepartmentStatisticsResult{}
}
first, second := read("2026-W42"), read("2026-W43")
if first.CacheKey == second.CacheKey || first.Snapshot.TotalVisits != 2 || second.Snapshot.TotalVisits != 1 {
t.Fatalf("week boundaries/cache contaminated: first=%+v second=%+v", first, second)
}
for _, r := range first.Snapshot.Rows {
if r.Visits != 2 || r.Visitors != 1 || r.TotalPeople != 1 {
t.Fatalf("current branch person was double-counted: %+v", r)
}
}
if repeated := read("2026-W42"); !repeated.CacheHit || repeated.Snapshot.GeneratedAt != first.Snapshot.GeneratedAt {
t.Fatal("same natural week was not cached")
}
}
@@ -0,0 +1,35 @@
package qaopsmirror
import (
"encoding/json"
"testing"
"time"
)
func TestDepartmentStatisticsNaturalWeekUsesBeijingISOCalendar(t *testing.T) {
now, _ := time.Parse(time.RFC3339, "2026-10-21T11:00:00+08:00")
for _, tc := range []struct{ raw, start, end string }{
{`{"period":"week","week":"2026-W43"}`, "2026-10-19T00:00:00+08:00", "2026-10-21T11:00:00+08:00"},
{`{"period":"week","week":"2026-W42"}`, "2026-10-12T00:00:00+08:00", "2026-10-19T00:00:00+08:00"},
{`{"period":"week","week":"2020-W53"}`, "2020-12-28T00:00:00+08:00", "2021-01-04T00:00:00+08:00"},
} {
var q DepartmentStatisticsQuery
if json.Unmarshal([]byte(tc.raw), &q) != nil {
t.Fatal("invalid test input")
}
start, end, err := q.Bounds(now)
if err != nil || start == nil || start.Format(time.RFC3339) != tc.start || end.Format(time.RFC3339) != tc.end {
t.Fatalf("natural week bounds incorrect: %s start=%v end=%v error=%v", tc.raw, start, end, err)
}
}
}
func TestDepartmentStatisticsRejectsInvalidMixedOrFutureWeeks(t *testing.T) {
now, _ := time.Parse(time.RFC3339, "2026-10-21T11:00:00+08:00")
for _, raw := range []string{`{"period":"week"}`, `{"period":"week","week":"2021-W53"}`, `{"period":"week","week":"2026-W00"}`, `{"period":"week","week":"2026-W44"}`, `{"period":"week","week":"2026-W1"}`, `{"period":"week","week":"2026-W+1"}`, `{"period":"week","week":"1999-W01"}`, `{"period":"week","week":"2026-W43","month":"2026-10"}`, `{"period":"all","week":"2026-W43"}`, `{"period":"month","month":"2026-10","week":"2026-W43"}`} {
var q DepartmentStatisticsQuery
_ = json.Unmarshal([]byte(raw), &q)
if _, _, err := q.Bounds(now); err == nil {
t.Fatal("invalid period accepted", raw)
}
}
}