zmc
2023-11-02 fd934f83afae1e3fce46db8610837d0e0f4d9393
src/utils/risk_estimate_common_function/index.js
@@ -1,4 +1,5 @@
import dayjs from 'dayjs';
// import exceptionApi from '../../api/exceptionApi';
export default {
  /**
   * 计算日期相差几天
@@ -9,6 +10,7 @@
  getDaysDifference(startDate, endDate) {
    return dayjs(endDate).diff(startDate, 'day') + 1;
  },
  /**
   * 从分析数据数组中计算最小和大值 ,平均值,    在线率,有效率,超标率(后三个值为0~100取值)
   * @param:分析表中的数据
@@ -31,7 +33,6 @@
    let begin = dayjs(beginTime).format('YYYY-MM-DD');
    let end = dayjs(endTime).format('YYYY-MM-DD');
    let dayDiff = this.getDaysDifference(begin, end);
    console.log('日期间隔', dayDiff);
    let obj = {};
    // 计算最小和大值
    arr.forEach((item) => {
@@ -49,7 +50,6 @@
    });
    // 计算均值
    avg = sumAvg / dayDiff;
    // console.log('sumavg:',sumAvg,dayDiff);
    online = sumOnline / dayDiff;
    valid = sumValid / dayDiff;
    exceeding = sumExceeding / dayDiff;
@@ -64,12 +64,23 @@
    return obj;
  },
  /**
   * 计算异常类型聚集度 异常复现率
   * 计算异常类型聚集度:该时段出现的异常类型数量除8
   *  异常复现率: 量级突变,超标临近和超标次数临界在该时段出现的累计复现此次除3(比如量级突变出现3次,算作复现2次)
   * @param: 异常数据数组
   * @returns:
   */
  calRecur(exceptionArr) {
    if(exceptionArr.length == 0){
      let obj = {};
      obj['exceptionRecurrence'] = 0;
      obj['exceptionTypeAggregation'] = 0;
      return obj;
    }
    // 典型异常复现率
    let exceptionTyprRecurRate = 0;
    // 量级突变
@@ -77,7 +88,7 @@
    // 超标临近
    let exceedingNearCount = 0;
    // 超标次数临界
    let exceedindCriticalDegree = 0;
    let exceedingCriticalDegree = 0;
    // 保存出现的不同异常类型
    let exception = [];
@@ -91,7 +102,7 @@
      } else if (item.exceptionType == 5) {
        exceedingNearCount++;
      } else if (item.exceptionType == 6) {
        exceedindCriticalDegree++;
        exceedingCriticalDegree++;
      }
      // 异常类型聚集度
@@ -110,40 +121,149 @@
      sum = sum + mutationCount - 1;
    }
    if (exceedingNearCount > 1) {
      sum = sum + exceedindCriticalDegree - 1;
      sum = sum + exceedingNearCount - 1;
    }
    if (exceedindCriticalDegree > 1) {
      sum = sum + exceedindCriticalDegree - 1;
    if (exceedingCriticalDegree > 1) {
      sum = sum + exceedingCriticalDegree - 1;
    }
    // console.log('sum:', sum);
    // console.log('exception:', exception);
    // console.log(
    //   '其他',
    //   mutationCount,
    //   exceedindCriticalDegree,
    //   exceedindCriticalDegree
    // );
    switch (sum) {
      case 0:
        exceptionTyprRecurRate = sum / 3;
    switch (true) {
      case (sum == 0 || sum == 1) :
        exceptionTyprRecurRate = (sum / 3).toFixed(2);
        break;
      case 1:
        exceptionTyprRecurRate = sum / 3;
        break;
      case 2:
      case sum >= 3:
      case (sum == 2|| sum >=3) :
        exceptionTyprRecurRate = 1;
        break;
      default:
        return 'error';
    }
    exceptionTypeAggregation = exception.length / 8;
    exceptionTypeAggregation = (exception.length / 8).toFixed(2);
    let obj = {};
    obj['exceptionRecurrence'] = exceptionTyprRecurRate;
    obj['exceptionTypeAggregation'] = exceptionTypeAggregation;
    return obj;
},
// 参数:对象数组(该对象中的属性不能是引用类型,否则拷贝的值还是会相互影响)
    // 功能:拷贝该对象数组。
shallowCopyList(val) {
  if(val == 'arr'){
    let tempList = [];
    return tempList;
  }else if(val == 'obj'){
    let tempList = {};
    return tempList;
  }
};
},
getRate(obj){
  let a = {}
  a.online = obj['dayOnline']
  a.valid = obj['dayValid']
  a.exceeding = obj['dayExceeding']
  return a
},
/**
 * 找到对象数组中属性mnCode为value的对象 添加进数组中
 * @param: 对象数组 ,mnCode等于value
 * @returns:
 */
findValue(exceptionData,value){
  if(exceptionData.length==0){
    return []
  }
  let temp = []
  exceptionData.forEach((res)=>{
    if(res.mnCode == value){
      temp.push(res)
    }
  })
  return temp
},
/**
 * 计算风险值
 * @param: 数组。依次是在线率,有效率,超标率,异常类型聚集度,异常复现率
 * @returns:
 */
calRiskValue(arr){
  // 用100减 是因为该属性需要计算的是风险值,应当是离线率,无效率
  // 乘以0.01是因为去除百分号后需要再缩小100倍
  let weight = (
    (100 - parseFloat(arr[0].slice(0, -1)))*0.01 * 0.1 +
    (100 - parseFloat(arr[1].slice(0, -1)))*0.01 * 0.2 +
    parseFloat(arr[2].slice(0, -1))*0.01 * 0.2 +
    arr[3] * 0.2 +
    arr[4] * 0.3
  ).toFixed(2)
  return weight
},
/**
 * 对分析值和异常值计算风险值
 * @param: 分析数据,异常数据,开始时间,结束时间
 * @returns:表格数据
 */
merge(anaData,exceptionData,beginTime,endTime){
  if (anaData.length == 0){
    return []
  }
  const table = []
  let i = 0
  anaData.forEach((res) =>{
      let siteName = res.name
      // 从分析数据中得到设备编号
      let mnCode = res.mnCode
      // 找到异常数据中mnCode等于value的对象
      let d = this.findValue(exceptionData,mnCode)
      // let temp = [...res,...d]
      // 计算在线,有效率,超标率
      let r1 = this.getRate(res, beginTime, endTime)
      // 计算复现率
      let r2 = this.calRecur(d)
      i = i + 1
      // 数组的拷贝 防止地址引用
      let temp = this.shallowCopyList('arr')
      temp.push(r1['online'])
      temp.push(r1['valid'])
      temp.push(r1['exceeding'])
      temp.push(r2['exceptionRecurrence'])
      temp.push(r2['exceptionTypeAggregation'])
      // 计算风险值
      let weight = this.calRiskValue(temp)
      // 对象的拷贝 防止地址引用
      let obj = this.shallowCopyList('obj')
      // 构成表格的一行
      obj.region = '金山区'
      obj.monitorType = '扬尘'
      obj.siteName = res.name
      obj.beginTime = beginTime
      obj.endTime = endTime
      obj.riskValue = weight
      if (weight >= 0.6) {
        obj.riskGrage = '高风险'
        obj.riskAdvice = '建议对该站点进行线下执法检查,专项数据对比'
      } else if (weight < 0.6 && weight >= 0.2) {
        obj.riskGrage = '中风险'
        obj.riskAdvice = '建议开展常态追踪分析'
      } else {
        obj.riskGrage = '低风险'
        obj.riskAdvice = '建议引导企业长态保持'
      }
      table.push(obj)
    })
    return table
}
}