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 com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import org.onap.dmaap.datarouter.provisioning.utils.DB;
44 * Generate a traffic volume report. The report is a .csv file containing the following columns:
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>
56 * @author Robert P. Eby
57 * @version $Id: VolumeReport.java,v 1.3 2014/02/28 15:11:13 eby Exp $
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;
68 public String toString() {
69 return String.format("%d,%d,%d,%d,%d,%d",
70 filespublished, bytespublished, filesdelivered,
71 bytesdelivered, filesexpired, bytesexpired);
77 Map<String, Counters> map = new HashMap<String, Counters>();
78 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
79 long start = System.currentTimeMillis();
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)) {
92 ps.setLong(4, stepsize);
93 try(ResultSet rs = ps.executeQuery()) {
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);
107 if (type.equalsIgnoreCase("pub")) {
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) {
115 c.bytesdelivered += clen;
117 } else if (type.equalsIgnoreCase("exp")) {
119 c.bytesexpired += clen;
124 catch (SQLException sqlException)
126 loggerVolumeReport.error("SqlException",sqlException);
131 } catch (SQLException e) {
132 loggerVolumeReport.error("SQLException: " + e.getMessage());
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()))
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()));
144 catch (FileNotFoundException e) {
145 System.err.println("File cannot be written: " + outfile);