deequ 2.0.1 anomaly的代码简单分析
创始人
2024-02-07 23:43:51

anomaly 总体说明

anomaly是基于两次check的结果,因此必须使用repository 进行比较,比较方法被称为strategy,有如下几种:

  • OnlineNormalStrategy 直接override def detect ,根据平均值和标准方差进行比较 mean and standard deviation.
  • SimpleThresholdStrategy : 值必须在指定范围内
  • BatchNormalStrategy :正太分布检测,所有数据在 mean lowerDeviationFactor * stdDev 和 mean + upperDeviationFactor * stdDev之间
  • AbsoluteChangeStrategy 两次变化的绝对数量,使用super trait的diff:减法
  • RelativeRateOfChangeStrategy ,两次变化的比例,diff函数中使用除法

repository

repository 目前只有文件及内存两种,文件其实就是json格式的结果保存(带timestamp 及tag:即key)

核心代码

生成Check,check包括3部分:

 /*** Add a check using Anomaly Detection methods. The Anomaly Detection Strategy only checks* if the new value is an Anomaly.** @param anomalyDetectionStrategy 策略,可以见上一节 The anomaly detection strategy* @param analyzer 和普通check一样的Analyzer,比如size,containUrl等The analyzer for the metric to run anomaly detection on* @param anomalyCheckConfig Some configuration settings for the Check 配置,主要是指定描述、指定可以比较的上一次结果的生成日期访问、tag等等,可以忽略不用,example里面也没有用*/def addAnomalyCheck[S <: State[S]](anomalyDetectionStrategy: AnomalyDetectionStrategy,analyzer: Analyzer[S, Metric[Double]],anomalyCheckConfig: Option[AnomalyCheckConfig] = None): this.type = {val anomalyCheckConfigOrDefault = anomalyCheckConfig.getOrElse {val checkDescription = s"Anomaly check for ${analyzer.toString}"AnomalyCheckConfig(CheckLevel.Warning, checkDescription)}checks :+= VerificationRunBuilderHelper.getAnomalyCheck(metricsRepository.get,anomalyDetectionStrategy, analyzer, anomalyCheckConfigOrDefault)this}

checkConfig 类

/*** Configuration for an anomaly check** @param level         Assertion level of the check group. If any of the constraints fail this*                      level is used for the status of the check.* @param description   The name describes the check block. Generally will be used to show in*                      the logs.* @param withTagValues Can contain a Map with tag names and the corresponding values to filter*                      for the Anomaly Detection* @param afterDate     The minimum dateTime of previous AnalysisResults to use for the*                      Anomaly Detection* @param beforeDate    The maximum dateTime of previous AnalysisResults to use for the*                      Anomaly Detection* @return*/
case class AnomalyCheckConfig(level: CheckLevel.Value,description: String,withTagValues: Map[String, String] = Map.empty,afterDate: Option[Long] = None,beforeDate: Option[Long] = None)

默认Constraint

 def anomalyConstraint[S <: State[S]](analyzer: Analyzer[S, Metric[Double]],anomalyAssertion: Double => Boolean,hint: Option[String] = None): Constraint = {val constraint = AnalysisBasedConstraint[S, Double, Double](analyzer, anomalyAssertion,hint = hint)new NamedConstraint(constraint, s"AnomalyConstraint($analyzer)")}

实际比较在detect 或者diff 函数内,不同strategy 类代码稍有不同

// 默认detect调用diff进行比较
override def detect(dataSeries: Vector[Double],searchInterval: (Int, Int))
: Seq[(Int, Anomaly)] = {val (start, end) = searchIntervalrequire(start <= end,"The start of the interval cannot be larger than the end.")val startPoint = Seq(start - order, 0).maxval data = diff(DenseVector(dataSeries.slice(startPoint, end): _*), order).datadata.zipWithIndex.filter { case (value, _) =>(value < maxRateDecrease.getOrElse(Double.MinValue)|| value > maxRateIncrease.getOrElse(Double.MaxValue))}.map { case (change, index) =>(index + startPoint + order, Anomaly(Option(dataSeries(index + startPoint + order)), 1.0,Some(s"[AbsoluteChangeStrategy]: Change of $change is not in bounds [" +s"${maxRateDecrease.getOrElse(Double.MinValue)}, " +s"${maxRateIncrease.getOrElse(Double.MaxValue)}]. Order=$order")))}
}/**
RelativeRateOfChangeStrategy 的diff ,除法* Calculates the rate of change with respect to the specified order.* If the order is set to 1, the resulting value for a point at index i* is equal to dataSeries (i) / dataSeries(i - 1).* Note that this difference cannot be calculated for the first [[order]] elements in the vector.* The resulting vector is therefore smaller by [[order]] elements.** @param dataSeries The values contained in a DenseVector[Double]* @param order      The order of the derivative.* @return A vector with the resulting rates of change for all values*         except the first [[order]] elements.*/
override def diff(dataSeries: DenseVector[Double], order: Int): DenseVector[Double] = {require(order > 0, "Order of diff cannot be zero or negative")if (dataSeries.length == 0) {dataSeries} else {val valuesRight = dataSeries.slice(order, dataSeries.length)val valuesLeft = dataSeries.slice(0, dataSeries.length - order)valuesRight / valuesLeft}
}// AbsoluteChangeStrategy 使用super trait的diff:减法
def diff(dataSeries: DenseVector[Double], order: Int): DenseVector[Double] = {require(order >= 0, "Order of diff cannot be negative")if (order == 0 || dataSeries.length == 0) {dataSeries} else {val valuesRight = dataSeries.slice(1, dataSeries.length)val valuesLeft = dataSeries.slice(0, dataSeries.length - 1)diff(valuesRight - valuesLeft, order - 1)}
}

OnlineNormalStrategy 直接override def detect ,根据平均值和标准方差进行比较 mean and standard deviation.

SimpleThresholdStrategy : 值必须在指定范围内

BatchNormalStrategy :正太分布检测,所有数据在 mean - lowerDeviationFactor * stdDev 和 mean + upperDeviationFactor * stdDev之间

相关内容

热门资讯

省委部署开展持续深化违规吃喝问... 转自:中安在线近日,省委办公厅印发《关于巩固拓展深入贯彻中央八项规定精神学习教育成果持续深化违规吃喝...
智者勇进•接续奋进新江苏|用数... 转自:扬子晚报在城市加速转型、发展方式不断更新迭代的当下,雨花台区把“数智”作为提升城市运行效率的重...
中创智领:董事长、副总经理拟合... 转自:贝壳财经新京报贝壳财经讯 12月21日,中创智领公告称,董事长焦承尧、副总经理付奇和张海斌因个...
广深港高铁客流破纪录:3000... 12月20日,从广州南发出的G6541次列车准时抵达香港西九龙站,来自香港南元朗官立小学的近百名学生...
加科思-B(01167)附属与... 加科思-B(01167)公布,该集团的非全资附属公司北京加科思新药研发有限公司与AstraZenec...