Blame view

js/app/models/BkgJobNode.js 17.3 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
12
13
14
 */

Ext.define('amdaModel.BkgJobNode', {

    extend: 'amdaModel.ExecutableNode',
    
c00f7d82   Benjamin Renard   Add another missi...
15
16
17
18
    requires: [
        'amdaPlotObj.PlotRequestObject'
    ],
    
16035364   Benjamin Renard   First commit
19
20
21
22
23
24
25
26
    action : null,
    
    statics: {       	 	 
        nodeType: 'bkgWorks',
        JOB_TYPES: {
            PLOT: 'request',//'plot',
            CONDITION: 'condition',
            DOWNLOAD: 'download',
d18b535d   elena   catalog draft + c...
27
            STATISTICS: 'statistics'
16035364   Benjamin Renard   First commit
28
29
30
31
32
        },
        JOB_ROOT_NODE: {
            PLOT: 'bkgPlot-treeRootNode',
            CONDITION: 'bkgSearch-treeRootNode',
            DOWNLOAD: 'bkgDown-treeRootNode',
d18b535d   elena   catalog draft + c...
33
            STATISTICS: 'bkgStatistics-treeRootNode'
16035364   Benjamin Renard   First commit
34
35
36
37
        },
	RES_ROOT_NODE: {
            PLOT: 'resPlot-treeRootNode',
            CONDITION: 'resSearch-treeRootNode',
3a5f60a1   elena   proper use os sta...
38
            DOWNLOAD:  'resDown-treeRootNode',
d18b535d   elena   catalog draft + c...
39
            STATISTICS: 'resStatistics-treeRootNode'
16035364   Benjamin Renard   First commit
40
41
42
43
44
45
46
47
48
49
        },
        STATUS_LIST: {
            IN_PROGRESS: 'in_progress',
            DONE: 'done',
            ERROR: 'error' 
        }	                     
    },
    fields: [ 
//         name, id, moduleId, object> from parent 
	{ name : 'jobType', type : 'string' },
3a5f60a1   elena   proper use os sta...
50
51
	{ name : 'processId', type : 'string' },
	{ name : 'status', type : 'string' },
16035364   Benjamin Renard   First commit
52
	{ name : 'percentDone', type : 'string', defaultValue : '0' },
3a5f60a1   elena   proper use os sta...
53
54
55
	{ name : 'start', type: 'date' },
	// { name : 'duration', type: 'int' , defaultValue: -1}
	{ name : 'stop', type: 'date', defaultValue: null },
16035364   Benjamin Renard   First commit
56
57
58
	{ name : 'rawname', type: 'string'},
	{ name : 'tabId', type: 'string'},
	{ name : 'info', type: 'string'}
16035364   Benjamin Renard   First commit
59
60
61
62
63
64
    ],

    constructor : function(config){
  
        this.callParent(arguments);
        this.set('ownerTreeId', amdaUI.ExplorerUI.JOB_TAB.TREE_ID);   
3a5f60a1   elena   proper use os sta...
65
      
16035364   Benjamin Renard   First commit
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
        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
                //       this.set('status',amdaModel.BkgJobNode.STATUS_LIST.DONE);              
                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');			      
            }
            if (config) this.set('jobType', config.jobType);
	    else this.set('jobType',this.get('jobType'));
        }     
    },
  
    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' 
            }*/];		
    },

    getMultiContextMenuItems: function(){
        return [{
            fnId : 'mult-deleteMulti',
            text : 'Delete Results'
        }];
    },
        
    onMenuItemClick : function(menu, item, event) {
        var fnId = Ext.util.Format.substr(item.fnId, 5, item.fnId.length);
            
        switch (fnId) {
           
            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; 
                
	    case 'editNode': 
//create object if it doesn't exist (tree load)	  
//TODO use data model?  
                if (!this.get('object')) {
                        AmdaAction.getObject(this.get('id'), this.get('nodeType'), this.getObjectCallback, this);		  
                }
                else {
                        var isInteractive = false;
                        var isNewTab = true;	  
                        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;
            }   
        },
        
    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;
                    
e511f2cc   elena   stat node
177
178
179
180
181
182
183
184
			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';    
			}
			
16035364   Benjamin Renard   First commit
185
186
187
188
189
                    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
190
		//delete fieldset from Result Module if it is open       
