StatisticalPlug.js 6.48 KB
/**
  * Project      :  AMDA-NG
  * Name         : statisticalPlug.js
  * @plugin 	  amdaUI.StatisticalPlug
  * @extends 	  Ext.util.Observable 
  * @ptype 	 	  statisticalPlugin
  * @brief		  Statistical info in Time Table Module UI (View)
  * @author Myriam
  * @version $Id: StatisticalPlug.js 1237 2013-02-12 15:42:14Z myriam $
  ********************************************************************************
  *    FT Id     :   Date   : Name - Description
  *******************************************************************************
  *  :           :08/06/2011: Myriam - Migration extjs4
  */


Ext.define('amdaUI.StatisticalPlug', {
	extend: 'Ext.util.Observable',
	alias: 'plugin.statisticalPlugin',
	
	win : null,
		
 	constructor: function(config) { 
        Ext.apply(this, config);  
        this.callParent(arguments);
	},
 	
	init: function(cmp) {
	    this.hostCmp = cmp;    
	    this.hostCmp.on({
		info: this.onInfo,
		refresh: this.refresh,		 
		scope: this});
	},
	
	onDestroy : function() {
	        this.win = null;   
	},
	
	refresh: function() {
		this.statTT();
		/*if (this.form) {
			this.form.getForm().setValues({min: this.min, max: this.max, mean:this.mean, stdev: this.stdev,
				median: this.median, density: this.density });
		}*/
	},

	onInfo: function(type) {
	        if (!this.win) {	   
		    this.win = new Ext.Window({			
			    id: 'statistical-win',
			    width: 370, 
			    height: 140,
			    x: 420, y: 330,
			    baseCls:'x-panel',	 
			    title: 'Statistical info',
			    layout: 'fit',
			    constrain: true,
			    ghost: false,
			    renderTo: this.hostCmp.id,
				tools: [
				         {
				        	type:'help',
				        	qtip: 'Help on Statistical info',
				        	handler: function(event, toolEl, panel){
                                                                                                                        
				        		AmdaAction.getInfo({name : 'statisticalHelp'+type}, function(res,e) {					    					   
								    if (res.success) myDesktopApp.infoMsg(res.result);
							 });  
				        	}
				         }
					],
			    items: this.getFormConfig(),
			    getConstrainVector: function(constrainTo){
					var me = this;
			        if (me.constrain || me.constrainHeader) {
			            constrainTo = constrainTo || (me.floatParent && me.floatParent.getTargetEl()) || me.container || me.el.getScopeParent();
			            return (me.constrainHeader ? me.header.el : me.el).getConstrainVector(constrainTo);
			        }
			    }
		    });
		    this.win.on('destroy', this.onDestroy, this);
		    this.win.show();
		}
	//    this.hostCmp.add(this.win);	    
	    this.refresh();
	},
	
	/**
	 * calculation method of statical values 
	 */
	statTT: function(){
	    if (this.win) {
                              var type =this.hostCmp[0]['id'];
	    	var me = this;
	    	AmdaAction.getTTCacheStatistics({name : type }, function (result, e) {
	    		if (!result || !result.success)
	    		{
	    			if (result.message)
	        			myDesktopApp.errorMsg(result.message);
	        		else
	        			myDesktopApp.errorMsg('Unknown error during statistics calculation');
	        		return;
	    		}
	    		
	    		// format min value
	    		me.min = me.format_unit(result.result.minDuration) + " (-- " + result.result.minDurationIndex + ")";
	    	    // format max value
	    		me.max = me.format_unit(result.result.maxDuration) + " (-- " + result.result.maxDurationIndex + ")";    	    
	    	    // format mean value
	    	    me.mean = me.format_unit(result.result.mean);
	    	    // format Stdev value
	    	    me.stdev = me.format_unit(result.result.stdev);
	    	    // format median value
	    	    me.median = me.format_unit(result.result.median);
	    	    // format density value
	    	    me.density = (result.result.density*100).toFixed(2) + " %";
	    	    
	    	    if (me.form) {
	    	    	me.form.getForm().setValues({min: me.min, max: me.max, mean:me.mean, stdev: me.stdev,
	    				median: me.median, density: me.density });
	    		}
	    	});
	    }
	},
	
	format_unit: function(unit) {
		if ( unit < 60 ) var string_unit = unit.toFixed(2) + " sec";
	    else if ((unit >= 60) && ( unit < 3600)) var string_unit = (unit/60).toFixed(2) + " min";
	    else if ((unit >= 3600) && ( unit < 86400)) var string_unit = (unit/3600).toFixed(2) + " hour";
	    else if ( unit >= 86400 ) var string_unit = (unit/86400).toFixed(2) + " day";
	    return string_unit;
	},	
	
	getFormConfig: function(){
	    this.form = new Ext.form.FormPanel( {
	    	frame: true, buttonAlign: 'center', flex: 1,
	    	fieldDefaults: {
                labelWidth: 50
            },
            items: [{
                xtype : 'container',
                layout:'anchor',
                items: [	
      	          {			            	        	        	  
      	        	  xtype: 'fieldcontainer',
      	        	  layout: 'hbox',
      	        	  fieldLabel:'Min',
      	        	  items: [
  	        	          {xtype:'textfield', name:'min', value: this.min, width: 100},
  	        	          {xtype:'splitter', width: 20},
  	        	          {xtype:'displayfield', value: 'Max:', width: 40},
  	        	          {xtype:'splitter', width: 13},
  	        	          {xtype:'textfield', name:'max', value: this.max, width: 100}
      	        	  ]
      	          },
      	          {			            	        	        	  
      	        	  xtype: 'fieldcontainer',
      	        	  layout: 'hbox',
      	        	  fieldLabel:'Mean',
      	        	  items: [
  	        	          {xtype:'textfield', name: 'mean', value: this.mean, width: 100},
  	        	          {xtype:'splitter', width: 20},
  	        	          {xtype:'displayfield', value: 'St.dev:', width: 50},
  	        	          {xtype:'splitter', width: 2},
  	        	          {xtype:'textfield', name: 'stdev', value: this.stdev, width: 100}
      	        	  ]
      	          },	        	          
      	          {			            	        	        	  
      	        	  xtype: 'fieldcontainer',
      	        	  layout: 'hbox',
      	        	  fieldLabel:'Median',
      	        	  items: [
  	        	          {xtype:'textfield', name: 'median', value: this.median, width: 100},
  	        	          {xtype:'splitter', width: 20},
  	        	          {xtype:'displayfield', value: 'Density:', width: 52},
  	        	          {xtype:'textfield', name: 'density', value: this.density, width: 100}
      	        	  ]
      	          }
      	          
      	         ]
            }]
	    });
	    return this.form;
	}    
		
});