refactor logging and filters

This commit is contained in:
Aleksandr Trushkin
2024-02-19 18:11:59 +03:00
parent 149cde5b22
commit fd9e5ade18
14 changed files with 409 additions and 92 deletions

View File

@ -27,6 +27,10 @@ import (
"github.com/urfave/cli/v3"
)
const (
defaultItemType = "Электрика"
)
type empty entity.Empty
func main() {
@ -594,7 +598,9 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
RemmantsAtleast: 5,
})
if err != nil {
return fmt.Errorf("getting next goods batch: %w", err)
logger.Warn().Err(err).Msg("unable to get items from catalog")
continue
}
productIDs = productIDs[:0]
@ -604,6 +610,7 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
remnants, err := client.GetGoodsRemnants(ctx, productIDs)
if err != nil {
logger.Warn().Err(err).Msg("unable to get goods remnants")
return fmt.Errorf("getting goods remnants: %w", err)
}
@ -613,6 +620,7 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
seenItem := seenItems[item.SKU]
if time.Since(seenItem.CreatedAt) < time.Hour*24 {
logger.Debug().Str("sku", item.SKU).Msg("skipping item because it's too fresh")
stats.skippedItem++
itemsUpdated[item.SKU] = empty{}
@ -626,14 +634,16 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
} else {
pi, err = client.GetProductInfo(ctx, int64(item.Cart))
if err != nil {
return fmt.Errorf("getting product info: %w", err)
logger.Warn().Err(err).Msg("unable to get product info, skipping")
continue
}
stats.fetchedInfo++
}
goodsItem, err := entity.MakeGoodsItem(item, remnants, pi)
if err != nil {
logger.Warn().Err(err).Any("item", item).Msg("unable to make goods item")
logger.Err(err).Any("item", item).Msg("unable to make goods item")
continue
}
@ -646,7 +656,8 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
goodsItems = append(goodsItems, goodsItem)
if goodsItem.Type == "" {
continue
logger.Warn().Int64("cart_id", goodsItem.Cart).Msg("found item without category, setting default type")
goodsItem.Type = defaultItemType
}
if _, ok := knownCategories[goodsItem.Type]; ok {
@ -658,7 +669,7 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("creating category: %w", err)
}
logger.Debug().
logger.Info().
Str("name", category.Name).
Int64("id", category.ID).
Msg("created new category")
@ -673,8 +684,8 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
progressFloat := float64(start) / float64(total)
progress := big.NewFloat(progressFloat).Text('f', 3)
elapsed := time.Since(startFrom).Seconds()
var left int
if progressFloat != 0 {
left = int(((1 - progressFloat) / progressFloat) * elapsed)
@ -711,11 +722,15 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
for k := range seenItems {
_, err := repository.GoodsItem().Delete(ctx, k)
if err != nil {
if errors.Is(err, entity.ErrNotFound) {
continue
}
logger.Warn().Err(err).Str("sku", k).Msg("unable to delete item")
continue
}
logger.Debug().Str("sku", k).Msg("deleted item")
logger.Info().Str("sku", k).Msg("deleted item")
}
return nil