2f08b176ddec8d665940c1bed233fb5844f215ca
[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         private long pubtime = 0;\r
74         private long clen = 0;\r
75         private List<Long> deltime = new ArrayList<>();\r
76         public long minLatency() {\r
77             long n = deltime.isEmpty() ? 0 : Long.MAX_VALUE;\r
78             for (Long l : deltime) {\r
79                 n = Math.min(n, l - pubtime);\r
80             }\r
81             return n;\r
82         }\r
83 \r
84         public long maxLatency() {\r
85             long n = 0;\r
86             for (Long l : deltime) {\r
87                 n = Math.max(n, l - pubtime);\r
88             }\r
89             return n;\r
90         }\r
91 \r
92         public long totalLatency() {\r
93             long n = 0;\r
94             for (Long l : deltime) {\r
95                 n += (l - pubtime);\r
96             }\r
97             return n;\r
98         }\r
99     }\r
100 \r
101     private class Counters {\r
102 \r
103         public final String date;\r
104         public final int feedid;\r
105         public final Map<String, Job> jobs;\r
106 \r
107         public Counters(String d, int fid) {\r
108             date = d;\r
109             feedid = fid;\r
110             jobs = new HashMap<>();\r
111         }\r
112 \r
113         public void addEvent(long etime, String type, String id, String fid, long clen) {\r
114             Job j = jobs.get(id);\r
115             if (j == null) {\r
116                 j = new Job();\r
117                 jobs.put(id, j);\r
118             }\r
119             if (type.equals("pub")) {\r
120                 j.pubtime = getPstart(id);\r
121                 j.clen = clen;\r
122             } else if (type.equals("del")) {\r
123                 j.deltime.add(etime);\r
124             }\r
125         }\r
126 \r
127         @Override\r
128         public String toString() {\r
129             long minsize = Long.MAX_VALUE, maxsize = 0, avgsize = 0;\r
130             long minl = Long.MAX_VALUE, maxl = 0;\r
131             long fanout = 0, totall = 0, totaln = 0;\r
132             for (Job j : jobs.values()) {\r
133                 minsize = Math.min(minsize, j.clen);\r
134                 maxsize = Math.max(maxsize, j.clen);\r
135                 avgsize += j.clen;\r
136                 minl = Math.min(minl, j.minLatency());\r
137                 maxl = Math.max(maxl, j.maxLatency());\r
138                 totall += j.totalLatency();\r
139                 totaln += j.deltime.size();\r
140                 fanout += j.deltime.size();\r
141             }\r
142             if (jobs.size() > 0) {\r
143                 avgsize /= jobs.size();\r
144                 fanout /= jobs.size();\r
145             }\r
146             long avgl = (totaln > 0) ? (totall / totaln) : 0;\r
147             return date + "," + feedid + "," + minsize + "," + maxsize + "," + avgsize + "," + minl + "," + maxl + ","\r
148                 + avgl + "," + fanout;\r
149         }\r
150     }\r
151 \r
152     private long getPstart(String t) {\r
153         if (t.indexOf('.') >= 0) {\r
154             t = t.substring(0, t.indexOf('.'));\r
155         }\r
156         return Long.parseLong(t);\r
157     }\r
158 \r
159     @Override\r
160     public void run() {\r
161         Map<String, Counters> map = new HashMap<>();\r
162         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");\r
163         long start = System.currentTimeMillis();\r
164         try {\r
165             DB db = new DB();\r
166             @SuppressWarnings("resource")\r
167             Connection conn = db.getConnection();\r
168             try (PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
169                 ps.setLong(1, from);\r
170                 ps.setLong(2, to);\r
171                 try (ResultSet rs = ps.executeQuery()) {\r
172                     while (rs.next()) {\r
173                         String id = rs.getString("PUBLISH_ID");\r
174                         int feed = rs.getInt("FEEDID");\r
175                         long etime = rs.getLong("EVENT_TIME");\r
176                         String type = rs.getString("TYPE");\r
177                         String fid = rs.getString("FEED_FILEID");\r
178                         long clen = rs.getLong("CONTENT_LENGTH");\r
179                         String date = sdf.format(new Date(getPstart(id)));\r
180                         String key = date + "," + feed;\r
181                         Counters c = map.get(key);\r
182                         if (c == null) {\r
183                             c = new Counters(date, feed);\r
184                             map.put(key, c);\r
185                         }\r
186                         c.addEvent(etime, type, id, fid, clen);\r
187                     }\r
188                 }\r
189 \r
190                 db.release(conn);\r
191             }\r
192         } catch (SQLException e) {\r
193             logger.error("SQLException: " + e.getMessage());\r
194         }\r
195         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
196         try (PrintWriter os = new PrintWriter(outfile)) {\r
197             os.println("date,feedid,minsize,maxsize,avgsize,minlat,maxlat,avglat,fanout");\r
198             for (String key : new TreeSet<>(map.keySet())) {\r
199                 Counters c = map.get(key);\r
200                 os.println(c.toString());\r
201             }\r
202         } catch (FileNotFoundException e) {\r
203             System.err.println("File cannot be written: " + outfile);\r
204             logger.error("FileNotFoundException: " + e.getMessage());\r
205         }\r
206     }\r
207 }\r