droboticflow/internal/api/acceptance_priority_refresh.go
d-robotics b7f4924110
Some checks are pending
web-ci / checks (pull_request) Blocked by required conditions
web-ci / pool-search-mongo (pull_request) Blocked by required conditions
web-ci / work-availability (pull_request) Blocked by required conditions
web-ci / query-serving (pull_request) Blocked by required conditions
📊 Refresh acceptance priority statistics nightly
(cherry picked from commit fb4f6715bb58c1c512284d0651024ac890e6f896)
2026-11-18 20:29:20 +08:00

139 lines
3.8 KiB
Go

package api
import (
"context"
"fmt"
"log"
"time"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
var acceptanceZone = time.FixedZone("Asia/Shanghai", 8*60*60)
const acceptanceWindow = 15 * time.Minute
// Give a failed or interrupted run one more window at 01:20. Deploying
// during the day never starts a catch-up scan during business hours.
func acceptanceRefreshSlot(now time.Time) time.Time {
now = now.In(acceptanceZone)
first := time.Date(now.Year(), now.Month(), now.Day(), 1, 0, 0, 0, acceptanceZone)
for _, slot := range []time.Time{first, first.Add(20 * time.Minute)} {
if now.Before(slot.Add(acceptanceWindow)) {
return slot
}
}
return first.AddDate(0, 0, 1)
}
func RunAcceptanceStatisticsSchedule(ctx context.Context, db *store.MongoStore) {
for ctx.Err() == nil {
slot := acceptanceRefreshSlot(time.Now())
if !waitUntil(ctx, slot) {
return
}
window, cancel := context.WithDeadline(ctx, slot.Add(acceptanceWindow))
claimed, err := db.ClaimAcceptanceRefresh(window, slot.Format(time.RFC3339))
if err == nil && claimed {
err = refreshAcceptanceOrganizations(window, db, slot)
}
cancel()
if err != nil {
log.Printf("acceptance statistics window failed: %v", err)
}
if !waitUntil(ctx, slot.Add(acceptanceWindow)) {
return
}
}
}
func waitUntil(ctx context.Context, at time.Time) bool {
t := time.NewTimer(time.Until(at))
defer t.Stop()
select {
case <-ctx.Done():
return false
case <-t.C:
return ctx.Err() == nil
}
}
func refreshAcceptanceOrganizations(ctx context.Context, db store.Store, slot time.Time) error {
orgs, err := db.ListOrganizations(ctx)
if err != nil {
return err
}
for _, org := range orgs {
if err := ctx.Err(); err != nil {
return err
}
last, err := db.ReadAcceptanceSnapshot(ctx, org.ID)
if err != nil {
return err
}
if last != nil {
completed, err := time.Parse(time.RFC3339Nano, last.CompletedAt)
if err == nil && completed.In(acceptanceZone).Format(time.DateOnly) == slot.In(acceptanceZone).Format(time.DateOnly) {
continue
}
}
start := time.Now()
n, err := refreshAcceptanceSnapshot(ctx, db, org.ID)
log.Printf("acceptance statistics org=%q candidates=%d elapsed=%s completed=%t", org.ID, n, time.Since(start), err == nil)
if err != nil {
log.Printf("acceptance statistics refresh failed: %v", err)
}
}
return ctx.Err()
}
func refreshAcceptanceSnapshot(ctx context.Context, db store.Store, org string) (int, error) {
type key struct{ project, supplier string }
counts := map[key]int{}
total := 0
err := db.VisitAcceptanceCandidates(ctx, org, fanyuV5PrePackageAcceptanceStageID, func(page []domain.HumanItemRecord) error {
total += len(page)
ids := make([]string, 0, len(page))
seen := map[string]bool{}
for _, row := range page {
if row.BatchID != "" && !seen[row.BatchID] {
ids = append(ids, row.BatchID)
seen[row.BatchID] = true
}
}
if len(ids) == 0 {
return nil
}
batches, err := db.ListHumanBatchAccessRecords(ctx, org, ids)
if err != nil {
return err
}
byID := map[string]*domain.HumanBatchRecord{}
for i := range batches {
byID[batches[i].ID] = &batches[i]
}
owners, err := db.ResolveAcceptanceSourceSuppliers(ctx, org, ids)
if err != nil {
return err
}
for _, row := range page {
if domain.SharedAcceptanceItem(row, byID[row.BatchID]) {
counts[key{row.ProjectID, owners[row.BatchID]}]++
}
}
return nil
})
if err != nil {
return total, fmt.Errorf("scan acceptance candidates: %w", err)
}
if err := ctx.Err(); err != nil {
return total, err
}
v := domain.AcceptanceSnapshot{OrganizationID: org, CompletedAt: time.Now().UTC().Format(time.RFC3339Nano)}
for k, n := range counts {
v.Counts = append(v.Counts, domain.AcceptanceCount{ProjectID: k.project, SupplierID: k.supplier, Count: n})
}
return total, db.SaveAcceptanceSnapshot(ctx, v)
}