main.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SOCKET IO
  2. const socket = io({
  3. auth: {
  4. token: "abc"
  5. }
  6. });
  7. // ON CONNECT EVENT
  8. socket.on('connect', () => {
  9. console.log('Connected');
  10. });
  11. // SELECT METRICS ELEMENTS
  12. const cpuText = document.getElementById('cpu-text');
  13. const cpuBar = document.getElementById('cpu-bar');
  14. const ramText = document.getElementById('ram-text');
  15. const ramBar = document.getElementById('ram-bar');
  16. const netText = document.getElementById('net-text');
  17. const netBar = document.getElementById('net-bar');
  18. const diskText = document.getElementById('disk-text');
  19. const diskBar = document.getElementById('disk-bar');
  20. const dockerCards = document.getElementById('cards');
  21. // create
  22. //Update usage bars
  23. socket.on('metrics', ({ cpu, ram, tx, rx, disk}) => {
  24. cpuText.innerHTML = `<span>CPU ${cpu} %</span>`;
  25. cpuBar.innerHTML = `<span style="width: ${cpu}%"><span></span></span>`;
  26. ramText.innerHTML = `<span>RAM ${ram} %</span>`;
  27. ramBar.innerHTML = `<span style="width: ${ram}%"><span></span></span>`;
  28. diskText.innerHTML = `<span>DISK ${disk} %</span>`;
  29. diskBar.innerHTML = `<span style="width: ${disk}%"><span></span></span>`;
  30. });
  31. function drawCharts(name, cpu_array, ram_array) {
  32. var elements = document.querySelectorAll(`${name}`);
  33. if (cpu_array == undefined) {
  34. cpu_array = [37, 35, 44, 28, 36, 24, 65, 31, 37, 39, 62, 51, 35, 41, 35, 27, 93, 53, 61, 27, 54, 43, 4, 46, 39, 62, 51, 35, 41, 67];
  35. }
  36. if (ram_array == undefined) {
  37. ram_array = [93, 54, 51, 24, 35, 35, 31, 67, 19, 43, 28, 36, 62, 61, 27, 39, 35, 41, 27, 35, 51, 46, 62, 37, 44, 53, 41, 65, 39, 37];
  38. }
  39. Array.from(elements).forEach(function(element) {
  40. if (window.ApexCharts) {
  41. new ApexCharts(element, {
  42. chart: {
  43. type: "line",
  44. fontFamily: 'inherit',
  45. height: 40.0,
  46. sparkline: {
  47. enabled: true
  48. },
  49. animations: {
  50. enabled: false
  51. }
  52. },
  53. fill: {
  54. opacity: 1
  55. },
  56. stroke: {
  57. width: [2, 1],
  58. dashArray: [0, 3],
  59. lineCap: "round",
  60. curve: "smooth"
  61. },
  62. series: [{
  63. name: "CPU",
  64. data: cpu_array
  65. }, {
  66. name: "RAM",
  67. data: ram_array
  68. }],
  69. tooltip: {
  70. theme: 'dark'
  71. },
  72. grid: {
  73. strokeDashArray: 4
  74. },
  75. xaxis: {
  76. labels: {
  77. padding: 0
  78. },
  79. tooltip: {
  80. enabled: false
  81. },
  82. type: 'datetime'
  83. },
  84. yaxis: {
  85. labels: {
  86. padding: 4
  87. }
  88. },
  89. labels: [
  90. '2020-06-20', '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19'
  91. ],
  92. colors: [tabler.getColor("primary"), tabler.getColor("gray-600")],
  93. legend: {
  94. show: false
  95. }
  96. }).render();
  97. }
  98. });
  99. }
  100. // container button actions
  101. function buttonAction(button) {
  102. socket.emit('clicked', {container: button.name, state: button.id, action: button.value});
  103. }
  104. socket.on('cards', (data) => {
  105. console.log('cards deleted');
  106. let deleteMeElements = document.querySelectorAll('.deleteme');
  107. deleteMeElements.forEach((element) => {
  108. element.parentNode.removeChild(element);
  109. });
  110. dockerCards.insertAdjacentHTML("afterend", data);
  111. // drawCharts('#cardChart');
  112. });
  113. socket.on('container_stats', (data) => {
  114. let {name, cpu, ram} = data;
  115. // get cpu and ram array of the container from local storage
  116. var cpu_array = JSON.parse(localStorage.getItem(`${name}_cpu`));
  117. var ram_array = JSON.parse(localStorage.getItem(`${name}_ram`));
  118. // if the cpu and ram arrays are null, create both arrays with 30 values of 0
  119. if (cpu_array == null) { cpu_array = Array(30).fill(0); }
  120. if (ram_array == null) { ram_array = Array(30).fill(0); }
  121. // add the new cpu and ram values to the arrays, but limit the array to 30 values
  122. cpu_array.push(cpu);
  123. ram_array.push(ram);
  124. cpu_array = cpu_array.slice(-30);
  125. ram_array = ram_array.slice(-30);
  126. // save the arrays to local storage
  127. localStorage.setItem(`${name}_cpu`, JSON.stringify(cpu_array));
  128. localStorage.setItem(`${name}_ram`, JSON.stringify(ram_array));
  129. // replace the old chart with the new one without breaking the surrounding elements
  130. let chart = document.getElementById(`${name}_chart`);
  131. let newChart = document.createElement('div');
  132. newChart.id = `${name}_chart`;
  133. chart.parentNode.replaceChild(newChart, chart);
  134. drawCharts(`#${name}_chart`, cpu_array, ram_array);
  135. });
  136. socket.on('install', (data) => {
  137. console.log('added install card');
  138. dockerCards.insertAdjacentHTML("afterend", data);
  139. });