1.增加了 地域流量,配置日志,丢弃量接口

2.增加了一个时间处理的工具类
This commit is contained in:
lihaochen
2018-12-10 17:55:36 +08:00
parent feb178ef2a
commit 067bc29ceb
8 changed files with 949 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
package com.nis.web.controller.restful;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.nis.domain.Page;
import com.nis.domain.restful.CfgLogInfo;
import com.nis.domain.restful.DropInfo;
import com.nis.domain.restful.NtcAreaHomeReport;
import com.nis.restful.RestBusinessCode;
import com.nis.restful.RestResult;
import com.nis.restful.RestServiceException;
import com.nis.restful.ServiceRuntimeException;
import com.nis.util.Constants;
import com.nis.web.controller.BaseRestController;
import com.nis.web.service.AuditLogThread;
import com.nis.web.service.ServicesRequestLogService;
import com.nis.web.service.restful.SystemHomePageService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
/**
* @Description 系统首页整体图API(1.地域流量API 2.配置统计API 3.日志统计API 4.丢弃量API)
* @author dell
* @date 2018年12月5日10:34:03
*
*/
@RestController
@RequestMapping("${servicePath}")
@Api(value = "SystemHomePageController", description = "系统首页整体图接口")
@SuppressWarnings("all")
public class SystemHomePageController extends BaseRestController {
@Autowired
ServicesRequestLogService servicesRequestLogService;
@Autowired
SystemHomePageService systemHomePageService;
@RequestMapping(value = "/log/v1/trafficAreaStat", method = RequestMethod.GET)
@ApiOperation(value = "地域流量获取", httpMethod = "GET", notes = "对地域流量获取服务信息进行查询", response = Map.class)
public Map<String, ?> trafficAreaStat(String searchReportStartTime, String searchReportEndTime,
HttpServletRequest request, HttpServletResponse response) {
long start = System.currentTimeMillis();
AuditLogThread saveLogThread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_GET, request,
null);
Page<NtcAreaHomeReport> trafficAreaStatPage = null;
try {
trafficAreaStatPage = systemHomePageService.getTrafficAreaStat(searchReportStartTime, searchReportEndTime,
request, response);
} catch (Exception e) {
saveLogThread.setExceptionInfo(e.getMessage() + " " + e.getCause());
e.printStackTrace();
logger.error(e.getMessage());
if (!(e instanceof RestServiceException)) {
throw new ServiceRuntimeException(saveLogThread, System.currentTimeMillis() - start,
"获取地域流量失败:" + e.getMessage(), RestBusinessCode.service_runtime_error.getValue());
} else {
throw ((RestServiceException) e);
}
}
return serviceLogResponse(saveLogThread, System.currentTimeMillis() - start, request, "获取地域流量成功",
trafficAreaStatPage, 0);
}
@RequestMapping(value = "/log/v1/cfgSortLogStat", method = RequestMethod.GET)
@ApiOperation(value = "配置日志TOP5统计获取", httpMethod = "GET", notes = "对配置日志TOP5统计信息进行查询", response = Map.class)
public Map<String, ?> cfgSortLogStat(String searchReportStartTime, String searchReportEndTime,
HttpServletRequest request, HttpServletResponse response) {
long start = System.currentTimeMillis();
AuditLogThread saveLogThread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_GET, request,
null);
Page<CfgLogInfo> cfgSortLogStat = null;
try {
cfgSortLogStat = systemHomePageService.getCfgSortLogStat(searchReportStartTime, searchReportEndTime,
request, response);
} catch (Exception e) {
saveLogThread.setExceptionInfo(e.getMessage() + " " + e.getCause());
e.printStackTrace();
logger.error(e.getMessage());
if (!(e instanceof RestServiceException)) {
throw new ServiceRuntimeException(saveLogThread, System.currentTimeMillis() - start,
"获取配置日志TOP5统计失败:" + e.getMessage(), RestBusinessCode.service_runtime_error.getValue());
} else {
throw ((RestServiceException) e);
}
}
return serviceLogResponse(saveLogThread, System.currentTimeMillis() - start, request, "获取配置日志TOP5统计成功",
cfgSortLogStat, 0);
}
@RequestMapping(value = "/log/v1/blockAndDropStat", method = RequestMethod.GET)
@ApiOperation(value = "丢弃量获取", httpMethod = "GET", notes = "对阻断和丢弃量进行查询", response = Map.class)
public Map<String, ?> blockAndDropStat(String searchReportStartTime, String searchReportEndTime,
@RequestParam(required = true) int searchBusinessType, HttpServletRequest request,
HttpServletResponse response) {
long start = System.currentTimeMillis();
AuditLogThread saveLogThread = super.saveRequestLog(servicesRequestLogService, Constants.OPACTION_GET, request,
null);
Page<DropInfo> dropInfo = null;
try {
dropInfo = systemHomePageService.getBlockAndDropStat(searchReportStartTime, searchReportEndTime,
searchBusinessType, request, response);
} catch (Exception e) {
saveLogThread.setExceptionInfo(e.getMessage() + " " + e.getCause());
e.printStackTrace();
logger.error(e.getMessage());
if (!(e instanceof RestServiceException)) {
throw new ServiceRuntimeException(saveLogThread, System.currentTimeMillis() - start,
"获取丢弃量失败:" + e.getMessage(), RestBusinessCode.service_runtime_error.getValue());
} else {
throw ((RestServiceException) e);
}
}
return serviceLogResponse(saveLogThread, System.currentTimeMillis() - start, request, "获取丢弃量成功", dropInfo, 0);
}
}

