view_stats.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. const csv = require('csvtojson');
  2. const fs = require('fs');
  3. const os = require('os');
  4. (function () {
  5. view_stats = function( app )
  6. {
  7. app.get('/stats', async (req, res) => {
  8. let d = (new Date());
  9. let file_date = d.getYear() + "_" + d.getMonth() + "_" + d.getDate() ;
  10. let csv_file = `./log/stats_${file_date}.csv`;
  11. if( ! fs.existsSync(csv_file) )
  12. {
  13. res.send("refresh in a few minutes , no stats have been generated yet" );
  14. return;
  15. }
  16. var raw_csv = fs.readFileSync( csv_file ).toString(); // .split( os.EOL );
  17. let labels_time = [];
  18. let chart1_cpu = [];
  19. let chart1_ram = [];
  20. let chart2_connections = [];
  21. let chart2_activeLobbies = [];
  22. let chart3_requestsCount = [];
  23. const array = await csv().fromFile(csv_file);
  24. var i = 0;
  25. if( array.length > 150 ) i = array.length - 150;
  26. for( i ; i < array.length; ++i )
  27. {
  28. var csv_obj = array[ i ];
  29. chart1_cpu.push( `${ csv_obj['cpu'] }` );
  30. chart1_ram.push( `${csv_obj['ram']}` );
  31. chart2_connections.push( `${ csv_obj['connections'] }` );
  32. chart2_activeLobbies.push( `${csv_obj['activeLobbies']}` );
  33. chart3_requestsCount.push( `${csv_obj['requestsCount']}` );
  34. var time_str = new Date( parseInt( csv_obj['time'] ) ).toLocaleTimeString("en-US");
  35. time_str = time_str.slice(0,4) + time_str.slice(8).toLocaleLowerCase() + " ";
  36. // var time_str = new Date( parseInt( csv_obj['time'] ) ).toISOString().slice(11, 16);
  37. labels_time.push( `"${time_str}"` );
  38. }
  39. res.set('Content-Type', 'text/html');
  40. res.send(
  41. // ----------------------------
  42. `
  43. <div class="graph-container"><canvas id="canvas1"></canvas></div>
  44. <div class="graph-container"><canvas id="canvas2"></canvas></div>
  45. <div class="graph-container"><canvas id="canvas3"></canvas></div>
  46. <div class="grid3">
  47. <div class="box">CSV Data<textarea>${raw_csv}</textarea></div>
  48. <div class="box">Connected Clients<textarea>${stats_request_ip_list}</textarea></div>
  49. <div class="box"></div>
  50. <div class="box">A</div>
  51. <div class="box">B</div>
  52. <div class="box">C</div>
  53. </div>
  54. <div class="grid2">
  55. <div class="box">0</div>
  56. <div class="box">1</div>
  57. <div class="box">2</div>
  58. <div class="box">3</div>
  59. </div>
  60. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  61. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
  62. <script>
  63. window.chartColors =
  64. {
  65. red: 'rgb(255, 99, 132)',
  66. orange: 'rgb(255, 159, 64)',
  67. yellow: 'rgb(255, 205, 86)',
  68. green: 'rgb(75, 192, 192)',
  69. blue: 'rgb(54, 162, 235)',
  70. purple: 'rgb(153, 102, 255)',
  71. grey: 'rgb(231,233,237)'
  72. };
  73. var chart_canvas1 = new Chart( document.getElementById("canvas1").getContext("2d"), {
  74. type: 'line',
  75. options: {
  76. maintainAspectRatio : false,
  77. responsive: true,
  78. tooltips: { mode: 'index', intersect: false },
  79. hover: { mode: 'nearest', intersect: true },
  80. scales: {
  81. xAxes: [{ display: false, scaleLabel: { display: true } }],
  82. yAxes: [{ display: true, scaleLabel: { display: true } }]
  83. }
  84. },
  85. data: {
  86. labels: [ ${labels_time} ],
  87. datasets:
  88. [
  89. {
  90. label: "CPU %",
  91. backgroundColor: window.chartColors.red,
  92. borderColor: window.chartColors.red,
  93. data: [ ${chart1_cpu} ],
  94. fill: false,
  95. }, {
  96. label: "RAM %",
  97. fill: false,
  98. backgroundColor: window.chartColors.blue,
  99. borderColor: window.chartColors.blue,
  100. data: [ ${chart1_ram} ],
  101. }
  102. ]
  103. }
  104. });
  105. new Chart( document.getElementById("canvas2").getContext("2d"), {
  106. type: 'line',
  107. options: {
  108. maintainAspectRatio : false,
  109. responsive: true,
  110. tooltips: { mode: 'index', intersect: false },
  111. hover: { mode: 'nearest', intersect: true },
  112. scales: {
  113. xAxes: [{ display: false, scaleLabel: { display: true } }],
  114. yAxes: [{ display: true, scaleLabel: { display: true } }]
  115. }
  116. },
  117. data: {
  118. labels: [ ${labels_time} ],
  119. datasets:
  120. [
  121. {
  122. label: "Connections",
  123. backgroundColor: window.chartColors.orange,
  124. borderColor: window.chartColors.orange,
  125. data: [ ${chart2_connections} ],
  126. fill: false,
  127. },
  128. {
  129. label: "Active Lobbies",
  130. fill: false,
  131. backgroundColor: window.chartColors.red,
  132. borderColor: window.chartColors.red,
  133. data: [ ${chart2_activeLobbies} ],
  134. }
  135. ]
  136. }
  137. });
  138. new Chart( document.getElementById("canvas3").getContext("2d"), {
  139. type: 'line',
  140. options: {
  141. maintainAspectRatio : false,
  142. responsive: true,
  143. tooltips: { mode: 'index', intersect: false },
  144. hover: { mode: 'nearest', intersect: true },
  145. scales: {
  146. xAxes: [{ display: false, scaleLabel: { display: true } }],
  147. yAxes: [{ display: true, scaleLabel: { display: true } }]
  148. }
  149. },
  150. data: {
  151. labels: [ ${labels_time} ],
  152. datasets:
  153. [
  154. {
  155. label: "Requests Count",
  156. fill: false,
  157. backgroundColor: window.chartColors.green,
  158. borderColor: window.chartColors.green,
  159. data: [ ${chart3_requestsCount} ],
  160. }
  161. ]
  162. }
  163. });
  164. </script>
  165. <style>
  166. html , body { margin: 0; }
  167. *Small media devices specific styles*/
  168. @media screen and (max-width: 800px) {
  169. .graph-container {
  170. display: block;
  171. float: none;
  172. width: 100%;
  173. margin-top: 0px;
  174. margin-right:0px;
  175. margin-left:0px;
  176. height: auto;
  177. }
  178. }
  179. .graph-container {
  180. width: 100%;
  181. height: 200px;
  182. }
  183. /* Grid with responsive column width */
  184. .grid2 {
  185. display: grid;
  186. grid-template-columns: 1fr 1fr;
  187. grid-template-rows: 1fr 1fr;
  188. grid-gap: 10px;
  189. }
  190. .grid3 {
  191. display: grid;
  192. grid-template-columns: repeat(3, 1fr);
  193. grid-template-rows: 1fr 0fr;
  194. grid-gap: 10px;
  195. }
  196. .box {
  197. background-color: #EFEFEF;
  198. border-radius: 5px;
  199. color: #fff;
  200. font-size: 1em;
  201. padding: 10px 15px;
  202. }
  203. textarea {
  204. width:95%;
  205. height: 200px;
  206. }
  207. div {
  208. font-family: "Lato", "Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif;
  209. font-size: 17px;
  210. }
  211. </style>
  212. `
  213. // ----------------------------
  214. );
  215. });
  216. };
  217. // ----------
  218. })();