aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java
blob: f98116d24067bbd8ff83e2154117a22dcc60ed36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * 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.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/


package org.onap.dmaap.datarouter.reports;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;

/**
 * Generate a per-file latency report.  It reports on the details related to one file published
 * on one feed. This report can be further reduced in order to generate more specific reports
 * based on feed ID or node name. The report is a .csv file containing the following columns:
 * <table>
 * <tr><td>recordid</td><td>the unique record ID assigned to a particular incoming feed</td></tr>
 * <tr><td>feedid</td><td>the Feed ID for this record</td></tr>
 * <tr><td>uri</td><td>the URI of the file delivered</td></tr>
 * <tr><td>size</td><td>the size of the file delivered</td></tr>
 * <tr><td>min</td><td>the minimum latency in delivering this feed to a subscriber (in ms)</td></tr>
 * <tr><td>max</td><td>the maximum latency in delivering this feed to a subscriber (in ms)</td></tr>
 * <tr><td>avg</td><td>the average latency in delivering this feed to all subscribers (in ms)</td></tr>
 * <tr><td>fanout</td><td>the number of subscribers this feed was delivered to</td></tr>
 * </table>
 *
 * @author Robert P. Eby
 * @version $Id: LatencyReport.java,v 1.1 2013/10/28 18:06:53 eby Exp $
 */
public class LatencyReport extends ReportBase {

    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<Event> 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");
    }
}