diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 6886b0a..a523720 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -895,6 +895,11 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error { } itemsUpdated := make(map[string]struct{}, len(seenItems)) + stats := struct { + fetchedInfo int + handledAll int + cachedInfo int + }{} startFrom := time.Now() for { @@ -928,14 +933,17 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error { goodsItems = goodsItems[:0] for _, item := range items { 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.PhotoURLs = seenItem.PhotoURLs + stats.cachedInfo++ } else { pi, err = client.GetProductInfo(ctx, int64(item.Cart)) if err != nil { return fmt.Errorf("getting product info: %w", err) } + stats.fetchedInfo++ } goodsItem, err := entity.MakeGoodsItem(item, remnants, pi) @@ -945,6 +953,7 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error { } itemsUpdated[goodsItem.Articul] = struct{}{} + stats.handledAll++ goodsItems = append(goodsItems, goodsItem) if goodsItem.Type == "" { @@ -1001,8 +1010,22 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error { for k := range itemsUpdated { 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 { - 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 diff --git a/internal/storage/badger/goodsitem.go b/internal/storage/badger/goodsitem.go index b7c7728..8f31a4a 100644 --- a/internal/storage/badger/goodsitem.go +++ b/internal/storage/badger/goodsitem.go @@ -103,6 +103,10 @@ func (c *goodsItemClient) ListIter( func (c *goodsItemClient) List( ctx context.Context, ) (out []entity.GoodsItem, err error) { + if ctx.Err() != nil { + return nil, ctx.Err() + } + err = c.db.View(func(txn *badger.Txn) error { opts := badger.DefaultIteratorOptions opts.PrefetchValues = true @@ -113,6 +117,10 @@ func (c *goodsItemClient) List( prefix := c.prefix() var cursor int for iter.Seek(prefix); iter.ValidForPrefix(prefix); iter.Next() { + if ctx.Err() != nil { + return ctx.Err() + } + cursor++ current := iter.Item() err = current.Value(func(val []byte) error { @@ -144,6 +152,10 @@ func (c *goodsItemClient) Get( ctx context.Context, sku string, ) (out entity.GoodsItem, err error) { + if ctx.Err() != nil { + return out, ctx.Err() + } + err = c.db.View(func(txn *badger.Txn) error { key := unsafe.Slice(unsafe.StringData(sku), len(sku)) 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) { + if ctx.Err() != nil { + return out, ctx.Err() + } + err = c.db.View(func(txn *badger.Txn) error { idxKey := c.prefixedIDByCartInt64(id) 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) { + if ctx.Err() != nil { + return out, ctx.Err() + } + err = c.db.Update(func(txn *badger.Txn) error { skuKey := c.prefixedStr(sku) 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) } + cartID := c.prefixedIDByCartInt64(out.Cart) + err = txn.Delete(cartID) + if err != nil { + return fmt.Errorf("deleting index key: %w", err) + } + return nil }) if err != nil { @@ -223,10 +249,8 @@ func (c *goodsItemClient) upsertByBatch(ctx context.Context, items []entity.Good err := func() error { for _, item := range items { - select { - case <-ctx.Done(): + if ctx.Err() != nil { return ctx.Err() - default: } key := c.prefixedStr(item.Articul)