Skip to content

Commit bf4dad9

Browse files
committed
Properly handle invalid measurement data
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.
1 parent 8366d85 commit bf4dad9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

api/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var ErrMetadataNotAvailable = errors.New("meta data not available")
99
// ErrDataNotAvailable indicates that no data set is yet available
1010
var ErrDataNotAvailable = errors.New("data not available")
1111

12+
// ErrDataInvalid indicates that the currently available data is not valid and should be ignored
13+
var ErrDataInvalid = errors.New("data not valid")
14+
1215
// ErrDataForMetadataKeyNotFound indicates that no data item is found for the given key
1316
var ErrDataForMetadataKeyNotFound = errors.New("data for key not found")
1417

usecases/internal/measurement.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func MeasurementPhaseSpecificDataForFilter(
6262
}
6363
}
6464

65+
// if the value state is set and not normal, the value is not valid and should be ignored
66+
// therefore we return an error
67+
if item.ValueState != nil && *item.ValueState != model.MeasurementValueStateTypeNormal {
68+
return nil, api.ErrDataInvalid
69+
}
70+
6571
value := item.Value.GetValue()
6672

6773
result = append(result, value)

usecases/internal/measurement_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,38 @@ func (s *InternalSuite) Test_MeasurementPhaseSpecificDataForFilter() {
161161
)
162162
assert.Nil(s.T(), err)
163163
assert.Equal(s.T(), []float64{10, 10, 10}, data)
164+
165+
measData = &model.MeasurementListDataType{
166+
MeasurementData: []model.MeasurementDataType{
167+
{
168+
MeasurementId: util.Ptr(model.MeasurementIdType(10)),
169+
},
170+
{
171+
MeasurementId: util.Ptr(model.MeasurementIdType(0)),
172+
Value: model.NewScaledNumberType(10),
173+
ValueState: util.Ptr(model.MeasurementValueStateTypeError),
174+
},
175+
{
176+
MeasurementId: util.Ptr(model.MeasurementIdType(1)),
177+
Value: model.NewScaledNumberType(10),
178+
},
179+
{
180+
MeasurementId: util.Ptr(model.MeasurementIdType(2)),
181+
Value: model.NewScaledNumberType(10),
182+
},
183+
},
184+
}
185+
186+
_, fErr = rFeature.UpdateData(true, model.FunctionTypeMeasurementListData, measData, nil, nil)
187+
assert.Nil(s.T(), fErr)
188+
189+
data, err = MeasurementPhaseSpecificDataForFilter(
190+
s.localEntity,
191+
s.monitoredEntity,
192+
filter,
193+
energyDirection,
194+
ucapi.PhaseNameMapping,
195+
)
196+
assert.NotNil(s.T(), err)
197+
assert.Nil(s.T(), data)
164198
}

0 commit comments

Comments
 (0)