Set criteria options (#2630)

* Set criteria options on condition edit

* Persists telemetry options correctly and loads them on mount

* Fixes saving the input value for criteria

* Display active condition's output in read only view

* Destroy classes and unsubscribe when condition set view is destroyed

* Fixes saving the input value for a criteria

* Handle telemetry removal

* Fixes tests

* Addresses comments - change function names, consolidate compute function

* Addresses review comments
- Use camelCase for events (did not change properties as eslint complains)
- Reduce repeated property access by assigning to a variable
- Use descriptive variable name

* Addressing comments - improves input value field visibility logic

* Change variable name to reflect intent
This commit is contained in:
Shefali Joshi 2020-01-31 14:44:28 -08:00 committed by GitHub
parent be428b326e
commit e580734c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 163 additions and 85 deletions

View File

@ -24,7 +24,7 @@ import * as EventEmitter from 'eventemitter3';
import uuid from 'uuid'; import uuid from 'uuid';
import TelemetryCriterion from "@/plugins/condition/criterion/TelemetryCriterion"; import TelemetryCriterion from "@/plugins/condition/criterion/TelemetryCriterion";
import { TRIGGER } from "@/plugins/condition/utils/constants"; import { TRIGGER } from "@/plugins/condition/utils/constants";
import {computeConditionForAll, computeConditionForAny} from "@/plugins/condition/utils/evaluator"; import {computeCondition} from "@/plugins/condition/utils/evaluator";
/* /*
* conditionDefinition = { * conditionDefinition = {
@ -228,11 +228,7 @@ export default class ConditionClass extends EventEmitter {
//TODO: implement as part of the evaluator class task. //TODO: implement as part of the evaluator class task.
evaluate() { evaluate() {
if (this.trigger === TRIGGER.ANY) { this.result = computeCondition(this.criteriaResults, this.trigger === TRIGGER.ALL);
this.result = computeConditionForAny(this.criteriaResults);
} else if (this.trigger === TRIGGER.ALL) {
this.result = computeConditionForAll(this.criteriaResults);
}
} }
emitEvent(eventName, data) { emitEvent(eventName, data) {

View File

@ -103,7 +103,6 @@ describe("The condition", function () {
it("initializes with criteria from the condition definition", function () { it("initializes with criteria from the condition definition", function () {
expect(conditionObj.criteria.length).toEqual(1); expect(conditionObj.criteria.length).toEqual(1);
let criterion = conditionObj.criteria[0]; let criterion = conditionObj.criteria[0];
console.log(criterion);
expect(criterion instanceof TelemetryCriterion).toBeTrue(); expect(criterion instanceof TelemetryCriterion).toBeTrue();
expect(criterion.operator).toEqual(testConditionDefinition.definition.criteria[0].operator); expect(criterion.operator).toEqual(testConditionDefinition.definition.criteria[0].operator);
expect(criterion.input).toEqual(testConditionDefinition.definition.criteria[0].input); expect(criterion.input).toEqual(testConditionDefinition.definition.criteria[0].input);

View File

@ -22,6 +22,8 @@
</template> </template>
<script> <script>
import ConditionClass from "@/plugins/condition/Condition";
export default { export default {
inject: ['openmct'], inject: ['openmct'],
props: { props: {
@ -39,10 +41,26 @@ export default {
condition: this.condition condition: this.condition
}; };
}, },
destroyed() {
this.conditionClass.off('conditionResultUpdated', this.handleConditionResult.bind(this));
if (this.conditionClass && typeof this.conditionClass.destroy === 'function') {
this.conditionClass.destroy();
}
},
mounted() { mounted() {
this.openmct.objects.get(this.conditionIdentifier).then((obj => { this.openmct.objects.get(this.conditionIdentifier).then((obj => {
this.condition = obj; this.condition = obj;
this.conditionClass = new ConditionClass(this.condition, this.openmct);
this.conditionClass.on('conditionResultUpdated', this.handleConditionResult.bind(this));
})); }));
},
methods: {
handleConditionResult(args) {
this.$emit('conditionResultUpdated', {
id: this.conditionIdentifier,
result: args.data.result
})
}
} }
} }
</script> </script>

View File

@ -35,15 +35,17 @@
> >
<div v-if="isEditing"> <div v-if="isEditing">
<ConditionEdit :condition-identifier="conditionIdentifier" <ConditionEdit :condition-identifier="conditionIdentifier"
:telemetry="telemetryObjs"
:current-condition-identifier="currentConditionIdentifier" :current-condition-identifier="currentConditionIdentifier"
@update-current-condition="updateCurrentCondition" @updateCurrentCondition="updateCurrentCondition"
@remove-condition="removeCondition" @removeCondition="removeCondition"
@condition-result-updated="handleConditionResult" @conditionResultUpdated="handleConditionResult"
/> />
</div> </div>
<div v-else> <div v-else>
<Condition :condition-identifier="conditionIdentifier" <Condition :condition-identifier="conditionIdentifier"
:current-condition-identifier="currentConditionIdentifier" :current-condition-identifier="currentConditionIdentifier"
@conditionResultUpdated="handleConditionResult"
/> />
</div> </div>
</div> </div>
@ -72,7 +74,8 @@ export default {
parentKeyString: this.openmct.objects.makeKeyString(this.domainObject.identifier), parentKeyString: this.openmct.objects.makeKeyString(this.domainObject.identifier),
conditionCollection: [], conditionCollection: [],
conditions: [], conditions: [],
currentConditionIdentifier: this.currentConditionIdentifier || {} currentConditionIdentifier: this.currentConditionIdentifier || {},
telemetryObjs: this.telemetryObjs
}; };
}, },
destroyed() { destroyed() {
@ -84,6 +87,7 @@ export default {
this.instantiate = this.openmct.$injector.get('instantiate'); this.instantiate = this.openmct.$injector.get('instantiate');
this.composition = this.openmct.composition.get(this.domainObject); this.composition = this.openmct.composition.get(this.domainObject);
this.composition.on('add', this.addTelemetry); this.composition.on('add', this.addTelemetry);
this.composition.on('remove', this.removeTelemetry);
this.composition.load(); this.composition.load();
this.conditionCollection = this.domainObject.configuration ? this.domainObject.configuration.conditionCollection : []; this.conditionCollection = this.domainObject.configuration ? this.domainObject.configuration.conditionCollection : [];
if (!this.conditionCollection.length) { if (!this.conditionCollection.length) {
@ -108,16 +112,26 @@ export default {
break; break;
} }
} }
this.$emit('current-condition-updated', currentConditionIdentifier); this.$emit('currentConditionUpdated', currentConditionIdentifier);
}, },
addTelemetry(telemetryDomainObject) { addTelemetry(telemetryDomainObject) {
this.telemetryObjs.push(telemetryDomainObject); this.telemetryObjs.push(telemetryDomainObject);
}, },
removeTelemetry(telemetryDomainObjectIdentifier) {
let index = _.findIndex(this.telemetryObjs, (obj) => {
let objId = this.openmct.objects.makeKeyString(obj.identifier);
let id = this.openmct.objects.makeKeyString(telemetryDomainObjectIdentifier);
return objId === id;
});
if (index > -1) {
this.telemetryObjs.splice(index, 1);
}
},
addCondition(event, isDefault) { addCondition(event, isDefault) {
let conditionDO = this.getConditionDomainObject(!!isDefault); let conditionDomainObject = this.getConditionDomainObject(!!isDefault);
//persist the condition DO so that we can do an openmct.objects.get on it and only persist the identifier in the conditionCollection of conditionSet //persist the condition domain object so that we can do an openmct.objects.get on it and only persist the identifier in the conditionCollection of conditionSet
this.openmct.objects.mutate(conditionDO, 'created', new Date()); this.openmct.objects.mutate(conditionDomainObject, 'created', new Date());
this.conditionCollection.unshift(conditionDO.identifier); this.conditionCollection.unshift(conditionDomainObject.identifier);
this.persist(); this.persist();
}, },
updateCurrentCondition(identifier) { updateCurrentCondition(identifier) {
@ -137,16 +151,16 @@ export default {
criteria: isDefault ? [] : [{ criteria: isDefault ? [] : [{
operation: '', operation: '',
input: '', input: '',
metaDataKey: this.openmct.telemetry.getMetadata(this.telemetryObjs[0]).values()[0].key, metaDataKey: '',
key: this.telemetryObjs.length ? this.openmct.objects.makeKeyString(this.telemetryObjs[0].identifier) : null key: ''
}] }]
}, },
summary: 'summary description' summary: 'summary description'
}; };
let conditionDOKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier); let conditionDomainObjectKeyString = this.openmct.objects.makeKeyString(conditionObj.identifier);
let newDO = this.instantiate(conditionObj, conditionDOKeyString); let newDomainObject = this.instantiate(conditionObj, conditionDomainObjectKeyString);
return newDO.useCapability('adapter'); return newDomainObject.useCapability('adapter');
}, },
updateCondition(updatedCondition) { updateCondition(updatedCondition) {
//TODO: this should only happen for reordering //TODO: this should only happen for reordering

View File

@ -84,17 +84,22 @@
</li> </li>
</ul> </ul>
<ul class="t-widget-rule-config"> <ul class="t-widget-rule-config">
<li v-if="telemetryObject && telemetryMetadata" <li v-if="telemetry.length"
class="has-local-controls t-condition" class="has-local-controls t-condition"
> >
<label>when</label> <label>when</label>
<span class="t-configuration"> <span class="t-configuration">
<span class="controls"> <span class="controls">
<select v-model="selectedTelemetryKey" <select v-model="selectedTelemetryKey"
class="" @change="updateTelemetryMetaData"
> >
<option value="">- Select Telemetry -</option> <option value="">- Select Telemetry -</option>
<option :value="telemetryObject.identifier">{{ telemetryObject.name }}</option> <option v-for="telemetryOption in telemetry"
:key="telemetryOption.identifier.key"
:value="telemetryOption.identifier"
>
{{ telemetryOption.name }}
</option>
</select> </select>
</span> </span>
<span class="controls"> <span class="controls">
@ -109,8 +114,8 @@
</select> </select>
</span> </span>
<span class="controls"> <span class="controls">
<select v-model="selectedOperationKey" <select v-model="selectOperationName"
@change="operationKeyChange" @change="setInputValueVisibility"
> >
<option value="">- Select Comparison -</option> <option value="">- Select Comparison -</option>
<option v-for="option in operations" <option v-for="option in operations"
@ -123,7 +128,7 @@
<input v-if="comparisonValueField" <input v-if="comparisonValueField"
class="t-rule-name-input" class="t-rule-name-input"
type="text" type="text"
@keyup="getOperationValue" v-model="operationValue"
> >
</span> </span>
</span> </span>
@ -152,6 +157,11 @@ export default {
currentConditionIdentifier: { currentConditionIdentifier: {
type: Object, type: Object,
required: true required: true
},
telemetry: {
type: Array,
required: true,
default: () => []
} }
}, },
data() { data() {
@ -161,12 +171,13 @@ export default {
telemetryObject: this.telemetryObject, telemetryObject: this.telemetryObject,
telemetryMetadata: this.telemetryMetadata, telemetryMetadata: this.telemetryMetadata,
operations: OPERATIONS, operations: OPERATIONS,
selectedMetaDataKey: null, selectedMetaDataKey: '',
selectedTelemetryKey: '', selectedTelemetryKey: '',
selectedOperationKey: '', selectOperationName: '',
selectedOutputKey: null, selectedOutputKey: '',
stringOutputField: false, stringOutputField: false,
comparisonValueField: false, comparisonValueField: false,
operationValue: this.operationValue,
outputOptions: [ outputOptions: [
{ {
key: 'false', key: 'false',
@ -185,42 +196,64 @@ export default {
}; };
}, },
destroyed() { destroyed() {
this.conditionClass.off('conditionResultUpdated', this.handleConditionResult.bind(this)); this.destroy();
if (this.conditionClass && typeof this.conditionClass.destroy === 'function') {
this.conditionClass.destroy();
}
}, },
mounted() { mounted() {
this.openmct.objects.get(this.conditionIdentifier).then((obj => { this.openmct.objects.get(this.conditionIdentifier).then((obj => {
this.condition = obj; this.condition = obj;
this.initialize();
}));
},
updated() {
//validate telemetry exists, update criteria as needed
this.validate();
this.persist();
},
methods: {
initialize() {
this.setOutput(); this.setOutput();
this.setOperation(); this.setOperation();
this.updateTelemetry(); this.updateTelemetry();
this.conditionClass = new ConditionClass(this.condition, this.openmct); this.conditionClass = new ConditionClass(this.condition, this.openmct);
this.conditionClass.on('conditionResultUpdated', this.handleConditionResult.bind(this)); this.conditionClass.on('conditionResultUpdated', this.handleConditionResult.bind(this));
})); },
}, destroy() {
updated() { this.conditionClass.off('conditionResultUpdated', this.handleConditionResult.bind(this));
if (this.isCurrent && this.isCurrent.key === this.condition.key) { if (this.conditionClass && typeof this.conditionClass.destroy === 'function') {
this.updateCurrentCondition(); this.conditionClass.destroy();
} delete this.conditionClass;
this.persist(); }
}, },
methods: { reset() {
this.selectedMetaDataKey = '';
this.selectedTelemetryKey = '';
this.selectOperationName = '';
this.operationValue = '';
},
validate() {
if (this.hasTelemetry() && !this.getTelemetryKey()) {
this.reset();
} else {
if (!this.conditionClass) {
this.initialize();
}
}
},
handleConditionResult(args) { handleConditionResult(args) {
this.$emit('condition-result-updated', { this.$emit('conditionResultUpdated', {
id: this.conditionIdentifier, id: this.conditionIdentifier,
result: args.data.result result: args.data.result
}) })
}, },
removeCondition(ev) { //move this to conditionCollection removeCondition(ev) {
this.$emit('remove-condition', this.conditionIdentifier); this.$emit('removeCondition', this.conditionIdentifier);
}, },
setOutput() { setOutput() {
if (this.condition.definition.output !== 'false' && this.condition.definition.output !== 'true') { let conditionOutput = this.condition.definition.output;
if (conditionOutput !== 'false' && conditionOutput !== 'true') {
this.selectedOutputKey = this.outputOptions[2].key; this.selectedOutputKey = this.outputOptions[2].key;
} else { } else {
if (this.condition.definition.output === 'true') { if (conditionOutput === 'true') {
this.selectedOutputKey = this.outputOptions[1].key; this.selectedOutputKey = this.outputOptions[1].key;
} else { } else {
this.selectedOutputKey = this.outputOptions[0].key; this.selectedOutputKey = this.outputOptions[0].key;
@ -231,7 +264,11 @@ export default {
if (this.condition.definition.criteria.length && this.condition.definition.criteria[0].operation) { if (this.condition.definition.criteria.length && this.condition.definition.criteria[0].operation) {
for (let i=0, ii=this.operations.length; i < ii; i++) { for (let i=0, ii=this.operations.length; i < ii; i++) {
if (this.condition.definition.criteria[0].operation === this.operations[i].name) { if (this.condition.definition.criteria[0].operation === this.operations[i].name) {
this.selectedOperationKey = this.operations[i].name; this.selectOperationName = this.operations[i].name;
this.comparisonValueField = this.operations[i].inputCount > 0;
if (this.comparisonValueField) {
this.operationValue = this.condition.definition.criteria[0].input[0];
}
} }
} }
} }
@ -241,17 +278,47 @@ export default {
this.openmct.objects.get(this.condition.definition.criteria[0].key).then((obj) => { this.openmct.objects.get(this.condition.definition.criteria[0].key).then((obj) => {
this.telemetryObject = obj; this.telemetryObject = obj;
this.telemetryMetadata = this.openmct.telemetry.getMetadata(this.telemetryObject).values(); this.telemetryMetadata = this.openmct.telemetry.getMetadata(this.telemetryObject).values();
this.selectedMetaDataKey = ''; this.selectedMetaDataKey = this.getTelemetryMetadataKey();
this.selectedTelemetryKey = this.telemetryObject.identifier; this.selectedTelemetryKey = this.getTelemetryKey();
}); });
} else { } else {
this.telemetryObject = null; this.telemetryObject = null;
} }
}, },
getTelemetryMetadataKey() {
let index = -1;
if (this.condition.definition.criteria[0].metaDataKey) {
index = _.findIndex(this.telemetryMetadata, (metadata) => {
return metadata.key === this.condition.definition.criteria[0].metaDataKey;
});
}
return this.telemetryMetadata.length && index > -1 ? this.telemetryMetadata[index].key : '';
},
getTelemetryKey() {
let index = -1;
if (this.condition.definition.criteria[0].key) {
index = _.findIndex(this.telemetry, (obj) => {
let key = this.openmct.objects.makeKeyString(obj.identifier);
let conditionKey = this.openmct.objects.makeKeyString(this.condition.definition.criteria[0].key);
return key === conditionKey;
});
}
return this.telemetry.length && index > -1 ? this.telemetry[index].identifier : '';
},
hasTelemetry() { hasTelemetry() {
return this.condition.definition.criteria.length && this.condition.definition.criteria[0].key; return this.condition.definition.criteria.length && this.condition.definition.criteria[0].key;
}, },
updateConditionCriteria() {
if (this.condition.definition.criteria.length) {
let criterion = this.condition.definition.criteria[0];
criterion.key = this.selectedTelemetryKey;
criterion.metaDataKey = this.selectedMetaDataKey;
criterion.operation = this.selectOperationName;
criterion.input = [this.operationValue];
}
},
persist() { persist() {
this.updateConditionCriteria();
this.openmct.objects.mutate(this.condition, 'definition', this.condition.definition); this.openmct.objects.mutate(this.condition, 'definition', this.condition.definition);
}, },
checkInputValue() { checkInputValue() {
@ -261,23 +328,22 @@ export default {
this.condition.definition.output = this.selectedOutputKey; this.condition.definition.output = this.selectedOutputKey;
} }
}, },
operationKeyChange(ev) { setInputValueVisibility(ev) {
if (ev.target.value !== 'isUndefined' && ev.target.value !== 'isDefined') { for (let i=0, ii=this.operations.length; i < ii; i++) {
this.comparisonValueField = true; if (this.selectOperationName === this.operations[i].name) {
} else { this.comparisonValueField = this.operations[i].inputCount > 0;
this.comparisonValueField = false; break;
}
} }
this.condition.definition.criteria[0].operation = this.selectedOperationKey;
this.persist();
//find the criterion being updated and set the operation property //find the criterion being updated and set the operation property
}, },
getOperationValue(ev) { updateTelemetryMetaData() {
this.condition.definition.criteria[0].input = [ev.target.value]; this.selectedMetaDataKey = '';
this.persist(); this.updateConditionCriteria();
//find the criterion being updated and set the input property this.updateTelemetry();
}, },
updateCurrentCondition() { updateCurrentCondition() {
this.$emit('update-current-condition', this.conditionIdentifier); this.$emit('updateCurrentCondition', this.conditionIdentifier);
} }
} }
} }

View File

@ -4,7 +4,7 @@
<CurrentOutput :condition="currentCondition" /> <CurrentOutput :condition="currentCondition" />
<TestData :is-editing="isEditing" /> <TestData :is-editing="isEditing" />
<ConditionCollection :is-editing="isEditing" <ConditionCollection :is-editing="isEditing"
@current-condition-updated="updateCurrentCondition" @currentConditionUpdated="updateCurrentCondition"
/> />
</div> </div>
</div> </div>

View File

@ -1,28 +1,13 @@
/** export const computeCondition = (resultMap, allMustBeTrue) => {
* Returns true only if at least one of the results is true
**/
export const computeConditionForAny = (resultMap) => {
let result = false; let result = false;
for (let key in resultMap) { for (let key in resultMap) {
if (resultMap.hasOwnProperty(key)) { if (resultMap.hasOwnProperty(key)) {
result = resultMap[key]; result = resultMap[key];
if (result) { if (allMustBeTrue && !result) {
break; //If we want all conditions to be true, then even one negative result should break.
} break;
} } else if (!allMustBeTrue && result) {
} //If we want at least one condition to be true, then even one positive result should break.
return result;
};
/**
* Returns true only if all the results are true
**/
export const computeConditionForAll = (resultMap) => {
let result = false;
for (let key in resultMap) {
if (resultMap.hasOwnProperty(key)) {
result = resultMap[key];
if (!result) {
break; break;
} }
} }