WIP detach request data from subscription data

extract getLatestTimestamp function
This commit is contained in:
David Tsay 2020-04-03 18:39:26 -07:00
parent 483ee173d6
commit 24bb96cc90
3 changed files with 84 additions and 51 deletions

View File

@ -25,6 +25,7 @@ import uuid from 'uuid';
import TelemetryCriterion from "./criterion/TelemetryCriterion"; import TelemetryCriterion from "./criterion/TelemetryCriterion";
import { TRIGGER } from "./utils/constants"; import { TRIGGER } from "./utils/constants";
import {computeCondition, computeConditionByLimit} from "./utils/evaluator"; import {computeCondition, computeConditionByLimit} from "./utils/evaluator";
import { getLatestTimestamp } from './utils/time';
import AllTelemetryCriterion from "./criterion/AllTelemetryCriterion"; import AllTelemetryCriterion from "./criterion/AllTelemetryCriterion";
/* /*
@ -59,7 +60,7 @@ export default class ConditionClass extends EventEmitter {
this.criteriaResults = {}; this.criteriaResults = {};
this.result = undefined; this.result = undefined;
this.latestTimestamp = {}; this.latestTimestamp = {};
this.timeSystems = this.openmct.time.getAllTimeSystems();
if (conditionConfiguration.configuration.criteria) { if (conditionConfiguration.configuration.criteria) {
this.createCriteria(conditionConfiguration.configuration.criteria); this.createCriteria(conditionConfiguration.configuration.criteria);
} }
@ -210,35 +211,39 @@ export default class ConditionClass extends EventEmitter {
} }
} }
updateCriteriaResults(eventData) { updateCriteriaResults(criteriaResults, eventData) {
const id = eventData.id; const id = eventData.id;
if (this.findCriterion(id)) { if (this.findCriterion(id)) {
// The !! here is important to convert undefined to false otherwise the criteriaResults won't get deleted when the criteria is destroyed // The !! here is important to convert undefined to false otherwise the criteriaResults won't get deleted when the criteria is destroyed
this.criteriaResults[id] = !!eventData.data.result; criteriaResults[id] = !!eventData.data.result;
} }
} }
handleCriterionResult(eventData) { handleCriterionResult(eventData) {
this.updateCriteriaResults(eventData); this.updateCriteriaResults(this.criteriaResults, eventData);
this.handleConditionUpdated(eventData.data); this.handleConditionUpdated(eventData.data);
} }
requestLADConditionResult() { requestLADConditionResult() {
const criteriaResults = this.criteria let latestTimestamp;
let criteriaResults = {};
const criteriaRequests = this.criteria
.map(criterion => criterion.requestLAD({telemetryObjects: this.conditionManager.telemetryObjects})); .map(criterion => criterion.requestLAD({telemetryObjects: this.conditionManager.telemetryObjects}));
return Promise.all(criteriaResults) return Promise.all(criteriaRequests)
.then(results => { .then(results => {
results.forEach(result => { results.forEach(result => {
this.updateCriteriaResults(result); this.updateCriteriaResults(criteriaResults, result)
this.latestTimestamp = this.getLatestTimestamp(this.latestTimestamp, result.data) latestTimestamp = getLatestTimestamp(
latestTimestamp,
result.data,
this.timeSystems
);
}); });
this.evaluate();
return { return {
id: this.id, id: this.id,
data: Object.assign({}, this.latestTimestamp, { result: this.result }) data: Object.assign({}, latestTimestamp, { result: this.evaluate(criteriaResults) })
} }
}); });
} }
@ -248,8 +253,9 @@ export default class ConditionClass extends EventEmitter {
} }
handleConditionUpdated(datum) { handleConditionUpdated(datum) {
console.log(datum);
// trigger an updated event so that consumers can react accordingly // trigger an updated event so that consumers can react accordingly
this.evaluate(); this.result = this.evaluate(this.criteriaResults);
this.emitEvent('conditionResultUpdated', this.emitEvent('conditionResultUpdated',
Object.assign({}, datum, { result: this.result }) Object.assign({}, datum, { result: this.result })
); );
@ -268,29 +274,16 @@ export default class ConditionClass extends EventEmitter {
return success; return success;
} }
evaluate() { evaluate(results) {
if (this.trigger && this.trigger === TRIGGER.XOR) { if (this.trigger && this.trigger === TRIGGER.XOR) {
this.result = computeConditionByLimit(this.criteriaResults, 1); return computeConditionByLimit(results, 1);
} else if (this.trigger && this.trigger === TRIGGER.NOT) { } else if (this.trigger && this.trigger === TRIGGER.NOT) {
this.result = computeConditionByLimit(this.criteriaResults, 0); return computeConditionByLimit(results, 0);
} else { } else {
this.result = computeCondition(this.criteriaResults, this.trigger === TRIGGER.ALL); return computeCondition(results, this.trigger === TRIGGER.ALL);
} }
} }
getLatestTimestamp(current, compare) {
const timestamp = Object.assign({}, current);
this.openmct.time.getAllTimeSystems().forEach(timeSystem => {
if (!timestamp[timeSystem.key]
|| compare[timeSystem.key] > timestamp[timeSystem.key]
) {
timestamp[timeSystem.key] = compare[timeSystem.key];
}
});
return timestamp;
}
emitEvent(eventName, data) { emitEvent(eventName, data) {
this.emit(eventName, { this.emit(eventName, {
id: this.id, id: this.id,

View File

@ -21,6 +21,7 @@
*****************************************************************************/ *****************************************************************************/
import Condition from "./Condition"; import Condition from "./Condition";
import { getLatestTimestamp } from './utils/time';
import uuid from "uuid"; import uuid from "uuid";
import EventEmitter from 'EventEmitter'; import EventEmitter from 'EventEmitter';
@ -31,6 +32,7 @@ export default class ConditionManager extends EventEmitter {
this.conditionSetDomainObject = conditionSetDomainObject; this.conditionSetDomainObject = conditionSetDomainObject;
this.timeAPI = this.openmct.time; this.timeAPI = this.openmct.time;
this.latestTimestamp = {}; this.latestTimestamp = {};
this.timeSystems = this.openmct.time.getAllTimeSystems();
this.composition = this.openmct.composition.get(conditionSetDomainObject); this.composition = this.openmct.composition.get(conditionSetDomainObject);
this.composition.on('add', this.subscribeToTelemetry, this); this.composition.on('add', this.subscribeToTelemetry, this);
this.composition.on('remove', this.unsubscribeFromTelemetry, this); this.composition.on('remove', this.unsubscribeFromTelemetry, this);
@ -186,12 +188,12 @@ export default class ConditionManager extends EventEmitter {
this.persistConditions(); this.persistConditions();
} }
getCurrentCondition() { getCurrentCondition(conditionResults) {
const conditionCollection = this.conditionSetDomainObject.configuration.conditionCollection; const conditionCollection = this.conditionSetDomainObject.configuration.conditionCollection;
let currentCondition = conditionCollection[conditionCollection.length-1]; let currentCondition = conditionCollection[conditionCollection.length-1];
for (let i = 0; i < conditionCollection.length - 1; i++) { for (let i = 0; i < conditionCollection.length - 1; i++) {
if (this.conditionResults[conditionCollection[i].id]) { if (conditionResults[conditionCollection[i].id]) {
//first condition to be true wins //first condition to be true wins
currentCondition = conditionCollection[i]; currentCondition = conditionCollection[i];
break; break;
@ -200,7 +202,8 @@ export default class ConditionManager extends EventEmitter {
return currentCondition; return currentCondition;
} }
updateConditionResults(resultObj) { updateConditionResults(conditionResults, resultObj) {
console.log(resultObj.data);
if (!resultObj) { if (!resultObj) {
return; return;
} }
@ -208,16 +211,21 @@ export default class ConditionManager extends EventEmitter {
const id = resultObj.id; const id = resultObj.id;
if (this.findConditionById(id)) { if (this.findConditionById(id)) {
this.conditionResults[id] = resultObj.data.result; conditionResults[id] = resultObj.data.result;
} }
this.updateTimestamp(resultObj.data); this.latestTimestamp = getLatestTimestamp(
this.latestTimestamp,
resultObj.data,
this.timeSystems
);
} }
handleConditionResult(resultObj) { handleConditionResult(resultObj) {
// update conditions results and then calculate the current condition // update conditions results and then calculate the current condition
this.updateConditionResults(resultObj); this.updateConditionResults(this.conditionResults, resultObj);
const currentCondition = this.getCurrentCondition(); const currentCondition = this.getCurrentCondition(this.conditionResults);
this.emit('conditionSetResultUpdated', this.emit('conditionSetResultUpdated',
Object.assign( Object.assign(
{ {
@ -230,29 +238,27 @@ export default class ConditionManager extends EventEmitter {
) )
} }
updateTimestamp(timestamp) {
this.timeAPI.getAllTimeSystems().forEach(timeSystem => {
if (!this.latestTimestamp[timeSystem.key]
|| timestamp[timeSystem.key] > this.latestTimestamp[timeSystem.key]
) {
this.latestTimestamp[timeSystem.key] = timestamp[timeSystem.key];
}
});
}
requestLADConditionSetOutput() { requestLADConditionSetOutput() {
if (!this.conditionClassCollection.length) { if (!this.conditionClassCollection.length) {
return Promise.resolve([]); return Promise.resolve([]);
} }
return this.compositionLoad.then(() => { return this.compositionLoad.then(() => {
const ladConditionResults = this.conditionClassCollection let latestTimestamp;
let conditionResults = {};
const conditionRequests = this.conditionClassCollection
.map(condition => condition.requestLADConditionResult()); .map(condition => condition.requestLADConditionResult());
return Promise.all(ladConditionResults) return Promise.all(conditionRequests)
.then((results) => { .then((results) => {
results.forEach(resultObj => { this.updateConditionResults(resultObj); }); results.forEach(resultObj => {
const currentCondition = this.getCurrentCondition(); latestTimestamp = getLatestTimestamp(
latestTimestamp,
resultObj.data,
this.timeSystems
);
});
const currentCondition = this.getCurrentCondition(conditionResults);
return Object.assign( return Object.assign(
{ {
@ -260,7 +266,7 @@ export default class ConditionManager extends EventEmitter {
id: this.conditionSetDomainObject.identifier, id: this.conditionSetDomainObject.identifier,
conditionId: currentCondition.id conditionId: currentCondition.id
}, },
this.latestTimestamp latestTimestamp
); );
}); });
}); });

View File

@ -0,0 +1,34 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2020, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
export const getLatestTimestamp = (current, compare, timeSystems) => {
const timestamp = Object.assign({}, current);
timeSystems.forEach(timeSystem => {
if (!timestamp[timeSystem.key]
|| compare[timeSystem.key] > timestamp[timeSystem.key]
) {
timestamp[timeSystem.key] = compare[timeSystem.key];
}
});
return timestamp;
}