Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / FeedReport.java
index d638aaa..3c63c28 100644 (file)
@@ -37,10 +37,9 @@ import java.util.Arrays;
 import java.util.Calendar;\r
 import java.util.Date;\r
 import java.util.GregorianCalendar;\r
-\r
 import org.json.JSONException;\r
 import org.json.JSONObject;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * Generate a feeds report.  The report is a .CSV file.\r
@@ -49,15 +48,6 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @version $Id: FeedReport.java,v 1.2 2013/11/06 16:23:55 eby Exp $\r
  */\r
 public class FeedReport extends ReportBase {\r
-    private static final String SELECT_SQL =\r
-            // Note to use the time in the publish_id, use date(from_unixtime(substring(publish_id, 1, 10)))\r
-            // To just use month, substring(from_unixtime(event_time div 1000), 1, 7)\r
-            "select date(from_unixtime(event_time div 1000)) as date, type, feedid, delivery_subid, count(*) as count" +\r
-                    " from LOG_RECORDS" +\r
-                    " where type = 'pub' or type = 'del'" +\r
-                    " group by date, type, feedid, delivery_subid";\r
-    private static final String SELECT_SQL_OLD =\r
-            "select PUBLISH_ID, TYPE, FEEDID, DELIVERY_SUBID from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ?";\r
 \r
     @Override\r
     public void run() {\r
@@ -65,64 +55,32 @@ public class FeedReport extends ReportBase {
         JSONObject jo = new JSONObject();\r
         long start = System.currentTimeMillis();\r
         StringBuilder sb = new StringBuilder();\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
-                if (alg1) {\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(\r
+                // Note to use the time in the publish_id, use date(from_unixtime(substring(publish_id, 1, 10)))\r
+                // To just use month, substring(from_unixtime(event_time div 1000), 1, 7)\r
+                "select date(from_unixtime(event_time div 1000)) as date, type, feedid, delivery_subid, count(*) "\r
+                    + "as count from LOG_RECORDS where type = 'pub' or type = 'del' group by date, type, feedid, delivery_subid")) {\r
+            try (ResultSet rs = ps.executeQuery()) {\r
+                while (rs.next()) {\r
                     String date = rs.getString("date");\r
                     String type = rs.getString("type");\r
                     int feedid = rs.getInt("feedid");\r
                     int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;\r
                     int count = rs.getInt("count");\r
                     sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");\r
-                } else {\r
-                    String date = rs.getString("date");\r
-                    JSONObject datemap = jo.optJSONObject(date);\r
-                    if (datemap == null) {\r
-                        datemap = new JSONObject();\r
-                        jo.put(date, datemap);\r
-                    }\r
-                    int feed = rs.getInt("FEEDID");\r
-                    JSONObject feedmap = datemap.optJSONObject("" + feed);\r
-                    if (feedmap == null) {\r
-                        feedmap = new JSONObject();\r
-                        feedmap.put("pubcount", 0);\r
-                        datemap.put("" + feed, feedmap);\r
-                    }\r
-                    String type = rs.getString("TYPE");\r
-                    int count = rs.getInt("count");\r
-                    if (type.equals("pub")) {\r
-                        feedmap.put("pubcount", count);\r
-                    } else if (type.equals("del")) {\r
-                        String subid = "" + rs.getInt("DELIVERY_SUBID");\r
-                        feedmap.put(subid, count);\r
-                    }\r
                 }\r
             }\r
-            rs.close();\r
-            ps.close();\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
-            e.printStackTrace();\r
+            logger.error(e.toString());\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
-        try {\r
-            PrintWriter os = new PrintWriter(outfile);\r
-            if (alg1) {\r
-                os.print("date,type,feedid,subid,count\n");\r
-                os.print(sb.toString());\r
-            } else {\r
-                os.println(toHTML(jo));\r
-            }\r
-            os.close();\r
+        try (PrintWriter os = new PrintWriter(outfile)) {\r
+            os.print("date,type,feedid,subid,count\n");\r
+            os.print(sb.toString());\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r
+            logger.error(e.toString());\r
         }\r
     }\r
 \r
@@ -130,53 +88,52 @@ public class FeedReport extends ReportBase {
         JSONObject jo = new JSONObject();\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_OLD);\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(\r
+                "select PUBLISH_ID, TYPE, FEEDID, DELIVERY_SUBID from LOG_RECORDS "\r
+                    + "where EVENT_TIME >= ? and EVENT_TIME <= ?")) {\r
             ps.setLong(1, from);\r
             ps.setLong(2, to);\r
             ps.setFetchSize(100000);\r
-            ResultSet rs = ps.executeQuery();\r
-            while (rs.next()) {\r
-                String id = rs.getString("PUBLISH_ID");\r
-                String date = sdf.format(new Date(getPstart(id)));\r
-                JSONObject datemap = jo.optJSONObject(date);\r
-                if (datemap == null) {\r
-                    datemap = new JSONObject();\r
-                    jo.put(date, datemap);\r
-                }\r
-                int feed = rs.getInt("FEEDID");\r
-                JSONObject feedmap = datemap.optJSONObject("" + feed);\r
-                if (feedmap == null) {\r
-                    feedmap = new JSONObject();\r
-                    feedmap.put("pubcount", 0);\r
-                    datemap.put("" + feed, feedmap);\r
-                }\r
-                String type = rs.getString("TYPE");\r
-                if (type.equals("pub")) {\r
-                    try {\r
-                        int n = feedmap.getInt("pubcount");\r
-                        feedmap.put("pubcount", n + 1);\r
-                    } catch (JSONException e) {\r
-                        feedmap.put("pubcount", 1);\r
+            try(ResultSet rs = ps.executeQuery()) {\r
+                while (rs.next()) {\r
+                    String id = rs.getString("PUBLISH_ID");\r
+                    String date = sdf.format(new Date(getPstart(id)));\r
+                    JSONObject datemap = jo.optJSONObject(date);\r
+                    if (datemap == null) {\r
+                        datemap = new JSONObject();\r
+                        jo.put(date, datemap);\r
+                    }\r
+                    int feed = rs.getInt("FEEDID");\r
+                    JSONObject feedmap = datemap.optJSONObject("" + feed);\r
+                    if (feedmap == null) {\r
+                        feedmap = new JSONObject();\r
+                        feedmap.put("pubcount", 0);\r
+                        datemap.put("" + feed, feedmap);\r
                     }\r
-                } else if (type.equals("del")) {\r
-                    String subid = "" + rs.getInt("DELIVERY_SUBID");\r
-                    try {\r
-                        int n = feedmap.getInt(subid);\r
-                        feedmap.put(subid, n + 1);\r
-                    } catch (JSONException e) {\r
-                        feedmap.put(subid, 1);\r
+                    String type = rs.getString("TYPE");\r
+                    if (type.equals("pub")) {\r
+                        try {\r
+                            int n = feedmap.getInt("pubcount");\r
+                            feedmap.put("pubcount", n + 1);\r
+                        } catch (JSONException e) {\r
+                            feedmap.put("pubcount", 1);\r
+                            logger.error(e.toString());\r
+                        }\r
+                    } else if (type.equals("del")) {\r
+                        String subid = "" + rs.getInt("DELIVERY_SUBID");\r
+                        try {\r
+                            int n = feedmap.getInt(subid);\r
+                            feedmap.put(subid, n + 1);\r
+                        } catch (JSONException e) {\r
+                            feedmap.put(subid, 1);\r
+                            logger.error(e.toString());\r
+                        }\r
                     }\r
                 }\r
             }\r
-            rs.close();\r
-            ps.close();\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
-            e.printStackTrace();\r
+            logger.error(e.toString());\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
         try {\r
@@ -185,6 +142,7 @@ public class FeedReport extends ReportBase {
             os.close();\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r
+            logger.error(e.toString());\r
         }\r
     }\r
 \r
@@ -253,11 +211,11 @@ public class FeedReport extends ReportBase {
             String[] feeds = JSONObject.getNames(j2);\r
             Arrays.sort(feeds);\r
             s.append("<tr><td rowspan=\"" + rc1 + "\">")\r
-                    .append(date)\r
-                    .append("</td>");\r
+                .append(date)\r
+                .append("</td>");\r
             s.append("<td rowspan=\"" + rc1 + "\">")\r
-                    .append(feeds.length)\r
-                    .append("</td>");\r
+                .append(feeds.length)\r
+                .append("</td>");\r
             String px1 = "";\r
             for (String feed : feeds) {\r
                 JSONObject j3 = j2.getJSONObject(feed);\r
@@ -267,15 +225,15 @@ public class FeedReport extends ReportBase {
                 String[] subs = JSONObject.getNames(j3);\r
                 Arrays.sort(subs);\r
                 s.append(px1)\r
-                        .append("<td rowspan=\"" + rc2 + "\">")\r
-                        .append(feed)\r
-                        .append("</td>");\r
+                    .append("<td rowspan=\"" + rc2 + "\">")\r
+                    .append(feed)\r
+                    .append("</td>");\r
                 s.append("<td rowspan=\"" + rc2 + "\">")\r
-                        .append(pubcount)\r
-                        .append("</td>");\r
+                    .append(pubcount)\r
+                    .append("</td>");\r
                 s.append("<td rowspan=\"" + rc2 + "\">")\r
-                        .append(subcnt)\r
-                        .append("</td>");\r
+                    .append(subcnt)\r
+                    .append("</td>");\r
                 String px2 = "";\r
                 for (String sub : subs) {\r
                     if (!sub.equals("pubcount")) {\r
@@ -311,7 +269,7 @@ public class FeedReport extends ReportBase {
      *\r
      * @param args\r
      */\r
-    public static void main(String[] args) {\r
+    public void main(String[] args) {\r
         int rtype = 0;    // 0 -> day, 1 -> week, 2 -> month, 3 -> year\r
         String infile = null;\r
         String outfile = null;\r
@@ -343,59 +301,61 @@ public class FeedReport extends ReportBase {
         }\r
         try {\r
             JSONObject jo = new JSONObject();\r
-            LineNumberReader lr = new LineNumberReader(new FileReader(infile));\r
-            String line = lr.readLine();\r
-            while (line != null) {\r
-                String[] tt = line.split(",");\r
-                if (tt[0].startsWith("2")) {\r
-                    String date = tt[0];\r
-                    switch (rtype) {\r
-                        case 1:\r
-                            String[] xx = date.split("-");\r
-                            Calendar cal = new GregorianCalendar(new Integer(xx[0]), new Integer(xx[1]) - 1, new Integer(xx[2]));\r
-                            date = xx[0] + "-W" + cal.get(Calendar.WEEK_OF_YEAR);\r
-                            break;\r
-                        case 2:\r
-                            date = date.substring(0, 7);\r
-                            break;\r
-                        case 3:\r
-                            date = date.substring(0, 4);\r
-                            break;\r
-                    }\r
-                    JSONObject datemap = jo.optJSONObject(date);\r
-                    if (datemap == null) {\r
-                        datemap = new JSONObject();\r
-                        jo.put(date, datemap);\r
-                    }\r
-                    int feed = Integer.parseInt(tt[2]);\r
-                    JSONObject feedmap = datemap.optJSONObject("" + feed);\r
-                    if (feedmap == null) {\r
-                        feedmap = new JSONObject();\r
-                        feedmap.put("pubcount", 0);\r
-                        datemap.put("" + feed, feedmap);\r
-                    }\r
-                    String type = tt[1];\r
-                    int count = Integer.parseInt(tt[4]);\r
-                    if (type.equals("pub")) {\r
-                        try {\r
-                            int n = feedmap.getInt("pubcount");\r
-                            feedmap.put("pubcount", n + count);\r
-                        } catch (JSONException e) {\r
-                            feedmap.put("pubcount", count);\r
+            try(LineNumberReader lr = new LineNumberReader(new FileReader(infile))) {\r
+                String line = lr.readLine();\r
+                while (line != null) {\r
+                    String[] tt = line.split(",");\r
+                    if (tt[0].startsWith("2")) {\r
+                        String date = tt[0];\r
+                        switch (rtype) {\r
+                            case 1:\r
+                                String[] xx = date.split("-");\r
+                                Calendar cal = new GregorianCalendar(new Integer(xx[0]), new Integer(xx[1]) - 1, new Integer(xx[2]));\r
+                                date = xx[0] + "-W" + cal.get(Calendar.WEEK_OF_YEAR);\r
+                                break;\r
+                            case 2:\r
+                                date = date.substring(0, 7);\r
+                                break;\r
+                            case 3:\r
+                                date = date.substring(0, 4);\r
+                                break;\r
                         }\r
-                    } else if (type.equals("del")) {\r
-                        String subid = tt[3];\r
-                        try {\r
-                            int n = feedmap.getInt(subid);\r
-                            feedmap.put(subid, n + count);\r
-                        } catch (JSONException e) {\r
-                            feedmap.put(subid, count);\r
+                        JSONObject datemap = jo.optJSONObject(date);\r
+                        if (datemap == null) {\r
+                            datemap = new JSONObject();\r
+                            jo.put(date, datemap);\r
+                        }\r
+                        int feed = Integer.parseInt(tt[2]);\r
+                        JSONObject feedmap = datemap.optJSONObject("" + feed);\r
+                        if (feedmap == null) {\r
+                            feedmap = new JSONObject();\r
+                            feedmap.put("pubcount", 0);\r
+                            datemap.put("" + feed, feedmap);\r
+                        }\r
+                        String type = tt[1];\r
+                        int count = Integer.parseInt(tt[4]);\r
+                        if (type.equals("pub")) {\r
+                            try {\r
+                                int n = feedmap.getInt("pubcount");\r
+                                feedmap.put("pubcount", n + count);\r
+                            } catch (JSONException e) {\r
+                                feedmap.put("pubcount", count);\r
+                                logger.error(e.toString());\r
+                            }\r
+                        } else if (type.equals("del")) {\r
+                            String subid = tt[3];\r
+                            try {\r
+                                int n = feedmap.getInt(subid);\r
+                                feedmap.put(subid, n + count);\r
+                            } catch (JSONException e) {\r
+                                feedmap.put(subid, count);\r
+                                logger.error(e.toString());\r
+                            }\r
                         }\r
                     }\r
+                    line = lr.readLine();\r
                 }\r
-                line = lr.readLine();\r
             }\r
-            lr.close();\r
             String t = toHTML(jo);\r
             switch (rtype) {\r
                 case 1:\r
@@ -411,7 +371,7 @@ public class FeedReport extends ReportBase {
             System.out.println(t);\r
         } catch (Exception e) {\r
             System.err.println(e);\r
-            e.printStackTrace();\r
+            logger.error(e.toString());\r
         }\r
     }\r
 }\r