You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
9.2 KiB

  1. // import { useState } from 'react';
  2. // material-ui
  3. import {
  4. Grid,
  5. Typography,
  6. Stack
  7. } from '@mui/material';
  8. import * as React from "react";
  9. import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
  10. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  11. const JVMDefault = () => {
  12. // const userData = JSON.parse(localStorage.getItem("userData"));
  13. React.useEffect(() => {
  14. localStorage.setItem('searchCriteria',"")
  15. }, [])
  16. const BackgroundHead = {
  17. backgroundImage: `url(${titleBackgroundImg})`,
  18. width: '100%',
  19. height: '100%',
  20. backgroundSize:'contain',
  21. backgroundRepeat: 'no-repeat',
  22. backgroundColor: '#0C489E',
  23. backgroundPosition: 'right'
  24. }
  25. const htmlContent = `
  26. <style>
  27. pre {
  28. background-color: #333333; /* Dark gray background */
  29. color: white; /* White text */
  30. padding: 15px; /* Add some padding */
  31. border-radius: 5px; /* Rounded corners */
  32. overflow-x: auto; /* Add horizontal scroll if needed */
  33. width: 90%; /* 90% width */
  34. }
  35. </style>
  36. <div>
  37. <p>Certainly! Here's the complete implementation, showing both the updated Java controller to provide JVM
  38. information in MB, and the corresponding UI code that will display the information.
  39. </p>
  40. <h2>Step 1: Java Code (Controller)</h2>
  41. <p>This Spring Boot controller exposes the JVM information (including heap memory usage, operating system
  42. details, and uptime) in MB, and it provides it via a REST API (/jvm-info).
  43. <p>
  44. <pre>
  45. import org.springframework.web.bind.annotation.GetMapping;
  46. import org.springframework.web.bind.annotation.RestController;
  47. import java.lang.management.ManagementFactory;
  48. import java.lang.management.MemoryMXBean;
  49. import java.lang.management.MemoryUsage;
  50. import java.lang.management.OperatingSystemMXBean;
  51. import java.util.HashMap;
  52. import java.util.Map;
  53. @RestController
  54. public class JvmInfoController {
  55. @GetMapping("/jvm-info")
  56. public Map<String, Object> getJvmInfo() {
  57. Map<String, Object> jvmInfo = new HashMap<>();
  58. // Get the Operating System MXBean for system-related info
  59. OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
  60. jvmInfo.put("osName", osBean.getName());
  61. jvmInfo.put("osVersion", osBean.getVersion());
  62. jvmInfo.put("osArch", osBean.getArch());
  63. // Get memory info and convert bytes to MB
  64. MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
  65. MemoryUsage heapMemoryUsage = memoryBean.getHeapMemoryUsage();
  66. jvmInfo.put("heapMemoryInitMB", bytesToMB(heapMemoryUsage.getInit()));
  67. jvmInfo.put("heapMemoryUsedMB", bytesToMB(heapMemoryUsage.getUsed()));
  68. jvmInfo.put("heapMemoryMaxMB", bytesToMB(heapMemoryUsage.getMax()));
  69. jvmInfo.put("heapMemoryCommittedMB", bytesToMB(heapMemoryUsage.getCommitted()));
  70. // Get JVM uptime and other stats
  71. long uptime = ManagementFactory.getRuntimeMXBean().getUptime();
  72. jvmInfo.put("jvmUptimeMillis", uptime);
  73. return jvmInfo;
  74. }
  75. // Helper function to convert bytes to MB
  76. private long bytesToMB(long bytes) {
  77. return bytes / (1024 * 1024);
  78. }
  79. }
  80. </pre>
  81. <h2>Step 2: Frontend UI (HTML + JavaScript)</h2>
  82. <p>Here’s the frontend HTML page that will display the JVM information in a clean and readable format. It
  83. uses JavaScript (fetch API) to get data from the /jvm-info endpoint and display it in a pre block
  84. </p>
  85. <pre>
  86. &lt;!DOCTYPE html&gt;
  87. &lt;html lang="en"&gt;
  88. &lt;head&gt;
  89. &lt;meta charset="UTF-8"&gt;
  90. &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  91. &lt;title&gt;JVM Information&lt;/title&gt;
  92. &lt;style&gt;
  93. body {
  94. font-family: Arial, sans-serif;
  95. margin: 20px;
  96. }
  97. h1 {
  98. color: #333;
  99. }
  100. pre {
  101. background-color: #f4f4f4;
  102. padding: 10px;
  103. border-radius: 5px;
  104. font-size: 14px;
  105. line-height: 1.6;
  106. color: #333;
  107. max-height: 400px;
  108. overflow-y: scroll;
  109. }
  110. .container {
  111. max-width: 800px;
  112. margin: 0 auto;
  113. }
  114. &lt;/style&gt;
  115. &lt;script&gt;
  116. function fetchJvmInfo() {
  117. fetch('/jvm-info')
  118. .then(response =&gt; response.json())
  119. .then(data =&gt; {
  120. document.getElementById("jvmInfo").innerHTML = JSON.stringify(data, null, 2);
  121. })
  122. .catch(error =&gt; console.error('Error fetching JVM info:', error));
  123. }
  124. window.onload = fetchJvmInfo;
  125. &lt;/script&gt;
  126. &lt;/head&gt;
  127. &lt;body&gt;
  128. &lt;div class="container"&gt;
  129. &lt;h1&gt;JVM Information&lt;/h1&gt;
  130. &lt;pre id="jvmInfo"&gt;Loading...&lt;/pre&gt;
  131. &lt;/div&gt;
  132. &lt;/body&gt;
  133. &lt;/html&gt;
  134. </pre>
  135. <h2>Breakdown of the Information Displayed</h2>
  136. <h3>1. Operating System Info:</h3>
  137. <ul>
  138. <li><strong>osName:</strong> The name of the operating system (e.g., Windows, Linux)</li>
  139. <li><strong>osVersion:</strong> The version of the operating system</li>
  140. <li><strong>osArch:</strong> The architecture of the operating system (e.g., x86_64)</li>
  141. </ul>
  142. <h3>2. Memory Usage Info:</h3>
  143. <ul>
  144. <li><strong>heapMemoryInitMB:</strong> The initial heap memory available to the JVM (in MB)</li>
  145. <li><strong>heapMemoryUsedMB:</strong> The current memory being used by the JVM (in MB)</li>
  146. <li><strong>heapMemoryMaxMB:</strong> The maximum heap memory that the JVM can use (in MB)</li>
  147. <li><strong>heapMemoryCommittedMB:</strong> The amount of memory committed by the JVM (in MB)</li>
  148. </ul>
  149. <h3>3. JVM Uptime:</h3>
  150. <ul>
  151. <li><strong>jvmUptimeMillis:</strong> The time the JVM has been running (in milliseconds)</li>
  152. </ul>
  153. <h2>Example JSON Output (after fetching data):</h2>
  154. <p>If the /jvm-info API returns the following JSON, it will be displayed on the UI:</p>
  155. <pre>
  156. {
  157. "osName": "Windows 10",
  158. "osVersion": "10.0",
  159. "osArch": "amd64",
  160. "heapMemoryInitMB": 128,
  161. "heapMemoryUsedMB": 50,
  162. "heapMemoryMaxMB": 1024,
  163. "heapMemoryCommittedMB": 512,
  164. "jvmUptimeMillis": 123456789
  165. }
  166. </pre>
  167. <h2>UI Display Example</h2>
  168. <p>On the webpage, this information will be displayed as follows in a neatly formatted block:</p>
  169. <pre>
  170. JVM Information
  171. {
  172. "osName": "Windows 10",
  173. "osVersion": "10.0",
  174. "osArch": "amd64",
  175. "heapMemoryInitMB": 128,
  176. "heapMemoryUsedMB": 50,
  177. "heapMemoryMaxMB": 1024,
  178. "heapMemoryCommittedMB": 512,
  179. "jvmUptimeMillis": 123456789
  180. }
  181. </pre>
  182. <h2>Styling and Functionality:</h2>
  183. <ol>
  184. <li><strong>Styling:</strong>The page has simple, clean styling with a scrollable pre block to display the JSON output in an organized manner. You can easily tweak the look and feel using CSS.</li>
  185. <li><strong>Dynamic Fetching:</strong>When the page loads, it automatically fetches the JVM information from the backend and updates the UI.</li>
  186. </ol>
  187. <h2>Optional Enhancements</h2>
  188. <ol>
  189. <li><strong>Graphical Visualization:</strong> You could use a library like Chart.js to visualize memory usage in graphs (e.g., pie charts for heap memory)</li>
  190. <li><strong>Auto-Refresh:</strong> You can add a refresh button or periodically fetch the data at a set interval (e.g., every 30 seconds) using setInterval() in JavaScript</li>
  191. </ol>
  192. <p>Let me know if you need further help with customization or any other features!</p>
  193. </div>
  194. `;
  195. return (
  196. <Grid container sx={{minHeight: '87vh', backgroundColor: "backgroundColor.default"}} direction="column">
  197. <Grid item xs={12}>
  198. <div style={BackgroundHead}>
  199. <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
  200. <Typography ml={15} color='#FFF' variant="h4" sx={{ "textShadow": "0px 0px 25px #0C489E" }}>
  201. JVM Information
  202. </Typography>
  203. </Stack>
  204. </div>
  205. </Grid>
  206. <Grid item xs={12} ml={15} mb={2}>
  207. <div dangerouslySetInnerHTML={{ __html: htmlContent }} />
  208. </Grid>
  209. </Grid>
  210. );
  211. };
  212. export default JVMDefault;