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

Implement the MPC use case for Monitored Units #108

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from

Conversation

sthelen-enqs
Copy link
Contributor

This adds an implementation of the MPC use case for Monitored Units.

The current implementation has 2 major restrictions so far:

  1. it automatically configures itself as a 3-phase meter (ABC) and there's no way to specify a different configuration right now
  2. it only supports the required phase-to-neutral and not the phase-to-phase voltages

I'm opening this as a Draft PR because while this UC works, it can still be improved and I'm looking for feedback and potential improvements. Some of the areas I'm explicitly looking for feedback in are:

  • how best to make instantiation of the use case configurable (number of phases supported etc)
  • how best to adapt the public usecase API when phases are configurable (e.g. adapting functions to take []float64 or something else entirely)
  • how to improve the current tests, I thought about connecting a remoteDevice and reading the set values out via that but couldn't get that to work easily, the alternative as far as I can see would be to add functions to the public api and copy the cs/lpc tests

@sthelen-enqs sthelen-enqs force-pushed the feat/implement-mu-mpc branch from 186302a to 47947ab Compare August 29, 2024 12:47
@DerAndereAndi
Copy link
Member

Thanks a lot for the PR!

Some first thoughts, more will surely come:

Having individual public functions for each scenario is what surely is expected and needed. But using those individually will result in a notify update message for each call, which I think is not nice.

I do see 2 options here:

  • Add another public method that can take all measurement values for all scenarios, with all being optional at the end so only the ones with actual data will be used
  • Introducing a mechanism in the SPINE repository to combine multiple local data updates to work like a transaction, where only the transaction end will trigger the actual local data storage update which will then automatically trigger the notify messages to all subscribers

@DerAndereAndi
Copy link
Member

Regarding initialization: how about the possibility to define the supported scenarios, and for each scenario define a configuration item where items like amount of phases, measurement type, reference to etc. needs to be set.

Scenario 1 is required, all other scenarios are option/recommended. And even within the scenarios there are optional items, e.g. phase specfiic power.

Also I am wondering you only added setters, but maybe it is also helpful to have getters to e.g. validate the current storage?

@DerAndereAndi DerAndereAndi added the enhancement New feature or request label Oct 15, 2024
@sthelen-enqs sthelen-enqs force-pushed the feat/implement-mu-mpc branch 2 times, most recently from a82f86c to 193fc5c Compare October 25, 2024 10:10
@sthelen-enqs
Copy link
Contributor Author

I've updated the PR with support for updating multiple measurements at the same time using the Update() function. For an example as to how to use the Update() function look at the Test_PowerPerPhase test.

The mu/mpc use case is now also configurable for e.g. non-three-phase use cases by passing configuration parameters to the NewMpc function. The config types are located in usecases/mu/mpc/config.go. For an example as to how to use these configuration parameters, look at the MPC testhelper

@sthelen-enqs sthelen-enqs marked this pull request as ready for review October 25, 2024 11:28
Copy link
Member

@DerAndereAndi DerAndereAndi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome updates, I really like the way you solve the multiple datapoint update topic!

usecases/usecase/usecase.go Outdated Show resolved Hide resolved
usecases/mu/mpc/config.go Show resolved Hide resolved
usecases/mu/mpc/config.go Outdated Show resolved Hide resolved
usecases/mu/mpc/config.go Outdated Show resolved Hide resolved
usecases/mu/mpc/config.go Outdated Show resolved Hide resolved
usecases/mu/mpc/usecase.go Show resolved Hide resolved
model.FeatureTypeTypeMeasurement,
},
},
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scenarios should only be added, if the configuration for this scenario is provided and hence the application supports it. Right now this code would always report that all scenarios are supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now, though it might be nice to have tests for it as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Test coverage still has potential to grow ;)

panic(err)
}

idEc1 := model.ElectricalConnectionIdType(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause problems, as other use cases may use an eletrical connection in the entity, and they may not be identical.

Instead we should check if one exists with the properties defined in the spec and use that. Otherwise create one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We added a GetOrAddIdForDescription function to the electricalConnection which checks if there's an ID we can reues and otherwise creates a new id

s.sut.AddFeatures()
s.sut.AddUseCase()

//s.remoteDevice, s.monitoredEntity = setupDevices(s.service, s.T())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to see such tests as in CS/LPC to be added, not just using the mocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We updated the tests to use Filters to query the stored data representation for the usecases. This should test that querying the data works from the client perspective without needing to reimplement the ma/mpc use cases in the tests for mu/mpc.

@sthelen-enqs sthelen-enqs force-pushed the feat/implement-mu-mpc branch from 6834d88 to 62108f5 Compare November 4, 2024 10:10
@sthelen-enqs
Copy link
Contributor Author

I've updated the PR with fixes for the simple changes, will look over the rest when I have more time

Copy link
Member

@DerAndereAndi DerAndereAndi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvements already, thank you!

usecases/mu/mpc/config.go Show resolved Hide resolved
return uc, nil
}

