Blame view

js/app/models/BkgJobNode.js 14.9 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
/** 
 * Project  : AMDA-NG4
 * Name     : BkgJobNode.js
 * @class   amdaModel.BkgJobNode
 * @extends amdaModel.AmdaNode
 * @brief   Basic Model of Node corresponding to Amda processes in background
 * @author  elena
 * @version $Id: BkgJobNode.js 1963 2013-12-06 17:50:37Z elena $
16035364   Benjamin Renard   First commit
9
10
11
 */

Ext.define('amdaModel.BkgJobNode', {
6bfa699d   Elena.Budnik   Statistics -> Stats
12
13
14
15
16
		extend: 'amdaModel.ExecutableNode',		
		requires: [
			'amdaPlotObj.PlotRequestObject'
		],    
		action : null,
16035364   Benjamin Renard   First commit
17
    
6bfa699d   Elena.Budnik   Statistics -> Stats
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
		statics: {       	 	 
			nodeType: 'bkgWorks',
			JOB_TYPES: {
				PLOT: 'request',//'plot',
				CONDITION: 'condition',
				DOWNLOAD: 'download',
				STATISTICS: 'statistics'
			},
			JOB_ROOT_NODE: {
				PLOT: 'bkgPlot-treeRootNode',
				CONDITION: 'bkgSearch-treeRootNode',
				DOWNLOAD: 'bkgDown-treeRootNode',
				STATISTICS: 'bkgStatistics-treeRootNode'
			},
			RES_ROOT_NODE: {
				PLOT: 'resPlot-treeRootNode',
				CONDITION: 'resSearch-treeRootNode',
				DOWNLOAD:  'resDown-treeRootNode',
				STATISTICS: 'resStatistics-treeRootNode'
			},
			STATUS_LIST: {
				IN_PROGRESS: 'in_progress',
				DONE: 'done',
				ERROR: 'error' 
			}	                     
		},
		fields: [ 
a038d0e2   Elena.Budnik   redmine #5687
45
			// name, id, moduleId, object > from parent 
6bfa699d   Elena.Budnik   Statistics -> Stats
46
47
48
49
50
51
52
53
54
55
56
			{ name : 'jobType', type : 'string' },
			{ name : 'processId', type : 'string' },
			{ name : 'status', type : 'string' },
			{ name : 'percentDone', type : 'string', defaultValue : '0' },
			{ name : 'start', type: 'date' },
			// { name : 'duration', type: 'int' , defaultValue: -1}
			{ name : 'stop', type: 'date', defaultValue: null },
			{ name : 'rawname', type: 'string'},
			{ name : 'tabId', type: 'string'},
			{ name : 'info', type: 'string'}
		],
16035364   Benjamin Renard   First commit
57

6bfa699d   Elena.Budnik   Statistics -> Stats
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		constructor : function(config)
		{  
			this.callParent(arguments);
			this.set('ownerTreeId', amdaUI.ExplorerUI.JOB_TAB.TREE_ID);   
			
			if (this.isLeaf()) 
			{
				switch (this.get('status')) 
				{
					case amdaModel.BkgJobNode.STATUS_LIST.ERROR: 
						//     this.set('status' ,amdaModel.BkgJobNode.STATUS_LIST.ERROR);
						this.set('iconCls','icon-error'); 
						break;             
					case amdaModel.BkgJobNode.STATUS_LIST.DONE:
						// set status
a038d0e2   Elena.Budnik   redmine #5687
73
						// this.set('status',amdaModel.BkgJobNode.STATUS_LIST.DONE);              
6bfa699d   Elena.Budnik   Statistics -> Stats
74
75
76
77
78
79
80
81
						this.set('iconCls','icon-done');
						break;          
					default : 
						this.set('percentDone',this.get('status')); 
						this.set('status',amdaModel.BkgJobNode.STATUS_LIST.IN_PROGRESS);		  
						this.set('iconCls','icon-pend');			      
				}
				
a038d0e2   Elena.Budnik   redmine #5687
82
83
84
				if (!this.get('processId') && this.get('id'))
					this.set('processId', this.get('id'));
				
6bfa699d   Elena.Budnik   Statistics -> Stats
85
86
87
88
				if (config) this.set('jobType', config.jobType);
				else this.set('jobType',this.get('jobType'));
			}     
		},
16035364   Benjamin Renard   First commit
89
  
6bfa699d   Elena.Budnik   Statistics -> Stats
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
		getAllContextMenuItems: function()
		{
			if (this.get('status') == amdaModel.BkgJobNode.STATUS_LIST.IN_PROGRESS)       
				return [
				{
					fnId : 'leaf-deleteNode',
					text : 'Stop/Delete Job'
				}/*, {
					fnId : 'leaf-showNode',
					text : 'Show Job Details'
				}, {
					fnId : 'leaf-renameNode',
					text : 'Rename Job'   
				}*/];
			else
				return [{
					fnId : 'leaf-editNode',
					text : 'Show Result' 
				},{
					fnId : 'leaf-deleteNode',
					text : 'Delete Result'
				}/*, {
					fnId : 'leaf-renameNode',
					text : 'Rename Result' 
				}*/];		
		},
16035364   Benjamin Renard   First commit
116

6bfa699d   Elena.Budnik   Statistics -> Stats
117
118
119
120
121
122
123
		getMultiContextMenuItems: function()
		{
			return [{
				fnId : 'mult-deleteMulti',
				text : 'Delete Results'
			}];
		},
16035364   Benjamin Renard   First commit
124
        
6bfa699d   Elena.Budnik   Statistics -> Stats
125
126
127
		onMenuItemClick : function(menu, item, event) 
		{
			var fnId = Ext.util.Format.substr(item.fnId, 5, item.fnId.length);
29c449bb   Elena.Budnik   format + redmine ...
128
129
         
			switch (fnId) {
6bfa699d   Elena.Budnik   Statistics -> Stats
130
131
132
133
134
135
136
137
138
139
140
141
142
				case 'deleteNode':      
					this.deleteJob();
					this.deleteNode();  
					break;					
				case 'showNode':  
					//alert('Show details pid ' + this.get('pid')); 
					alert('NOT IMPLEMENTED YET');
					break;    	
				case 'renameNode':  
					// alert('rename pid ' + this.get('pid')); 
					alert('NOT IMPLEMENTED YET');
					// this.renameNode(); 
					break; 						
29c449bb   Elena.Budnik   format + redmine ...
143
144
145
146
				case 'editNode':   
					// create object if it doesn't exist (tree load)	  
					// TODO use data model ?  	
					if (!this.get('object')) {
6bfa699d   Elena.Budnik   Statistics -> Stats
147
						AmdaAction.getObject(this.get('id'), this.get('nodeType'), this.getObjectCallback, this);		  
29c449bb   Elena.Budnik   format + redmine ...
148
149
					}
					else {
6bfa699d   Elena.Budnik   Statistics -> Stats
150
						var isInteractive = false;
29c449bb   Elena.Budnik   format + redmine ...
151
						var isNewTab = true;
6bfa699d   Elena.Budnik   Statistics -> Stats
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
						this.editNode(isNewTab, isInteractive);
					}
					break; 	        
				case 'deleteMulti':							
					var selectedNodes = this.myGetOwnerTree().getSelectionModel().selected.items;
					if (selectedNodes.length > 0) 
					{
						Ext.Array.each(selectedNodes,function(item, index){
								item.deleteJob();
								item.deleteNode();
						});
					}
					break;            
				default:
					break;
			}   
		},
16035364   Benjamin Renard   First commit
169
        
6bfa699d   Elena.Budnik   Statistics -> Stats
170
171
172
173
174
175
176
177
178
		deleteJob :  function() 
		{
			if (this.get('status') == amdaModel.BkgJobNode.STATUS_LIST.IN_PROGRESS) 
			{
				amdaDesktop.JobsMgr.jobsInProgress--;          
				if (amdaDesktop.JobsMgr.jobsInProgress == 0 && amdaDesktop.JobsMgr.updateStatus)                      
					Ext.TaskManager.stop(amdaDesktop.JobsMgr.updateStatus);
				
				amdaDesktop.JobsMgr.updateStatus = null;
16035364   Benjamin Renard   First commit
179
                    
6bfa699d   Elena.Budnik   Statistics -> Stats
180
181
182
183
184
185
186
187
188
189
190
191
192
				switch (this.get('jobType')) 
				{
					case 'condition' : var type = 'Data Mining '; break;
					case 'request' : var type = 'Plot '; break;   
					case 'download' : var type = 'Download '; break;
					case 'statistics' : var type = 'Statistics '; break;
					default: var type = 'unknown';    
				}
				
				var message = Ext.Date.format(new Date(), 'd-m-Y H:i:s: ') + ': '+ type + ' '+this.get('text') + ' killed';
				myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.explorer.id, true, function (module) {
					module.addLogMessage(message); 
				});
e511f2cc   elena   stat node
193
			}
6bfa699d   Elena.Budnik   Statistics -> Stats
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
			//delete fieldset from Result Module if it is open       
			else {
				var me = this;
				myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.result.id, true, function (module) {
					if (module && module.linkedNodes) 
					{                 
						module.linkedNodes.each(function(item, index)
						{                     
							if (me.get('id') == item.get('id')) {                 
									module.removeLinkedNode(item, index);
									return false;
							}
						}, this);                      
					}
				});
			} 
		},	
