2883: Live updating log wip
[arvados.git] / apps / workbench / app / views / jobs / _show_log.html.erb
1 <script>
2
3 function addToLogViewer(logViewer, lines) {
4   var re = /((\d\d\d\d)-(\d\d)-(\d\d))_((\d\d):(\d\d):(\d\d)) ([a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}) (\d+) (\d+)? (.*)/;
5   for (var a in lines) {
6     var v = lines[a].match(re);
7     if (v != null) {
8     v11 = v[11];
9     if (typeof v[11] === 'undefined') {
10       v11 = '&nbsp;';
11     }
12
13   var message = v[12];
14   var type = "";
15   if (v11 != '&nbsp;') {
16     if (/^stderr /.test(message)) {
17         type = "task-output";
18         message = message.substr(7);
19     } else {
20         type = "task-dispatch";
21     }
22   } else {
23     if (/^status: /.test(message)) {
24         type = "job-status";
25         message = message.substr(8);
26     } else {
27         type = "crunch";
28     }
29   }
30
31     logViewer.add({
32       id: logViewer.items.length,
33       timestamp: v[1] + " " + v[5],
34       taskid: v11,
35       message: message,
36       type: type
37     });
38
39     } else {
40       console.log("Did not parse: " + lines[a]);
41     }
42   }
43   logViewer.update();
44 }
45
46 (function() {
47 var logViewer = new List('log-viewer', {
48   valueNames: [ 'id', 'timestamp', 'taskid', 'message', 'type']
49 });
50
51 var makeFilter = function() {
52   var pass = [];
53   $(".toggle-filter").each(function(i, e) {
54     if (e.checked) {
55       pass.push(e.id.substr(5));
56     }
57   });
58   return (function(item) {
59     for (a in pass) {
60       if (pass[a] == item.values().type) { return true; }
61     }
62     return false;
63   });
64 }
65
66 <% if @object.log %>
67 <% logcollection = Collection.find @object.log %>
68
69 $.ajax('<%=j url_for logcollection %>/<%=j logcollection.files[0][1] %>').
70   done(function(data, status, jqxhr) {
71     logViewer.filter();
72     addToLogViewer(logViewer, data.split("\n"));
73     logViewer.filter(makeFilter());
74     $("#logloadspinner").detach();
75   });
76 <% else %>
77   <% backlog = ""
78      Log.filter([['object_uuid', '=', @object.uuid],
79                   ['event_type', '=', 'stderr']]).order(["id", "asc"]).each do |l|
80         backlog += l.properties["text"]
81      end
82    %>
83   var backlog = "<%=j backlog %>";
84     logViewer.filter();
85     addToLogViewer(logViewer, backlog.split("\n"));
86     logViewer.filter(makeFilter());
87
88 <% end %>
89
90 $(".toggle-filter").on("change", function() {
91   logViewer.filter(makeFilter());
92 });
93
94 $("#filter-all").on("click", function() {
95   $(".toggle-filter").each(function(i, f) { f.checked = true; });
96   logViewer.filter(makeFilter());
97 });
98
99 $("#filter-none").on("click", function() {
100   $(".toggle-filter").each(function(i, f) { f.checked = false; console.log(f); });
101   logViewer.filter(makeFilter());
102 });
103
104 $("#sort-by-time").on("change", function() {
105   logViewer.sort("id");
106 });
107
108 $("#sort-by-task").on("change", function() {
109   logViewer.sort("taskid");
110 });
111
112 })();
113
114 </script>
115
116 <div id="log-viewer">
117
118     <div class="radio-inline">
119       <label><input id="sort-by-time" type="radio" name="sort-radio" checked> Sort by time</label>
120     </div>
121     <div class="radio-inline">
122       <label><input id="sort-by-task" type="radio" name="sort-radio" > Sort by task</label>
123     </div>
124 <br>
125   <div class="checkbox-inline">
126     <label><input id="show-crunch" type="checkbox" checked="true" class="toggle-filter"> Show crunch output</label>
127   </div>
128   <div class="checkbox-inline">
129     <label><input id="show-job-status" type="checkbox" checked="true" class="toggle-filter"> Show job status</label>
130   </div>
131   <div class="checkbox-inline">
132     <label><input id="show-task-dispatch" type="checkbox" checked="true" class="toggle-filter"> Show task dispatch</label>
133   </div>
134   <div class="checkbox-inline">
135     <label><input id="show-task-output" type="checkbox" checked="true" class="toggle-filter"> Show task output</label>
136   </div>
137
138 <div class="pull-right">
139     <button id="filter-all" class="btn">
140       Select all
141     </button>
142     <button id="filter-none" class="btn">
143       Select none
144     </button>
145 </div>
146
147   <table class="log-viewer-table">
148     <thead>
149       <tr>
150         <th class="id" data-sort="id"></th>
151         <th class="timestamp" data-sort="timestamp">Timestamp</th>
152         <th class="type" data-sort="type">Log type</th>
153         <th class="taskid"  data-sort="taskid">Task</th>
154         <th class="message" data-sort="message">Message</th>
155       </tr>
156     </thead>
157     <tbody class="list">
158       <tr>
159         <td class="id"></td>
160         <td class="timestamp"></td>
161         <td class="type"></td>
162         <td class="taskid"></td>
163         <td class="message"></td>
164       </tr>
165     </tbody>
166   </table>
167
168 </div>
169
170 <%= image_tag 'ajax-loader.gif', id: "logloadspinner" %>