2016-12-09 20:28:21 +00:00
|
|
|
/*****************************************************************************
|
2022-01-18 19:27:28 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2022, United States Government
|
2016-12-09 20:28:21 +00:00
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
define([
|
|
|
|
'lodash'
|
|
|
|
], function (
|
|
|
|
_
|
|
|
|
) {
|
|
|
|
|
|
|
|
function applyReasonableDefaults(valueMetadata, index) {
|
|
|
|
valueMetadata.source = valueMetadata.source || valueMetadata.key;
|
|
|
|
valueMetadata.hints = valueMetadata.hints || {};
|
|
|
|
|
2020-08-10 18:23:35 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(valueMetadata.hints, 'x')) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(valueMetadata.hints, 'domain')) {
|
2017-05-10 02:15:15 +00:00
|
|
|
valueMetadata.hints.domain = valueMetadata.hints.x;
|
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2017-05-10 02:15:15 +00:00
|
|
|
delete valueMetadata.hints.x;
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:23:35 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(valueMetadata.hints, 'y')) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(valueMetadata.hints, 'range')) {
|
2017-05-10 02:15:15 +00:00
|
|
|
valueMetadata.hints.range = valueMetadata.hints.y;
|
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2017-05-10 02:15:15 +00:00
|
|
|
delete valueMetadata.hints.y;
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:29:34 +00:00
|
|
|
if (valueMetadata.format === 'enum') {
|
|
|
|
if (!valueMetadata.values) {
|
2020-05-27 17:59:02 +00:00
|
|
|
valueMetadata.values = valueMetadata.enumerations.map(e => e.value);
|
2018-03-02 22:29:34 +00:00
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2020-08-10 18:23:35 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(valueMetadata, 'max')) {
|
2020-05-27 17:59:02 +00:00
|
|
|
valueMetadata.max = Math.max(valueMetadata.values) + 1;
|
2018-03-02 22:29:34 +00:00
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2020-08-10 18:23:35 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(valueMetadata, 'min')) {
|
2020-05-27 17:59:02 +00:00
|
|
|
valueMetadata.min = Math.min(valueMetadata.values) - 1;
|
2018-03-02 22:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:23:35 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(valueMetadata.hints, 'priority')) {
|
2016-12-09 20:28:21 +00:00
|
|
|
valueMetadata.hints.priority = index;
|
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2016-12-09 20:28:21 +00:00
|
|
|
return valueMetadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 21:18:08 +00:00
|
|
|
* Utility class for handling and inspecting telemetry metadata. Applies
|
|
|
|
* reasonable defaults to simplify the task of providing metadata, while
|
|
|
|
* also providing methods for interrogating telemetry metadata.
|
2016-12-09 20:28:21 +00:00
|
|
|
*/
|
2018-03-21 21:18:08 +00:00
|
|
|
function TelemetryMetadataManager(metadata) {
|
|
|
|
this.metadata = metadata;
|
2016-12-09 20:28:21 +00:00
|
|
|
|
2020-03-26 17:25:04 +00:00
|
|
|
this.valueMetadatas = this.metadata.values ? this.metadata.values.map(applyReasonableDefaults) : [];
|
2016-12-09 20:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get value metadata for a single key.
|
|
|
|
*/
|
|
|
|
TelemetryMetadataManager.prototype.value = function (key) {
|
|
|
|
return this.valueMetadatas.filter(function (metadata) {
|
2016-12-19 23:57:07 +00:00
|
|
|
return metadata.key === key;
|
2016-12-09 20:28:21 +00:00
|
|
|
})[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all value metadatas, sorted by priority.
|
|
|
|
*/
|
|
|
|
TelemetryMetadataManager.prototype.values = function () {
|
|
|
|
return this.valuesForHints(['priority']);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of valueMetadatas that posess all hints requested.
|
|
|
|
* Array is sorted based on hint priority.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
TelemetryMetadataManager.prototype.valuesForHints = function (
|
|
|
|
hints
|
|
|
|
) {
|
|
|
|
function hasHint(hint) {
|
2020-08-10 17:59:18 +00:00
|
|
|
// eslint-disable-next-line no-invalid-this
|
2020-08-10 18:23:35 +00:00
|
|
|
return Object.prototype.hasOwnProperty.call(this.hints, hint);
|
2016-12-09 20:28:21 +00:00
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2016-12-09 20:28:21 +00:00
|
|
|
function hasHints(metadata) {
|
|
|
|
return hints.every(hasHint, metadata);
|
|
|
|
}
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2020-08-10 19:13:23 +00:00
|
|
|
const matchingMetadata = this.valueMetadatas.filter(hasHints);
|
2019-03-14 20:49:37 +00:00
|
|
|
let iteratees = hints.map(hint => {
|
|
|
|
return (metadata) => {
|
2016-12-09 20:28:21 +00:00
|
|
|
return metadata.hints[hint];
|
2020-07-31 19:11:03 +00:00
|
|
|
};
|
2016-12-09 20:28:21 +00:00
|
|
|
});
|
2020-07-31 19:11:03 +00:00
|
|
|
|
2020-05-27 17:59:02 +00:00
|
|
|
return _.sortBy(matchingMetadata, ...iteratees);
|
2016-12-09 20:28:21 +00:00
|
|
|
};
|
|
|
|
|
2022-06-04 02:32:32 +00:00
|
|
|
/**
|
|
|
|
* check out of a given metadata has array values
|
|
|
|
*/
|
|
|
|
TelemetryMetadataManager.prototype.isArrayValue = function (metadata) {
|
|
|
|
const regex = /\[\]$/g;
|
|
|
|
if (!metadata.format && !metadata.formatString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (metadata.format || metadata.formatString).match(regex) !== null;
|
|
|
|
};
|
|
|
|
|
2019-03-12 19:20:03 +00:00
|
|
|
TelemetryMetadataManager.prototype.getFilterableValues = function () {
|
|
|
|
return this.valueMetadatas.filter(metadatum => metadatum.filters && metadatum.filters.length > 0);
|
2020-07-31 19:11:03 +00:00
|
|
|
};
|
2019-03-12 19:20:03 +00:00
|
|
|
|
2018-12-11 19:28:16 +00:00
|
|
|
TelemetryMetadataManager.prototype.getDefaultDisplayValue = function () {
|
|
|
|
let valueMetadata = this.valuesForHints(['range'])[0];
|
|
|
|
|
|
|
|
if (valueMetadata === undefined) {
|
|
|
|
valueMetadata = this.values().filter(values => {
|
|
|
|
return !(values.hints.domain);
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valueMetadata === undefined) {
|
|
|
|
valueMetadata = this.values()[0];
|
|
|
|
}
|
|
|
|
|
2022-06-02 20:46:13 +00:00
|
|
|
return valueMetadata;
|
2018-12-11 19:28:16 +00:00
|
|
|
};
|
|
|
|
|
2016-12-09 20:28:21 +00:00
|
|
|
return TelemetryMetadataManager;
|
|
|
|
|
|
|
|
});
|