# Important Note #

- `autogen` folder is auto generated, please do not modify files inside `autogen` folder manually.
- Post updating proto IDL file - `kspt.proto`, execute below command to regenerate files inside `autogen` folder.
- While updating `kspt.proto`, also add similar proto file changes in [KeplerStudioPerfomanceExtension](https://code.amazon.com/packages/KeplerStudioPerformanceExtension/trees/mainline) package.

***Command***
```
$ python3 -m grpc_tools.protoc -I<relative_path_of_directory_containing_proto_files> --python_out=<output_dir> --pyi_out=<output_dir> --grpc_python_out=<output_dir> <IDL File name>
```

***Example***
```
vprabal@88665a4a9c13 grpc % pwd
/Users/vprabal/Documents/KSPT/src/VegaUIReact-PerfTests/src/kpi_collector/grpc_internal
vprabal@88665a4a9c13 grpc % python3 -m grpc_tools.protoc -I. --python_out=autogen --pyi_out=autogen --grpc_python_out=autogen kspt.proto
vprabal@88665a4a9c13 grpc % 
```

# Starting gRPC server #

For internal testing purposes, start gRPC server by going to VS Code debugger (CMD + SHIFT + D) and selecting launch configuration - `gRPC Server Activity Monitoring` or `gRPC Server Memory Monitoring`. Post this click on the Green Play button (or F5) to start gRPC server.


# Testing gRPC server with gRPC client.

Once server is started, run kspt_client.py python script to test all RPC calls exposed by the server.
Eg: If server is `gRPC Server Activity Monitoring` then run `kspt_client.py` with argument `--collection_type=activity`. Else use argument `--collection_type=memory` for `gRPC Server Memory Monitoring`.

```
vprabal@88665a4a9c13 kpi_collector % ./kspt_client.py --collection_type=activity
INFO:root:activity start_collection invoked
...
...
INFO:root:stop_trace_recording response received: traceFileLocation: "/Users/vprabal/Documents/KSPT/src/VegaUIReact-PerfTests/traceoutput/2023-07-06_21-00-49/Basic-trace-recorder2023-07-06_21-00-49.json"

INFO:root:activity stop_collection invoked
INFO:root:activity stop_collection response received: 
INFO:root:Collection finished!
```

```
vprabal@88665a4a9c13 kpi_collector % ./kspt_client.py --collection_type=memory
INFO:root:memory start_collection invoked
...
...
INFO:root:stop_trace_recording response received: traceFileLocation: "/Users/vprabal/Documents/KSPT/src/VegaUIReact-PerfTests/traceoutput/2023-07-06_21-00-49/Basic-trace-recorder2023-07-06_21-03-40.json"

INFO:root:memory stop_collection invoked
INFO:root:memory stop_collection response received: 
INFO:root:Collection finished!
```

# Common Issues


### 1. `ModuleNotFoundError No module named 'kspt_pb2'`

In the `kspt_pb2_grpc.py` file replace `import kspt_pb2 as kspt__pb2` with `from . import kspt_pb2 as kspt__pb2` 

### 2. Import Errors

Example import error:

```
 "ImportError: cannot import name 'runtime_version' from 'google.protobuf' (/Users/pandimkp/.kepler_studio/tools/kepler_perf_cli/lib/google/protobuf/__init__.py)\n",
```

This is caused by using wrong library versions of: `python`, `grpc` and `protobuf` for autogenerating files.
To fix this, use the following library versions:
```
    python 3.9
    grpcio 1.49.1
    grpcio-tools 1.49.1
    protobuf 4.25.6
```
