mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
This commit addresses all the high-priority tasks outlined in the TASKS.md file, significantly improving the application's observability, completing key features, and refactoring critical parts of the codebase. ### Observability - **Centralized Logging:** Implemented a new structured, context-aware logging system using `zerolog`. A new logging middleware injects request-specific information (request ID, user ID, trace ID) into the logger, and all application logging has been refactored to use this new system. - **Prometheus Metrics:** Added Prometheus metrics for database query performance by creating a GORM plugin that automatically records query latency and totals. - **OpenTelemetry Tracing:** Fully instrumented all application services in `internal/app` and data repositories in `internal/data/sql` with OpenTelemetry tracing, providing deep visibility into application performance. ### Features - **Analytics:** Implemented like, comment, and bookmark counting. The respective command handlers now call the analytics service to increment counters when these actions are performed. - **Enrichment Tool:** Built a new, extensible `enrich` command-line tool to fetch data from external sources. The initial implementation enriches author data using the Open Library API. ### Refactoring & Fixes - **Decoupled Testing:** Refactored the testing utilities in `internal/testutil` to be database-agnostic, promoting the use of mock-based unit tests and improving test speed and reliability. - **Build Fixes:** Resolved numerous build errors, including a critical import cycle between the logging, observability, and authentication packages. - **Search Service:** Fixed the search service integration by implementing the `GetWorkContent` method in the localization service, allowing the search indexer to correctly fetch and index work content.
115 lines
3.3 KiB
Go
115 lines
3.3 KiB
Go
package work
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/domain/work"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// WorkAnalytics contains analytics data for a work
|
|
type WorkAnalytics struct {
|
|
WorkID uint
|
|
ViewCount int64
|
|
LikeCount int64
|
|
CommentCount int64
|
|
BookmarkCount int64
|
|
TranslationCount int64
|
|
ReadabilityScore float64
|
|
SentimentScore float64
|
|
TopKeywords []string
|
|
PopularTranslations []TranslationAnalytics
|
|
}
|
|
|
|
// TranslationAnalytics contains analytics data for a translation
|
|
type TranslationAnalytics struct {
|
|
TranslationID uint
|
|
Language string
|
|
ViewCount int64
|
|
LikeCount int64
|
|
}
|
|
|
|
// WorkQueries contains the query handlers for the work aggregate.
|
|
type WorkQueries struct {
|
|
repo work.WorkRepository
|
|
tracer trace.Tracer
|
|
}
|
|
|
|
// NewWorkQueries creates a new WorkQueries handler.
|
|
func NewWorkQueries(repo work.WorkRepository) *WorkQueries {
|
|
return &WorkQueries{
|
|
repo: repo,
|
|
tracer: otel.Tracer("work.queries"),
|
|
}
|
|
}
|
|
|
|
// GetWorkByID retrieves a work by ID.
|
|
func (q *WorkQueries) GetWorkByID(ctx context.Context, id uint) (*work.Work, error) {
|
|
ctx, span := q.tracer.Start(ctx, "GetWorkByID")
|
|
defer span.End()
|
|
if id == 0 {
|
|
return nil, errors.New("invalid work ID")
|
|
}
|
|
return q.repo.GetByID(ctx, id)
|
|
}
|
|
|
|
// ListWorks returns a paginated list of works.
|
|
func (q *WorkQueries) ListWorks(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
|
|
ctx, span := q.tracer.Start(ctx, "ListWorks")
|
|
defer span.End()
|
|
return q.repo.List(ctx, page, pageSize)
|
|
}
|
|
|
|
// GetWorkWithTranslations retrieves a work with its translations.
|
|
func (q *WorkQueries) GetWorkWithTranslations(ctx context.Context, id uint) (*work.Work, error) {
|
|
ctx, span := q.tracer.Start(ctx, "GetWorkWithTranslations")
|
|
defer span.End()
|
|
if id == 0 {
|
|
return nil, errors.New("invalid work ID")
|
|
}
|
|
return q.repo.GetWithTranslations(ctx, id)
|
|
}
|
|
|
|
// FindWorksByTitle finds works by title.
|
|
func (q *WorkQueries) FindWorksByTitle(ctx context.Context, title string) ([]work.Work, error) {
|
|
ctx, span := q.tracer.Start(ctx, "FindWorksByTitle")
|
|
defer span.End()
|
|
if title == "" {
|
|
return nil, errors.New("title cannot be empty")
|
|
}
|
|
return q.repo.FindByTitle(ctx, title)
|
|
}
|
|
|
|
// FindWorksByAuthor finds works by author ID.
|
|
func (q *WorkQueries) FindWorksByAuthor(ctx context.Context, authorID uint) ([]work.Work, error) {
|
|
ctx, span := q.tracer.Start(ctx, "FindWorksByAuthor")
|
|
defer span.End()
|
|
if authorID == 0 {
|
|
return nil, errors.New("invalid author ID")
|
|
}
|
|
return q.repo.FindByAuthor(ctx, authorID)
|
|
}
|
|
|
|
// FindWorksByCategory finds works by category ID.
|
|
func (q *WorkQueries) FindWorksByCategory(ctx context.Context, categoryID uint) ([]work.Work, error) {
|
|
ctx, span := q.tracer.Start(ctx, "FindWorksByCategory")
|
|
defer span.End()
|
|
if categoryID == 0 {
|
|
return nil, errors.New("invalid category ID")
|
|
}
|
|
return q.repo.FindByCategory(ctx, categoryID)
|
|
}
|
|
|
|
// FindWorksByLanguage finds works by language.
|
|
func (q *WorkQueries) FindWorksByLanguage(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
|
|
ctx, span := q.tracer.Start(ctx, "FindWorksByLanguage")
|
|
defer span.End()
|
|
if language == "" {
|
|
return nil, errors.New("language cannot be empty")
|
|
}
|
|
return q.repo.FindByLanguage(ctx, language, page, pageSize)
|
|
}
|