Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / DailyLatencyReport.java
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/DailyLatencyReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/DailyLatencyReport.java
new file mode 100644 (file)
index 0000000..cd48e62
--- /dev/null
@@ -0,0 +1,194 @@
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+\r
+\r
+package org.onap.dmaap.datarouter.reports;\r
+\r
+import java.io.FileNotFoundException;\r
+import java.io.PrintWriter;\r
+import java.sql.Connection;\r
+import java.sql.PreparedStatement;\r
+import java.sql.ResultSet;\r
+import java.sql.SQLException;\r
+import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.TreeSet;\r
+\r
+import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+\r
+/**\r
+ * Generate a daily per feed latency report.  The report is a .csv file containing the following columns:\r
+ * <table>\r
+ * <tr><td>date</td><td>the date for this record</td></tr>\r
+ * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>\r
+ * <tr><td>minsize</td><td>the minimum size of all files published on this feed and date</td></tr>\r
+ * <tr><td>maxsize</td><td>the maximum size of all files published on this feed and date</td></tr>\r
+ * <tr><td>avgsize</td><td>the average size of all files published on this feed and date</td></tr>\r
+ * <tr><td>minlat</td><td>the minimum latency in delivering this feed to all subscribers (in ms)</td></tr>\r
+ * <tr><td>maxlat</td><td>the maximum latency in delivering this feed to all subscribers (in ms)</td></tr>\r
+ * <tr><td>avglat</td><td>the average latency in delivering this feed to all subscribers (in ms)</td></tr>\r
+ * <tr><td>fanout</td><td>the average number of subscribers this feed was delivered to</td></tr>\r
+ * </table>\r
+ * <p>\r
+ * In the context of this report, latency is defined as the value\r
+ * <i>(D<sub>e</sub> - P<sub>s</sub>)</i>\r
+ * where:\r
+ * </p>\r
+ * <p>P<sub>s</sub> is the time that the publication of the file to the node starts.</p>\r
+ * <p>D<sub>e</sub> is the time that the delivery of the file to the subscriber ends.</p>\r
+ *\r
+ * @author Robert P. Eby\r
+ * @version $Id: DailyLatencyReport.java,v 1.2 2013/11/06 16:23:54 eby Exp $\r
+ */\r
+public class DailyLatencyReport extends ReportBase {\r
+       private static final String SELECT_SQL =\r
+               "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS" +\r
+               " where EVENT_TIME >= ? and EVENT_TIME <= ?";\r
+\r
+       private class Job {\r
+               public long pubtime = 0;\r
+               public long clen = 0;\r
+               public List<Long> deltime = new ArrayList<Long>();\r
+               public long minLatency() {\r
+                       long n = deltime.isEmpty() ? 0 : Long.MAX_VALUE;\r
+                       for (Long l : deltime)\r
+                               n = Math.min(n, l-pubtime);\r
+                       return n;\r
+               }\r
+               public long maxLatency() {\r
+                       long n = 0;\r
+                       for (Long l : deltime)\r
+                               n = Math.max(n, l-pubtime);\r
+                       return n;\r
+               }\r
+               public long totalLatency() {\r
+                       long n = 0;\r
+                       for (Long l : deltime)\r
+                               n += (l-pubtime);\r
+                       return n;\r
+               }\r
+       }\r
+       private class Counters {\r
+               public final String date;\r
+               public final int feedid;\r
+               public final Map<String, Job> jobs;\r
+               public Counters(String d, int fid) {\r
+                       date = d;\r
+                       feedid = fid;\r
+                       jobs = new HashMap<String, Job>();\r
+               }\r
+               public void addEvent(long etime, String type, String id, String fid, long clen) {\r
+                       Job j = jobs.get(id);\r
+                       if (j == null) {\r
+                               j = new Job();\r
+                               jobs.put(id, j);\r
+                       }\r
+                       if (type.equals("pub")) {\r
+                               j.pubtime = getPstart(id);\r
+                               j.clen = clen;\r
+                       } else if (type.equals("del")) {\r
+                               j.deltime.add(etime);\r
+                       }\r
+               }\r
+               @Override\r
+               public String toString() {\r
+                       long minsize = Long.MAX_VALUE, maxsize = 0, avgsize = 0;\r
+                       long minl    = Long.MAX_VALUE, maxl    = 0;\r
+                       long fanout  = 0, totall = 0, totaln = 0;\r
+                       for (Job j : jobs.values()) {\r
+                               minsize = Math.min(minsize, j.clen);\r
+                               maxsize = Math.max(maxsize, j.clen);\r
+                               avgsize += j.clen;\r
+                               minl    = Math.min(minl, j.minLatency());\r
+                               maxl    = Math.max(maxl, j.maxLatency());\r
+                               totall  += j.totalLatency();\r
+                               totaln  += j.deltime.size();\r
+                               fanout  += j.deltime.size();\r
+                       }\r
+                       if (jobs.size() > 0) {\r
+                               avgsize /= jobs.size();\r
+                               fanout  /= jobs.size();\r
+                       }\r
+                       long avgl = (totaln > 0) ? (totall / totaln) : 0;\r
+                       return date + "," + feedid + "," + minsize + "," + maxsize + "," + avgsize + "," + minl + "," + maxl + "," + avgl + "," + fanout;\r
+               }\r
+       }\r
+       private long getPstart(String t) {\r
+               if (t.indexOf('.') > 0)\r
+                       t = t.substring(0, t.indexOf('.'));\r
+               return Long.parseLong(t);\r
+       }\r
+\r
+       @Override\r
+       public void run() {\r
+               Map<String, Counters> map = new HashMap<String, Counters>();\r
+               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");\r
+               long start = System.currentTimeMillis();\r
+               try {\r
+                       DB db = new DB();\r
+                       @SuppressWarnings("resource")\r
+                       Connection conn = db.getConnection();\r
+                       PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
+                       ps.setLong(1, from);\r
+                       ps.setLong(2, to);\r
+                       ResultSet rs = ps.executeQuery();\r
+                       while (rs.next()) {\r
+                               String id   = rs.getString("PUBLISH_ID");\r
+                               int feed    = rs.getInt("FEEDID");\r
+                               long etime  = rs.getLong("EVENT_TIME");\r
+                               String type = rs.getString("TYPE");\r
+                               String fid  = rs.getString("FEED_FILEID");\r
+                               long clen   = rs.getLong("CONTENT_LENGTH");\r
+                               String date = sdf.format(new Date(getPstart(id)));\r
+                               String key  = date + "," + feed;\r
+                               Counters c = map.get(key);\r
+                               if (c == null) {\r
+                                       c = new Counters(date, feed);\r
+                                       map.put(key, c);\r
+                               }\r
+                               c.addEvent(etime, type, id, fid, clen);\r
+                       }\r
+                       rs.close();\r
+                       ps.close();\r
+                       db.release(conn);\r
+               } catch (SQLException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               logger.debug("Query time: " + (System.currentTimeMillis()-start) + " ms");\r
+               try {\r
+                       PrintWriter os = new PrintWriter(outfile);\r
+                       os.println("date,feedid,minsize,maxsize,avgsize,minlat,maxlat,avglat,fanout");\r
+                       for (String key : new TreeSet<String>(map.keySet())) {\r
+                               Counters c = map.get(key);\r
+                               os.println(c.toString());\r
+                       }\r
+                       os.close();\r
+               } catch (FileNotFoundException e) {\r
+                       System.err.println("File cannot be written: "+outfile);\r
+               }\r
+       }\r
+}\r