-
Notifications
You must be signed in to change notification settings - Fork 78
/
ArtifactIndexControl.js
294 lines (242 loc) · 9.94 KB
/
ArtifactIndexControl.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*globals define, WebGMEGlobal*/
/*jshint browser: true*/
define([
'deepforge/storage/index',
'deepforge/globals',
'blob/BlobClient',
'js/Constants',
'q',
], function (
Storage,
DeepForge,
BlobClient,
CONSTANTS,
Q,
) {
'use strict';
var ArtifactIndexControl;
ArtifactIndexControl = function (options) {
this._logger = options.logger.fork('Control');
this.blobClient = new BlobClient({
logger: this._logger.fork('BlobClient')
});
this._client = options.client;
this._embedded = options.embedded;
// Initialize core collections and variables
this._widget = options.widget;
this._currentNodeId = null;
this._initWidgetEventHandlers();
this._logger.debug('ctor finished');
};
ArtifactIndexControl.prototype._initWidgetEventHandlers = function () {
this._widget.onNodeClick = (/*id*/) => {
// Change the current active object
// This is currently disabled as there are not any good
// visualizers for the data types
// WebGMEGlobal.State.registerActiveObject(id);
};
this._widget.onNodeDeleteClicked = async (id, config) => {
const node = this._client.getNode(id);
const name = node.getAttribute('name');
const msg = `Deleted "${name}" artifact (${id})`;
const deleteDataFromStorage = !!config;
if (deleteDataFromStorage) {
const dataInfo = this.getDataInfo(node);
const {backend} = dataInfo;
const preparedConfig = await Storage.prepareConfig(backend, config);
const storage = await Storage.getClient(backend, this._logger, preparedConfig);
await storage.deleteFile(dataInfo);
}
this._client.startTransaction(msg);
this._client.deleteNode(id);
this._client.completeTransaction();
};
this._widget.onAttributeChange = (id, attr, newValue) => {
const node = this._client.getNode(id);
const name = node.getAttribute('name');
const oldValue = node.getAttribute(attr);
const fromMsg = oldValue ? ` (from ${oldValue})` : '';
const msg = `Set ${name} artifact's ${attr} to ${newValue}${fromMsg}`;
this._client.startTransaction(msg);
this._client.setAttribute(id, attr, newValue);
this._client.completeTransaction();
};
this._widget.$el.on(
'ReifyProvenance',
(event, artifactId) => this.reifyProvenance(artifactId),
);
};
ArtifactIndexControl.prototype.reifyProvenance = async function (artifactId) {
const pluginId = 'ReifyArtifactProv';
const pluginContext = this._client.getCurrentPluginContext(pluginId);
pluginContext.managerConfig.activeNode = await DeepForge.places.MyPipelines();
pluginContext.managerConfig.namespace = 'pipeline';
pluginContext.pluginConfig = {artifactId};
const territory = {};
territory[pluginContext.managerConfig.activeNode] = {children: 0};
const result = await this.doInTerritory(
territory,
() => Q.ninvoke(this._client, 'runBrowserPlugin', pluginId, pluginContext),
);
const [{activeNode: pipeline}] = result.messages;
WebGMEGlobal.State.registerActiveObject(pipeline.id);
};
ArtifactIndexControl.prototype.doInTerritory = async function (territory, fn) {
const deferred = Q.defer();
const ui = this._client.addUI(this, async () => {
const result = await fn();
this._client.removeUI(ui);
deferred.resolve(result);
});
this._client.updateTerritory(ui, territory);
return deferred.promise;
};
/* * * * * * * * Visualizer content update callbacks * * * * * * * */
// One major concept here is with managing the territory. The territory
// defines the parts of the project that the visualizer is interested in
// (this allows the browser to then only load those relevant parts).
ArtifactIndexControl.prototype.selectedObjectChanged = function (nodeId) {
this._logger.debug('activeObject nodeId \'' + nodeId + '\'');
// Remove current territory patterns
if (this._currentNodeId) {
this._client.removeUI(this._territoryId);
}
this._currentNodeId = nodeId;
if (typeof this._currentNodeId === 'string') {
// Put new node's info into territory rules
this._widget.currentNode = this._currentNodeId;
this._selfPatterns = {};
this._territoryId = this._client.addUI(this, events => {
this._eventCallback(events);
});
this._selfPatterns[nodeId] = {children: 1};
this._client.updateTerritory(this._territoryId, this._selfPatterns);
}
};
// This next function retrieves the relevant node information for the widget
ArtifactIndexControl.prototype._getObjectDescriptor = async function (nodeId) {
const node = this._client.getNode(nodeId);
const dataInfo = this.tryGetDataInfo(node);
let backendName;
if(dataInfo) {
backendName = Storage.getStorageMetadata(dataInfo.backend).name;
}
if (node && dataInfo) {
const type = node.getAttribute('type');
const metadata = await Storage.getMetadata(dataInfo, this._logger);
const size = this._humanFileSize(metadata.size);
return {
id: node.getId(),
type: type,
name: node.getAttribute('name'),
createdAt: node.getAttribute('createdAt'),
parentId: node.getParentId(),
backendName: backendName,
hasProvenance: node.getPointer('provenance').to,
dataInfo,
size,
};
}
};
ArtifactIndexControl.prototype.getDataInfo = function (node) {
const rawDataInfo = node.getAttribute('data');
try {
return JSON.parse(rawDataInfo);
} catch (err) {
if (rawDataInfo) {
throw new Error(`Invalid DataInfo: "${rawDataInfo}"`);
} else {
throw new Error('Missing DataInfo');
}
}
};
ArtifactIndexControl.prototype.tryGetDataInfo = function (node) {
try {
return this.getDataInfo(node);
} catch (err) {
return null;
}
};
ArtifactIndexControl.prototype._humanFileSize = function (bytes, si) {
var thresh = si ? 1000 : 1024,
units = si ?
['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] :
['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
u = -1;
if (bytes < thresh) {
return bytes + ' B';
}
do {
bytes = bytes / thresh;
u += 1;
} while (bytes >= thresh);
return bytes.toFixed(1) + ' ' + units[u];
};
/* * * * * * * * Node Event Handling * * * * * * * */
ArtifactIndexControl.prototype._eventCallback = function (events) {
events = events.filter(event => event.eid !== this._currentNodeId);
let i = events ? events.length : 0;
this._logger.debug('_eventCallback \'' + i + '\' items');
let event;
while (i--) {
event = events[i];
switch (event.etype) {
case CONSTANTS.TERRITORY_EVENT_LOAD:
this._onLoad(event.eid);
break;
case CONSTANTS.TERRITORY_EVENT_UPDATE:
this._onUpdate(event.eid);
break;
case CONSTANTS.TERRITORY_EVENT_UNLOAD:
this._onUnload(event.eid);
break;
default:
break;
}
}
this._logger.debug('_eventCallback \'' + events.length + '\' items - DONE');
};
ArtifactIndexControl.prototype._onLoad = function (gmeId) {
this._getObjectDescriptor(gmeId).then(desc => this._widget.addNode(desc));
};
ArtifactIndexControl.prototype._onUpdate = function (gmeId) {
this._getObjectDescriptor(gmeId).then(desc => this._widget.updateNode(desc));
};
ArtifactIndexControl.prototype._onUnload = function (gmeId) {
this._widget.removeNode(gmeId);
};
ArtifactIndexControl.prototype._stateActiveObjectChanged = function (model, activeObjectId) {
if (this._currentNodeId === activeObjectId) {
// The same node selected as before - do not trigger
} else {
this.selectedObjectChanged(activeObjectId);
}
};
/* * * * * * * * Visualizer life cycle callbacks * * * * * * * */
ArtifactIndexControl.prototype.destroy = function () {
this._detachClientEventListeners();
};
ArtifactIndexControl.prototype._attachClientEventListeners = function () {
this._detachClientEventListeners();
if (!this._embedded) {
WebGMEGlobal.State.on('change:' + CONSTANTS.STATE_ACTIVE_OBJECT, this._stateActiveObjectChanged, this);
}
};
ArtifactIndexControl.prototype._detachClientEventListeners = function () {
if (!this._embedded) {
WebGMEGlobal.State.off('change:' + CONSTANTS.STATE_ACTIVE_OBJECT, this._stateActiveObjectChanged);
}
};
ArtifactIndexControl.prototype.onActivate = function () {
this._attachClientEventListeners();
if (typeof this._currentNodeId === 'string') {
WebGMEGlobal.State.registerSuppressVisualizerFromNode(true);
WebGMEGlobal.State.registerActiveObject(this._currentNodeId);
WebGMEGlobal.State.registerSuppressVisualizerFromNode(false);
}
};
ArtifactIndexControl.prototype.onDeactivate = function () {
this._detachClientEventListeners();
};
return ArtifactIndexControl;
});