Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / Report.java
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/Report.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/Report.java
new file mode 100644 (file)
index 0000000..f4e0255
--- /dev/null
@@ -0,0 +1,155 @@
+/*******************************************************************************\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.lang.reflect.Constructor;\r
+import java.util.Calendar;\r
+import java.util.GregorianCalendar;\r
+import java.util.TimeZone;\r
+\r
+/**\r
+ * This class provides a CLI to generate any of the reports defined in this package.\r
+ *\r
+ * @author Robert P. Eby\r
+ * @version $Id: Report.java,v 1.2 2013/11/06 16:23:55 eby Exp $\r
+ */\r
+public class Report {\r
+       /**\r
+        * Generate .csv report files from the database.  Usage:\r
+        * <pre>\r
+        * java org.onap.dmaap.datarouter.reports.Report [ -t <i>type</i> ] [ -o <i>outfile</i> ] [ <i>fromdate</i> [ <i>todate</i> ]]\r
+        * </pre>\r
+        * <i>type</i> should be <b>volume</b> for a {@link VolumeReport},\r
+        * <b>feed</b> for a {@link FeedReport},\r
+        * <b>latency</b> for a {@link LatencyReport}, or\r
+        * <b>dailyLatency</b> for a {@link DailyLatencyReport}.\r
+        * If <i>outfile</i> is not specified, the report goes into a file <i>/tmp/nnnnnnnnnnnnn.csv</i>,\r
+        * where nnnnnnnnnnnnn is the current time in milliseconds.\r
+        * If <i>from</i> and <i>to</i> are not specified, then the report is limited to the last weeks worth of data.\r
+        * <i>from</i> can be the keyword <b>ALL</b> to specify all data in the DB, or the keyword <b>yesterday</b>.\r
+        * Otherwise, <i>from</i> and <i>to</i> should match the pattern YYYY-MM-DD.\r
+        * @param args the command line arguments\r
+        */\r
+       public static void main(String[] args) {\r
+               ReportBase report = new VolumeReport();\r
+               String outfile = "/tmp/" + System.currentTimeMillis() + ".csv";\r
+               String from = null, to = null;\r
+\r
+               for (int i = 0; i < args.length; i++) {\r
+                       if (args[i].equals("-?")) {\r
+                               System.err.println("usage: java org.onap.dmaap.datarouter.reports.Report [ -t <i>type</i> ] [ -o <i>outfile</i> ] [ <i>fromdate</i> [ <i>todate</i> ]]");\r
+                               System.exit(0);\r
+                       } else if (args[i].equals("-o")) {\r
+                               if (++i < args.length) {\r
+                                       outfile = args[i];\r
+                               }\r
+                       } else if (args[i].equals("-t")) {\r
+                               if (++i < args.length) {\r
+                                       String base = args[i];\r
+                                       base = Character.toUpperCase(base.charAt(0)) + base.substring(1);\r
+                                       base = "org.onap.dmaap.datarouter.reports."+base+"Report";\r
+                                       try {\r
+                                               @SuppressWarnings("unchecked")\r
+                                               Class<? extends ReportBase> cl = (Class<? extends ReportBase>) Class.forName(base);\r
+                                               Constructor<? extends ReportBase> con = cl.getConstructor();\r
+                                               report = con.newInstance();\r
+                                       } catch (Exception e) {\r
+                                               System.err.println("Unknown report type: "+args[i]);\r
+                                               System.exit(1);\r
+                                       }\r
+                               }\r
+                       } else if (from == null) {\r
+                               from = args[i];\r
+                       } else {\r
+                               to = args[i];\r
+                       }\r
+               }\r
+               long lfrom = 0, lto = 0;\r
+               if (from == null) {\r
+                       // last 7 days\r
+                       TimeZone utc = TimeZone.getTimeZone("UTC");\r
+                       Calendar cal = new GregorianCalendar(utc);\r
+                       cal.set(Calendar.HOUR_OF_DAY, 0);\r
+                       cal.set(Calendar.MINUTE, 0);\r
+                       cal.set(Calendar.SECOND, 0);\r
+                       cal.set(Calendar.MILLISECOND, 0);\r
+                       lfrom = cal.getTimeInMillis() - (7 * 24 * 60 * 60 * 1000L);     // 1 week\r
+                       lto   = cal.getTimeInMillis() - 1;\r
+               } else if (to == null) {\r
+                       try {\r
+                               String[] dates = getDates(from);\r
+                               lfrom = Long.parseLong(dates[0]);\r
+                               lto   = Long.parseLong(dates[1]);\r
+                       } catch (Exception e) {\r
+                               System.err.println("Invalid date: "+from);\r
+                               System.exit(1);\r
+                       }\r
+               } else {\r
+                       String[] dates;\r
+                       try {\r
+                               dates = getDates(from);\r
+                               lfrom = Long.parseLong(dates[0]);\r
+                       } catch (Exception e) {\r
+                               System.err.println("Invalid date: "+from);\r
+                               System.exit(1);\r
+                       }\r
+                       try {\r
+                               dates = getDates(to);\r
+                               lto   = Long.parseLong(dates[0]);\r
+                       } catch (Exception e) {\r
+                               System.err.println("Invalid date: "+to);\r
+                               System.exit(1);\r
+                       }\r
+               }\r
+\r
+               report.setFrom(lfrom);\r
+               report.setTo(lto);\r
+               report.setOutputFile(outfile);\r
+               report.run();\r
+       }\r
+\r
+       private static String[] getDates(String d) throws Exception {\r
+               if (d.equals("ALL"))\r
+                       return new String[] { "1", ""+System.currentTimeMillis() };\r
+\r
+               TimeZone utc = TimeZone.getTimeZone("UTC");\r
+               Calendar cal = new GregorianCalendar(utc);\r
+               if (d.matches("20\\d\\d-\\d\\d-\\d\\d")) {\r
+                       cal.set(Calendar.YEAR,         Integer.parseInt(d.substring(0, 4)));\r
+                       cal.set(Calendar.MONTH,        Integer.parseInt(d.substring(5, 7))-1);\r
+                       cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(d.substring(8, 10)));\r
+               } else if (d.equals("yesterday")) {\r
+                       cal.add(Calendar.DAY_OF_YEAR, -1);\r
+               } else\r
+                       throw new Exception("wa?");\r
+               cal.set(Calendar.HOUR_OF_DAY, 0);\r
+               cal.set(Calendar.MINUTE, 0);\r
+               cal.set(Calendar.SECOND, 0);\r
+               cal.set(Calendar.MILLISECOND, 0);\r
+               long start = cal.getTimeInMillis();\r
+               long end   = start + (24 * 60 * 60 * 1000L) - 1;\r
+               return new String[] { ""+start, ""+end };\r
+       }\r
+}\r