Mercurial Hosting > nabble
comparison src/nabble/view/web/tools/Index.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 22a701699b2a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 | |
2 package nabble.view.web.tools; | |
3 | |
4 import fschmidt.util.java.IoUtils; | |
5 import fschmidt.util.servlet.ConnectionLimitFilter; | |
6 import nabble.model.Executors; | |
7 import nabble.model.ModelHome; | |
8 import nabble.model.ViewCount; | |
9 import nabble.model.export.Export; | |
10 import nabble.view.lib.UrlMappable; | |
11 import nabble.view.web.template.NabbleNamespace; | |
12 import nabble.view.web.user.OnlineStatus; | |
13 import org.slf4j.Logger; | |
14 import org.slf4j.LoggerFactory; | |
15 | |
16 import javax.servlet.ServletException; | |
17 import javax.servlet.http.HttpServlet; | |
18 import javax.servlet.http.HttpServletRequest; | |
19 import javax.servlet.http.HttpServletResponse; | |
20 import java.io.IOException; | |
21 import java.io.InputStream; | |
22 import java.io.PrintWriter; | |
23 import java.text.DecimalFormat; | |
24 import java.util.Collections; | |
25 import java.util.Date; | |
26 import java.util.Map; | |
27 import java.util.Set; | |
28 import java.util.regex.Pattern; | |
29 | |
30 | |
31 public final class Index extends HttpServlet implements UrlMappable { | |
32 | |
33 private static final Logger logger = LoggerFactory.getLogger(Index.class); | |
34 | |
35 private static final DecimalFormat FORMATTER = new DecimalFormat("0.00"); | |
36 private static final Pattern URL_PATTERN = Pattern.compile("://[^/]+/tools/$"); | |
37 | |
38 private static String path() { | |
39 return "/tools/"; | |
40 } | |
41 | |
42 public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) { | |
43 return Collections.emptyMap(); | |
44 } | |
45 | |
46 public Pattern getUrlPattern() { | |
47 return URL_PATTERN; | |
48 } | |
49 | |
50 protected void service(HttpServletRequest request,HttpServletResponse response) | |
51 throws ServletException, IOException | |
52 { | |
53 PrintWriter out = response.getWriter(); | |
54 float oneMega = 1024f * 1024f; | |
55 float free = Runtime.getRuntime().freeMemory() / oneMega; | |
56 float total = Runtime.getRuntime().totalMemory() / oneMega; | |
57 float used = total - free; | |
58 | |
59 String loadAverage = null; | |
60 try { | |
61 Process process = Runtime.getRuntime().exec("uptime"); | |
62 InputStream input = process.getInputStream(); | |
63 byte[] result = IoUtils.readAll(input); | |
64 input.close(); | |
65 loadAverage = new String(result).replaceAll(".*average:",""); | |
66 } catch(IOException e) {} // not on linux | |
67 | |
68 Map<java.lang.Thread, java.lang.StackTraceElement[]> m = Thread.getAllStackTraces(); | |
69 int socketRead = 0; | |
70 int idleThread = 0; | |
71 int sleepingThread = 0; | |
72 for (Map.Entry<Thread, StackTraceElement[]> entry : m.entrySet()) { | |
73 StackTraceElement[] trace = entry.getValue(); | |
74 if (trace.length > 0) { | |
75 String firstTrace = trace[0].toString(); | |
76 if (firstTrace.equals("java.net.SocketInputStream.socketRead0(Native Method)")) | |
77 socketRead++; | |
78 else if (firstTrace.equals("java.lang.Thread.sleep(Native Method)")) | |
79 sleepingThread++; | |
80 else if (firstTrace.equals("java.lang.Object.wait(Native Method)")) | |
81 idleThread++; | |
82 } | |
83 } | |
84 | |
85 long viewCountDiffMillis = System.currentTimeMillis() - ViewCount.lastSaved; | |
86 float minutesSinceLastSaved = viewCountDiffMillis / 60000f; | |
87 | |
88 long digestDiffMillis = System.currentTimeMillis() - ModelHome.lastDigestRun; | |
89 float hoursSinceLastDigestRun = digestDiffMillis / 3600000f; | |
90 | |
91 out.print( "\r\n<html>\r\n <head>\r\n <title>Nabble tools</title>\r\n <style type=\"text/css\">\r\n .gray {\r\n background-color: #eeeeee;\r\n padding: .5em;\r\n }\r\n p { padding-left: .5em; }\r\n td.row-label {\r\n font-weight:bold;\r\n padding: .1em .4em .1em 0;\r\n }\r\n td.row-separator { padding-top: 1em; }\r\n\r\n td.category {\r\n font-variant:small-caps;\r\n text-align:center;\r\n padding: 0 .5em;\r\n }\r\n </style>\r\n <script type=\"text/javascript\">\r\n var months = [\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"];\r\n function fmt(i) { return i <= 9? \"0\" + i : i; };\r\n function formatTime(date) {\r\n var hours = date.getHours();\r\n if( hours < 12 ) {\r\n var xm = \"am\";\r\n if (hours==0)\r\n hours = 12;\r\n } else {\r\n var xm = \"pm\";\r\n if (hours > 12)\r\n hours -= 12;\r\n }\r\n return fmt(hours) + \":\" + fmt(date.getMinutes()) + xm;\r\n };\r\n function formatDate(date) {\r\n return months[date.getMonth()] + \" \" + fmt(date.getDate()) + \", \" + date.getFullYear();\r\n };\r\n function formatDateTime(date) {\r\n return formatDate(date) + \"; \" + formatTime(date);\r\n };\r\n </script>\r\n </head>\r\n <body style=\"font: .8em Verdana, Serif;\">\r\n <p class=\"gray\">\r\n Built time =\r\n <b>\r\n <script type=\"text/javascript\">\r\n document.write(formatDateTime(new Date(" ); | |
92 out.print( (new Date(ClassLoader.getSystemResource("nabble/view/web/Index.class").openConnection().getLastModified()).getTime()) ); | |
93 out.print( ")));\r\n </script>\r\n </b>\r\n </p>\r\n\r\n <table style=\"font-size: 1em;\">\r\n <tr>\r\n <td class=\"category\" style=\"background-color:#fafafa;\">General</td>\r\n <td style=\"padding:.5em 0\">\r\n <p><a href=\"Admin.jtp\">Caches</a></p>\r\n <p><a href=\"shell.luan\">Shell</a> (<a href=\"ShellHelp.jtp\">\r\n <small>help</small>\r\n </a>)\r\n </p>\r\n <p><a href=\"run.luan\">Run Batch</a></p>\r\n <p><a href=\"SendMail.jtp\">Send mail</a></p>\r\n </td>\r\n </tr>\r\n <tr>\r\n <td class=\"category\" style=\"background-color:#e0e0e0;\">Sites</td>\r\n <td style=\"padding:.5em 0\">\r\n <p><a href=\"AdminNotice.jtp\">Administrator Notice</a></p>\r\n </td>\r\n </tr>\r\n <tr>\r\n <td class=\"category\" style=\"background-color:#d9d9d9;\">Others</td>\r\n <td style=\"padding:.5em 0\">\r\n <p><a href=\"TestMacro.jtp\">Test Macro</a></p>\r\n <p><a href=\"Profile.jtp\">Profiling</a></p>\r\n <p><a href=\"/tools2\">Generic tools</a></p>\r\n <p><a href=\"OnlineUsers.jtp\">Online Users</a></p>\r\n </td>\r\n </tr>\r\n </table>\r\n\r\n <div class=\"gray\" style=\"margin: 1em 0\">\r\n <table style=\"font-size:100%\">\r\n <tr>\r\n <td class=\"row-label\">Free Memory</td>\r\n <td>\r\n " ); | |
94 out.print( (String.format("%.2f",free)) ); | |
95 out.print( " Mb\r\n </td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Used Memory</td>\r\n <td>" ); | |
96 out.print( (String.format("%.2f",used)) ); | |
97 out.print( " Mb</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Total Memory</td>\r\n <td>" ); | |
98 out.print( (String.format("%.2f",total)) ); | |
99 out.print( " Mb</td>\r\n </tr>\r\n " ); | |
100 if (loadAverage != null) { | |
101 out.print( "\r\n <tr>\r\n <td class=\"row-label\">Load Average</td>\r\n <td>" ); | |
102 out.print( (loadAverage) ); | |
103 out.print( "</td>\r\n </tr>\r\n " ); | |
104 } | |
105 out.print( "\r\n <tr>\r\n <td class=\"row-label row-separator\">Pool Active Count</td>\r\n <td class=\"row-separator\">" ); | |
106 out.print( (Executors.foregroundExecutor.getActiveCount()) ); | |
107 out.print( " threads</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Pool Size</td>\r\n <td>" ); | |
108 out.print( (Executors.foregroundExecutor.getPoolSize()) ); | |
109 out.print( " threads</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Connection Limit</td>\r\n <td>" ); | |
110 out.print( (ConnectionLimitFilter.counter.get()) ); | |
111 out.print( "/" ); | |
112 out.print( (ConnectionLimitFilter.max) ); | |
113 out.print( " (" ); | |
114 out.print( (ConnectionLimitFilter.queue.size()) ); | |
115 out.print( ")</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Queue</td>\r\n <td>" ); | |
116 out.print( (Executors.foregroundExecutor.getQueueSize()) ); | |
117 out.print( " connections</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label row-separator\">Thread Count</td>\r\n <td class=\"row-separator\">" ); | |
118 out.print( (m.size()) ); | |
119 out.print( " threads</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Idle Threads</td>\r\n <td>" ); | |
120 out.print( (idleThread) ); | |
121 out.print( " threads are idle</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Database Threads</td>\r\n <td>" ); | |
122 out.print( (socketRead) ); | |
123 out.print( " socket reading</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Sleeping Threads</td>\r\n <td>" ); | |
124 out.print( (sleepingThread) ); | |
125 out.print( " threads are sleeping</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label row-separator\">Exporting</td>\r\n <td class=\"row-separator\">[" ); | |
126 out.print( (csv(Export.exportSiteIds)) ); | |
127 out.print( "]</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Backups Running</td>\r\n <td>[" ); | |
128 out.print( (csv(NabbleNamespace.sitesRunningBackup)) ); | |
129 out.print( "]</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Online users</td>\r\n <td>" ); | |
130 out.print( (OnlineStatus.getOnlineStats()) ); | |
131 out.print( "</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Views Last Saved</td>\r\n <td style=\"color:#" ); | |
132 out.print( (minutesSinceLastSaved > 2f? "red":"black") ); | |
133 out.print( "\">" ); | |
134 out.print( (FORMATTER.format(minutesSinceLastSaved)) ); | |
135 out.print( " minutes ago</td>\r\n </tr>\r\n <tr>\r\n <td class=\"row-label\">Last Digest Run</td>\r\n <td style=\"color:#" ); | |
136 out.print( (hoursSinceLastDigestRun > 24f? "red":"black") ); | |
137 out.print( "\">" ); | |
138 out.print( (FORMATTER.format(hoursSinceLastDigestRun)) ); | |
139 out.print( " hours ago</td>\r\n </tr>\r\n </table>\r\n </div>\r\n </body>\r\n</html>\r\n" ); | |
140 | |
141 } | |
142 | |
143 private String csv(Set<Long> ids) { | |
144 Long[] idArray = ids.toArray(new Long[0]); | |
145 StringBuilder buf = new StringBuilder(); | |
146 for (long id : idArray) { | |
147 if (buf.length() > 0) | |
148 buf.append(", "); | |
149 buf.append(id); | |
150 } | |
151 return buf.toString(); | |
152 } | |
153 } | |
154 |