-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathUpdates.js
More file actions
212 lines (194 loc) · 8.57 KB
/
Updates.js
File metadata and controls
212 lines (194 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* globals define */
define([
'deepforge/storage/index',
'deepforge/viz/PlotlyDescExtractor',
'deepforge/viz/FigureExtractor',
'./Version',
'text!deepforge/extensions/Libraries.json',
], function(
Storage,
PlotlyDescExtractor,
FigureExtractor,
Version,
LibrariesTxt,
) {
const Libraries = JSON.parse(LibrariesTxt);
const GRAPH = 'Graph';
const getGraphNodes = async function(core, rootNode, graphNodes=[]) {
const children = await core.loadChildren(rootNode);
for(let i = 0; i < children.length; i++) {
if (core.getAttribute(children[i], 'name') === GRAPH && !core.isMetaNode(children[i])) {
graphNodes.push(children[i]);
}
await getGraphNodes(core, children[i], graphNodes);
}
};
const addMetadataMixinToNodeSubTree = async function(core, META, node) {
const METADATA_NODE_PATH = core.getPath(META['pipeline.Metadata']);
const IMPLICIT_OPERATION_NODE = META['pipeline.ImplicitOperation'];
const graphNodeChildren = (await core.loadSubTree(node))
.filter(node => {
return IMPLICIT_OPERATION_NODE ?
!core.isTypeOf(node, IMPLICIT_OPERATION_NODE) :
true;
});
graphNodeChildren.forEach(node => {
core.addMixin(node, METADATA_NODE_PATH);
});
};
const getPipelineLibraryVersion = function(core, rootNode) {
const pipelineRoot = core.getLibraryRoot(rootNode, 'pipeline');
const hasPipelineLibrary = !!pipelineRoot;
if (hasPipelineLibrary) {
const versionString = core.getAttribute(pipelineRoot, 'version');
return new Version(versionString);
}
};
const allUpdates = [
{
name: 'CustomUtilities',
isNeeded: function(core, rootNode) {
// Check the root directory for a MyUtilities node
return core.loadChildren(rootNode)
.then(children => {
const names = children.map(node => core.getAttribute(node, 'name'));
return !names.includes('MyUtilities');
});
},
apply: function(core, rootNode, META) {
// Create 'MyUtilities' node
const utils = core.createNode({
parent: rootNode,
base: META.FCO
});
core.setAttribute(utils, 'name', 'MyUtilities');
// Add 'MyUtilities' to the META
const META_ASPECT_SET_NAME = 'MetaAspectSet';
const META_SHEETS = 'MetaSheets';
const tabId = core.getRegistry(rootNode, META_SHEETS)
.find(desc => desc.order === 0)
.SetID;
core.addMember(rootNode, META_ASPECT_SET_NAME, utils);
core.addMember(rootNode, tabId, utils);
// Add 'Code' from 'pipelines' as a valid child
core.setChildMeta(utils, META['pipeline.Code']);
// Set the default visualizer to TabbedTextEditor
core.setRegistry(utils, 'validVisualizers', 'TabbedTextEditor');
}
},
{
name: 'UpdateDataNodesToUserAssets',
isNeeded: async function(core, rootNode) {
const pipelineLibraryVersion = getPipelineLibraryVersion(core, rootNode);
if(pipelineLibraryVersion) {
return pipelineLibraryVersion.lessThan(new Version('0.13.0'));
}
},
apply: async function(core, rootNode, META) {
const isDataNode = node => core.getMetaType(node) === META['pipeline.Data'];
const dataNodes = (await core.loadSubTree(rootNode))
.filter(isDataNode)
.filter(node => !core.isLibraryElement(node));
const storageClient = await Storage.getBackend('gme').getClient();
for (let i = dataNodes.length; i--;) {
const node = dataNodes[i];
const hash = core.getAttribute(node, 'data');
if (hash && !hash.includes('{')) { // not already updated
const dataInfo = storageClient.createDataInfo(hash);
core.setAttribute(node, 'data', JSON.stringify(dataInfo));
}
}
}
},
{
name: 'UpdateGraphContainment',
beforeLibraryUpdates: true,
isNeeded: async function(core, rootNode) {
const pipelineLibraryVersion = getPipelineLibraryVersion(core, rootNode);
if (pipelineLibraryVersion) {
return pipelineLibraryVersion.lessThan(new Version('0.22.0')) &&
pipelineLibraryVersion.greaterThan(new Version('0.19.1'));
}
},
apply: async function(core, rootNode, META) {
let graphNodes = [];
await getGraphNodes(core, rootNode, graphNodes);
const coreFigureExtractor = new FigureExtractor.CoreFigureExtractor(core, rootNode);
const pipelineVersion = getPipelineLibraryVersion(core, rootNode);
const shouldAddMetadataMixin = pipelineVersion ?
pipelineVersion.lessThan(new Version('0.21.1')) :
false;
for (let i = 0; i < graphNodes.length; i++){
const graphNode = graphNodes[i];
if(shouldAddMetadataMixin){
await addMetadataMixinToNodeSubTree(core, META, graphNode);
}
const desc = await coreFigureExtractor.extract(graphNode);
const plotlyJSON = PlotlyDescExtractor.descToPlotlyJSON(desc);
const parentNode = core.getParent(graphNode);
const updatedGraphNode = core.createNode({
parent: parentNode,
base: META['pipeline.Graph']
});
core.setAttribute(updatedGraphNode, 'data', JSON.stringify(plotlyJSON));
core.deleteNode(graphNode);
}
}
}
];
const Updates = {};
Updates.getAvailableUpdates = async function(core, rootNode) {
const migrations = await this.getMigrationUpdates(core, rootNode);
const libUpdates = await this.getLibraryUpdates(core, rootNode);
return migrations.concat(libUpdates);
};
Updates.getMigrationUpdates = async function(core, rootNode) {
const isNeeded = await Promise.all(
allUpdates.map(update => update.isNeeded(core, rootNode))
);
const updates = allUpdates.filter((update, i) => isNeeded[i]);
return updates.map(update => ({
type: Updates.MIGRATION,
name: update.name,
}));
};
Updates.getLibraryUpdates = async function(core, rootNode) {
const children = await core.loadChildren(rootNode);
const libraryType = Object.values(core.getAllMetaNodes(rootNode))
.find(node => core.getAttribute(node, 'name') === 'LibraryCode');
const libraries = children.filter(child => core.isTypeOf(child, libraryType));
const libUpdates = Libraries.filter(libraryInfo => {
const {name, version} = libraryInfo;
const libNode = libraries
.find(library => {
const nodeName = core.getAttribute(library, 'name');
return nodeName === name || nodeName === `${name}InitCode`;
});
if (libNode) {
const nodeVersion = core.getAttribute(libNode, 'version');
const installedVersion = new Version(nodeVersion || '0.0.0');
const availableVersion = new Version(version || '0.0.0');
return installedVersion.lessThan(availableVersion);
}
});
return libUpdates.map(libInfo => ({
type: Updates.LIBRARY,
name: `Update ${libInfo.name} library`,
info: libInfo,
}));
};
Updates.getUpdates = function(names) {
if (names) {
return allUpdates.filter(update => names.includes(update.name));
}
return allUpdates;
};
Updates.getUpdate = function(name) {
return Updates.getUpdates([name])[0];
};
// Constants
Updates.MIGRATION = 'Migration';
Updates.SEED = 'SeedUpdate';
Updates.LIBRARY = 'Library';
return Updates;
});