tercul-backend/internal/jobs/linguistics/factory.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

109 lines
3.0 KiB
Go

package linguistics
import (
"tercul/internal/platform/cache"
"tercul/internal/platform/config"
"gorm.io/gorm"
)
// LinguisticsFactory provides easy access to all linguistics components
type LinguisticsFactory struct {
textAnalyzer TextAnalyzer
analysisCache AnalysisCache
analysisRepo AnalysisRepository
workAnalysisService WorkAnalysisService
analyzer Analyzer
sentimentProvider SentimentProvider
}
// NewLinguisticsFactory creates a new LinguisticsFactory with all components
func NewLinguisticsFactory(
db *gorm.DB,
cache cache.Cache,
concurrency int,
cacheEnabled bool,
sentimentProvider SentimentProvider,
) *LinguisticsFactory {
// Create text analyzer and wire providers (prefer external libs when available)
textAnalyzer := NewBasicTextAnalyzer()
// Wire sentiment provider
textAnalyzer = textAnalyzer.WithSentimentProvider(sentimentProvider)
// Wire language detector: lingua-go (configurable)
if config.Cfg.NLPUseLingua {
textAnalyzer = textAnalyzer.WithLanguageDetector(NewLinguaLanguageDetector())
}
// Wire keyword provider: lightweight TF-IDF approximation (configurable)
if config.Cfg.NLPUseTFIDF {
textAnalyzer = textAnalyzer.WithKeywordProvider(NewTFIDFKeywordProvider())
}
// Create cache components
memoryCache := NewMemoryAnalysisCache(cacheEnabled)
redisCache := NewRedisAnalysisCache(cache, cacheEnabled)
analysisCache := NewCompositeAnalysisCache(memoryCache, redisCache, cacheEnabled)
// Create repository
analysisRepo := NewGORMAnalysisRepository(db)
// Create work analysis service
workAnalysisService := NewWorkAnalysisService(
textAnalyzer,
analysisCache,
analysisRepo,
concurrency,
cacheEnabled,
)
// Create analyzer that combines text analysis and work analysis
analyzer := NewBasicAnalyzer(
textAnalyzer,
workAnalysisService,
cache,
concurrency,
cacheEnabled,
)
return &LinguisticsFactory{
textAnalyzer: textAnalyzer,
analysisCache: analysisCache,
analysisRepo: analysisRepo,
workAnalysisService: workAnalysisService,
analyzer: analyzer,
sentimentProvider: sentimentProvider,
}
}
// GetTextAnalyzer returns the text analyzer
func (f *LinguisticsFactory) GetTextAnalyzer() TextAnalyzer {
return f.textAnalyzer
}
// GetAnalysisCache returns the analysis cache
func (f *LinguisticsFactory) GetAnalysisCache() AnalysisCache {
return f.analysisCache
}
// GetAnalysisRepository returns the analysis repository
func (f *LinguisticsFactory) GetAnalysisRepository() AnalysisRepository {
return f.analysisRepo
}
// GetWorkAnalysisService returns the work analysis service
func (f *LinguisticsFactory) GetWorkAnalysisService() WorkAnalysisService {
return f.workAnalysisService
}
// GetAnalyzer returns the analyzer
func (f *LinguisticsFactory) GetAnalyzer() Analyzer {
return f.analyzer
}
// GetSentimentProvider returns the sentiment provider
func (f *LinguisticsFactory) GetSentimentProvider() SentimentProvider {
return f.sentimentProvider
}