X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Freports%2FFeedReport.java;h=652978864845c0a9ce02f6189b240e057192e323;hb=b60213dc26540543f500b3442b061565907c3cf8;hp=a3e15dc336954b3bca754af0c8161a516ae89b22;hpb=e4b20cc6f7c31f48ddd0de5bcd054b09a35cd510;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java index a3e15dc3..65297886 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.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. @@ -49,346 +49,343 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: FeedReport.java,v 1.2 2013/11/06 16:23:55 eby Exp $ */ public class FeedReport extends ReportBase { - private static final String SELECT_SQL = - // Note to use the time in the publish_id, use date(from_unixtime(substring(publish_id, 1, 10))) - // To just use month, substring(from_unixtime(event_time div 1000), 1, 7) - "select date(from_unixtime(event_time div 1000)) as date, type, feedid, delivery_subid, count(*) as count" + - " from LOG_RECORDS" + - " where type = 'pub' or type = 'del'" + - " group by date, type, feedid, delivery_subid"; - private static final String SELECT_SQL_OLD = - "select PUBLISH_ID, TYPE, FEEDID, DELIVERY_SUBID from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ?"; + private static final String SELECT_SQL = + // Note to use the time in the publish_id, use date(from_unixtime(substring(publish_id, 1, 10))) + // To just use month, substring(from_unixtime(event_time div 1000), 1, 7) + "select date(from_unixtime(event_time div 1000)) as date, type, feedid, delivery_subid, count(*) as count" + + " from LOG_RECORDS" + + " where type = 'pub' or type = 'del'" + + " group by date, type, feedid, delivery_subid"; + private static final String SELECT_SQL_OLD = + "select PUBLISH_ID, TYPE, FEEDID, DELIVERY_SUBID from LOG_RECORDS where EVENT_TIME >= ? and EVENT_TIME <= ?"; + + @Override + public void run() { + boolean alg1 = true; + JSONObject jo = new JSONObject(); + long start = System.currentTimeMillis(); + StringBuilder sb = new StringBuilder(); + try { + DB db = new DB(); + @SuppressWarnings("resource") + Connection conn = db.getConnection(); + try( PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) { + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + String date = rs.getString("date"); + String type = rs.getString("type"); + int feedid = rs.getInt("feedid"); + int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0; + int count = rs.getInt("count"); + sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n"); + } + } + } + db.release(conn); + } catch (SQLException e) { + logger.error(e.toString()); + } + logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms"); + try (PrintWriter os = new PrintWriter(outfile)) { + os.print("date,type,feedid,subid,count\n"); + os.print(sb.toString()); + } catch (FileNotFoundException e) { + System.err.println("File cannot be written: " + outfile); + logger.error(e.toString()); + } + } + + public void run2() { + JSONObject jo = new JSONObject(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + long start = System.currentTimeMillis(); + try { + DB db = new DB(); + @SuppressWarnings("resource") + Connection conn = db.getConnection(); + try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL_OLD)) { + ps.setLong(1, from); + ps.setLong(2, to); + ps.setFetchSize(100000); + try(ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + String id = rs.getString("PUBLISH_ID"); + String date = sdf.format(new Date(getPstart(id))); + JSONObject datemap = jo.optJSONObject(date); + if (datemap == null) { + datemap = new JSONObject(); + jo.put(date, datemap); + } + int feed = rs.getInt("FEEDID"); + JSONObject feedmap = datemap.optJSONObject("" + feed); + if (feedmap == null) { + feedmap = new JSONObject(); + feedmap.put("pubcount", 0); + datemap.put("" + feed, feedmap); + } + String type = rs.getString("TYPE"); + if (type.equals("pub")) { + try { + int n = feedmap.getInt("pubcount"); + feedmap.put("pubcount", n + 1); + } catch (JSONException e) { + feedmap.put("pubcount", 1); + logger.error(e.toString()); + } + } else if (type.equals("del")) { + String subid = "" + rs.getInt("DELIVERY_SUBID"); + try { + int n = feedmap.getInt(subid); + feedmap.put(subid, n + 1); + } catch (JSONException e) { + feedmap.put(subid, 1); + logger.error(e.toString()); + } + } + } + } + } + db.release(conn); + } catch (SQLException e) { + logger.error(e.toString()); + } + logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms"); + try { + PrintWriter os = new PrintWriter(outfile); + os.println(toHTML(jo)); + os.close(); + } catch (FileNotFoundException e) { + System.err.println("File cannot be written: " + outfile); + logger.error(e.toString()); + } + } + + private long getPstart(String t) { + if (t.indexOf('.') > 0) + t = t.substring(0, t.indexOf('.')); + return Long.parseLong(t); + } + + @SuppressWarnings("unused") + private static String toHTMLNested(JSONObject jo) { + StringBuilder s = new StringBuilder(); + s.append("\n"); + s.append("\n"); + String[] dates = JSONObject.getNames(jo); + Arrays.sort(dates); + for (int i = dates.length - 1; i >= 0; i--) { + String date = dates[i]; + JSONObject j2 = jo.getJSONObject(date); + String[] feeds = JSONObject.getNames(j2); + Arrays.sort(feeds); + s.append("\n"); + } + s.append("
DateFeeds
" + date + ""); + s.append(feeds.length).append(feeds.length > 1 ? " Feeds\n" : " Feed\n"); + s.append("\n"); + s.append("\n"); + for (String feed : feeds) { + JSONObject j3 = j2.getJSONObject(feed); + String[] subs = JSONObject.getNames(j3); + Arrays.sort(subs); + s.append(""); + s.append(""); + int scnt = j3.length() - 1; + s.append("\n"); + } + s.append("
Feed IDPublish CountSubscriptions
" + feed + "" + j3.getInt("pubcount") + "").append(scnt).append(" Subcription"); + if (scnt > 1) + s.append("s"); + s.append("\n"); + s.append("\n"); + for (String sub : subs) { + if (!sub.equals("pubcount")) { + s.append(""); + s.append(""); + s.append("\n"); + } + } + s.append("
Sub IDDelivery Count
" + sub + "" + j3.getInt(sub) + "
\n"); - @Override - public void run() { - boolean alg1 = true; - JSONObject jo = new JSONObject(); - long start = System.currentTimeMillis(); - StringBuilder sb = new StringBuilder(); - 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(); - while (rs.next()) { - if (alg1) { - String date = rs.getString("date"); - String type = rs.getString("type"); - int feedid = rs.getInt("feedid"); - int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0; - int count = rs.getInt("count"); - sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n"); - } else { - String date = rs.getString("date"); - JSONObject datemap = jo.optJSONObject(date); - if (datemap == null) { - datemap = new JSONObject(); - jo.put(date, datemap); - } - int feed = rs.getInt("FEEDID"); - JSONObject feedmap = datemap.optJSONObject(""+feed); - if (feedmap == null) { - feedmap = new JSONObject(); - feedmap.put("pubcount", 0); - datemap.put(""+feed, feedmap); - } - String type = rs.getString("TYPE"); - int count = rs.getInt("count"); - if (type.equals("pub")) { - feedmap.put("pubcount", count); - } else if (type.equals("del")) { - String subid = ""+rs.getInt("DELIVERY_SUBID"); - feedmap.put(subid, count); - } - } - } - 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); - if (alg1) { - os.print("date,type,feedid,subid,count\n"); - os.print(sb.toString()); - } else { - os.println(toHTML(jo)); - } - os.close(); - } catch (FileNotFoundException e) { - System.err.println("File cannot be written: "+outfile); - } - } + s.append("
\n"); + s.append("
\n"); + return s.toString(); + } - public void run2() { - JSONObject jo = new JSONObject(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - long start = System.currentTimeMillis(); - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - PreparedStatement ps = conn.prepareStatement(SELECT_SQL_OLD); - ps.setLong(1, from); - ps.setLong(2, to); - ps.setFetchSize(100000); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - String id = rs.getString("PUBLISH_ID"); - String date = sdf.format(new Date(getPstart(id))); - JSONObject datemap = jo.optJSONObject(date); - if (datemap == null) { - datemap = new JSONObject(); - jo.put(date, datemap); - } - int feed = rs.getInt("FEEDID"); - JSONObject feedmap = datemap.optJSONObject(""+feed); - if (feedmap == null) { - feedmap = new JSONObject(); - feedmap.put("pubcount", 0); - datemap.put(""+feed, feedmap); - } - String type = rs.getString("TYPE"); - if (type.equals("pub")) { - try { - int n = feedmap.getInt("pubcount"); - feedmap.put("pubcount", n+1); - } catch (JSONException e) { - feedmap.put("pubcount", 1); - } - } else if (type.equals("del")) { - String subid = ""+rs.getInt("DELIVERY_SUBID"); - try { - int n = feedmap.getInt(subid); - feedmap.put(subid, n+1); - } catch (JSONException e) { - feedmap.put(subid, 1); - } - } - } - 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); - os.println(toHTML(jo)); - os.close(); - } catch (FileNotFoundException e) { - System.err.println("File cannot be written: "+outfile); - } - } - private long getPstart(String t) { - if (t.indexOf('.') > 0) - t = t.substring(0, t.indexOf('.')); - return Long.parseLong(t); - } - @SuppressWarnings("unused") - private static String toHTMLNested(JSONObject jo) { - StringBuilder s = new StringBuilder(); - s.append("\n"); - s.append("\n"); - String[] dates = JSONObject.getNames(jo); - Arrays.sort(dates); - for (int i = dates.length-1; i >= 0; i--) { - String date = dates[i]; - JSONObject j2 = jo.getJSONObject(date); - String[] feeds = JSONObject.getNames(j2); - Arrays.sort(feeds); - s.append("\n"); - } - s.append("
DateFeeds
"+date+""); - s.append(feeds.length).append(feeds.length > 1 ? " Feeds\n" : " Feed\n"); - s.append("\n"); - s.append("\n"); - for (String feed : feeds) { - JSONObject j3 = j2.getJSONObject(feed); - String[] subs = JSONObject.getNames(j3); - Arrays.sort(subs); - s.append(""); - s.append(""); - int scnt = j3.length()-1; - s.append("\n"); - } - s.append("
Feed IDPublish CountSubscriptions
"+feed+""+j3.getInt("pubcount")+"").append(scnt).append(" Subcription"); - if (scnt > 1) - s.append("s"); - s.append("\n"); - s.append("\n"); - for (String sub : subs) { - if (!sub.equals("pubcount")) { - s.append(""); - s.append(""); - s.append("\n"); - } - } - s.append("
Sub IDDelivery Count
"+sub+""+j3.getInt(sub)+"
\n"); + private static String toHTML(JSONObject jo) { + StringBuilder s = new StringBuilder(); + s.append("\n"); + s.append("\n"); + String[] dates = JSONObject.getNames(jo); + Arrays.sort(dates); + for (int i = dates.length - 1; i >= 0; i--) { + String date = dates[i]; + JSONObject j2 = jo.getJSONObject(date); + int rc1 = countrows(j2); + String[] feeds = JSONObject.getNames(j2); + Arrays.sort(feeds); + s.append(""); + s.append(""); + String px1 = ""; + for (String feed : feeds) { + JSONObject j3 = j2.getJSONObject(feed); + int pubcount = j3.getInt("pubcount"); + int subcnt = j3.length() - 1; + int rc2 = (subcnt < 1) ? 1 : subcnt; + String[] subs = JSONObject.getNames(j3); + Arrays.sort(subs); + s.append(px1) + .append(""); + s.append(""); + s.append(""); + String px2 = ""; + for (String sub : subs) { + if (!sub.equals("pubcount")) { + s.append(px2); + s.append(""); + s.append(""); + s.append("\n"); + px2 = ""; + } + } + if (px2.equals("")) + s.append("\n"); + px1 = ""; + } + } + s.append("
DateFeedsFeed IDPublish CountSubsSub IDDelivery Count
") + .append(date) + .append("") + .append(feeds.length) + .append("") + .append(feed) + .append("") + .append(pubcount) + .append("") + .append(subcnt) + .append("" + sub + "" + j3.getInt(sub) + "
\n"); + return s.toString(); + } - s.append("
\n"); - s.append("
\n"); - return s.toString(); - } - private static String toHTML(JSONObject jo) { - StringBuilder s = new StringBuilder(); - s.append("\n"); - s.append("\n"); - String[] dates = JSONObject.getNames(jo); - Arrays.sort(dates); - for (int i = dates.length-1; i >= 0; i--) { - String date = dates[i]; - JSONObject j2 = jo.getJSONObject(date); - int rc1 = countrows(j2); - String[] feeds = JSONObject.getNames(j2); - Arrays.sort(feeds); - s.append(""); - s.append(""); - String px1 = ""; - for (String feed : feeds) { - JSONObject j3 = j2.getJSONObject(feed); - int pubcount = j3.getInt("pubcount"); - int subcnt = j3.length()-1; - int rc2 = (subcnt < 1) ? 1 : subcnt; - String[] subs = JSONObject.getNames(j3); - Arrays.sort(subs); - s.append(px1) - .append(""); - s.append(""); - s.append(""); - String px2 = ""; - for (String sub : subs) { - if (!sub.equals("pubcount")) { - s.append(px2); - s.append(""); - s.append(""); - s.append("\n"); - px2 = ""; - } - } - if (px2.equals("")) - s.append("\n"); - px1 = ""; - } - } - s.append("
DateFeedsFeed IDPublish CountSubsSub IDDelivery Count
") - .append(date) - .append("") - .append(feeds.length) - .append("") - .append(feed) - .append("") - .append(pubcount) - .append("") - .append(subcnt) - .append(""+sub+""+j3.getInt(sub)+"
\n"); - return s.toString(); - } - private static int countrows(JSONObject x) { - int n = 0; - for (String feed : JSONObject.getNames(x)) { - JSONObject j3 = x.getJSONObject(feed); - int subcnt = j3.length()-1; - int rc2 = (subcnt < 1) ? 1 : subcnt; - n += rc2; - } - return (n > 0) ? n : 1; - } + private static int countrows(JSONObject x) { + int n = 0; + for (String feed : JSONObject.getNames(x)) { + JSONObject j3 = x.getJSONObject(feed); + int subcnt = j3.length() - 1; + int rc2 = (subcnt < 1) ? 1 : subcnt; + n += rc2; + } + return (n > 0) ? n : 1; + } - /** - * Convert a .CSV file (as generated by the normal FeedReport mechanism) to an HTML table. - * @param args - */ - public static void main(String[] args) { - int rtype = 0; // 0 -> day, 1 -> week, 2 -> month, 3 -> year - String infile = null; - String outfile = null; - for (int i = 0; i < args.length; i++) { - if (args[i].equals("-t")) { - switch (args[++i].charAt(0)) { - case 'w': rtype = 1; break; - case 'm': rtype = 2; break; - case 'y': rtype = 3; break; - default: rtype = 0; break; - } - } else if (infile == null) { - infile = args[i]; - } else if (outfile == null) { - outfile = args[i]; - } - } - if (infile == null) { - System.err.println("usage: FeedReport [ -t ] [ ] [ ]"); - System.exit(1); - } - try { - JSONObject jo = new JSONObject(); - LineNumberReader lr = new LineNumberReader(new FileReader(infile)); - String line = lr.readLine(); - while (line != null) { - String[] tt = line.split(","); - if (tt[0].startsWith("2")) { - String date = tt[0]; - switch (rtype) { - case 1: - String[] xx = date.split("-"); - Calendar cal = new GregorianCalendar(new Integer(xx[0]), new Integer(xx[1])-1, new Integer(xx[2])); - date = xx[0] + "-W" + cal.get(Calendar.WEEK_OF_YEAR); - break; - case 2: date = date.substring(0, 7); break; - case 3: date = date.substring(0, 4); break; - } - JSONObject datemap = jo.optJSONObject(date); - if (datemap == null) { - datemap = new JSONObject(); - jo.put(date, datemap); - } - int feed = Integer.parseInt(tt[2]); - JSONObject feedmap = datemap.optJSONObject(""+feed); - if (feedmap == null) { - feedmap = new JSONObject(); - feedmap.put("pubcount", 0); - datemap.put(""+feed, feedmap); - } - String type = tt[1]; - int count = Integer.parseInt(tt[4]); - if (type.equals("pub")) { - try { - int n = feedmap.getInt("pubcount"); - feedmap.put("pubcount", n+count); - } catch (JSONException e) { - feedmap.put("pubcount", count); - } - } else if (type.equals("del")) { - String subid = tt[3]; - try { - int n = feedmap.getInt(subid); - feedmap.put(subid, n+count); - } catch (JSONException e) { - feedmap.put(subid, count); - } - } - } - line = lr.readLine(); - } - lr.close(); - String t = toHTML(jo); - switch (rtype) { - case 1: t = t.replaceAll("Date", "Week"); break; - case 2: t = t.replaceAll("Date", "Month"); break; - case 3: t = t.replaceAll("Date", "Year"); break; - } - System.out.println(t); - } catch (Exception e) { - System.err.println(e); - e.printStackTrace(); - } - } + /** + * Convert a .CSV file (as generated by the normal FeedReport mechanism) to an HTML table. + * + * @param args + */ + public void main(String[] args) { + int rtype = 0; // 0 -> day, 1 -> week, 2 -> month, 3 -> year + String infile = null; + String outfile = null; + for (int i = 0; i < args.length; i++) { + if (args[i].equals("-t")) { + switch (args[++i].charAt(0)) { + case 'w': + rtype = 1; + break; + case 'm': + rtype = 2; + break; + case 'y': + rtype = 3; + break; + default: + rtype = 0; + break; + } + } else if (infile == null) { + infile = args[i]; + } else if (outfile == null) { + outfile = args[i]; + } + } + if (infile == null) { + System.err.println("usage: FeedReport [ -t ] [ ] [ ]"); + System.exit(1); + } + try { + JSONObject jo = new JSONObject(); + try(LineNumberReader lr = new LineNumberReader(new FileReader(infile))) { + String line = lr.readLine(); + while (line != null) { + String[] tt = line.split(","); + if (tt[0].startsWith("2")) { + String date = tt[0]; + switch (rtype) { + case 1: + String[] xx = date.split("-"); + Calendar cal = new GregorianCalendar(new Integer(xx[0]), new Integer(xx[1]) - 1, new Integer(xx[2])); + date = xx[0] + "-W" + cal.get(Calendar.WEEK_OF_YEAR); + break; + case 2: + date = date.substring(0, 7); + break; + case 3: + date = date.substring(0, 4); + break; + } + JSONObject datemap = jo.optJSONObject(date); + if (datemap == null) { + datemap = new JSONObject(); + jo.put(date, datemap); + } + int feed = Integer.parseInt(tt[2]); + JSONObject feedmap = datemap.optJSONObject("" + feed); + if (feedmap == null) { + feedmap = new JSONObject(); + feedmap.put("pubcount", 0); + datemap.put("" + feed, feedmap); + } + String type = tt[1]; + int count = Integer.parseInt(tt[4]); + if (type.equals("pub")) { + try { + int n = feedmap.getInt("pubcount"); + feedmap.put("pubcount", n + count); + } catch (JSONException e) { + feedmap.put("pubcount", count); + logger.error(e.toString()); + } + } else if (type.equals("del")) { + String subid = tt[3]; + try { + int n = feedmap.getInt(subid); + feedmap.put(subid, n + count); + } catch (JSONException e) { + feedmap.put(subid, count); + logger.error(e.toString()); + } + } + } + line = lr.readLine(); + } + } + String t = toHTML(jo); + switch (rtype) { + case 1: + t = t.replaceAll("Date", "Week"); + break; + case 2: + t = t.replaceAll("Date", "Month"); + break; + case 3: + t = t.replaceAll("Date", "Year"); + break; + } + System.out.println(t); + } catch (Exception e) { + System.err.println(e); + logger.error(e.toString()); + } + } }