datarouter-prov code clean - remove tabs
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / Report.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.reports;\r
26 \r
27 import java.lang.reflect.Constructor;\r
28 import java.util.Calendar;\r
29 import java.util.GregorianCalendar;\r
30 import java.util.TimeZone;\r
31 \r
32 /**\r
33  * This class provides a CLI to generate any of the reports defined in this package.\r
34  *\r
35  * @author Robert P. Eby\r
36  * @version $Id: Report.java,v 1.2 2013/11/06 16:23:55 eby Exp $\r
37  */\r
38 public class Report {\r
39     /**\r
40      * Generate .csv report files from the database.  Usage:\r
41      * <pre>\r
42      * java org.onap.dmaap.datarouter.reports.Report [ -t <i>type</i> ] [ -o <i>outfile</i> ] [ <i>fromdate</i> [ <i>todate</i> ]]\r
43      * </pre>\r
44      * <i>type</i> should be <b>volume</b> for a {@link VolumeReport},\r
45      * <b>feed</b> for a {@link FeedReport},\r
46      * <b>latency</b> for a {@link LatencyReport}, or\r
47      * <b>dailyLatency</b> for a {@link DailyLatencyReport}.\r
48      * If <i>outfile</i> is not specified, the report goes into a file <i>/tmp/nnnnnnnnnnnnn.csv</i>,\r
49      * where nnnnnnnnnnnnn is the current time in milliseconds.\r
50      * If <i>from</i> and <i>to</i> are not specified, then the report is limited to the last weeks worth of data.\r
51      * <i>from</i> can be the keyword <b>ALL</b> to specify all data in the DB, or the keyword <b>yesterday</b>.\r
52      * Otherwise, <i>from</i> and <i>to</i> should match the pattern YYYY-MM-DD.\r
53      *\r
54      * @param args the command line arguments\r
55      */\r
56     public static void main(String[] args) {\r
57         ReportBase report = new VolumeReport();\r
58         String outfile = "/tmp/" + System.currentTimeMillis() + ".csv";\r
59         String from = null, to = null;\r
60 \r
61         for (int i = 0; i < args.length; i++) {\r
62             if (args[i].equals("-?")) {\r
63                 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
64                 System.exit(0);\r
65             } else if (args[i].equals("-o")) {\r
66                 if (++i < args.length) {\r
67                     outfile = args[i];\r
68                 }\r
69             } else if (args[i].equals("-t")) {\r
70                 if (++i < args.length) {\r
71                     String base = args[i];\r
72                     base = Character.toUpperCase(base.charAt(0)) + base.substring(1);\r
73                     base = "org.onap.dmaap.datarouter.reports." + base + "Report";\r
74                     try {\r
75                         @SuppressWarnings("unchecked")\r
76                         Class<? extends ReportBase> cl = (Class<? extends ReportBase>) Class.forName(base);\r
77                         Constructor<? extends ReportBase> con = cl.getConstructor();\r
78                         report = con.newInstance();\r
79                     } catch (Exception e) {\r
80                         System.err.println("Unknown report type: " + args[i]);\r
81                         System.exit(1);\r
82                     }\r
83                 }\r
84             } else if (from == null) {\r
85                 from = args[i];\r
86             } else {\r
87                 to = args[i];\r
88             }\r
89         }\r
90         long lfrom = 0, lto = 0;\r
91         if (from == null) {\r
92             // last 7 days\r
93             TimeZone utc = TimeZone.getTimeZone("UTC");\r
94             Calendar cal = new GregorianCalendar(utc);\r
95             cal.set(Calendar.HOUR_OF_DAY, 0);\r
96             cal.set(Calendar.MINUTE, 0);\r
97             cal.set(Calendar.SECOND, 0);\r
98             cal.set(Calendar.MILLISECOND, 0);\r
99             lfrom = cal.getTimeInMillis() - (7 * 24 * 60 * 60 * 1000L);    // 1 week\r
100             lto = cal.getTimeInMillis() - 1;\r
101         } else if (to == null) {\r
102             try {\r
103                 String[] dates = getDates(from);\r
104                 lfrom = Long.parseLong(dates[0]);\r
105                 lto = Long.parseLong(dates[1]);\r
106             } catch (Exception e) {\r
107                 System.err.println("Invalid date: " + from);\r
108                 System.exit(1);\r
109             }\r
110         } else {\r
111             String[] dates;\r
112             try {\r
113                 dates = getDates(from);\r
114                 lfrom = Long.parseLong(dates[0]);\r
115             } catch (Exception e) {\r
116                 System.err.println("Invalid date: " + from);\r
117                 System.exit(1);\r
118             }\r
119             try {\r
120                 dates = getDates(to);\r
121                 lto = Long.parseLong(dates[0]);\r
122             } catch (Exception e) {\r
123                 System.err.println("Invalid date: " + to);\r
124                 System.exit(1);\r
125             }\r
126         }\r
127 \r
128         report.setFrom(lfrom);\r
129         report.setTo(lto);\r
130         report.setOutputFile(outfile);\r
131         report.run();\r
132     }\r
133 \r
134     private static String[] getDates(String d) throws Exception {\r
135         if (d.equals("ALL"))\r
136             return new String[]{"1", "" + System.currentTimeMillis()};\r
137 \r
138         TimeZone utc = TimeZone.getTimeZone("UTC");\r
139         Calendar cal = new GregorianCalendar(utc);\r
140         if (d.matches("20\\d\\d-\\d\\d-\\d\\d")) {\r
141             cal.set(Calendar.YEAR, Integer.parseInt(d.substring(0, 4)));\r
142             cal.set(Calendar.MONTH, Integer.parseInt(d.substring(5, 7)) - 1);\r
143             cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(d.substring(8, 10)));\r
144         } else if (d.equals("yesterday")) {\r
145             cal.add(Calendar.DAY_OF_YEAR, -1);\r
146         } else\r
147             throw new Exception("wa?");\r
148         cal.set(Calendar.HOUR_OF_DAY, 0);\r
149         cal.set(Calendar.MINUTE, 0);\r
150         cal.set(Calendar.SECOND, 0);\r
151         cal.set(Calendar.MILLISECOND, 0);\r
152         long start = cal.getTimeInMillis();\r
153         long end = start + (24 * 60 * 60 * 1000L) - 1;\r
154         return new String[]{"" + start, "" + end};\r
155     }\r
156 }\r