From: pvtroshin Date: Wed, 25 May 2011 17:42:50 +0000 (+0000) Subject: Further work on statistics display X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7d0c30c4d86b904f012ecfe707735c895731beca;p=jabaws.git Further work on statistics display git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4163 e3abac25-378b-4346-85de-24260fe3988d --- diff --git a/webservices/compbio/stat/collector/StatDB.java b/webservices/compbio/stat/collector/StatDB.java index dd7e2b7..c58974b 100644 --- a/webservices/compbio/stat/collector/StatDB.java +++ b/webservices/compbio/stat/collector/StatDB.java @@ -8,6 +8,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Set; @@ -32,15 +33,23 @@ public class StatDB { private static final Logger log = Logger.getLogger(StatDB.class); - Connection conn; - private static Connection getDBConnection() throws SQLException { - String dbpath = PropertyHelperManager.getLocalPath(); - log.info("Looking for JABAWS access statistics database at: " + dbpath); - System.setProperty("derby.system.home", dbpath); - Connection conn = DriverManager.getConnection(protocol + statDBName - + ";create=false"); + static Connection conn; - conn.setAutoCommit(true); + private synchronized static Connection getDBConnection() + throws SQLException { + + if (conn != null && !conn.isClosed()) { + return conn; + } else { + String dbpath = PropertyHelperManager.getLocalPath(); + log.info("Looking for JABAWS access statistics database at: " + + dbpath); + System.setProperty("derby.system.home", dbpath); + conn = DriverManager.getConnection(protocol + statDBName + + ";create=false"); + + conn.setAutoCommit(true); + } return conn; } @@ -142,6 +151,21 @@ public class StatDB { pstm.close(); } + public Date getEarliestRecord() throws SQLException { + String query = "select min(start) from exec_stat"; + Statement st = conn.createStatement(); + ResultSet res = st.executeQuery(query); + boolean exist = res.next(); + Date date = new Date(); + if (exist) { + date = res.getDate(1); + } + + res.close(); + st.close(); + return date; + } + public List readData(Timestamp from, Timestamp to, Services wservice, Boolean clusterOnly) throws SQLException { diff --git a/webservices/compbio/stat/collector/StatManager.java b/webservices/compbio/stat/collector/StatManager.java deleted file mode 100644 index ede4d0c..0000000 --- a/webservices/compbio/stat/collector/StatManager.java +++ /dev/null @@ -1,85 +0,0 @@ -package compbio.stat.collector; - -import java.sql.SQLException; -import java.sql.Timestamp; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.Iterator; -import java.util.Map; -import java.util.TreeMap; - -import compbio.ws.client.Services; - -public class StatManager { - - static class DateRoller implements Iterator { - final Date initDate; - final Calendar calendar; - - public DateRoller(Date date) { - this.initDate = date; - calendar = GregorianCalendar.getInstance(); - calendar.setTime(date); - calendar.add(Calendar.MONTH, -12); - } - - Date getCurrentDate() { - return initDate; - } - - @Override - public boolean hasNext() { - return !calendar.getTime().equals(initDate); - } - - @Override - public Date next() { - calendar.add(Calendar.MONTH, 1); - return calendar.getTime(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - } - - void getStats() throws SQLException { - Calendar startTime = Calendar.getInstance(); - startTime.roll(Calendar.YEAR, false); - Timestamp startDate = new Timestamp(startTime.getTimeInMillis()); - Timestamp stopDate = new Timestamp(new Date().getTime()); - StatDB statdb = null; - - statdb = new StatDB(); - - // Total - Map stats = new TreeMap(); - for (Services service : Services.values()) { - stats.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, null))); - } - - // Cluster - Map statsCluster = new TreeMap(); - for (Services service : Services.values()) { - statsCluster.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, true))); - } - // Local - Map statsLocal = new TreeMap(); - for (Services service : Services.values()) { - statsLocal.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, false))); - } - - } -} diff --git a/webservices/compbio/stat/collector/StatProcessor.java b/webservices/compbio/stat/collector/StatProcessor.java index 8b197f8..2e6d0eb 100644 --- a/webservices/compbio/stat/collector/StatProcessor.java +++ b/webservices/compbio/stat/collector/StatProcessor.java @@ -165,4 +165,9 @@ public class StatProcessor { return report; } + @Override + public String toString() { + return this.reportStat(); + } + } diff --git a/webservices/compbio/stat/servlet/AnnualStat.java b/webservices/compbio/stat/servlet/AnnualStat.java new file mode 100644 index 0000000..b54af52 --- /dev/null +++ b/webservices/compbio/stat/servlet/AnnualStat.java @@ -0,0 +1,44 @@ +package compbio.stat.servlet; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Date; +import java.util.Map; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import compbio.stat.collector.StatDB; +import compbio.stat.servlet.util.StatCollection; +import compbio.stat.servlet.util.Totals; + +public class AnnualStat extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + try { + StatDB db = new StatDB(); + Date earliestRec = db.getEarliestRecord(); + Map stats = StatCollection + .getStats(earliestRec); + Map monthlyTotals = StatCollection + .getTotalStats(stats); + req.setAttribute("stat", monthlyTotals); + req.setAttribute("total", Totals.sumOfTotals(monthlyTotals)); + + RequestDispatcher dispatcher = req + .getRequestDispatcher("statpages/MonthlySummary.jsp"); + dispatcher.forward(req, resp); + + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/webservices/compbio/stat/servlet/DisplayStat.java b/webservices/compbio/stat/servlet/DisplayStat.java index 9f88069..957dd7b 100644 --- a/webservices/compbio/stat/servlet/DisplayStat.java +++ b/webservices/compbio/stat/servlet/DisplayStat.java @@ -2,11 +2,13 @@ package compbio.stat.servlet; import java.io.IOException; import java.sql.SQLException; -import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.Map; -import java.util.TreeMap; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -14,54 +16,36 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import compbio.stat.collector.StatDB; -import compbio.stat.collector.StatProcessor; -import compbio.ws.client.Services; +import compbio.stat.servlet.util.StatCollection; +import compbio.stat.servlet.util.Totals; public class DisplayStat extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // TODO - Calendar startTime = Calendar.getInstance(); - startTime.roll(Calendar.YEAR, false); - Timestamp startDate = new Timestamp(startTime.getTimeInMillis()); - Timestamp stopDate = new Timestamp(new Date().getTime()); - StatDB statdb = null; - try { - statdb = new StatDB(); - - Map stats = new TreeMap(); - for (Services service : Services.values()) { - stats.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, null))); - } - - Map statsCluster = new TreeMap(); - for (Services service : Services.values()) { - statsCluster.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, true))); - } - - Map statsLocal = new TreeMap(); - for (Services service : Services.values()) { - statsLocal.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, false))); - } - req.setAttribute("stat", stats); - req.setAttribute("statTotal", Totals.sumStats(stats)); + String month = req.getParameter("month"); + System.out.println("? " + month); + DateFormat df = SimpleDateFormat.getInstance(); - req.setAttribute("statCluster", statsCluster); - req.setAttribute("statLocal", statsLocal); - req.setAttribute("startDate", startDate.getTime()); - req.setAttribute("stopDate", stopDate.getTime()); + try { + Date fromDate = df.parse(month); + Map statsMap = StatCollection + .getStats(fromDate); + assert statsMap.size() == 1; + StatCollection stats = statsMap.values().iterator().next(); + req.setAttribute("stat", stats.getAllStat()); + req.setAttribute("statTotal", Totals.sumStats(stats.getAllStat())); + req.setAttribute("statTotalCluster", + Totals.sumStats(stats.getClusterStat())); + req.setAttribute("statTotalLocal", + Totals.sumStats(stats.getLocalStat())); + + req.setAttribute("startDate", fromDate.getTime()); + Calendar c = GregorianCalendar.getInstance(); + c.setTime(fromDate); + c.add(Calendar.MONTH, 1); + req.setAttribute("stopDate", c.getTime()); RequestDispatcher dispatcher = req .getRequestDispatcher("statpages/Statistics.jsp"); @@ -70,8 +54,10 @@ public class DisplayStat extends HttpServlet { } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } } - } diff --git a/webservices/compbio/stat/servlet/YearStat.java b/webservices/compbio/stat/servlet/YearStat.java deleted file mode 100644 index bf975ec..0000000 --- a/webservices/compbio/stat/servlet/YearStat.java +++ /dev/null @@ -1,77 +0,0 @@ -package compbio.stat.servlet; - -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.util.Calendar; -import java.util.Date; -import java.util.Map; -import java.util.TreeMap; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import compbio.stat.collector.StatDB; -import compbio.stat.collector.StatProcessor; -import compbio.ws.client.Services; - -public class YearStat extends HttpServlet { - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - // TODO - Calendar startTime = Calendar.getInstance(); - startTime.roll(Calendar.YEAR, false); - Timestamp startDate = new Timestamp(startTime.getTimeInMillis()); - Timestamp stopDate = new Timestamp(new Date().getTime()); - StatDB statdb = null; - try { - statdb = new StatDB(); - - Map stats = new TreeMap(); - for (Services service : Services.values()) { - stats.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, null))); - } - - Map statsCluster = new TreeMap(); - for (Services service : Services.values()) { - statsCluster.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, true))); - } - - Map statsLocal = new TreeMap(); - for (Services service : Services.values()) { - statsLocal.put( - service, - new StatProcessor(statdb.readData(startDate, stopDate, - service, false))); - } - req.setAttribute("stat", stats); - req.setAttribute("statTotal", Totals.sumStats(stats)); - - req.setAttribute("statCluster", statsCluster); - req.setAttribute("statLocal", statsLocal); - req.setAttribute("startDate", startDate.getTime()); - req.setAttribute("stopDate", stopDate.getTime()); - - RequestDispatcher dispatcher = req - .getRequestDispatcher("statpages/Statistics.jsp"); - dispatcher.forward(req, resp); - - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - -} diff --git a/webservices/compbio/stat/servlet/util/StatCollection.java b/webservices/compbio/stat/servlet/util/StatCollection.java new file mode 100644 index 0000000..d6644c3 --- /dev/null +++ b/webservices/compbio/stat/servlet/util/StatCollection.java @@ -0,0 +1,180 @@ +package compbio.stat.servlet.util; + +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Map; +import java.util.TreeMap; + +import compbio.stat.collector.StatDB; +import compbio.stat.collector.StatProcessor; +import compbio.ws.client.Services; + +public class StatCollection { + + /** + * Total number of requests + * + * incomplete abandoned cancelled + * + * @author pvtroshin + * + */ + + public enum Stattype { + CLUSTER, LOCAL, ALL + } + + private Map allStat; + private Map clusterStat; + private Map localStat; + + public Map getAllStat() { + return allStat; + } + public Map getClusterStat() { + return clusterStat; + } + public Map getLocalStat() { + return localStat; + } + + public static Map getStats(int monthsToReport) + throws SQLException { + Calendar fromCal = GregorianCalendar.getInstance(); + fromCal.add(Calendar.MONTH, -monthsToReport); + return getStats(fromCal.getTime()); + } + + public static Map getStats(Date fromDate) + throws SQLException { + Map allstats = new TreeMap(); + + Calendar fromCal = GregorianCalendar.getInstance(); + fromCal.setTime(fromDate); + fromCal.set(Calendar.DAY_OF_MONTH, 1); + + Calendar toCal = GregorianCalendar.getInstance(); + toCal.setTime(new Date()); + + if (fromCal.after(toCal)) { + throw new AssertionError("From Date must be before ToDate! "); + } + + while (true) { + Date from = fromCal.getTime(); + fromCal.add(Calendar.MONTH, 1); + if (toCal.before(fromCal)) { + allstats.put(toCal.getTime(), + StatCollection.newStatCollecton(from, toCal.getTime())); + break; + } + // System.out.println("!" + from + " !!! " + fromCal.getTime()); + allstats.put(from, + StatCollection.newStatCollecton(from, fromCal.getTime())); + } + return allstats; + } + public static Map getTotalStats( + Map detailedStats) { + Map totals = new TreeMap(); + for (Map.Entry stat : detailedStats.entrySet()) { + totals.put(stat.getKey(), Totals.sumStats(stat.getValue().allStat)); + } + return totals; + } + + static StatCollection newStatCollecton(Date startDate, Date endDate) + throws SQLException { + + Timestamp startStamp = new Timestamp(startDate.getTime()); + Timestamp stopStamp = new Timestamp(endDate.getTime()); + StatCollection collection = new StatCollection(); + StatDB statdb = new StatDB(); + + // Total + collection.allStat = new TreeMap(); + for (Services service : Services.values()) { + collection.allStat.put( + service, + new StatProcessor(statdb.readData(startStamp, stopStamp, + service, null))); + } + + // Cluster + collection.clusterStat = new TreeMap(); + for (Services service : Services.values()) { + collection.clusterStat.put( + service, + new StatProcessor(statdb.readData(startStamp, stopStamp, + service, true))); + } + + // Local + collection.localStat = new TreeMap(); + for (Services service : Services.values()) { + collection.localStat.put( + service, + new StatProcessor(statdb.readData(startStamp, stopStamp, + service, false))); + } + return collection; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((allStat == null) ? 0 : allStat.hashCode()); + result = prime * result + + ((clusterStat == null) ? 0 : clusterStat.hashCode()); + result = prime * result + + ((localStat == null) ? 0 : localStat.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + StatCollection other = (StatCollection) obj; + if (allStat == null) { + if (other.allStat != null) + return false; + } else if (!allStat.equals(other.allStat)) + return false; + if (clusterStat == null) { + if (other.clusterStat != null) + return false; + } else if (!clusterStat.equals(other.clusterStat)) + return false; + if (localStat == null) { + if (other.localStat != null) + return false; + } else if (!localStat.equals(other.localStat)) + return false; + return true; + } + @Override + public String toString() { + String value = ""; + for (Map.Entry entry : allStat.entrySet()) { + value += entry.getKey() + ": "; + value += entry.getValue() + "\n"; + } + for (Map.Entry entry : clusterStat.entrySet()) { + value += entry.getKey() + ": "; + value += entry.getValue() + "\n"; + } + for (Map.Entry entry : localStat.entrySet()) { + value += entry.getKey() + ": "; + value += entry.getValue() + "\n"; + } + return value; + } + +} diff --git a/webservices/compbio/stat/servlet/Totals.java b/webservices/compbio/stat/servlet/util/Totals.java similarity index 59% rename from webservices/compbio/stat/servlet/Totals.java rename to webservices/compbio/stat/servlet/util/Totals.java index 8894eb4..d9cacb5 100644 --- a/webservices/compbio/stat/servlet/Totals.java +++ b/webservices/compbio/stat/servlet/util/Totals.java @@ -1,5 +1,6 @@ -package compbio.stat.servlet; +package compbio.stat.servlet.util; +import java.util.Date; import java.util.Map; import compbio.stat.collector.StatProcessor; @@ -32,7 +33,20 @@ public class Totals { return failed; } - static Totals sumStats(Map stat) { + public static Totals sumOfTotals(Map stat) { + Totals total = new Totals(); + for (Map.Entry entry : stat.entrySet()) { + Totals mtotal = entry.getValue(); + total.total += mtotal.getTotal(); + total.incomplete += mtotal.getIncomplete(); + total.abandoned += mtotal.getAbandoned(); + total.cancelled += mtotal.getCancelled(); + total.failed += mtotal.getFailed(); + } + return total; + } + + public static Totals sumStats(Map stat) { Totals total = new Totals(); for (Map.Entry serv : stat.entrySet()) { total.total += serv.getValue().getJobNumber();