Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / LatencyReport.java
index ba8f15a..f98116d 100644 (file)
@@ -32,8 +32,7 @@ import java.sql.ResultSet;
 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
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * Generate a per-file latency report.  It reports on the details related to one file published\r
@@ -54,9 +53,6 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @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
@@ -80,7 +76,7 @@ public class LatencyReport extends ReportBase {
             feedid = fid;\r
             clen = c;\r
             fileid = s;\r
-            events = new ArrayList<Event>();\r
+            events = new ArrayList<>();\r
         }\r
 \r
         private long pubtime;\r
@@ -141,48 +137,43 @@ public class LatencyReport extends ReportBase {
     @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
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(\r
+                "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS where "\r
+                    + "EVENT_TIME >= ? and EVENT_TIME <= ? order by PUBLISH_ID, EVENT_TIME")) {\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
+            try(ResultSet rs = ps.executeQuery()) {\r
+                try (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
+                        c.addEvent(type, etime);\r
+                    }\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
+            logger.error("SQLException: " + e.getMessage());\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
     }\r