public void run() {\r
Map<String, Counters> map = new HashMap<String, Counters>();\r
long start = System.currentTimeMillis();\r
+\r
try {\r
DB db = new DB();\r
@SuppressWarnings("resource")\r
Connection conn = db.getConnection();\r
- PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
- ps.setLong(1, from);\r
- ps.setLong(2, to);\r
- ResultSet rs = ps.executeQuery();\r
- while (rs.next()) {\r
- String date = rs.getString("DATE");\r
- int sub = rs.getInt("DELIVERY_SUBID");\r
- int res = rs.getInt("RESULT");\r
- int count = rs.getInt("COUNT");\r
- String key = date + "," + sub;\r
- Counters c = map.get(key);\r
- if (c == null) {\r
- c = new Counters(date, sub);\r
- map.put(key, c);\r
+ try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
+ ps.setLong(1, from);\r
+ ps.setLong(2, to);\r
+ try(ResultSet rs = ps.executeQuery()) {\r
+ while (rs.next()) {\r
+ String date = rs.getString("DATE");\r
+ int sub = rs.getInt("DELIVERY_SUBID");\r
+ int res = rs.getInt("RESULT");\r
+ int count = rs.getInt("COUNT");\r
+ String key = date + "," + sub;\r
+ Counters c = map.get(key);\r
+ if (c == null) {\r
+ c = new Counters(date, sub);\r
+ map.put(key, c);\r
+ }\r
+ c.addCounts(res, count);\r
+ }\r
}\r
- c.addCounts(res, count);\r
}\r
- rs.close();\r
- ps.close();\r
\r
- ps = conn.prepareStatement(SELECT_SQL2);\r
- ps.setLong(1, from);\r
- ps.setLong(2, to);\r
- rs = ps.executeQuery();\r
- while (rs.next()) {\r
- String date = rs.getString("DATE");\r
- int sub = rs.getInt("DELIVERY_SUBID");\r
- int count = rs.getInt("COUNT");\r
- String key = date + "," + sub;\r
- Counters c = map.get(key);\r
- if (c == null) {\r
- c = new Counters(date, sub);\r
- map.put(key, c);\r
- }\r
- c.addDlxCount(count);\r
- }\r
- rs.close();\r
- ps.close();\r
+ try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {\r
+ ps2.setLong(1, from);\r
+ ps2.setLong(2, to);\r
+ try(ResultSet rs2 = ps2.executeQuery()) {\r
+ while (rs2.next()) {\r
+ String date = rs2.getString("DATE");\r
+ int sub = rs2.getInt("DELIVERY_SUBID");\r
+ int count = rs2.getInt("COUNT");\r
+ String key = date + "," + sub;\r
+ Counters c = map.get(key);\r
+ if (c == null) {\r
+ c = new Counters(date, sub);\r
+ map.put(key, c);\r
+ }\r
+ c.addDlxCount(count);\r
+ }\r
+ }\r
+ }\r
\r
db.release(conn);\r
} catch (SQLException e) {\r
e.printStackTrace();\r
}\r
logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
- try {\r
- PrintWriter os = new PrintWriter(outfile);\r
+ try (PrintWriter os = new PrintWriter(outfile)){\r
os.println("date,subid,count100,count200,count300,count400,count500,countminus1,countdlx");\r
for (String key : new TreeSet<String>(map.keySet())) {\r
Counters c = map.get(key);\r
os.println(c.toString());\r
}\r
- os.close();\r
} catch (FileNotFoundException e) {\r
System.err.println("File cannot be written: " + outfile);\r
}\r
import java.util.Map;\r
import java.util.TreeSet;\r
\r
+import org.apache.log4j.Logger;\r
import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
\r
/**\r
public class VolumeReport extends ReportBase {\r
private static final String SELECT_SQL = "select EVENT_TIME, TYPE, FEEDID, CONTENT_LENGTH, RESULT" +\r
" from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ? LIMIT ?, ?";\r
-\r
+ private Logger loggerVolumeReport=Logger.getLogger("org.onap.dmaap.datarouter.reports");\r
private class Counters {\r
public int filespublished, filesdelivered, filesexpired;\r
public long bytespublished, bytesdelivered, bytesexpired;\r
final long stepsize = 6000000L;\r
boolean go_again = true;\r
for (long i = 0; go_again; i += stepsize) {\r
- PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
- ps.setLong(1, from);\r
- ps.setLong(2, to);\r
- ps.setLong(3, i);\r
- ps.setLong(4, stepsize);\r
- ResultSet rs = ps.executeQuery();\r
- go_again = false;\r
- while (rs.next()) {\r
- go_again = true;\r
- long etime = rs.getLong("EVENT_TIME");\r
- String type = rs.getString("TYPE");\r
- int feed = rs.getInt("FEEDID");\r
- long clen = rs.getLong("CONTENT_LENGTH");\r
- String key = sdf.format(new Date(etime)) + ":" + feed;\r
- Counters c = map.get(key);\r
- if (c == null) {\r
- c = new Counters();\r
- map.put(key, c);\r
- }\r
- if (type.equalsIgnoreCase("pub")) {\r
- c.filespublished++;\r
- c.bytespublished += clen;\r
- } else if (type.equalsIgnoreCase("del")) {\r
- // Only count successful deliveries\r
- int statusCode = rs.getInt("RESULT");\r
- if (statusCode >= 200 && statusCode < 300) {\r
- c.filesdelivered++;\r
- c.bytesdelivered += clen;\r
+ try (PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
+ ps.setLong(1, from);\r
+ ps.setLong(2, to);\r
+ ps.setLong(3, i);\r
+ ps.setLong(4, stepsize);\r
+ try(ResultSet rs = ps.executeQuery()) {\r
+ go_again = false;\r
+ while (rs.next()) {\r
+ go_again = true;\r
+ long etime = rs.getLong("EVENT_TIME");\r
+ String type = rs.getString("TYPE");\r
+ int feed = rs.getInt("FEEDID");\r
+ long clen = rs.getLong("CONTENT_LENGTH");\r
+ String key = sdf.format(new Date(etime)) + ":" + feed;\r
+ Counters c = map.get(key);\r
+ if (c == null) {\r
+ c = new Counters();\r
+ map.put(key, c);\r
+ }\r
+ if (type.equalsIgnoreCase("pub")) {\r
+ c.filespublished++;\r
+ c.bytespublished += clen;\r
+ } else if (type.equalsIgnoreCase("del")) {\r
+ // Only count successful deliveries\r
+ int statusCode = rs.getInt("RESULT");\r
+ if (statusCode >= 200 && statusCode < 300) {\r
+ c.filesdelivered++;\r
+ c.bytesdelivered += clen;\r
+ }\r
+ } else if (type.equalsIgnoreCase("exp")) {\r
+ c.filesexpired++;\r
+ c.bytesexpired += clen;\r
+ }\r
}\r
- } else if (type.equalsIgnoreCase("exp")) {\r
- c.filesexpired++;\r
- c.bytesexpired += clen;\r
}\r
+\r
+ }\r
+ catch (SQLException sqlException)\r
+ {\r
+ loggerVolumeReport.error("SqlException",sqlException);\r
}\r
- rs.close();\r
- ps.close();\r
}\r
+\r
db.release(conn);\r
} catch (SQLException e) {\r
e.printStackTrace();\r
}\r
logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
- try {\r
- PrintWriter os = new PrintWriter(outfile);\r
+ try (PrintWriter os = new PrintWriter(outfile)) {\r
os.println("date,feedid,filespublished,bytespublished,filesdelivered,bytesdelivered,filesexpired,bytesexpired");\r
- for (String key : new TreeSet<String>(map.keySet())) {\r
+ for(String key :new TreeSet<String>(map.keySet()))\r
+ {\r
Counters c = map.get(key);\r
String[] p = key.split(":");\r
os.println(String.format("%s,%s,%s", p[0], p[1], c.toString()));\r
}\r
- os.close();\r
- } catch (FileNotFoundException e) {\r
+ }\r
+ catch (FileNotFoundException e) {\r
System.err.println("File cannot be written: " + outfile);\r
}\r
}\r