X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Freports%2FLatencyReport.java;h=f98116d24067bbd8ff83e2154117a22dcc60ed36;hb=68a9ca240970fceaf12bbe91b7bad8e1d98ecd93;hp=c213c03d863811a7360a16f1287106c3ddd80de5;hpb=e4b20cc6f7c31f48ddd0de5bcd054b09a35cd510;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java index c213c03d..f98116d2 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java @@ -7,9 +7,9 @@ * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at - * * + * * * * http://www.apache.org/licenses/LICENSE-2.0 - * * + * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -32,8 +32,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; - -import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils; /** * Generate a per-file latency report. It reports on the details related to one file published @@ -54,126 +53,128 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: LatencyReport.java,v 1.1 2013/10/28 18:06:53 eby Exp $ */ public class LatencyReport extends ReportBase { - private static final String SELECT_SQL = - "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS" + - " where EVENT_TIME >= ? and EVENT_TIME <= ? order by PUBLISH_ID, EVENT_TIME"; - - private class Event { - public final String type; - public final long time; - public Event(String t, long tm) { - type = t; - time = tm; - } - } - private class Counters { - public final String id; - public final int feedid; - public final long clen; - public final String fileid; - public final List events; - public Counters(String i, int fid, long c, String s) { - id = i; - feedid = fid; - clen = c; - fileid = s; - events = new ArrayList(); - } - private long pubtime; - public void addEvent(String t, long tm) { - events.add(new Event(t, tm)); - if (t.equals("pub")) - pubtime = tm; - } - public long min() { - long min = Long.MAX_VALUE; - for (Event e : events) { - if (e.type.equals("del")) { - min = Math.min(min, e.time - pubtime); - } - } - return min; - } - public long max() { - long max = 0; - for (Event e : events) { - if (e.type.equals("del")) { - max = Math.max(max, e.time - pubtime); - } - } - return max; - } - public long avg() { - long total = 0, c = 0; - for (Event e : events) { - if (e.type.equals("del")) { - total += e.time - pubtime; - c++; - } - } - return (c == 0) ? 0 : total/c; - } - public int fanout() { - int n = 0; - for (Event e : events) { - if (e.type.equals("del")) { - n++; - } - } - return n; - } - @Override - public String toString() { - return feedid + "," + fileid + "," + clen + "," + min() + "," + max() + "," + avg() + "," + fanout(); - } - } - - @Override - public void run() { - long start = System.currentTimeMillis(); - try { - 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(); - PrintWriter os = new PrintWriter(outfile); - os.println("recordid,feedid,uri,size,min,max,avg,fanout"); - Counters c = null; - while (rs.next()) { - long etime = rs.getLong("EVENT_TIME"); - String type = rs.getString("TYPE"); - String id = rs.getString("PUBLISH_ID"); - String fid = rs.getString("FEED_FILEID"); - int feed = rs.getInt("FEEDID"); - long clen = rs.getLong("CONTENT_LENGTH"); - if (c != null && !id.equals(c.id)) { - String line = id + "," + c.toString(); - os.println(line); - c = null; - } - if (c == null) { - c = new Counters(id, feed, clen, fid); - } - if (feed != c.feedid) - System.err.println("Feed ID mismatch, "+feed+" <=> "+c.feedid); - if (clen != c.clen) - System.err.println("Cont Len mismatch, "+clen+" <=> "+c.clen); -// if (fid != c.fileid) -// System.err.println("File ID mismatch, "+fid+" <=> "+c.fileid); - c.addEvent(type, etime); - } - rs.close(); - ps.close(); - db.release(conn); - os.close(); - } catch (FileNotFoundException e) { - System.err.println("File cannot be written: "+outfile); - } catch (SQLException e) { - e.printStackTrace(); - } - logger.debug("Query time: " + (System.currentTimeMillis()-start) + " ms"); - } + + private class Event { + public final String type; + public final long time; + + public Event(String t, long tm) { + type = t; + time = tm; + } + } + + private class Counters { + public final String id; + public final int feedid; + public final long clen; + public final String fileid; + public final List events; + + public Counters(String i, int fid, long c, String s) { + id = i; + feedid = fid; + clen = c; + fileid = s; + events = new ArrayList<>(); + } + + private long pubtime; + + public void addEvent(String t, long tm) { + events.add(new Event(t, tm)); + if (t.equals("pub")) + pubtime = tm; + } + + public long min() { + long min = Long.MAX_VALUE; + for (Event e : events) { + if (e.type.equals("del")) { + min = Math.min(min, e.time - pubtime); + } + } + return min; + } + + public long max() { + long max = 0; + for (Event e : events) { + if (e.type.equals("del")) { + max = Math.max(max, e.time - pubtime); + } + } + return max; + } + + public long avg() { + long total = 0, c = 0; + for (Event e : events) { + if (e.type.equals("del")) { + total += e.time - pubtime; + c++; + } + } + return (c == 0) ? 0 : total / c; + } + + public int fanout() { + int n = 0; + for (Event e : events) { + if (e.type.equals("del")) { + n++; + } + } + return n; + } + + @Override + public String toString() { + return feedid + "," + fileid + "," + clen + "," + min() + "," + max() + "," + avg() + "," + fanout(); + } + } + + @Override + public void run() { + long start = System.currentTimeMillis(); + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement( + "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS where " + + "EVENT_TIME >= ? and EVENT_TIME <= ? order by PUBLISH_ID, EVENT_TIME")) { + ps.setLong(1, from); + ps.setLong(2, to); + try(ResultSet rs = ps.executeQuery()) { + try (PrintWriter os = new PrintWriter(outfile)) { + os.println("recordid,feedid,uri,size,min,max,avg,fanout"); + Counters c = null; + while (rs.next()) { + long etime = rs.getLong("EVENT_TIME"); + String type = rs.getString("TYPE"); + String id = rs.getString("PUBLISH_ID"); + String fid = rs.getString("FEED_FILEID"); + int feed = rs.getInt("FEEDID"); + long clen = rs.getLong("CONTENT_LENGTH"); + if (c != null && !id.equals(c.id)) { + String line = id + "," + c.toString(); + os.println(line); + c = null; + } + if (c == null) { + c = new Counters(id, feed, clen, fid); + } + if (feed != c.feedid) + System.err.println("Feed ID mismatch, " + feed + " <=> " + c.feedid); + if (clen != c.clen) + System.err.println("Cont Len mismatch, " + clen + " <=> " + c.clen); + c.addEvent(type, etime); + } + } + } + } catch (FileNotFoundException e) { + System.err.println("File cannot be written: " + outfile); + } catch (SQLException e) { + logger.error("SQLException: " + e.getMessage()); + } + logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms"); + } }