package api import ( "context" "fmt" "log" "time" "droboticflow/internal/domain" "droboticflow/internal/store" ) var acceptanceZone = time.FixedZone("Asia/Shanghai", 8*60*60) const ( acceptanceWindow = 30 * time.Minute acceptanceRetryOffset = acceptanceWindow + 5*time.Minute acceptanceOrganizationBudget = 15 * time.Minute acceptanceSnapshotLookupBudget = 2 * time.Second ) // Give a failed or interrupted run one more window at 01:35. 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(acceptanceRetryOffset)} { 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 } day := slot.In(acceptanceZone).Format(time.DateOnly) pending := make([]string, 0, len(orgs)) for _, org := range orgs { if err := ctx.Err(); err != nil { return err } lookup, cancel := context.WithTimeout(ctx, acceptanceSnapshotLookupBudget) last, err := db.ReadAcceptanceSnapshot(lookup, org.ID) cancel() if err != nil { log.Printf("acceptance statistics org=%q could not read previous snapshot: %v", org.ID, err) continue } if last != nil { completed, err := time.Parse(time.RFC3339Nano, last.CompletedAt) if err == nil && completed.In(acceptanceZone).Format(time.DateOnly) == day { continue } } pending = append(pending, org.ID) } for i, org := range pending { if err := ctx.Err(); err != nil { return err } // Share only among organizations that still need a scan this window. remaining := acceptanceWindow if deadline, ok := ctx.Deadline(); ok { remaining = time.Until(deadline) } budget := min(acceptanceOrganizationBudget, remaining/time.Duration(len(pending)-i)) orgCtx, cancel := context.WithTimeout(ctx, budget) start := time.Now() n, err := refreshAcceptanceSnapshot(orgCtx, db, org) cancel() log.Printf("acceptance statistics org=%q candidates=%d elapsed=%s completed=%t", org, n, time.Since(start), err == nil) if err != nil { log.Printf("acceptance statistics org=%q refresh failed: %v", org, 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) }