feat: add persisted page insight platform

This commit is contained in:
2026-08-21 18:51:00 +08:00
parent 8507b5d017
commit 17e3f1a7d0
18 changed files with 1587 additions and 3 deletions

View File

@@ -12,6 +12,7 @@ import (
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/config"
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/database"
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/growthreport"
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/insight"
"github.com/redis/go-redis/v9"
)
@@ -40,11 +41,19 @@ func main() {
coordinator := growthreport.NewCoordinator(redisClient, growthreport.CoordinatorConfig{CreatePerMinute: cfg.AICreatePerMinute, UncachedPerDay: cfg.AIUncachedScopesPerDay, DailyRequests: cfg.AIDailyRequestBudget, DailyTokens: cfg.AIDailyTokenBudget, MaxConcurrent: cfg.AIMaxConcurrent})
avalai := growthreport.NewAvalAIClient(growthreport.AvalAIConfig{BaseURL: cfg.AvalAIBaseURL, APIKey: cfg.AvalAIAPIKey, Model: cfg.AvalAIModel, Timeout: 30 * time.Second}, &http.Client{})
processor := growthreport.NewProcessor(repository, growthreport.NewPostgreSQLBuilder(pool), avalai, coordinator, cfg.AIGrowthEnabled && cfg.AvalAIAPIKey != "")
insightService := insight.NewService(insight.NewRepository(pool), insight.NewPostgreSQLBuilder(pool))
logger.Info("growth worker ready", "ai_enabled", cfg.AIGrowthEnabled && cfg.AvalAIAPIKey != "")
poll := time.NewTicker(time.Second)
defer poll.Stop()
cleanup := time.NewTicker(24 * time.Hour)
defer cleanup.Stop()
nightly := time.NewTicker(time.Hour)
defer nightly.Stop()
if created, err := insightService.EnqueueAll(ctx, "NIGHTLY"); err != nil {
logger.Error("initial insight scheduling failed", "error", err)
} else {
logger.Info("initial insight scheduling complete", "created_jobs", created)
}
for {
select {
case <-ctx.Done():
@@ -56,6 +65,12 @@ func main() {
if err != nil && !errors.Is(err, growthreport.ErrNotFound) {
logger.Error("growth report processing failed", "error", err)
}
insightCtx, insightCancel := context.WithTimeout(ctx, 2*time.Minute)
err = insightService.ProcessNext(insightCtx)
insightCancel()
if err != nil && !errors.Is(err, insight.ErrNotFound) {
logger.Error("page insight processing failed", "error", err)
}
case <-cleanup.C:
cleanupCtx, cleanupCancel := context.WithTimeout(ctx, 30*time.Second)
deleted, err := repository.CleanupSuperseded(cleanupCtx, time.Now().UTC().AddDate(0, 0, -90))
@@ -65,6 +80,15 @@ func main() {
} else if deleted > 0 {
logger.Info("growth cleanup complete", "deleted_reports", deleted)
}
case <-nightly.C:
scheduleCtx, scheduleCancel := context.WithTimeout(ctx, 5*time.Minute)
created, err := insightService.EnqueueAll(scheduleCtx, "NIGHTLY")
scheduleCancel()
if err != nil {
logger.Error("nightly insight scheduling failed", "error", err)
} else if created > 0 {
logger.Info("nightly insight scheduling complete", "created_jobs", created)
}
}
}
}