

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# PreviewAnomalyDetector API
<a name="anomaly-detection-api"></a>

使用 `PreviewAnomalyDetector` 操作创建端点，用于演示异常检测算法在指定时间段内如何分析您的指标数据。此端点有助于您在实施之前评估和验证检测器的性能。

有效的 HTTP 动词  
`GET`, `POST`

支持的有效载荷类型  
URL 编码的参数  
`POST` 的 `application/x-www-form-urlencoded`

支持的参数  
`query=<string>` Prometheus 表达式查询字符串。  
`start=<rfc3339 | unix_timestamp>` 如果您使用 `query_range` 查询一段时间范围，则为开始时间戳。  
`end=<rfc3339 | unix_timestamp>` 如果您使用 `query_range` 查询一段时间范围，则为结束时间戳。  
`step=<duration | float>` `duration` 格式的查询解析步长，或者是 `float` 秒数。仅在您使用 `query_range` 查询一段时间范围时使用，并且对于此类查询是必需的。

## 查询参数格式
<a name="anomaly-detection-query-formatting"></a>

在查询参数中用 RandomCutForest (RCF) 伪函数包装原始 PromQL 表达式。有关更多信息，请参阅适用于 *Prometheus 的亚马逊托管服务* API 参考[RandomCutForestConfiguration](https://docs.aws.amazon.com/prometheus/latest/APIReference/API_RandomCutForestConfiguration.html)中。

RCF 函数使用此格式：

```
RCF(<query>
[,shingle size
[,sample size
[,ignore near expected from above
[,ignore near expected from below
[,ignore near expected from above ratio
[,ignore near expected from below ratio]]]]])
```

除查询之外的所有参数都是可选的，省略时使用默认值。最小语法为：

```
RCF(<query>)
```

必须使用聚合函数包装查询。要使用特定的可选参数，同时忽略其他可选参数，请在函数中保留空位置：

```
RCF(<query>,,,,,1.0,1.0)
```

此示例仅根据预期值和观测值之间的比率来设置忽略异常检测峰值和下降的比率参数。

## API 请求和响应
<a name="anomaly-detection-query-formatting"></a>

成功的调用返回的格式与 [QueryMetrics API](AMP-APIReference-QueryMetrics.md) 相同。除了原始时间序列外，当有足够的样本可用时，API 还会返回以下新的时间序列：
+ `anomaly_detector_preview:lower_band`：PromQL 表达式结果的预期值的下限
+ `anomaly_detector_preview:score`：异常分数介于 0 和 1 之间，其中 1 表示该数据点的异常可信度较高
+ `anomaly_detector_preview:upper_band`：PromQL 表达式结果的预期值的上限

 **示例请求** 

```
POST /workspaces/workspace-id/anomalydetectors/preview
Content-Type: application/x-www-form-urlencoded

query=RCF%28avg%28vector%28time%28%29%29%29%2C%208%2C%20256%29&start=1735689600&end=1735695000&step=1m
```

 **示例响应** 

```
200 OK
...

{
  "status": "success",
  "data": {
    "result": [
      {
        "metric": {},
        "values": [
          [
            1735689600,
            "1735689600"
          ],
          [
            1735689660,
            "1735689660"
          ],
          .........
        ]
      },
      {
        "metric": {
          "anomaly_detector_preview": "upper_band"
        },
        "values": [
          [
            1735693500,
            "1.7356943E9"
          ],
          [
            1735693560,
            "1.7356945E9"
          ]
          ],
          .........
        ]
      },
      {
        "metric": {
          "anomaly_detector_preview": "lower_band"
        },
        "values": [
          [
            1735693500,
            "1.7356928E9"
          ],
          [
            1735693560,
            "1.7356929E9"
          ],
          ......... 
        ]
      },
      {
        "metric": {
          "anomaly_detector_preview": "score"
        },
        "values": [
          [
            1735693500,
            "0.0"
          ],
          [
            1735695000,
            "0.0"
          ],
          .........
        ]
      }
    ],
    "resultType": "matrix"
  }
}
```