> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 计数与分组

> 将 Attack Surface Query 转化为数字：groupBy、count、countDistinct、sum、min、max、avg、条件聚合、having、时间序列和时区

列表回答*有哪些*；聚合回答*有多少*。在查询中添加 `aggregate` 后，它会按分组每组返回一行，而不是每条记录返回一行。

*每个端口有多少主机开放？*

```json theme={null}
{
  "from": "services",
  "groupBy": ["port"],
  "aggregate": { "hosts": { "fn": "countDistinct", "field": "host.id" } },
  "orderBy": [{ "field": "hosts", "dir": "desc" }],
  "limit": 20
}
```

```json theme={null}
{ "rows": [ { "port": 443, "hosts": 118 }, { "port": 80, "hosts": 69 }, { "port": 8080, "hosts": 33 } ] }
```

## aggregate

一个映射，键为你自定义的名称，值为 `{ "fn", "field"?, "where"? }`。每个查询最多 **8** 个。

| `fn` | 字段 | 结果 |
| - | - | - |
| `count` | 无 | 分组中的行数 |
| `count` | 某个字段 | 该字段有值的行数 |
| `countDistinct` | 某个字段 | 不同值的数量（`host.id` 统计的是主机数而不是服务数） |
| `sum` | 数字 | 总和 |
| `avg` | 数字 | 平均值 |
| `min`、`max` | 数字、时间、文本或枚举 | 最小值 / 最大值 |

聚合名称不能同时作为分组键。

### 条件聚合

为聚合指定它自己的 `where`，它就只统计匹配的行。这样一个查询就能返回整张矩阵，每一列都基于同一组行计算。

*每个主机的未修复发现结果，每个严重级别一列：*

```json theme={null}
{
  "from": "vulnerabilities",
  "groupBy": ["host.value"],
  "where": { "logic": "and", "items": [{ "field": "isOpen", "op": "eq", "value": "true" }] },
  "aggregate": {
    "open": { "fn": "count" },
    "critical": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "critical" }] } },
    "high": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "high" }] } },
    "maxCvss": { "fn": "max", "field": "cvssScore" }
  },
  "orderBy": [{ "field": "critical", "dir": "desc" }]
}
```

```json theme={null}
{ "rows": [
  { "host": { "value": "api.example.com" }, "open": 7, "critical": 2, "high": 3, "maxCvss": 9.8 },
  { "host": { "value": "shop.example.com" }, "open": 4, "critical": 1, "high": 0, "maxCvss": 9.1 }
] }
```

