优化动作,网站流量详情接口算法,动作增加drop,loop

This commit is contained in:
zhanghongqing
2018-12-23 18:09:29 +08:00
parent 27b18c9901
commit 8c7d1124a6
5 changed files with 251 additions and 120 deletions

View File

@@ -4,20 +4,79 @@ import java.util.Date;
public class TrafficHttpFocusStatistic {
private Long stat_id;
private Integer web_id;
private Long unique_num;
private Long link_num;
private Long c2s_pkt_num;
private Long s2c_pkt_num;
private Long c2s_byte_len;
private Long s2c_byte_len;
private Integer entrance_id;
private Date stat_time;
private Long statId;
private Integer webId;
private Long uniqueNum;
private Long linkNum;
private Long c2sPktNum;
private Long s2cPktNum;
private Long c2sByteLen;
private Long s2cByteLen;
private Integer entranceId;
private Date statTime;
private String time;
private Long count;
public Long getStatId() {
return statId;
}
public void setStatId(Long statId) {
this.statId = statId;
}
public Integer getWebId() {
return webId;
}
public void setWebId(Integer webId) {
this.webId = webId;
}
public Long getUniqueNum() {
return uniqueNum;
}
public void setUniqueNum(Long uniqueNum) {
this.uniqueNum = uniqueNum;
}
public Long getLinkNum() {
return linkNum;
}
public void setLinkNum(Long linkNum) {
this.linkNum = linkNum;
}
public Long getC2sPktNum() {
return c2sPktNum;
}
public void setC2sPktNum(Long c2sPktNum) {
this.c2sPktNum = c2sPktNum;
}
public Long getS2cPktNum() {
return s2cPktNum;
}
public void setS2cPktNum(Long s2cPktNum) {
this.s2cPktNum = s2cPktNum;
}
public Long getC2sByteLen() {
return c2sByteLen;
}
public void setC2sByteLen(Long c2sByteLen) {
this.c2sByteLen = c2sByteLen;
}
public Long getS2cByteLen() {
return s2cByteLen;
}
public void setS2cByteLen(Long s2cByteLen) {
this.s2cByteLen = s2cByteLen;
}
public Integer getEntranceId() {
return entranceId;
}
public void setEntranceId(Integer entranceId) {
this.entranceId = entranceId;
}
public Date getStatTime() {
return statTime;
}
public void setStatTime(Date statTime) {
this.statTime = statTime;
}
public String getTime() {
return time;
}
@@ -30,66 +89,6 @@ public class TrafficHttpFocusStatistic {
public void setCount(Long count) {
this.count = count;
}
public Long getStat_id() {
return stat_id;
}
public void setStat_id(Long stat_id) {
this.stat_id = stat_id;
}
public Integer getWeb_id() {
return web_id;
}
public void setWeb_id(Integer web_id) {
this.web_id = web_id;
}
public Long getUnique_num() {
return unique_num;
}
public void setUnique_num(Long unique_num) {
this.unique_num = unique_num;
}
public Long getLink_num() {
return link_num;
}
public void setLink_num(Long link_num) {
this.link_num = link_num;
}
public Long getC2s_pkt_num() {
return c2s_pkt_num;
}
public void setC2s_pkt_num(Long c2s_pkt_num) {
this.c2s_pkt_num = c2s_pkt_num;
}
public Long getS2c_pkt_num() {
return s2c_pkt_num;
}
public void setS2c_pkt_num(Long s2c_pkt_num) {
this.s2c_pkt_num = s2c_pkt_num;
}
public Long getC2s_byte_len() {
return c2s_byte_len;
}
public void setC2s_byte_len(Long c2s_byte_len) {
this.c2s_byte_len = c2s_byte_len;
}
public Long getS2c_byte_len() {
return s2c_byte_len;
}
public void setS2c_byte_len(Long s2c_byte_len) {
this.s2c_byte_len = s2c_byte_len;
}
public Integer getEntrance_id() {
return entrance_id;
}
public void setEntrance_id(Integer entrance_id) {
this.entrance_id = entrance_id;
}
public Date getStat_time() {
return stat_time;
}
public void setStat_time(Date stat_time) {
this.stat_time = stat_time;
}
}

View File

@@ -100,7 +100,7 @@ public class DashboardServiceController extends BaseRestController {
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.getActionTrans(beginDate, endDate, searchAction);
List<HashMap> resultList = dashboardService.getActionTrans2(beginDate, endDate, searchAction);
if (resultList != null && resultList.size() > 0) {
list = resultList;
}
@@ -880,7 +880,7 @@ public class DashboardServiceController extends BaseRestController {
try {
// 带查询时间查询所有
List<HashMap> websiteChart = dashboardService.getDomainTrans(beginDate, endDate,domain,entranceId);
List<HashMap> websiteChart = dashboardService.getDomainTrans2(beginDate, endDate,domain,entranceId);
if (websiteChart != null && websiteChart.size() > 0) {
list = websiteChart;
}

View File

@@ -128,7 +128,7 @@
</select> -->
<!-- entrance 1,2 ,动作为阻断,近一小时 间隔5分钟数据 -->
<select id="getActionTrans" resultType="com.nis.domain.restful.NtcEntranceReport">
select sum(sum) sum,report_time time from ntc_entrance_report r where
select sum(sum) sum,report_time reportTime from ntc_entrance_report r where
<![CDATA[${serviceSql} and report_time<=#{endDate} and report_time>=#{beginDate}]]>
and entrance_id=#{entranceId}
group by report_time order by report_time

View File

@@ -125,7 +125,7 @@ FROM
<select id="getTrafficHttpDomain" resultType="java.util.HashMap">
select web_id webId, max(unique_num) uniqueNum,entrance_id entranceId,SUM(c2s_byte_len + s2c_byte_len) byteCount,
sum(c2s_pkt_num + s2c_pkt_num) pktCount from traffic_http_focus_statistic t where
<![CDATA[ stat_time>= #{beginDate} and stat_time< #{endDate}]]>
<![CDATA[ stat_time> #{beginDate} and stat_time<= #{endDate}]]>
<if test="domain!=null">
and web_id in
<foreach collection="domain" index="index" item="item" open="(" separator="," close=")">
@@ -138,7 +138,7 @@ FROM
group by web_id order by unique_num
</select>
<select id="getDomainTrans" resultType="com.nis.domain.restful.dashboard.TrafficHttpFocusStatistic">
select stat_time time, max(unique_num)count from traffic_http_focus_statistic t where
select stat_time statTime, max(unique_num)count from traffic_http_focus_statistic t where
<![CDATA[ stat_time>= #{beginDate} and stat_time< #{endDate}]]>
and web_id=#{domain}
<if test="entranceId!=null">

View File

@@ -286,10 +286,13 @@ public class DashboardService extends BaseService {
/**
* 根据service 动作查询近五分钟变化趋势 entrance 默认为1,2
*/
public List<HashMap> getActionTrans(String begin, String end, String serviceType) {
/*
public List<HashMap> getActionTrans(String begin, String end, String serviceType) {
String sql = "";
String sqlBlock = "((service>=16 and service<=40) or (service>=258 and service<=273) or (service=576))"; // 阻断
String sqlMonitor = "((service>=128 and service<=152) or (service>=384 and service<=513) or (service=592) or (service>=848 and service<=1030) or (service=1152))"; // 监测
String sqlDrop = "((service1040 and service<=1042) or (service=3) or (service=5) or (service=25) or (service=28) or (service=274))"; // 丢弃
String sqlLoop = "((service=4) or (service>=832 and service<=834))"; // 回流
// 区分动作
if (StringUtils.isNotBlank(serviceType)) {
if (serviceType.equalsIgnoreCase("block")) {
@@ -298,6 +301,12 @@ public class DashboardService extends BaseService {
if (serviceType.equalsIgnoreCase("monitor")) {
sql = sqlMonitor;
}
if (serviceType.equalsIgnoreCase("drop")) {
sql = sqlDrop;
}
if (serviceType.equalsIgnoreCase("loop")) {
sql = sqlLoop;
}
}
ArrayList<HashMap> listMap = new ArrayList<HashMap>();
List<NtcEntranceReport> entrance1 = new ArrayList<NtcEntranceReport>();
@@ -363,13 +372,107 @@ public class DashboardService extends BaseService {
m1.put("count", sumList1);
m1.put("statTime", timeList);
m1.put("entranceId", 1);
m2.put("count", sumList2);// [{link1:[],time:[],entrance:"1"},]
m2.put("count", sumList2);//
m2.put("statTime", timeList);
m2.put("entranceId", 2);
listMap.add(m1);
listMap.add(m2);
return listMap;
}
*/
/**
* 根据service 动作查询近五分钟变化趋势 entrance 默认为1,2
*/
public List<HashMap> getActionTrans2(String begin, String end, String serviceType) {
String sql = "";
String sqlBlock = "((service>=16 and service<=40) or (service>=258 and service<=273) or (service=576))"; // 阻断
String sqlMonitor = "((service>=128 and service<=152) or (service>=384 and service<=513) or (service=592) or (service>=848 and service<=1030) or (service=1152))"; // 监测
String sqlDrop = "((service1040 and service<=1042) or (service=3) or (service=5) or (service=25) or (service=28) or (service=274))"; // 丢弃
String sqlLoop = "((service=4) or (service>=832 and service<=834))"; // 回流
// 区分动作
if (StringUtils.isNotBlank(serviceType)) {
if (serviceType.equalsIgnoreCase("block")) {
sql = sqlBlock;
}
if (serviceType.equalsIgnoreCase("monitor")) {
sql = sqlMonitor;
}
if (serviceType.equalsIgnoreCase("drop")) {
sql = sqlDrop;
}
if (serviceType.equalsIgnoreCase("loop")) {
sql = sqlLoop;
}
}
ArrayList<HashMap> listMap = new ArrayList<HashMap>();
List<NtcEntranceReport> entrance1 = new ArrayList<NtcEntranceReport>();
List<NtcEntranceReport> entrance2 = new ArrayList<NtcEntranceReport>();
HashMap m1 = new HashMap();
HashMap m2 = new HashMap();
entrance1 = ntcTotalReportDao.getActionTrans(begin, end, 1, sql);
entrance2 = ntcTotalReportDao.getActionTrans(begin, end, 2, sql);
Map<String, Comparable> m = new HashMap<String, Comparable>();
Date beginDate = DateUtils.parseDate(begin);
Date endDate = DateUtils.parseDate(end);
List<Date> dateRangeList = new ArrayList<Date>();
List<String> strDateRangeList = new ArrayList<String>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginDate);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
while (calendar.getTime().compareTo(endDate)<=0) {
dateRangeList.add(calendar.getTime());
strDateRangeList.add(sdf.format(calendar.getTime()));
calendar.add(Calendar.MINUTE, 5);
}
//存放每个时间点的总数
Map<String,List<Long>> ipCountListMap = new HashMap<String, List<Long>>();
int index=0;
List sumList1 =new ArrayList();
List sumList2 =new ArrayList();
for (int i = 0; i < dateRangeList.size(); i++) {
//存放一个时间点中总数
Map<String, Long> ipCountMap = new HashMap<String, Long>();
Date date = dateRangeList.get(i);
Long num1 = 0L;
Long num2 = 0L;
for (int j = index; j < entrance1.size(); j++) {
NtcEntranceReport ipInfo = entrance1.get(j);
if (ipInfo.getReportTime()!=null){
if(ipInfo.getReportTime().compareTo(date)>=0&&(i+1<dateRangeList.size()&&ipInfo.getReportTime().compareTo(dateRangeList.get(i+1))<0)) {
num1=num1+ipInfo.getSum();
}else{
index = j;
break;
}
}
}
sumList1.add(num1);
for (int j = index; j < entrance2.size(); j++) {
NtcEntranceReport ipInfo = entrance2.get(j);
if (ipInfo.getReportTime()!=null){
if(ipInfo.getReportTime().compareTo(date)>=0&&(i+1<dateRangeList.size()&&ipInfo.getReportTime().compareTo(dateRangeList.get(i+1))<0)) {
num2=num2+ipInfo.getSum();
}else{
index = j;
break;
}
}
}
sumList2.add(num2);
}
//整合 count time
m1.put("count", sumList1);
m1.put("statTime", strDateRangeList);
m1.put("entranceId", 1);
m2.put("count", sumList2);//
m2.put("statTime", strDateRangeList);
m2.put("entranceId", 2);
listMap.add(m1);
listMap.add(m2);
return listMap;
}
/**
* 最近活跃端口时间五分钟数据
@@ -1291,52 +1394,81 @@ public class DashboardService extends BaseService {
* @param entranceId
* @return
*/
public List<HashMap> getDomainTrans(String beginDate, String endDate, Integer domain, Integer entranceId) {
List<HashMap> listMap = new ArrayList<HashMap>();
HashMap resulMap = new HashMap();
/*
* public List<HashMap> getDomainTrans(String beginDate, String endDate,
* Integer domain, Integer entranceId) { List<HashMap> listMap = new
* ArrayList<HashMap>(); HashMap resulMap = new HashMap();
* List<TrafficHttpFocusStatistic> domainList = new
* ArrayList<TrafficHttpFocusStatistic>(); domainList =
* trafficHttpStatisticDao.getDomainTrans(entranceId, beginDate, endDate,
* domain); List timeList = new ArrayList(); List countList = new
* ArrayList(); if (domainList != null && domainList.size() > 0) {
* Map<String, Comparable> m = new HashMap<String, Comparable>(); int inter
* = 1000 * 60 * 30;// 间隔时间为30分钟 // 开始时间,结束时间 时间戳 Long b =
* dateToStamp(beginDate); Long e = dateToStamp(endDate); int num = 0; Long
* pointTime = b; while (pointTime < e) { Map rm = new HashMap(); Long sumL
* = 0l; if (pointTime >= e) { break; // 停止 } for (TrafficHttpFocusStatistic
* tt : domainList) { // 实际时间 String time = tt.getTime(); Long t =
* dateToStamp(time); if (t >= pointTime && t < pointTime + inter) { //
* 范围之内分到此pointTime组 sumL = sumL + tt.getCount(); } } //
* 在结束时间只有当值大于0时才记录数据防止折线降为0引起误会 if (pointTime >= e - inter && sumL > 0) {
* countList.add(sumL); timeList.add(stampToDate(pointTime)); } if
* (pointTime < e - inter) { timeList.add(stampToDate(pointTime));
* countList.add(sumL); } num = num + 1; pointTime = b + inter * num; }
* resulMap.put("count", countList); resulMap.put("statTime", timeList);
* listMap.add(resulMap); } return listMap; }
*/
/**
* 优化后的网站详情域名趋势查询
* @param beginDate
* @param endDate
* @param domain
* @param entranceId
* @return
*/
public List<HashMap> getDomainTrans2(String begin, String end, Integer domain, Integer entranceId) {
List<TrafficHttpFocusStatistic> domainList = new ArrayList<TrafficHttpFocusStatistic>();
domainList = trafficHttpStatisticDao.getDomainTrans(entranceId, beginDate, endDate, domain);
List timeList = new ArrayList();
List countList = new ArrayList();
if (domainList != null && domainList.size() > 0) {
Map<String, Comparable> m = new HashMap<String, Comparable>();
int inter = 1000 * 60 * 30;// 间隔时间为30分钟
// 开始时间,结束时间 时间戳
Long b = dateToStamp(beginDate);
Long e = dateToStamp(endDate);
int num = 0;
Long pointTime = b;
while (pointTime < e) {
Map rm = new HashMap();
Long sumL = 0l;
if (pointTime >= e) {
break; // 停止
}
for (TrafficHttpFocusStatistic tt : domainList) {
// 实际时间
String time = tt.getTime();
Long t = dateToStamp(time);
if (t >= pointTime && t < pointTime + inter) {
// 范围之内分到此pointTime组
sumL = sumL + tt.getCount();
domainList = trafficHttpStatisticDao.getDomainTrans(entranceId, begin, end, domain);
Date beginDate = DateUtils.parseDate(begin);
Date endDate = DateUtils.parseDate(end);
List<HashMap> listMap = new ArrayList<HashMap>();
List<Date> dateRangeList = new ArrayList<Date>();
List<String> strDateRangeList = new ArrayList<String>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginDate);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
while (calendar.getTime().compareTo(endDate)<=0) {
dateRangeList.add(calendar.getTime());
strDateRangeList.add(sdf.format(calendar.getTime()));
calendar.add(Calendar.MINUTE, 5);
}
//存放每个时间点的总数
Map<String,List<Long>> ipCountListMap = new HashMap<String, List<Long>>();
int index=0;
List countList =new ArrayList();
for (int i = 0; i < dateRangeList.size(); i++) {
//存放一个时间点中总数
Map<String, Long> ipCountMap = new HashMap<String, Long>();
Date date = dateRangeList.get(i);
Long num = 0L;
for (int j = index; j < domainList.size(); j++) {
TrafficHttpFocusStatistic ipInfo = domainList.get(j);
if (ipInfo.getStatTime()!=null){
if(ipInfo.getStatTime().compareTo(date)>=0&&(i+1<dateRangeList.size()&&ipInfo.getStatTime().compareTo(dateRangeList.get(i+1))<0)) {
num=num+ipInfo.getCount();
}else{
index = j;
break;
}
}
}
// 在结束时间只有当值大于0时才记录数据防止折线降为0引起误会
if (pointTime >= e - inter && sumL > 0) {
countList.add(sumL);
timeList.add(stampToDate(pointTime));
}
if (pointTime < e - inter) {
timeList.add(stampToDate(pointTime));
countList.add(sumL);
}
num = num + 1;
pointTime = b + inter * num;
countList.add(num);
}
resulMap.put("count", countList);
resulMap.put("statTime", timeList);
listMap.add(resulMap);
}
//整合 count time
HashMap ipTrafficMap = new HashMap();
ipTrafficMap.put("count", countList);
ipTrafficMap.put("statTime", strDateRangeList);
listMap.add(ipTrafficMap);
return listMap;
}