X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FStatisticsServlet.java;h=1c324e5a4868e06f9d1fa5f232261e510df8ad7f;hb=bda6aeaa60607ab4fe5af508156019d7bd5c0ce4;hp=7163a63821bd389e11780d518ce7f722acd9ce2d;hpb=a2fa78a7e5e9aa69454f3280f46081ac6965129c;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java index 7163a638..1c324e5a 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java @@ -25,6 +25,7 @@ package org.onap.dmaap.datarouter.provisioning; import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError; +import jakarta.servlet.ServletOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; @@ -39,9 +40,8 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.TimeZone; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.json.JSONException; import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord; import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject; @@ -64,6 +64,7 @@ public class StatisticsServlet extends BaseServlet { private static final String FMT1 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; private static final String FMT2 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public static final String FEEDID = "FEEDID"; + public static final String START = "start"; //sql Strings private static final String SQL_SELECT_NAME = "SELECT (SELECT NAME FROM FEEDS AS f WHERE f.FEEDID in("; @@ -155,22 +156,43 @@ public class StatisticsServlet extends BaseServlet { if (req.getParameter("type") != null) { map.put(EVENT_TYPE, req.getParameter("type").replace("|", ",")); } - if (req.getParameter(OUTPUT_TYPE) != null && req.getParameter(OUTPUT_TYPE).equals(CSV_OUTPUT_TYPE)) { - map.put(OUTPUT_TYPE, CSV_OUTPUT_TYPE); - outputType = CSV_OUTPUT_TYPE; + if (req.getParameter(OUTPUT_TYPE) != null) { + map.put(OUTPUT_TYPE, req.getParameter(OUTPUT_TYPE)); + outputType = req.getParameter(OUTPUT_TYPE); } if (req.getParameter(OUTPUT_TYPE) != null && req.getParameter(OUTPUT_TYPE).equals(JSON_OUTPUT_TYPE)) { map.put(OUTPUT_TYPE, JSON_OUTPUT_TYPE); } if (req.getParameter(START_TIME) != null) { - map.put(START_TIME, req.getParameter(START_TIME)); + String startTime = req.getParameter(START_TIME); + try { + Long.parseLong(startTime); + map.put(START_TIME, startTime); + } + catch (NumberFormatException e) { + eventlogger.error("Invalid start time StatisticsServlet.doGet: " + e.getMessage(), e); + } } if (req.getParameter(END_TIME) != null) { - map.put(END_TIME, req.getParameter(END_TIME)); + String endTime = req.getParameter(END_TIME); + try { + Long.parseLong(endTime); + map.put(END_TIME, endTime); + } + catch (NumberFormatException e) { + eventlogger.error("Invalid end time StatisticsServlet.doGet: " + e.getMessage(), e); + } } if (req.getParameter("time") != null) { - map.put(START_TIME, req.getParameter("time")); - map.put(END_TIME, null); + String time = req.getParameter("time"); + try { + Long.parseLong(time); + map.put(START_TIME, time); + map.put(END_TIME, null); + } + catch (NumberFormatException e) { + eventlogger.error("Invalid end time StatisticsServlet.doGet: " + e.getMessage(), e); + } } try { this.getRecordsForSQL(map, outputType, resp.getOutputStream(), resp); @@ -180,7 +202,6 @@ public class StatisticsServlet extends BaseServlet { } - /** * rsToJson - Converting RS to JSON object. * @@ -281,17 +302,23 @@ public class StatisticsServlet extends BaseServlet { /** - * queryGeneretor - Generating sql query. + * getResultSet - Set the result from the query. * * @param map as key value pare of all user input fields */ - private String queryGeneretor(Map map) throws ParseException { + private void getResultSet(Map map, String outputType, ServletOutputStream out, + HttpServletResponse resp) throws ParseException, SQLException, IOException { String sql; String feedids = null; String startTime = null; String endTime = null; + long compareTime = 0; + long startInMillis = 0; + long endInMillis = 0; String subid = " "; + String compareType; + ResultSet rs; if (map.get(FEEDIDS) != null) { feedids = map.get(FEEDIDS); @@ -306,48 +333,50 @@ public class StatisticsServlet extends BaseServlet { subid = map.get(SUBID); } - eventlogger.info("Generating sql query to get Statistics resultset. "); - if (endTime == null && startTime == null) { - - sql = SQL_SELECT_NAME + feedids + SQL_FEED_ID + SQL_SELECT_COUNT + feedids + SQL_TYPE_PUB - + SQL_SELECT_SUM - + feedids + SQL_PUBLISH_LENGTH - + SQL_SUBSCRIBER_URL + SQL_SUB_ID + SQL_DELIVERY_TIME + SQL_AVERAGE_DELAY + SQL_JOIN_RECORDS - + feedids + ") " + subid - + SQL_STATUS_204 + SQL_GROUP_SUB_ID; - - return sql; + sql = SQL_SELECT_NAME + "?" + SQL_FEED_ID + SQL_SELECT_COUNT + "?" + SQL_TYPE_PUB + SQL_SELECT_SUM + + "?" + SQL_PUBLISH_LENGTH + SQL_SUBSCRIBER_URL + SQL_SUB_ID + SQL_DELIVERY_TIME + SQL_AVERAGE_DELAY + + SQL_JOIN_RECORDS + "?" + ") " + SQL_STATUS_204 + SQL_GROUP_SUB_ID; + compareType = "default"; } else if (startTime != null && endTime == null) { - long inputTimeInMilli = 60000 * Long.parseLong(startTime); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); long currentTimeInMilli = cal.getTimeInMillis(); - long compareTime = currentTimeInMilli - inputTimeInMilli; - - sql = SQL_SELECT_NAME + feedids + SQL_FEED_ID + SQL_SELECT_COUNT + feedids + SQL_TYPE_PUB - + SQL_SELECT_SUM - + feedids + SQL_PUBLISH_LENGTH - + SQL_SUBSCRIBER_URL + SQL_SUB_ID + SQL_DELIVERY_TIME + SQL_AVERAGE_DELAY + SQL_JOIN_RECORDS - + feedids + ") " + subid - + SQL_STATUS_204 + " and e.event_time>=" + compareTime + SQL_GROUP_SUB_ID; - return sql; - + compareTime = currentTimeInMilli - inputTimeInMilli; + sql = SQL_SELECT_NAME + "?" + SQL_FEED_ID + SQL_SELECT_COUNT + "?" + SQL_TYPE_PUB + SQL_SELECT_SUM + + "?" + SQL_PUBLISH_LENGTH + SQL_SUBSCRIBER_URL + SQL_SUB_ID + SQL_DELIVERY_TIME + SQL_AVERAGE_DELAY + + SQL_JOIN_RECORDS + "?" + ") " + SQL_STATUS_204 + + " and e.event_time>=" + "?" + SQL_GROUP_SUB_ID; + compareType = START; } else { SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date startDate = inFormat.parse(startTime); Date endDate = inFormat.parse(endTime); - - long startInMillis = startDate.getTime(); - long endInMillis = endDate.getTime(); - - sql = SQL_SELECT_NAME + feedids + SQL_FEED_ID + SQL_SELECT_COUNT + feedids + SQL_TYPE_PUB - + SQL_SELECT_SUM - + feedids + SQL_PUBLISH_LENGTH + SQL_SUBSCRIBER_URL - + SQL_SUB_ID + SQL_DELIVERY_TIME + SQL_AVERAGE_DELAY + SQL_JOIN_RECORDS + feedids + ")" + subid + SQL_STATUS_204 - +" and e.event_time between " + startInMillis + " and " + endInMillis + SQL_GROUP_SUB_ID; - - return sql; + startInMillis = startDate.getTime(); + endInMillis = endDate.getTime(); + sql = SQL_SELECT_NAME + "?" + SQL_FEED_ID + SQL_SELECT_COUNT + "?" + SQL_TYPE_PUB + SQL_SELECT_SUM + + "?" + SQL_PUBLISH_LENGTH + SQL_SUBSCRIBER_URL + SQL_SUB_ID + SQL_DELIVERY_TIME + SQL_AVERAGE_DELAY + + SQL_JOIN_RECORDS + "?" + ") " + SQL_STATUS_204 + + " and e.event_time between " + "?" + " and " + "?" + SQL_GROUP_SUB_ID; + compareType = "startAndEnd"; + } + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement(sql)) { + eventlogger.debug("SQL Query for Statistics resultset. " + sql); + intlogger.debug(sql); + ps.setString(1, feedids); + ps.setString(2, feedids); + ps.setString(3, feedids); + ps.setString(4, feedids); + if (compareType.equals(START)) { + ps.setLong(5, compareTime); + } + if (compareType.equals("startAndEnd")) { + ps.setLong(5, startInMillis); + ps.setLong(6, endInMillis); + } + rs = ps.executeQuery(); + parseResult(outputType, out, resp, rs); } } @@ -434,7 +463,7 @@ public class StatisticsServlet extends BaseServlet { return map; } map.put("statusSQL", sql); - map.put("resultSQL", sql.replaceAll("STATUS", "RESULT")); + map.put("resultSQL", sql.replace("STATUS", "RESULT")); } str = req.getParameter("expiryReason"); @@ -459,7 +488,7 @@ public class StatisticsServlet extends BaseServlet { } } - long stime = getTimeFromParam(req.getParameter("start")); + long stime = getTimeFromParam(req.getParameter(START)); if (stime < 0) { map.put("err", "bad start"); return map; @@ -506,29 +535,13 @@ public class StatisticsServlet extends BaseServlet { private void getRecordsForSQL(Map map, String outputType, ServletOutputStream out, HttpServletResponse resp) { + eventlogger.info("Generating sql query to get Statistics resultset. "); try { - String filterQuery = this.queryGeneretor(map); - eventlogger.debug("SQL Query for Statistics resultset. " + filterQuery); - intlogger.debug(filterQuery); long start = System.currentTimeMillis(); - try (Connection conn = ProvDbUtils.getInstance().getConnection(); - PreparedStatement ps = conn.prepareStatement(filterQuery); - ResultSet rs = ps.executeQuery()) { - if (CSV_OUTPUT_TYPE.equals(outputType)) { - resp.setContentType("application/octet-stream"); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); - resp.setHeader("Content-Disposition", - "attachment; filename=\"result:" + LocalDateTime.now().format(formatter) + ".csv\""); - eventlogger.info("Generating CSV file from Statistics resultset"); - rsToCSV(rs, out); - } else { - eventlogger.info("Generating JSON for Statistics resultset"); - this.rsToJson(rs, out); - } - } catch (SQLException e) { - eventlogger.error("SQLException:" + e); - } + this.getResultSet(map, outputType, out, resp); intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); + } catch (SQLException e) { + eventlogger.error("SQLException:" + e.getMessage()); } catch (IOException e) { eventlogger.error("IOException - Generating JSON/CSV:" + e); } catch (JSONException e) { @@ -537,5 +550,20 @@ public class StatisticsServlet extends BaseServlet { eventlogger.error("ParseException - executing SQL query:" + e); } } + + private void parseResult(String outputType, ServletOutputStream out, HttpServletResponse resp, + ResultSet rs) throws IOException, SQLException { + if (CSV_OUTPUT_TYPE.equals(outputType)) { + resp.setContentType("application/octet-stream"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); + resp.setHeader("Content-Disposition", + "attachment; filename=\"result:" + LocalDateTime.now().format(formatter) + ".csv\""); + eventlogger.info("Generating CSV file from Statistics resultset"); + rsToCSV(rs, out); + } else { + eventlogger.info("Generating JSON for Statistics resultset"); + this.rsToJson(rs, out); + } + } }