Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / VolumeReport.java
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/VolumeReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/VolumeReport.java
new file mode 100644 (file)
index 0000000..b210ef9
--- /dev/null
@@ -0,0 +1,140 @@
+/*******************************************************************************\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.Date;\r
+import java.util.HashMap;\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 traffic volume 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>filespublished</td><td>the number of files published on this feed and date</td></tr>\r
+ * <tr><td>bytespublished</td><td>the number of bytes published on this feed and date</td></tr>\r
+ * <tr><td>filesdelivered</td><td>the number of files delivered on this feed and date</td></tr>\r
+ * <tr><td>bytesdelivered</td><td>the number of bytes delivered on this feed and date</td></tr>\r
+ * <tr><td>filesexpired</td><td>the number of files expired on this feed and date</td></tr>\r
+ * <tr><td>bytesexpired</td><td>the number of bytes expired on this feed and date</td></tr>\r
+ * </table>\r
+ *\r
+ * @author Robert P. Eby\r
+ * @version $Id: VolumeReport.java,v 1.3 2014/02/28 15:11:13 eby Exp $\r
+ */\r
+public class VolumeReport extends ReportBase {\r
+       private static final String SELECT_SQL = "select EVENT_TIME, TYPE, FEEDID, CONTENT_LENGTH, RESULT" +\r
+               " from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ? LIMIT ?, ?";\r
+\r
+       private class Counters {\r
+               public int  filespublished, filesdelivered, filesexpired;\r
+               public long bytespublished, bytesdelivered, bytesexpired;\r
+               @Override\r
+               public String toString() {\r
+                       return String.format("%d,%d,%d,%d,%d,%d",\r
+                               filespublished, bytespublished, filesdelivered,\r
+                               bytesdelivered, filesexpired, bytesexpired);\r
+               }\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
+                       // We need to run this SELECT in stages, because otherwise we run out of memory!\r
+                       final long stepsize = 6000000L;\r
+                       boolean go_again = true;\r
+                       for (long i = 0; go_again; i += stepsize) {\r
+                               PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
+                               ps.setLong(1, from);\r
+                               ps.setLong(2, to);\r
+                               ps.setLong(3, i);\r
+                               ps.setLong(4, stepsize);\r
+                               ResultSet rs = ps.executeQuery();\r
+                               go_again = false;\r
+                               while (rs.next()) {\r
+                                       go_again = true;\r
+                                       long etime  = rs.getLong("EVENT_TIME");\r
+                                       String type = rs.getString("TYPE");\r
+                                       int feed    = rs.getInt("FEEDID");\r
+                                       long clen   = rs.getLong("CONTENT_LENGTH");\r
+                                       String key  = sdf.format(new Date(etime)) + ":" + feed;\r
+                                       Counters c = map.get(key);\r
+                                       if (c == null) {\r
+                                               c = new Counters();\r
+                                               map.put(key, c);\r
+                                       }\r
+                                       if (type.equalsIgnoreCase("pub")) {\r
+                                               c.filespublished++;\r
+                                               c.bytespublished += clen;\r
+                                       } else if (type.equalsIgnoreCase("del")) {\r
+                                               // Only count successful deliveries\r
+                                               int statusCode = rs.getInt("RESULT");\r
+                                               if (statusCode >= 200 && statusCode < 300) {\r
+                                                       c.filesdelivered++;\r
+                                                       c.bytesdelivered += clen;\r
+                                               }\r
+                                       } else if (type.equalsIgnoreCase("exp")) {\r
+                                               c.filesexpired++;\r
+                                               c.bytesexpired += clen;\r
+                                       }\r
+                               }\r
+                               rs.close();\r
+                               ps.close();\r
+                       }\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,filespublished,bytespublished,filesdelivered,bytesdelivered,filesexpired,bytesexpired");\r
+                       for (String key : new TreeSet<String>(map.keySet())) {\r
+                               Counters c = map.get(key);\r
+                               String[] p = key.split(":");\r
+                               os.println(String.format("%s,%s,%s", p[0], p[1], c.toString()));\r
+                       }\r
+                       os.close();\r
+               } catch (FileNotFoundException e) {\r
+                       System.err.println("File cannot be written: "+outfile);\r
+               }\r
+       }\r
+}\r