Stop evaluation of conditions when one evaluates to true (#3276)

* Stop evaluation of conditions when one evaluates to true

* Fix broken test

* Fixes broken tests

* Addresses review comments - Rename getResult to updateResult

* Rename condition getResult to updateResult

* Renames condition getResult to updateResult
This commit is contained in:
Shefali Joshi 2020-08-14 10:09:35 -07:00 committed by GitHub
parent 891fe0a36b
commit 37debefadc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 157 additions and 19 deletions

View File

@ -68,7 +68,7 @@ export default class Condition extends EventEmitter {
this.description = '';
}
getResult(datum) {
updateResult(datum) {
if (!datum || !datum.id) {
console.log('no data received');
@ -79,9 +79,9 @@ export default class Condition extends EventEmitter {
this.criteria.forEach(criterion => {
if (this.isAnyOrAllTelemetry(criterion)) {
criterion.getResult(datum, this.conditionManager.telemetryObjects);
criterion.updateResult(datum, this.conditionManager.telemetryObjects);
} else {
criterion.getResult(datum);
criterion.updateResult(datum);
}
});

View File

@ -57,7 +57,9 @@ export default class ConditionManager extends EventEmitter {
return;
}
this.telemetryObjects[id] = Object.assign({}, endpoint, {telemetryMetaData: this.openmct.telemetry.getMetadata(endpoint).valueMetadatas});
const metadata = this.openmct.telemetry.getMetadata(endpoint);
this.telemetryObjects[id] = Object.assign({}, endpoint, {telemetryMetaData: metadata ? metadata.valueMetadatas : []});
this.subscriptions[id] = this.openmct.telemetry.subscribe(
endpoint,
this.telemetryReceived.bind(this, endpoint)
@ -322,8 +324,11 @@ export default class ConditionManager extends EventEmitter {
let timestamp = {};
timestamp[timeSystemKey] = normalizedDatum[timeSystemKey];
this.conditions.forEach(condition => {
condition.getResult(normalizedDatum);
//We want to stop when the first condition evaluates to true.
this.conditions.some((condition) => {
condition.updateResult(normalizedDatum);
return condition.result === true;
});
this.updateCurrentCondition(timestamp);

View File

@ -149,7 +149,7 @@ describe("The condition", function () {
});
it("gets the result of a condition when new telemetry data is received", function () {
conditionObj.getResult({
conditionObj.updateResult({
value: '0',
utc: 'Hi',
id: testTelemetryObject.identifier.key
@ -158,7 +158,7 @@ describe("The condition", function () {
});
it("gets the result of a condition when new telemetry data is received", function () {
conditionObj.getResult({
conditionObj.updateResult({
value: '1',
utc: 'Hi',
id: testTelemetryObject.identifier.key
@ -167,14 +167,14 @@ describe("The condition", function () {
});
it("keeps the old result new telemetry data is not used by it", function () {
conditionObj.getResult({
conditionObj.updateResult({
value: '0',
utc: 'Hi',
id: testTelemetryObject.identifier.key
});
expect(conditionObj.result).toBeTrue();
conditionObj.getResult({
conditionObj.updateResult({
value: '1',
utc: 'Hi',
id: '1234'

View File

@ -122,7 +122,7 @@ export default class AllTelemetryCriterion extends TelemetryCriterion {
return datum;
}
getResult(data, telemetryObjects) {
updateResult(data, telemetryObjects) {
const validatedData = this.isValid() ? data : {};
if (validatedData) {

View File

@ -120,7 +120,7 @@ export default class TelemetryCriterion extends EventEmitter {
return datum;
}
getResult(data) {
updateResult(data) {
const validatedData = this.isValid() ? data : {};
if (this.isStalenessCheck()) {
if (this.stalenessSubscription) {
@ -201,7 +201,9 @@ export default class TelemetryCriterion extends EventEmitter {
let metadataObject;
if (metadata) {
const telemetryMetadata = this.openmct.telemetry.getMetadata(telemetryObject);
metadataObject = telemetryMetadata.valueMetadatas.find((valueMetadata) => valueMetadata.key === metadata);
if (telemetryMetadata) {
metadataObject = telemetryMetadata.valueMetadatas.find((valueMetadata) => valueMetadata.key === metadata);
}
}
return metadataObject;

View File

@ -110,7 +110,7 @@ describe("The telemetry criterion", function () {
});
it("returns a result on new data from relevant telemetry providers", function () {
telemetryCriterion.getResult({
telemetryCriterion.updateResult({
value: 'Hello',
utc: 'Hi',
id: testTelemetryObject.identifier.key

View File

@ -45,8 +45,9 @@ describe('the plugin', function () {
type: "test-object",
name: "Test Object",
telemetry: {
valueMetadatas: [{
key: "some-key",
values: [{
key: "some-key2",
source: "some-key2",
name: "Some attribute",
hints: {
range: 2
@ -64,6 +65,13 @@ describe('the plugin', function () {
source: "value",
name: "Test",
format: "string"
},
{
key: "some-key",
source: "some-key",
hints: {
domain: 1
}
}]
}
};
@ -458,7 +466,7 @@ describe('the plugin', function () {
};
});
xit('should evaluate as stale when telemetry is not received in the allotted time', (done) => {
it('should evaluate as stale when telemetry is not received in the allotted time', (done) => {
let conditionMgr = new ConditionManager(conditionSetDomainObject, openmct);
conditionMgr.on('conditionSetResultUpdated', mockListener);
@ -480,7 +488,7 @@ describe('the plugin', function () {
}, 400);
});
xit('should not evaluate as stale when telemetry is received in the allotted time', (done) => {
it('should not evaluate as stale when telemetry is received in the allotted time', (done) => {
const date = Date.now();
conditionSetDomainObject.configuration.conditionCollection[0].configuration.criteria[0].input = ["0.4"];
let conditionMgr = new ConditionManager(conditionSetDomainObject, openmct);
@ -500,10 +508,133 @@ describe('the plugin', function () {
key: 'cf4456a9-296a-4e6b-b182-62ed29cd15b9'
},
conditionId: '2532d90a-e0d6-4935-b546-3123522da2de',
utc: undefined
utc: date
});
done();
}, 300);
});
});
describe('the condition evaluation', () => {
let conditionSetDomainObject;
beforeEach(() => {
conditionSetDomainObject = {
"configuration": {
"conditionTestData": [
{
"telemetry": "",
"metadata": "",
"input": ""
}
],
"conditionCollection": [
{
"id": "39584410-cbf9-499e-96dc-76f27e69885f",
"configuration": {
"name": "Unnamed Condition0",
"output": "Any telemetry less than 0",
"trigger": "all",
"criteria": [
{
"id": "35400132-63b0-425c-ac30-8197df7d5864",
"telemetry": "any",
"operation": "lessThan",
"input": [
"0"
],
"metadata": "some-key"
}
]
},
"summary": "Match if all criteria are met: Any telemetry value is less than 0"
},
{
"id": "39584410-cbf9-499e-96dc-76f27e69885d",
"configuration": {
"name": "Unnamed Condition",
"output": "Any telemetry greater than 0",
"trigger": "all",
"criteria": [
{
"id": "35400132-63b0-425c-ac30-8197df7d5862",
"telemetry": "any",
"operation": "greaterThan",
"input": [
"0"
],
"metadata": "some-key"
}
]
},
"summary": "Match if all criteria are met: Any telemetry value is greater than 0"
},
{
"id": "39584410-cbf9-499e-96dc-76f27e69885e",
"configuration": {
"name": "Unnamed Condition1",
"output": "Any telemetry greater than 1",
"trigger": "all",
"criteria": [
{
"id": "35400132-63b0-425c-ac30-8197df7d5863",
"telemetry": "any",
"operation": "greaterThan",
"input": [
"1"
],
"metadata": "some-key"
}
]
},
"summary": "Match if all criteria are met: Any telemetry value is greater than 1"
},
{
"isDefault": true,
"id": "2532d90a-e0d6-4935-b546-3123522da2de",
"configuration": {
"name": "Default",
"output": "Default",
"trigger": "all",
"criteria": [
]
},
"summary": ""
}
]
},
"composition": [
{
"namespace": "",
"key": "test-object"
}
],
"telemetry": {
},
"name": "Condition Set",
"type": "conditionSet",
"identifier": {
"namespace": "",
"key": "cf4456a9-296a-4e6b-b182-62ed29cd15b9"
}
};
});
it('should stop evaluating conditions when a condition evaluates to true', () => {
const date = Date.now();
let conditionMgr = new ConditionManager(conditionSetDomainObject, openmct);
conditionMgr.on('conditionSetResultUpdated', mockListener);
conditionMgr.telemetryObjects = {
"test-object": testTelemetryObject
};
conditionMgr.updateConditionTelemetryObjects();
conditionMgr.telemetryReceived(testTelemetryObject, {
"some-key": 2,
utc: date
});
let result = conditionMgr.conditions.map(condition => condition.result);
expect(result[2]).toBeUndefined();
});
});
});