Skip to content

Commit

Permalink
load csv, iterate and stop each connection
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesReate committed Feb 3, 2025
1 parent 90235af commit c632bf1
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cmd/devices-api/smartcar_stop_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package main

import (
"context"
"encoding/csv"
"flag"
"fmt"
"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/google/subcommands"
"github.com/rs/zerolog"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)

type smartcarStopConnectionsCmd struct {
Expand All @@ -34,9 +39,75 @@ func (p *smartcarStopConnectionsCmd) SetFlags(_ *flag.FlagSet) {

func (p *smartcarStopConnectionsCmd) Execute(ctx context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
const filename = "smartcar_stop_connections.csv"
const smartcarIntegrationID = "22N2xaPOq2WW2gAHBHd0Ikn4Zob"
// open csv file
// read VIN's, get the user device api integration filtered by smartcar and get the relation to userdevice
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err, filename)
return subcommands.ExitFailure
}
defer file.Close()

// Read the CSV file
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error reading CSV file:", err)
return subcommands.ExitFailure
}

if len(records) == 0 {
fmt.Println("CSV file is empty")
return subcommands.ExitFailure
}

// Find the index of the 'vin' column
var vinIndex = -1
for i, columnName := range records[0] {
if columnName == "vin" {
vinIndex = i
break
}
}

if vinIndex == -1 {
fmt.Println("Column 'vin' not found in CSV file")
return subcommands.ExitFailure
}

// Print the VIN values
fmt.Println("VIN values found in CSV file:", len(records))
for _, row := range records[1:] { // Skip header row
if vinIndex < len(row) {
vin := row[vinIndex]
fmt.Println("VIN:", vin)

// get the user device api integration filtered by smartcar and get the relation to userdevice
ud, err := models.UserDevices(models.UserDeviceWhere.VinIdentifier.EQ(null.StringFrom(vin)),
models.UserDeviceWhere.VinConfirmed.EQ(true)).One(ctx, p.pdb.DBS().Reader)
if err != nil {
fmt.Println("Error getting user device, continuing:", err)
continue
}

scInt, err := models.UserDeviceAPIIntegrations(
models.UserDeviceAPIIntegrationWhere.UserDeviceID.EQ(ud.ID),
models.UserDeviceAPIIntegrationWhere.IntegrationID.EQ(smartcarIntegrationID),
qm.Load(models.UserDeviceAPIIntegrationRels.UserDevice)).One(ctx, p.pdb.DBS().Reader)
if err != nil {
fmt.Println("Error getting user device api integration, continuing:", err)
continue
}

err = p.stopConnections(ctx, scInt)
if err != nil {
fmt.Println("Error stopping connections, continuing:", err)
continue
}
fmt.Println("Stopped connections for VIN:", vin)
}
}
return subcommands.ExitSuccess
}

Expand Down

0 comments on commit c632bf1

Please sign in to comment.