Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / LatencyReport.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.reports;\r
26 \r
27 import java.io.FileNotFoundException;\r
28 import java.io.PrintWriter;\r
29 import java.sql.Connection;\r
30 import java.sql.PreparedStatement;\r
31 import java.sql.ResultSet;\r
32 import java.sql.SQLException;\r
33 import java.util.ArrayList;\r
34 import java.util.List;\r
35 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
36 \r
37 /**\r
38  * Generate a per-file latency report.  It reports on the details related to one file published\r
39  * on one feed. This report can be further reduced in order to generate more specific reports\r
40  * based on feed ID or node name. The report is a .csv file containing the following columns:\r
41  * <table>\r
42  * <tr><td>recordid</td><td>the unique record ID assigned to a particular incoming feed</td></tr>\r
43  * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>\r
44  * <tr><td>uri</td><td>the URI of the file delivered</td></tr>\r
45  * <tr><td>size</td><td>the size of the file delivered</td></tr>\r
46  * <tr><td>min</td><td>the minimum latency in delivering this feed to a subscriber (in ms)</td></tr>\r
47  * <tr><td>max</td><td>the maximum latency in delivering this feed to a subscriber (in ms)</td></tr>\r
48  * <tr><td>avg</td><td>the average latency in delivering this feed to all subscribers (in ms)</td></tr>\r
49  * <tr><td>fanout</td><td>the number of subscribers this feed was delivered to</td></tr>\r
50  * </table>\r
51  *\r
52  * @author Robert P. Eby\r
53  * @version $Id: LatencyReport.java,v 1.1 2013/10/28 18:06:53 eby Exp $\r
54  */\r
55 public class LatencyReport extends ReportBase {\r
56 \r
57     private class Event {\r
58         public final String type;\r
59         public final long time;\r
60 \r
61         public Event(String t, long tm) {\r
62             type = t;\r
63             time = tm;\r
64         }\r
65     }\r
66 \r
67     private class Counters {\r
68         public final String id;\r
69         public final int feedid;\r
70         public final long clen;\r
71         public final String fileid;\r
72         public final List<Event> events;\r
73 \r
74         public Counters(String i, int fid, long c, String s) {\r
75             id = i;\r
76             feedid = fid;\r
77             clen = c;\r
78             fileid = s;\r
79             events = new ArrayList<>();\r
80         }\r
81 \r
82         private long pubtime;\r
83 \r
84         public void addEvent(String t, long tm) {\r
85             events.add(new Event(t, tm));\r
86             if (t.equals("pub"))\r
87                 pubtime = tm;\r
88         }\r
89 \r
90         public long min() {\r
91             long min = Long.MAX_VALUE;\r
92             for (Event e : events) {\r
93                 if (e.type.equals("del")) {\r
94                     min = Math.min(min, e.time - pubtime);\r
95                 }\r
96             }\r
97             return min;\r
98         }\r
99 \r
100         public long max() {\r
101             long max = 0;\r
102             for (Event e : events) {\r
103                 if (e.type.equals("del")) {\r
104                     max = Math.max(max, e.time - pubtime);\r
105                 }\r
106             }\r
107             return max;\r
108         }\r
109 \r
110         public long avg() {\r
111             long total = 0, c = 0;\r
112             for (Event e : events) {\r
113                 if (e.type.equals("del")) {\r
114                     total += e.time - pubtime;\r
115                     c++;\r
116                 }\r
117             }\r
118             return (c == 0) ? 0 : total / c;\r
119         }\r
120 \r
121         public int fanout() {\r
122             int n = 0;\r
123             for (Event e : events) {\r
124                 if (e.type.equals("del")) {\r
125                     n++;\r
126                 }\r
127             }\r
128             return n;\r
129         }\r
130 \r
131         @Override\r
132         public String toString() {\r
133             return feedid + "," + fileid + "," + clen + "," + min() + "," + max() + "," + avg() + "," + fanout();\r
134         }\r
135     }\r
136 \r
137     @Override\r
138     public void run() {\r
139         long start = System.currentTimeMillis();\r
140         try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
141             PreparedStatement ps = conn.prepareStatement(\r
142                 "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS where "\r
143                     + "EVENT_TIME >= ? and EVENT_TIME <= ? order by PUBLISH_ID, EVENT_TIME")) {\r
144             ps.setLong(1, from);\r
145             ps.setLong(2, to);\r
146             try(ResultSet rs = ps.executeQuery()) {\r
147                 try (PrintWriter os = new PrintWriter(outfile)) {\r
148                     os.println("recordid,feedid,uri,size,min,max,avg,fanout");\r
149                     Counters c = null;\r
150                     while (rs.next()) {\r
151                         long etime = rs.getLong("EVENT_TIME");\r
152                         String type = rs.getString("TYPE");\r
153                         String id = rs.getString("PUBLISH_ID");\r
154                         String fid = rs.getString("FEED_FILEID");\r
155                         int feed = rs.getInt("FEEDID");\r
156                         long clen = rs.getLong("CONTENT_LENGTH");\r
157                         if (c != null && !id.equals(c.id)) {\r
158                             String line = id + "," + c.toString();\r
159                             os.println(line);\r
160                             c = null;\r
161                         }\r
162                         if (c == null) {\r
163                             c = new Counters(id, feed, clen, fid);\r
164                         }\r
165                         if (feed != c.feedid)\r
166                             System.err.println("Feed ID mismatch, " + feed + " <=> " + c.feedid);\r
167                         if (clen != c.clen)\r
168                             System.err.println("Cont Len mismatch, " + clen + " <=> " + c.clen);\r
169                         c.addEvent(type, etime);\r
170                     }\r
171                 }\r
172             }\r
173         } catch (FileNotFoundException e) {\r
174             System.err.println("File cannot be written: " + outfile);\r
175         } catch (SQLException e) {\r
176             logger.error("SQLException: " + e.getMessage());\r
177         }\r
178         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
179     }\r
180 }\r