Merge branch 'feature-traffic-log' of https://git.mesalab.cn/K18_NTCS_WEB/NTC.git into feature-traffic-log

This commit is contained in:
zhangwei
2019-01-26 20:20:40 +06:00
16 changed files with 1019 additions and 8 deletions

View File

@@ -29,4 +29,6 @@ public interface AsnGroupInfoDao extends CrudDao<AsnGroupInfo> {
List<AsnGroupInfo> findAsnGroupInfos();
Long getCount();
void modifyIssuedIp(AsnGroupInfo info);
void updateIpNum(@Param("v4Num")long v4Num,@Param("v6Num")long v6Num,@Param("groupId")Integer groupId);
List<Object[]> getASNIPNum(@Param("asnNo")Integer asnNo);
}

View File

@@ -285,4 +285,10 @@
</if>
</trim>
</select>
<update id="updateIpNum" >
update asn_group_info set v4_num=#{v4Num},v6_num=#{v6Num} where group_id=#{groupId}
</update>
<select id="getASNIPNum" resultType="map">
select v4_num,v6_num from asn_group_info where asn_id=#{asnNo}
</select>
</mapper>

View File

@@ -32,4 +32,5 @@ public interface AsnIpCfgDao extends CrudDao<AsnIpCfg>{
public int hasValidAsnIp(@Param("asnId")Long asnNo);
public AsnIpCfg getOne(AsnIpCfg cfg);
public void updateAsn(@Param("asnId")String asnNo,@Param("organization")String organization,@Param("country")String country,@Param("detail")String detail);
public List<Object[]> findAllAsnIpCfgList();
}

View File

@@ -630,6 +630,11 @@
<!-- left join policy_group_info d on r.asn_ip_group = d.service_group_id -->
where r.CFG_ID in (${ids})
</select>
<select id="findAllAsnIpCfgList" resultType="map">
select
<include refid="columns"></include>
from asn_ip_cfg r
where r.is_valid !=-1
</select>
</mapper>

View File

@@ -0,0 +1,134 @@
package com.nis.web.task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.nis.domain.basics.AsnGroupInfo;
import com.nis.domain.basics.AsnIpCfg;
import com.nis.util.IPUtil;
import com.nis.util.StringUtil;
import com.nis.web.dao.basics.AsnGroupInfoDao;
import com.nis.web.dao.basics.AsnIpCfgDao;
import com.nis.web.service.SpringContextHolder;
import net.sf.json.JSONObject;
/**
* 定时统计ASN 下IPv4和IPV6的个数
* @author intraUser
*
*/
public class StatisticASNIpNumer {
private Logger logger = Logger.getLogger(this.getClass());
@Autowired
protected AsnIpCfgDao asnIpCfgDao;
public void calculateASNIp() {
logger.info("定时统计ASN IP个数开始。。。。。。。。。。。。。。");
long start=System.currentTimeMillis();
Map<Integer,Map<String,Long>> asnNumerMap=new HashMap<>();
List<Object[]> asnIpList=new ArrayList<>();
asnIpList=asnIpCfgDao.findAllAsnIpCfgList();
getAllASNIPNumber(asnIpList, asnNumerMap);
updateAllASNIPNumber(asnNumerMap);
long end=System.currentTimeMillis();
logger.info("定时统计ASN IP个数结束:"+(end-start)+"。。。。。。。。。。。。。。");
}
public void getAllASNIPNumber(List<Object[]> asnIpList,Map<Integer,Map<String,Long>> asnNumerMap){
logger.info("计算ASN IP开始asn ip size:"+asnIpList.size()+"。。。。。。。。。。。。。。");
long start=System.currentTimeMillis();
if(!StringUtil.isEmpty(asnIpList)){
try {
for (Iterator iterator = asnIpList.iterator(); iterator.hasNext();) {
Map asnIpMap=(Map) iterator.next();
//groupId
Integer asnGroupId = (Integer)asnIpMap.get("asn_ip_group");
//ipType(4:v4,6:v6)
Integer ipType= (Integer)asnIpMap.get("ip_type");
//(1:mask,2:range,3:ip)
//Integer ipPattern= (Integer)asnIpMap.get("ip_pattern");
//dest_ip_address
String ipAddress= (String)asnIpMap.get("dest_ip_address");
long IPNumber=0;
if(ipType.equals(4)){
if(ipAddress.indexOf("/") > -1){
Integer mask=Integer.parseInt(ipAddress.split("/")[1]);
ipAddress=ipAddress.split("/")[0];
IPNumber=IPUtil.getIpNum(ipAddress, mask);
}else{
IPNumber=1;
}
//判断组是否已经存在
if(asnNumerMap.keySet().contains(asnGroupId)){
asnNumerMap.get(asnGroupId).put("v4", asnNumerMap.get(asnGroupId).get("v4")+IPNumber);
asnNumerMap.put(asnGroupId, asnNumerMap.get(asnGroupId));
}else{
Map<String, Long> map=new HashMap<>();
map.put("v4", IPNumber);
map.put("v6", 0l);
asnNumerMap.put(asnGroupId, map);
}
}else{
IPNumber=1;
//判断组是否已经存在
if(asnNumerMap.keySet().contains(asnGroupId)){
asnNumerMap.get(asnGroupId).put("v6", asnNumerMap.get(asnGroupId).get("v6")+IPNumber);
asnNumerMap.put(asnGroupId, asnNumerMap.get(asnGroupId));
}else{
Map<String, Long> map=new HashMap<>();
map.put("v4", 0l);
map.put("v6", IPNumber);
asnNumerMap.put(asnGroupId, map);
}
}
}
} catch (Exception e) {
logger.error("计算ASN IP个数失败",e);
}
}
long end=System.currentTimeMillis();
logger.info("计算ASN IP个数结束:"+(end-start)+"。。。。。。。。。。。。。。");
}
public void updateAllASNIPNumber(Map<Integer,Map<String,Long>> asnNumerMap) {
logger.info("修改ASN IP个数开始asn size:"+asnNumerMap.size()+"。。。。。。。。。。。。。。");
long start=System.currentTimeMillis();
if(!StringUtil.isEmpty(asnNumerMap)){
int index=0;
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
SqlSession batchSqlSession = null;
try {
for(Entry<Integer,Map<String,Long>> e: asnNumerMap.entrySet()) {
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
((AsnGroupInfoDao) batchSqlSession.getMapper(AsnGroupInfoDao.class)).updateIpNum(e.getValue().get("v4"),e.getValue().get("v6"),e.getKey());
if(index!= 0 && index%20000==0){
batchSqlSession.commit();
}
index++;
}
batchSqlSession.commit();
}catch (Exception e) {
logger.error("修改ASN IP个数失败", e);
} finally {
if(batchSqlSession != null){
batchSqlSession.close();
}
}
}
long end=System.currentTimeMillis();
logger.info("修改ASN IP个数结束:"+(end-start)+"。。。。。。。。。。。。。。");
}
}