mirror of
https://github.com/nasa/openmct.git
synced 2025-05-02 08:43:17 +00:00
Object migration (#2282)
I implementation of data migrations for display layouts and fixed position elements.
This commit is contained in:
parent
5ee22b3481
commit
1c8f23dea1
@ -81,6 +81,7 @@
|
|||||||
openmct.install(openmct.plugins.Tabs());
|
openmct.install(openmct.plugins.Tabs());
|
||||||
openmct.install(openmct.plugins.FlexibleLayout());
|
openmct.install(openmct.plugins.FlexibleLayout());
|
||||||
openmct.install(openmct.plugins.LADTable());
|
openmct.install(openmct.plugins.LADTable());
|
||||||
|
openmct.install(openmct.plugins.ObjectMigration());
|
||||||
openmct.start();
|
openmct.start();
|
||||||
</script>
|
</script>
|
||||||
</html>
|
</html>
|
||||||
|
200
src/plugins/objectMigration/Migrations.js
Normal file
200
src/plugins/objectMigration/Migrations.js
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT, Copyright (c) 2014-2018, 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.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
define([
|
||||||
|
'uuid'
|
||||||
|
], function (
|
||||||
|
uuid
|
||||||
|
) {
|
||||||
|
function isTelemetry(domainObject) {
|
||||||
|
if (openmct.telemetry.isTelemetryObject(domainObject)
|
||||||
|
&& domainObject.type !== 'summary-widget'
|
||||||
|
&& domainObject.type !== 'example.imagery') {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function migrateDisplayLayout(domainObject, childObjects) {
|
||||||
|
const DEFAULT_GRID_SIZE = [32, 32];
|
||||||
|
let migratedObject = Object.assign({}, domainObject);
|
||||||
|
let panels = migratedObject.configuration.layout.panels;
|
||||||
|
let items = [];
|
||||||
|
|
||||||
|
Object.keys(panels).forEach(key => {
|
||||||
|
let panel = panels[key];
|
||||||
|
let domainObject = childObjects[key];
|
||||||
|
|
||||||
|
if (isTelemetry(domainObject)) {
|
||||||
|
items.push({
|
||||||
|
width: panel.dimensions[0],
|
||||||
|
height: panel.dimensions[1],
|
||||||
|
x: panel.position[0],
|
||||||
|
y: panel.position[1],
|
||||||
|
useGrid: true,
|
||||||
|
identifier: domainObject.identifier,
|
||||||
|
id: uuid(),
|
||||||
|
type: 'telemetry-view',
|
||||||
|
displayMode: 'all',
|
||||||
|
value: openmct.telemetry.getMetadata(domainObject).getDefaultDisplayValue(),
|
||||||
|
stroke: "transparent",
|
||||||
|
fill: "",
|
||||||
|
color: "",
|
||||||
|
size: "13px"
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
items.push({
|
||||||
|
width: panel.dimensions[0],
|
||||||
|
height: panel.dimensions[1],
|
||||||
|
x: panel.position[0],
|
||||||
|
y: panel.position[1],
|
||||||
|
useGrid: true,
|
||||||
|
identifier: domainObject.identifier,
|
||||||
|
id: uuid(),
|
||||||
|
type: 'subobject-view',
|
||||||
|
hasFrame: panel.hasFrame
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
migratedObject.configuration.items = items;
|
||||||
|
migratedObject.configuration.layoutGrid = migratedObject.layoutGrid || DEFAULT_GRID_SIZE;
|
||||||
|
delete migratedObject.layoutGrid;
|
||||||
|
delete migratedObject.configuration.layout;
|
||||||
|
return migratedObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
function migrateFixedPositionConfiguration(elements, telemetryObjects) {
|
||||||
|
const DEFAULT_STROKE = "transparent";
|
||||||
|
const DEFAULT_SIZE = "13px";
|
||||||
|
const DEFAULT_COLOR = "";
|
||||||
|
const DEFAULT_FILL = "";
|
||||||
|
let items = [];
|
||||||
|
|
||||||
|
elements.forEach(element => {
|
||||||
|
let item = {
|
||||||
|
x: element.x,
|
||||||
|
y: element.y,
|
||||||
|
width: element.width,
|
||||||
|
height: element.height,
|
||||||
|
useGrid: element.useGrid,
|
||||||
|
id: uuid()
|
||||||
|
};
|
||||||
|
|
||||||
|
if (element.type === "fixed.telemetry") {
|
||||||
|
item.type = "telemetry-view";
|
||||||
|
item.stroke = element.stroke || DEFAULT_STROKE;
|
||||||
|
item.fill = element.fill || DEFAULT_FILL;
|
||||||
|
item.color = element.color || DEFAULT_COLOR;
|
||||||
|
item.size = element.size || DEFAULT_SIZE;
|
||||||
|
item.identifier = telemetryObjects[element.id].identifier;
|
||||||
|
item.displayMode = element.titled ? 'all' : 'value';
|
||||||
|
item.value = openmct.telemetry.getMetadata(telemetryObjects[element.id]).getDefaultDisplayValue();
|
||||||
|
} else if (element.type === 'fixed.box') {
|
||||||
|
item.type = "box-view";
|
||||||
|
item.stroke = element.stroke || DEFAULT_STROKE;
|
||||||
|
item.fill = element.fill || DEFAULT_FILL;
|
||||||
|
} else if (element.type === 'fixed.line') {
|
||||||
|
item.type = "line-view";
|
||||||
|
item.x2 = element.x2;
|
||||||
|
item.y2 = element.y2;
|
||||||
|
item.stroke = element.stroke || DEFAULT_STROKE;
|
||||||
|
delete item.height;
|
||||||
|
delete item.width;
|
||||||
|
} else if (element.type === 'fixed.text') {
|
||||||
|
item.type = "text-view";
|
||||||
|
item.text = element.text;
|
||||||
|
item.stroke = element.stroke || DEFAULT_STROKE;
|
||||||
|
item.fill = element.fill || DEFAULT_FILL;
|
||||||
|
item.color = element.color || DEFAULT_COLOR;
|
||||||
|
item.size = element.size || DEFAULT_SIZE;
|
||||||
|
} else if (element.type === 'fixed.image') {
|
||||||
|
item.type = "image-view";
|
||||||
|
item.url =element.url;
|
||||||
|
item.stroke = element.stroke || DEFAULT_STROKE;
|
||||||
|
}
|
||||||
|
|
||||||
|
items.push(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
check(domainObject) {
|
||||||
|
return domainObject.type === 'layout' && domainObject.configuration.layout;
|
||||||
|
},
|
||||||
|
migrate(domainObject) {
|
||||||
|
let childObjects = {};
|
||||||
|
let promises = Object.keys(domainObject.configuration.layout.panels).map(key => {
|
||||||
|
return openmct.objects.get(key)
|
||||||
|
.then(object => {
|
||||||
|
childObjects[key] = object;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(promises)
|
||||||
|
.then(function () {
|
||||||
|
return migrateDisplayLayout(domainObject, childObjects);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
check(domainObject) {
|
||||||
|
return domainObject.type === 'telemetry.fixed' && domainObject.configuration['fixed-display'];
|
||||||
|
},
|
||||||
|
migrate(domainObject) {
|
||||||
|
const DEFAULT_GRID_SIZE = [64, 16];
|
||||||
|
let newLayoutObject = {
|
||||||
|
identifier: domainObject.identifier,
|
||||||
|
location: domainObject.location,
|
||||||
|
name: domainObject.name,
|
||||||
|
type: "layout"
|
||||||
|
};
|
||||||
|
let layoutType = openmct.types.get('layout');
|
||||||
|
layoutType.definition.initialize(newLayoutObject);
|
||||||
|
newLayoutObject.composition = domainObject.composition;
|
||||||
|
newLayoutObject.configuration.layoutGrid = domainObject.layoutGrid || DEFAULT_GRID_SIZE;
|
||||||
|
|
||||||
|
let elements = domainObject.configuration['fixed-display'].elements;
|
||||||
|
let telemetryObjects = {};
|
||||||
|
let promises = elements.map(element => {
|
||||||
|
if (element.id) {
|
||||||
|
return openmct.objects.get(element.id)
|
||||||
|
.then(object => {
|
||||||
|
telemetryObjects[element.id] = object;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(promises)
|
||||||
|
.then(function () {
|
||||||
|
newLayoutObject.configuration.items =
|
||||||
|
migrateFixedPositionConfiguration(elements, telemetryObjects);
|
||||||
|
return newLayoutObject;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
});
|
51
src/plugins/objectMigration/plugin.js
Normal file
51
src/plugins/objectMigration/plugin.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT, Copyright (c) 2014-2018, 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.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
import migrations from './Migrations.js'
|
||||||
|
|
||||||
|
export default function () {
|
||||||
|
function needsMigration(domainObject) {
|
||||||
|
return migrations.some(m => m.check(domainObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
function migrateObject(domainObject) {
|
||||||
|
return migrations.filter(m => m.check(domainObject))[0]
|
||||||
|
.migrate(domainObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return function (openmct) {
|
||||||
|
let wrappedFunction = openmct.objects.get;
|
||||||
|
openmct.objects.get = function migrate(identifier) {
|
||||||
|
return wrappedFunction.apply(openmct.objects, [identifier])
|
||||||
|
.then(function (object) {
|
||||||
|
if (needsMigration(object)) {
|
||||||
|
migrateObject(object)
|
||||||
|
.then(newObject => {
|
||||||
|
openmct.objects.mutate(newObject, 'persisted', Date.now());
|
||||||
|
return newObject;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -39,7 +39,8 @@ define([
|
|||||||
'./folderView/plugin',
|
'./folderView/plugin',
|
||||||
'./flexibleLayout/plugin',
|
'./flexibleLayout/plugin',
|
||||||
'./tabs/plugin',
|
'./tabs/plugin',
|
||||||
'./LADTable/plugin'
|
'./LADTable/plugin',
|
||||||
|
'./objectMigration/plugin'
|
||||||
], function (
|
], function (
|
||||||
_,
|
_,
|
||||||
UTCTimeSystem,
|
UTCTimeSystem,
|
||||||
@ -59,7 +60,8 @@ define([
|
|||||||
FolderView,
|
FolderView,
|
||||||
FlexibleLayout,
|
FlexibleLayout,
|
||||||
Tabs,
|
Tabs,
|
||||||
LADTable
|
LADTable,
|
||||||
|
ObjectMigration
|
||||||
) {
|
) {
|
||||||
var bundleMap = {
|
var bundleMap = {
|
||||||
LocalStorage: 'platform/persistence/local',
|
LocalStorage: 'platform/persistence/local',
|
||||||
@ -174,6 +176,7 @@ define([
|
|||||||
plugins.Tabs = Tabs;
|
plugins.Tabs = Tabs;
|
||||||
plugins.FlexibleLayout = FlexibleLayout;
|
plugins.FlexibleLayout = FlexibleLayout;
|
||||||
plugins.LADTable = LADTable;
|
plugins.LADTable = LADTable;
|
||||||
|
plugins.ObjectMigration = ObjectMigration.default;
|
||||||
|
|
||||||
return plugins;
|
return plugins;
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user