1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @author: Brett Slatkin (bslatkin@google.com)
19 */
20
21
22function initRootList() {
23  setButter('Loading root jobs...');
24  $.ajax({
25    type: 'GET',
26    url: 'rpc/list' + window.location.search,
27    dataType: 'text',
28    error: function(request, textStatus) {
29      getResponseDataJson(textStatus);
30    },
31    success: function(data, textStatus, request) {
32      var response = getResponseDataJson(null, data);
33      if (response) {
34        clearButter();
35        initRootListDone(response);
36      }
37    }
38  });
39}
40
41
42function initRootListDone(response) {
43  if (response.pipelines && response.pipelines.length > 0) {
44    $('#root-list').show();
45    if (response.cursor) {
46      // Prepend the cursor to the next link. This may have a suffix of
47      // the class_path from initRootNamesDone() below.
48      var href = $('#next-link').attr('href');
49      $('#next-link').attr('href', '?cursor=' + response.cursor + href);
50      $('#next-link').show();
51    }
52
53    $.each(response.pipelines, function(index, infoMap) {
54      var row = $('<tr>');
55      $('<td class="class-path">').text(infoMap.classPath).appendTo(row);
56      $('<td class="status">').text(infoMap.status).appendTo(row);
57
58      if (infoMap.startTimeMs) {
59        var sinceSpan = $('<abbr class="timeago">');
60        var isoDate = getIso8601String(infoMap.startTimeMs);
61        sinceSpan.attr('title', isoDate);
62        sinceSpan.text(isoDate);
63        sinceSpan.timeago();
64        $('<td class="start-time">').append(sinceSpan).appendTo(row);
65      } else {
66        $('<td class="start-time">').text('-').appendTo(row);
67      }
68
69      if (infoMap.endTimeMs) {
70        $('<td class="run-time">').text(getElapsedTimeString(
71            infoMap.startTimeMs, infoMap.endTimeMs)).appendTo(row);
72      } else {
73        $('<td class="run-time">').text('-').appendTo(row);
74      }
75
76      $('<td class="links">')
77          .append(
78            $('<a>')
79                .attr('href', 'status?root=' + infoMap.pipelineId)
80                .text(infoMap.pipelineId))
81          .appendTo(row);
82      $('#root-list>tbody').append(row);
83    });
84  } else {
85    $('#empty-list-message').text('No pipelines found.').show();
86  }
87
88  initRootNames();
89}
90
91
92function initRootNames() {
93  setButter('Loading names...');
94  $.ajax({
95    type: 'GET',
96    url: 'rpc/class_paths',
97    dataType: 'text',
98    error: function(request, textStatus) {
99      getResponseDataJson(textStatus);
100    },
101    success: function(data, textStatus, request) {
102      var response = getResponseDataJson(null, data);
103      if (response) {
104        clearButter();
105        initRootNamesDone(response);
106      }
107    }
108  });
109}
110
111
112function initRootNamesDone(response) {
113  if (response.classPaths) {
114    var filterMenu = $('#filter_menu');
115
116    $.each(response.classPaths, function(index, path) {
117      // Ignore internal pipelines.
118      if (path.match(/\.?pipeline\./)) {
119        return;
120      }
121
122      var option = $('<option>').val(path).text(path);
123      if (window.location.search.indexOf(path) != -1) {
124        option.attr('selected', 'selected');
125        // Append the class name selected to the "next page" link. This
126        // may already have a value from initRootListDone() above.
127        var href = $('#next-link').attr('href');
128        $('#next-link').attr('href', href + '&class_path=' + path);
129      }
130      option.appendTo(filterMenu);
131    });
132  }
133}
134