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,7 +27,7 @@ func ItemsCommandTree() *cli.Command {
newItemsCountCommand(h),
newItemsUniqueParamsCommand(h),
newItemsAggregateParametersCommand(h),
newItemsFillSizesCommand(h),
newItemsFixSizesCommand(h),
},
}
}
@ -100,13 +100,13 @@ func newItemsAggregateParametersCommand(h itemsHandlers) *cli.Command {
return cmdWithAction(cmd, h.AggregateParameters)
}
func newItemsFillSizesCommand(h itemsHandlers) *cli.Command {
func newItemsFixSizesCommand(h itemsHandlers) *cli.Command {
cmd := cli.Command{
Name: "fix-sizes",
Usage: "Iterates over params and sets sizes from parameters",
}
return cmdWithAction(cmd, h.FillSizes)
return cmdWithAction(cmd, h.FixSizes)
}
type itemsHandlers struct{}
@ -186,13 +186,10 @@ func (itemsHandlers) Count(ctx context.Context, cmd *cli.Command) error {
}
if len(seenPatterns) == len(patternMapped) {
println("Item matched", item.Articul, item.Cart)
count++
}
}
println(count)
return nil
}
@ -304,13 +301,19 @@ func (itemsHandlers) AggregateParameters(ctx context.Context, cmd *cli.Command)
return bw.Flush()
}
func (itemsHandlers) FillSizes(ctx context.Context, cmd *cli.Command) error {
func (itemsHandlers) FixSizes(ctx context.Context, cmd *cli.Command) error {
repository, err := components.GetRepository()
if err != nil {
return fmt.Errorf("getting repository: %w", err)
}
dimensionDispatcher := makeDefaultDimensionDispatcher()
matcher, err := components.GetDimensionMatcher()
if err != nil {
return fmt.Errorf("getting dimension matcher: %w", err)
}
dimensionDispatcher := dimensionDispatcher{m: matcher}
log := zerolog.Ctx(ctx)
toUpdate := make([]entity.GoodsItem, 0, 20_000)
@ -354,6 +357,15 @@ func (itemsHandlers) FillSizes(ctx context.Context, cmd *cli.Command) error {
}
}
var updated bool
oldSizes := item.Sizes
item.Sizes, updated = entity.FixupSizes(oldSizes)
if updated {
log.Info().Int64("cart", item.Cart).Any("old", oldSizes).Any("new", item.Sizes).Msg("sizes been fixed")
}
valueBeenUpdated = valueBeenUpdated || updated
if valueBeenUpdated {
toUpdate = append(toUpdate, item)
}