流量统计带宽sql修改成后台计算,增加地区条件,动作统计时间为1小时

This commit is contained in:
zhanghongqing
2018-12-12 10:43:47 +08:00
parent 8169fe417a
commit 7710b332a1
4 changed files with 226 additions and 8 deletions

View File

@@ -1,6 +1,9 @@
package com.nis.web.controller.restful;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@@ -20,6 +23,7 @@ import com.nis.restful.ServiceRuntimeException;
import com.nis.util.Constants;
import com.nis.util.ExceptionUtil;
import com.nis.util.JsonMapper;
import com.nis.util.StringUtils;
import com.nis.web.controller.BaseRestController;
import com.nis.web.service.AuditLogThread;
import com.nis.web.service.ServicesRequestLogService;
@@ -90,13 +94,49 @@ public class DashboardServiceController extends BaseRestController {
*/
@RequestMapping(value = "trafficBandwidthTrans", method = RequestMethod.GET)
@ApiOperation(value = "带宽根据ip46,协议tcp,udp查询详情", httpMethod = "GET", notes = "对应带宽根据IPv4,6,协议tcp,udp统计数据显示")
public Map<String,?> trafficBandwidthTrans(String addrType,Integer transType,Model model, HttpServletRequest request, HttpServletResponse response) {
public Map<String,?> trafficBandwidthTrans(String beginDate,String endDate,String addrType,Integer transType,Model model, HttpServletRequest request, HttpServletResponse response) {
long start = System.currentTimeMillis();
AuditLogThread auditLogThread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_GET, request, null);
List<HashMap> list = new ArrayList<HashMap>();
try {
List<HashMap> resultList = dashboardService.getBandwidthTrans(addrType, transType);
if(StringUtils.isEmpty(beginDate)||StringUtils.isEmpty(endDate)) {
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
endDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
beginDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(cal.getTime());
}
List<HashMap> resultList = dashboardService.getBandwidthTrans(addrType, transType,beginDate,endDate);
if (resultList!=null&&resultList.size() > 0) {
list = resultList;
}
} catch (Exception e) {
e.printStackTrace();
auditLogThread.setExceptionInfo(e.getMessage() + " " + e.getCause());
logger.error("带宽实时统计数据检索失败"+ExceptionUtil.getExceptionMsg(e));
throw new ServiceRuntimeException(auditLogThread, System.currentTimeMillis() - start,
"带宽实时统计数据检索失败:" + e.getMessage(), RestBusinessCode.service_runtime_error.getValue());
}
return serviceLogResponse(auditLogThread, System.currentTimeMillis() - start, request, "带宽实时统计数据检索成功",list, 0);
}
/**
* 根据ip46,协议tcpudp查询带宽
*/
@RequestMapping(value = "trafficBandwidthTransTwo", method = RequestMethod.GET)
@ApiOperation(value = "带宽根据ip46,协议tcp,udp查询详情", httpMethod = "GET", notes = "对应带宽根据IPv4,6,协议tcp,udp统计数据显示")
public Map<String,?> trafficBandwidthTransTow(String beginDate,String endDate,String addrType,Integer transType,Model model, HttpServletRequest request, HttpServletResponse response) {
long start = System.currentTimeMillis();
AuditLogThread auditLogThread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_GET, request, null);
List<HashMap> list = new ArrayList<HashMap>();
try {
if(StringUtils.isEmpty(beginDate)||StringUtils.isEmpty(endDate)) {
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
endDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
beginDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(cal.getTime());
}
List<HashMap> resultList = dashboardService.getBandwidthTrans2(addrType, transType,beginDate,endDate);
if (resultList!=null&&resultList.size() > 0) {
list = resultList;
}

View File

@@ -16,7 +16,7 @@ public interface NtcTotalReportDao {
Map getMaxStatTime();
List<Map> getTotalReportList(@Param("reportTime") Date reportTime);
List<Map> getNetFlowPortInfoNew(@Param("statTime") Date statTime);
List<TrafficTransStatistic> getBandwidthTrans(@Param("statTime") Date statTime,@Param("addrType") String addrType,@Param("transType") Integer transType);
List<TrafficTransStatistic> getBandwidthTrans(@Param("entranceId") Integer entranceId,@Param("beginDate") String beginDate,@Param("endDate") String endDate,@Param("addrType") String addrType,@Param("transType") Integer transType);
Map getEntranceMaxReportTime();
List<NtcEntranceReport> getActionTrans(@Param("reportTime") Date reportTime,@Param("entranceId") Integer entranceId,@Param("serviceSql") String serviceSql);

View File

@@ -23,7 +23,7 @@
<select id="getTotalReportList" resultType="java.util.HashMap">
SELECT SUM(reject_num) rejectNum,SUM(monitor_num) monitorNum,SUM(c2s_pkt_num) c2sPktNum,SUM(s2c_pkt_num) s2cPktNum,SUM(c2s_byte_len) c2sByteLen,SUM(s2c_byte_len) s2cByteLen,SUM(new_uni_conn_num) newUniConnNum,SUM(live_conn_num) liveConnNum,
SUM(drop_conn_num) dropConnNum,SUM(loop_conn_num) loopConnNum FROM ntc_total_report
where report_time = #{reportTime}
where report_time > DATE_SUB(#{reportTime},INTERVAL 1 hour)
</select>
<!-- 获取带宽最近时间 -->
@@ -45,7 +45,7 @@
</select>
<!-- 获取近五分钟的带宽根据ip46协议 tcp,udp变化 -->
<select id="getBandwidthTrans" resultMap="BandwidthResultMap">
<!-- <select id="getBandwidthTrans" resultMap="BandwidthResultMap">
select IFNULL(p.gbps,0) gbps,IFNULL(p.pps,0) pps,IFNULL(p.sumNum,0) linkNum, date_format(a.min5,'%Y-%m-%d %H:%i') time from
(select date_sub(#{statTime},interval @mycnt :=@mycnt + 5 MINUTE) min5
from (SELECT @mycnt:=-5) m, traffic_trans_statistic limit 12) a
@@ -68,8 +68,22 @@
) p
on date_format(p.timestamp,'%Y-%m-%d %H:%i')=date_format(a.min5,'%Y-%m-%d %H:%i')
ORDER by time
</select> -->
<!-- 获取近一小时的带宽根据ip46协议 tcp,udp变化 -->
<select id="getBandwidthTrans" resultMap="BandwidthResultMap">
select IFNULL(SUM(c2s_byte_len+s2c_byte_len),0) gbps,IFNULL(SUM(c2s_pkt_num+s2c_pkt_num),0) pps,IFNULL(SUM(link_num),0) linkNum, stat_time time from traffic_trans_statistic
where <![CDATA[stat_time<=#{endDate} and stat_time>=#{beginDate}]]>
<if test="addrType != null">
and addr_type=#{addrType}
</if>
<if test="transType != null">
and trans_type=${transType}
</if>
and entrance_id=#{entranceId}
group by stat_time order by stat_time
</select>
<!-- 获取报表最近时间 -->
<select id="getEntranceMaxReportTime" resultType="java.util.HashMap">
SELECT report_time reportTime FROM ntc_entrance_report order by report_time desc limit 1

View File

@@ -73,7 +73,7 @@ public class DashboardService extends BaseService {
* @param transType
* @return
*/
public List<HashMap> getBandwidthTrans(String addrType, Integer transType) {
/* public List<HashMap> getBandwidthTrans(String addrType, Integer transType,String beginDate,String endDate) {
ArrayList<HashMap> listMap = new ArrayList<HashMap>();
List<TrafficTransStatistic> bandwidthList = new ArrayList<TrafficTransStatistic>();
Map maxStatTime = ntcTotalReportDao.getMaxStatTime();
@@ -108,7 +108,171 @@ public class DashboardService extends BaseService {
listMap.add(m);
}
return listMap;
}*/
/**
* 根据ip46,协议tcpudp查询带宽
*
* @param addrType
* @param transType
* @return
*/
public List<HashMap> getBandwidthTrans(String addrType, Integer transType,String beginDate,String endDate){
ArrayList<HashMap> listMap = new ArrayList<HashMap>();
HashMap m1 = getBandwidthTransEntrance(addrType, transType, beginDate, endDate, 1);
if(m1!=null&&m1.size()>0) {
m1.put("entranceId", 1);// 局点1.2 不同来源
listMap.add(m1);
}
return listMap;
}
/**
* 根据ip46,协议tcpudp查询带宽
*
* @param addrType
* @param transType
* @return
*/
public List<HashMap> getBandwidthTrans2(String addrType, Integer transType,String beginDate,String endDate){
ArrayList<HashMap> listMap = new ArrayList<HashMap>();
HashMap m2 = getBandwidthTransEntrance(addrType, transType, beginDate, endDate, 2);
if(m2!=null&&m2.size()>0) {
m2.put("entranceId", 2);
listMap.add(m2);
}
return listMap;
}
/**
*查询单个局点流量的数据信息
* @param addrType
* @param transType
* @param beginDate
* @param endDate
* @param entranceId
* @return
*/
public HashMap getBandwidthTransEntrance(String addrType, Integer transType,String beginDate,String endDate,Integer entranceId) {
long start = System.currentTimeMillis();
ArrayList<HashMap> listMap = new ArrayList<HashMap>();
List<TrafficTransStatistic> bandwidthList = new ArrayList<TrafficTransStatistic>();
HashMap resulMap = new HashMap();
bandwidthList = ntcTotalReportDao.getBandwidthTrans(entranceId,beginDate,endDate,addrType, transType);
List timeList = new ArrayList();
List linkList = new ArrayList();
List gbpsList = new ArrayList();
List ppsList = new ArrayList();
if (bandwidthList != null && bandwidthList.size() > 0) {
Map<String, Comparable> m= new HashMap<String, Comparable>();
int inter=1000*60*5;// 间隔时间为五分钟
// 开始时间,结束时间 时间戳
Long b = dateToStamp(endDate);
Long e = dateToStamp(beginDate);
int num=0;
Long pointTime=b;
while(pointTime>e){
Map rm=new HashMap();
Long sumL=0l;
Long sumP=0l;
Long sumG=0l;
if(pointTime<e){
break; //停止
}
for (TrafficTransStatistic tt : bandwidthList) {
// 实际时间
String time= tt.getTime();
Long t = dateToStamp(time);
if(t>=pointTime&&t<pointTime+inter){
// 范围之内分到此pointTime组
sumL=sumL+ tt.getLinkNum();
sumP=sumP+ tt.getPps();
sumG=sumG+ tt.getGbps();
}
}
timeList.add(stampToDate(pointTime));
linkList.add(sumL);
gbpsList.add(sumG);
ppsList.add(sumP);
num=num+1;
pointTime =b-inter*num;
}
resulMap.put("linkNum", linkList);
resulMap.put("gbps", gbpsList);
resulMap.put("pps", ppsList);
resulMap.put("statTime", timeList);
}
return resulMap;
}
/**
* 将结果格式为时间间隔相同数量补0数据
* @param begin
* @param end
* @param li
* @return
*/
public static List<Map> formatDateData(String begin,String end, List<Map> li){
List<Map> resList = new ArrayList<Map>();
Map<String, Comparable> m= new HashMap<String, Comparable>();
int inter=1000*60*60;// 间隔时间为一小时
// 开始时间,结束时间 时间戳
Long b = dateToStamp(begin);
Long e = dateToStamp(end);
int num=0;
Long pointTime=b;
while(pointTime>e){
Map rm=new HashMap();
Long sum=0l;
if(pointTime<e){
break; //停止
}
for (Map<?, ?> map : li) {
// 实际时间
String time=(String) map.get("time");
Long t = dateToStamp(time);
if(t>=pointTime&&t<pointTime+inter){
// 范围之内分到此pointTime组
sum=sum+(Long) map.get("count");
}
}
rm.put("time", stampToDate(pointTime));
rm.put("sum", sum);
num=num+1;
pointTime =b-inter*num;
resList.add(rm);
}
return resList;
}
/**
* 日期格式字符串转换成时间戳
* @param date 字符串日期
* @param format 如yyyy-MM-dd HH:mm:ss
* @return
*/
public static Long dateToStamp(String date_str){
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(date_str).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return 0l;
}
/**
* 时间戳 换成日期格式字符串转
* @param 时间 1544602212000
* @param format 如yyyy-MM-dd HH:mm:ss
* @return
*/
public static String stampToDate(long timeStamp){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
String sd = sdf.format(new Date(timeStamp)); // 时间戳转换成时间
return sd;
}
/**
* 根据service 动作查询近五分钟变化趋势 entrance 默认为1,2