~autopilot/+junk/apview

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
$(document).ready(function() {
    var JSON_API_STR = "/api/json?jsonp=?";
        
    $('form#config').submit(function(event){
        event.preventDefault();
        var details = {};
        $.each($(this).serializeArray(), function(i, field) {
            details[field.name] = field.value;
        });
        console.log("Grabbing config for: %s, job: %s",
                    details['server_url'],
                    details['job_name']);
        display_build_menu(details['server_url'], details['job_name']);
    });
    
    /* Menu control for the requested Job */
    function display_build_menu(server_url, job_path) {
        $('#menu_pane ul li').remove();
        
        var menu_loading_status = $("#menu_pane span.status");
        menu_loading_status.text("Loading").show();

        $.ajax({
            type: 'GET',
            dataType: 'json',
            crossDomain: true,
            url: get_build_list_url(),
            success: function(data){
                build_menu(data);
            },
            error: function(request, textStatus, errorThrown) {
                console.log("ERROR: %s - %s", textStatus, errorThrown);
            },
            complete: function(request, textStatus) {
                menu_loading_status.hide();
            }

        });
        
        
        function get_build_list_url() {
            return server_url + '/job/' + job_path + JSON_API_STR;
        }

        function new_menu_link(details) {
            return $('<li><a href="'+details['url']+'">'+details['number']+'</a></li>');
        }

        function build_menu(data) {
            var menu_list = $('#menu_pane ul');
            var last_complete_build = parseInt(data['lastCompletedBuild']['number']);
            
            $.each(data['builds'], function(i, element) {
                if(parseInt(element['number']) <= last_complete_build) {
                    var link = new_menu_link(element);
                    link.click(function(event) {
                        $("#detail_status").fadeIn();
                        event.preventDefault();
                        var build_url =  $('a', $(this)).attr('href');
                        populate_test_details(build_url);
                        return false;
                    });
                    link.appendTo(menu_list);
                }
            });
        }
    }

    /* Control for the builds details */
    function populate_test_details(url) {
        /* Actual actions */
        $('#details ul li').remove();
        $("#details .error_message").remove();
        
        $.ajax({
            type: 'GET',
            dataType: 'json',
            crossDomain: true,
            url: get_testreport_url(),
            
            success: function(data) {
                $("#detail_status").fadeOut();
                if(data['childReports'].length == 0) {
                    console.log("No data returned");
                    $("<div class='error_message'>No Data was returned. Perhaps this build was cancelled?</div>").appendTo("#details");
                }
                else {
                    var testreport_data = collate_testreport_data(data);
                    refresh_details_view(testreport_data);
                }
            }
        });

        /* Support methods */
        function get_testreport_url() {
            return url+'/testReport'+JSON_API_STR;
        }

        function add_detail(details) {
            var test_details = details['details'];
            var nodes = details['nodes'];

            var tabs_html = '';
            var tab_containers_html = '';
            for(var i=0; i<nodes.length; ++i) {
                var name = nodes[i]['name'];
                var url = nodes[i]['url'];
                var tab_id = name+'-'+test_details['testName'];
                tabs_html += '<li class="'+name+'"><a href="#'+tab_id+'">'+name+'</a></li>';
                tab_containers_html += '<div id="'+tab_id+'"><span class="data">'+url+'</span></div>';
            }

            var li = '<li>'
                +'<div class="accordion_header closed">'+details['name']+'</div>'
                +'<div class="details_wrapper">'
                +'<span class="data moduleName">'+test_details['moduleName']+'</span>'
                +'<span class="data className">'+test_details['className']+'</span>'
                +'<span class="data testName">'+test_details['testName']+'</span>'
                +'<ul class="result_detail_tabs">'+tabs_html+'</ul>'
                +tab_containers_html
                +'</li>';
            return li;
        }

        function refresh_details_view(testreports) {
            // var start = new Date().getTime();
            // console.log('j');

            var sorted_keys = get_sorted_keys(testreports);

            var coll = '';
            for(var i=0; i<sorted_keys.length; ++i) {
                coll += add_detail(testreports[sorted_keys[i]]);
            }

            $('#details ul').append(coll);
            add_event_handlers();

            // console.log('D: %s', new Date().getTime() - start);
            
            function add_event_handlers() {
                $(".details_wrapper").tabs({selected: null});
                
                // Show the first nodes log when opening
                $('#details > ul li').toggle(function(event) {
                    $('div.details_wrapper', $(this)).show();
                    $('.details_wrapper', $(this)).tabs('select', 0);
                    $('.accordion_header', $(this))
                        .removeClass('closed')
                        .addClass('open');
                }, function(event) {
                    $('div.details_wrapper', $(this)).hide();
                    $('.accordion_header', $(this))
                        .removeClass('open')
                        .addClass('closed');
                });

                // if the details haven't been loaded then we need to load them
                $('.details_wrapper').bind('tabsselect', function(event, ui) {
                    var panel = $(ui.panel);
                    
                    var test_result_url = $('span.data', panel).text();
                    var wrapper = panel.parents('div.details_wrapper');
                    var test_details = {
                        className: $('span.className', wrapper).text(),
                        moduleName: $('span.moduleName', wrapper).text(),
                        testName: $('span.testName', wrapper).text()
                    };
                                        
                    // Only update the log details if they haven't been pulled
                    // in yet, always update the video url
                    if(! panel.hasClass('data_present')) {
                        var loading_text = $('<b>Loading . . .</b>').appendTo(panel);
                        $.ajax({
                            type: 'GET',
                            dataType: 'json',
                            crossDomain: true,
                            url: get_test_details_url(test_result_url,
                                                  test_details),
                            success: function(data) {
                                loading_text.remove();
                                panel.append('<pre>'
                                             +data['errorStackTrace']
                                             +'</pre>');
                                panel.addClass('data_present');
                            }
                        });
                    }
                    update_test_video(test_result_url, test_details);
                });
                
                function construct_full_test_path(details, separator) {
                    return [details['moduleName'],
                            details['className'],
                            details['testName']].join(separator);
                }
                
                function get_test_details_url(node_url, test_details) {
                    // var class_name = $('span.moduleName', test_details).text();
                    // var module_name = $('span.className', test_details).text();
                    // var test_name = $('span.testName', test_details).text();
                    var details_url = node_url+'/'+'testReport/'
                        +construct_full_test_path(test_details, '/')
                        +JSON_API_STR;
                    details_url = details_url.replace(/[\(\)\ ]/mg, '_');
                    return details_url;
                }

                function get_video_url(node_url, test_details){
                    var video_url = node_url+'/artifact/artifacts/'
                        +construct_full_test_path(test_details, '.')
                        +'.ogv';
                    video_url = video_url.replace(/\(/mg, '%20%28')
                        .replace(/\)/mg, '%29');
                    return video_url;
                }

                function update_test_video(node_url, test_details) {
                    var video_url = get_video_url(node_url, test_details);
                    $('#video_player').attr('src', video_url);
                    var player = $('#video_player').get(0);
                    player.load();
                    player.play();
                    $('a#video_url').attr('href', video_url);
                }
            }

            function get_sorted_keys(data) {
                var sorted = [];
                for(var key in data) {
                    if(data.hasOwnProperty(key)) {
                        sorted.push(key);
                    }
                }
                sorted.sort();
                return sorted;
            }
        }

        function collate_testreport_data(raw_data) {
            var data_collection = {};
            for(var i=0; i<raw_data['childReports'].length; ++i) {
                var build_report = raw_data['childReports'][i]['child'];
                var config_results = raw_data['childReports'][i]['result']['suites'][0]['cases'];
                // each result object has: age, className, name, skipped, status

                var label_re = /\/\.\/([\w|-]+)=([\w|-]+)\//;
                var match = label_re.exec(build_report['url']);
                var label = match[2];

                for(var r=0; r < config_results.length; ++r) {
                    if(config_results[r]['status'] != 'PASSED'
                       && config_results[r]['status'] != 'FIXED') {
                        append_config_result_details(config_results[r],
                                                     build_report,
                                                     label);
                    }
                }
            }
            
            return data_collection;

            // Append multiple configuration results for a test
            function append_config_result_details(results, build_report, label) {
                var key = results['className']+'.'+results['name'];
                if(!data_collection[key]) {
                    var name_parts = results['className'].split('.');
                    var class_name = name_parts.pop();
                    var module_name = name_parts.join('.');
                    data_collection[key] = {
                        name: key,
                        details: {
                            moduleName: module_name,
                            className: class_name,
                            testName: results['name']
                        },
                        nodes: []
                    };
                }
                data_collection[key]['nodes'].push({name: label,
                                                    url: build_report['url']});
            }
        }
    }
});