获取活跃端口趋势数据
This commit is contained in:
@@ -18,15 +18,15 @@ import com.zdjizhi.utils.StringUtil;
|
||||
|
||||
/**
|
||||
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
|
||||
*
|
||||
* @author ThinkGem
|
||||
* @version 2014-4-15
|
||||
*/
|
||||
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
public static final Logger logger = Logger.getLogger(DateUtils.class);
|
||||
private static String[] parsePatterns = {
|
||||
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
||||
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
|
||||
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
||||
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
||||
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
|
||||
"yyyy.MM.dd HH:mm", "yyyy.MM" };
|
||||
|
||||
/**
|
||||
* 得到当前日期字符串 格式(yyyy-MM-dd)
|
||||
@@ -34,14 +34,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
public static String getDate() {
|
||||
return getDate("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
|
||||
*/
|
||||
public static String getDate(String pattern) {
|
||||
return DateFormatUtils.format(new Date(), pattern);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
|
||||
*/
|
||||
@@ -54,7 +54,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
}
|
||||
return formatDate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
|
||||
*/
|
||||
@@ -103,15 +103,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
public static String getWeek() {
|
||||
return formatDate(new Date(), "E");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 日期型字符串转化为日期 格式
|
||||
* { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
|
||||
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
|
||||
* "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
|
||||
* 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
|
||||
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
|
||||
* "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
|
||||
*/
|
||||
public static Date parseDate(Object str) {
|
||||
if (str == null){
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
@@ -123,48 +122,52 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
|
||||
/**
|
||||
* 获取过去的天数
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static long pastDays(Date date) {
|
||||
long t = new Date().getTime()-date.getTime();
|
||||
return t/(24*60*60*1000);
|
||||
long t = new Date().getTime() - date.getTime();
|
||||
return t / (24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取过去的小时
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static long pastHour(Date date) {
|
||||
long t = new Date().getTime()-date.getTime();
|
||||
return t/(60*60*1000);
|
||||
long t = new Date().getTime() - date.getTime();
|
||||
return t / (60 * 60 * 1000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取过去的分钟
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static long pastMinutes(Date date) {
|
||||
long t = new Date().getTime()-date.getTime();
|
||||
return t/(60*1000);
|
||||
long t = new Date().getTime() - date.getTime();
|
||||
return t / (60 * 1000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 转换为时间(天,时:分:秒.毫秒)
|
||||
*
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
public static String formatDateTime(long timeMillis){
|
||||
long day = timeMillis/(24*60*60*1000);
|
||||
long hour = (timeMillis/(60*60*1000)-day*24);
|
||||
long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
|
||||
long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
|
||||
long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
|
||||
return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
|
||||
}
|
||||
|
||||
public static String formatDateTime(long timeMillis) {
|
||||
long day = timeMillis / (24 * 60 * 60 * 1000);
|
||||
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
|
||||
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
||||
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
||||
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
|
||||
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个日期之间的天数
|
||||
*
|
||||
@@ -177,6 +180,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
long afterTime = after.getTime();
|
||||
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @Description: 按类型获取默认时间
|
||||
@@ -189,87 +193,138 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Map<String, String> getLocalTime(String startTime,String endTime,Long localLen,String type)throws Exception {
|
||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
Map<String, String> timeMap=new HashMap<String, String>();
|
||||
public static Map<String, String> getLocalTime(String startTime, String endTime, Long localLen, String type)
|
||||
throws Exception {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
Map<String, String> timeMap = new HashMap<String, String>();
|
||||
if (StringUtil.isEmpty(type)) {
|
||||
type = "minute";
|
||||
}
|
||||
//实时报表统计,默认查询最近5分钟
|
||||
if("minute".equals(type) && startTime == null && endTime == null){
|
||||
Calendar cal=Calendar.getInstance();
|
||||
// 实时报表统计,默认查询最近5分钟
|
||||
if ("minute".equals(type) && startTime == null && endTime == null) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
timeMap.put("endTime", sdf.format(cal.getTime()));
|
||||
if(localLen != null){
|
||||
if (localLen != null) {
|
||||
cal.add(Calendar.MINUTE, -localLen.intValue());
|
||||
}else{
|
||||
} else {
|
||||
cal.add(Calendar.MINUTE, -5);
|
||||
}
|
||||
timeMap.put("startTime", sdf.format(cal.getTime()));
|
||||
logger.info("实时报表统计默认开始时间条件:"+timeMap.get("startTime"));
|
||||
logger.info("默认结束时间条件:"+timeMap.get("endTime"));
|
||||
|
||||
}else if("hour".equals(type) && startTime == null && endTime == null){//小时报表默认查询最近一小时的数据
|
||||
Calendar cal=Calendar.getInstance();
|
||||
logger.info("实时报表统计默认开始时间条件:" + timeMap.get("startTime"));
|
||||
logger.info("默认结束时间条件:" + timeMap.get("endTime"));
|
||||
|
||||
} else if ("hour".equals(type) && startTime == null && endTime == null) {// 小时报表默认查询最近一小时的数据
|
||||
Calendar cal = Calendar.getInstance();
|
||||
timeMap.put("endTime", sdf.format(cal.getTime()));
|
||||
cal.add(Calendar.HOUR_OF_DAY, -1);
|
||||
timeMap.put("startTime", sdf.format(cal.getTime()));
|
||||
logger.info("小时报默认开始时间条件:"+timeMap.get("startTime"));
|
||||
logger.info("小时报默认结束时间条件:"+timeMap.get("endTime"));
|
||||
}else if("daily".equals(type) && startTime == null && endTime == null){//日报表默认查询最近一天的数据
|
||||
Calendar cal=Calendar.getInstance();
|
||||
logger.info("小时报默认开始时间条件:" + timeMap.get("startTime"));
|
||||
logger.info("小时报默认结束时间条件:" + timeMap.get("endTime"));
|
||||
} else if ("daily".equals(type) && startTime == null && endTime == null) {// 日报表默认查询最近一天的数据
|
||||
Calendar cal = Calendar.getInstance();
|
||||
timeMap.put("endTime", sdf.format(cal.getTime()));
|
||||
cal.add(Calendar.DAY_OF_YEAR, -1);
|
||||
timeMap.put("startTime", sdf.format(cal.getTime()));
|
||||
logger.info("日报默认开始时间条件:"+timeMap.get("startTime"));
|
||||
logger.info("日报默认结束时间条件:"+timeMap.get("endTime"));
|
||||
}else if("month".equals(type) && startTime == null && endTime == null){//月报表默认查询最近一月的数据
|
||||
Calendar cal=Calendar.getInstance();
|
||||
logger.info("日报默认开始时间条件:" + timeMap.get("startTime"));
|
||||
logger.info("日报默认结束时间条件:" + timeMap.get("endTime"));
|
||||
} else if ("month".equals(type) && startTime == null && endTime == null) {// 月报表默认查询最近一月的数据
|
||||
Calendar cal = Calendar.getInstance();
|
||||
timeMap.put("endTime", sdf.format(cal.getTime()));
|
||||
cal.add(Calendar.MONTH,-1);
|
||||
cal.add(Calendar.MONTH, -1);
|
||||
timeMap.put("startTime", sdf.format(cal.getTime()));
|
||||
logger.info("月报默认开始时间条件:"+timeMap.get("startTime"));
|
||||
logger.info("月报默认结束时间条件:"+timeMap.get("endTime"));
|
||||
logger.info("月报默认开始时间条件:" + timeMap.get("startTime"));
|
||||
logger.info("月报默认结束时间条件:" + timeMap.get("endTime"));
|
||||
}
|
||||
return timeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将短时间格式字符串转换为时间 yyyy-MM-dd
|
||||
*
|
||||
* @param strDate
|
||||
* @return
|
||||
*/
|
||||
* 将短时间格式字符串转换为时间 yyyy-MM-dd
|
||||
*
|
||||
* @param strDate
|
||||
* @return
|
||||
*/
|
||||
/**
|
||||
* 将短时间格式字符串转换为指定时间
|
||||
*
|
||||
* @param strDate
|
||||
* @param dateFormat "yyyy-MM-dd HH:mm:ss" 此参数为空,默认为:"yyyy-MM-dd"
|
||||
* @param dateFormat
|
||||
* "yyyy-MM-dd HH:mm:ss" 此参数为空,默认为:"yyyy-MM-dd"
|
||||
* @return
|
||||
* @throws ParseException
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static Date strToDate(String strDate,String dateFormat) throws ParseException {
|
||||
if(dateFormat==null || "".equals(dateFormat)){
|
||||
dateFormat = "yyyy-MM-dd";
|
||||
}
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
Date strtodate = sdf.parse(strDate);
|
||||
return strtodate;
|
||||
public static Date strToDate(String strDate, String dateFormat) throws ParseException {
|
||||
if (dateFormat == null || "".equals(dateFormat)) {
|
||||
dateFormat = "yyyy-MM-dd";
|
||||
}
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
Date strtodate = sdf.parse(strDate);
|
||||
return strtodate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
* @throws ParseException
|
||||
*/
|
||||
public static void main(String[] args) throws ParseException {
|
||||
// System.out.println(formatDate(parseDate("2010/3/6")));
|
||||
// System.out.println(getDate("yyyy年MM月dd日 E"));
|
||||
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
|
||||
// System.out.println(time/(24*60*60*1000));
|
||||
Date date=new Date();
|
||||
Calendar cal=Calendar.getInstance();
|
||||
cal.add(Calendar.MONTH, date.getMonth()-2);
|
||||
// System.out.println(formatDate(parseDate("2010/3/6")));
|
||||
// System.out.println(getDate("yyyy年MM月dd日 E"));
|
||||
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
|
||||
// System.out.println(time/(24*60*60*1000));
|
||||
Date date = new Date();
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.MONTH, date.getMonth() - 2);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
//timeMap.put("startTime", sdf.format(cal2.getTime()));
|
||||
//timeMap.put("endTime", sdf.format(cal.getTime()));
|
||||
logger.info("月报默认开始时间条件:"+sdf.format(cal.getTime()));
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// timeMap.put("startTime", sdf.format(cal2.getTime()));
|
||||
// timeMap.put("endTime", sdf.format(cal.getTime()));
|
||||
logger.info("月报默认开始时间条件:" + sdf.format(cal.getTime()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定日期的前一天
|
||||
*
|
||||
* @param specifiedDay
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getSpecifiedDayBefore(String specifiedDay) {
|
||||
// SimpleDateFormat simpleDateFormat = new
|
||||
// SimpleDateFormat("yyyy-MM-dd");
|
||||
Calendar c = Calendar.getInstance();
|
||||
Date date = null;
|
||||
try {
|
||||
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
c.setTime(date);
|
||||
int day = c.get(Calendar.DATE);
|
||||
c.set(Calendar.DATE, day - 1);
|
||||
|
||||
String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
|
||||
return dayBefore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定日期的后一天
|
||||
*
|
||||
* @param specifiedDay
|
||||
* @return
|
||||
*/
|
||||
public static String getSpecifiedDayAfter(String specifiedDay) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
Date date = null;
|
||||
try {
|
||||
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
c.setTime(date);
|
||||
int day = c.get(Calendar.DATE);
|
||||
c.set(Calendar.DATE, day + 1);
|
||||
|
||||
String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
|
||||
return dayAfter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -902,4 +902,51 @@ public class DashboardServiceController extends BaseRestController {
|
||||
return serviceLogResponse(auditLogThread, System.currentTimeMillis() - start, request, "HTTP网站域名分类分析数据检索成功",
|
||||
list, 0);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "trafficPortActiveFiveMinute", method = RequestMethod.GET)
|
||||
@ApiOperation(value = "流量端口详情趋势", httpMethod = "GET", notes = "域名UV")
|
||||
public Map<String, ?> trafficPortAcitve(String beginDate, String endDate, Model model, Integer[] port,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
AuditLogThread auditLogThread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_GET, request,
|
||||
null);
|
||||
List<Map> list = new ArrayList<Map>();
|
||||
try {
|
||||
Calendar cal=Calendar.getInstance();
|
||||
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
if((beginDate == null || "".equals(beginDate)) && (endDate == null || "".equals(endDate))){
|
||||
//如果时间全部为空查询最近一小时
|
||||
endDate = sdf.format(cal.getTime());
|
||||
cal.add(Calendar.HOUR_OF_DAY, -1);
|
||||
beginDate = sdf.format(cal.getTime());
|
||||
}else if((beginDate == null || "".equals(beginDate)) && (endDate != null && !"".equals(endDate))){
|
||||
beginDate = DateUtils.getSpecifiedDayBefore(endDate)+" "+endDate.substring(11);
|
||||
}else if((endDate == null || "".equals(endDate)) && (beginDate != null && !"".equals(beginDate))){
|
||||
endDate = DateUtils.getSpecifiedDayAfter(beginDate)+" "+beginDate.substring(11);
|
||||
}
|
||||
// 带查询时间查询所有
|
||||
List<Map> portActiveChart = dashboardService.findTrafficPortActive(beginDate, endDate,port);
|
||||
if (portActiveChart != null && portActiveChart.size() > 0) {
|
||||
list = portActiveChart;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
auditLogThread.setExceptionInfo("流量端口详情趋势数据检索失败:" + e.getMessage());
|
||||
logger.error("流量端口详情趋势数据检索失败:" + ExceptionUtil.getExceptionMsg(e));
|
||||
if (e instanceof RestServiceException) {
|
||||
throw new RestServiceException(auditLogThread, System.currentTimeMillis() - start,
|
||||
"流量端口详情趋势数据检索失败:" + e.getMessage(), ((RestServiceException) e).getErrorCode());
|
||||
} else if (e instanceof ServiceRuntimeException) {
|
||||
throw new ServiceRuntimeException(auditLogThread, System.currentTimeMillis() - start,
|
||||
"流量端口详情趋势数据检索失败:" + e.getMessage(), ((ServiceRuntimeException) e).getErrorCode());
|
||||
} else {
|
||||
throw new ServiceRuntimeException(auditLogThread, System.currentTimeMillis() - start,
|
||||
"流量端口详情趋势数据检索失败:" + e.getMessage(), RestBusinessCode.service_runtime_error.getValue());
|
||||
}
|
||||
}
|
||||
return serviceLogResponse(auditLogThread, System.currentTimeMillis() - start, request, "流量端口详情趋势数据检索成功",
|
||||
list, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.nis.web.dao.dashboard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -14,4 +16,8 @@ public interface TrafficPortActiveStatisticDao {
|
||||
TrafficPortActiveStatistic getMaxStatTime();
|
||||
List<TrafficPortActiveStatistic> getPortActiveList(@Param("statTime")Date statTime);
|
||||
TrafficPortActiveStatistic getPortActiveOld(@Param("port")Integer port,@Param("statTime")Date statTime);
|
||||
//获取端口详情趋势
|
||||
ArrayList<TrafficPortActiveStatistic> findTrafficPortActive(@Param("statStartTime")String statStartTime,@Param("statEndTime")String statEndTime,@Param("port")String port);
|
||||
ArrayList<LinkedHashMap> findTrafficPortActiveTopN(@Param("statStartTime")String statStartTime,@Param("statEndTime")String statEndTime);
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.nis.web.dao.dashboard.TrafficPortActiveStatisticDao">
|
||||
<resultMap id="BaseResultMap" type="com.nis.domain.restful.dashboard.TrafficPortActiveStatistic">
|
||||
<id column="stat_id" jdbcType="INTEGER" property="statId" />
|
||||
<!-- <id column="stat_id" jdbcType="INTEGER" property="statId" /> -->
|
||||
<result column="port" jdbcType="INTEGER" property="port" />
|
||||
<result column="sum" jdbcType="BIGINT" property="sum" />
|
||||
<result column="stat_time" jdbcType="TIMESTAMP" property="statTime" />
|
||||
@@ -26,4 +26,30 @@
|
||||
and stat_time > DATE_SUB(#{statTime},INTERVAL 2 hour)
|
||||
GROUP BY port ORDER BY sum DESC limit 0,10
|
||||
</select>
|
||||
<!-- 获取指定时间段内的活跃端口详情数据 -->
|
||||
<select id="findTrafficPortActive" resultMap="BaseResultMap">
|
||||
SELECT stat_time,port,sum(sum) as sum from traffic_port_active_statistic
|
||||
WHERE <![CDATA[ port in (${port}) and stat_time >= #{statStartTime} and stat_time < #{statEndTime} ]]>
|
||||
<!-- <if test="port!=null">
|
||||
and port in
|
||||
<foreach collection="port" index="index" item="item" open="(" separator="," close=")">
|
||||
#{item}
|
||||
</foreach>
|
||||
</if> -->
|
||||
GROUP BY stat_time,port ORDER BY stat_time ASC
|
||||
</select>
|
||||
|
||||
<select id="findTrafficPortActiveTopN" resultType="java.util.LinkedHashMap">
|
||||
SELECT port,sum(sum) as sum from traffic_port_active_statistic
|
||||
WHERE
|
||||
<!-- -->
|
||||
<![CDATA[stat_time >= #{statStartTime} and stat_time < #{statEndTime}]]>
|
||||
<!--
|
||||
stat_id BETWEEN
|
||||
(select stat_id from traffic_port_active_statistic where<![CDATA[ stat_time >= #{statStartTime} order by stat_id limit 1)]]>
|
||||
AND
|
||||
(select stat_id from traffic_port_active_statistic where<![CDATA[ stat_time < #{statEndTime} order by stat_id desc limit 1)]]>
|
||||
-->
|
||||
GROUP BY port order by sum desc LIMIT 0,10
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -1473,4 +1473,113 @@ public class DashboardService extends BaseService {
|
||||
return listMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取活跃Port top10 的趋势图
|
||||
* @throws ParseException
|
||||
*/
|
||||
public List<Map> findTrafficPortActive(String beginDate, String endDate, Integer[] portArray) throws ParseException {
|
||||
List<Map> listMap = new ArrayList<Map>();
|
||||
StringBuilder portStr = new StringBuilder();
|
||||
List<LinkedHashMap> portListTop10 = null;
|
||||
if(portArray!=null && portArray.length>0){//如果有传人参数,获取传人参数
|
||||
for(int pi=0;pi<portArray.length;pi++){
|
||||
portStr.append("'"+portArray[pi]+"',");
|
||||
}
|
||||
}else{//否则获取TOP10
|
||||
portListTop10 = trafficPortActiveStatisticDao.findTrafficPortActiveTopN(beginDate, endDate);
|
||||
for (LinkedHashMap map : portListTop10) {
|
||||
portStr.append("'"+map.get("port")+"',");
|
||||
}
|
||||
}
|
||||
if (!StringUtil.isEmpty(portStr)&&portStr.indexOf(",")>0) {
|
||||
portStr.deleteCharAt(portStr.lastIndexOf(","));
|
||||
}
|
||||
Date beginDate1 = DateUtils.strToDate(beginDate, "yyyy-MM-dd HH:mm:ss");
|
||||
Date endDate1 = DateUtils.strToDate(endDate, "yyyy-MM-dd HH:mm:ss");
|
||||
//if (portListTop10 != null && portListTop10.size() > 0) {
|
||||
//StringBuilder portStr = new StringBuilder();
|
||||
if(!StringUtil.isEmpty(portStr)){
|
||||
//如果所有IP在最后一个时间点的count都为0,则移除最后一个点
|
||||
//开始划分时间段,间隔5分钟
|
||||
List<Date> dateRangeList = new ArrayList<Date>();
|
||||
List<String> strDateRangeList = new ArrayList<String>();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(beginDate1);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
while (calendar.getTime().compareTo(endDate1)<=0) {
|
||||
dateRangeList.add(calendar.getTime());
|
||||
strDateRangeList.add(sdf.format(calendar.getTime()));
|
||||
calendar.add(Calendar.MINUTE, 5);
|
||||
}
|
||||
|
||||
ArrayList<TrafficPortActiveStatistic> portInfoList = trafficPortActiveStatisticDao.findTrafficPortActive(beginDate, endDate, portStr.toString());
|
||||
//存放每个IP,每个时间点的总数
|
||||
Map<String,List<Long>> portCountListMap = new HashMap<String, List<Long>>();
|
||||
int index=0;
|
||||
for (int i = 0; i < dateRangeList.size(); i++) {
|
||||
//存放一个时间点中每个IP的总数
|
||||
Map<String, Long> portCountMap = new HashMap<String, Long>();
|
||||
Date date = dateRangeList.get(i);
|
||||
for (int j = index; j < portInfoList.size(); j++) {
|
||||
TrafficPortActiveStatistic portInfo = portInfoList.get(j);
|
||||
if (portInfo.getStatTime()!=null){
|
||||
if(portInfo.getStatTime().compareTo(date)>=0&&(i+1<dateRangeList.size()&&portInfo.getStatTime().compareTo(dateRangeList.get(i+1))<0)) {
|
||||
Long num = portInfo.getSum();
|
||||
if (portCountMap.containsKey(portInfo.getPort())) {
|
||||
num=num+portCountMap.get(portInfo.getPort());
|
||||
}
|
||||
portCountMap.put(portInfo.getPort().toString(), num);
|
||||
}else{
|
||||
index = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(portArray!=null && portArray.length>0){//如果有传人参数,获取传人参数
|
||||
for(int m=0;m<portArray.length;m++){
|
||||
String port = portArray[m].toString();
|
||||
Long sum = 0l;
|
||||
if (portCountMap.containsKey(port)) {
|
||||
sum=portCountMap.get(port);
|
||||
}
|
||||
if (portCountListMap.containsKey(port)) {
|
||||
portCountListMap.get(port).add(sum);
|
||||
}else{
|
||||
List<Long> list = new ArrayList<Long>();
|
||||
list.add(sum);
|
||||
portCountListMap.put(port, list);
|
||||
}
|
||||
}
|
||||
|
||||
}else if(portListTop10!=null){
|
||||
for (LinkedHashMap map : portListTop10) {
|
||||
String port = map.get("port").toString();
|
||||
Long sum = 0l;
|
||||
if (portCountMap.containsKey(port)) {
|
||||
sum=portCountMap.get(port);
|
||||
}
|
||||
if (portCountListMap.containsKey(port)) {
|
||||
portCountListMap.get(port).add(sum);
|
||||
}else{
|
||||
List<Long> list = new ArrayList<Long>();
|
||||
list.add(sum);
|
||||
portCountListMap.put(port, list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//整合port sum time
|
||||
Iterator iterator = portCountListMap.keySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String ip = iterator.next()+"";
|
||||
Map trafficPortActiveMap = new HashMap();
|
||||
trafficPortActiveMap.put("port", ip);
|
||||
trafficPortActiveMap.put("sum", portCountListMap.get(ip));
|
||||
trafficPortActiveMap.put("statTime", strDateRangeList);
|
||||
listMap.add(trafficPortActiveMap);
|
||||
}
|
||||
}
|
||||
return listMap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user