16035364   Benjamin Renard   First commit
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
                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);                      
                      }
                  });
                  
                  
                } 
    },	
    
   getObjectCallback : function(result,remoteEvent){//result, e) {
        var t = remoteEvent.getTransaction();
        
        //AKKA - Rework of the result treatment for the integration with the new kernel
 	   	if (!remoteEvent.status)
 	   	{
 	   		myDesktopApp.errorMsg('Internal error during download request');
 	   		return;
 	   	}
 	   	
 	    if (!result.success)
	    {
 	    	myDesktopApp.errorMsg(result.message);
 	    	return;
	    }
        
        var obj = null;
        switch (this.get('jobType'))
        {
d18b535d   elena   catalog draft + c...
227
		    case 'condition' : 
16035364   Benjamin Renard   First commit
228
229
230
231
232
				obj = Ext.create('amdaModel.Search',
		    			 {name: result.name,
	    	   		  	resultId : result.result,
	    	   		  	folderId : result.folder});
		    	break;
d18b535d   elena   catalog draft + c...
233
234
235
236
237
238
		    case 'statistics' : 
				obj = Ext.create('amdaModel.Statistics',
		    			 {name: result.name,
	    	   		  	resultId : result.result,
	    	   		  	folderId : result.folder});
		    	break;
16035364   Benjamin Renard   First commit
239
		    case 'request' : 
18d4a11e   Benjamin Renard   Save and load plo...
240
		    	obj = Ext.create('amdaPlotObj.PlotRequestObject.',
16035364   Benjamin Renard   First commit
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
		    			 {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;
		}
        	
        if (!obj)
        {
        	myDesktopApp.errorMsg("Unknown job type");
        	return;
        }	
        
        // set parameter into node
        this.set('object',obj);
        
	    var isInteractive = false;
	    var isNewTab = true;	 
	    this.editNode(isNewTab, isInteractive);	   
    },
 
// Show Result 
    editNode : function(isNewTab, isInteractive) {
      var me = this;
      if (isInteractive) {
    	  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) {
    		  if (isNewTab) {	 
    			  module.setLinkedNode(me); 
    		  }	
    		  module.createWindow();
    	  });
      }
    },
  
    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
        }
        
        var rootNode = this.getRootNode(isResult);
        // expand the corresponding rootNode
        if (rootNode.parentNode) {
           if (!rootNode.parentNode.isExpanded()) {
               rootNode.parentNode.expand(false);
           }
        }  
        
        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);
        }                                                                                                   
                
        // 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;
ae7dc148   elena   added stat node
328
	              case 'statistics' : var type = 'Statistics '; break;
16035364   Benjamin Renard   First commit
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
		       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);
            });
	    
            amdaDesktop.JobsMgr.jobsInProgress++;          	 
            if (!amdaDesktop.JobsMgr.updateStatus)
            {
                amdaDesktop.JobsMgr.updateStatus =  
                    Ext.TaskManager.start({
                        run : amdaDesktop.JobsMgr.getStatus,
                        interval : amdaDesktop.JobsMgr.interval 
                    });
            }
	} 		
    },
    
    //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;
d18b535d   elena   catalog draft + c...
364
365
		    case amdaModel.BkgJobNode.JOB_TYPES.STATISTICS:                   
			rootNodeId = amdaModel.BkgJobNode.JOB_ROOT_NODE.STATISTICS;
16035364   Benjamin Renard   First commit
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
			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;
d18b535d   elena   catalog draft + c...
382
383
		    case amdaModel.BkgJobNode.JOB_TYPES.STATISTICS:                   
			rootNodeId = amdaModel.BkgJobNode.RES_ROOT_NODE.STATISTICS;
16035364   Benjamin Renard   First commit
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
			break;
		    default:
			break;
		}
	    } 
            this.set('rootNode',this.myGetOwnerTree().getRootNode().findChild( 'id', rootNodeId, true));
        }
        return this.get('rootNode');
    },
    
    /**
     * @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');
	
	this.action = jsonObj.action.name;
	
     //   this.set('tabId', jsonObj.tabId);
	
// Node exists already, interactive Session 
	var isInteractive = true;
	var isNewTab = false; 
        
        loadMask.show(this.get('object').get('tabId'));
        AmdaAction.execute({nodeType : this.get('nodeType')}, jsonObj, function(res,e){
        
            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 Backgrounf 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);

    },
        
    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();
                }
    },
       
    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);

                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); 
                }  
    }
    
});