16035364   Benjamin Renard   First commit
211
    
29c449bb   Elena.Budnik   format + redmine ...
212
213
		getObjectCallback : function(result,remoteEvent){
			var t = remoteEvent.getTransaction();        
6bfa699d   Elena.Budnik   Statistics -> Stats
214
			//AKKA - Rework of the result treatment for the integration with the new kernel
29c449bb   Elena.Budnik   format + redmine ...
215
			if (!remoteEvent.status) {
6bfa699d   Elena.Budnik   Statistics -> Stats
216
217
218
				myDesktopApp.errorMsg('Internal error during download request');
				return;
			}
16035364   Benjamin Renard   First commit
219
 	   	
29c449bb   Elena.Budnik   format + redmine ...
220
			if (!result.success) {
6bfa699d   Elena.Budnik   Statistics -> Stats
221
222
223
				myDesktopApp.errorMsg(result.message);
				return;
			}
16035364   Benjamin Renard   First commit
224
        
6bfa699d   Elena.Budnik   Statistics -> Stats
225
			var obj = null;
29c449bb   Elena.Budnik   format + redmine ...
226
227
			
			switch (this.get('jobType')) {
6bfa699d   Elena.Budnik   Statistics -> Stats
228
229
230
231
232
233
234
235
236
237
238
239
240
				case 'condition' : 
					obj = Ext.create('amdaModel.Search',
							{name: result.name,
							resultId : result.result,
							folderId : result.folder});
					break;
				case 'statistics' : 
					obj = Ext.create('amdaModel.Stats',
							{name: result.name,
							resultId : result.result,
							folderId : result.folder});
					break;
				case 'request' : 
29c449bb   Elena.Budnik   format + redmine ...
241
					obj = Ext.create('amdaPlotObj.PlotRequestObject',
6bfa699d   Elena.Budnik   Statistics -> Stats
242
243
244
245
246
247
248
249
250
251
252
253
							{name: result.name, format: result.format,
							resultId : result.result,
							folderId : result.folder}); 
					break;   
				case 'download' : 
					obj = Ext.create('amdaModel.Download',
							{name: result.name,
							compression: result.compression,
							resultId : result.result,
							folderId : result.folder}); 
					break;
			}
16035364   Benjamin Renard   First commit
254
        	
29c449bb   Elena.Budnik   format + redmine ...
255
			if (!obj){
6bfa699d   Elena.Budnik   Statistics -> Stats
256
257
				myDesktopApp.errorMsg("Unknown job type");
				return;
29c449bb   Elena.Budnik   format + redmine ...
258
			}
16035364   Benjamin Renard   First commit
259
        
6bfa699d   Elena.Budnik   Statistics -> Stats
260
261
			// set parameter into node
			this.set('object',obj);
16035364   Benjamin Renard   First commit
262
        
6bfa699d   Elena.Budnik   Statistics -> Stats
263
			var isInteractive = false;
29c449bb   Elena.Budnik   format + redmine ...
264
265
			var isNewTab = true;
			
6bfa699d   Elena.Budnik   Statistics -> Stats
266
267
			this.editNode(isNewTab, isInteractive);	   
		},
