85 lines
3.5 KiB
Go
85 lines
3.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ReportingEventsRollupRepo provides data access for ReportingEventsRollup.
|
|
type ReportingEventsRollupRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewReportingEventsRollupRepo(db *gorm.DB) *ReportingEventsRollupRepo {
|
|
return &ReportingEventsRollupRepo{db: db}
|
|
}
|
|
|
|
func (r *ReportingEventsRollupRepo) Create(ctx context.Context, rollup *model.ReportingEventsRollup) error {
|
|
return r.db.WithContext(ctx).Create(rollup).Error
|
|
}
|
|
|
|
func (r *ReportingEventsRollupRepo) FindByAccountAndDateRange(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
var rollups []model.ReportingEventsRollup
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND date >= ? AND date <= ?", accountID, since, until).
|
|
Find(&rollups).Error
|
|
return rollups, err
|
|
}
|
|
|
|
func (r *ReportingEventsRollupRepo) FindByMetric(ctx context.Context, accountID uint, metric model.RollupMetric, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
var rollups []model.ReportingEventsRollup
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND metric = ? AND date >= ? AND date <= ?", accountID, metric, since, until).
|
|
Find(&rollups).Error
|
|
return rollups, err
|
|
}
|
|
|
|
func (r *ReportingEventsRollupRepo) FindByDimension(ctx context.Context, accountID uint, dimType model.DimensionType, dimID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
var rollups []model.ReportingEventsRollup
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND dimension_type = ? AND dimension_id = ? AND date >= ? AND date <= ?", accountID, dimType, dimID, since, until).
|
|
Find(&rollups).Error
|
|
return rollups, err
|
|
}
|
|
|
|
// AggregateSummary returns aggregated metrics for a given dimension type across the date range.
|
|
func (r *ReportingEventsRollupRepo) AggregateSummary(ctx context.Context, accountID uint, dimType model.DimensionType, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
var rollups []model.ReportingEventsRollup
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND dimension_type = ? AND date >= ? AND date <= ?", accountID, dimType, since, until).
|
|
Find(&rollups).Error
|
|
return rollups, err
|
|
}
|
|
|
|
// DeleteByAccountAndDate deletes all rollups for a specific account and date.
|
|
func (r *ReportingEventsRollupRepo) DeleteByAccountAndDate(ctx context.Context, accountID uint, date time.Time) error {
|
|
return r.db.WithContext(ctx).
|
|
Where("account_id = ? AND date = ?", accountID, date).
|
|
Delete(&model.ReportingEventsRollup{}).Error
|
|
}
|
|
|
|
// BulkCreate inserts multiple rollup rows in a single transaction.
|
|
func (r *ReportingEventsRollupRepo) BulkCreate(ctx context.Context, rollups []model.ReportingEventsRollup) error {
|
|
if len(rollups) == 0 {
|
|
return nil
|
|
}
|
|
return r.db.WithContext(ctx).CreateInBatches(rollups, 100).Error
|
|
}
|
|
|
|
// FindByAccountAndDateRangeWithDimension retrieves rollups with dimension filters.
|
|
func (r *ReportingEventsRollupRepo) FindByAccountAndDateRangeWithDimension(ctx context.Context, accountID uint, since, until time.Time, dimType model.DimensionType, dimID uint) ([]model.ReportingEventsRollup, error) {
|
|
var rollups []model.ReportingEventsRollup
|
|
query := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND date >= ? AND date <= ?", accountID, since, until)
|
|
if dimType != "" {
|
|
query = query.Where("dimension_type = ?", dimType)
|
|
}
|
|
if dimID > 0 {
|
|
query = query.Where("dimension_id = ?", dimID)
|
|
}
|
|
err := query.Find(&rollups).Error
|
|
return rollups, err
|
|
} |