Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRO-1852 refactor, update to uber gomock, remove unused code, update how we create a user device #194

Merged
merged 11 commits into from
Sep 1, 2023
18 changes: 15 additions & 3 deletions cmd/devices-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (
"syscall"
"time"

"github.com/DIMO-Network/devices-api/internal/middleware"

"github.com/DIMO-Network/devices-api/internal/rpc"

"github.com/DIMO-Network/devices-api/internal/middleware/metrics"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"

"github.com/DIMO-Network/shared/redis"

"github.com/DIMO-Network/shared/db"
Expand Down Expand Up @@ -106,6 +110,7 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,
hardwareTemplateService := autopi.NewHardwareTemplateService(autoPiSvc, pdb.DBS, &logger)
autoPi := autopi.NewIntegration(pdb.DBS, ddSvc, autoPiSvc, autoPiTaskService, autoPiIngest, eventService, deviceDefinitionRegistrar, hardwareTemplateService, &logger)
macaron := macaron.NewIntegration(pdb.DBS, ddSvc, autoPiIngest, eventService, deviceDefinitionRegistrar, &logger)
userDeviceSvc := services.NewUserDeviceService(ddSvc, logger, pdb.DBS, eventService)

openAI := services.NewOpenAI(&logger, *settings)
dcnSvc := registry.NewDcnService(settings)
Expand All @@ -131,7 +136,10 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,
}

// controllers
userDeviceController := controllers.NewUserDevicesController(settings, pdb.DBS, &logger, ddSvc, ddIntSvc, eventService, smartcarClient, scTaskSvc, teslaSvc, teslaTaskService, cipher, autoPiSvc, services.NewNHTSAService(), autoPiIngest, deviceDefinitionRegistrar, autoPiTaskService, producer, s3NFTServiceClient, autoPi, redisCache, openAI, usersClient, ddaSvc, natsSvc, wallet, valuationsSvc)
userDeviceController := controllers.NewUserDevicesController(settings, pdb.DBS, &logger, ddSvc, ddIntSvc, eventService,
smartcarClient, scTaskSvc, teslaSvc, teslaTaskService, cipher, autoPiSvc, services.NewNHTSAService(), autoPiIngest,
deviceDefinitionRegistrar, autoPiTaskService, producer, s3NFTServiceClient, autoPi, redisCache, openAI, usersClient,
ddaSvc, natsSvc, wallet, userDeviceSvc, valuationsSvc)
geofenceController := controllers.NewGeofencesController(settings, pdb.DBS, &logger, producer, ddSvc)
webhooksController := controllers.NewWebhooksController(settings, pdb.DBS, &logger, autoPiSvc, ddIntSvc)
documentsController := controllers.NewDocumentsController(settings, &logger, s3ServiceClient, pdb.DBS)
Expand Down Expand Up @@ -359,7 +367,7 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,
logger.Fatal().Err(err).Msg("Failed to create transaction listener")
}

go startGRPCServer(settings, pdb.DBS, hardwareTemplateService, &logger, ddSvc, eventService, iss)
go startGRPCServer(settings, pdb.DBS, hardwareTemplateService, &logger, ddSvc, eventService, iss, userDeviceSvc)

// start task consumer for autopi
autoPiTaskService.StartConsumer(ctx)
Expand Down Expand Up @@ -397,23 +405,27 @@ func startGRPCServer(
deviceDefSvc services.DeviceDefinitionService,
eventService services.EventService,
vcIss *issuer.Issuer,
userDeviceSvc services.UserDeviceService,
) {
lis, err := net.Listen("tcp", ":"+settings.GRPCPort)
if err != nil {
logger.Fatal().Err(err).Msgf("Couldn't listen on gRPC port %s", settings.GRPCPort)
}

logger.Info().Msgf("Starting gRPC server on port %s", settings.GRPCPort)
gp := middleware.GRPCPanicker{Logger: logger}
server := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
metrics.GRPCMetricsMiddleware(),
grpc_ctxtags.UnaryServerInterceptor(),
grpc_prometheus.UnaryServerInterceptor,
recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(gp.GRPCPanicRecoveryHandler)),
)),
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
)

