Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / app / views / jobs / _show_log.html.erb
index aaae89addee10492cc9c924787b6d6afac9f6779..b4ede751183206e8259afd6df196f7a368c2bb7e 100644 (file)
@@ -1,12 +1,26 @@
+<%# Copyright (C) The Arvados Authors. All rights reserved.
+
+SPDX-License-Identifier: AGPL-3.0 %>
+
 <% if !@object.log %>
 
-<% log_history = stderr_log_history([@object.uuid]) %>
-<div class="arv-log-event-listener arv-log-event-handler-append-logs arv-job-log-window" id="pipeline_event_log_div" data-object-uuids="<%= @object.uuid %>">
-  <% log_history.each do |entry| %>
-    <%=entry%><br/>
-  <% end %>
+<div id="log_graph_div"
+     class="arv-log-event-listener"
+     style="display:none"
+     data-object-uuid="<%= @object.uuid %>"></div>
+
+<div id="event_log_div"
+     class="arv-log-event-listener arv-log-event-handler-append-logs arv-job-log-window"
+     data-object-uuid="<%= @object.uuid %>"
+  ><%= @object.stderr_log_lines(Rails.configuration.running_job_log_records_to_fetch).join("\n") %>
 </div>
 
+<%# Applying a long throttle suppresses the auto-refresh of this
+    partial that would normally be triggered by arv-log-event. %>
+<div class="arv-log-refresh-control"
+     data-load-throttle="86486400000" <%# 1001 nights %>
+     ></div>
+
 <% else %>
 
 <script>
@@ -50,35 +64,65 @@ var makeFilter = function() {
   });
 }
 
-<% if @object.log %>
+<% if @object.log and !@object.log.empty? %>
   <% logcollection = Collection.find @object.log %>
   <% if logcollection %>
-    var log_maxbytes = <%= Rails.configuration.log_viewer_max_bytes %>
-    $.ajax('<%=j url_for logcollection %>/<%=j logcollection.files[0][1] %>',
-          {
-            headers: {'Range': 'bytes=0-' + log_maxbytes}
-          }).
-       done(function(data, status, jqxhr) {
-           logViewer.filter();
-           addToLogViewer(logViewer, data.split("\n"), taskState);
-           logViewer.filter(makeFilter());
-           if (data.length == log_maxbytes) {
-             $("#log-viewer-overview").html(
-               '<p>The log was truncated after ' + log_maxbytes +
-               ' bytes. To view the entire log, run this command' +
-               ' from an Arvados shell VM:</p>' +
-               ' <p><span style="font-family:monospace">arv-get' +
-               ' <%=j logcollection.uuid %>/<%=j logcollection.files[0][1] %>' +
-               '</p>'
-             );
-            } else {
-             generateJobOverview("#log-viewer-overview", logViewer, taskState);
-           }
-           $("#log-viewer .spinner").detach();
-       }).
-       fail(function(jqxhr, status, error) {
-           $("#log-viewer .spinner").detach();
-       });
+    var log_size = <%= logcollection.files[0][2] %>
+    var log_maxbytes = <%= Rails.configuration.log_viewer_max_bytes %>;
+    var logcollection_url = '<%=j url_for logcollection %>/<%=j logcollection.files[0][1] %>';
+    $("#log-viewer-download-url").attr('href', logcollection_url);
+    $("#log-viewer-download-pane").show();
+    var headers = {};
+    if (log_size > log_maxbytes) {
+      headers['Range'] = 'bytes=0-' + log_maxbytes;
+    }
+    var ajax_opts = { dataType: 'text', headers: headers };
+    load_log();
+
+    function load_log() {
+        $.ajax(logcollection_url, ajax_opts).done(done).fail(fail);
+    }
+    function done(data, status, jqxhr) {
+        if (jqxhr.getResponseHeader('Content-Type').indexOf('application/json') === 0) {
+            // The browser won't allow a redirect-with-cookie response
+            // because keep-web isn't same-origin with us. Instead, we
+            // assure keep-web it's OK to respond with the content
+            // immediately by setting the token in the request body
+            // instead and adding disposition=attachment.
+            logcollection_url = JSON.parse(data).href;
+            var queryAt = logcollection_url.indexOf('?api_token=');
+            if (queryAt >= 0) {
+                ajax_opts.method = 'POST';
+                ajax_opts.data = {
+                    api_token: logcollection_url.slice(queryAt+11),
+                    disposition: 'attachment',
+                };
+                logcollection_url = logcollection_url.slice(0, queryAt);
+            }
+            return load_log();
+        }
+        logViewer.filter();
+        addToLogViewer(logViewer, data.split("\n"), taskState);
+        logViewer.filter(makeFilter());
+        content_range_hdr = jqxhr.getResponseHeader('Content-Range');
+        var v = content_range_hdr && content_range_hdr.match(/bytes \d+-(\d+)\/(.+)/);
+        short_log = v && (v[2] == '*' || parseInt(v[1]) + 1 < v[2]);
+        if (jqxhr.status == 206 && short_log) {
+            $("#log-viewer-overview").html(
+                '<p>Showing only ' + data.length + ' bytes of this log.' +
+                    ' Timing information is unavailable since' +
+                    ' the full log was not retrieved.</p>'
+            );
+        } else {
+            generateJobOverview("#log-viewer-overview", logViewer, taskState);
+        }
+        $("#log-viewer .spinner").detach();
+    }
+    function fail(jqxhr, status, error) {
+        // TODO: tell the user about the error
+        console.log('load_log failed: status='+status+' error='+error);
+        $("#log-viewer .spinner").detach();
+    }
   <% end %>
 <% else %>
   <%# Live log loading not implemented yet. %>
@@ -134,6 +178,10 @@ $("#set-show-failed-only").on("click", function() {
     <% end %>
   </p>
 
+  <p id="log-viewer-download-pane" style="display:none">
+    <a id="log-viewer-download-url" href="">Download the full log</a>
+  </p>
+
   <div class="h3">Log
 
     <span class="pull-right">