Bug fixes:

Ensures that the default condition emits a condition result update
Fixes remove condition
This commit is contained in:
Joshi 2020-02-25 12:29:47 -08:00
parent 7d2256d70f
commit b744467f21
5 changed files with 110 additions and 80 deletions

View File

@ -58,11 +58,11 @@ export default class ConditionClass extends EventEmitter {
this.id = this.openmct.objects.makeKeyString(conditionConfiguration.identifier);
this.criteria = [];
this.criteriaResults = {};
this.result = null;
if (conditionConfiguration.configuration.criteria) {
this.createCriteria(conditionConfiguration.configuration.criteria);
}
this.trigger = conditionConfiguration.configuration.trigger;
this.result = null;
this.openmct.objects.get(this.id).then(obj => this.observeForChanges(obj));
}
@ -199,9 +199,7 @@ export default class ConditionClass extends EventEmitter {
subscribe() {
this.criteria.forEach((criterion) => {
if (criterion.isValid()) {
criterion.subscribe();
}
criterion.subscribe();
})
}

View File

@ -91,6 +91,11 @@ export default class ConditionManager extends EventEmitter {
} else {
this.conditionCollection.unshift(condition);
}
//There are no criteria for a default condition and hence no subscriptions.
//Hence the conditionResult must be manually triggered for it.
if (conditionConfiguration.isDefault) {
this.handleConditionResult();
}
}
createConditionDomainObject(isDefault, conditionConfiguration) {
@ -160,7 +165,7 @@ export default class ConditionManager extends EventEmitter {
if (found) {
let index = found.index;
let condition = this.conditionCollection[index];
let conditionIdAsString = this.openmct.objects.makeKeyString(condition.identifier);
let conditionIdAsString = condition.id;
condition.destroyCriteria();
condition.off('conditionResultUpdated', this.handleConditionResult.bind(this));
this.conditionCollection.splice(index, 1);
@ -169,6 +174,7 @@ export default class ConditionManager extends EventEmitter {
delete this.conditionResults[conditionIdAsString];
}
this.persist();
this.handleConditionResult();
}
}
@ -201,28 +207,33 @@ export default class ConditionManager extends EventEmitter {
this.persist();
}
handleConditionResult(args) {
let idAsString = this.openmct.objects.makeKeyString(args.id);
let found = this.findConditionById(idAsString);
handleConditionResult(resultObj) {
let conditionCollection = this.domainObject.configuration.conditionCollection;
let currentConditionIdentifier = conditionCollection[conditionCollection.length-1];
if (found) {
this.conditionResults[idAsString] = args.data.result;
for (let i = 0, ii = conditionCollection.length - 1; i < ii; i++) {
let conditionIdAsString = this.openmct.objects.makeKeyString(conditionCollection[i]);
if (this.conditionResults[conditionIdAsString]) {
//first condition to be true wins
currentConditionIdentifier = conditionCollection[i];
break;
}
if (resultObj) {
let idAsString = this.openmct.objects.makeKeyString(resultObj.id);
let found = this.findConditionById(idAsString);
if (found) {
this.conditionResults[idAsString] = resultObj.data.result;
}
}
for (let i = 0, ii = conditionCollection.length - 1; i < ii; i++) {
let conditionIdAsString = this.openmct.objects.makeKeyString(conditionCollection[i]);
if (this.conditionResults[conditionIdAsString]) {
//first condition to be true wins
currentConditionIdentifier = conditionCollection[i];
break;
}
}
this.openmct.objects.get(currentConditionIdentifier).then((obj) => {
console.log(obj.configuration.output);
this.emit('conditionSetResultUpdated', {
id: this.domainObject.identifier,
output: obj.configuration.output
output: obj.configuration.output,
conditionId: currentConditionIdentifier
})
});
}

View File

@ -25,55 +25,74 @@ import ConditionManager from './ConditionManager';
describe('ConditionManager', () => {
let conditionMgr;
let testTelemetryObject;
let mockListener;
let openmct = {};
let parentDomainObject;
let composition;
let conditionSetDomainObject = {
identifier: {
namespace: "",
key: "600a7372-8d48-4dc4-98b6-548611b1ff7e"
},
type: "conditionSet",
location: "mine",
configuration: {
conditionCollection: []
}
};
let mockConditionDomainObject = {
isDefault: true,
type: 'condition',
identifier: {
namespace: '',
key: '1234-5678'
}
};
beforeAll(function () {
testTelemetryObject = {
identifier:{ namespace: "", key: "test-object"},
type: "test-object",
name: "Test Object",
telemetry: {
values: [{
key: "some-key",
name: "Some attribute",
hints: {
domain: 1
}
}, {
key: "some-other-key",
name: "Another attribute",
hints: {
range: 1
}
}]
function mockAngularComponents() {
let mockInjector = jasmine.createSpyObj('$injector', ['get']);
let mockInstantiate = jasmine.createSpy('mockInstantiate');
mockInstantiate.and.returnValue(mockInstantiate);
let mockDomainObject = {
useCapability: function () {
return mockConditionDomainObject;
}
};
openmct.objects = jasmine.createSpyObj('objects', ['get', 'makeKeyString']);
openmct.objects.get.and.returnValue(testTelemetryObject);
openmct.objects.makeKeyString.and.returnValue(testTelemetryObject.identifier.key);
openmct.telemetry = jasmine.createSpyObj('telemetry', ['isTelemetryObject']);
conditionMgr = new ConditionManager(openmct);
parentDomainObject = {};
composition = {};
mockInstantiate.and.callFake(function () {
return mockDomainObject;
});
mockInjector.get.and.callFake(function (service) {
return {
'instantiate': mockInstantiate
}[service];
});
openmct.$injector = mockInjector;
}
beforeAll(function () {
mockAngularComponents();
openmct.objects = jasmine.createSpyObj('objects', ['get', 'makeKeyString', 'observe', 'mutate']);
openmct.objects.get.and.returnValues(new Promise(function (resolve, reject) {
resolve(conditionSetDomainObject);
}), new Promise(function (resolve, reject) {
resolve(mockConditionDomainObject);
}));
openmct.objects.makeKeyString.and.returnValue(conditionSetDomainObject.identifier.key);
openmct.objects.observe.and.returnValue(function () {});
openmct.objects.mutate.and.returnValue(function () {});
conditionMgr = new ConditionManager(conditionSetDomainObject, openmct);
mockListener = jasmine.createSpy('mockListener');
conditionMgr.on('conditionSetResultUpdated', mockListener);
});
it('creates a conditionCollection with a default condition', function () {
});
it('adds a condition to the collection with one criterion', function () {
});
it('updates a condition\'s criteria to something valid', function () {
});
it('removes a condition from the conditionCollection', function () {
expect(conditionMgr.domainObject.configuration.conditionCollection.length).toEqual(1);
let defaultConditionIdentifier = conditionMgr.domainObject.configuration.conditionCollection[0];
expect(defaultConditionIdentifier).toEqual(mockConditionDomainObject.identifier);
});
});

View File

@ -200,12 +200,7 @@ export default {
this.currentConditionIdentifier = identifier;
},
removeCondition(identifier) {
let index = _.findIndex(this.conditionCollection, (condition) => {
let conditionId = this.openmct.objects.makeKeyString(condition);
let id = this.openmct.objects.makeKeyString(identifier);
return conditionId === id;
});
this.conditionManager.removeCondition(null, index);
this.conditionManager.removeCondition(identifier);
},
reorder(reorderPlan) {
this.conditionManager.reorderConditions(reorderPlan);

View File

@ -72,17 +72,19 @@ export default class TelemetryCriterion extends EventEmitter {
}
computeResult(data) {
let comparator = this.findOperation(this.operation);
let params = [];
let result = false;
params.push(data[this.metadata]);
if (this.input instanceof Array && this.input.length) {
params.push(this.input[0]);
} else if (this.input) {
params.push(this.input);
}
if (typeof comparator === 'function') {
result = comparator(params);
if (data) {
let comparator = this.findOperation(this.operation);
let params = [];
params.push(data[this.metadata]);
if (this.input instanceof Array && this.input.length) {
params.push(this.input[0]);
} else if (this.input) {
params.push(this.input);
}
if (typeof comparator === 'function') {
result = comparator(params);
}
}
return result;
}
@ -100,12 +102,17 @@ export default class TelemetryCriterion extends EventEmitter {
/**
* Subscribes to the telemetry object and returns an unsubscribe function
* If the telemetry is not valid, returns nothing
*/
subscribe() {
this.unsubscribe();
this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => {
this.handleSubscription(datum);
});
if (this.isValid()) {
this.unsubscribe();
this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => {
this.handleSubscription(datum);
});
} else {
this.handleSubscription();
}
}
/**