fix ltv linter

This commit is contained in:
Pavel 2023-12-23 11:14:01 +03:00
parent 0a8f184207
commit 61fb3f4e3a

@ -3,18 +3,20 @@ package repository
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
"log"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/fields"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
mongoWrapper "penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/mongo"
"time"
)
type HistoryRepositoryDeps struct {
@ -287,20 +289,20 @@ func (receiver *HistoryRepository) CalculateCustomerLTV(ctx context.Context, fro
timeFilter["createdAt"] = timeRange
}
pipeline := mongo.Pipeline{
{{"$match", bson.M{"key": models.CustomerHistoryKeyPayCart, "isDeleted": false}}},
{{"$match", timeFilter}},
{{"$group", bson.M{
{{Key: "$match", Value: bson.M{"key": models.CustomerHistoryKeyPayCart, "isDeleted": false}}},
{{Key: "$match", Value: timeFilter}},
{{Key: "$group", Value: bson.M{
"_id": "$userId",
"firstPayment": bson.M{"$min": "$createdAt"},
"lastPayment": bson.M{"$max": "$createdAt"},
}}},
{{"$project", bson.M{
{{Key: "$project", Value: bson.M{
"lifeTimeInDays": bson.M{"$divide": []interface{}{
bson.M{"$subtract": []interface{}{bson.M{"$toDate": "$lastPayment"}, bson.M{"$toDate": "$firstPayment"}}},
86400000,
}},
}}},
{{"$group", bson.M{
{{Key: "$group", Value: bson.M{
"_id": nil,
"averageLTV": bson.M{"$avg": "$lifeTimeInDays"},
}}},
@ -316,7 +318,11 @@ func (receiver *HistoryRepository) CalculateCustomerLTV(ctx context.Context, fro
errors.ErrInternalError,
)
}
defer cursor.Close(ctx)
defer func() {
if err := cursor.Close(ctx); err != nil {
receiver.logger.Error("failed to close cursor", zap.Error(err))
}
}()
var results []struct{ AverageLTV float64 }
if err := cursor.All(ctx, &results); err != nil {
receiver.logger.Error("failed to getting result LTV <CalculateCustomerLTV> of <HistoryRepository>",