Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
50 lines
2.4 KiB
Go
50 lines
2.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// SummaryReportRepo provides read-only data access for summary reports.
|
|
// It queries the existing reporting_events_rollups table — no separate summary_reports table exists.
|
|
// Reference: Chatwoot SummaryReport controller — queries ReportingEventsRollup filtered by dimension_type.
|
|
type SummaryReportRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewSummaryReportRepo creates a new SummaryReportRepo.
|
|
func NewSummaryReportRepo(db *gorm.DB) *SummaryReportRepo {
|
|
return &SummaryReportRepo{db: db}
|
|
}
|
|
|
|
// GetAgentSummary retrieves agent-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetAgentSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionAgent, since, until)
|
|
}
|
|
|
|
// GetTeamSummary retrieves team-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetTeamSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionTeam, since, until)
|
|
}
|
|
|
|
// GetInboxSummary retrieves inbox-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetInboxSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionInbox, since, until)
|
|
}
|
|
|
|
// GetLabelSummary retrieves label-level summary metrics for an account within a date range.
|
|
func (r *SummaryReportRepo) GetLabelSummary(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEventsRollup, error) {
|
|
return r.aggregateByDimensionType(ctx, accountID, model.DimensionLabel, since, until)
|
|
}
|
|
|
|
// aggregateByDimensionType queries ReportingEventsRollup filtered by the given dimension type and date range.
|
|
func (r *SummaryReportRepo) aggregateByDimensionType(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
|
|
} |