cache and stats
This commit is contained in:
@ -895,6 +895,11 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
itemsUpdated := make(map[string]struct{}, len(seenItems))
|
itemsUpdated := make(map[string]struct{}, len(seenItems))
|
||||||
|
stats := struct {
|
||||||
|
fetchedInfo int
|
||||||
|
handledAll int
|
||||||
|
cachedInfo int
|
||||||
|
}{}
|
||||||
|
|
||||||
startFrom := time.Now()
|
startFrom := time.Now()
|
||||||
for {
|
for {
|
||||||
@ -928,14 +933,17 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
|
|||||||
goodsItems = goodsItems[:0]
|
goodsItems = goodsItems[:0]
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
var pi entity.GoodsItemInfo
|
var pi entity.GoodsItemInfo
|
||||||
if seenItem, ok := seenItems[item.SKU]; ok {
|
seenItem := seenItems[item.SKU]
|
||||||
|
if len(seenItem.Parameters) != 0 && len(seenItem.PhotoURLs) != 0 {
|
||||||
pi.Parameters = seenItem.Parameters
|
pi.Parameters = seenItem.Parameters
|
||||||
pi.PhotoURLs = seenItem.PhotoURLs
|
pi.PhotoURLs = seenItem.PhotoURLs
|
||||||
|
stats.cachedInfo++
|
||||||
} else {
|
} else {
|
||||||
pi, err = client.GetProductInfo(ctx, int64(item.Cart))
|
pi, err = client.GetProductInfo(ctx, int64(item.Cart))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("getting product info: %w", err)
|
return fmt.Errorf("getting product info: %w", err)
|
||||||
}
|
}
|
||||||
|
stats.fetchedInfo++
|
||||||
}
|
}
|
||||||
|
|
||||||
goodsItem, err := entity.MakeGoodsItem(item, remnants, pi)
|
goodsItem, err := entity.MakeGoodsItem(item, remnants, pi)
|
||||||
@ -945,6 +953,7 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
itemsUpdated[goodsItem.Articul] = struct{}{}
|
itemsUpdated[goodsItem.Articul] = struct{}{}
|
||||||
|
stats.handledAll++
|
||||||
|
|
||||||
goodsItems = append(goodsItems, goodsItem)
|
goodsItems = append(goodsItems, goodsItem)
|
||||||
if goodsItem.Type == "" {
|
if goodsItem.Type == "" {
|
||||||
@ -1001,8 +1010,22 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
|
|||||||
for k := range itemsUpdated {
|
for k := range itemsUpdated {
|
||||||
delete(seenItems, k)
|
delete(seenItems, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Info().
|
||||||
|
Int("handled", stats.handledAll).
|
||||||
|
Int("cached", stats.cachedInfo).
|
||||||
|
Int("fetched", stats.fetchedInfo).
|
||||||
|
Int("to_delete", len(seenItems)).
|
||||||
|
Msg("processed items")
|
||||||
|
|
||||||
for k := range seenItems {
|
for k := range seenItems {
|
||||||
repository.GoodsItem().Delete(ctx, k)
|
out, err := repository.GoodsItem().Delete(ctx, k)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn().Err(err).Str("sku", k).Msg("unable to delete item")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug().Str("sku", k).Msg("deleted item")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@ -103,6 +103,10 @@ func (c *goodsItemClient) ListIter(
|
|||||||
func (c *goodsItemClient) List(
|
func (c *goodsItemClient) List(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
) (out []entity.GoodsItem, err error) {
|
) (out []entity.GoodsItem, err error) {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
err = c.db.View(func(txn *badger.Txn) error {
|
err = c.db.View(func(txn *badger.Txn) error {
|
||||||
opts := badger.DefaultIteratorOptions
|
opts := badger.DefaultIteratorOptions
|
||||||
opts.PrefetchValues = true
|
opts.PrefetchValues = true
|
||||||
@ -113,6 +117,10 @@ func (c *goodsItemClient) List(
|
|||||||
prefix := c.prefix()
|
prefix := c.prefix()
|
||||||
var cursor int
|
var cursor int
|
||||||
for iter.Seek(prefix); iter.ValidForPrefix(prefix); iter.Next() {
|
for iter.Seek(prefix); iter.ValidForPrefix(prefix); iter.Next() {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
cursor++
|
cursor++
|
||||||
current := iter.Item()
|
current := iter.Item()
|
||||||
err = current.Value(func(val []byte) error {
|
err = current.Value(func(val []byte) error {
|
||||||
@ -144,6 +152,10 @@ func (c *goodsItemClient) Get(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
sku string,
|
sku string,
|
||||||
) (out entity.GoodsItem, err error) {
|
) (out entity.GoodsItem, err error) {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return out, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
err = c.db.View(func(txn *badger.Txn) error {
|
err = c.db.View(func(txn *badger.Txn) error {
|
||||||
key := unsafe.Slice(unsafe.StringData(sku), len(sku))
|
key := unsafe.Slice(unsafe.StringData(sku), len(sku))
|
||||||
out, err = c.getBySKU(key, txn)
|
out, err = c.getBySKU(key, txn)
|
||||||
@ -161,6 +173,10 @@ func (c *goodsItemClient) Get(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *goodsItemClient) GetByCart(ctx context.Context, id int64) (out entity.GoodsItem, err error) {
|
func (c *goodsItemClient) GetByCart(ctx context.Context, id int64) (out entity.GoodsItem, err error) {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return out, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
err = c.db.View(func(txn *badger.Txn) error {
|
err = c.db.View(func(txn *badger.Txn) error {
|
||||||
idxKey := c.prefixedIDByCartInt64(id)
|
idxKey := c.prefixedIDByCartInt64(id)
|
||||||
skuByCartIDItem, err := txn.Get(idxKey)
|
skuByCartIDItem, err := txn.Get(idxKey)
|
||||||
@ -196,6 +212,10 @@ func (c *goodsItemClient) UpsertMany(ctx context.Context, items ...entity.GoodsI
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *goodsItemClient) Delete(ctx context.Context, sku string) (out entity.GoodsItem, err error) {
|
func (c *goodsItemClient) Delete(ctx context.Context, sku string) (out entity.GoodsItem, err error) {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return out, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
err = c.db.Update(func(txn *badger.Txn) error {
|
err = c.db.Update(func(txn *badger.Txn) error {
|
||||||
skuKey := c.prefixedStr(sku)
|
skuKey := c.prefixedStr(sku)
|
||||||
out, err = c.getBySKU(skuKey, txn)
|
out, err = c.getBySKU(skuKey, txn)
|
||||||
@ -208,6 +228,12 @@ func (c *goodsItemClient) Delete(ctx context.Context, sku string) (out entity.Go
|
|||||||
return fmt.Errorf("deleting key: %w", err)
|
return fmt.Errorf("deleting key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cartID := c.prefixedIDByCartInt64(out.Cart)
|
||||||
|
err = txn.Delete(cartID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("deleting index key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -223,10 +249,8 @@ func (c *goodsItemClient) upsertByBatch(ctx context.Context, items []entity.Good
|
|||||||
|
|
||||||
err := func() error {
|
err := func() error {
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
select {
|
if ctx.Err() != nil {
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
|
|
||||||
key := c.prefixedStr(item.Articul)
|
key := c.prefixedStr(item.Articul)
|
||||||
|
|||||||
Reference in New Issue
Block a user