package business import ( "context" "kra/internal/biz/business" ) func (r *dashboardRepo) events(ctx context.Context, scope business.AnalyticsScope, sources []string, w business.DashboardWindow, out *business.DashboardData) error { out.EventsAvailable = true for _, source := range eventSources(sources) { db, err := r.databases.Database(source) if err != nil || db == nil { out.EventsAvailable = false continue } db = db.WithContext(ctx) ok, err := businessTableAvailable(db, "tab_promote_business_event") if err != nil { return err } if !ok { out.EventsAvailable = false continue } var coverage []channelEventPO if err := db.Where("kind='coverage'").Order("occurred_at,id").Limit(1).Find(&coverage).Error; err != nil { return business.ErrActivationSource } if len(coverage) > 0 && w.PreviousBegin.Before(coverage[0].OccurredAt) { out.EventsAvailable = false out.EventsIncomplete = true } q := db.Model(&channelEventPO{}).Where("kind IN ? AND occurred_at>=? AND occurred_at=?) AS current,COUNT(DISTINCT promote_id) AS count", w.Begin).Group("kind,current").Scan(&rows).Error; err != nil { return business.ErrActivationSource } for _, row := range rows { target := &out.Previous if row.Current { target = &out.Current } if row.Kind == "locked" { target.LockedChannels += row.Count } else { target.TerminatedChannels += row.Count } } } return nil }