# API

*注意*：目前 API 没有鉴权，请谨慎调用

Endpoints:
- 生产环境：https://ctd1rbkuai.execute-api.us-west-2.amazonaws.com/prod
- staging 环境：https://sfj4a92oj2.execute-api.us-west-2.amazonaws.com/staging

### 任务列表

```
GET /tasks
```

#### example

```
curl ${endpoint}/tasks | jq
```

```
[
  "log-diag-summary",
  "metrics-diag",
  "test",
  "log-diag-prod"
]
```

### 触发任务（同步）

```
POST /tasks/{task}/sync

{
  "args": {
    ...
  },
  "tags": {
    ...
  }
}
```

- task: 任务名，如 "log-diag-summary"
- args: 任务参数，内容取决于具体任务
- tags: 任务 tag，可用于后续执行结果的搜索

#### example

```
curl -d '{"args": {"cluster": "1311285575352848384", "start_ts": 1654580000, "stop_ts": 1654590000}}' ${endpoint}/tasks/log-diag-summary/sync | jq
```

```
{
  "id": "1a77899f-a225-4562-b351-b3115c8304d5",
  "data": {
    "abnormal": true,
    "symptoms": [
      {
        "advices": [
          "可以通过查看 TiDB 对应 Region 的 switch region peer to next due to send request fail 日志，排查发送失败根本原因",
          "或根据 https://docs.pingcap.com/zh/tidb/stable/tidb-troubleshooting-map 排查是否出现了 TiKV 丢 leader 的情况"
        ],
        "category": "performance",
        "component": "tikv",
        "conditions": [
          "peer is not leader"
        ],
        "count": 35266,
        "name": "请求发到了非 Leader 的副本上",
        "start_ts": 1654579980,
        "stop_ts": 1654589940
      }
    ]
  }
}
```

- id: task 运行实例 ID
- data: task 执行返回的结果

### 触发任务（异步）

```
POST /tasks/{task}/async

{
  "args": {
    ...
  },
  "tags": {
    ...
  }
}
```

- task: 任务名，如 "log-diag-summary"
- args: 任务参数，内容取决于具体任务
- tags: 任务 tag，可用于后续执行结果的搜索

#### example

```
curl -d '{"args": {"cluster": "1311285575352848384", "start_ts": 1654580000, "stop_ts": 1654590000}}' ${endpoint}/tasks/log-diag-summary/async | jq
```

```
{
  "id": "1a77899f-a225-4562-b351-b3115c8304d5"
}
```

- id: 任务执行 ID，用于获取结果

### 获取异步任务执行结果

```
GET /instances/{id}
```

- id: 任务执行 ID

#### example

```
curl ${endpoint}/instances/1a77899f-a225-4562-b351-b3115c8304d5
```

```
{
  "id": "1a77899f-a225-4562-b351-b3115c8304d5",
  "name": "log-diag-summary",
  "status": "success",
  "tags": {},
  "args": {
    "cluster": "1311285575352848384",
    "start_ts": 1654580000,
    "stop_ts": 1654590000
  },
  "data": {
    "abnormal": true,
    "symptoms": [
      {
        "advices": [
          "可以通过查看 TiDB 对应 Region 的 switch region peer to next due to send request fail 日志，排查发送失败根本原因",
          "或根据 https://docs.pingcap.com/zh/tidb/stable/tidb-troubleshooting-map 排查是否出现了 TiKV 丢 leader 的情况"
        ],
        "category": "performance",
        "component": "tikv",
        "conditions": [
          "peer is not leader"
        ],
        "count": 35266,
        "name": "请求发到了非 Leader 的副本上",
        "start_ts": 1654579980,
        "stop_ts": 1654589940
      }
    ]
  },
  "created_at": "2022-06-13T07:46:58Z",
  "updated_at": "2022-06-13T07:47:22Z"
}
```

- id: 任务执行 ID，一定和参数中的 ID 相同
- name: task 名字
- status: 执行结果（success|failed|running)
- tags: 触发任务时传的 tags 参数
- args: 触发任务时传的 args 参数
- data: 任务执行返回的数据
- created_at: 任务创建时间
- updated_at: 任务最后更新时间

### 任务执行列表

```
POST /tasks/{task}?status={status}&begin={begin}&end={end}

{
  "tags": {...},
  "args": {...},
  "data": {...}
}
```

- task: 任务名
- status: 任务执行状态 (running|success|failed)，默认所有状态
- begin, end: 任务创建的时间范围，默认当前一小时内
- tags: 按照 tags 筛选任务
- args: 按照 args 筛选任务
- data: 按照任务返回的 data 筛选任务

#### example

```
curl -d '{"args": {"cluster": "1379661944610434065"}}' "${endpoint}/tasks/log-diag-prod?begin=1655115000&end=1655115060" | jq
```

```
[
  {
    "id": "b8f3ee4f-c3c2-441e-9f14-d356d3f8d4b2",
    "name": "log-diag-prod",
    "status": "success",
    "tags": {
      "source": "auto"
    },
    "args": {
      "cluster": "1379661944610434065",
      "start_ts": 1655114940,
      "stop_ts": 1655115000
    },
    "data": {
      "abnormal": false,
      "symptoms": {}
    },
    "created_at": "2022-06-13T10:10:24Z",
    "updated_at": "2022-06-13T10:10:25Z"
  }
]
```

- 返回任务执行实例的列表

### 重试任务（同步）

```
PUT /instances/{id}/sync
```

- id: 任务运行实例 id

#### example

```
curl -X PUT ${endpoint}/instances/b8f3ee4f-c3c2-441e-9f14-d356d3f8d4b2/sync | jq
```

```
{
  "id": "b8f3ee4f-c3c2-441e-9f14-d356d3f8d4b2",
  "data": {
    "abnormal": false,
    "symptoms": {}
  }
}
```

- id: 和参数重的 id 相同
- data: 任务返回的数据

### 重试任务（异步）

```
PUT /instances/{id}/async
```

- id: 任务运行实例 id

#### example

```
curl -X PUT ${endpoint}/instances/b8f3ee4f-c3c2-441e-9f14-d356d3f8d4b2/async | jq
```

```
{
  "id": "b8f3ee4f-c3c2-441e-9f14-d356d3f8d4b2",
}
```

- id: 和参数重的 id 相同
