taskManager.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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;">100 kbps</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" style="border-left: 2px solid #bc793f; padding-left: 1em;">
  77. <span id="rx">Loading</span>
  78. <div class="sub header">Received</div>
  79. </div>
  80. </div>
  81. <div class="four wide column">
  82. <div class="ui header" style="border-left: 2px dotted #bc793f; padding-left: 1em;">
  83. <span id="tx">Loading</span>
  84. <div class="sub header">Transmitted</div>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. <br><br>
  90. </div>
  91. <script>
  92. var cpuChart;
  93. var ramChart;
  94. var netChart;
  95. var previousNetData = [0, 0];
  96. //Override Chart.js v3 poor API designs
  97. Chart.defaults.plugins.tooltip.enabled = false;
  98. Chart.defaults.plugins.legend.display = false;
  99. var options = {
  100. maintainAspectRatio: true,
  101. responsive: true,
  102. spanGaps: false,
  103. elements: {
  104. line: {
  105. tension: 0.000001
  106. }
  107. },
  108. plugins: {
  109. filler: {
  110. propagate: false
  111. },
  112. },
  113. scales: {
  114. x: {
  115. grid: {
  116. color: "rgba(83, 160, 205, 0.2)"
  117. }
  118. },
  119. y: {
  120. min: 0,
  121. max: 100,
  122. grid: {
  123. color: "rgba(83, 160, 205, 0.2)"
  124. },
  125. ticks: {
  126. display: false,
  127. }
  128. }
  129. },
  130. legend: {
  131. display: false,
  132. },
  133. tooltips: {
  134. callbacks: {
  135. label: function(tooltipItem) {
  136. return tooltipItem.yLabel;
  137. }
  138. }
  139. }
  140. };
  141. var ramOptions = {
  142. maintainAspectRatio: true,
  143. responsive: true,
  144. spanGaps: false,
  145. elements: {
  146. line: {
  147. tension: 0.000001
  148. }
  149. },
  150. plugins: {
  151. filler: {
  152. propagate: false
  153. },
  154. },
  155. scales: {
  156. x: {
  157. grid: {
  158. color: "rgba(156, 55, 185, 0.2)"
  159. }
  160. },
  161. y: {
  162. min: 0,
  163. max: 100,
  164. grid: {
  165. color: "rgba(156, 55, 185, 0.2)"
  166. },
  167. ticks: {
  168. display: false,
  169. }
  170. }
  171. },
  172. legend: {
  173. display: false,
  174. },
  175. tooltips: {
  176. callbacks: {
  177. label: function(tooltipItem) {
  178. return tooltipItem.yLabel;
  179. }
  180. }
  181. }
  182. };
  183. var netOptions = {
  184. maintainAspectRatio: true,
  185. responsive: true,
  186. spanGaps: false,
  187. elements: {
  188. line: {
  189. tension: 0.000001
  190. }
  191. },
  192. plugins: {
  193. filler: {
  194. propagate: false
  195. },
  196. },
  197. scales: {
  198. x: {
  199. grid: {
  200. color: "rgba(167, 79, 1, 0.2)"
  201. }
  202. },
  203. y: {
  204. min: Math.min.apply(this, getMergedRxTxDataset()),
  205. max: Math.max.apply(this, getMergedRxTxDataset()) + 5,
  206. grid: {
  207. color: "rgba(167, 79, 1, 0.2)"
  208. },
  209. ticks: {
  210. display: false,
  211. }
  212. }
  213. },
  214. legend: {
  215. display: false,
  216. },
  217. tooltips: {
  218. callbacks: {
  219. label: function(tooltipItem) {
  220. return tooltipItem.yLabel;
  221. }
  222. }
  223. }
  224. };
  225. initInfo();
  226. chartInit();
  227. //Special code to handle embedding into the System Setting embedded windows
  228. var insideIframe = false;
  229. if (parent.managerInIframe !== undefined && parent.managerInIframe == true){
  230. $(window).on("click", function(e){
  231. parent.ao_module_focus();
  232. });
  233. }
  234. function initInfo(){
  235. $.get("../../system/info/getCPUinfo", function(data){
  236. var data = JSON.parse(data);
  237. console.log(data);
  238. $("#CPUname").text(data.Model);
  239. if (parseFloat(data.Freq) > 1000){
  240. $("#cpufreq").text((data.Freq/1000).toFixed(2) + " Ghz");
  241. }else{
  242. $("#cpufreq").text(data.Freq + " Mhz");
  243. }
  244. });
  245. $.get("../../system/info/getRAMinfo", function(data){
  246. //Return ram in byte
  247. var ramsize = bytesToSize(data);
  248. $("#RAMInfo").text(ramsize);
  249. })
  250. }
  251. function bytesToSize(bytes) {
  252. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  253. if (bytes == 0) return '0 Byte';
  254. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  255. return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
  256. }
  257. function bitToSize(bytes) {
  258. var sizes = ['b', 'Kb', 'Mb', 'Gb', 'Tb'];
  259. if (bytes == 0) return '0 b';
  260. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  261. return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
  262. }
  263. function chartInit(){
  264. cpuChart = new Chart('cpuChart', {
  265. type: 'line',
  266. data: {
  267. labels: [],
  268. datasets: [{
  269. backgroundColor: "rgba(241,246,250,0.4)",
  270. borderColor: "#4c9dcb",
  271. data: [],
  272. radius: 0,
  273. borderWidth: 2,
  274. fill: 'start'
  275. }]
  276. },
  277. options: options
  278. });
  279. //Push 60 empty data into the chart
  280. for (var i =0; i < 60; i++){
  281. addData(cpuChart, "",0)
  282. }
  283. //Create RAM Chart
  284. ramChart = new Chart('ramChart', {
  285. type: 'line',
  286. data: {
  287. labels: [],
  288. datasets: [{
  289. backgroundColor: "rgba(244,242,244,0.4)",
  290. borderColor: "#9528b4",
  291. data: [],
  292. radius: 0,
  293. borderWidth: 2,
  294. fill: 'start'
  295. }]
  296. },
  297. options: ramOptions
  298. });
  299. for (var i =0; i < 60; i++){
  300. addData(ramChart, "",0)
  301. }
  302. //Create Network Chart
  303. netChart = new Chart('netChart', {
  304. type: 'line',
  305. data: {
  306. labels: [],
  307. datasets: [{
  308. backgroundColor: "rgba(252,243,235,0.4)",
  309. borderColor: "#a74f01",
  310. data: [],
  311. radius: 0,
  312. borderWidth: 2,
  313. fill: 'start'
  314. },
  315. {
  316. backgroundColor: "rgba(252,243,235,0.2)",
  317. borderColor: "#a74f01",
  318. borderDash: [3, 3],
  319. data: [],
  320. radius: 0,
  321. borderWidth: 2,
  322. fill: 'start'
  323. }]
  324. },
  325. options: netOptions
  326. });
  327. for (var i =0; i < 60; i++){
  328. addNetData(netChart, "", 0, 0)
  329. }
  330. }
  331. resizeCharts();
  332. $(window).on("resize", function(){
  333. resizeCharts();
  334. })
  335. function resizeCharts(){
  336. $("#cpuChart").width($("#cpuChartContainer").width());
  337. $("#ramChart").width($("#ramChartContainer").width());
  338. }
  339. updateData();
  340. function updateData(){
  341. //Testing use
  342. /*
  343. setInterval(function(){
  344. addAndShiftChartDate(cpuChart, "", Math.floor(Math.random() * 50) + 20)
  345. addAndShiftChartDate(ramChart, "", Math.floor(Math.random() * 10) + 30)
  346. }, 1000)
  347. */
  348. //Calculate the bandwidth diff
  349. $.get("../../system/network/getNICUsage", function(data){
  350. if (data.error !== undefined){
  351. //Error
  352. console.log(data.error);
  353. $("#netGraphScale").text(data.error);
  354. return;
  355. }
  356. if (previousNetData[0] == 0 && previousNetData[1] == 0){
  357. //Not initiated. Set base and wait for next iteration
  358. previousNetData = [data.RX, data.TX];
  359. }else{
  360. var rxd = data.RX - previousNetData[0];
  361. var txd = data.TX - previousNetData[1];
  362. previousNetData = [data.RX, data.TX];
  363. addAndShiftNetworkData(netChart, "", rxd, txd);
  364. $("#rx").text(bitToSize(rxd)+"/s");
  365. $("#tx").text(bitToSize(txd)+"/s");
  366. //Get the max value of the diagram, round it to the cloest 10x
  367. var chartMaxValue = Math.max.apply(this, getMergedRxTxDataset()) * 1.2;
  368. //Special Rounding for calculating graph scale
  369. baseValue = parseInt(chartMaxValue);
  370. var scale = "0 bps"
  371. var sizes = ['b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb'];
  372. function roundUpNearest(num) {
  373. return Math.ceil(num / 10) * 10;
  374. }
  375. if (baseValue == 0){
  376. }else{
  377. var i = parseInt(Math.floor(Math.log(baseValue) / Math.log(1000)));
  378. scale = roundUpNearest((baseValue / Math.pow(1024, i)).toFixed(0))
  379. scale += ' ' + sizes[i] + "ps";
  380. }
  381. //console.log(baseValue, chartMaxValue, scale);
  382. $("#netGraphScale").text(scale);
  383. }
  384. })
  385. $.get("../../system/info/getUsageInfo", function(data){
  386. //Update graph
  387. addAndShiftChartDate(cpuChart, "", data.CPU);
  388. addAndShiftChartDate(ramChart, "", data.RamUsage);
  389. //Update values
  390. $("#cpuUsage").text(data.CPU.toFixed(1) + "%");
  391. $("#ramUsedPercentage").text(data.RamUsage.toFixed(1) + "%")
  392. $("#ramUsed").text(data.UsedRAM);
  393. $("#ramTotal").text(data.TotalRam);
  394. setTimeout(function(){
  395. updateData();
  396. }, 1000);
  397. });
  398. }
  399. function addNetData(chart, label, rx, tx) {
  400. chart.data.labels.push(label);
  401. chart.data.datasets[0].data.push(rx);
  402. chart.data.datasets[1].data.push(tx);
  403. chart.update();
  404. }
  405. function addData(chart, label, data) {
  406. chart.data.labels.push(label);
  407. chart.data.datasets.forEach((dataset) => {
  408. dataset.data.push(data);
  409. });
  410. chart.update();
  411. }
  412. function addAndShiftChartDate(chart, label, newdata) {
  413. chart.data.labels.splice(0, 1); // remove first label
  414. chart.data.datasets.forEach(function(dataset) {
  415. dataset.data.splice(0, 1); // remove first data point
  416. });
  417. chart.update();
  418. // Add new data
  419. chart.data.labels.push(label); // add new label at end
  420. chart.data.datasets.forEach(function(dataset, index) {
  421. dataset.data.push(newdata); // add new data at end
  422. });
  423. chart.update();
  424. }
  425. function addAndShiftNetworkData(chart, label, rxd, txd) {
  426. chart.data.labels.splice(0, 1); // remove first label
  427. chart.data.datasets.forEach(function(dataset) {
  428. dataset.data.splice(0, 1); // remove first data point
  429. });
  430. chart.update();
  431. // Add new data
  432. chart.data.labels.push(label); // add new label at end
  433. chart.data.datasets[0].data.push(rxd);
  434. chart.data.datasets[1].data.push(txd);
  435. //Update the sacle as well
  436. netChart.options.scales.y.min = Math.min.apply(this, getMergedRxTxDataset());
  437. netChart.options.scales.y.max = Math.max.apply(this, getMergedRxTxDataset()) *1.2;
  438. chart.update();
  439. }
  440. function getMergedRxTxDataset(){
  441. if (netChart == undefined){
  442. return [0, 100];
  443. }
  444. var newArr = [];
  445. newArr = newArr.concat(netChart.data.datasets[0].data,netChart.data.datasets[1].data);
  446. return newArr;
  447. }
  448. </script>
  449. </body>
  450. </html>