Skip to content

Commit

Permalink
SM-1239 implement script for vin decode comparison (#408)
Browse files Browse the repository at this point in the history
* implement script for vin decode comparison

* linter
  • Loading branch information
JamesReate authored Oct 29, 2024
1 parent 1c8b96b commit 5a5caab
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fmt:
@go mod tidy

lint:
@go vet $(GO_FLAGS) ./...
@golangci-lint run

test: $(APPS)
@go test $(GO_FLAGS) -timeout 3m -race ./...
Expand Down
1 change: 1 addition & 0 deletions cmd/devices-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func main() {
subcommands.Register(&stopTaskByKeyCmd{logger: logger, settings: settings, container: deps}, "tasks")

subcommands.Register(&syncDeviceTemplatesCmd{logger: logger, settings: settings, pdb: pdb}, "user devices")
subcommands.Register(&vinDecodeCompareCmd{logger: logger, settings: settings, pdb: pdb}, "user devices")

flag.Parse()
os.Exit(int(subcommands.Execute(ctx)))
Expand Down
100 changes: 100 additions & 0 deletions cmd/devices-api/vin_decode_compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"context"
"encoding/csv"
"flag"
"math/big"
"os"

"github.com/DIMO-Network/devices-api/internal/config"
"github.com/DIMO-Network/devices-api/internal/services"
"github.com/DIMO-Network/devices-api/models"
"github.com/DIMO-Network/shared/db"
"github.com/ericlagergren/decimal"
"github.com/google/subcommands"
"github.com/rs/zerolog"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"github.com/volatiletech/sqlboiler/v4/types"
)

type vinDecodeCompareCmd struct {
logger zerolog.Logger
settings config.Settings
pdb db.Store
}

func (*vinDecodeCompareCmd) Name() string { return "vin-decode-compare" }
func (*vinDecodeCompareCmd) Synopsis() string {
return "iterate through all minted vehicles and decode their VIN comparing resulting definition id"
}
func (*vinDecodeCompareCmd) Usage() string {
return `vin-decode-compare`
}

func (p *vinDecodeCompareCmd) SetFlags(_ *flag.FlagSet) {
// p.targetTemplateID = f.String("target-template", "", "Filter device definitions to apply for where the template is this value. Good for moving only certain autopi's.")
//f.BoolVar(&p.moveAllDevices, "move-all-devices", false, "move all devices in autopi to the template specified.")
}

func (p *vinDecodeCompareCmd) Execute(ctx context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
ddSvc := services.NewDeviceDefinitionService(p.pdb.DBS, &p.logger, &p.settings)

file, err := os.OpenFile("vin-decode-compare.csv", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
defer file.Close() // nolint
if err != nil {
p.logger.Error().Err(err).Msg("failed to open file")
return subcommands.ExitFailure
}
// todo: check if file exists and what token left off on

writer := csv.NewWriter(file)
defer writer.Flush()
// Write header
err = writer.Write([]string{"Token ID", "VIN", "Original Definition ID", "New Definition ID"})
if err != nil {
p.logger.Error().Err(err).Msg("failed to write CSV header")
return subcommands.ExitFailure
}

keepGoing := true
zero := big.NewInt(int64(0))
markerTokenID := types.NewNullDecimal(new(decimal.Big).SetBigMantScale(zero, 0))

for keepGoing {
uds, err := models.UserDevices(models.UserDeviceWhere.TokenID.IsNotNull(), models.UserDeviceWhere.VinConfirmed.EQ(true),
models.UserDeviceWhere.TokenID.GT(markerTokenID),
qm.OrderBy(models.UserDeviceColumns.TokenID), qm.Limit(1000)).All(ctx, p.pdb.DBS().Reader)
if err != nil {
p.logger.Error().Err(err).Msg("failed to get user devices")
return subcommands.ExitFailure
}
if len(uds) < 999 {
keepGoing = false
}
tk, _ := markerTokenID.Uint64()
p.logger.Info().Msgf("processing %d user devices, starting at token_id: %d", len(uds), tk)

markerTokenID = uds[len(uds)-1].TokenID

for _, ud := range uds {
decodeVIN, err := ddSvc.DecodeVIN(ctx, ud.VinIdentifier.String, "", 0, ud.CountryCode.String)
t, _ := ud.TokenID.Uint64()
if err != nil {
p.logger.Error().Uint64("token_id", t).Err(err).Msg("failed to decode vin")
}
if ud.DefinitionID != decodeVIN.DefinitionId {
// log and csv
p.logger.Info().Uint64("token_id", t).Str("vin", ud.VinIdentifier.String).
Msgf("vin decode mismatch. original: %s, new: %s", ud.DefinitionID, decodeVIN.DefinitionId)

errW := writer.Write([]string{ud.TokenID.String(), ud.VinIdentifier.String, ud.DefinitionID, decodeVIN.DefinitionId})
if errW != nil {
p.logger.Error().Err(errW).Msg("failed to write CSV row")
}
}
}
}

return subcommands.ExitSuccess
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.23.2

require (
github.com/DIMO-Network/device-data-api v0.9.10
github.com/DIMO-Network/device-definitions-api v1.2.36
github.com/DIMO-Network/device-definitions-api v1.2.51
github.com/DIMO-Network/go-mnemonic v0.0.0-20230406181942-6ddfe6f8c21c
github.com/DIMO-Network/meta-transaction-processor v0.3.2
github.com/DIMO-Network/shared v0.12.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/DIMO-Network/device-data-api v0.9.10 h1:ORyxDSKPCEPFGG5S593+2OkLzWFsy
github.com/DIMO-Network/device-data-api v0.9.10/go.mod h1:tjR1bA9sw2sj5GNi88dqpGsGLjYHjQzSfv2lcCYLjqQ=
github.com/DIMO-Network/device-definitions-api v1.2.36 h1:tRWLGjEfzkoF45tZfz5nRibbXpTNAvvuMVt+KdCehEc=
github.com/DIMO-Network/device-definitions-api v1.2.36/go.mod h1:Zwrxqb4SKAzS/10yzO7ULLCqqWbWpp/5uwmqDxyBe1k=
github.com/DIMO-Network/device-definitions-api v1.2.51 h1:kMCy8yS+KwGzTfQnXDfA1MJFlqqxNLfAcyCCI4jC/gw=
github.com/DIMO-Network/device-definitions-api v1.2.51/go.mod h1:c9oOnRXxPQvgQOBKYkaf3ZIcpb9fQkdmd/kHmNk6ZR8=
github.com/DIMO-Network/go-mnemonic v0.0.0-20230406181942-6ddfe6f8c21c h1:sKuB4i3m/5NpLjO6tKyc2gSqQHmRe02sMP+pQqJNTuQ=
github.com/DIMO-Network/go-mnemonic v0.0.0-20230406181942-6ddfe6f8c21c/go.mod h1:K5a/Iqh9iZYdMSh3UE3J+tc4aV1PxHe4Fz0ZEhZMPbg=
github.com/DIMO-Network/meta-transaction-processor v0.3.2 h1:eSf34mSmiRilMbQx+4Iyu6aNm/WWJjbopIGYNjz/Z6w=
Expand Down

0 comments on commit 5a5caab

Please sign in to comment.