tercul-backend/internal/data/sql/analytics_repository.go
google-labs-jules[bot] f66936bc4b feat: Implement event-driven analytics features
This commit implements a robust, production-ready analytics system using an event-driven architecture with Redis and `asynq`.

Key changes:
- Event-Driven Architecture: Instead of synchronous database updates, analytics events (e.g., views, likes, comments) are now published to a Redis queue. This improves API response times and decouples the analytics system from the main application flow.
- Background Worker: A new worker process (`cmd/worker`) has been created to consume events from the queue and update the analytics counters in the database.
- View Counting: Implemented the missing view counting feature for both works and translations.
- New Analytics Query: Added a `popularTranslations` GraphQL query to demonstrate how to use the collected analytics data.
- Testing: Added unit tests for the new event publisher and integration tests for the analytics worker.

Known Issue:
The integration tests for the analytics worker (`AnalyticsWorkerSuite`) and the GraphQL API (`GraphQLIntegrationSuite`) are currently failing due to the lack of a Redis service in the test environment. The tests are written and are expected to pass in an environment where Redis is available on `localhost:6379`, as configured in the CI pipeline.
2025-09-07 22:30:23 +00:00

187 lines
5.9 KiB
Go

package sql
import (
"context"
"fmt"
"tercul/internal/domain"
"time"
"gorm.io/gorm"
)
type analyticsRepository struct {
db *gorm.DB
}
func NewAnalyticsRepository(db *gorm.DB) domain.AnalyticsRepository {
return &analyticsRepository{db: db}
}
var allowedWorkCounterFields = map[string]bool{
"views": true,
"likes": true,
"comments": true,
"bookmarks": true,
"shares": true,
"translation_count": true,
}
var allowedTranslationCounterFields = map[string]bool{
"views": true,
"likes": true,
"comments": true,
"shares": true,
}
func (r *analyticsRepository) IncrementWorkCounter(ctx context.Context, workID uint, field string, value int) error {
if !allowedWorkCounterFields[field] {
return fmt.Errorf("invalid work counter field: %s", field)
}
// Using a transaction to ensure atomicity
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// First, try to update the existing record
result := tx.Model(&domain.WorkStats{}).Where("work_id = ?", workID).UpdateColumn(field, gorm.Expr(fmt.Sprintf("%s + ?", field), value))
if result.Error != nil {
return result.Error
}
// If no rows were affected, the record does not exist, so create it
if result.RowsAffected == 0 {
initialData := map[string]interface{}{"work_id": workID, field: value}
return tx.Model(&domain.WorkStats{}).Create(initialData).Error
}
return nil
})
}
func (r *analyticsRepository) GetPopularTranslations(ctx context.Context, workID uint, limit int) ([]*domain.Translation, error) {
var translations []*domain.Translation
err := r.db.WithContext(ctx).
Joins("LEFT JOIN translation_stats ON translation_stats.translation_id = translations.id").
Where("translations.translatable_id = ? AND translations.translatable_type = ?", workID, "Work").
Order("translation_stats.views + (translation_stats.likes * 2) DESC").
Limit(limit).
Find(&translations).Error
if err != nil {
return nil, err
}
return translations, nil
}
func (r *analyticsRepository) GetTrendingWorks(ctx context.Context, timePeriod string, limit int) ([]*domain.Work, error) {
var trendingWorks []*domain.Trending
err := r.db.WithContext(ctx).
Where("entity_type = ? AND time_period = ?", "Work", timePeriod).
Order("rank ASC").
Limit(limit).
Find(&trendingWorks).Error
if err != nil {
return nil, err
}
if len(trendingWorks) == 0 {
return []*domain.Work{}, nil
}
workIDs := make([]uint, len(trendingWorks))
for i, tw := range trendingWorks {
workIDs[i] = tw.EntityID
}
var works []*domain.Work
err = r.db.WithContext(ctx).
Where("id IN ?", workIDs).
Find(&works).Error
// This part is tricky because the order from the IN clause is not guaranteed.
// We need to re-order the works based on the trending rank.
workMap := make(map[uint]*domain.Work)
for _, work := range works {
workMap[work.ID] = work
}
orderedWorks := make([]*domain.Work, len(workIDs))
for i, id := range workIDs {
if work, ok := workMap[id]; ok {
orderedWorks[i] = work
}
}
return orderedWorks, err
}
func (r *analyticsRepository) IncrementTranslationCounter(ctx context.Context, translationID uint, field string, value int) error {
if !allowedTranslationCounterFields[field] {
return fmt.Errorf("invalid translation counter field: %s", field)
}
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
result := tx.Model(&domain.TranslationStats{}).Where("translation_id = ?", translationID).UpdateColumn(field, gorm.Expr(fmt.Sprintf("%s + ?", field), value))
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
initialData := map[string]interface{}{"translation_id": translationID, field: value}
return tx.Model(&domain.TranslationStats{}).Create(initialData).Error
}
return nil
})
}
func (r *analyticsRepository) UpdateWorkStats(ctx context.Context, workID uint, stats domain.WorkStats) error {
return r.db.WithContext(ctx).Model(&domain.WorkStats{}).Where("work_id = ?", workID).Updates(stats).Error
}
func (r *analyticsRepository) UpdateTranslationStats(ctx context.Context, translationID uint, stats domain.TranslationStats) error {
return r.db.WithContext(ctx).Model(&domain.TranslationStats{}).Where("translation_id = ?", translationID).Updates(stats).Error
}
func (r *analyticsRepository) GetOrCreateWorkStats(ctx context.Context, workID uint) (*domain.WorkStats, error) {
var stats domain.WorkStats
err := r.db.WithContext(ctx).Where(domain.WorkStats{WorkID: workID}).FirstOrCreate(&stats).Error
return &stats, err
}
func (r *analyticsRepository) GetOrCreateTranslationStats(ctx context.Context, translationID uint) (*domain.TranslationStats, error) {
var stats domain.TranslationStats
err := r.db.WithContext(ctx).Where(domain.TranslationStats{TranslationID: translationID}).FirstOrCreate(&stats).Error
return &stats, err
}
func (r *analyticsRepository) GetOrCreateUserEngagement(ctx context.Context, userID uint, date time.Time) (*domain.UserEngagement, error) {
var engagement domain.UserEngagement
err := r.db.WithContext(ctx).Where(domain.UserEngagement{UserID: userID, Date: date}).FirstOrCreate(&engagement).Error
return &engagement, err
}
func (r *analyticsRepository) UpdateUserEngagement(ctx context.Context, userEngagement *domain.UserEngagement) error {
return r.db.WithContext(ctx).Save(userEngagement).Error
}
func (r *analyticsRepository) UpdateTrendingWorks(ctx context.Context, timePeriod string, trending []*domain.Trending) error {
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// Clear old trending data for this time period
if err := tx.Where("time_period = ?", timePeriod).Delete(&domain.Trending{}).Error; err != nil {
return fmt.Errorf("failed to delete old trending data: %w", err)
}
if len(trending) == 0 {
return nil
}
// Insert new trending data
if err := tx.Create(trending).Error; err != nil {
return fmt.Errorf("failed to insert new trending data: %w", err)
}
return nil
})
}