Skip to content

Commit

Permalink
Properly handle invalid measurement data
Browse files Browse the repository at this point in the history
If a measurement is provided with an ValueState != Normal, then the value should be ignored by the application. To handle that, the public API will then report an ErrDataInvalid error, so the application knows that the currently no valid data is available.
  • Loading branch information
DerAndereAndi committed Nov 20, 2024
1 parent 8366d85 commit bf4dad9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var ErrMetadataNotAvailable = errors.New("meta data not available")
// ErrDataNotAvailable indicates that no data set is yet available
var ErrDataNotAvailable = errors.New("data not available")

// ErrDataInvalid indicates that the currently available data is not valid and should be ignored
var ErrDataInvalid = errors.New("data not valid")

// ErrDataForMetadataKeyNotFound indicates that no data item is found for the given key
var ErrDataForMetadataKeyNotFound = errors.New("data for key not found")

Expand Down
6 changes: 6 additions & 0 deletions usecases/internal/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func MeasurementPhaseSpecificDataForFilter(
}
}

// if the value state is set and not normal, the value is not valid and should be ignored
// therefore we return an error
if item.ValueState != nil && *item.ValueState != model.MeasurementValueStateTypeNormal {
return nil, api.ErrDataInvalid
}

value := item.Value.GetValue()

result = append(result, value)
Expand Down
34 changes: 34 additions & 0 deletions usecases/internal/measurement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,38 @@ func (s *InternalSuite) Test_MeasurementPhaseSpecificDataForFilter() {
)
assert.Nil(s.T(), err)
assert.Equal(s.T(), []float64{10, 10, 10}, data)

measData = &model.MeasurementListDataType{
MeasurementData: []model.MeasurementDataType{
{
MeasurementId: util.Ptr(model.MeasurementIdType(10)),
},
{
MeasurementId: util.Ptr(model.MeasurementIdType(0)),
Value: model.NewScaledNumberType(10),
ValueState: util.Ptr(model.MeasurementValueStateTypeError),
},
{
MeasurementId: util.Ptr(model.MeasurementIdType(1)),
Value: model.NewScaledNumberType(10),
},
{
MeasurementId: util.Ptr(model.MeasurementIdType(2)),
Value: model.NewScaledNumberType(10),
},
},
}

_, fErr = rFeature.UpdateData(true, model.FunctionTypeMeasurementListData, measData, nil, nil)
assert.Nil(s.T(), fErr)

data, err = MeasurementPhaseSpecificDataForFilter(
s.localEntity,
s.monitoredEntity,
filter,
energyDirection,
ucapi.PhaseNameMapping,
)
assert.NotNil(s.T(), err)
assert.Nil(s.T(), data)
}

0 comments on commit bf4dad9

Please sign in to comment.