tercul-backend/internal/jobs/trending/trending.go
google-labs-jules[bot] f8b3ecb9bd feat: Implement trending works feature
This commit introduces a new trending works feature to the application.

The feature includes:
- A new `Trending` domain model to store ranked works.
- An `UpdateTrending` method in the `AnalyticsService` that calculates a trending score for each work based on views, likes, and comments.
- A background job that runs hourly to update the trending works.
- A new `trendingWorks` query in the GraphQL API to expose the trending works.
- New tests for the trending feature, and fixes for existing tests.

This commit also includes a refactoring of the analytics repository to use a more generic `IncrementWorkCounter` method, and enhancements to the `WorkStats` and `TranslationStats` models with new metrics like `readingTime`, `complexity`, and `sentiment`.
2025-09-07 20:40:35 +00:00

40 lines
921 B
Go

package trending
import (
"context"
"encoding/json"
"tercul/internal/app/analytics"
"github.com/hibiken/asynq"
)
const (
TaskUpdateTrending = "task:trending:update"
)
type UpdateTrendingPayload struct {
// No payload needed for now
}
func NewUpdateTrendingTask() (*asynq.Task, error) {
payload, err := json.Marshal(UpdateTrendingPayload{})
if err != nil {
return nil, err
}
return asynq.NewTask(TaskUpdateTrending, payload), nil
}
func HandleUpdateTrendingTask(analyticsService analytics.Service) asynq.HandlerFunc {
return func(ctx context.Context, t *asynq.Task) error {
var p UpdateTrendingPayload
if err := json.Unmarshal(t.Payload(), &p); err != nil {
return err
}
return analyticsService.UpdateTrending(ctx)
}
}
func RegisterTrendingHandlers(mux *asynq.ServeMux, analyticsService analytics.Service) {
mux.HandleFunc(TaskUpdateTrending, HandleUpdateTrendingTask(analyticsService))
}