Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / LatencyReport.java
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java
new file mode 100644 (file)
index 0000000..c213c03
--- /dev/null
@@ -0,0 +1,179 @@
+/*******************************************************************************\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.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+\r
+/**\r
+ * Generate a per-file latency report.  It reports on the details related to one file published\r
+ * on one feed. This report can be further reduced in order to generate more specific reports\r
+ * based on feed ID or node name. The report is a .csv file containing the following columns:\r
+ * <table>\r
+ * <tr><td>recordid</td><td>the unique record ID assigned to a particular incoming feed</td></tr>\r
+ * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>\r
+ * <tr><td>uri</td><td>the URI of the file delivered</td></tr>\r
+ * <tr><td>size</td><td>the size of the file delivered</td></tr>\r
+ * <tr><td>min</td><td>the minimum latency in delivering this feed to a subscriber (in ms)</td></tr>\r
+ * <tr><td>max</td><td>the maximum latency in delivering this feed to a subscriber (in ms)</td></tr>\r
+ * <tr><td>avg</td><td>the average latency in delivering this feed to all subscribers (in ms)</td></tr>\r
+ * <tr><td>fanout</td><td>the number of subscribers this feed was delivered to</td></tr>\r
+ * </table>\r
+ *\r
+ * @author Robert P. Eby\r
+ * @version $Id: LatencyReport.java,v 1.1 2013/10/28 18:06:53 eby Exp $\r
+ */\r
+public class LatencyReport 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 <= ? order by PUBLISH_ID, EVENT_TIME";\r
+\r
+       private class Event {\r
+               public final String type;\r
+               public final long time;\r
+               public Event(String t, long tm) {\r
+                       type = t;\r
+                       time = tm;\r
+               }\r
+       }\r
+       private class Counters {\r
+               public final String id;\r
+               public final int feedid;\r
+               public final long clen;\r
+               public final String fileid;\r
+               public final List<Event> events;\r
+               public Counters(String i, int fid, long c, String s) {\r
+                       id = i;\r
+                       feedid = fid;\r
+                       clen = c;\r
+                       fileid = s;\r
+                       events = new ArrayList<Event>();\r
+               }\r
+               private long pubtime;\r
+               public void addEvent(String t, long tm) {\r
+                       events.add(new Event(t, tm));\r
+                       if (t.equals("pub"))\r
+                               pubtime = tm;\r
+               }\r
+               public long min() {\r
+                       long min = Long.MAX_VALUE;\r
+                       for (Event e : events) {\r
+                               if (e.type.equals("del")) {\r
+                                       min = Math.min(min, e.time - pubtime);\r
+                               }\r
+                       }\r
+                       return min;\r
+               }\r
+               public long max() {\r
+                       long max = 0;\r
+                       for (Event e : events) {\r
+                               if (e.type.equals("del")) {\r
+                                       max = Math.max(max, e.time - pubtime);\r
+                               }\r
+                       }\r
+                       return max;\r
+               }\r
+               public long avg() {\r
+                       long total = 0, c = 0;\r
+                       for (Event e : events) {\r
+                               if (e.type.equals("del")) {\r
+                                       total += e.time - pubtime;\r
+                                       c++;\r
+                               }\r
+                       }\r
+                       return (c == 0) ? 0 : total/c;\r
+               }\r
+               public int fanout() {\r
+                       int n = 0;\r
+                       for (Event e : events) {\r
+                               if (e.type.equals("del")) {\r
+                                       n++;\r
+                               }\r
+                       }\r
+                       return n;\r
+               }\r
+               @Override\r
+               public String toString() {\r
+                       return feedid + "," + fileid + "," + clen + "," + min() + "," + max() + "," + avg() + "," + fanout();\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void run() {\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
+                       PrintWriter os = new PrintWriter(outfile);\r
+                       os.println("recordid,feedid,uri,size,min,max,avg,fanout");\r
+                       Counters c = null;\r
+                       while (rs.next()) {\r
+                               long etime  = rs.getLong("EVENT_TIME");\r
+                               String type = rs.getString("TYPE");\r
+                               String id   = rs.getString("PUBLISH_ID");\r
+                               String fid  = rs.getString("FEED_FILEID");\r
+                               int feed    = rs.getInt("FEEDID");\r
+                               long clen   = rs.getLong("CONTENT_LENGTH");\r
+                               if (c != null && !id.equals(c.id)) {\r
+                                       String line = id + "," + c.toString();\r
+                                       os.println(line);\r
+                                       c = null;\r
+                               }\r
+                               if (c == null) {\r
+                                       c = new Counters(id, feed, clen, fid);\r
+                               }\r
+                               if (feed != c.feedid)\r
+                                       System.err.println("Feed ID mismatch, "+feed+" <=> "+c.feedid);\r
+                               if (clen != c.clen)\r
+                                       System.err.println("Cont Len mismatch, "+clen+" <=> "+c.clen);\r
+//                             if (fid != c.fileid)\r
+//                                     System.err.println("File ID mismatch, "+fid+" <=> "+c.fileid);\r
+                               c.addEvent(type, etime);\r
+                       }\r
+                       rs.close();\r
+                       ps.close();\r
+                       db.release(conn);\r
+                       os.close();\r
+               } catch (FileNotFoundException e) {\r
+                       System.err.println("File cannot be written: "+outfile);\r
+               } catch (SQLException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               logger.debug("Query time: " + (System.currentTimeMillis()-start) + " ms");\r
+       }\r
+}\r