9514: Refactor job log related bit out of log_viewer.js into job_log_viewer.js
[arvados.git] / apps / workbench / app / views / work_units / _show_log.html.erb
index 6a0916fe9853b2ecd6ca10bbc3fa4d94923ebbb8..f9b6241ea3ac8540b0f4091b45d53b129165df01 100644 (file)
   </div>
 <% end %>
 
-<%# Show log in terminal window %>
-<h4>Recent logs</h4>
-<div id="event_log_div"
-     class="arv-log-event-listener arv-log-event-handler-append-logs arv-job-log-window"
-     data-object-uuids="<%= wu.log_object_uuids.join(' ') %>"
-  ><%= wu.live_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>
+<% live_log_lines = wu.live_log_lines(Rails.configuration.running_job_log_records_to_fetch).join("\n") %>
+<% if !render_log or (live_log_lines.size > 0) %>
+  <%# Still running, or recently finished and logs are still available from logs table %>
+  <%# Show recent logs in terminal window %>
+  <h4>Recent logs</h4>
+  <div id="event_log_div"
+       class="arv-log-event-listener arv-log-event-handler-append-logs arv-job-log-window"
+       data-object-uuids="<%= wu.log_object_uuids.join(' ') %>"
+    ><%= live_log_lines %>
+  </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>
+<% elsif render_log[:log] %>
+  <%# Retrieve finished log from keep and show %>
+  <script>
+    (function() {
+      var pagesize = 1000;
+      var logViewer = new List('log-viewer', {
+        valueNames: [ 'message'],
+        page: pagesize
+      });
+
+      logViewer.page_offset = 0;
+      logViewer.on("updated", function() { updatePaging(".log-viewer-paging", logViewer, pagesize) } );
+      $(".log-viewer-page-up").on("click", function() { prevPage(logViewer, pagesize, ".log-viewer-paging"); return false; });
+      $(".log-viewer-page-down").on("click", function() { nextPage(logViewer, pagesize, ".log-viewer-paging"); return false; });
+
+      <% logcollection = render_log[:log] %>
+      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] %>';
+      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();
+        }
+        addToLogViewer(logViewer, data.split("\n"));
+        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.</p>'
+          );
+        }
+
+        $("#log-viewer .spinner").detach();
+      }
+      function fail(jqxhr, status, error) {
+        // TODO: tell the user about the error
+        $("#log-viewer .spinner").detach();
+      }
+    })();
+  </script>
+
+  <div id="log-viewer">
+    <p id="log-viewer-overview"></p>
+
+    <div class="h3">Log</div>
+    <div class="smart-scroll" data-smart-scroll-padding-bottom="50" style="margin-bottom: 0px">
+      <table class="log-viewer-table">
+        <tbody class="list">
+          <tr>
+            <td class="message"></td>
+          </tr>
+        </tbody>
+      </table>
+
+      <% if logcollection %>
+        <div class="spinner spinner-32px"></div>
+      <% end %>
+    </div>
+
+    <div class="log-viewer-paging-div" style="margin-bottom: -15px">
+      <a href="#" class="log-viewer-page-up"><span class='glyphicon glyphicon-arrow-up'></span></a>
+      <span class="log-viewer-paging"></span>
+      <a href="#" class="log-viewer-page-down"><span class='glyphicon glyphicon-arrow-down'></span></a>
+    </div>
+  </div>
+<% end %>