69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
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<?", []string{"locked", "terminated"}, w.PreviousBegin, w.End.AddDate(0, 0, 1))
|
|
if !scope.Viewer.All {
|
|
var ids []uint64
|
|
if err := channelBindingQuery(r.data.DB().WithContext(ctx), scope.Viewer, business.ChannelFilter{Sources: []string{source}}).Distinct("b.platform_busier_id").Pluck("b.platform_busier_id", &ids).Error; err != nil {
|
|
return business.ErrStorage
|
|
}
|
|
if len(ids) == 0 {
|
|
continue
|
|
}
|
|
q = q.Where("old_busier_id IN ?", ids)
|
|
}
|
|
if !scope.IncludeSub {
|
|
q = q.Where("promote_level=1")
|
|
}
|
|
var rows []struct {
|
|
Kind string
|
|
Current bool
|
|
Count int64
|
|
}
|
|
if err := q.Select("kind,(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
|
|
}
|