# Clinic

## Overview
clinic server receive runtime data of tidb cluster and rebuild the monitoring info for researching oncall issue.
The design doc is [here](https://docs.google.com/document/d/1o9wnII1pWrkBUludvbxLzQ-WAHFtEBw85MsdEWrSeMw/edit) 

## Development

Run the following command and fix all lint errors before submitting your code:

```bash
# make check test build
make
```

If you want to avoid time-consuming tests and builds in fast local development, you can run:

```bash
make check
```

## Usage

### Server

```bash
make server

bin/server -c=pkg/config/config_example.yaml
```

### Ngm Api Server

1. Start TiDB with TiUP

    ```bash
    tiup playground
    ```

2. Setup a port-forward to dbaas core-svc

    ```bash
    kubectl --context arn:aws:eks:us-west-2:385595570414:cluster/dev-base-us-west-2 port-forward -n nightly-ms svc/dbaas-core-svc-server-grpc-clinic 8888:8888
    ```

3. Start ngm api server
    ```bash
    make ngm_api
    
    bin/ngm_api -c=pkg/ngm/config/config_example.yaml
    ```

### Cli

```bash
make cli
bin/clinic --help
clinic is a command-line client for clinic

Usage:
  clinic [flags]
  clinic [command]

Available Commands:
  completion  generate the autocompletion script for the specified shell
  download    download file
  help        Help about any command
  history     show upload history
  upload      upload a file

Flags:
      --endpoint string   the clinic service endpoint, not required.
  -h, --help              help for clinic

Use "clinic [command] --help" for more information about a command.
```

It suppose the network is bottleneck, so cli will use multi goroutine to upload/download file.
Please refer [http header](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Range)

#### Upload file

- Send `PreCreate` request to server to initialize the file
- Parallel send to `uploadPart` requests by cutting the file with fixed block size
- Send `flush` request to server to indicate the file is completed
- Merge the parts to a file

#### Download file

- Get `MetaData` of file
- Get part of file by `Range` of http header
- Download each part by multi goroutine
- Merge the each part to a file

## Decompress the download file

If you download file successfully, there will be a file which has a `.zst` suffix.

- Install zstd

```bash
bash -c "$(curl -fsSL https://raw.githubusercontent.com/horta/zstd.install/main/install)"
```

- Decompress the file
```bash
zstd -d $DownloadFile
tar xvf $DecompressFile
```
