taskManager.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <!DOCTYPE html>
  2. <head>
  3. <title>Task Manager</title>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
  6. <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
  7. <script type="text/javascript" src="../../script/jquery.min.js"></script>
  8. <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
  9. <script type="text/javascript" src="../../script/ao_module.js"></script>
  10. <script type="text/javascript" src="../info/js/chart/Chart.min.js"></script>
  11. </head>
  12. <body>
  13. <br>
  14. <div class="ui container">
  15. <div id="cpuChartContainer" style="position: relative;">
  16. <h2 class="ui header">
  17. CPU
  18. <div class="sub header">CPU Usage (%) in the previous 60 seconds</div>
  19. </h2>
  20. <p id="CPUname" style="position: absolute; top: 1em; right: 0.3em; font-size: 16px;">Generic Processor</p>
  21. <canvas id="cpuChart" width="1200" height="300"></canvas>
  22. <div class="ui stackable grid">
  23. <div class="four wide column">
  24. <div class="ui header" >
  25. <span id="cpuUsage">0%</span>
  26. <div class="sub header">Usage</div>
  27. </div>
  28. </div>
  29. <div class="four wide column">
  30. <div class="ui header" >
  31. <span id="cpufreq">Loading</span>
  32. <div class="sub header">Frequency</div>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. <br>
  38. <div id="ramChartContainer" style="position: relative;">
  39. <h2 class="ui header">
  40. Memory
  41. <div class="sub header">RAM Usage</div>
  42. </h2>
  43. <p id="RAMInfo" style="position: absolute; top: 1em; right: 0.3em; font-size: 16px;"></p>
  44. <canvas id="ramChart" width="1200" height="300"></canvas>
  45. <br>
  46. <div class="ui stackable grid">
  47. <div class="four wide column">
  48. <div class="ui header" >
  49. <span id="ramUsed">Loading</span>
  50. <div class="sub header">Used</div>
  51. </div>
  52. </div>
  53. <div class="four wide column">
  54. <div class="ui header" >
  55. <span id="ramUsedPercentage">Loading</span>
  56. <div class="sub header">Used (%)</div>
  57. </div>
  58. </div>
  59. <div class="four wide column">
  60. <div class="ui header">
  61. <span id="ramTotal">Loading</span>
  62. <div class="sub header">Total</div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. <div id="netChartContainer" style="position: relative; margin-top: 1.2em;">
  68. <h2 class="ui header">
  69. Network
  70. <div class="sub header">Network usage in the previous 60 seconds</div>
  71. </h2>
  72. <p id="netGraphScale" style="position: absolute; top: 1em; right: 0.3em; font-size: 16px;">n/a</p>
  73. <canvas id="netChart" width="1200" height="300"></canvas>
  74. <div class="ui stackable grid">
  75. <div class="four wide column">
  76. <div class="ui header" >
  77. <span id="rx">0</span>
  78. <div class="sub header">Received</div>
  79. </div>
  80. </div>
  81. <div class="four wide column">
  82. <div class="ui header" >
  83. <span id="tx">0</span>
  84. <div class="sub header">Transmitted</div>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. <script>
  91. var cpuChart;
  92. var ramChart;
  93. var netChart;
  94. var previousNetData = [0, 0];
  95. //Override Chart.js v3 poor API designs
  96. Chart.defaults.plugins.tooltip.enabled = false;
  97. Chart.defaults.plugins.legend.display = false;
  98. var options = {
  99. maintainAspectRatio: true,
  100. responsive: true,
  101. spanGaps: false,
  102. elements: {
  103. line: {
  104. tension: 0.000001
  105. }
  106. },
  107. plugins: {
  108. filler: {
  109. propagate: false
  110. },
  111. },
  112. scales: {
  113. x: {
  114. grid: {
  115. color: "rgba(83, 160, 205, 0.2)"
  116. }
  117. },
  118. y: {
  119. min: 0,
  120. max: 100,
  121. grid: {
  122. color: "rgba(83, 160, 205, 0.2)"
  123. }
  124. }
  125. },
  126. legend: {
  127. display: false,
  128. },
  129. tooltips: {
  130. callbacks: {
  131. label: function(tooltipItem) {
  132. return tooltipItem.yLabel;
  133. }
  134. }
  135. }
  136. };
  137. var ramOptions = {
  138. maintainAspectRatio: true,
  139. responsive: true,
  140. spanGaps: false,
  141. elements: {
  142. line: {
  143. tension: 0.000001
  144. }
  145. },
  146. plugins: {
  147. filler: {
  148. propagate: false
  149. },
  150. },
  151. scales: {
  152. x: {
  153. grid: {
  154. color: "rgba(156, 55, 185, 0.2)"
  155. }
  156. },
  157. y: {
  158. min: 0,
  159. max: 100,
  160. grid: {
  161. color: "rgba(156, 55, 185, 0.2)"
  162. }
  163. }
  164. },
  165. legend: {
  166. display: false,
  167. },
  168. tooltips: {
  169. callbacks: {
  170. label: function(tooltipItem) {
  171. return tooltipItem.yLabel;
  172. }
  173. }
  174. }
  175. };
  176. var netOptions = {
  177. maintainAspectRatio: true,
  178. responsive: true,
  179. spanGaps: false,
  180. elements: {
  181. line: {
  182. tension: 0.000001
  183. }
  184. },
  185. plugins: {
  186. filler: {
  187. propagate: false
  188. },
  189. },
  190. scales: {
  191. x: {
  192. grid: {
  193. color: "rgba(167, 79, 1, 0.2)"
  194. }
  195. },
  196. y: {
  197. min: Math.min.apply(this, getMergedRxTxDataset()),
  198. max: Math.max.apply(this, getMergedRxTxDataset()) + 5,
  199. grid: {
  200. color: "rgba(167, 79, 1, 0.2)"
  201. }
  202. }
  203. },
  204. legend: {
  205. display: false,
  206. },
  207. tooltips: {
  208. callbacks: {
  209. label: function(tooltipItem) {
  210. return tooltipItem.yLabel;
  211. }
  212. }
  213. }
  214. };
  215. initInfo();
  216. chartInit();
  217. //Special code to handle embedding into the System Setting embedded windows
  218. var insideIframe = false;
  219. if (parent.managerInIframe !== undefined && parent.managerInIframe == true){
  220. $(window).on("click", function(e){
  221. parent.ao_module_focus();
  222. });
  223. }
  224. function initInfo(){
  225. $.get("../../system/info/getCPUinfo", function(data){
  226. var data = JSON.parse(data);
  227. console.log(data);
  228. $("#CPUname").text(data.Model);
  229. if (parseFloat(data.Freq) > 1000){
  230. $("#cpufreq").text((data.Freq/1000).toFixed(2) + " Ghz");
  231. }else{
  232. $("#cpufreq").text(data.Freq + " Mhz");
  233. }
  234. });
  235. $.get("../../system/info/getRAMinfo", function(data){
  236. //Return ram in byte
  237. var ramsize = bytesToSize(data);
  238. $("#RAMInfo").text(ramsize);
  239. })
  240. }
  241. function bytesToSize(bytes) {
  242. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  243. if (bytes == 0) return '0 Byte';
  244. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  245. return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
  246. }
  247. function bitToSize(bytes) {
  248. var sizes = ['b', 'Kb', 'Mb', 'Gb', 'Tb'];
  249. if (bytes == 0) return '0 b';
  250. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  251. return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
  252. }
  253. function chartInit(){
  254. cpuChart = new Chart('cpuChart', {
  255. type: 'line',
  256. data: {
  257. labels: [],
  258. datasets: [{
  259. backgroundColor: "rgba(241,246,250,0.4)",
  260. borderColor: "#4c9dcb",
  261. data: [],
  262. radius: 0,
  263. borderWidth: 2,
  264. fill: 'start'
  265. }]
  266. },
  267. options: options
  268. });
  269. //Push 60 empty data into the chart
  270. for (var i =0; i < 60; i++){
  271. addData(cpuChart, "",0)
  272. }
  273. //Create RAM Chart
  274. ramChart = new Chart('ramChart', {
  275. type: 'line',
  276. data: {
  277. labels: [],
  278. datasets: [{
  279. backgroundColor: "rgba(244,242,244,0.4)",
  280. borderColor: "#9528b4",
  281. data: [],
  282. radius: 0,
  283. borderWidth: 2,
  284. fill: 'start'
  285. }]
  286. },
  287. options: ramOptions
  288. });
  289. for (var i =0; i < 60; i++){
  290. addData(ramChart, "",0)
  291. }
  292. //Create Network Chart
  293. netChart = new Chart('netChart', {
  294. type: 'line',
  295. data: {
  296. labels: [],
  297. datasets: [{
  298. backgroundColor: "rgba(252,243,235,0.4)",
  299. borderColor: "#a74f01",
  300. data: [],
  301. radius: 0,
  302. borderWidth: 2,
  303. fill: 'start'
  304. },
  305. {
  306. backgroundColor: "rgba(252,243,235,0.2)",
  307. borderColor: "#a74f01",
  308. borderDash: [5, 5],
  309. data: [],
  310. radius: 0,
  311. borderWidth: 2,
  312. fill: 'start'
  313. }]
  314. },
  315. options: netOptions
  316. });
  317. for (var i =0; i < 60; i++){
  318. addNetData(netChart, "", 0, 0)
  319. }
  320. }
  321. resizeCharts();
  322. $(window).on("resize", function(){
  323. resizeCharts();
  324. })
  325. function resizeCharts(){
  326. $("#cpuChart").width($("#cpuChartContainer").width());
  327. $("#ramChart").width($("#ramChartContainer").width());
  328. }
  329. updateData();
  330. function updateData(){
  331. //Testing use
  332. /*
  333. setInterval(function(){
  334. addAndShiftChartDate(cpuChart, "", Math.floor(Math.random() * 50) + 20)
  335. addAndShiftChartDate(ramChart, "", Math.floor(Math.random() * 10) + 30)
  336. }, 1000)
  337. */
  338. //Calculate the bandwidth diff
  339. $.get("../../system/network/getNICUsage", function(data){
  340. if (data.error !== undefined){
  341. //Error
  342. console.log(data.error);
  343. $("#netGraphScale").text(data.error);
  344. return;
  345. }
  346. if (previousNetData[0] == 0 && previousNetData[1] == 0){
  347. //Not initiated. Set base and wait for next iteration
  348. previousNetData = [data.RX, data.TX];
  349. }else{
  350. var rxd = data.RX - previousNetData[0];
  351. var txd = data.TX - previousNetData[1];
  352. previousNetData = [data.RX, data.TX];
  353. addAndShiftNetworkDate(netChart, "", rxd/1024, txd/1024);
  354. $("#rx").text(bitToSize(rxd)+"/s");
  355. $("#tx").text(bitToSize(txd)+"/s");
  356. //Get the max value of the diagram, round it to the cloest 10x
  357. var chartMaxValue = Math.max.apply(this, getMergedRxTxDataset())
  358. var logVal = Math.log(chartMaxValue) / Math.log(1024);
  359. //Round and display the network speed as cloest 100 value
  360. function roundNearest100(num, logVal) {
  361. return Math.round(num / 100) * 100 * Math.pow(10, logVal);
  362. }
  363. var scale = roundNearest100(chartMaxValue, logVal);
  364. $("#netGraphScale").text(scale);
  365. }
  366. })
  367. $.get("../../system/info/getUsageInfo", function(data){
  368. //Update graph
  369. addAndShiftChartDate(cpuChart, "", data.CPU);
  370. addAndShiftChartDate(ramChart, "", data.RamUsage);
  371. //Update values
  372. $("#cpuUsage").text(data.CPU.toFixed(1) + "%");
  373. $("#ramUsedPercentage").text(data.RamUsage.toFixed(1) + "%")
  374. $("#ramUsed").text(data.UsedRAM);
  375. $("#ramTotal").text(data.TotalRam);
  376. setTimeout(function(){
  377. updateData();
  378. }, 1000);
  379. });
  380. }
  381. function addNetData(chart, label, rx, tx) {
  382. chart.data.labels.push(label);
  383. chart.data.datasets[0].data.push(rx);
  384. chart.data.datasets[1].data.push(tx);
  385. chart.update();
  386. }
  387. function addData(chart, label, data) {
  388. chart.data.labels.push(label);
  389. chart.data.datasets.forEach((dataset) => {
  390. dataset.data.push(data);
  391. });
  392. chart.update();
  393. }
  394. function addAndShiftChartDate(chart, label, newdata) {
  395. chart.data.labels.splice(0, 1); // remove first label
  396. chart.data.datasets.forEach(function(dataset) {
  397. dataset.data.splice(0, 1); // remove first data point
  398. });
  399. chart.update();
  400. // Add new data
  401. chart.data.labels.push(label); // add new label at end
  402. chart.data.datasets.forEach(function(dataset, index) {
  403. dataset.data.push(newdata); // add new data at end
  404. });
  405. chart.update();
  406. }
  407. function addAndShiftNetworkDate(chart, label, rxd, txd) {
  408. chart.data.labels.splice(0, 1); // remove first label
  409. chart.data.datasets.forEach(function(dataset) {
  410. dataset.data.splice(0, 1); // remove first data point
  411. });
  412. chart.update();
  413. // Add new data
  414. chart.data.labels.push(label); // add new label at end
  415. chart.data.datasets[0].data.push(rxd);
  416. chart.data.datasets[1].data.push(txd);
  417. //Update the sacle as well
  418. netChart.options.scales.y.min = Math.min.apply(this, getMergedRxTxDataset());
  419. netChart.options.scales.y.max = Math.max.apply(this, getMergedRxTxDataset()) *1.2;
  420. chart.update();
  421. }
  422. function getMergedRxTxDataset(){
  423. if (netChart == undefined){
  424. return [0, 100];
  425. }
  426. var newArr = [];
  427. newArr = newArr.concat(netChart.data.datasets[0].data,netChart.data.datasets[1].data);
  428. return newArr;
  429. }
  430. </script>
  431. </body>
  432. </html>