修改周期行计算方式
This commit is contained in:
@@ -39,7 +39,7 @@ public class BaselineSingleThread extends Thread {
|
||||
private final CountDownLatch countDownLatch;
|
||||
|
||||
private final ArrayList<Integer> frequencyBinCounter = new ArrayList<>(Collections.nCopies(ApplicationConfig.MONITOR_FREQUENCY_BIN_NUM, 0));
|
||||
private final ArrayList<Integer> generateTypeCounter = new ArrayList<>(Collections.nCopies(3, 0));
|
||||
private final ArrayList<Integer> generateTypeCounter = new ArrayList<>(Collections.nCopies(2, 0));
|
||||
private int discardBaselineCounter = 0;
|
||||
private ArrayList<String> discardIpList = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -3,10 +3,13 @@ package cn.mesalab.utils;
|
||||
import cn.mesalab.config.ApplicationConfig;
|
||||
import cn.mesalab.dao.DruidData;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.sun.istack.internal.NotNull;
|
||||
import org.jfree.util.Log;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.time.Duration;
|
||||
@@ -21,9 +24,6 @@ import java.util.*;
|
||||
*/
|
||||
public class SeriesUtils {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SeriesUtils.class);
|
||||
|
||||
private static DruidData druidData = new DruidData();
|
||||
|
||||
public static List<Map<String, Object>> readCsvToList(String filePath) {
|
||||
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
||||
|
||||
@@ -100,38 +100,39 @@ public class SeriesUtils {
|
||||
* @return
|
||||
*/
|
||||
public static Boolean isPeriod(List<Integer> historicalSeries){
|
||||
Boolean result = true;
|
||||
boolean result = true;
|
||||
List<List<Integer>> partitions = Lists.partition(historicalSeries, 24*60/ApplicationConfig.HISTORICAL_GRAD);
|
||||
List<Integer> aggregatedPart = Arrays.asList();
|
||||
try{
|
||||
aggregatedPart = columnAverage(partitions.subList(0, ApplicationConfig.READ_HISTORICAL_DAYS-1));
|
||||
} catch (IndexOutOfBoundsException e){
|
||||
Log.error("历史");
|
||||
}
|
||||
|
||||
// Pearson corrcoef
|
||||
double pearsonCorrelationScore = getPearsonCorrelationScore(aggregatedPart.stream().mapToInt(Integer::valueOf).toArray(),
|
||||
partitions.get(partitions.size() - 1).stream().mapToInt(Integer::valueOf).toArray());
|
||||
|
||||
int size = partitions.size();
|
||||
ArrayList<Double> corrScores = new ArrayList<>();
|
||||
for(int i = 0; i < size; i ++){
|
||||
for(int j = i+1; j < size; j ++){
|
||||
corrScores.add(getPearsonCorrelationScore(partitions.get(i).stream().mapToDouble(Number::doubleValue).toArray(),
|
||||
partitions.get(j).stream().mapToDouble(Integer::valueOf).toArray()));
|
||||
}
|
||||
}
|
||||
double pearsonCorrelationScore = corrScores.stream().mapToDouble(Number::doubleValue).average().orElse(0.0);
|
||||
if (pearsonCorrelationScore < ApplicationConfig.BASELINE_PERIOD_CORR_THRE){
|
||||
result=false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static double getPearsonCorrelationScore(int[] xData, int[] yData) {
|
||||
public static double getPearsonCorrelationScore(double[] xData, double[] yData) {
|
||||
|
||||
if (xData.length != yData.length) {
|
||||
Log.error("Pearson CorrelationScore 数组长度不相等!");
|
||||
}
|
||||
int xMeans;
|
||||
int yMeans;
|
||||
double xMeans;
|
||||
double yMeans;
|
||||
double numerator = 0;
|
||||
double denominator = 0;
|
||||
|
||||
double result = 0;
|
||||
// 拿到两个数据的平均值
|
||||
xMeans = (int) getMeans(xData);
|
||||
yMeans = (int) getMeans(yData);
|
||||
xMeans = getMeans(xData);
|
||||
yMeans = getMeans(yData);
|
||||
// 计算皮尔逊系数的分子
|
||||
numerator = generateNumerator(xData, xMeans, yData, yMeans);
|
||||
// 计算皮尔逊系数的分母
|
||||
@@ -143,7 +144,7 @@ public class SeriesUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int generateNumerator(int[] xData, int xMeans, int[] yData, int yMeans) {
|
||||
private static float generateNumerator(double[] xData, double xMeans, double[] yData, double yMeans) {
|
||||
int numerator = 0;
|
||||
for (int i = 0; i < xData.length; i++) {
|
||||
numerator += (xData[i] - xMeans) * (yData[i] - yMeans);
|
||||
@@ -151,22 +152,22 @@ public class SeriesUtils {
|
||||
return numerator;
|
||||
}
|
||||
|
||||
private static double generateDenomiator(int[] xData, int xMeans, int[] yData, int yMeans) {
|
||||
private static double generateDenomiator(double[] xData, double xMeans, double[] yData, double yMeans) {
|
||||
double xSum = 0.0;
|
||||
for (int i = 0; i < xData.length; i++) {
|
||||
xSum += (xData[i] - xMeans) * (xData[i] - xMeans);
|
||||
for (double xDatum : xData) {
|
||||
xSum += (xDatum - xMeans) * (xDatum - xMeans);
|
||||
}
|
||||
double ySum = 0.0;
|
||||
for (int i = 0; i < yData.length; i++) {
|
||||
ySum += (yData[i] - yMeans) * (yData[i] - yMeans);
|
||||
for (double yDatum : yData) {
|
||||
ySum += (yDatum - yMeans) * (yDatum - yMeans);
|
||||
}
|
||||
return Math.sqrt(xSum) * Math.sqrt(ySum);
|
||||
}
|
||||
|
||||
private static double getMeans(int[] datas) {
|
||||
double sum = 0.0;
|
||||
for (int i = 0; i < datas.length; i++) {
|
||||
sum += datas[i];
|
||||
private static double getMeans(double[] datas) {
|
||||
float sum = 0;
|
||||
for (double data : datas) {
|
||||
sum += data;
|
||||
}
|
||||
return sum / datas.length;
|
||||
}
|
||||
@@ -175,8 +176,8 @@ public class SeriesUtils {
|
||||
ArrayList<Integer> averages = new ArrayList<>();
|
||||
for(int i=0; i<list.get(0).size(); i++){
|
||||
int columnSum = 0;
|
||||
for(int j = 0; j< list.size(); j++){
|
||||
columnSum += list.get(j).get(i);
|
||||
for (List<Integer> integers : list) {
|
||||
columnSum += integers.get(i);
|
||||
}
|
||||
averages.add(columnSum / list.size());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user