16035364   Benjamin Renard   First commit
268
 
6bfa699d   Elena.Budnik   Statistics -> Stats
269
		// Show Result 
29c449bb   Elena.Budnik   format + redmine ...
270
		editNode : function(isNewTab, isInteractive) {
6bfa699d   Elena.Budnik   Statistics -> Stats
271
			var me = this;
29c449bb   Elena.Budnik   format + redmine ...
272
			if (isInteractive) {
6bfa699d   Elena.Budnik   Statistics -> Stats
273
274
275
276
277
278
279
280
281
282
				myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.interactive_plot.id, true, function(module) {
					if (isNewTab) {	 
						module.setLinkedNode(me); 
					}	
					module.createWindow();
				});
			}
			else 
			{
				myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.result.id, true, function(module) {
29c449bb   Elena.Budnik   format + redmine ...
283
284
285
					if (isNewTab) {	
						module.setLinkedNode(me);
					}
6bfa699d   Elena.Budnik   Statistics -> Stats
286
287
288
289
					module.createWindow();
				});
			}
		},
16035364   Benjamin Renard   First commit
290
  
6bfa699d   Elena.Budnik   Statistics -> Stats
291
292
293
294
295
296
297
298
		createJobNode: function(isResult)
		{       
			// if ownerTree panel is not active
			if (this.myGetOwnerTree().ownerCt.getActiveTab() !== this.myGetOwnerTree()) {
				// set ownerTree panel as the active tab
				this.myGetOwnerTree().ownerCt.setActiveTab(this.myGetOwnerTree());
				// to enable selection of this node his ownerTree must have a view
			}
16035364   Benjamin Renard   First commit
299
        
6bfa699d   Elena.Budnik   Statistics -> Stats
300
301
302
303
304
305
306
			var rootNode = this.getRootNode(isResult);
			// expand the corresponding rootNode
			if (rootNode.parentNode) {
				if (!rootNode.parentNode.isExpanded()) {
					rootNode.parentNode.expand(false);
				}
			}  
16035364   Benjamin Renard   First commit
307
        
6bfa699d   Elena.Budnik   Statistics -> Stats
308
309
310
311
312
313
314
315
316
317
318
319
320
321
			if (!rootNode.isExpanded())  {                                                        
				rootNode.expand(false, function(){
					if (!this.parentNode) {
						nodeFantom = rootNode.findChild('id',this.get('id'));  
						if (nodeFantom) {                                    
							nodeFantom.remove();
							rootNode.appendChild(this);
						}
					}                        
				}, this);                                                        
			}
			else {                
				rootNode.appendChild(this);
			}                                                                                                   
16035364   Benjamin Renard   First commit
322
                
6bfa699d   Elena.Budnik   Statistics -> Stats
323
324
325
326
327
328
329
330
			// select the new node                  
			this.myGetOwnerTree().getSelectionModel().select(this);
			if (!isResult)
			{
				switch (this.get('jobType')) {
					case 'condition' : var type = 'Data Mining '; break;
					case 'request' : var type = 'Plot '; break;   
					case 'download' : var type = 'Download '; break;
4078a635   Elena.Budnik   small cleanning up
331
					case 'statistics' : var type = 'Statistics '; break;
6bfa699d   Elena.Budnik   Statistics -> Stats
332
333
334
335
336
337
338
					default: var type = 'unknown';    
				}
				myDesktopApp.infoMsg('Your request is still running and has been assigned the name '+this.get('text')+' -- Check Jobs in Progress');
				var message = Ext.Date.format(new Date(), 'd-m-Y H:i:s: ')+ type + ' '+this.get('text')+ ' created';
				myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.explorer.id, true, function (module) {
					module.addLogMessage(message);
				});
16035364   Benjamin Renard   First commit
339
	    
6bfa699d   Elena.Budnik   Statistics -> Stats
340
341
342
343
344
345
346
347
348
349
350
				amdaDesktop.JobsMgr.jobsInProgress++;          	 
				if (!amdaDesktop.JobsMgr.updateStatus)
				{
					amdaDesktop.JobsMgr.updateStatus =  
						Ext.TaskManager.start({
							run : amdaDesktop.JobsMgr.getStatus,
							interval : amdaDesktop.JobsMgr.interval 
						});
				}
			}
		},
