Files
knowledge-governance/internal/dobotaiops/organization_records.go
T
姚章浩 4b3aaff321 feat(qa-ops): 完成复检留档与人工复核闭环
治理侧保存输入观测版本、自动复检及人工复核历史,输入变化失效、版本冲突和幂等重放不覆盖人工记录。批量质量数量使用完整筛选元数据,未采集原文不伪造快照,延迟旧扫描不回退新输入。

验证:全仓 make verify 退出码0,Go656及前端253通过;五包竞态、源/组织/质量隔离PG、真实只读源和隔离网页读回复核通过。默认91个可选集成跳过不计通过。未接AI模型,不写DobotAI源库,未推送、未合并、未部署。
2026-10-17 22:37:26 +08:00

107 lines
3.5 KiB
Go

package dobotaiops
import (
"context"
"encoding/json"
"time"
)
// Actor keys are internal join material, never fields of the browser response.
type ActorIdentity struct {
ActorID string `json:"actor_id"`
StaffID string `json:"staff_id"`
UnionID string `json:"union_id"`
}
type OrganizationVersion struct {
State string `json:"state"`
DirectoryID string `json:"directory_id"`
DirectorySource string `json:"directory_source"`
SyncedAt *time.Time `json:"synced_at"`
RevisionID string `json:"revision_id"`
Basis string `json:"basis"`
}
type RecordOrganization struct {
Version OrganizationVersion `json:"version"`
State string `json:"state"`
Departments []string `json:"departments"`
Region string `json:"region"`
SourceDepartment string `json:"source_department"`
SourceRegion string `json:"source_region"`
DepartmentOrigin string `json:"department_origin"`
RegionOrigin string `json:"region_origin"`
}
type UsageGroup struct {
Identity ActorIdentity `json:"-"`
Total int64
Succeeded int64
Failed int64
}
type UsageGroups struct {
Groups []UsageGroup `json:"-"`
AsOf time.Time
}
func decodeRecord(raw string) (Record, error) {
var wire struct {
Record
Identity ActorIdentity `json:"_identity"`
QualityHash string `json:"_quality_hash"`
QualityObservedAt time.Time `json:"_quality_observed_at"`
}
if err := json.Unmarshal([]byte(raw), &wire); err != nil {
return Record{}, err
}
wire.Record.Identity = wire.Identity
wire.Record.QualityInputHash = wire.QualityHash
wire.Record.QualityObservedAt = wire.QualityObservedAt
wire.Record.Coverage = CheckAnswerCoverage(wire.Record.Answer)
return wire.Record, nil
}
func (c *Client) ReadUsageGroups(ctx context.Context, query RecordQuery) (UsageGroups, error) {
if err := c.validateQueryWindow(query.Window); err != nil {
return UsageGroups{}, err
}
if query.Cursor != "" {
return UsageGroups{}, ErrInvalidQuery
}
pattern, patternErr := recordKeywordPattern(query.Keyword)
if patternErr != nil {
return UsageGroups{}, patternErr
}
result := UsageGroups{Groups: []UsageGroup{}}
err := c.withReadOnlyTx(ctx, func(ctx context.Context, q querier) error {
if q.QueryRow(ctx, "SELECT transaction_timestamp()").Scan(&result.AsOf) != nil {
return ErrUnavailable
}
rows, err := q.Query(ctx, recordsBaseSQL+` SELECT identity::text,count(*),count(*) FILTER(WHERE status='succeeded'),count(*) FILTER(WHERE status='failed') FROM records WHERE `+recordFilterSQL+` GROUP BY identity`, query.Window.TenantKey, query.Window.Start, query.Window.End, pattern)
if err != nil {
return ErrUnavailable
}
defer rows.Close()
for rows.Next() {
var raw string
var group UsageGroup
if rows.Scan(&raw, &group.Total, &group.Succeeded, &group.Failed) != nil || json.Unmarshal([]byte(raw), &group.Identity) != nil {
return ErrUnavailable
}
result.Groups = append(result.Groups, group)
}
return rows.Err()
})
return result, err
}
// Keep the agreed field ordering after governance enriches an initially empty column.
func AddRecordColumns(page *RecordPage, extra ...string) {
all := map[string]bool{}
for _, col := range append(page.Columns, extra...) {
all[col] = true
}
page.Columns = []string{}
for _, col := range recordColumnOrder {
if all[col] {
page.Columns = append(page.Columns, col)
}
}
}