2023-05-06 15:08:21 +08:00
|
|
|
package com.zdjizhi.utils.general;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.log.Log;
|
|
|
|
|
import cn.hutool.log.LogFactory;
|
2023-05-19 14:03:40 +08:00
|
|
|
import com.zdjizhi.common.pojo.Fields;
|
2023-05-06 15:08:21 +08:00
|
|
|
import com.zdjizhi.utils.StringUtil;
|
|
|
|
|
import org.apache.datasketches.hll.HllSketch;
|
|
|
|
|
import org.apache.datasketches.hll.Union;
|
|
|
|
|
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author qidaijie
|
|
|
|
|
* @Package com.zdjizhi.storm.utils.general
|
|
|
|
|
* @Description:
|
|
|
|
|
* @date 2021/7/2015:31
|
|
|
|
|
*/
|
|
|
|
|
public class MetricUtil {
|
|
|
|
|
private static final Log logger = LogFactory.get();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于对业务指标进行统计
|
|
|
|
|
*
|
|
|
|
|
* @param cacheData 缓存中数据
|
|
|
|
|
* @param newData 新数据
|
|
|
|
|
*/
|
2023-05-19 14:03:40 +08:00
|
|
|
public static Fields statisticsMetrics(Fields cacheData, Fields newData) {
|
|
|
|
|
|
|
|
|
|
Long sessions = MetricUtil.longSum(cacheData.getSessions(), newData.getSessions());
|
|
|
|
|
|
|
|
|
|
Long inBytes = MetricUtil.longSum(cacheData.getIn_bytes(), newData.getIn_bytes());
|
|
|
|
|
Long outBytes = MetricUtil.longSum(cacheData.getOut_bytes(), newData.getOut_bytes());
|
|
|
|
|
Long inPkts = MetricUtil.longSum(cacheData.getIn_pkts(), newData.getIn_pkts());
|
|
|
|
|
Long outPkts = MetricUtil.longSum(cacheData.getOut_pkts(), newData.getOut_pkts());
|
|
|
|
|
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long c2sBytes = MetricUtil.longSum(cacheData.getC2s_bytes(), newData.getC2s_bytes());
|
|
|
|
|
Long s2cBytes = MetricUtil.longSum(cacheData.getS2c_bytes(), newData.getS2c_bytes());
|
|
|
|
|
Long c2sPkts = MetricUtil.longSum(cacheData.getC2s_pkts(), newData.getC2s_pkts());
|
|
|
|
|
Long s2cPkts = MetricUtil.longSum(cacheData.getS2c_pkts(), newData.getS2c_pkts());
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long c2sFragments = MetricUtil.longSum(cacheData.getC2s_fragments(), newData.getC2s_fragments());
|
|
|
|
|
Long s2cFragments = MetricUtil.longSum(cacheData.getS2c_fragments(), newData.getS2c_fragments());
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long c2sTcpLostBytes = MetricUtil.longSum(cacheData.getC2s_tcp_lost_bytes(), newData.getC2s_tcp_lost_bytes());
|
|
|
|
|
Long s2cTcpLostBytes = MetricUtil.longSum(cacheData.getS2c_tcp_lost_bytes(), newData.getS2c_tcp_lost_bytes());
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long c2sTcpooorderPkts = MetricUtil.longSum(cacheData.getC2s_tcp_ooorder_pkts(), newData.getC2s_tcp_ooorder_pkts());
|
|
|
|
|
Long s2cTcpooorderPkts = MetricUtil.longSum(cacheData.getS2c_tcp_ooorder_pkts(), newData.getS2c_tcp_ooorder_pkts());
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long c2sTcpretransmittedPkts = MetricUtil.longSum(cacheData.getC2s_tcp_retransmitted_pkts(), newData.getC2s_tcp_retransmitted_pkts());
|
|
|
|
|
Long s2cTcpretransmittedPkts = MetricUtil.longSum(cacheData.getS2c_tcp_retransmitted_pkts(), newData.getS2c_tcp_retransmitted_pkts());
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long c2sTcpretransmittedBytes = MetricUtil.longSum(cacheData.getC2s_tcp_retransmitted_bytes(), newData.getC2s_tcp_retransmitted_bytes());
|
|
|
|
|
Long s2cTcpretransmittedBytes = MetricUtil.longSum(cacheData.getS2c_tcp_retransmitted_bytes(), newData.getS2c_tcp_retransmitted_bytes());
|
2023-05-06 15:08:21 +08:00
|
|
|
|
2023-12-14 10:55:03 +08:00
|
|
|
|
2023-11-09 14:13:45 +08:00
|
|
|
// String clientIpSketch = MetricUtil.hllSketchUnion(cacheData.getClient_ip_sketch(), newData.getClient_ip_sketch());
|
|
|
|
|
// return new Fields(sessions,
|
|
|
|
|
// inBytes, outBytes, inPkts, outPkts,
|
|
|
|
|
// c2sPkts, s2cPkts, c2sBytes, s2cBytes,
|
|
|
|
|
// c2sFragments, s2cFragments,
|
|
|
|
|
// c2sTcpLostBytes, s2cTcpLostBytes,
|
|
|
|
|
// c2sTcpooorderPkts, s2cTcpooorderPkts,
|
|
|
|
|
// c2sTcpretransmittedPkts, s2cTcpretransmittedPkts,
|
|
|
|
|
// c2sTcpretransmittedBytes, s2cTcpretransmittedBytes,
|
|
|
|
|
// clientIpSketch);
|
|
|
|
|
|
|
|
|
|
return new Fields(sessions,
|
|
|
|
|
inBytes, outBytes, inPkts, outPkts,
|
|
|
|
|
c2sPkts, s2cPkts, c2sBytes, s2cBytes,
|
|
|
|
|
c2sFragments, s2cFragments,
|
|
|
|
|
c2sTcpLostBytes, s2cTcpLostBytes,
|
|
|
|
|
c2sTcpooorderPkts, s2cTcpooorderPkts,
|
|
|
|
|
c2sTcpretransmittedPkts, s2cTcpretransmittedPkts,
|
|
|
|
|
c2sTcpretransmittedBytes, s2cTcpretransmittedBytes,
|
|
|
|
|
null);
|
2023-05-06 15:08:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Long类型的数据求和
|
|
|
|
|
*
|
2023-12-14 10:55:03 +08:00
|
|
|
* @param cacheData 缓存中的值
|
|
|
|
|
* @param newData 新来数据的值
|
|
|
|
|
* @return cacheData + newData
|
2023-05-06 15:08:21 +08:00
|
|
|
*/
|
2023-12-14 10:55:03 +08:00
|
|
|
private static Long longSum(Long cacheData, Long newData) {
|
|
|
|
|
|
2023-05-19 14:03:40 +08:00
|
|
|
Long result;
|
2023-05-06 15:08:21 +08:00
|
|
|
try {
|
2023-12-14 10:55:03 +08:00
|
|
|
if (cacheData >= 0 && newData >= 0) {
|
|
|
|
|
result = cacheData + newData;
|
2023-05-06 15:08:21 +08:00
|
|
|
} else {
|
2023-12-14 10:55:03 +08:00
|
|
|
result = cacheData;
|
2023-05-06 15:08:21 +08:00
|
|
|
}
|
|
|
|
|
} catch (RuntimeException e) {
|
2023-12-14 10:55:03 +08:00
|
|
|
logger.error("Abnormal sending of traffic indicator statistics! The message is:{}" , e);
|
|
|
|
|
result = cacheData;
|
2023-05-06 15:08:21 +08:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param cacheHll 缓存的sketch
|
|
|
|
|
* @param newHll 聚合后的sketch
|
|
|
|
|
* @return 合并后的sketch
|
|
|
|
|
*/
|
|
|
|
|
private static String hllSketchUnion(String cacheHll, String newHll) {
|
|
|
|
|
Union union = new Union(12);
|
|
|
|
|
try {
|
|
|
|
|
if (StringUtil.isNotBlank(cacheHll)) {
|
|
|
|
|
byte[] cacheHllBytes = Base64.getDecoder().decode(cacheHll);
|
|
|
|
|
HllSketch cacheSketch = HllSketch.heapify(cacheHllBytes);
|
|
|
|
|
union.update(cacheSketch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtil.isNotBlank(newHll)) {
|
|
|
|
|
byte[] newHllBytes = Base64.getDecoder().decode(newHll);
|
|
|
|
|
HllSketch newSketch = HllSketch.heapify(newHllBytes);
|
|
|
|
|
union.update(newSketch);
|
|
|
|
|
}
|
|
|
|
|
return Base64.getEncoder().encodeToString(union.getResult().toUpdatableByteArray());
|
|
|
|
|
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
logger.error("Merge hllSketch results abnormal! The message is:" + e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|