From c2b240715d463f197c7b8fe482f512ac5feccbd1 Mon Sep 17 00:00:00 2001 From: Amaresh Kumar Date: Thu, 30 Aug 2018 19:01:35 +0530 Subject: fix for sonar blocker in DailyLatencyReport.Java Fixed 3 Sonar issues. https://sonar.onap.org/project/issues?fileUuids=AWHFaQitEU5FCDfKtnEP&id=org.onap.dmaap.datarouter%3Aparent&resolved=false&severities=BLOCKER Issue-ID: DMAAP-678 Change-Id: I148d3e2af84c2bf5f3ed8c1aa1460bfb679c0948 Signed-off-by: Amaresh Kumar --- .../datarouter/reports/DailyLatencyReport.java | 47 +++++++++++----------- 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'datarouter-prov/src/main/java/org') diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/DailyLatencyReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/DailyLatencyReport.java index a5281c06..28740c0f 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/DailyLatencyReport.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/DailyLatencyReport.java @@ -152,41 +152,40 @@ public class DailyLatencyReport extends ReportBase { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - PreparedStatement ps = conn.prepareStatement(SELECT_SQL); - ps.setLong(1, from); - ps.setLong(2, to); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - String id = rs.getString("PUBLISH_ID"); - int feed = rs.getInt("FEEDID"); - long etime = rs.getLong("EVENT_TIME"); - String type = rs.getString("TYPE"); - String fid = rs.getString("FEED_FILEID"); - long clen = rs.getLong("CONTENT_LENGTH"); - String date = sdf.format(new Date(getPstart(id))); - String key = date + "," + feed; - Counters c = map.get(key); - if (c == null) { - c = new Counters(date, feed); - map.put(key, c); + try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) { + ps.setLong(1, from); + ps.setLong(2, to); + try(ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + String id = rs.getString("PUBLISH_ID"); + int feed = rs.getInt("FEEDID"); + long etime = rs.getLong("EVENT_TIME"); + String type = rs.getString("TYPE"); + String fid = rs.getString("FEED_FILEID"); + long clen = rs.getLong("CONTENT_LENGTH"); + String date = sdf.format(new Date(getPstart(id))); + String key = date + "," + feed; + Counters c = map.get(key); + if (c == null) { + c = new Counters(date, feed); + map.put(key, c); + } + c.addEvent(etime, type, id, fid, clen); + } } - c.addEvent(etime, type, id, fid, clen); + + db.release(conn); } - rs.close(); - ps.close(); - db.release(conn); } catch (SQLException e) { e.printStackTrace(); } logger.debug("Query time: " + (System.currentTimeMillis()-start) + " ms"); - try { - PrintWriter os = new PrintWriter(outfile); + try (PrintWriter os = new PrintWriter(outfile)){ os.println("date,feedid,minsize,maxsize,avgsize,minlat,maxlat,avglat,fanout"); for (String key : new TreeSet(map.keySet())) { Counters c = map.get(key); os.println(c.toString()); } - os.close(); } catch (FileNotFoundException e) { System.err.println("File cannot be written: "+outfile); } -- cgit 1.2.3-korg