7884: Detect when a "download log" response has a JSON-encoded redirect
[arvados.git] / apps / workbench / app / views / jobs / _show_log.html.erb
1 <% if !@object.log %>
2
3 <div id="log_graph_div"
4      class="arv-log-event-listener"
5      style="display:none"
6      data-object-uuid="<%= @object.uuid %>"></div>
7
8 <div id="event_log_div"
9      class="arv-log-event-listener arv-log-event-handler-append-logs arv-job-log-window"
10      data-object-uuid="<%= @object.uuid %>"
11      ></div>
12
13 <%# Applying a long throttle suppresses the auto-refresh of this
14     partial that would normally be triggered by arv-log-event. %>
15 <div class="arv-log-refresh-control"
16      data-load-throttle="86486400000" <%# 1001 nights %>
17      ></div>
18
19 <% else %>
20
21 <script>
22 (function() {
23 var pagesize = 1000;
24 var logViewer = new List('log-viewer', {
25   valueNames: [ 'id', 'timestamp', 'taskid', 'message', 'type'],
26   page: pagesize
27 });
28
29 logViewer.page_offset = 0;
30 logViewer.on("updated", function() { updatePaging(".log-viewer-paging", logViewer, pagesize) } );
31 $(".log-viewer-page-up").on("click", function() { prevPage(logViewer, pagesize, ".log-viewer-paging"); return false; });
32 $(".log-viewer-page-down").on("click", function() { nextPage(logViewer, pagesize, ".log-viewer-paging"); return false; });
33
34 var taskState = newTaskState();
35
36 var makeFilter = function() {
37   var pass = [];
38   $(".toggle-filter, .radio-filter").each(function(i, e) {
39     if (e.checked) {
40       pass.push(e.id.substr(5));
41     }
42   });
43
44   return (function(item) {
45     var v = false;
46     if (item.values().taskid !== "") {
47       for (a in pass) {
48         if (pass[a] == "all-tasks") { v = true; }
49         else if (pass[a] == "successful-tasks" && taskState[item.values().taskid].outcome == "success") { v = true; }
50         else if (pass[a] == "failed-tasks" && taskState[item.values().taskid].outcome == "failure") { v = true; }
51       }
52     } else {
53       v = true;
54     }
55     for (a in pass) {
56       if (pass[a] == item.values().type) { return v; }
57     }
58     return false;
59   });
60 }
61
62 <% if @object.log and !@object.log.empty? %>
63   <% logcollection = Collection.find @object.log %>
64   <% if logcollection %>
65     var log_size = <%= logcollection.files[0][2] %>
66     var log_maxbytes = <%= Rails.configuration.log_viewer_max_bytes %>;
67     var logcollection_url = '<%=j url_for logcollection %>/<%=j logcollection.files[0][1] %>';
68     $("#log-viewer-download-url").attr('href', logcollection_url);
69     $("#log-viewer-download-pane").show();
70     var headers = {};
71     if (log_size > log_maxbytes) {
72       headers['Range'] = 'bytes=0-' + log_maxbytes;
73     }
74     var ajax_opts = { dataType: 'text', headers: headers };
75     load_log();
76
77     function load_log() {
78         $.ajax(logcollection_url, ajax_opts).done(done).fail(fail);
79     }
80     function done(data, status, jqxhr) {
81         if (jqxhr.getResponseHeader('Content-Type').indexOf('application/json') === 0) {
82             // The browser won't allow a redirect-with-cookie response
83             // because keep-web isn't same-origin with us. Instead, we
84             // assure keep-web it's OK to respond with the content
85             // immediately by setting the token in the request body
86             // instead and adding disposition=attachment.
87             logcollection_url = JSON.parse(data).href;
88             var queryAt = logcollection_url.indexOf('?api_token=');
89             if (queryAt >= 0) {
90                 ajax_opts.method = 'POST';
91                 ajax_opts.data = {
92                     api_token: logcollection_url.slice(queryAt+11),
93                     disposition: 'attachment',
94                 };
95                 logcollection_url = logcollection_url.slice(0, queryAt);
96             }
97             return load_log();
98         }
99         logViewer.filter();
100         addToLogViewer(logViewer, data.split("\n"), taskState);
101         logViewer.filter(makeFilter());
102         content_range_hdr = jqxhr.getResponseHeader('Content-Range');
103         var v = content_range_hdr && content_range_hdr.match(/bytes \d+-(\d+)\/(.+)/);
104         short_log = v && (v[2] == '*' || parseInt(v[1]) + 1 < v[2]);
105         if (jqxhr.status == 206 && short_log) {
106             $("#log-viewer-overview").html(
107                 '<p>Showing only ' + data.length + ' bytes of this log.' +
108                     ' Timing information is unavailable since' +
109                     ' the full log was not retrieved.</p>'
110             );
111         } else {
112             generateJobOverview("#log-viewer-overview", logViewer, taskState);
113         }
114         $("#log-viewer .spinner").detach();
115     }
116     function fail(jqxhr, status, error) {
117         // TODO: tell the user about the error
118         console.log('load_log failed: status='+status+' error='+error);
119         $("#log-viewer .spinner").detach();
120     }
121   <% end %>
122 <% else %>
123   <%# Live log loading not implemented yet. %>
124 <% end %>
125
126 $(".toggle-filter, .radio-filter").on("change", function() {
127   logViewer.filter(makeFilter());
128 });
129
130 $("#filter-all").on("click", function() {
131   $(".toggle-filter").each(function(i, f) { f.checked = true; });
132   logViewer.filter(makeFilter());
133 });
134
135 $("#filter-none").on("click", function() {
136   $(".toggle-filter").each(function(i, f) { f.checked = false; console.log(f); });
137   logViewer.filter(makeFilter());
138 });
139
140 $("#sort-by-time").on("change", function() {
141   logViewer.sort("id", {sortFunction: sortById});
142 });
143
144 $("#sort-by-task").on("change", function() {
145   logViewer.sort("taskid", {sortFunction: sortByTask});
146 });
147
148 $("#sort-by-node").on("change", function() {
149   logViewer.sort("node", {sortFunction: sortByNode});
150 });
151
152 $("#set-show-failed-only").on("click", function() {
153   $("#sort-by-task").prop("checked", true);
154   $("#show-failed-tasks").prop("checked", true);
155   $("#show-crunch").prop("checked", false);
156   $("#show-task-dispatch").prop("checked", true);
157   $("#show-script-print").prop("checked", true);
158   $("#show-crunchstat").prop("checked", false);
159   logViewer.filter(makeFilter());
160   logViewer.sort("taskid", {sortFunction: sortByTask});
161 });
162
163 })();
164
165 </script>
166
167 <div id="log-viewer">
168
169   <h3>Summary</h3>
170   <p id="log-viewer-overview">
171     <% if !logcollection %>
172       The collection containing the job log was not found.
173     <% end %>
174   </p>
175
176   <p id="log-viewer-download-pane" style="display:none">
177     <a id="log-viewer-download-url" href="">Download the full log</a>
178   </p>
179
180   <div class="h3">Log
181
182     <span class="pull-right">
183       <% if @object.andand.tasks_summary.andand[:failed] and @object.tasks_summary[:failed] > 0 %>
184         <button id="set-show-failed-only" class="btn btn-danger">
185           Show failed task diagnostics only
186         </button>
187       <% end %>
188
189       <button id="filter-all" class="btn">
190         Select all
191       </button>
192       <button id="filter-none" class="btn">
193         Select none
194       </button>
195     </span>
196   </div>
197
198   <input class="search pull-right" style="margin-top: 1em" placeholder="Search" />
199
200   <div>
201     <div class="radio-inline log-viewer-button" style="margin-left: 10px">
202       <label><input id="sort-by-time" type="radio" name="sort-radio" checked> Sort by time</label>
203     </div>
204     <div class="radio-inline log-viewer-button">
205       <label><input id="sort-by-node" type="radio" name="sort-radio" > Sort by node</label>
206     </div>
207
208     <div class="radio-inline log-viewer-button">
209       <label><input id="sort-by-task" type="radio" name="sort-radio" > Sort by task</label>
210     </div>
211   </div>
212
213   <div>
214     <div class="radio-inline log-viewer-button" style="margin-left: 10px">
215       <label><input id="show-all-tasks" type="radio" name="show-tasks-group" checked="true" class="radio-filter"> Show all tasks</label>
216     </div>
217     <div class="radio-inline log-viewer-button">
218       <label><input id="show-successful-tasks" type="radio" name="show-tasks-group" class="radio-filter"> Only successful tasks</label>
219     </div>
220     <div class="radio-inline log-viewer-button">
221       <label><input id="show-failed-tasks" type="radio" name="show-tasks-group" class="radio-filter"> Only failed tasks</label>
222     </div>
223   </div>
224
225   <div>
226     <div class="checkbox-inline log-viewer-button" style="margin-left: 10px">
227       <label><input id="show-crunch" type="checkbox" checked="true" class="toggle-filter"> Show crunch diagnostics</label>
228     </div>
229     <div class="checkbox-inline log-viewer-button">
230       <label><input id="show-task-dispatch" type="checkbox" checked="true" class="toggle-filter"> Show task dispatch</label>
231     </div>
232     <div class="checkbox-inline log-viewer-button">
233       <label><input id="show-task-print" type="checkbox" checked="true" class="toggle-filter"> Show task diagnostics</label>
234     </div>
235     <div class="checkbox-inline log-viewer-button">
236       <label><input id="show-crunchstat" type="checkbox" checked="true" class="toggle-filter"> Show compute usage</label>
237     </div>
238
239   </div>
240
241   <div class="smart-scroll" data-smart-scroll-padding-bottom="50" style="margin-bottom: 0px">
242     <table class="log-viewer-table">
243       <thead>
244         <tr>
245           <th class="id" data-sort="id"></th>
246           <th class="timestamp" data-sort="timestamp">Timestamp</th>
247           <th class="node"  data-sort="node">Node</th>
248           <th class="slot"  data-sort="slot">Slot</th>
249           <th class="type" data-sort="type">Log type</th>
250           <th class="taskid"  data-sort="taskid">Task</th>
251           <th class="message" data-sort="message">Message</th>
252         </tr>
253       </thead>
254       <tbody class="list">
255         <tr>
256           <td class="id"></td>
257           <td class="timestamp"></td>
258           <td class="node"></td>
259           <td class="slot"></td>
260           <td class="type"></td>
261           <td class="taskid"></td>
262           <td class="message"></td>
263         </tr>
264       </tbody>
265     </table>
266
267     <% if @object.log and logcollection %>
268       <div class="spinner spinner-32px"></div>
269     <% end %>
270
271   </div>
272
273   <div class="log-viewer-paging-div" style="margin-bottom: -15px">
274     <a href="#" class="log-viewer-page-up"><span class='glyphicon glyphicon-arrow-up'></span></a>
275     <span class="log-viewer-paging"></span>
276     <a href="#" class="log-viewer-page-down"><span class='glyphicon glyphicon-arrow-down'></span></a>
277   </div>
278
279 </div>
280
281 <% end %>