View File

@@ -0,0 +1,24 @@
package com.nis.web.dao;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.nis.domain.restful.CfgLogInfo;
import com.nis.domain.restful.DropInfo;
import com.nis.domain.restful.NtcAreaHomeReport;
@MyBatisDao
public interface SystemHomePageDao {
List<NtcAreaHomeReport> getTrafficAreaStatList(@Param("startTime") Date searchReportStartTime,
@Param("endTime") Date searchReportEndTime);
List<CfgLogInfo> getCfgSortLogStatList(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
List<DropInfo> getBlockAndDropStatListAll(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
List<DropInfo> getBlockAndDropStatListMinute(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
}

View File

@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nis.web.dao.SystemHomePageDao">
<!-- 地域流量 -->
<select id="getTrafficAreaStatList" resultType="com.nis.domain.restful.NtcAreaHomeReport"
useCache="false" flushCache="true">
SELECT
device.entrance_id entranceId,
CASE device.entrance_id
WHEN 1 THEN
'阿斯塔纳'
WHEN 2 THEN
'阿拉木图'
ELSE
- 1
END AS area,
(netflow.INOCTETS_SPEED+
netflow.OUTOCTETS_SPEED) as sum,
netflow.RECV_TIME reportTime
FROM
ntc_device_info device
LEFT JOIN (
SELECT
netflow.INOCTETS_SPEED
INOCTETS_SPEED,
netflow.OUTOCTETS_SPEED
OUTOCTETS_SPEED,
netflow.RECV_TIME,
netflow.NODE_IP
FROM
traffic_netflow_port_info netflow
<where>
<if
test="null!=startTime and null!=endTime and ''!=startTime and ''!=endTime">
netflow.RECV_TIME&gt;#{startTime}
and
netflow.RECV_TIME&lt;=#{endTime}
</if>
</where>
) netflow
ON device.manager_ip = netflow.NODE_IP
WHERE
netflow.RECV_TIME
IS NOT
NULL
</select>
<!-- 配置日志TOP统计 -->
<select id="getCfgSortLogStatList" resultType="com.nis.domain.restful.CfgLogInfo"
useCache="false" flushCache="true">
SELECT
dic.service_code as serviceCode,
sum(service.sum) as sum,
dic.service_name_zh as serviceNameZh,
dic.service_name_en as
serviceNameEn,
dic.service_name_ru as serviceNameRu,
service.report_time as reportTime
FROM
ntc_service_report
service,ntc_service_dic dic
WHERE
service.service =
dic.service_code
and
<if
test="null!=startTime and null!=endTime and ''!=startTime and ''!=endTime">
service.report_time&gt;#{startTime} and
service.report_time&lt;=#{endTime}
</if>
GROUP BY
service.report_time,
service.service
ORDER BY
service.report_time ASC,
service.sum DESC
</select>
<!-- 丢弃量 -->
<!-- 总量 -->
<select id="getBlockAndDropStatListAll" resultType="com.nis.domain.restful.DropInfo"
useCache="false" flushCache="true">
SELECT
'drop' AS label,
IFNULL(
(
sum(reject_num) + SUM(drop_conn_num)
),
0
) AS sum,#{startTime} as reportTime
FROM
ntc_total_report
<where>
<if
test="null!=startTime and null!=endTime and ''!=startTime and ''!=endTime">
report_time&gt;#{startTime} and
report_time&lt;=#{endTime}
</if>
</where>
order by report_time
asc;
</select>
<!-- 五分钟业务 -->
<select id="getBlockAndDropStatListMinute" resultType="com.nis.domain.restful.DropInfo"
useCache="false" flushCache="true">
SELECT
'drop' AS label,
IFNULL(
(
sum(reject_num) + SUM(drop_conn_num)
),
0
) AS sum,report_time as reportTime
FROM
ntc_total_report
<where>
<if
test="null!=startTime and null!=endTime and ''!=startTime and ''!=endTime">
report_time&gt;#{startTime} and
report_time&lt;=#{endTime}
</if>
</where>
GROUP BY report_time
order by report_time
asc;
</select>
</mapper>

View File

@@ -0,0 +1,222 @@
package com.nis.web.service.restful;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import com.nis.domain.Page;
import com.nis.domain.restful.CfgLogInfo;
import com.nis.domain.restful.DropInfo;
import com.nis.domain.restful.NtcAreaHomeReport;
import com.nis.util.CalendarUtils;
import com.nis.web.dao.SystemHomePageDao;
@Service
public class SystemHomePageService {
@Autowired
SystemHomePageDao systemHomePageDao;
protected final Logger logger = Logger.getLogger(SystemHomePageService.class);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取地域流量
public Page<NtcAreaHomeReport> getTrafficAreaStat(String searchReportStartTime, String searchReportEndTime,
HttpServletRequest request, HttpServletResponse response) {
List<NtcAreaHomeReport> list = new LinkedList<NtcAreaHomeReport>();
Page<NtcAreaHomeReport> page = new Page<>();
Date endTime = null;
Date startTime = null;
if (null != searchReportStartTime && null != searchReportEndTime && !"".equals(searchReportStartTime)
&& !"".equals(searchReportEndTime)) {
logger.info("开始获取地域流量,参数为{"
+ ((null == searchReportStartTime && !"".equals(searchReportStartTime)) ? ""
: searchReportStartTime)
+ "},{"
+ ((null == searchReportEndTime && !"".equals(searchReportEndTime)) ? "" : searchReportEndTime)
+ "}");
try {
startTime = format.parse(searchReportStartTime);
endTime = format.parse(searchReportEndTime);
} catch (ParseException e) {
e.printStackTrace();
logger.error("处理时间转换异常,{}", e);
}
} else {
endTime = CalendarUtils.getRangeOf5min(0);
startTime = CalendarUtils.getTPrevHour(endTime, -24);
logger.info("获取地域流量API请求参数为空,默认查询最近24小时");
}
list = systemHomePageDao.getTrafficAreaStatList(startTime, endTime);
if (null != list && 0 != list.size()) {
list = converNtcAreaHomeList(list, startTime, endTime);
}
logger.trace("查询地域流量结束");
page.setList(list);
return page;
}
// 获取配置日志TOP5统计
public Page<CfgLogInfo> getCfgSortLogStat(String searchReportStartTime, String searchReportEndTime,
HttpServletRequest request, HttpServletResponse response) {
List<CfgLogInfo> list = new LinkedList<CfgLogInfo>();
Page<CfgLogInfo> page = new Page<>();
Date endTime = null;
Date startTime = null;
if (null != searchReportStartTime && null != searchReportEndTime && !"".equals(searchReportStartTime)
&& !"".equals(searchReportEndTime)) {
logger.info("开始获取配置日志TOP5,参数为{"
+ ((null == searchReportStartTime && !"".equals(searchReportStartTime)) ? ""
: searchReportStartTime)
+ "},{"
+ ((null == searchReportEndTime && !"".equals(searchReportEndTime)) ? "" : searchReportEndTime)
+ "}");
try {
startTime = format.parse(searchReportStartTime);
endTime = format.parse(searchReportEndTime);
} catch (ParseException e) {
e.printStackTrace();
logger.error("处理时间转换异常,{}", e);
}
} else {
endTime = CalendarUtils.getRangeOf5min(0);
startTime = CalendarUtils.getTPrevHour(endTime, -24);
logger.info("获取配置日志TOP5API请求参数为空,默认查询最近24小时");
}
list = systemHomePageDao.getCfgSortLogStatList(startTime, endTime);
if (null != list && 0 != list.size()) {
list = converCfgLogList(list, startTime, endTime);
} else {
logger.info("没有查到配置日志Top的数据");
}
logger.trace("查询配置日志TOP5结束");
page.setList(list);
return page;
}
// 获取丢弃量
public Page<DropInfo> getBlockAndDropStat(String searchReportStartTime, String searchReportEndTime,
int searchBusinessType, HttpServletRequest request, HttpServletResponse response) {
List<DropInfo> list = new LinkedList<DropInfo>();
Page<DropInfo> page = new Page<>();
Date endTime = null;
Date startTime = null;
if (null != searchReportStartTime && null != searchReportEndTime && !"".equals(searchReportStartTime)
&& !"".equals(searchReportEndTime)) {
logger.info("开始获取丢弃量,参数为{"
+ ((null == searchReportStartTime && !"".equals(searchReportStartTime)) ? ""
: searchReportStartTime)
+ "},{"
+ ((null == searchReportEndTime && !"".equals(searchReportEndTime)) ? "" : searchReportEndTime)
+ "},{" + searchBusinessType + "}");
try {
startTime = format.parse(searchReportStartTime);
endTime = format.parse(searchReportEndTime);
} catch (ParseException e) {
e.printStackTrace();
logger.error("处理时间转换异常,{}", e);
}
} else {
endTime = CalendarUtils.getRangeOf5min(0);
startTime = CalendarUtils.getTPrevHour(endTime, -24);
logger.info("获取丢弃量API请求参数为空,默认查询最近24小时");
}
if (1 == searchBusinessType) {
logger.info("查询类型为:{丢弃总量业务}");
list = systemHomePageDao.getBlockAndDropStatListAll(startTime, endTime);
} else if (2 == searchBusinessType) {
logger.info("查询类型为:{丢弃量统计业务-分钟}");
list = systemHomePageDao.getBlockAndDropStatListMinute(startTime, endTime);
} else {
logger.error("searchBusinessType参数异常,默认查询总量业务");
list = systemHomePageDao.getBlockAndDropStatListAll(startTime, endTime);
}
logger.trace("查询丢弃量结束");
page.setList(list);
return page;
}
/**
* 进行筛选配置日志的Top5
*
* @param list
* @param startTime
* @param endTime
* @return
*/
public List<CfgLogInfo> converCfgLogList(List<CfgLogInfo> list, Date startTime, Date endTime) {
List<CfgLogInfo> newList = new LinkedList<>();
long startTimes = (startTime.getTime() + 300000);
long endTimes = endTime.getTime();
for (; startTimes <= endTimes; startTimes += 300000) {
int nums = 0;
for (int i = 0; i < list.size(); i++) {
if (nums == 5) {
break;
}
CfgLogInfo cfgLogInfo = list.get(i);
if (cfgLogInfo.getReportTime().getTime() == startTimes) {
newList.add(cfgLogInfo);
nums++;
}
}
}
return newList;
}
public List<NtcAreaHomeReport> converNtcAreaHomeList(List<NtcAreaHomeReport> list, Date startTime, Date endTime) {
final String area1 = "阿斯塔纳";
final String area2 = "阿拉木图";
List<NtcAreaHomeReport> newList = new LinkedList<>();
long startTimes = (startTime.getTime() + 300000);
long endTimes = endTime.getTime();
for (; startTimes <= endTimes; startTimes += 300000) {
NtcAreaHomeReport ntcAreaHomeReportArea1 = new NtcAreaHomeReport();
NtcAreaHomeReport ntcAreaHomeReportArea2 = new NtcAreaHomeReport();
long sums1 = 0;
long sums2 = 0;
for (int i = 0; i < list.size(); i++) {
NtcAreaHomeReport ntcAreaHomeReport2 = list.get(i);
if (ntcAreaHomeReport2.getReportTime().getTime() > startTimes
&& ntcAreaHomeReport2.getReportTime().getTime() <= (startTimes + 300000)) {
if (ntcAreaHomeReport2.getEntranceId() == 1) {
sums1 = sums1 + ntcAreaHomeReport2.getSum();
} else if (ntcAreaHomeReport2.getEntranceId() == 2) {
sums2 = sums2 + ntcAreaHomeReport2.getSum();
} else {
logger.debug("未知地域,地域ID为{" + ntcAreaHomeReport2.getEntranceId() + "}");
}
}
}
ntcAreaHomeReportArea1.setEntranceId(1);
ntcAreaHomeReportArea2.setEntranceId(2);
ntcAreaHomeReportArea1.setArea(area1);
ntcAreaHomeReportArea2.setArea(area2);
ntcAreaHomeReportArea1.setSum(sums1);
ntcAreaHomeReportArea2.setSum(sums2);
ntcAreaHomeReportArea1.setReportTime(new Date(startTimes));
ntcAreaHomeReportArea2.setReportTime(new Date(startTimes));
newList.add(ntcAreaHomeReportArea1);
newList.add(ntcAreaHomeReportArea2);
}
return newList;
}
}