tercul-backend/internal/data/sql/analytics_repository.go
google-labs-jules[bot] caf07df08d feat(analytics): Enhance analytics capabilities
This commit introduces a comprehensive enhancement of the application's analytics features, addressing performance, data modeling, and feature set.

The key changes include:

- **Performance Improvement:** The analytics repository now uses a database "UPSERT" operation to increment counters, reducing two separate database calls (read and write) into a single, more efficient operation.

- **New Metrics:** The `WorkStats` and `TranslationStats` models have been enriched with new, calculated metrics:
  - `ReadingTime`: An estimation of the time required to read the work or translation.
  - `Complexity`: A score representing the linguistic complexity of the text.
  - `Sentiment`: A score indicating the emotional tone of the text.

- **Service Refactoring:** The analytics service has been refactored to support the new metrics. It now includes methods to calculate and update these scores, leveraging the existing linguistics package for text analysis.

- **GraphQL API Expansion:** The new analytics fields (`readingTime`, `complexity`, `sentiment`) have been exposed through the GraphQL API by updating the `WorkStats` and `TranslationStats` types in the schema.

- **Validation and Testing:**
  - GraphQL input validation has been centralized and improved by moving from ad-hoc checks to a consistent validation pattern in the GraphQL layer.
  - The test suite has been significantly improved with the addition of new tests for the analytics service and the data access layer, ensuring the correctness and robustness of the new features. This includes fixing several bugs that were discovered during the development process.
2025-09-07 19:26:51 +00:00

124 lines
4.1 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) 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) UpdateTrending(ctx context.Context, trending []domain.Trending) error {
if len(trending) == 0 {
return nil
}
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
timePeriod := trending[0].TimePeriod
date := trending[0].Date
if err := tx.Where("time_period = ? AND date = ?", timePeriod, date).Delete(&domain.Trending{}).Error; err != nil {
return err
}
return tx.Create(&trending).Error
})
}