Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added more test cases for + added provision for grpc service name
  • Loading branch information
anandrgitnirman committed May 26, 2022
1 parent cc02a4e commit 5336343
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 92 deletions.
83 changes: 53 additions & 30 deletions training/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (service ModelService) getServiceClient() (conn *grpc.ClientConn, client Mo
client = NewModelClient(conn)
return
}
func (service ModelService) createModelDetails(request *CreateModelRequest, response *ModelDetailsResponse) (err error) {
func (service ModelService) createModelDetails(request *CreateModelRequest, response *ModelDetailsResponse) (data *ModelData, err error) {
key := service.getModelKeyToCreate(request, response)
data := service.createModelData(request, response)

data = service.getModelDataToCreate(request, response)
//store the model details in etcd
err = service.storage.Put(key, data)
//for every accessible address in the list , store the user address and all the model Ids associated with it
for _, address := range data.AuthorizedAddresses {
Expand All @@ -92,11 +92,12 @@ func (service ModelService) createModelDetails(request *CreateModelRequest, resp
}
func getModelUserKey(key *ModelKey, address string) *ModelUserKey {
return &ModelUserKey{
OrganizationId: key.OrganizationId,
ServiceId: key.ServiceId,
GroupId: key.GroupId,
MethodName: key.MethodName,
UserAddress: address,
OrganizationId: key.OrganizationId,
ServiceId: key.ServiceId,
GroupId: key.GroupId,
GRPCMethodName: key.GRPCMethodName,
GRPCServiceName: key.GRPCServiceName,
UserAddress: address,
}
}

Expand All @@ -112,7 +113,7 @@ func (service ModelService) getModelUserData(key *ModelKey, address string) *Mod
OrganizationId: key.OrganizationId,
ServiceId: key.ServiceId,
GroupId: key.GroupId,
MethodName: key.MethodName,
GRPCMethodName: key.GRPCMethodName,
UserAddress: address,
ModelIds: modelIds,
}
Expand Down Expand Up @@ -151,9 +152,9 @@ func (service ModelService) deleteModelDetails(request *UpdateModelRequest) (err
}
func convertModelDataToBO(data *ModelData) (responseData *ModelDetails) {
responseData = &ModelDetails{
ModelId: data.ModelId,
MethodName: data.MethodName,
Description: data.Description,
ModelId: data.ModelId,
GrpcMethodName: data.GRPCMethodName,
Description: data.Description,
}
return
}
Expand All @@ -166,7 +167,7 @@ func (service ModelService) updateModelDetails(request *UpdateModelRequest, resp
copy(oldAddresses, data.AuthorizedAddresses)
if data, ok, err := service.storage.Get(key); err != nil && ok {
data.AuthorizedAddresses = request.AddressList
data.isPublic = request.IsPubliclyAccessible
data.IsPublic = request.IsPubliclyAccessible
data.UpdatedByAddress = request.ModelDetailsRequest.Authorization.SignerAddress
data.Status = string(response.Status)
}
Expand Down Expand Up @@ -232,11 +233,12 @@ func (service ModelService) updateModelDetailsForStatus(request *ModelDetailsReq
}
func (service ModelService) getModelKeyToCreate(request *CreateModelRequest, response *ModelDetailsResponse) (key *ModelKey) {
key = &ModelKey{
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: service.organizationMetaData.GetGroupIdString(),
MethodName: request.MethodName,
ModelId: response.ModelDetails.ModelId,
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: service.organizationMetaData.GetGroupIdString(),
GRPCMethodName: request.ModelDetails.GrpcMethodName,
GRPCServiceName: request.ModelDetails.GrpcServiceName,
ModelId: response.ModelDetails.ModelId,
}
return
}
Expand All @@ -246,7 +248,7 @@ func (service ModelService) getModelKeyToUpdate(request *ModelDetailsRequest) (k
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: service.organizationMetaData.GetGroupIdString(),
MethodName: request.ModelDetails.MethodName,
GRPCMethodName: request.ModelDetails.GrpcMethodName,
ModelId: request.ModelDetails.ModelId,
}
return
Expand Down Expand Up @@ -279,7 +281,7 @@ func (service ModelService) GetAllModels(c context.Context, request *AccessibleM
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: service.organizationMetaData.GetGroupIdString(),
MethodName: request.MethodName,
GRPCMethodName: request.MethodName,
UserAddress: request.Authorization.SignerAddress,
}
modelDetailsArray := make([]*ModelDetails, 0)
Expand All @@ -289,7 +291,7 @@ func (service ModelService) GetAllModels(c context.Context, request *AccessibleM
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: service.organizationMetaData.GetGroupIdString(),
MethodName: request.MethodName,
GRPCMethodName: request.MethodName,
ModelId: modelId,
}
if modelData, modelOk, modelErr := service.storage.Get(modelKey); modelOk && modelData != nil && modelErr != nil {
Expand All @@ -303,12 +305,17 @@ func (service ModelService) GetAllModels(c context.Context, request *AccessibleM
return
}

func (service ModelService) createModelData(request *CreateModelRequest, response *ModelDetailsResponse) (data *ModelData) {
func (service ModelService) getModelDataToCreate(request *CreateModelRequest, response *ModelDetailsResponse) (data *ModelData) {

data = &ModelData{
Status: string(response.Status),
GRPCServiceName: request.ModelDetails.GrpcServiceName,
GRPCMethodName: request.ModelDetails.GrpcMethodName,
CreatedByAddress: request.Authorization.SignerAddress,
AuthorizedAddresses: request.AddressList,
isPublic: request.IsPubliclyAccessible,
UpdatedByAddress: request.Authorization.SignerAddress,
AuthorizedAddresses: request.ModelDetails.AddressList,
IsPublic: request.ModelDetails.IsPubliclyAccessible,
IsDefault: request.ModelDetails.IsDefaultModel,
ModelId: response.ModelDetails.ModelId,
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
Expand Down Expand Up @@ -341,8 +348,10 @@ func (service ModelService) CreateModel(c context.Context, request *CreateModelR
response, err = client.CreateModel(c, request)
if err == nil {
//store the details in etcd
log.Infof("Creating model based on response from CreateModel")
if err = service.createModelDetails(request, response); err != nil {
log.Infof("Creating model based on response from CreateModel of training service")
if data, err := service.createModelDetails(request, response); err == nil {
response = BuildCreateModelResponse(data)
} else {
return response, fmt.Errorf("issue with storing Model Id in the Daemon Storage %v", err)
}
}
Expand All @@ -351,9 +360,26 @@ func (service ModelService) CreateModel(c context.Context, request *CreateModelR
return &ModelDetailsResponse{Status: Status_ERROR},
fmt.Errorf("error in invoking service for Model Training %v", err)
}

return
}

func BuildCreateModelResponse(data *ModelData) *ModelDetailsResponse {
return &ModelDetailsResponse{
Status: 0,
ModelDetails: &ModelDetails{
ModelId: data.ModelId,
GrpcMethodName: data.GRPCMethodName,
GrpcServiceName: data.GRPCServiceName,
Description: data.Description,
IsPubliclyAccessible: data.IsPublic,
AddressList: data.AuthorizedAddresses,
TrainingDataLink: data.TrainingLink,
IsDefaultModel: data.IsDefault,
OrganizationId: data.OrganizationId,
ServiceId: data.ServiceId,
},
}
}
func (service ModelService) UpdateModelAccess(c context.Context, request *UpdateModelRequest) (response *ModelDetailsResponse,
err error) {
if request == nil || request.ModelDetailsRequest == nil || request.ModelDetailsRequest.Authorization == nil {
Expand Down Expand Up @@ -465,6 +491,3 @@ func NewModelService(channelService escrow.PaymentChannelService, serMetaData *b
return &NoModelSupportService{}
}
}

type IModelService interface {
}
49 changes: 29 additions & 20 deletions training/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,28 @@ func (suite *ModelServiceTestSuite) TestModelService_CreateModel() {
Signature: suite.getSignature("__CreateModel", 1200, suite.senderPvtKy),
CurrentBlock: 1200,
},
MethodName: "TESTMETHOD",
ModelDescription: "Just Testing",
IsPubliclyAccessible: false,
AddressList: []string{"A1", "A2", "A3"},
ModelDetails: &ModelDetails{
GrpcServiceName: "TESTSERVICE",
GrpcMethodName: "TESTMETHOD",
Description: "Just Testing",
IsPubliclyAccessible: false,
AddressList: []string{"A1", "A2", "A3"},
},
}
fmt.Println(suite.senderAddress.String())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2000)
defer cancel()
response, err = suite.service.CreateModel(ctx, request)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "1", response.ModelDetails.ModelId)
assert.Equal(suite.T(), response.ModelDetails.AddressList, []string{"A1", "A2", "A3", suite.senderAddress.String()})
userKey := &ModelUserKey{
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: suite.service.(*ModelService).organizationMetaData.GetGroupIdString(),
MethodName: "TESTMETHOD",
UserAddress: "A1",
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: suite.service.(*ModelService).organizationMetaData.GetGroupIdString(),
GRPCMethodName: "TESTMETHOD",
GRPCServiceName: "TESTSERVICE",
UserAddress: "A1",
}
//check if we have stored the user's associated model Ids
data, ok, err := suite.service.(*ModelService).userStorage.Get(userKey)
Expand All @@ -177,11 +182,12 @@ func (suite *ModelServiceTestSuite) TestModelService_CreateModel() {

//check if the model Id stored has all the details
key := &ModelKey{
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: suite.service.(*ModelService).organizationMetaData.GetGroupIdString(),
MethodName: "TESTMETHOD",
ModelId: "1",
OrganizationId: config.GetString(config.OrganizationId),
ServiceId: config.GetString(config.ServiceId),
GroupId: suite.service.(*ModelService).organizationMetaData.GetGroupIdString(),
GRPCMethodName: "TESTMETHOD",
GRPCServiceName: "TESTSERVICE",
ModelId: "1",
}
modelData, ok, err := suite.service.(*ModelService).storage.Get(key)
assert.Equal(suite.T(), []string{"A1", "A2", "A3", suite.senderAddress.String()}, modelData.AuthorizedAddresses)
Expand Down Expand Up @@ -211,8 +217,9 @@ func (suite *ModelServiceTestSuite) TestModelService_DeleteModel() {
request := &UpdateModelRequest{
ModelDetailsRequest: &ModelDetailsRequest{
ModelDetails: &ModelDetails{
ModelId: "1",
MethodName: "TESTMETHOD",
ModelId: "1",
GrpcServiceName: "TESTSERVICE",
GrpcMethodName: "TESTMETHOD",
},
Authorization: authorization,
},
Expand All @@ -231,8 +238,9 @@ func (suite *ModelServiceTestSuite) TestModelService_DeleteModel() {
func (suite *ModelServiceTestSuite) TestModelService_GetModelStatus() {
request := &ModelDetailsRequest{
ModelDetails: &ModelDetails{
ModelId: "1",
MethodName: "TESTMETHOD",
ModelId: "1",
GrpcServiceName: "TESTSERVICE",
GrpcMethodName: "TESTMETHOD",
},
Authorization: &AuthorizationDetails{
SignerAddress: suite.senderAddress.String(),
Expand All @@ -253,8 +261,9 @@ func (suite *ModelServiceTestSuite) TestModelService_UpdateModelAccess() {
request := &UpdateModelRequest{
ModelDetailsRequest: &ModelDetailsRequest{
ModelDetails: &ModelDetails{
ModelId: "1",
MethodName: "TESTMETHOD",
ModelId: "1",
GrpcServiceName: "TESTSERVICE",
GrpcMethodName: "TESTMETHOD",
},
Authorization: &AuthorizationDetails{
SignerAddress: suite.senderAddress.String(),
Expand Down
47 changes: 26 additions & 21 deletions training/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ func NewModelStorage(atomicStorage storage.AtomicStorage) *ModelStorage {
}

type ModelUserKey struct {
OrganizationId string
ServiceId string
GroupId string
MethodName string
UserAddress string
OrganizationId string
ServiceId string
GroupId string
GRPCMethodName string
GRPCServiceName string
UserAddress string
}

func (key *ModelUserKey) String() string {
return fmt.Sprintf("{ID:%v/%v/%v/%v/%v}", key.OrganizationId,
key.ServiceId, key.GroupId, key.MethodName, key.UserAddress)
return fmt.Sprintf("{ID:%v/%v/%v/%v/%v/%v}", key.OrganizationId,
key.ServiceId, key.GroupId, key.GRPCServiceName, key.GRPCMethodName, key.UserAddress)
}

// ModelUserData maintain the list of all modelIds for a given user address
Expand All @@ -52,38 +53,39 @@ type ModelUserData struct {
OrganizationId string
ServiceId string
GroupId string
MethodName string
GRPCMethodName string
UserAddress string
}

func (data *ModelUserData) String() string {
return fmt.Sprintf("{DATA:%v/%v/%v/%v/%v,ModelsId:%v}",
data.OrganizationId,
data.ServiceId, data.GroupId, data.MethodName, data.UserAddress, data.ModelIds)
data.ServiceId, data.GroupId, data.GRPCMethodName, data.UserAddress, data.ModelIds)
}

type ModelKey struct {
OrganizationId string
ServiceId string
GroupId string
MethodName string
ModelId string
OrganizationId string
ServiceId string
GroupId string
GRPCMethodName string
GRPCServiceName string
ModelId string
}

func (key *ModelKey) String() string {
return fmt.Sprintf("{ID:%v/%v/%v/%v/%v}", key.OrganizationId,
key.ServiceId, key.GroupId, key.MethodName, key.ModelId)
return fmt.Sprintf("{ID:%v/%v/%v/%v/%v/%v}", key.OrganizationId,
key.ServiceId, key.GroupId, key.GRPCServiceName, key.GRPCMethodName, key.ModelId)
}

func (data *ModelData) String() string {
return fmt.Sprintf("{DATA:%v/%v/%v/%v/%v/isPublic:%v/accesibleAddress:%v/createdBy:%v/updatedBy:%v/status:%v}",
return fmt.Sprintf("{DATA:%v/%v/%v/%v/%v/%v/IsPublic:%v/accesibleAddress:%v/createdBy:%v/updatedBy:%v/status:%v/TrainingLin:%v}",
data.OrganizationId,
data.ServiceId, data.GroupId, data.MethodName, data.ModelId, data.AuthorizedAddresses, data.isPublic,
data.CreatedByAddress, data.UpdatedByAddress, data.Status)
data.ServiceId, data.GroupId, data.GRPCServiceName, data.GRPCMethodName, data.ModelId, data.AuthorizedAddresses, data.IsPublic,
data.CreatedByAddress, data.UpdatedByAddress, data.Status, data.TrainingLink)
}

type ModelData struct {
isPublic bool
IsPublic bool
AuthorizedAddresses []string
Status string
CreatedByAddress string
Expand All @@ -92,8 +94,11 @@ type ModelData struct {
GroupId string
OrganizationId string
ServiceId string
MethodName string
GRPCMethodName string
GRPCServiceName string
Description string
IsDefault bool
TrainingLink string
}

func serializeModelKey(key interface{}) (serialized string, err error) {
Expand Down
4 changes: 2 additions & 2 deletions training/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ModelStorageSuite struct {

func (suite *ModelStorageSuite) getUserModelKey(modelId string) *ModelKey {
return &ModelKey{OrganizationId: suite.organizationId, GroupId: suite.groupId,
ServiceId: suite.serviceId, ModelId: modelId, MethodName: suite.methodName}
ServiceId: suite.serviceId, ModelId: modelId, GRPCMethodName: suite.methodName}
}

func (suite *ModelStorageSuite) getUserModelData(modelId string) *ModelData {
Expand All @@ -30,7 +30,7 @@ func (suite *ModelStorageSuite) getUserModelData(modelId string) *ModelData {
OrganizationId: suite.organizationId,
ServiceId: suite.serviceId,
GroupId: suite.groupId,
MethodName: suite.methodName,
GRPCMethodName: suite.methodName,
AuthorizedAddresses: suite.accessibleAddress,
CreatedByAddress: suite.accessibleAddress[0],
UpdatedByAddress: suite.accessibleAddress[1],
Expand Down
Loading

0 comments on commit 5336343

Please sign in to comment.