1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.reports;
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;
37 import java.util.TreeSet;
39 import org.apache.log4j.Logger;
40 import org.onap.dmaap.datarouter.provisioning.utils.DB;
43 * Generate a traffic volume report. The report is a .csv file containing the following columns:
45 * <tr><td>date</td><td>the date for this record</td></tr>
46 * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>
47 * <tr><td>filespublished</td><td>the number of files published on this feed and date</td></tr>
48 * <tr><td>bytespublished</td><td>the number of bytes published on this feed and date</td></tr>
49 * <tr><td>filesdelivered</td><td>the number of files delivered on this feed and date</td></tr>
50 * <tr><td>bytesdelivered</td><td>the number of bytes delivered on this feed and date</td></tr>
51 * <tr><td>filesexpired</td><td>the number of files expired on this feed and date</td></tr>
52 * <tr><td>bytesexpired</td><td>the number of bytes expired on this feed and date</td></tr>
55 * @author Robert P. Eby
56 * @version $Id: VolumeReport.java,v 1.3 2014/02/28 15:11:13 eby Exp $
58 public class VolumeReport extends ReportBase {
59 private static final String SELECT_SQL = "select EVENT_TIME, TYPE, FEEDID, CONTENT_LENGTH, RESULT" +
60 " from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ? LIMIT ?, ?";
61 private Logger loggerVolumeReport=Logger.getLogger("org.onap.dmaap.datarouter.reports");
62 private class Counters {
63 int filespublished, filesdelivered, filesexpired;
64 long bytespublished, bytesdelivered, bytesexpired;
67 public String toString() {
68 return String.format("%d,%d,%d,%d,%d,%d",
69 filespublished, bytespublished, filesdelivered,
70 bytesdelivered, filesexpired, bytesexpired);
76 Map<String, Counters> map = new HashMap<String, Counters>();
77 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
78 long start = System.currentTimeMillis();
81 @SuppressWarnings("resource")
82 Connection conn = db.getConnection();
83 // We need to run this SELECT in stages, because otherwise we run out of memory!
84 final long stepsize = 6000000L;
85 boolean go_again = true;
86 for (long i = 0; go_again; i += stepsize) {
87 try (PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {
91 ps.setLong(4, stepsize);
92 try(ResultSet rs = ps.executeQuery()) {
96 long etime = rs.getLong("EVENT_TIME");
97 String type = rs.getString("TYPE");
98 int feed = rs.getInt("FEEDID");
99 long clen = rs.getLong("CONTENT_LENGTH");
100 String key = sdf.format(new Date(etime)) + ":" + feed;
101 Counters c = map.get(key);
106 if (type.equalsIgnoreCase("pub")) {
108 c.bytespublished += clen;
109 } else if (type.equalsIgnoreCase("del")) {
110 // Only count successful deliveries
111 int statusCode = rs.getInt("RESULT");
112 if (statusCode >= 200 && statusCode < 300) {
114 c.bytesdelivered += clen;
116 } else if (type.equalsIgnoreCase("exp")) {
118 c.bytesexpired += clen;
123 catch (SQLException sqlException)
125 loggerVolumeReport.error("SqlException",sqlException);
130 } catch (SQLException e) {
131 loggerVolumeReport.error("SQLException: " + e.getMessage());
133 logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");
134 try (PrintWriter os = new PrintWriter(outfile)) {
135 os.println("date,feedid,filespublished,bytespublished,filesdelivered,bytesdelivered,filesexpired,bytesexpired");
136 for(String key :new TreeSet<String>(map.keySet()))
138 Counters c = map.get(key);
139 String[] p = key.split(":");
140 os.println(String.format("%s,%s,%s", p[0], p[1], c.toString()));
143 catch (FileNotFoundException e) {
144 System.err.println("File cannot be written: " + outfile);