1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package org.onap.dmaap.datarouter.reports;
\r
27 import java.io.FileNotFoundException;
\r
28 import java.io.PrintWriter;
\r
29 import java.sql.Connection;
\r
30 import java.sql.PreparedStatement;
\r
31 import java.sql.ResultSet;
\r
32 import java.sql.SQLException;
\r
33 import java.util.HashMap;
\r
34 import java.util.Map;
\r
35 import java.util.TreeSet;
\r
37 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
40 * Generate a subscribers report. The report is a .CSV file. It contains information per-day and per-subscriber,
\r
41 * on the status codes returned from each delivery attempt (1XX, 2XX, etc.) as well as a count of 4XX instead of a 100.
\r
43 * @author Robert P. Eby
\r
44 * @version $Id: SubscriberReport.java,v 1.2 2013/11/06 16:23:55 eby Exp $
\r
46 public class SubscriberReport extends ReportBase {
\r
47 private static final String SELECT_SQL =
\r
48 "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, RESULT, COUNT(RESULT) as COUNT" +
\r
49 " from LOG_RECORDS" +
\r
50 " where TYPE = 'del' and EVENT_TIME >= ? and EVENT_TIME <= ?" +
\r
51 " group by DATE, DELIVERY_SUBID, RESULT";
\r
52 private static final String SELECT_SQL2 =
\r
53 "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, COUNT(CONTENT_LENGTH_2) as COUNT" +
\r
54 " from LOG_RECORDS" +
\r
55 " where TYPE = 'dlx' and CONTENT_LENGTH_2 = -1 and EVENT_TIME >= ? and EVENT_TIME <= ?" +
\r
56 " group by DATE, DELIVERY_SUBID";
\r
58 private class Counters {
\r
59 private String date;
\r
61 private int c100, c200, c300, c400, c500, cm1, cdlx;
\r
63 public Counters(String date, int sub) {
\r
66 c100 = c200 = c300 = c400 = c500 = cm1 = cdlx = 0;
\r
69 public void addCounts(int status, int n) {
\r
72 } else if (status >= 100 && status <= 199) {
\r
74 } else if (status >= 200 && status <= 299) {
\r
76 } else if (status >= 300 && status <= 399) {
\r
78 } else if (status >= 400 && status <= 499) {
\r
80 } else if (status >= 500 && status <= 599) {
\r
85 public void addDlxCount(int n) {
\r
90 public String toString() {
\r
91 return date + "," + sub + "," +
\r
92 c100 + "," + c200 + "," + c300 + "," + c400 + "," + c500 + "," +
\r
99 Map<String, Counters> map = new HashMap<String, Counters>();
\r
100 long start = System.currentTimeMillis();
\r
104 @SuppressWarnings("resource")
\r
105 Connection conn = db.getConnection();
\r
106 try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {
\r
107 ps.setLong(1, from);
\r
109 try(ResultSet rs = ps.executeQuery()) {
\r
110 while (rs.next()) {
\r
111 String date = rs.getString("DATE");
\r
112 int sub = rs.getInt("DELIVERY_SUBID");
\r
113 int res = rs.getInt("RESULT");
\r
114 int count = rs.getInt("COUNT");
\r
115 String key = date + "," + sub;
\r
116 Counters c = map.get(key);
\r
118 c = new Counters(date, sub);
\r
121 c.addCounts(res, count);
\r
126 try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {
\r
127 ps2.setLong(1, from);
\r
128 ps2.setLong(2, to);
\r
129 try(ResultSet rs2 = ps2.executeQuery()) {
\r
130 while (rs2.next()) {
\r
131 String date = rs2.getString("DATE");
\r
132 int sub = rs2.getInt("DELIVERY_SUBID");
\r
133 int count = rs2.getInt("COUNT");
\r
134 String key = date + "," + sub;
\r
135 Counters c = map.get(key);
\r
137 c = new Counters(date, sub);
\r
140 c.addDlxCount(count);
\r
146 } catch (SQLException e) {
\r
147 e.printStackTrace();
\r
149 logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");
\r
150 try (PrintWriter os = new PrintWriter(outfile)){
\r
151 os.println("date,subid,count100,count200,count300,count400,count500,countminus1,countdlx");
\r
152 for (String key : new TreeSet<String>(map.keySet())) {
\r
153 Counters c = map.get(key);
\r
154 os.println(c.toString());
\r
156 } catch (FileNotFoundException e) {
\r
157 System.err.println("File cannot be written: " + outfile);
\r