[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / com / att / research / 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 com.att.research.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 com.att.research.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          * @param args the command line arguments\r
54          */\r
55         public static void main(String[] args) {\r
56                 ReportBase report = new VolumeReport();\r
57                 String outfile = "/tmp/" + System.currentTimeMillis() + ".csv";\r
58                 String from = null, to = null;\r
59 \r
60                 for (int i = 0; i < args.length; i++) {\r
61                         if (args[i].equals("-?")) {\r
62                                 System.err.println("usage: java com.att.research.datarouter.reports.Report [ -t <i>type</i> ] [ -o <i>outfile</i> ] [ <i>fromdate</i> [ <i>todate</i> ]]");\r
63                                 System.exit(0);\r
64                         } else if (args[i].equals("-o")) {\r
65                                 if (++i < args.length) {\r
66                                         outfile = args[i];\r
67                                 }\r
68                         } else if (args[i].equals("-t")) {\r
69                                 if (++i < args.length) {\r
70                                         String base = args[i];\r
71                                         base = Character.toUpperCase(base.charAt(0)) + base.substring(1);\r
72                                         base = "com.att.research.datarouter.reports."+base+"Report";\r
73                                         try {\r
74                                                 @SuppressWarnings("unchecked")\r
75                                                 Class<? extends ReportBase> cl = (Class<? extends ReportBase>) Class.forName(base);\r
76                                                 Constructor<? extends ReportBase> con = cl.getConstructor();\r
77                                                 report = con.newInstance();\r
78                                         } catch (Exception e) {\r
79                                                 System.err.println("Unknown report type: "+args[i]);\r
80                                                 System.exit(1);\r
81                                         }\r
82                                 }\r
83                         } else if (from == null) {\r
84                                 from = args[i];\r
85                         } else {\r
86                                 to = args[i];\r
87                         }\r
88                 }\r
89                 long lfrom = 0, lto = 0;\r
90                 if (from == null) {\r
91                         // last 7 days\r
92                         TimeZone utc = TimeZone.getTimeZone("UTC");\r
93                         Calendar cal = new GregorianCalendar(utc);\r
94                         cal.set(Calendar.HOUR_OF_DAY, 0);\r
95                         cal.set(Calendar.MINUTE, 0);\r
96                         cal.set(Calendar.SECOND, 0);\r
97                         cal.set(Calendar.MILLISECOND, 0);\r
98                         lfrom = cal.getTimeInMillis() - (7 * 24 * 60 * 60 * 1000L);     // 1 week\r
99                         lto   = cal.getTimeInMillis() - 1;\r
100                 } else if (to == null) {\r
101                         try {\r
102                                 String[] dates = getDates(from);\r
103                                 lfrom = Long.parseLong(dates[0]);\r
104                                 lto   = Long.parseLong(dates[1]);\r
105                         } catch (Exception e) {\r
106                                 System.err.println("Invalid date: "+from);\r
107                                 System.exit(1);\r
108                         }\r
109                 } else {\r
110                         String[] dates;\r
111                         try {\r
112                                 dates = getDates(from);\r
113                                 lfrom = Long.parseLong(dates[0]);\r
114                         } catch (Exception e) {\r
115                                 System.err.println("Invalid date: "+from);\r
116                                 System.exit(1);\r
117                         }\r
118                         try {\r
119                                 dates = getDates(to);\r
120                                 lto   = Long.parseLong(dates[0]);\r
121                         } catch (Exception e) {\r
122                                 System.err.println("Invalid date: "+to);\r
123                                 System.exit(1);\r
124                         }\r
125                 }\r
126 \r
127                 report.setFrom(lfrom);\r
128                 report.setTo(lto);\r
129                 report.setOutputFile(outfile);\r
130                 report.run();\r
131         }\r
132 \r
133         private static String[] getDates(String d) throws Exception {\r
134                 if (d.equals("ALL"))\r
135                         return new String[] { "1", ""+System.currentTimeMillis() };\r
136 \r
137                 TimeZone utc = TimeZone.getTimeZone("UTC");\r
138                 Calendar cal = new GregorianCalendar(utc);\r
139                 if (d.matches("20\\d\\d-\\d\\d-\\d\\d")) {\r
140                         cal.set(Calendar.YEAR,         Integer.parseInt(d.substring(0, 4)));\r
141                         cal.set(Calendar.MONTH,        Integer.parseInt(d.substring(5, 7))-1);\r
142                         cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(d.substring(8, 10)));\r
143                 } else if (d.equals("yesterday")) {\r
144                         cal.add(Calendar.DAY_OF_YEAR, -1);\r
145                 } else\r
146                         throw new Exception("wa?");\r
147                 cal.set(Calendar.HOUR_OF_DAY, 0);\r
148                 cal.set(Calendar.MINUTE, 0);\r
149                 cal.set(Calendar.SECOND, 0);\r
150                 cal.set(Calendar.MILLISECOND, 0);\r
151                 long start = cal.getTimeInMillis();\r
152                 long end   = start + (24 * 60 * 60 * 1000L) - 1;\r
153                 return new String[] { ""+start, ""+end };\r
154         }\r
155 }\r