aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/SubscriberReport.java
blob: aac6dab1a1d47d8bcec67375ecc6972282dbf076 (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
/*******************************************************************************
 * ============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.HashMap;
import java.util.Map;
import java.util.TreeSet;
import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;

/**
 * Generate a subscribers report.  The report is a .CSV file.  It contains information per-day and per-subscriber,
 * on the status codes returned from each delivery attempt (1XX, 2XX, etc.) as well as a count of 4XX instead of a 100.
 *
 * @author Robert P. Eby
 * @version $Id: SubscriberReport.java,v 1.2 2013/11/06 16:23:55 eby Exp $
 */
public class SubscriberReport extends ReportBase {
    private static final String SELECT_SQL =
        "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, RESULT, COUNT(RESULT) as COUNT" +
            " from LOG_RECORDS" +
            " where TYPE = 'del' and EVENT_TIME >= ? and EVENT_TIME <= ?" +
            " group by DATE, DELIVERY_SUBID, RESULT";
    private static final String SELECT_SQL2 =
        "select date(from_unixtime(EVENT_TIME div 1000)) as DATE, DELIVERY_SUBID, COUNT(CONTENT_LENGTH_2) as COUNT" +
            " from LOG_RECORDS" +
            " where TYPE = 'dlx' and CONTENT_LENGTH_2 = -1 and EVENT_TIME >= ? and EVENT_TIME <= ?" +
            " group by DATE, DELIVERY_SUBID";

    private class Counters {
        private String date;
        private int sub;
        private int c100, c200, c300, c400, c500, cm1, cdlx;

        public Counters(String date, int sub) {
            this.date = date;
            this.sub = sub;
            c100 = c200 = c300 = c400 = c500 = cm1 = cdlx = 0;
        }

        public void addCounts(int status, int n) {
            if (status < 0) {
                cm1 += n;
            } else if (status >= 100 && status <= 199) {
                c100 += n;
            } else if (status >= 200 && status <= 299) {
                c200 += n;
            } else if (status >= 300 && status <= 399) {
                c300 += n;
            } else if (status >= 400 && status <= 499) {
                c400 += n;
            } else if (status >= 500 && status <= 599) {
                c500 += n;
            }
        }

        public void addDlxCount(int n) {
            cdlx += n;
        }

        @Override
        public String toString() {
            return date + "," + sub + "," +
                c100 + "," + c200 + "," + c300 + "," + c400 + "," + c500 + "," +
                cm1 + "," + cdlx;
        }
    }

    @Override
    public void run() {
        Map<String, Counters> map = new HashMap<>();
        long start = System.currentTimeMillis();

        try (Connection conn = ProvDbUtils.getInstance().getConnection()) {
            try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {
                ps.setLong(1, from);
                ps.setLong(2, to);
                try(ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        String date = rs.getString("DATE");
                        int sub = rs.getInt("DELIVERY_SUBID");
                        int res = rs.getInt("RESULT");
                        int count = rs.getInt("COUNT");
                        String key = date + "," + sub;
                        Counters c = map.get(key);
                        if (c == null) {
                            c = new Counters(date, sub);
                            map.put(key, c);
                        }
                        c.addCounts(res, count);
                    }
                }
            }

            try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {
                ps2.setLong(1, from);
                ps2.setLong(2, to);
                try (ResultSet rs2 = ps2.executeQuery()) {
                    while (rs2.next()) {
                        String date = rs2.getString("DATE");
                        int sub = rs2.getInt("DELIVERY_SUBID");
                        int count = rs2.getInt("COUNT");
                        String key = date + "," + sub;
                        Counters c = map.get(key);
                        if (c == null) {
                            c = new Counters(date, sub);
                            map.put(key, c);
                        }
                        c.addDlxCount(count);
                    }
                }
            }
        } catch (SQLException e) {
            logger.error("SQLException: " + e.getMessage());
        }
        logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");
        try (PrintWriter os = new PrintWriter(outfile)) {
            os.println("date,subid,count100,count200,count300,count400,count500,countminus1,countdlx");
            for (String key : new TreeSet<>(map.keySet())) {
                Counters c = map.get(key);
                os.println(c.toString());
            }
        } catch (FileNotFoundException e) {
            System.err.println("File cannot be written: " + outfile);
            logger.error("FileNotFoundException: " + e.getMessage());
        }
    }
}