From 2190da9f6019423b97a837c1ba3b382428ab0cab Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 00:52:41 +0000 Subject: [PATCH] feat: Implement analytics and fix Translation resolver - Implemented view counting for works and translations by adding calls to the analytics service in the `Work` and `Translation` resolvers. - Implemented translation counting for works by adding a call to the analytics service in the `CreateTranslation` resolver. - Fixed the `Translation` resolver, which was previously unimplemented and caused a panic. - Updated `TASKS.md` to mark the implemented analytics features as complete. --- TASKS.md | 4 +-- internal/adapters/graphql/schema.resolvers.go | 31 ++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/TASKS.md b/TASKS.md index 63d044d..e420ae3 100644 --- a/TASKS.md +++ b/TASKS.md @@ -20,11 +20,11 @@ This document is the single source of truth for all outstanding development task ### Features - [ ] **Implement Analytics Features:** - **Context:** This is required for user engagement insights. The following counts need to be implemented and stored, likely on the `Work` and `Translation` models. - - [ ] Implement view counting. + - [x] Implement view counting. - [ ] Implement like counting. - [ ] Implement comment counting. - [ ] Implement bookmark counting. - - [ ] Implement translation counting. + - [x] Implement translation counting. - [ ] Implement a service to calculate popular translations based on the above metrics. - *Note: This is referenced in the old `TODO.md` and a TODO comment in `internal/jobs/linguistics/work_analysis_service.go`.* diff --git a/internal/adapters/graphql/schema.resolvers.go b/internal/adapters/graphql/schema.resolvers.go index 74c5580..34655be 100644 --- a/internal/adapters/graphql/schema.resolvers.go +++ b/internal/adapters/graphql/schema.resolvers.go @@ -227,6 +227,9 @@ func (r *mutationResolver) CreateTranslation(ctx context.Context, input model.Tr return nil, err } + // Increment translation count for the work + go r.App.Analytics.IncrementWorkTranslationCount(context.Background(), uint(workID)) + // Convert to GraphQL model return &model.Translation{ ID: fmt.Sprintf("%d", createdTranslation.ID), @@ -1095,6 +1098,9 @@ func (r *queryResolver) Work(ctx context.Context, id string) (*model.Work, error return nil, nil } + // Increment view count in the background + go r.App.Analytics.IncrementWorkViews(context.Background(), uint(workID)) + content := r.resolveWorkContent(ctx, workRecord.ID, workRecord.Language) return &model.Work{ @@ -1140,7 +1146,30 @@ func (r *queryResolver) Works(ctx context.Context, limit *int32, offset *int32, // Translation is the resolver for the translation field. func (r *queryResolver) Translation(ctx context.Context, id string) (*model.Translation, error) { - panic(fmt.Errorf("not implemented: Translation - translation")) + translationID, err := strconv.ParseUint(id, 10, 32) + if err != nil { + return nil, fmt.Errorf("invalid translation ID: %v", err) + } + + translationRecord, err := r.App.Translation.Queries.Translation(ctx, uint(translationID)) + if err != nil { + return nil, err + } + if translationRecord == nil { + return nil, nil + } + + // Increment view count in the background + go r.App.Analytics.IncrementTranslationViews(context.Background(), uint(translationID)) + + // Convert to GraphQL model + return &model.Translation{ + ID: id, + Name: translationRecord.Title, + Language: translationRecord.Language, + Content: &translationRecord.Content, + WorkID: fmt.Sprintf("%d", translationRecord.TranslatableID), + }, nil } // Translations is the resolver for the translations field.