16035364   Benjamin Renard   First commit
351
    
6bfa699d   Elena.Budnik   Statistics -> Stats
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
		//override amdaModel.AmdaNode.getRootNode method
		getRootNode: function(isResult)
		{    
			if (!this.get('rootNode'))
			{
				var rootNodeId;
				if (!isResult) 
				{
					switch(this.get('jobType'))
					{
						case amdaModel.BkgJobNode.JOB_TYPES.PLOT:		   
							rootNodeId = amdaModel.BkgJobNode.JOB_ROOT_NODE.PLOT;
						break;
						case amdaModel.BkgJobNode.JOB_TYPES.CONDITION:		   
							rootNodeId = amdaModel.BkgJobNode.JOB_ROOT_NODE.CONDITION;
						break;
						case amdaModel.BkgJobNode.JOB_TYPES.DOWNLOAD:		    
							rootNodeId = amdaModel.BkgJobNode.JOB_ROOT_NODE.DOWNLOAD;
						break;
						case amdaModel.BkgJobNode.JOB_TYPES.STATISTICS:                   
							rootNodeId = amdaModel.BkgJobNode.JOB_ROOT_NODE.STATISTICS;
						break;
						default:
						break;
					}
				}
				else 
				{
					switch(this.get('jobType'))
					{
						case amdaModel.BkgJobNode.JOB_TYPES.PLOT:		   
							rootNodeId = amdaModel.BkgJobNode.RES_ROOT_NODE.PLOT;
						break;
						case amdaModel.BkgJobNode.JOB_TYPES.CONDITION:		   
							rootNodeId = amdaModel.BkgJobNode.RES_ROOT_NODE.CONDITION;
						break;
						case amdaModel.BkgJobNode.JOB_TYPES.DOWNLOAD:		    
							rootNodeId = amdaModel.BkgJobNode.RES_ROOT_NODE.DOWNLOAD;
						break;
						case amdaModel.BkgJobNode.JOB_TYPES.STATISTICS:                   
							rootNodeId = amdaModel.BkgJobNode.RES_ROOT_NODE.STATISTICS;
						break;
						default:
						break;
					}
				} 
				this.set('rootNode',this.myGetOwnerTree().getRootNode().findChild( 'id', rootNodeId, true));
			}
			return this.get('rootNode');
		},
