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=76991ca0bde993243ec10fdfda90c93225fdd46a;hpb=68a9ca240970fceaf12bbe91b7bad8e1d98ecd93;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 76991ca0..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; @@ -32,14 +33,15 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; 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; @@ -62,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("; @@ -77,6 +80,8 @@ public class StatisticsServlet extends BaseServlet { private static final String SQL_JOIN_RECORDS = " e JOIN LOG_RECORDS m ON m.PUBLISH_ID = e.PUBLISH_ID AND e.FEEDID IN ("; private static final String SQL_STATUS_204 = " AND m.STATUS=204 AND e.RESULT=204 "; private static final String SQL_GROUP_SUB_ID = " group by SUBID"; + private static final String JSON_OUTPUT_TYPE = "json"; + private static final String CSV_OUTPUT_TYPE = "csv"; /** @@ -107,7 +112,7 @@ public class StatisticsServlet extends BaseServlet { // check Accept: header?? resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(LOGLIST_CONTENT_TYPE); - String outputType = "json"; + String outputType = JSON_OUTPUT_TYPE; if (req.getParameter(FEEDID) == null && req.getParameter(GROUPID) == null) { try { resp.getOutputStream().print("Invalid request, Feedid or Group ID is required."); @@ -153,19 +158,41 @@ public class StatisticsServlet extends BaseServlet { } 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); - } - if (req.getParameter(OUTPUT_TYPE) != null) { - outputType = req.getParameter(OUTPUT_TYPE); + 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); @@ -175,7 +202,6 @@ public class StatisticsServlet extends BaseServlet { } - /** * rsToJson - Converting RS to JSON object. * @@ -276,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); @@ -301,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); } } @@ -429,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"); @@ -454,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; @@ -501,30 +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".equals(outputType)) { - resp.setContentType("application/octet-stream"); - Date date = new Date(); - SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss"); - resp.setHeader("Content-Disposition", - "attachment; filename=\"result:" + dateFormat.format(date) + ".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) { @@ -533,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); + } + } }