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.
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/worker"
|
|
)
|
|
|
|
const TaskTypeReportingRollupDay = "reporting:rollup_day"
|
|
|
|
type reportingRollupDayJob struct {
|
|
AccountID uint `json:"account_id"`
|
|
Date string `json:"date"`
|
|
}
|
|
|
|
var reportingRollupRegistrations sync.Map
|
|
|
|
func RegisterReportingRollupJobs(wp *worker.WorkerPool, svc *AnalyticsService) {
|
|
if wp == nil || svc == nil {
|
|
return
|
|
}
|
|
if _, loaded := reportingRollupRegistrations.LoadOrStore(wp, struct{}{}); loaded {
|
|
return
|
|
}
|
|
wp.Register(TaskTypeReportingRollupDay, svc.performReportingRollupDayJob)
|
|
}
|
|
|
|
func EnqueueReportingRollupDay(ctx context.Context, wp *worker.WorkerPool, accountID uint, date time.Time) (*model.BackgroundJob, error) {
|
|
if wp == nil || accountID == 0 {
|
|
return nil, nil
|
|
}
|
|
day := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)
|
|
dateStr := day.Format(time.DateOnly)
|
|
return wp.Enqueue(ctx, TaskTypeReportingRollupDay, reportingRollupDayJob{AccountID: accountID, Date: dateStr},
|
|
worker.WithQueue("low"),
|
|
worker.WithMaxAttempts(3),
|
|
worker.WithIdempotencyKey(fmt.Sprintf("reporting:rollup_day:%d:%s", accountID, dateStr)),
|
|
)
|
|
}
|
|
|
|
func (s *AnalyticsService) performReportingRollupDayJob(ctx context.Context, job *model.BackgroundJob) error {
|
|
var payload reportingRollupDayJob
|
|
if err := json.Unmarshal(job.Payload, &payload); err != nil {
|
|
return fmt.Errorf("unmarshal reporting rollup job: %w", err)
|
|
}
|
|
if payload.AccountID == 0 || payload.Date == "" {
|
|
return fmt.Errorf("invalid reporting rollup payload: %#v", payload)
|
|
}
|
|
date, err := time.Parse(time.DateOnly, payload.Date)
|
|
if err != nil {
|
|
return fmt.Errorf("parse reporting rollup date: %w", err)
|
|
}
|
|
if s == nil || s.rollups == nil {
|
|
return fmt.Errorf("reporting rollup service is required")
|
|
}
|
|
return s.rollups.ComputeDailyRollup(ctx, payload.AccountID, date)
|
|
}
|