|
| 1 | +<% navigation(title: "Dashboard", section: :dashboard) %> |
| 2 | + |
| 3 | +<div class="columns"> |
| 4 | + <div class="column"> |
| 5 | + <div class="notification"> |
| 6 | + <h6 class="title is-6">Pending</h6> |
| 7 | + <span id="pending">--</span> |
| 8 | + </div> |
| 9 | + </div> |
| 10 | + <div class="column"> |
| 11 | + <div class="notification"> |
| 12 | + <h6 class="title is-6">Scheduled</h6> |
| 13 | + <span id="scheduled">--</span> |
| 14 | + </div> |
| 15 | + </div> |
| 16 | + <div class="column"> |
| 17 | + <div class="notification"> |
| 18 | + <h6 class="title is-6">In Progress</h6> |
| 19 | + <span id="in-progress">--</span> |
| 20 | + </div> |
| 21 | + </div> |
| 22 | + <div class="column"> |
| 23 | + <div class="notification"> |
| 24 | + <h6 class="title is-6">Finished</h6> |
| 25 | + <span id="finished">--</span> |
| 26 | + </div> |
| 27 | + </div> |
| 28 | + <div class="column"> |
| 29 | + <div class="notification"> |
| 30 | + <h6 class="title is-6">Failed</h6> |
| 31 | + <span id="failed">--</span> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | +</div> |
| 35 | + |
| 36 | +<div class="columns"> |
| 37 | + <div class="column"> |
| 38 | + General Overview |
| 39 | + </div> |
| 40 | + <div class="column has-text-right"> |
| 41 | + <div class="select is-small"> |
| 42 | + <select id="change-uptime" value="5" onchange="handleSelectUptime(this.value)"> |
| 43 | + <option value="10">10 seconds</option> |
| 44 | + <option value="5">5 seconds</option> |
| 45 | + <option value="3">3 seconds</option> |
| 46 | + <option value="1">1 second</option> |
| 47 | + </select> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | +</div> |
| 51 | +<hr/> |
| 52 | + |
| 53 | +<div> |
| 54 | + <canvas id="general-overview-chart"></canvas> |
| 55 | +</div> |
| 56 | + |
| 57 | +<script> |
| 58 | + |
| 59 | +if (typeof uptimeInterval === "undefined") { |
| 60 | + var uptimeInterval = null; |
| 61 | +} |
| 62 | +if (typeof chart === "undefined") { |
| 63 | + var chart = null; |
| 64 | +} |
| 65 | + |
| 66 | +document.addEventListener("turbo:load", () => { |
| 67 | + const canvas = document.getElementById('general-overview-chart'); |
| 68 | + |
| 69 | + if (!canvas) |
| 70 | + return; |
| 71 | + |
| 72 | + const ctx = canvas.getContext('2d'); |
| 73 | + |
| 74 | + if (chart) { |
| 75 | + chart.destroy(); |
| 76 | + chart = null; |
| 77 | + } |
| 78 | + |
| 79 | + const finished = document.getElementById('finished'); |
| 80 | + const scheduled = document.getElementById('scheduled'); |
| 81 | + const pending = document.getElementById('pending'); |
| 82 | + const inProgress = document.getElementById('in-progress'); |
| 83 | + const failed = document.getElementById('failed'); |
| 84 | + |
| 85 | + let uptime = 5; |
| 86 | + |
| 87 | + const labels = []; |
| 88 | + const data = { |
| 89 | + labels: labels, |
| 90 | + datasets: [{ |
| 91 | + label: 'Success', |
| 92 | + data: [], |
| 93 | + fill: false, |
| 94 | + borderColor: 'rgb(0, 219, 124)', |
| 95 | + tension: 0.1 |
| 96 | + }, |
| 97 | + { |
| 98 | + label: 'Error', |
| 99 | + data: [], |
| 100 | + fill: false, |
| 101 | + borderColor: 'rgb(226, 15, 15)', |
| 102 | + tension: 0.1 |
| 103 | + }, |
| 104 | + { |
| 105 | + label: 'Pending', |
| 106 | + data: [], |
| 107 | + fill: false, |
| 108 | + borderColor: 'rgb(237, 209, 0)', |
| 109 | + tension: 0.1 |
| 110 | + }] |
| 111 | + }; |
| 112 | + |
| 113 | + const config = { |
| 114 | + type: 'line', |
| 115 | + data: data, |
| 116 | + }; |
| 117 | + |
| 118 | + chart = new Chart(ctx, config); |
| 119 | + |
| 120 | + function handleSelectUptime(value) { |
| 121 | + // console.log("Update Uptime to " + value); |
| 122 | + uptime = value; |
| 123 | + clearInterval(uptimeInterval); |
| 124 | + uptimeInterval = setInterval(() => updateChartData(), value * 1000); |
| 125 | + } |
| 126 | + |
| 127 | + async function updateChartData() { |
| 128 | + try { |
| 129 | + const response = await fetch(`<%= application_internal_api_dashboard_index_path %>?uptime=${uptime}`); |
| 130 | + if (!response.ok) throw new Error('Network response was not ok'); |
| 131 | + |
| 132 | + const data = await response.json(); |
| 133 | + |
| 134 | + if (chart == null) return; |
| 135 | + |
| 136 | + chart.data.labels.push(data.uptime.label); |
| 137 | + chart.data.labels = chart.data.labels.slice(-10); |
| 138 | + |
| 139 | + AddChartData(0, data.uptime.finished); |
| 140 | + AddChartData(1, data.uptime.failed); |
| 141 | + AddChartData(2, data.uptime.pending); |
| 142 | + |
| 143 | + finished.innerHTML = data.total.finished; |
| 144 | + inProgress.innerHTML = data.total.in_progress; |
| 145 | + pending.innerHTML = data.total.pending; |
| 146 | + scheduled.innerHTML = data.total.scheduled; |
| 147 | + failed.innerHTML = data.total.failed; |
| 148 | + |
| 149 | + chart.update(); |
| 150 | + } catch (error) { |
| 151 | + console.error('Error at consult chart API:', error); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + function AddChartData(datasetIndex, quantity) { |
| 156 | + chart.data.datasets[datasetIndex].data.push(quantity); |
| 157 | + chart.data.datasets[datasetIndex].data = chart.data.datasets[datasetIndex].data.slice(-10); |
| 158 | + } |
| 159 | + |
| 160 | + if (uptimeInterval != null) |
| 161 | + clearInterval(uptimeInterval); |
| 162 | + |
| 163 | + uptimeInterval = setInterval(() => updateChartData(), 5000); |
| 164 | + updateChartData(); |
| 165 | + |
| 166 | + // Exponha a função no escopo global |
| 167 | + window.handleSelectUptime = handleSelectUptime; |
| 168 | +}); |
| 169 | + |
| 170 | +// Limpa o gráfico e o intervalo antes de sair da página |
| 171 | +document.addEventListener("turbo:before-render", () => { |
| 172 | + if (uptimeInterval != null) { |
| 173 | + clearInterval(uptimeInterval); |
| 174 | + uptimeInterval = null; |
| 175 | + } |
| 176 | + |
| 177 | + if (chart) { |
| 178 | + chart.destroy(); |
| 179 | + chart = null; |
| 180 | + } |
| 181 | +}); |
| 182 | + |
| 183 | + |
| 184 | +</script> |
0 commit comments