1
2
3
4
5 package org.astrogrid.monitor;
6
7 import java.io.IOException;
8 import java.io.Writer;
9 import org.astrogrid.status.ServiceStatus;
10 import org.astrogrid.status.TaskStatus;
11
12 /***
13 * Serves body-html (to include within the body html element) containing server
14 * and tasks status. For example, include it in a JSP that has the correct
15 * headers and footers for the application.
16 *
17 * @author mch
18 */
19 public class ServerStatusHtmlRenderer {
20
21 public void writeHtmlStatus(Writer out, ServiceStatus status, TaskStatus[] persistedStatus) throws IOException {
22 out.write(
23 "<p>Started: "+status.getStarted()+"</p>"+
24 "<h2>Memory</h2>"+
25 "<p><table>"+
26 "<tr><td align='right'>Free<td><td align='right'>"+status.getFreeMemory()+"</td></tr>"+
27 "<tr><td align='right'>Max<td><td align='right'>"+status.getMaxMemory()+"</td></tr>"+
28 "<tr><td align='right'>Total<td><td align='right'>"+status.getTotalMemory()+"</td></tr>"+
29 "</table></p>");
30
31 TaskStatus[] tasks = status.getTasks();
32
33 out.write(
34 "<h2>Tasks ("+(tasks.length)+")</h2>\n"+
35 "<table>");
36
37 if (tasks.length>0) {
38
39 out.write(
40 "<tr>"+
41 "<th>Query</th>"+
42 "<th>Started</th>"+
43 "<th>Time</th>"+
44 "<th>Owner</th>"+
45 "<th>State</th>"+
46 "<th>Size</th>"+
47 "<th>Source</th>"+
48 "</tr>\n");
49
50 for (int i=0;i<tasks.length;i++) {
51 writeTaskStatusRow(out, tasks[i]);
52 }
53
54 out.write(
55 "</table></p>");
56 }
57
58 if ((persistedStatus != null) && (persistedStatus.length>0)) {
59
60 out.write(
61 "<h2>History</h2>\n"+
62 "<table>"+
63 "<tr>"+
64 "<th>Query</th>"+
65 "<th>Started</th>"+
66 "<th>Time</th>"+
67 "<th>Owner</th>"+
68 "<th>State</th>"+
69 "<th>Size</th>"+
70 "<th>Source</th>"+
71 "</tr>\n");
72
73
74 for (int i = persistedStatus.length-1; i >=0 ; i--)
75 {
76 writeTaskStatusRow(out, persistedStatus[i]);
77 }
78
79 out.write("</table></p>");
80
81 out.flush();
82 }
83 }
84
85 /*** Creates HTML for displaying a task in a row in a table */
86 public void writeTaskStatusRow(Writer out, TaskStatus task) throws IOException {
87 String bg = "#FFFFFF";
88 String fg = "#000000";
89 if (task.getStage().equals(TaskStatus.ABORTED)) { bg = "#FFFFAA"; }
90 if (task.getStage().equals(TaskStatus.ERROR)) { bg = "#FFAAAA"; }
91 if (task.getStage().equals(TaskStatus.RUNNING)) { bg = "#AAFFAA"; }
92 if (task.getStage().equals(TaskStatus.COMPLETE)) { bg = "#AAAAFF"; }
93
94
95 out.write("<tr bgcolor='"+bg+"' fgcolor='"+fg+"'>");
96 out.write("<td><a href='queryStatus.jsp?ID="+task.getId()+"'>"+task.getId()+"</a></td>");
97 out.write("<td nowrap>"+task.getFirst().getTimestamp()+"</td>");
98 if (task.getFirst() == task) {
99 out.write("<td>Unknown</td>");
100 }
101 else {
102 long timeTaken = task.getTimestamp().getTime()-task.getFirst().getTimestamp().getTime();
103 int s = (int) timeTaken/1000;
104 int m = (int) s/60;
105 int h = (int) m/60;
106 out.write("<td nowrap>"+h+":"+padZero(m % 60,2) +":" + padZero(s % 60,2)+"."+padZero(timeTaken % 1000,3) +"</td>");
107 }
108 out.write("<td nowrap>"+task.getOwner().getName()+"</td>");
109 out.write("<td>"+task.getStage()+"</td>");
110
111
112 if (task.getProgressMax()>0) {
113 out.write("<td>"+task.getProgressMax()+"</td>");
114 }
115 else {
116 out.write("<td></td>");
117 }
118
119
120 out.write("<td nowrap>"+emptyNuller(task.getSource())+"</td>");
121
122
123 if (!task.isFinished()) {
124 out.write("<td class='button'><a href='./AttemptAbort?ID="+task.getId()+"'>Abort</a></td>");
125 }
126 out.write("</tr>\n");
127 }
128
129 private String padZero(long i, int digits) {
130 String s = "00000000000"+i;
131 return s.substring(s.length()-digits);
132 }
133
134 private String emptyNuller(String s) {
135 if (s==null) {
136 return "";
137 }
138 else {
139 return s;
140 }
141 }
142
143 }
144