aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/main/java/com/att/research/datarouter/reports/Report.java
blob: bd64e0eba782e2ffd28729e8a7b28dbbc7449a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/


package com.att.research.datarouter.reports;

import java.lang.reflect.Constructor;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * This class provides a CLI to generate any of the reports defined in this package.
 *
 * @author Robert P. Eby
 * @version $Id: Report.java,v 1.2 2013/11/06 16:23:55 eby Exp $
 */
public class Report {
	/**
	 * Generate .csv report files from the database.  Usage:
	 * <pre>
	 * java com.att.research.datarouter.reports.Report [ -t <i>type</i> ] [ -o <i>outfile</i> ] [ <i>fromdate</i> [ <i>todate</i> ]]
	 * </pre>
	 * <i>type</i> should be <b>volume</b> for a {@link VolumeReport},
	 * <b>feed</b> for a {@link FeedReport},
	 * <b>latency</b> for a {@link LatencyReport}, or
	 * <b>dailyLatency</b> for a {@link DailyLatencyReport}.
	 * If <i>outfile</i> is not specified, the report goes into a file <i>/tmp/nnnnnnnnnnnnn.csv</i>,
	 * where nnnnnnnnnnnnn is the current time in milliseconds.
	 * If <i>from</i> and <i>to</i> are not specified, then the report is limited to the last weeks worth of data.
	 * <i>from</i> can be the keyword <b>ALL</b> to specify all data in the DB, or the keyword <b>yesterday</b>.
	 * Otherwise, <i>from</i> and <i>to</i> should match the pattern YYYY-MM-DD.
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		ReportBase report = new VolumeReport();
		String outfile = "/tmp/" + System.currentTimeMillis() + ".csv";
		String from = null, to = null;

		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-?")) {
				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> ]]");
				System.exit(0);
			} else if (args[i].equals("-o")) {
				if (++i < args.length) {
					outfile = args[i];
				}
			} else if (args[i].equals("-t")) {
				if (++i < args.length) {
					String base = args[i];
					base = Character.toUpperCase(base.charAt(0)) + base.substring(1);
					base = "com.att.research.datarouter.reports."+base+"Report";
					try {
						@SuppressWarnings("unchecked")
						Class<? extends ReportBase> cl = (Class<? extends ReportBase>) Class.forName(base);
						Constructor<? extends ReportBase> con = cl.getConstructor();
						report = con.newInstance();
					} catch (Exception e) {
						System.err.println("Unknown report type: "+args[i]);
						System.exit(1);
					}
				}
			} else if (from == null) {
				from = args[i];
			} else {
				to = args[i];
			}
		}
		long lfrom = 0, lto = 0;
		if (from == null) {
			// last 7 days
			TimeZone utc = TimeZone.getTimeZone("UTC");
			Calendar cal = new GregorianCalendar(utc);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			lfrom = cal.getTimeInMillis() - (7 * 24 * 60 * 60 * 1000L);	// 1 week
			lto   = cal.getTimeInMillis() - 1;
		} else if (to == null) {
			try {
				String[] dates = getDates(from);
				lfrom = Long.parseLong(dates[0]);
				lto   = Long.parseLong(dates[1]);
			} catch (Exception e) {
				System.err.println("Invalid date: "+from);
				System.exit(1);
			}
		} else {
			String[] dates;
			try {
				dates = getDates(from);
				lfrom = Long.parseLong(dates[0]);
			} catch (Exception e) {
				System.err.println("Invalid date: "+from);
				System.exit(1);
			}
			try {
				dates = getDates(to);
				lto   = Long.parseLong(dates[0]);
			} catch (Exception e) {
				System.err.println("Invalid date: "+to);
				System.exit(1);
			}
		}

		report.setFrom(lfrom);
		report.setTo(lto);
		report.setOutputFile(outfile);
		report.run();
	}

	private static String[] getDates(String d) throws Exception {
		if (d.equals("ALL"))
			return new String[] { "1", ""+System.currentTimeMillis() };

		TimeZone utc = TimeZone.getTimeZone("UTC");
		Calendar cal = new GregorianCalendar(utc);
		if (d.matches("20\\d\\d-\\d\\d-\\d\\d")) {
			cal.set(Calendar.YEAR,         Integer.parseInt(d.substring(0, 4)));
			cal.set(Calendar.MONTH,        Integer.parseInt(d.substring(5, 7))-1);
			cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(d.substring(8, 10)));
		} else if (d.equals("yesterday")) {
			cal.add(Calendar.DAY_OF_YEAR, -1);
		} else
			throw new Exception("wa?");
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long start = cal.getTimeInMillis();
		long end   = start + (24 * 60 * 60 * 1000L) - 1;
		return new String[] { ""+start, ""+end };
	}
}