Stat collection and display done
[jabaws.git] / webservices / compbio / stat / servlet / Joblist.java
1 package compbio.stat.servlet;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.sql.SQLException;\r
6 import java.sql.Timestamp;\r
7 \r
8 import javax.servlet.RequestDispatcher;\r
9 import javax.servlet.ServletException;\r
10 import javax.servlet.http.HttpServlet;\r
11 import javax.servlet.http.HttpServletRequest;\r
12 import javax.servlet.http.HttpServletResponse;\r
13 import javax.servlet.http.HttpSession;\r
14 \r
15 import compbio.engine.conf.PropertyHelperManager;\r
16 import compbio.stat.collector.StatDB;\r
17 import compbio.stat.collector.StatProcessor;\r
18 import compbio.util.PropertyHelper;\r
19 import compbio.util.Util;\r
20 import compbio.ws.client.Services;\r
21 \r
22 public class Joblist extends HttpServlet {\r
23 \r
24         static final String JT_FAILED = "failed";\r
25         static final String JT_ABANDONED = "abandoned";\r
26         static final String JT_CANCELLED = "cancelled";\r
27         static final String JT_ALL = "all";\r
28         static final String JT_INCOMPLETE = "incomplete";\r
29         /**\r
30          * Input:\r
31          * \r
32          * ws=${ws.key}\r
33          * \r
34          * where=everywhere cluster local\r
35          * \r
36          * type=cancelled all incomplete\r
37          * \r
38          * from=${startDate}\r
39          * \r
40          * to=${stopDate}\r
41          * \r
42          */\r
43         @Override\r
44         protected void doGet(HttpServletRequest req, HttpServletResponse resp)\r
45                         throws ServletException, IOException {\r
46                 /*\r
47                  * Values for this servlet are not user supplied, so do not bother with\r
48                  * nice error messages just throw the exception is something is wrong!\r
49                  */\r
50                 String wsname = req.getParameter("ws");\r
51                 Services wservice = Services.getService(wsname);\r
52                 if (wservice == null) {\r
53                         throw new ServletException(\r
54                                         "Webservice name 'ws' is not specified or is incorrect. Given value:"\r
55                                                         + wsname);\r
56                 }\r
57                 String executor = req.getParameter("where");\r
58                 if (Util.isEmpty(executor)) {\r
59                         throw new ServletException("'Where' is not specified!");\r
60                 }\r
61                 if (!(executor.equalsIgnoreCase("everywhere")\r
62                                 || executor.equalsIgnoreCase("local") || executor\r
63                                 .equalsIgnoreCase("cluster"))) {\r
64                         throw new ServletException("Invalid 'where' value '" + executor\r
65                                         + "' can be one of 'everywhere', 'local', 'cluster'!");\r
66                 }\r
67                 Boolean where = null;\r
68                 if (executor.equalsIgnoreCase("local")) {\r
69                         where = false;\r
70                 } else if (executor.equalsIgnoreCase("cluster")) {\r
71                         where = true;\r
72                 }\r
73 \r
74                 String jobtype = req.getParameter("type");\r
75                 if (Util.isEmpty(executor)) {\r
76                         throw new ServletException("'type' is not specified!");\r
77                 }\r
78                 if (!(jobtype.equalsIgnoreCase(JT_CANCELLED)\r
79                                 || jobtype.equalsIgnoreCase(JT_ALL)\r
80                                 || jobtype.equalsIgnoreCase(JT_INCOMPLETE)\r
81                                 || jobtype.equalsIgnoreCase(JT_ABANDONED) || jobtype\r
82                                 .equalsIgnoreCase(JT_FAILED))) {\r
83                         throw new ServletException("Invalid 'jobtype' value '" + jobtype\r
84                                         + "' can be one of 'cancelled', 'all', 'incomplete', "\r
85                                         + "'failed', 'abandoned'!");\r
86                 }\r
87                 String fromDate = req.getParameter("from");\r
88                 if (Util.isEmpty(fromDate)) {\r
89                         throw new ServletException("'fromDate' is not specified!");\r
90                 }\r
91                 String toDate = req.getParameter("to");\r
92                 if (Util.isEmpty(toDate)) {\r
93                         throw new ServletException("'toDate' is not specified!");\r
94                 }\r
95 \r
96                 PropertyHelper helper = PropertyHelperManager.getPropertyHelper();\r
97                 String clusterTempDir = helper.getProperty("cluster.tmp.directory")\r
98                                 .trim();\r
99                 clusterTempDir = new File(clusterTempDir).getName();\r
100                 String localTempDir = helper.getProperty("local.tmp.directory").trim();\r
101 \r
102                 Timestamp startDate = new Timestamp(Long.parseLong(fromDate));\r
103                 Timestamp stopDate = new Timestamp(Long.parseLong(toDate));\r
104                 StatDB statdb = null;\r
105                 try {\r
106                         statdb = new StatDB();\r
107                         StatProcessor stat = new StatProcessor(statdb.readData(startDate,\r
108                                         stopDate, wservice, where));\r
109 \r
110                         HttpSession session = req.getSession();\r
111                         if (jobtype.equalsIgnoreCase(JT_CANCELLED)) {\r
112                                 session.setAttribute("stat",\r
113                                                 new StatProcessor(stat.getCancelledJobs()));\r
114                         } else if (jobtype.equalsIgnoreCase(JT_INCOMPLETE)) {\r
115                                 session.setAttribute("stat",\r
116                                                 new StatProcessor(stat.getIncompleteJobs()));\r
117                         } else if (jobtype.equalsIgnoreCase(JT_ALL)) {\r
118                                 session.setAttribute("stat", stat);\r
119                         } else if (jobtype.equalsIgnoreCase(JT_FAILED)) {\r
120                                 session.setAttribute("stat",\r
121                                                 new StatProcessor(stat.getFailedJobs()));\r
122                         } else if (jobtype.equalsIgnoreCase(JT_ABANDONED)) {\r
123                                 session.setAttribute("stat",\r
124                                                 new StatProcessor(stat.getAbandonedJobs()));\r
125                         } else {\r
126                                 throw new AssertionError("Unrecognised job type: " + jobtype);\r
127                         }\r
128                         session.setAttribute("clusterTemp", clusterTempDir);\r
129                         session.setAttribute("localTemp", localTempDir);\r
130                         req.setAttribute("startDate", startDate.getTime());\r
131                         req.setAttribute("stopDate", stopDate.getTime());\r
132 \r
133                         RequestDispatcher dispatcher = req\r
134                                         .getRequestDispatcher("statpages/Joblist.jsp");\r
135                         dispatcher.forward(req, resp);\r
136 \r
137                 } catch (SQLException e) {\r
138                         e.printStackTrace();\r
139                         throw new ServletException("SQLException : "\r
140                                         + e.getLocalizedMessage(), e);\r
141                 }\r
142 \r
143         }\r
144 }\r