feat: shit shit

This commit is contained in:
2025-08-15 11:10:28 +03:00
parent 0c86228095
commit 38acc4a443
1402 changed files with 453050 additions and 111 deletions

View File

@@ -2,20 +2,16 @@ package products
import (
"context"
"encoding/json"
"fmt"
"github.com/go-faster/errors"
"github.com/redis/rueidis"
"github.com/samber/lo"
pb "sipro-mps/api/generated/v1/wb/products"
"sipro-mps/internal/marketplace"
"sipro-mps/internal/redis"
"sipro-mps/internal/tasks/client"
"sipro-mps/internal/tasks/types"
"sipro-mps/internal/wb"
"sipro-mps/internal/wb/products/mapping/generated"
wbapi "sipro-mps/pkg/api/wb/client"
"sipro-mps/pkg/utils"
"github.com/deliveryhero/pipeline/v2"
"github.com/samber/lo"
)
const (
@@ -97,79 +93,99 @@ func fetchProducts(
}
func (a apiRepository) StreamAllProductsCache(ctx context.Context, marketplaceId int, resultChan chan<- []pb.Product, errChan chan<- error) {
defer close(resultChan)
defer close(errChan)
_, sellerId, err := a.ParseMarketplace(ctx, marketplaceId)
// DO NOT close channels here - WithCache will handle it (caller/creator owns them)
mp, err := a.marketplaceRepository.GetMarketplaceByID(ctx, marketplaceId)
if err != nil {
errChan <- err
errChan <- fmt.Errorf("getting marketplace by ID: %w", err)
return
}
c := *redis.Client
key := fmt.Sprintf("wb:products:%s", sellerId)
jsonString, err := c.Do(ctx, c.B().Get().Key(key).Build()).ToString()
if err == nil && jsonString != "null" {
var result []pb.Product
err = json.Unmarshal([]byte(jsonString), &result)
if err != nil {
errChan <- fmt.Errorf("unmarshalling products from cache: %w", err)
return
}
task, err := types.NewFetchProductsTask(types.TypeWbFetchProducts, marketplaceId)
if err != nil {
errChan <- fmt.Errorf("creating fetch products task: %w", err)
return
}
_, err = client.Client.Enqueue(task)
if err != nil {
errChan <- fmt.Errorf("enqueueing fetch products task: %w", err)
return
}
resultChan <- result
identifier, err := mp.GetIdentifier()
if err != nil {
errChan <- fmt.Errorf("getting marketplace identifier: %w", err)
return
}
if !errors.As(err, &rueidis.Nil) && err != nil {
errChan <- fmt.Errorf("fetching products from cache: %w", err)
client, err := wb.GetClientFromMarketplace(mp)
if err != nil {
return
}
converter := generated.ConverterImpl{}
innerResultChan := make(chan []WbProduct)
innerErrChan := make(chan error)
go a.StreamAllProducts(ctx, marketplaceId, innerResultChan, innerErrChan)
var allProducts []pb.Product
defer func() {
jsonData, err := json.Marshal(allProducts)
if err != nil {
errChan <- fmt.Errorf("marshalling products to cache: %w", err)
return
}
err = c.Do(ctx, c.B().Set().Key(key).Value(string(jsonData)).Build()).Error()
if err != nil {
errChan <- fmt.Errorf("setting products to cache: %w", err)
return
}
}()
for {
select {
case err, ok := <-innerErrChan:
if !ok {
return
}
errChan <- fmt.Errorf("streaming products: %w", err)
return
case products, ok := <-innerResultChan:
if !ok {
return
}
pbProducts := lo.Map(products, func(p WbProduct, _ int) pb.Product {
return *converter.ToProto(&p)
})
allProducts = append(allProducts, pbProducts...)
resultChan <- pbProducts
}
transform := pipeline.NewProcessor(func(_ context.Context, products []WbProduct) ([]pb.Product, error) {
return lo.Map(products, func(item WbProduct, _ int) pb.Product {
return *converter.ToProto(&item)
}), nil
}, nil)
inputChan := make(chan []WbProduct)
fetchProducts(ctx, client, identifier, inputChan, nil)
for out := range pipeline.Process(ctx, transform, inputChan) {
resultChan <- out
}
//c := *redis.Client
//key := fmt.Sprintf("wb:products:%s", sellerId)
//jsonString, err := c.Do(ctx, c.B().Get().Key(key).Build()).ToString()
//if err == nil && jsonString != "null" {
// var result []pb.Product
// err = json.Unmarshal([]byte(jsonString), &result)
// if err != nil {
// errChan <- fmt.Errorf("unmarshalling products from cache: %w", err)
// return
// }
// task, err := types.NewFetchProductsTask(types.TypeWbFetchProducts, marketplaceId)
// if err != nil {
// errChan <- fmt.Errorf("creating fetch products task: %w", err)
// return
// }
// _, err = client.Client.Enqueue(task)
// if err != nil {
// errChan <- fmt.Errorf("enqueueing fetch products task: %w", err)
// return
// }
//
// resultChan <- result
// return
//}
//if !errors.As(err, &rueidis.Nil) && err != nil {
// errChan <- fmt.Errorf("fetching products from cache: %w", err)
// return
//}
//converter := generated.ConverterImpl{}
//
//innerResultChan := make(chan []WbProduct)
//innerErrChan := make(chan error)
//go a.StreamAllProducts(ctx, marketplaceId, innerResultChan, innerErrChan)
//var allProducts []pb.Product
//defer func() {
// jsonData, err := json.Marshal(allProducts)
// if err != nil {
// errChan <- fmt.Errorf("marshalling products to cache: %w", err)
// return
// }
// err = c.Do(ctx, c.B().Set().Key(key).Value(string(jsonData)).Build()).Error()
// if err != nil {
// errChan <- fmt.Errorf("setting products to cache: %w", err)
// return
// }
//}()
//for {
// select {
// case err, ok := <-innerErrChan:
// if !ok {
// return
// }
// errChan <- fmt.Errorf("streaming products: %w", err)
// return
// case products, ok := <-innerResultChan:
// if !ok {
// return
// }
// pbProducts := lo.Map(products, func(p WbProduct, _ int) pb.Product {
// return *converter.ToProto(&p)
// })
// allProducts = append(allProducts, pbProducts...)
// resultChan <- pbProducts
// }
//}
}
func (a apiRepository) GetAllProducts(ctx context.Context, marketplaceId int) ([]WbProduct, error) {
marketplaceByID, sellerId, err := a.ParseMarketplace(ctx, marketplaceId)