7d5268b15ae042636cdee0d19ac8cd10b0966554
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / VolumeReport.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.reports;
26
27 import java.io.FileNotFoundException;
28 import java.io.PrintWriter;
29 import java.sql.Connection;
30 import java.sql.PreparedStatement;
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.TreeSet;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import org.onap.dmaap.datarouter.provisioning.utils.DB;
42
43 /**
44  * Generate a traffic volume report. The report is a .csv file containing the following columns:
45  * <table>
46  * <tr><td>date</td><td>the date for this record</td></tr>
47  * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>
48  * <tr><td>filespublished</td><td>the number of files published on this feed and date</td></tr>
49  * <tr><td>bytespublished</td><td>the number of bytes published on this feed and date</td></tr>
50  * <tr><td>filesdelivered</td><td>the number of files delivered on this feed and date</td></tr>
51  * <tr><td>bytesdelivered</td><td>the number of bytes delivered on this feed and date</td></tr>
52  * <tr><td>filesexpired</td><td>the number of files expired on this feed and date</td></tr>
53  * <tr><td>bytesexpired</td><td>the number of bytes expired on this feed and date</td></tr>
54  * </table>
55  *
56  * @author Robert P. Eby
57  * @version $Id: VolumeReport.java,v 1.3 2014/02/28 15:11:13 eby Exp $
58  */
59 public class VolumeReport extends ReportBase {
60     private static final String SELECT_SQL = "select EVENT_TIME, TYPE, FEEDID, CONTENT_LENGTH, RESULT" +
61             " from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ? LIMIT ?, ?";
62     private EELFLogger loggerVolumeReport= EELFManager.getInstance().getLogger("ReportLog");;
63     private class Counters {
64         int filespublished, filesdelivered, filesexpired;
65         long bytespublished, bytesdelivered, bytesexpired;
66
67         @Override
68         public String toString() {
69             return String.format("%d,%d,%d,%d,%d,%d",
70                     filespublished, bytespublished, filesdelivered,
71                     bytesdelivered, filesexpired, bytesexpired);
72         }
73     }
74
75     @Override
76     public void run() {
77         Map<String, Counters> map = new HashMap<String, Counters>();
78         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
79         long start = System.currentTimeMillis();
80         try {
81             DB db = new DB();
82             @SuppressWarnings("resource")
83             Connection conn = db.getConnection();
84             // We need to run this SELECT in stages, because otherwise we run out of memory!
85             final long stepsize = 6000000L;
86             boolean go_again = true;
87             for (long i = 0; go_again; i += stepsize) {
88                 try (PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {
89                     ps.setLong(1, from);
90                     ps.setLong(2, to);
91                     ps.setLong(3, i);
92                     ps.setLong(4, stepsize);
93                     try(ResultSet rs = ps.executeQuery()) {
94                         go_again = false;
95                         while (rs.next()) {
96                             go_again = true;
97                             long etime = rs.getLong("EVENT_TIME");
98                             String type = rs.getString("TYPE");
99                             int feed = rs.getInt("FEEDID");
100                             long clen = rs.getLong("CONTENT_LENGTH");
101                             String key = sdf.format(new Date(etime)) + ":" + feed;
102                             Counters c = map.get(key);
103                             if (c == null) {
104                                 c = new Counters();
105                                 map.put(key, c);
106                             }
107                             if (type.equalsIgnoreCase("pub")) {
108                                 c.filespublished++;
109                                 c.bytespublished += clen;
110                             } else if (type.equalsIgnoreCase("del")) {
111                                 // Only count successful deliveries
112                                 int statusCode = rs.getInt("RESULT");
113                                 if (statusCode >= 200 && statusCode < 300) {
114                                     c.filesdelivered++;
115                                     c.bytesdelivered += clen;
116                                 }
117                             } else if (type.equalsIgnoreCase("exp")) {
118                                 c.filesexpired++;
119                                 c.bytesexpired += clen;
120                             }
121                         }
122                     }
123                 }
124                 catch (SQLException sqlException)
125                 {
126                     loggerVolumeReport.error("SqlException",sqlException);
127                 }
128             }
129
130             db.release(conn);
131         } catch (SQLException e) {
132             loggerVolumeReport.error("SQLException: " + e.getMessage());
133         }
134         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");
135         try (PrintWriter os = new PrintWriter(outfile)) {
136             os.println("date,feedid,filespublished,bytespublished,filesdelivered,bytesdelivered,filesexpired,bytesexpired");
137             for(String key :new TreeSet<String>(map.keySet()))
138             {
139                 Counters c = map.get(key);
140                 String[] p = key.split(":");
141                 os.println(String.format("%s,%s,%s", p[0], p[1], c.toString()));
142             }
143         }
144         catch (FileNotFoundException e) {
145             System.err.println("File cannot be written: " + outfile);
146         }
147     }
148 }