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

SM-775 - Validate property length #93

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/device-data-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, dbs func() *d
vTokenV2.Get("/history", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData, privileges.VehicleAllTimeLocation}), cacheHandler, deviceDataControllerV2.GetHistoricalPermissioned)
vTokenV2.Get("/analytics/daily-distance", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData}), cacheHandler, deviceDataControllerV2.GetDailyDistance)
vTokenV2.Get("/analytics/total-distance", tk.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleNonLocationData}), cacheHandler, deviceDataControllerV2.GetDistanceDriven)

udMw := owner.New(usersClient, deviceAPIService, &logger)
v1Auth := app.Group("/v1", jwtAuth)

Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/device_data_controller_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (d *DeviceDataControllerV2Suite) TestGetDistanceDriven() {
},
expectedResponse: fiber.StatusBadRequest,
expectedDevice: nil,
customClaims: []privileges.Privilege{privileges.VehicleNonLocationData},
customClaims: []privileges.Privilege{privileges.VehicleNonLocationData},
},
}
d.esMock.EXPECT().GetTotalDistanceDriven(gomock.Any(), expectedDeviceID).Return([]byte(`{"aggregations": {"max_odometer": {"value": 200},"min_odometer": {"value": 50}}}`), nil).AnyTimes()
Expand All @@ -90,7 +90,7 @@ func (d *DeviceDataControllerV2Suite) TestGetDistanceDriven() {
for _, tc := range tests {
app.Use(test.ClaimsInjectorTestHandler(tc.customClaims))
app.Get("/v2/vehicles/:tokenID/analytics/total-distance", test.AuthInjectorTestHandler(testUserID), d.SUT.GetDistanceDriven)

var errDevice error
if tc.expectedDevice == nil {
errDevice = errors.New("device not found")
Expand Down
7 changes: 3 additions & 4 deletions internal/services/vehicle_signals_event_batch_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ func (v *vehicleSignalsEventBatchService) GenerateVehicleDataTracking(ctx contex
const CacheKey = "VehicleDataTrackingProperties"
get, found := v.memoryCache.Get(CacheKey)

eventAvailableProperties := make(map[string]string)
eventAvailableProperties := make(map[string]models.VehicleSignalsAvailableProperty)
if found {
eventAvailableProperties = get.(map[string]string)
eventAvailableProperties = get.(map[string]models.VehicleSignalsAvailableProperty)
} else {
availableProperties, err := models.VehicleSignalsAvailableProperties().All(ctx, v.db().Reader)
if err != nil {
return err
}
for i := 0; i < len(availableProperties); i++ {
eventAvailableProperties[availableProperties[i].Name] = availableProperties[i].ID
eventAvailableProperties[availableProperties[i].Name] = *availableProperties[i]
}
v.memoryCache.Set(CacheKey, eventAvailableProperties, 30*time.Minute)
}
Expand All @@ -76,7 +76,6 @@ func (v *vehicleSignalsEventBatchService) GenerateVehicleDataTracking(ctx contex
deviceDefinitionCount := make(map[string]int)

for _, item := range deviceDataEvents {

device := &pb.UserDevice{}
cachedUD, foundCached := v.memoryCache.Get(item.UserDeviceID + "_" + item.IntegrationID)
if foundCached {
Expand Down
37 changes: 33 additions & 4 deletions internal/services/vehicle_signals_event_property_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type vehicleSignalsEventPropertyService struct {
}

type VehicleSignalsEventPropertyService interface {
GenerateData(ctx context.Context, dateKey string, integrationID string, ud *models.UserDeviceDatum, deviceDefinition *pb.GetDeviceDefinitionItemResponse, eventAvailableProperties map[string]string) error
GenerateData(ctx context.Context, dateKey string, integrationID string, ud *models.UserDeviceDatum, deviceDefinition *pb.GetDeviceDefinitionItemResponse, eventAvailableProperties map[string]models.VehicleSignalsAvailableProperty) error
}

func NewVehicleSignalsEventPropertyService(db func() *db.ReaderWriter, log *zerolog.Logger) VehicleSignalsEventPropertyService {
Expand All @@ -35,7 +35,7 @@ func NewVehicleSignalsEventPropertyService(db func() *db.ReaderWriter, log *zero
}
}

func (v *vehicleSignalsEventPropertyService) GenerateData(ctx context.Context, dateKey string, integrationID string, ud *models.UserDeviceDatum, deviceDefinition *pb.GetDeviceDefinitionItemResponse, eventAvailableProperties map[string]string) error {
func (v *vehicleSignalsEventPropertyService) GenerateData(ctx context.Context, dateKey string, integrationID string, ud *models.UserDeviceDatum, deviceDefinition *pb.GetDeviceDefinitionItemResponse, eventAvailableProperties map[string]models.VehicleSignalsAvailableProperty) error {

var data map[string]interface{}
err := json.Unmarshal(ud.Signals.JSON, &data)
Expand All @@ -45,6 +45,11 @@ func (v *vehicleSignalsEventPropertyService) GenerateData(ctx context.Context, d

for key, value := range eventAvailableProperties {

// Validate if the property should be considered
if !v.shouldValueBeConsidered(key, value, data) {
continue
}

deviceMakeID := deviceDefinition.Make.Id
model := deviceDefinition.Type.Model
year := int(deviceDefinition.Type.Year)
Expand All @@ -70,7 +75,7 @@ func (v *vehicleSignalsEventPropertyService) GenerateData(ctx context.Context, d
DateID: dateKey,
IntegrationID: integrationID,
DeviceMakeID: deviceDefinition.Make.Id,
PropertyID: value,
PropertyID: value.ID,
Year: int(deviceDefinition.Type.Year),
Model: deviceDefinition.Type.Model,
DeviceDefinitionID: deviceDefinition.DeviceDefinitionId,
Expand Down Expand Up @@ -116,7 +121,7 @@ func (v *vehicleSignalsEventPropertyService) GenerateData(ctx context.Context, d
DateID: dateKey,
IntegrationID: integrationID,
DeviceMakeID: deviceDefinition.Make.Id,
PropertyID: value,
PropertyID: value.ID,
Year: int(deviceDefinition.Type.Year),
Model: deviceDefinition.Type.Model,
DeviceDefinitionID: deviceDefinition.DeviceDefinitionId,
Expand Down Expand Up @@ -144,3 +149,27 @@ func (v *vehicleSignalsEventPropertyService) GenerateData(ctx context.Context, d

return nil
}

func (v *vehicleSignalsEventPropertyService) shouldValueBeConsidered(key string, value models.VehicleSignalsAvailableProperty, data map[string]interface{}) bool {
if _, ok := data[key]; ok {
if value.ValidMinLength.Valid && value.ValidMinLength.Int > 0 {
object, ok := data[key].(map[string]interface{})
if !ok {
return false
}

objectValue, ok := object["value"].(string)
if !ok {
return false
}

if len(objectValue) < value.ValidMinLength.Int {
return false
}
}

return true
}

return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
SET search_path = device_data_api, public;
alter table vehicle_signals_available_properties add column valid_min_length int;

update vehicle_signals_available_properties set valid_min_length = 17 where name = 'vin';

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
SET search_path = device_data_api, public;
alter table vehicle_signals_available_properties drop column valid_min_length;
-- +goose StatementEnd
7 changes: 6 additions & 1 deletion models/boil_queries.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/boil_table_names.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/boil_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/boil_view_names.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 50 additions & 12 deletions models/psql_upsert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading