tercul-backend/internal/app/analytics
google-labs-jules[bot] c2e9a118e2 feat(testing): Increase test coverage and fix authz bugs
This commit significantly increases the test coverage across the application and fixes several underlying bugs that were discovered while writing the new tests.

The key changes include:

- **New Tests:** Added extensive integration and unit tests for GraphQL resolvers, application services, and data repositories, substantially increasing the test coverage for packages like `graphql`, `user`, `translation`, and `analytics`.

- **Authorization Bug Fixes:**
  - Fixed a critical bug where a user creating a `Work` was not correctly associated as its author, causing subsequent permission failures.
  - Corrected the authorization logic in `authz.Service` to properly check for entity ownership by non-admin users.

- **Test Refactoring:**
  - Refactored numerous test suites to use `testify/mock` instead of manual mocks, improving test clarity and maintainability.
  - Isolated integration tests by creating a fresh admin user and token for each test run, eliminating test pollution.
  - Centralized domain errors into `internal/domain/errors.go` and updated repositories to use them, making error handling more consistent.

- **Code Quality Improvements:**
  - Replaced manual mock implementations with `testify/mock` for better consistency.
  - Cleaned up redundant and outdated test files.

These changes stabilize the test suite, improve the overall quality of the codebase, and move the project closer to the goal of 80% test coverage.
2025-10-09 07:03:45 +00:00
..
interfaces.go feat: Complete large-scale refactor and prepare for production 2025-10-07 11:09:37 +00:00
README.md This commit introduces a series of significant improvements to bring the codebase closer to a production-ready state. 2025-10-08 17:25:02 +00:00
service_test.go feat(testing): Increase test coverage and fix authz bugs 2025-10-09 07:03:45 +00:00
service.go test: Increase test coverage for work package to over 80% 2025-10-08 20:45:49 +00:00

Analytics Service

This package is responsible for collecting, processing, and retrieving all analytical data for the Tercul platform. It handles statistics for works, translations, and user engagement.

Architecture Overview

The analytics service provides a central point for all statistical operations. It is designed to be called by other application services (e.g., after a user likes a work) to increment or decrement counters. It also provides methods for more complex analytical tasks, such as calculating reading time and sentiment scores.

Key Components

  • service.go: The main entry point for the analytics service. It implements the Service interface and contains the core business logic for all analytical operations.
  • interfaces.go: Defines the Service and Repository interfaces, establishing a clear contract for the service's capabilities and its data persistence requirements.
  • Repository (external): The service relies on an AnalyticsRepository, implemented in the internal/data/sql package, to interact with the database.

Features

  • Counter Management: Provides methods to increment and decrement statistics like views, likes, comments, and bookmarks for works and translations.
  • Content Analysis: Calculates and updates metrics such as:
    • Reading time
    • Complexity (via readability scores)
    • Sentiment analysis
  • User Engagement Tracking: Monitors user activities like works read, comments made, and likes given.
  • Trending System: Calculates and stores trending works based on a scoring algorithm that considers views, likes, and comments.

Usage

The analytics.Service is intended to be injected into other application services that need to record analytical events.

Example: Incrementing Work Likes

// In another application service (e.g., the 'like' service)
err := analyticsService.IncrementWorkLikes(ctx, workID)

Example: Updating Work Analysis

// In a command handler (e.g., work.AnalyzeWork)
err := analyticsService.UpdateWorkReadingTime(ctx, workID)
if err != nil {
    // handle error
}

err = analyticsService.UpdateWorkComplexity(ctx, workID)
if err != nil {
    // handle error
}

Dependencies

  • internal/domain: Uses the core domain entities (e.g., WorkStats, TranslationStats, Trending).
  • internal/jobs/linguistics: Relies on the linguistics package for analysis data like readability scores and sentiment.
  • Database: Persists all statistical data to the main application database via the AnalyticsRepository.
  • Logging: Uses the centralized logger from internal/platform/log.
  • OpenTelemetry: All service methods are instrumented for distributed tracing.