|
1 | 1 | this.AgentsIndexPage = class AgentsIndexPage { |
2 | 2 | constructor() { |
3 | | - $("#agents-table").dataTable({ |
4 | | - order: [[1, 'asc']], |
5 | | - pageLength: 100 |
| 3 | + function updateDiagram(json) { |
| 4 | + window.updateDiagramStatus(json); |
| 5 | + } |
| 6 | + |
| 7 | + function timeAgo(date) { |
| 8 | + if (!date) { |
| 9 | + return ''; |
| 10 | + } |
| 11 | + var seconds = Math.floor(((new Date().getTime()/1000) - date)), |
| 12 | + interval = Math.floor(seconds / 31536000); |
| 13 | + |
| 14 | + if (interval > 1) return interval + "y ago"; |
| 15 | + |
| 16 | + interval = Math.floor(seconds / 2592000); |
| 17 | + if (interval > 1) return interval + "m ago"; |
| 18 | + |
| 19 | + interval = Math.floor(seconds / 86400); |
| 20 | + if (interval >= 1) return interval + "d ago"; |
| 21 | + |
| 22 | + interval = Math.floor(seconds / 3600); |
| 23 | + if (interval >= 1) return interval + "h ago"; |
| 24 | + |
| 25 | + interval = Math.floor(seconds / 60); |
| 26 | + if (interval >= 1) return interval + "m ago"; |
| 27 | + |
| 28 | + return "<1m ago"; |
| 29 | + } |
| 30 | + |
| 31 | + var agentsTable = $("#agents-table").dataTable({ |
| 32 | + paging: false, |
| 33 | + createdRow: function(row, data, index) { |
| 34 | + if (data.unavailable) { |
| 35 | + $(row).children().each( function(i, td) { |
| 36 | + if ($(td).find('.btn-group').length == 0) { |
| 37 | + $(td).addClass('agent-unavailable'); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + }, |
| 42 | + columns: [ |
| 43 | + { |
| 44 | + data: 'name', |
| 45 | + render: function (data, type, row) { |
| 46 | + if (type == 'display') { |
| 47 | + var workflow_links = row.workflows.map(workflow => { |
| 48 | + return `<a class="badge" style="color:${workflow.fg_color};background-color:${workflow.bg_color};" href="/workflows/${workflow.id}">${workflow.name}</a>`; |
| 49 | + }); |
| 50 | + var agentUrl = `/agents/${row.id}`; |
| 51 | + var workflow_id = workflowId(); |
| 52 | + if (workflow_id) { |
| 53 | + agentUrl = `${agentUrl}?workflow_id=${workflow_id}`; |
| 54 | + } |
| 55 | + return `<a href="${agentUrl}">${data}</a><br/><span class='text-muted'>${row.human_type}</span><span>${workflow_links}</span>`; |
| 56 | + } |
| 57 | + return data; |
| 58 | + } |
| 59 | + }, |
| 60 | + { data: 'schedule' }, |
| 61 | + { |
| 62 | + data: 'last_check_at', |
| 63 | + render: function(data, type, row) { |
| 64 | + if (type == 'display') { |
| 65 | + return timeAgo(data); |
| 66 | + } |
| 67 | + return data; |
| 68 | + } |
| 69 | + }, |
| 70 | + { |
| 71 | + data: 'last_receive_at', |
| 72 | + render: function(data, type, row) { |
| 73 | + if (type == 'display') { |
| 74 | + return timeAgo(data); |
| 75 | + } |
| 76 | + return data; |
| 77 | + } |
| 78 | + }, |
| 79 | + { |
| 80 | + data: 'last_message_at', |
| 81 | + render: function(data, type, row) { |
| 82 | + if (type == 'display') { |
| 83 | + return timeAgo(data); |
| 84 | + } |
| 85 | + return data; |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + data: 'messages_count', |
| 90 | + render: function(data, type, row) { |
| 91 | + if (type == 'display') { |
| 92 | + return `<a href="/agents/${row.id}?tab=messages">${data}</a>`; |
| 93 | + } |
| 94 | + return data; |
| 95 | + } |
| 96 | + }, |
| 97 | + { |
| 98 | + data: 'working', |
| 99 | + render: function(data, type, row) { |
| 100 | + if (type == 'display') { |
| 101 | + if (data == true) { |
| 102 | + return '<span class="badge badge-success">Yes</span>'; |
| 103 | + } else { |
| 104 | + return '<span class="badge badge-danger">No</span>'; |
| 105 | + } |
| 106 | + } |
| 107 | + return data; |
| 108 | + } |
| 109 | + }, |
| 110 | + { |
| 111 | + data: 'action_menu', |
| 112 | + render: function(data, type, row) { |
| 113 | + if (type == 'display') { |
| 114 | + return `<div class="btn-group btn-group-sm"><button type="button" ` + |
| 115 | + `class="btn btn-primary btn-sm dropdown dropdown-toggle" `+ |
| 116 | + `data-toggle="dropdown"><i class="fa fa-th-list"></i> Actions <span class="caret"></span></button>${data}</div>`; |
| 117 | + } |
| 118 | + return data; |
| 119 | + } |
| 120 | + }, |
| 121 | + ] |
| 122 | + }); |
| 123 | + |
| 124 | + agentsTable.on('xhr.dt', function(e, settings, json, xhr) { |
| 125 | + if (json) { |
| 126 | + updateDiagram(json); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + // Check if any pop-ups are open. |
| 131 | + function canUpdate() { |
| 132 | + if ($('#agents-table .dropdown-menu.show').length > 0) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + if ($('.confirm-agent.modal.show').length > 0) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + function updateTable(json) { |
| 142 | + if (canUpdate()) { |
| 143 | + var table = agentsTable.api(); |
| 144 | + table.clear(); |
| 145 | + table.rows.add(json); |
| 146 | + table.draw(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + function workflowId() { |
| 151 | + return $('#agents-table').data('workflow_id'); |
| 152 | + } |
| 153 | + |
| 154 | + function loadData() { |
| 155 | + if (canUpdate()) { |
| 156 | + var url = '/agents/table.json'; |
| 157 | + var workflow_id = workflowId(); |
| 158 | + if (workflow_id) { |
| 159 | + url = `${url}?workflow_id=${workflow_id}`; |
| 160 | + } |
| 161 | + $.getJSON(url, function(json) { |
| 162 | + updateTable(json); |
| 163 | + updateDiagram(json); |
| 164 | + }); |
| 165 | + } |
| 166 | + } |
| 167 | + loadData(); |
| 168 | + |
| 169 | + setInterval(function() { |
| 170 | + loadData(); |
| 171 | + }, 2000); |
| 172 | + |
| 173 | + // Close modal with remote forms after successful submit. |
| 174 | + $(document).on('ajax:success', '.modal', function(event) { |
| 175 | + $('.modal').modal('hide'); |
6 | 176 | }); |
7 | 177 | } |
8 | 178 | }; |
9 | 179 |
|
10 | | -$(() => Utils.registerPage(AgentsIndexPage, {forPathsMatching: /^agents/})); |
| 180 | +$(() => Utils.registerPage(AgentsIndexPage, {forPathsMatching: /^(agents|workflows)/})); |
11 | 181 |
|
0 commit comments