16035364   Benjamin Renard   First commit
402
    
6bfa699d   Elena.Budnik   Statistics -> Stats
403
404
405
406
407
408
409
410
411
412
413
414
415
416
		/**
		* @override amdaModel.ExecutableNode.execute  PNG (interactive session only!!!)
		*/
		execute : function(arguments) 
		{
			// Not needed to send the whole request
			//  var jsonObj = this.get('object').getJsonValues(true);
			//TODO append jsonObj.action at server side => history!!!
			var jsonObj = {};
			var isMulti =  arguments[0];	
			jsonObj.action = {name:arguments[1],arg1:arguments[2],arg2:arguments[3]};
			//AKKA replace resultID by folderId
			jsonObj.folderId =  this.get('object').get('folderId');
			//jsonObj.resultId =  this.get('object').get('resultFolder');
16035364   Benjamin Renard   First commit
417
	
6bfa699d   Elena.Budnik   Statistics -> Stats
418
			this.action = jsonObj.action.name;
16035364   Benjamin Renard   First commit
419
	
6bfa699d   Elena.Budnik   Statistics -> Stats
420
			//   this.set('tabId', jsonObj.tabId);
16035364   Benjamin Renard   First commit
421
	
6bfa699d   Elena.Budnik   Statistics -> Stats
422
423
424
			// Node exists already, interactive Session 
			var isInteractive = true;
			var isNewTab = false; 
16035364   Benjamin Renard   First commit
425
        
6bfa699d   Elena.Budnik   Statistics -> Stats
426
			loadMask.show(this.get('object').get('tabId'));
4078a635   Elena.Budnik   small cleanning up
427
428
			AmdaAction.execute({nodeType : this.get('nodeType')}, jsonObj, function(res,e)
			{       
6bfa699d   Elena.Budnik   Statistics -> Stats
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
				loadMask.hide();            
				//AKKA - Rework of the result treatment for the integration with the new kernel
				if (!e.status)
				{
					myDesktopApp.errorMsg('Internal error during request');
					return;
				}     	   
				if (!res.success)
				{
					myDesktopApp.errorMsg(res.message);
					return;
				}            
				// NO background jobs for PNG !!!!! Timeout KILL
				if (res.status == amdaModel.BkgJobNode.STATUS_LIST.DONE) 
				{
					this.updateNode(res);	  		              		 
					this.updateObject(res);
					this.editNode(isNewTab, isInteractive);
				}
				else 
				{
					myDesktopApp.warningMsg(res.message);                    
					// keep this for case of Background Job
					/* var id = res.id;
						var text = 'job_' + res.pid;
						var status = amdaModel.BkgJobNode.STATUS_LIST.IN_PROGRESS; 
						var newobj = Ext.create('amdaModel.Plot',  
										{ resultId : id, name : res.rawname, resultId: res.rawname});
										var newNode = Ext.create(this.$className, { id : id, pid : res.pid, text : text, jobType : 'request',
																	leaf : true, status : status, rawname : res.rawname, object : newobj});
						newNode.createJobNode(false);
					*/
				}
			}, this);
		},
