This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/controller/dashboard/TrafficStatisticsInfoController.java
zhanghongqing 6da583a766 提交误覆盖
2018-12-17 11:12:27 +08:00

488 lines
18 KiB
Java

package com.nis.web.controller.dashboard;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.beust.jcommander.internal.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.LongSerializationPolicy;
import com.google.gson.reflect.TypeToken;
import com.nis.domain.PageLog;
import com.nis.domain.configuration.WebsiteDomainTopic;
import com.nis.domain.dashboard.TrafficIpActiveStatistic;
import com.nis.util.CodeDicUtils;
import com.nis.util.Constants;
import com.nis.util.DateUtil;
import com.nis.util.StringUtil;
import com.nis.util.httpclient.HttpClientUtil;
import com.nis.web.controller.BaseController;
import com.nis.web.dao.dashboard.codedic.CodeResult;
@Controller
@RequestMapping("${adminPath}/dashboard/traffic")
public class TrafficStatisticsInfoController extends BaseController {
/**
* rejectNum 封堵数量
* newUniConn 新建连接数
* liveConnNum 活跃链接数
* dropConnNum 丢弃链接数
*
*/
@RequestMapping(value="info")
@ResponseBody
public Map ipActive(){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
HashMap<String, Object> m = new HashMap<String, Object>();
List list = new ArrayList();
try {
String string = HttpClientUtil.get(Constants.DASHBOARD_URL+Constants.NTC_TOTAL_REPORT);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
list = (ArrayList) fromJsonList.get("data");
Random r = new Random();
Double rejectNum=0d;
Double dropConnNum=0d;
Double monitorNum=0d;
Double loopConnNum=0d;
Double liveConnNum=0d;
Double newUniConnNum=0d;
Double inoctetsNum=0d;
Double outoctetsNum=0d;
Double bandwidth=0d;
if(list!=null&&list.size()>0){
Map map = (Map) list.get(0);
rejectNum = (Double) map.get("rejectNum") ;
monitorNum = (Double) map.get("monitorNum");
dropConnNum = (Double) map.get("dropConnNum");
loopConnNum = (Double) map.get("loopConnNum");
liveConnNum = (Double) map.get("liveConnNum");
newUniConnNum = (Double) map.get("newUniConnNum");
inoctetsNum = (Double) map.get("inoctets");
outoctetsNum = (Double) map.get("outoctets");
//单位换算 byte->bit/s
inoctetsNum=(inoctetsNum*8)/(5*60);
outoctetsNum=(outoctetsNum*8)/(5*60);
}
bandwidth=inoctetsNum+outoctetsNum;
m.put("rejectNum", rejectNum);
m.put("monitorNum", monitorNum);
m.put("loopConnNum", loopConnNum);
m.put("dropConnNum", dropConnNum);
m.put("liveConnNum", liveConnNum);
m.put("newUniConnNum", newUniConnNum);
m.put("bandwidth", bandwidth);
m.put("inoctetsNum", inoctetsNum);
m.put("outoctetsNum", outoctetsNum);
logger.debug("统计流量信息数据"+fromJsonList);
return m;
} catch (Exception e) {
e.printStackTrace();
logger.error("统计流量信息数据错误"+e);
}
return m;
}
@RequestMapping(value="bandwidthList")
public String bandwidthList(Model model){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
String now = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
String oneHoursAgo = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
model.addAttribute("beginDate", oneHoursAgo);
model.addAttribute("endDate", now);
return "/dashboard/trafficBandwidthList";
}
/**
* 流量统计滚动动作查看详情页面
*/
@RequestMapping(value={"trafficBlockList","trafficMonitorList"})
public String serviceBlockList(@RequestParam("searchAction")String searchAction,Model model){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
String now = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
String oneHoursAgo = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
model.addAttribute("beginDate", oneHoursAgo);
model.addAttribute("endDate", now);
model.addAttribute("searchAction", searchAction);
return "/dashboard/trafficActionTransList";
}
/**
* 根据动作查询entrance趋势 间隔5分钟数据
*/
@RequestMapping(value="actionEntranceTrans")
@ResponseBody
public List actionTrans(String beginDate,String endDate,@RequestParam("searchAction")String searchAction){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
List resultList = new ArrayList();
String url = Constants.DASHBOARD_URL+Constants.NTC_ACTION_ENTRANCE_REPORT;
url=url+"?searchAction="+searchAction;
try {
url = urlAddDate(url,beginDate,endDate);
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.debug(searchAction+"动作间隔5分钟数据"+fromJsonList);
resultList = (ArrayList) fromJsonList.get("data");
} catch (Exception e) {
e.printStackTrace();
logger.error(searchAction+"动作详情数据获取错误"+e);
resultList.add(Maps.newHashMap("error","request_service_failed"));
}
return resultList;
}
/**
*
*
* url路径时间参数格式化
* @param url
* @param beginDate
* @param endDate
* @return
* @throws URISyntaxException
*/
public String urlAddDate(String url,String beginDate,String endDate) throws URISyntaxException{
if(StringUtil.isBlank(beginDate)||StringUtil.isBlank(endDate)){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
endDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
beginDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
}
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameter("beginDate",beginDate);
uriBuilder.addParameter("endDate",endDate);
return uriBuilder.toString();
}
/**
* 根据ip46,协议tcp,udp查询带宽 间隔5分钟数据
*/
@RequestMapping(value="bandwidthTrans")
@ResponseBody
public Map bandwidthTrans(String beginDate,String endDate,@RequestParam("addrType")String addrType,@RequestParam("transType")Integer transType){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
Map map = new HashMap();
String url = Constants.DASHBOARD_URL+Constants.TRAFFIC_BANDWIDTH_TRANS;
if(!StringUtil.isBlank(addrType)){
url=url+"?addrType="+addrType;
}
if(transType!=null){
url=url+"?transType="+transType;
}
try {
url=urlAddDate(url,beginDate,endDate);
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.debug("带宽1小时,间隔5分钟数据"+fromJsonList);
List list = (ArrayList) fromJsonList.get("data");
if(!StringUtil.isEmpty(list)){
map=(Map) list.get(0);
}
return map;
} catch (Exception e) {
e.printStackTrace();
logger.error("带宽详情数据获取错误"+e);
}
return map;
}
/**
* 根据ip46,协议tcp,udp查询带宽 间隔5分钟数据
*/
@RequestMapping(value="bandwidthTransTwo")
@ResponseBody
public Map bandwidthTransTwo(String beginDate,String endDate,@RequestParam("addrType")String addrType,@RequestParam("transType")Integer transType){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
Map map = new HashMap();
String url = Constants.DASHBOARD_URL+Constants.TRAFFIC_BANDWIDTH_TRANS_TWO;
if(!StringUtil.isBlank(addrType)){
url=url+"?addrType="+addrType;
}
if(transType!=null){
url=url+"?transType="+transType;
}
try {
url=urlAddDate(url,beginDate,endDate);
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.debug("带宽1小时,间隔5分钟数据"+fromJsonList);
List list = (ArrayList) fromJsonList.get("data");
if(!StringUtil.isEmpty(list)){
map=(Map) list.get(0);
}
return map;
} catch (Exception e) {
e.printStackTrace();
logger.error("带宽详情数据获取错误"+e);
}
return map;
}
/**
* 协议类型详细列表
*/
@RequestMapping(value="protocolTypeList")
public String protocolTypeList(Model model){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
String now = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
String oneHoursAgo = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
model.addAttribute("beginDate", oneHoursAgo);
model.addAttribute("endDate", now);
return "/dashboard/trafficProtocolTypeList";
}
/**
* 协议详情统计图跟表
*/
@RequestMapping(value="protocolList")
@ResponseBody
public List protocolList(Model model,@RequestParam(required=false)String beginDate,@RequestParam(required=false)String endDate){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
List list = new ArrayList();
String url = Constants.DASHBOARD_URL+Constants.TRAFFIC_PROTOCOL_LIST;
try {
url=urlAddDate(url,beginDate,endDate);
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.debug("协议详情5分钟数据"+fromJsonList);
list = (ArrayList) fromJsonList.get("data");
Long totalLink=0l;
Long totalPackets=0l;
Double totalGByte=0d;
DecimalFormat lf = new DecimalFormat("0");
DecimalFormat df = new DecimalFormat("0.0000000000");
List<CodeResult> appCodeList = CodeDicUtils.getCodeList("appCode");
if(!StringUtil.isEmpty(list)){
for (Object object : list) {
Map m=(Map) object;
Double value1 = Double.parseDouble(m.get("protoType").toString());
totalGByte+=Double.parseDouble(m.get("GByte").toString());
// String linkNum = df.format(m.get("linkNum"));
m.put("GByte", df.format(m.get("GByte")));
m.put("packets", lf.format(m.get("packets")));
m.put("linkNum", lf.format(m.get("linkNum")));
totalLink+=Long.parseLong( m.get("linkNum").toString());
totalPackets+=Long.parseLong(m.get("packets").toString());
// 协议没匹配的匹配app码表
for (CodeResult code : appCodeList) {
Double value3 = Double.valueOf(code.getCode());
if(value1.equals(value3)){
m.put("protocolType", code.getItem());
break;
}
}
}
for (Object object : list) {
Map m=(Map) object;
m.put("totalLink", totalLink);
m.put("totalPackets", totalPackets);
m.put("totalGByte", totalGByte);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("协议详情数据获取错误"+e);
list.add(Maps.newHashMap("error","request_service_failed"));
}
return list;
}
/**
* App类型详细列表
*/
@RequestMapping(value="appTypeList")
public String appTypeList(Model model){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
String now = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
String oneHoursAgo = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
model.addAttribute("beginDate", oneHoursAgo);
model.addAttribute("endDate", now);
return "/dashboard/trafficAppTypeList";
}
/**
* App详情统计图跟表
*/
@RequestMapping(value="appList")
@ResponseBody
public List appList(@RequestParam(value="appType",required=false)Integer appType,@RequestParam(value="beginDate",required=false)String beginDate,@RequestParam(value="endDate",required=false)String endDate){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
List list = new ArrayList();
String url = Constants.DASHBOARD_URL+Constants.TRAFFIC_APP_LIST;
try {
url=urlAddDate(url,beginDate,endDate);
if(!StringUtil.isEmpty(appType)){
url=url+"&appType="+appType;
}
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.debug("app详情5分钟数据"+fromJsonList);
list = (ArrayList) fromJsonList.get("data");
Long totalLink=0l;
Long totalPackets=0l;
Double totalGByte=0d;
DecimalFormat lf = new DecimalFormat("0");
DecimalFormat df = new DecimalFormat("0.0000000000");
if(!StringUtil.isEmpty(list)){
for (Object object : list) {
Map m=(Map) object;
totalGByte+=Double.parseDouble(m.get("GByte").toString());
// String linkNum = df.format(m.get("linkNum"));
m.put("GByte", df.format(m.get("GByte")));
m.put("packets", lf.format(m.get("packets")));
m.put("linkNum", lf.format(m.get("linkNum")));
totalLink+=Long.parseLong( m.get("linkNum").toString());
totalPackets+=Long.parseLong(m.get("packets").toString());
}
for (Object object : list) {
Map m=(Map) object;
m.put("totalLink", totalLink);
m.put("totalPackets", totalPackets);
m.put("totalGByte", totalGByte);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("app详情数据获取错误"+e);
list.add(Maps.newHashMap("error","request_service_failed"));
}
return list;
}
/**
* 网址类型列表
*/
@RequestMapping("webTypeList")
public String webTypeList(Model model){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
String now = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
String oneHoursAgo = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
model.addAttribute("beginDate", oneHoursAgo);
model.addAttribute("endDate", now);
return "/dashboard/trafficWebTypeList";
}
/**
* 网站统计图跟表
*/
@RequestMapping(value="websiteList")
@ResponseBody
public List websiteList(Model model,@RequestParam(required=false)String beginDate,@RequestParam(required=false)String endDate){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
List list = new ArrayList();
String url = Constants.DASHBOARD_URL+Constants.TRAFFIC_WEBSITELIST;
// String url = "http://192.168.11.87:8088/galaxy-service/service/log/v1/"+Constants.TRAFFIC_WEBSITELIST;
try {
url=urlAddDate(url,beginDate,endDate);
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.debug("website接口数据"+fromJsonList);
list = (ArrayList) fromJsonList.get("data");
BigDecimal divisor=new BigDecimal(1024*1024*1024);
DecimalFormat df = new DecimalFormat("0.00");
DecimalFormat dl = new DecimalFormat("0");
DecimalFormat pf = new DecimalFormat("0.00%");
Double totalLink=0d;
Double totalGbyte=0d;
Double totalPackets=0d;
for (Object object : list) {
Map m=(Map)object;
Double count=(Double)m.get("count");
totalGbyte+=count;
totalLink+=(Double)m.get("linkNum");
totalPackets+=(Double)m.get("packets");
String format = df.format(count/1024/1024/1024);
m.put("Gbyte", format);
}
List<WebsiteDomainTopic> codeList = appCfgService.getDomainDict(new WebsiteDomainTopic());
Map<Long, String> map = new HashMap<Long,String>();
for (WebsiteDomainTopic websiteDomainTopic : codeList) {
if(!map.containsKey(websiteDomainTopic.getWebsiteServiceId())){
map.put(websiteDomainTopic.getId(),websiteDomainTopic.getDomain());
}
}
for (Object object : list) {
Map m=(Map)object;
Double perLink = ((Double)m.get("linkNum"))/totalLink;
m.put("perLink",pf.format(perLink));
Double perPackets = ((Double)m.get("packets"))/totalPackets;
m.put("perPackets", pf.format(perPackets));
Double perGbyte = ((Double)m.get("count"))/totalGbyte;
m.put("perGbyte", pf.format(perGbyte));
if(map.containsKey(Long.parseLong(dl.format(m.get("webId"))))){
m.put("website", map.get(Long.parseLong(dl.format(m.get("webId")))));
}
}
Collections.sort(list, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
if(o1==null&&o2!=null){
return 1;
}
if(o1!=null&&o2==null){
return -1;
}
if(o1==null&&o2==null){
return 0;
}
Map m1=(Map)o1;
Map m2=(Map)o2;
if((Double)m1.get("count")==(Double)m2.get("count")){
return 0;
}
int result=((Double)m1.get("count")-(Double)m2.get("count"))>0?-1:1;
return result;
}
});
} catch (Exception e) {
e.printStackTrace();
logger.error("网站域名数据获取错误"+e);
list.add(Maps.newHashMap("error","request_service_failed"));
}
return list;
}
}