先应用查询的 `where`，再应用每个聚合自己的过滤器。聚合过滤器接受 [`where`](https://docs.oasm.io/zh/attack-surface-query/filtering) 所接受的一切，包括嵌套分组和关系测试。

## groupBy

最多三个分组键。分组键是在[资源和字段](https://docs.oasm.io/zh/attack-surface-query/resources.md)中标记为 *groupable* 的字段，可以是该资源的字段，也可以是关联行的字段：

```json theme={null}
{ "from": "hosts", "groupBy": ["target.value", "kind"], "aggregate": { "n": { "fn": "count" } } }
```

没有 `groupBy` 时，查询返回一行汇总：

```json theme={null}
{ "from": "vulnerabilities",
  "aggregate": { "open": { "fn": "count" }, "maxCvss": { "fn": "max", "field": "cvssScore" } },
  "where": { "logic": "and", "items": [{ "field": "isOpen", "op": "eq", "value": "true" }] } }
```

## having：过滤分组

`where` 在计数之前过滤记录；`having` 在计数之后过滤分组。它的字段是你的分组键和聚合名称，结构与 `where` 相同（分组、`not`）。

*至少有一个严重或高危发现结果的主机：*

```json theme={null}
{
  "from": "vulnerabilities",
  "groupBy": ["host.value"],
  "aggregate": {
    "critical": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "critical" }] } },
    "high": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "high" }] } }
  },
  "having": { "logic": "or", "items": [
    { "field": "critical", "op": "gt", "value": "0" },
    { "field": "high", "op": "gt", "value": "0" }
  ] }
}
```

`having` 对所有键都支持 `eq`、`neq`、`in` 和 `notIn`，对数字和时间还支持 `gt`、`gte`、`lt`、`lte`。计数按数字比较（`"value": "0"`），分组键按其自身类型比较（枚举值、端口……）。

## 时间序列

### 时间桶

时间字段通过时间桶按周期分组：`hour`、`day`、`week` 或 `month`。

```json theme={null}
{
  "window": { "since": "now-30d" },
  "queries": {
    "hostsPerDay": {
      "from": "hosts",
      "groupBy": [{ "field": "firstSeen", "bucket": "day" }],
      "aggregate": { "n": { "fn": "count" } },
      "limit": 31
    }
  }
}
```

没有时间桶的时间字段不能作为分组键。

### 填充空桶

默认情况下，没有数据的那一天根本没有对应的行，这会在图表中留下空洞。添加 `"fill": true` 后，查询会返回**时间窗口内的每一个时间桶**，空桶的值为 `0`：

```json theme={null}
{
  "window": { "since": "now-14d" },
  "queries": {
    "newPortsPerDay": {
      "from": "services",
      "groupBy": [{ "field": "firstSeen", "bucket": "day" }],
      "aggregate": { "n": { "fn": "count" } },
      "fill": true
    }
  }
}
```

```json theme={null}
{ "rows": [
  { "firstSeen": "2026-11-06T00:00:00.000Z", "n": 0 },
  { "firstSeen": "2026-11-07T00:00:00.000Z", "n": 12 },
  { "firstSeen": "2026-11-08T00:00:00.000Z", "n": 0 },
  "…"
] }
```

* `fill` 需要一个 `window`（序列覆盖整个窗口），并且只能有一个分组键，即带时间桶的时间字段。
* 行按时间顺序返回，`limit` 默认为时间桶的数量。
* 在空桶中，`count`、`countDistinct` 和 `sum` 为 `0`；`min`、`max` 和 `avg` 为 `null`。
* 序列必须在行数限制之内：按天统计 14 天没问题，按小时统计一年则不行。请使用更大的时间桶或更短的时间窗口。

### 时区

除非批次另有指定，时间桶按 **UTC** 切分。河内的一天比 UTC 的一天早七个小时开始，因此为当地团队绘制的每日图表应指定其自己的时区：

```json theme={null}
{
  "window": { "since": "now-14d" },
  "options": { "timezone": "Asia/Ho_Chi_Minh" },
  "queries": { "newPortsPerDay": { "...": "..." } }
}
```

`timezone` 接受任意 IANA 名称（`Europe/Paris`、`America/New_York`、`UTC`……），并应用于批次中的所有时间桶。响应中的时间仍为 ISO 8601 时间点：河内 11 月 7 日的时间桶会以 `2026-11-06T17:00:00.000Z` 返回。

## 排序与数量

* `orderBy` 指定一个分组键或聚合名称：`{ "field": "hosts", "dir": "desc" }`。默认按第一个聚合降序排列（填充的序列则按时间顺序）；值相同时按分组键排序。
* `limit` 限制分组的数量（默认 50）。
* 聚合查询不接受 `select`、`include`、`cursor` 或 `withTotal`。

## Rollups：面向大型工作区的即时计数

`rollups` 资源保存 OASM 每 15 分钟根据每个服务最新的 HTTP 响应重新计算的计数。无论工作区多大，读取它们都是即时的。

| `metric` | `key` 是 | `value` 是 |
| - | - | - |
| `port` | 端口号 | 开放该端口的服务数 |
| `technology` | 技术名称 | 运行该技术的服务数 |
| `webserver` | `Server` 响应头 | 返回该响应头的服务数 |
| `statusCode` | HTTP 状态码 | 返回该状态码的服务数 |
| `tlsVersion` | TLS 版本 | 协商该版本的服务数 |

```json theme={null}
{ "from": "rollups", "select": ["key", "value", "computedAt"],
  "where": { "logic": "and", "items": [{ "field": "metric", "op": "eq", "value": "technology" }] },
  "limit": 20 }
```

Rollups 是工作区级别的：不能在项目内读取，并且相对实时数据最多滞后 15 分钟。如需精确或带过滤的计数，请改为对实时资源进行聚合。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.