func (e *MPC) AddFeatures() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is getting quite loooooooong. How about splitting it up into individual functions for each config?
Also I am still not sure if using panics is the right approach. Maybe AddFeatures() signature should be changed to AddFeatures() error overall ?

Also the code looks quite repetitive, and maybe there is a good way to shrink this down, and thus making it also easire to improve the test coverage for all error scenarios?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We split the constituent measurement types into separate methods, and adjusted AddFeatures and AddUsecase to return error for all usecases

Copy link
Member

@DerAndereAndi DerAndereAndi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are getting there :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains some unused variables in BasicSuite. Would be great to remove or comment them.

@@ -31,6 +31,48 @@ func NewElectricalConnection(localEntity spineapi.EntityLocalInterface) (*Electr
return ec, nil
}

func (e *ElectricalConnection) GetOrAddIdForDescription(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments for this function to understand its usage

description.Label == electricalConnectionDescription.Label &&
description.Description == electricalConnectionDescription.Description {
electricalConnectionId = description.ElectricalConnectionId
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about returning here directly? That would remove the need for the if statement in line 62

// client features
_ = e.LocalEntity.GetOrAddFeature(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeClient)

// server features
f := e.LocalEntity.GetOrAddFeature(model.FeatureTypeTypeLoadControl, model.RoleTypeServer)
f.AddFunctionType(model.FunctionTypeLoadControlLimitDescriptionListData, true, false)
f.AddFunctionType(model.FunctionTypeLoadControlLimitListData, true, true)
_ = f.AddWriteApprovalCallback(e.loadControlWriteCB)
err := f.AddWriteApprovalCallback(e.loadControlWriteCB)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about (for consistency) use if err := ...; err != nil ... ?

@@ -204,6 +208,8 @@ func (e *LPC) AddFeatures() {
},
}
_ = lc.UpdateLimitDataForIds(newLimiData)
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With these long if statements, it might be easier to read the code if the error and return are handled directly in the if statement.

So changing instances like this into

dcs, err := ...
if err != nil { 
  return err
}

That also removes the need for an if statement for success.

In addition I think we should check very early for feature existence and exit before adding any data if any of the required features are missing. This applies to all modified usecase.go implementations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If would love to see added testcases for checking the error handling, e.g. by adding tests to usecase_test.go like:

func (s *CsLPCSuite) Test_AddFeatures() {
	localEntity := spinemocks.NewEntityLocalInterface(s.T())
	s.sut.LocalEntity = localEntity

	mockLc := spinemocks.NewFeatureLocalInterface(s.T())
	mockLc.EXPECT().AddFunctionType(mock.Anything, mock.Anything, mock.Anything).Return()

	localEntity.EXPECT().GetOrAddFeature(model.FeatureTypeTypeDeviceDiagnosis, model.RoleTypeClient).Return(nil).Once()
	localEntity.EXPECT().GetOrAddFeature(model.FeatureTypeTypeLoadControl, model.RoleTypeServer).Return(mockLc).Once()

	expErr := errors.New("error")
	mockLc.EXPECT().AddWriteApprovalCallback(mock.Anything).Return(expErr).Once()

	err := s.sut.AddFeatures()
	assert.NotNil(s.T(), err)
}

@@ -114,6 +117,10 @@ func (u *UseCaseBase) IsCompatibleEntityType(entity spineapi.EntityRemoteInterfa
return false
}

if u.allEntityTypesValid {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this scenario, e..g by adding the following in usecase_test.go link 26:

	s.uc.allEntityTypesValid = true
	result = s.uc.IsCompatibleEntityType(payload.Entity)
	assert.True(s.T(), result)

return nil
}

func (e *MPC) getMeasurementDataForId(id *model.MeasurementIdType) (float64, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for this function implementation


parameterDescriptionId := electricalConnection.AddParameterDescription(parameterDescription)
if parameterDescriptionId == nil {
return errors.New("could not add parameter description")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These error cases could be tested by passing mocks of server.ElectricalConnection and return an error on the function call.

Applies to all the other private functions as well.

for _, measurementDataForId := range updateData {
if !measurementDataForId.Supported() {
return measurementDataForId.NotSupportedError()
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else statement is not needed here, as we return in the other case.

So this code could be simplified to:

if !measurementDataForId.Supported() {
  return measurementDataForId.NotSupportedError()
}

measurementDataForIds = append(measurementDataForIds, measurementDataForId.MeasurementData())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants