Fix DailyLatencyReport vulnerabilities
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / DailyLatencyReport.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.text.SimpleDateFormat;\r
34 import java.util.ArrayList;\r
35 import java.util.Date;\r
36 import java.util.HashMap;\r
37 import java.util.List;\r
38 import java.util.Map;\r
39 import java.util.TreeSet;\r
40 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
41 \r
42 /**\r
43  * Generate a daily per feed latency report.  The report is a .csv file containing the following columns:\r
44  * <table>\r
45  * <tr><td>date</td><td>the date for this record</td></tr>\r
46  * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>\r
47  * <tr><td>minsize</td><td>the minimum size of all files published on this feed and date</td></tr>\r
48  * <tr><td>maxsize</td><td>the maximum size of all files published on this feed and date</td></tr>\r
49  * <tr><td>avgsize</td><td>the average size of all files published on this feed and date</td></tr>\r
50  * <tr><td>minlat</td><td>the minimum latency in delivering this feed to all subscribers (in ms)</td></tr>\r
51  * <tr><td>maxlat</td><td>the maximum latency in delivering this feed to all subscribers (in ms)</td></tr>\r
52  * <tr><td>avglat</td><td>the average latency in delivering this feed to all subscribers (in ms)</td></tr>\r
53  * <tr><td>fanout</td><td>the average number of subscribers this feed was delivered to</td></tr>\r
54  * </table>\r
55  * <p>\r
56  * In the context of this report, latency is defined as the value\r
57  * <i>(D<sub>e</sub> - P<sub>s</sub>)</i>\r
58  * where:\r
59  * </p>\r
60  * <p>P<sub>s</sub> is the time that the publication of the file to the node starts.</p>\r
61  * <p>D<sub>e</sub> is the time that the delivery of the file to the subscriber ends.</p>\r
62  *\r
63  * @author Robert P. Eby\r
64  * @version $Id: DailyLatencyReport.java,v 1.2 2013/11/06 16:23:54 eby Exp $\r
65  */\r
66 public class DailyLatencyReport extends ReportBase {\r
67 \r
68     private static final String SELECT_SQL =\r
69         "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS" +\r
70             " where EVENT_TIME >= ? and EVENT_TIME <= ?";\r
71 \r
72     private class Job {\r
73 \r
74         public long pubtime = 0;\r
75         public long clen = 0;\r
76         public List<Long> deltime = new ArrayList<Long>();\r
77 \r
78         public long minLatency() {\r
79             long n = deltime.isEmpty() ? 0 : Long.MAX_VALUE;\r
80             for (Long l : deltime) {\r
81                 n = Math.min(n, l - pubtime);\r
82             }\r
83             return n;\r
84         }\r
85 \r
86         public long maxLatency() {\r
87             long n = 0;\r
88             for (Long l : deltime) {\r
89                 n = Math.max(n, l - pubtime);\r
90             }\r
91             return n;\r
92         }\r
93 \r
94         public long totalLatency() {\r
95             long n = 0;\r
96             for (Long l : deltime) {\r
97                 n += (l - pubtime);\r
98             }\r
99             return n;\r
100         }\r
101     }\r
102 \r
103     private class Counters {\r
104 \r
105         public final String date;\r
106         public final int feedid;\r
107         public final Map<String, Job> jobs;\r
108 \r
109         public Counters(String d, int fid) {\r
110             date = d;\r
111             feedid = fid;\r
112             jobs = new HashMap<>();\r
113         }\r
114 \r
115         public void addEvent(long etime, String type, String id, String fid, long clen) {\r
116             Job j = jobs.get(id);\r
117             if (j == null) {\r
118                 j = new Job();\r
119                 jobs.put(id, j);\r
120             }\r
121             if (type.equals("pub")) {\r
122                 j.pubtime = getPstart(id);\r
123                 j.clen = clen;\r
124             } else if (type.equals("del")) {\r
125                 j.deltime.add(etime);\r
126             }\r
127         }\r
128 \r
129         @Override\r
130         public String toString() {\r
131             long minsize = Long.MAX_VALUE, maxsize = 0, avgsize = 0;\r
132             long minl = Long.MAX_VALUE, maxl = 0;\r
133             long fanout = 0, totall = 0, totaln = 0;\r
134             for (Job j : jobs.values()) {\r
135                 minsize = Math.min(minsize, j.clen);\r
136                 maxsize = Math.max(maxsize, j.clen);\r
137                 avgsize += j.clen;\r
138                 minl = Math.min(minl, j.minLatency());\r
139                 maxl = Math.max(maxl, j.maxLatency());\r
140                 totall += j.totalLatency();\r
141                 totaln += j.deltime.size();\r
142                 fanout += j.deltime.size();\r
143             }\r
144             if (jobs.size() > 0) {\r
145                 avgsize /= jobs.size();\r
146                 fanout /= jobs.size();\r
147             }\r
148             long avgl = (totaln > 0) ? (totall / totaln) : 0;\r
149             return date + "," + feedid + "," + minsize + "," + maxsize + "," + avgsize + "," + minl + "," + maxl + ","\r
150                 + avgl + "," + fanout;\r
151         }\r
152     }\r
153 \r
154     private long getPstart(String t) {\r
155         if (t.indexOf('.') >= 0) {\r
156             t = t.substring(0, t.indexOf('.'));\r
157         }\r
158         return Long.parseLong(t);\r
159     }\r
160 \r
161     @Override\r
162     public void run() {\r
163         Map<String, Counters> map = new HashMap<>();\r
164         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");\r
165         long start = System.currentTimeMillis();\r
166         try {\r
167             DB db = new DB();\r
168             @SuppressWarnings("resource")\r
169             Connection conn = db.getConnection();\r
170             try (PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
171                 ps.setLong(1, from);\r
172                 ps.setLong(2, to);\r
173                 try (ResultSet rs = ps.executeQuery()) {\r
174                     while (rs.next()) {\r
175                         String id = rs.getString("PUBLISH_ID");\r
176                         int feed = rs.getInt("FEEDID");\r
177                         long etime = rs.getLong("EVENT_TIME");\r
178                         String type = rs.getString("TYPE");\r
179                         String fid = rs.getString("FEED_FILEID");\r
180                         long clen = rs.getLong("CONTENT_LENGTH");\r
181                         String date = sdf.format(new Date(getPstart(id)));\r
182                         String key = date + "," + feed;\r
183                         Counters c = map.get(key);\r
184                         if (c == null) {\r
185                             c = new Counters(date, feed);\r
186                             map.put(key, c);\r
187                         }\r
188                         c.addEvent(etime, type, id, fid, clen);\r
189                     }\r
190                 }\r
191 \r
192                 db.release(conn);\r
193             }\r
194         } catch (SQLException e) {\r
195             logger.error("SQLException: " + e.getMessage());\r
196         }\r
197         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
198         try (PrintWriter os = new PrintWriter(outfile)) {\r
199             os.println("date,feedid,minsize,maxsize,avgsize,minlat,maxlat,avglat,fanout");\r
200             for (String key : new TreeSet<>(map.keySet())) {\r
201                 Counters c = map.get(key);\r
202                 os.println(c.toString());\r
203             }\r
204         } catch (FileNotFoundException e) {\r
205             System.err.println("File cannot be written: " + outfile);\r
206             logger.error("FileNotFoundException: " + e.getMessage());\r
207         }\r
208     }\r
209 }\r