summaryrefslogtreecommitdiffstats
path: root/qooxdoo/source/class/Tr/ui/TraceTable.js
blob: 446cbad9ffe8b869092a2ad2f027f5629bc6946a (plain)
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
/* ************************************************************************
#module(Tr)
************************************************************************ */

/**
 * a widget showing the Tr target tree
 */

qx.Class.define('Tr.ui.TraceTable', 
{
    extend: qx.ui.table.Table,        

    /*
    *****************************************************************************
       CONSTRUCTOR
    *****************************************************************************
    */


    construct: function () {
       
        var tableModel = new qx.ui.table.model.Simple();
        this.__tableModel = tableModel;
        tableModel.setColumns([ this.tr("Hop"), this.tr("Host"),this.tr("Ip"), 
                                this.tr("Loss [%]"), this.tr("Sent"), this.tr("Last [ms]"), //"; help syntax highliter
                                this.tr("Avg [ms]"), this.tr("Best [ms]"), this.tr("Worst [ms]"), this.tr("StDev [ms]") ]);
        var custom = {
            tableColumnModel:  function(obj) {
                return new qx.ui.table.columnmodel.Resize(obj);
            }
        };
        with(this){
			base(arguments,tableModel,custom);
            set({
                width: '100%',
                height: '1*',
                border: 'dark-shadow',
                showCellFocusIndicator: false,
                statusBarVisible: false
			});
        };
      	var tcm = this.getTableColumnModel();
        this.__tcm = tcm;

        tcm.setDataCellRenderer(0, new Tr.ui.Cellrenderer(1));
        tcm.setDataCellRenderer(3, new Tr.ui.Cellrenderer(0,' %'));
        tcm.setDataCellRenderer(4, new Tr.ui.Cellrenderer(0));
        
        var render_ms = new Tr.ui.Cellrenderer(1);

        for (var i=5;i<10;i++){
            tcm.setDataCellRenderer(i, render_ms);
        }
        

        // Obtain the behavior object to manipulate
        var resizeBehavior = tcm.getBehavior();
        // This uses the set() method to set all attriutes at once; uses flex
        resizeBehavior.set(0, { width:"2*"});
        resizeBehavior.set(1, { width:"9*"});
        resizeBehavior.set(2, { width:"5*"});

        for (var i=3;i<10;i++){
            resizeBehavior.set(i, { width:"3*"});
        }
        qx.event.message.Bus.subscribe('tr.cmd',this.__handle_tr,this);
    },

    /*
    *****************************************************************************
     Statics
    *****************************************************************************
    */
	members: {
        __make_empty_row: function (){            
            return ([undefined,undefined,undefined,0,0,undefined,undefined,undefined,undefined,undefined,0,0,0]);
        },
        __stop_table: function (){
            var tableModel = this.__tableModel;
            for (var i=0;i<10;i++){
                tableModel.setColumnSortable(i,true);
            }
            qx.event.message.Bus.dispatch('tr.status','stopped');
            this.__handle = undefined;
        },
        __handle_tr: function(m){
            var self = this;
            var f_hop = 0,f_host=1,f_ip=2,f_loss=3,f_snt=4,f_last=5,f_avg=6,f_best=7,f_worst=8,f_stdev=9,f_cnt=10,f_sum=11,f_sqsum=12;
            var fill_table;
            fill_table = function(retval,exc,id){
                if (exc == null){ 
                    if ( self.__handle == undefined ) {
                        qx.event.message.Bus.dispatch('tr.status','started');
                    }
                    self.__handle = retval['handle'];
                    var tableModel = self.__tableModel;
                    var lines = retval['output'].length;
                    var data = self.__data;
                    var sleep = 0;
                    for(var i=0;i<lines;i++){
                        sleep = 0;
                        var hop = retval['output'][i][0];
                        if (hop == 'SLEEP'){
                            sleep = retval['output'][i][1];
                            continue;                            
                        } 
                        else if (hop == 'INFO'){
                            qx.event.message.Bus.dispatch('tr.info',retval['output'][i][1]);
                            continue;
                        }                        
                        var host = retval['output'][i][1];
                        var ip = retval['output'][i][2];
                        var value = retval['output'][i][3];
                        var ii = 0;
                        var max = data.length;
                        while (true){
                            if ( ii == max ) break;
                            if ( Math.floor(data[ii][0]) > hop) break;
                            if ( Math.floor(data[ii][0]) == hop ){
                                if ( ip == undefined ) break;
                                if ( ip == data[ii][2] ) break;
                            }
                            ii++;
                        }
                        if (ii == max || Math.floor(data[ii][0]) > hop ){
                            if (ii > 0 &&  Math.floor(data[ii-1][0]) == hop ){
                                hop = data[ii-1][0] + 0.1;                                
                            }
                            data.splice(ii,0,self.__make_empty_row());
                            data[ii][0] = hop;
                        }

                        var drow = data[ii];
                        if (drow[f_host] == undefined && host != undefined){
                            drow[f_host] = host;
                        }
                        if (drow[f_ip] == undefined && ip != undefined){
                            drow[f_ip] = ip;
                        }
                        drow[f_snt]++;
                        drow[f_last] = value;
                                

                        if (value != undefined){
                            var best = drow[f_best];
                            if (best == undefined || best > value){
                                drow[f_best] = value;     
                           }
                            var worst = drow[f_worst]; 
                            if (worst == undefined || worst < value){
                                drow[f_worst] = value;     
                            }
                            drow[f_sum] += value;                            
                            var sum = drow[f_sum];
                            drow[f_cnt] ++;
                            var cnt = drow[f_cnt];
                            var sqsum =  drow[f_sqsum]+value*value;
                            drow[f_sqsum] = sqsum;    
                            drow[f_avg] = drow[f_sum]/drow[f_cnt];
                            drow[f_stdev] = Math.sqrt((cnt*sqsum-sum*sum)/(cnt*(cnt-1)))
                        }
                        drow[f_loss] = ((drow[f_snt]-drow[f_cnt])/drow[f_snt])*100;
                    } 

                    tableModel.setData(data);
                    if (retval['again']){
                        var next_round = function (){Tr.Server.getInstance().callAsync(
                                                     fill_table,'run_tr',{ handle: retval['handle'],
                                                                            point:  retval['point']})};
                        qx.client.Timer.once(next_round,self,sleep*1000);
                    }
                    else {
                        self.__stop_table();
                    }
                }
                else {
                    alert(exc);   
                    self.__stop_table();
                }				
            };

            var stop_handler = function (data,exc,id){
                if (exc == null){
                    qx.event.message.Bus.dispatch('tr.status','stopped');
                }
                else {
                   alert(exc);
                }               
            };

            var cmd = m.getData();
            switch(cmd['action']){
            case 'stop':
                qx.event.message.Bus.dispatch('tr.status','stopping');
                Tr.Server.getInstance().callAsync(stop_handler,'stop_tr',this.__handle);
                break;
            case 'go':
                this.__data = [];
                this.__tableModel.setData(this.__data);
                this.__delay = cmd['delay'];
                for (var i=0;i<10;i++){
                    this.__tableModel.setColumnSortable(i,false);
                }                
                qx.event.message.Bus.dispatch('tr.status','starting');
                Tr.Server.getInstance().callAsync(fill_table,'run_tr',{host: cmd['host'], rounds: cmd['rounds'], delay: cmd['delay']});
                break;
            default:
                alert('Unknown Command '+cmd['action']);
            }
        }
	}
});