Skip to content

Commit 784012a

Browse files
committed
[GTI-129] Add unit tests and black formatter
1 parent bc0f0f5 commit 784012a

8 files changed

Lines changed: 1196 additions & 526 deletions

File tree

.github/workflows/pr-checks.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: PR Checks
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
7+
jobs:
8+
format-check:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Set up Python
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: '3.11'
18+
19+
- name: Install Black
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install black
23+
24+
- name: Check code formatting with Black
25+
run: |
26+
black --check *.py
27+
28+
test:
29+
runs-on: ubuntu-latest
30+
strategy:
31+
matrix:
32+
python-version: [3.9, "3.10", "3.11", "3.12", "3.13"]
33+
34+
steps:
35+
- uses: actions/checkout@v4
36+
37+
- name: Set up Python ${{ matrix.python-version }}
38+
uses: actions/setup-python@v4
39+
with:
40+
python-version: ${{ matrix.python-version }}
41+
42+
- name: Install dependencies
43+
run: |
44+
python -m pip install --upgrade pip
45+
pip install -r requirements.txt
46+
47+
- name: Run tests
48+
run: |
49+
pytest test_msstats.py -v

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,7 @@ config.ini
135135
# Output files
136136
*.xlsx
137137
*.csv
138-
*.json
138+
*.json
139+
140+
# Claude
141+
.claude

CLAUDE.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
MSStats is a Python tool for extracting Google Cloud MemoryStore (Redis) database metrics using the Google Cloud Monitoring API. It processes Redis databases (single instance and replicated) across multiple GCP service accounts and generates Excel reports with usage statistics.
8+
9+
## Architecture
10+
11+
- **Single Python script**: `msstats.py` - Main application that processes Redis metrics
12+
- **Batch processing**: Shell scripts for bulk operations across multiple GCP projects
13+
- **Service account management**: Scripts to grant/revoke monitoring permissions
14+
- **Output format**: Excel files with detailed Redis command statistics and throughput data
15+
16+
## Key Components
17+
18+
- **Metric Processing**: Categorizes Redis commands by type (Get, Set, Hash, List, etc.) from monitoring data
19+
- **Multi-node handling**: Aggregates metrics across Redis cluster nodes taking maximum values
20+
- **Time series data**: Configurable duration (default 7 days) and step intervals (default 60s)
21+
- **GCP Integration**: Uses Google Cloud Monitoring API with service account authentication
22+
23+
## Development Commands
24+
25+
### Setup Environment
26+
```bash
27+
# Create and activate virtual environment
28+
python -m venv .env && source .env/bin/activate
29+
30+
# Install dependencies
31+
pip install -r requirements.txt
32+
33+
# Place service account JSON files in root directory
34+
cp path/to/service_account.json .
35+
```
36+
37+
### Running the Tool
38+
```bash
39+
# Basic usage (7 days, 60s steps)
40+
python msstats.py
41+
42+
# Custom duration and step size
43+
python msstats.py --duration 1800 --step 300
44+
45+
# Specific project
46+
python msstats.py -p project-id
47+
```
48+
49+
### Batch Operations
50+
```bash
51+
# Grant monitoring permissions to service account
52+
./grant_sa_monitoring_viewer.sh service-account@project.iam.gserviceaccount.com
53+
54+
# Run across all accessible projects
55+
./batch_run_msstats.sh
56+
57+
# Remove monitoring permissions
58+
./remove_sa_monitoring_viewer.sh service-account@project.iam.gserviceaccount.com
59+
60+
# Clean up environment
61+
deactivate
62+
```
63+
64+
## Command Line Options
65+
66+
- `--duration SECONDS`: Time period to analyze (default: 604800 = 7 days)
67+
- `--step SECONDS`: Metric sampling interval (default: 60 seconds)
68+
- `-p PROJECT_ID`: Target specific GCP project
69+
70+
### Testing and Code Quality
71+
```bash
72+
# Run all tests (unit and integration)
73+
pytest test_msstats.py
74+
75+
# Format code with Black
76+
black *.py
77+
78+
# Check formatting without making changes
79+
black --check *.py
80+
```
81+
82+
## Dependencies
83+
84+
- `openpyxl>=3.0.4`: Excel file generation
85+
- `google-cloud-monitoring==2.18.0`: GCP Monitoring API client
86+
- `black>=25.1.0`: Code formatter
87+
- `pytest>=8.4.0`: Testing framework
88+
- `pytest-mock>=3.14.0`: Mock utilities for testing
89+
- Python 3.9+ required
90+
91+
## Security Notes
92+
93+
- Service account JSON files must be placed in repository root
94+
- Tool uses read-only monitoring API access
95+
- Never connects directly to Redis instances
96+
- No impact on database performance or data

CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# These owners will be the default owners for everything in
22
# the repo. Unless a later match takes precedence will be requested
33
# for review when someone opens a pull request.
4-
* @thomasefthymiou78 @erniavr @mar1boroman @ewpreston @gmflau
4+
* @ajGingrich @kurtfm @sagile @thomasefthymiou78
55

66
# Order is important; the last matching pattern takes the most
77
# precedence. When someone opens a pull request that only

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MSSTATS
22

3-
MSStats is a tool for extracting MemoryStore database metrics. The script is able to process all the Redis databases, both single instance and replicated (Basic or Standard) ones that belong to a specific service account. Multiple service accounts can be used at once.
3+
MSStats is a tool for extracting MemoryStore database metrics. The script is able to process all the Redis databases, both single instance and replicated (Basic or Standard) ones that belong to a specific service account. Multiple service accounts can be used at once.
44

55
The script will purely use google cloud monitoring api for getting the metrics. It will never connect to the Redis databases and it will NOT send any commands to the databases.
66

@@ -42,24 +42,24 @@ Copy your service account .json files in the root directory of the project:
4242
cp path/to/service_account.json .
4343
```
4444

45-
Execute
45+
Execute
4646

4747
```
4848
python msstats.py
4949
```
5050

5151
This generates a file named <your project>.xlsx. You need to get that file and send it to Redis.
5252
By default, it uses steps of 60 seconds and a period of 7 days (604800 seconds).
53-
You can set different values as follows :
53+
You can set different values as follows:
5454

5555
````
5656
python msstats.py --duration 1800 --step 300
5757
````
5858

59-
This can help solving issue like :
59+
This can help solving issue like:
6060

6161
```
62-
google.api_core.exceptions.ResourceExhausted: 429 Maximum response size of 200000000 bytes reached.
62+
google.api_core.exceptions.ResourceExhausted: 429 Maximum response size of 200000000 bytes reached.
6363
Consider querying less data by increasing the step or interval, using more filters and aggregations, or limiting the time duration.
6464
```
6565

@@ -87,8 +87,8 @@ Execute
8787
```
8888
./batch_run_msstats.sh
8989
```
90-
91-
Remove monitoring.viewer role from the service account in all associated Google Cloud projects
90+
91+
Remove monitoring.viewer role from the service account in all associated Google Cloud projects
9292

9393
```
9494
./remove_sa_monitoring_viewer.sh <service_account>
@@ -98,6 +98,18 @@ For example,
9898
./remove_sa_monitoring_viewer.sh gmflau-sa@gcp-dev-day-nyc.iam.gserviceaccount.com
9999
```
100100

101+
## Testing and Code Quality
102+
103+
Run all tests (unit and integration):
104+
```
105+
pytest test_msstats.py
106+
```
107+
108+
Format code:
109+
```
110+
black *.py
111+
```
112+
101113
When finished do not forget to deactivate the virtual environment
102114

103115
```

0 commit comments

Comments
 (0)