pb.RegisterUserDeviceServiceServer(server, rpc.NewUserDeviceService(dbs, settings, hardwareTemplateService, logger, deviceDefSvc, eventService, vcIss))
pb.RegisterUserDeviceServiceServer(server, rpc.NewUserDeviceRPCService(dbs, settings, hardwareTemplateService, logger,
deviceDefSvc, eventService, vcIss, userDeviceSvc))
pb.RegisterAftermarketDeviceServiceServer(server, rpc.NewAftermarketDeviceService(dbs, logger))

if err := server.Serve(lis); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/devices-api/generate_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"flag"

"github.com/DIMO-Network/shared"

"github.com/DIMO-Network/devices-api/internal/constants"

ddgrpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc"
Expand Down Expand Up @@ -90,7 +92,7 @@ func generateEvents(logger zerolog.Logger, pdb db.Store, eventService services.E
}

err = eventService.Emit(
&services.Event{
&shared.CloudEvent[any]{
Type: constants.UserDeviceCreationEventType,
Subject: device.UserID,
Source: "devices-api",
Expand Down Expand Up @@ -149,7 +151,7 @@ func generateEvents(logger zerolog.Logger, pdb db.Store, eventService services.E
}

err = eventService.Emit(
&services.Event{
&shared.CloudEvent[any]{
Type: "com.dimo.zone.device.integration.create",
Subject: scInteg.UserDeviceID,
Source: "devices-api",
Expand Down
4 changes: 2 additions & 2 deletions cmd/devices-api/populate_powertrain.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func populateUSAPowertrain(ctx context.Context, logger *zerolog.Logger, pdb db.S
if len(resp.DeviceAttributes) > 0 {
// Find device attribute (powertrain_type)
for _, item := range resp.DeviceAttributes {
if item.Name == constants.PowerTrainType {
powertrainType := deviceDefSvc.ConvertPowerTrainStringToPowertrain(item.Value)
if item.Name == constants.PowerTrainTypeKey {
powertrainType := services.ConvertPowerTrainStringToPowertrain(item.Value)
md.PowertrainType = &powertrainType
if err := device.Metadata.Marshal(md); err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/DIMO-Network/device-data-api v0.4.23-0.20230718210255-60a2c7be0aa7
github.com/DIMO-Network/device-definitions-api v1.0.16
github.com/DIMO-Network/go-mnemonic v0.0.0-20230406181942-6ddfe6f8c21c
github.com/DIMO-Network/shared v0.10.2
github.com/DIMO-Network/shared v0.10.4
github.com/DIMO-Network/synthetic-wallet-instance v0.0.0-20230601233541-6a4c8afb27d3
github.com/DIMO-Network/valuations-api v0.2.0
github.com/DIMO-Network/zflogger v1.0.0-beta
Expand All @@ -25,9 +25,9 @@ require (
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gofiber/fiber/v2 v2.48.0
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/golang/mock v1.6.0
github.com/google/subcommands v1.2.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/jarcoal/httpmock v1.1.0
github.com/lib/pq v1.10.8
Expand All @@ -48,6 +48,7 @@ require (
github.com/volatiletech/sqlboiler/v4 v4.14.2
github.com/volatiletech/strmangle v0.0.4
go.uber.org/automaxprocs v1.5.1
go.uber.org/mock v0.2.0
golang.org/x/mod v0.12.0
)

Expand All @@ -65,6 +66,7 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/uint256 v1.2.2 // indirect
github.com/lovoo/goka v1.1.7 // indirect
Expand Down Expand Up @@ -164,7 +166,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
golang.org/x/crypto v0.12.0 // indirect
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.30.0
google.golang.org/protobuf v1.31.0
)

require (
Expand Down Expand Up @@ -194,7 +196,7 @@ require (
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/volatiletech/randomize v0.0.1 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down
Loading
Loading