16035364   Benjamin Renard   First commit
464
        
6bfa699d   Elena.Budnik   Statistics -> Stats
465
466
467
468
469
470
471
472
473
474
475
476
477
		updateNode : function(res) 
		{    
			var windowId = 'plot' + this.get('tabId')+'-win';		  
			var win = myDesktopApp.getDesktop().getWindow(windowId); 
			//TODO if it is possible to close window before getting result?	    
			if (!win) 
				myDesktopApp.errorMsg('You have closed window!!!');
			else 
			{
				var panelResult = win.items.items[0];
				panelResult.setObjectIntoNode();
			}
		},
16035364   Benjamin Renard   First commit
478
       
6bfa699d   Elena.Budnik   Statistics -> Stats
479
480
481
482
483
484
485
		updateObject : function(res) 
		{
			var object =  this.get('object');
			object.set('outputName', res.name);
			object.set('resultId', res.result);
			object.set('startDate', res.startDate);
			object.set('stopDate', res.stopDate);
16035364   Benjamin Renard   First commit
486

6bfa699d   Elena.Budnik   Statistics -> Stats
487
488
489
490
491
492
493
			if (object.get('timesrc') == amdaModel.AmdaTimeObject.inputTimeSrc[0] && this.action != 'zoom') 
			{
				object.set('intervalN', res.intervalN); 
				object.set('totalN', res.totalN);
				object.set('ttName', res.tableName); 
			}  
		}    
16035364   Benjamin Renard   First commit
494
});