|
| 1 | +# Contributing to API Server |
| 2 | + |
| 3 | +> [!Note] |
| 4 | +> This is the API server component of the peermetrics WebRTC monitoring service. For the full project, see [peermetrics](https://github.com/peermetrics/peermetrics). |
| 5 | +
|
| 6 | +We welcome contributions to the API server! This guide will help you set up your development environment and understand our contribution workflow. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +Before you begin, ensure you have the following installed: |
| 11 | + |
| 12 | +- Python 3.8 |
| 13 | +- PostgreSQL |
| 14 | +- Redis |
| 15 | +- pip |
| 16 | +- virtualenv (recommended) |
| 17 | +- Git |
| 18 | + |
| 19 | +## Local Development Setup |
| 20 | + |
| 21 | +### 1. Clone the Repository |
| 22 | + |
| 23 | +```bash |
| 24 | +git clone https://github.com/peermetrics/api-server.git |
| 25 | +cd api-server |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. Set Up Python Virtual Environment |
| 29 | + |
| 30 | +```bash |
| 31 | +python3 -m venv venv |
| 32 | +source venv/bin/activate # On Windows: venv\Scripts\activate |
| 33 | +``` |
| 34 | + |
| 35 | +### 3. Install Dependencies |
| 36 | + |
| 37 | +```bash |
| 38 | +pip install -r requirements.txt |
| 39 | +``` |
| 40 | + |
| 41 | +### 4. Configure Environment Variables |
| 42 | + |
| 43 | +Copy the `.env` file and update the values as needed: |
| 44 | + |
| 45 | +```bash |
| 46 | +cp .env .env.local |
| 47 | +``` |
| 48 | + |
| 49 | +Key environment variables to configure: |
| 50 | + |
| 51 | +- `DEBUG`: Set to `True` for development |
| 52 | +- `SECRET_KEY`: Django secret key |
| 53 | +- `INIT_TOKEN_SECRET`: Used to generate JWT tokens |
| 54 | +- `SESSION_TOKEN_SECRET`: Used to encrypt session cookies |
| 55 | +- `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USER`, `DATABASE_PASSWORD`, `DATABASE_NAME`: PostgreSQL connection details |
| 56 | +- `REDIS_HOST`: Redis server location |
| 57 | +- `WEB_DOMAIN`: Domain for the web interface (default: `localhost:8080`) |
| 58 | + |
| 59 | +### 5. Set Up the Database |
| 60 | + |
| 61 | +Run Django migrations to create the database schema: |
| 62 | + |
| 63 | +```bash |
| 64 | +python manage.py migrate |
| 65 | +``` |
| 66 | + |
| 67 | +### 6. Create a Superuser (Optional) |
| 68 | + |
| 69 | +To access the Django admin interface: |
| 70 | + |
| 71 | +```bash |
| 72 | +python manage.py createsuperuser |
| 73 | +``` |
| 74 | + |
| 75 | +### 7. Populate Test Data |
| 76 | + |
| 77 | +Use the `populate_db.py` script to generate test data: |
| 78 | + |
| 79 | +```bash |
| 80 | +# Generate data for the last 7 days |
| 81 | +python populate_db.py 7 |
| 82 | + |
| 83 | +# Clean all test data |
| 84 | +python populate_db.py clean |
| 85 | +``` |
| 86 | + |
| 87 | +### 8. Run the Development Server |
| 88 | + |
| 89 | +```bash |
| 90 | +python manage.py runserver |
| 91 | +``` |
| 92 | + |
| 93 | +The API will be available at `http://localhost:8000`. |
| 94 | + |
| 95 | +## Project Structure |
| 96 | + |
| 97 | +``` |
| 98 | +api-server/ |
| 99 | +├── api/ # Django project settings |
| 100 | +│ ├── settings.py # Main settings file |
| 101 | +│ └── urls.py # Root URL configuration |
| 102 | +├── app/ # Main application |
| 103 | +│ ├── models/ # Database models |
| 104 | +│ ├── views/ # API views/endpoints |
| 105 | +│ ├── middleware.py # Custom middleware |
| 106 | +│ └── decorators.py # Custom decorators |
| 107 | +├── static/ # Static files |
| 108 | +├── config_populate_db/ # Test data configuration |
| 109 | +├── manage.py # Django management script |
| 110 | +├── populate_db.py # Script to populate test data |
| 111 | +└── requirements.txt # Python dependencies |
| 112 | +``` |
| 113 | + |
| 114 | +### Key Files |
| 115 | + |
| 116 | +- **manage.py**: Standard Django management script for running commands |
| 117 | +- **populate_db.py**: Generates realistic test data for development |
| 118 | +- **config_populate_db**: Configuration files for test data generation |
| 119 | + |
| 120 | +### Models |
| 121 | + |
| 122 | +The main models are located in `app/models/`: |
| 123 | + |
| 124 | +- **Organization**: Groups multiple apps |
| 125 | +- **App**: Groups conferences |
| 126 | +- **Conference**: Represents a call between participants |
| 127 | +- **Participant**: A user participating in conferences |
| 128 | +- **Session**: A participant's presence in a conference |
| 129 | +- **GenericEvent**: Events collected during calls |
| 130 | + |
| 131 | +### Views |
| 132 | + |
| 133 | +API endpoints are organized in `app/views/` and split into: |
| 134 | + |
| 135 | +- **Public routes**: Used by the SDK (e.g., `/initialize`, `/events/*`, `/stats`) |
| 136 | +- **Private routes**: Used for data queries (e.g., `/sessions`, `/conferences`, `/participants`) |
| 137 | + |
| 138 | +## Development Workflow |
| 139 | + |
| 140 | +### 1. Create a New Branch |
| 141 | + |
| 142 | +```bash |
| 143 | +git checkout -b feature/your-feature-name |
| 144 | +``` |
| 145 | + |
| 146 | +Use descriptive branch names: |
| 147 | +- `feature/` for new features |
| 148 | +- `fix/` for bug fixes |
| 149 | +- `docs/` for documentation changes |
| 150 | + |
| 151 | +### 2. Make Your Changes |
| 152 | + |
| 153 | +Follow these guidelines: |
| 154 | + |
| 155 | +- Write clear, self-documenting code |
| 156 | +- Add comments for complex logic |
| 157 | +- Keep functions focused and small |
| 158 | +- Follow Django best practices |
| 159 | + |
| 160 | +### 3. Test Your Changes |
| 161 | + |
| 162 | +Run the development server and test your changes manually: |
| 163 | + |
| 164 | +```bash |
| 165 | +python manage.py runserver |
| 166 | +``` |
| 167 | + |
| 168 | +Use the Django admin interface at `http://localhost:8000/admin` to inspect data. |
| 169 | + |
| 170 | +### 4. Commit Your Changes |
| 171 | + |
| 172 | +Write clear commit messages: |
| 173 | + |
| 174 | +```bash |
| 175 | +git add . |
| 176 | +git commit -m "Add feature: brief description of changes" |
| 177 | +``` |
| 178 | + |
| 179 | +Good commit message format: |
| 180 | +``` |
| 181 | +Add feature: allow filtering conferences by date range |
| 182 | +
|
| 183 | +- Added date_from and date_to query parameters |
| 184 | +- Updated Conference view to handle date filtering |
| 185 | +- Added validation for date format |
| 186 | +``` |
| 187 | + |
| 188 | +## Django Admin Interface |
| 189 | + |
| 190 | +Access the admin interface at `http://localhost:8000/admin` to: |
| 191 | + |
| 192 | +- View and edit raw data |
| 193 | +- Inspect model relationships |
| 194 | +- Debug data issues |
| 195 | +- Manually create test records |
| 196 | + |
| 197 | +Login with the superuser credentials you created during setup. |
| 198 | + |
| 199 | +## API Development Guidelines |
| 200 | + |
| 201 | +### Adding New Endpoints |
| 202 | + |
| 203 | +1. Define the view in `app/views/` |
| 204 | +2. Add the URL pattern in `api/urls.py` |
| 205 | +3. Implement authentication/authorization if needed |
| 206 | +4. Test the endpoint manually |
| 207 | + |
| 208 | +### Model Changes |
| 209 | + |
| 210 | +When modifying models: |
| 211 | + |
| 212 | +```bash |
| 213 | +# Create migrations |
| 214 | +python manage.py makemigrations |
| 215 | + |
| 216 | +# Apply migrations |
| 217 | +python manage.py migrate |
| 218 | +``` |
| 219 | + |
| 220 | +Always review the generated migration files before committing. |
| 221 | + |
| 222 | +### Authentication Patterns |
| 223 | + |
| 224 | +- Public endpoints use JWT tokens generated via `/initialize` |
| 225 | +- Private endpoints require authentication (check existing views for patterns) |
| 226 | +- Use the decorators in `app/decorators.py` for common auth patterns |
| 227 | + |
| 228 | +### Error Handling |
| 229 | + |
| 230 | +- Return appropriate HTTP status codes |
| 231 | +- Provide clear error messages in the response |
| 232 | +- Log errors for debugging |
| 233 | +- Follow the error handling patterns in existing views |
| 234 | + |
| 235 | +## Submitting Changes |
| 236 | + |
| 237 | +### 1. Push Your Branch |
| 238 | + |
| 239 | +```bash |
| 240 | +git push origin feature/your-feature-name |
| 241 | +``` |
| 242 | + |
| 243 | +### 2. Create a Pull Request |
| 244 | + |
| 245 | +- Provide a clear title and description |
| 246 | +- Reference any related issues |
| 247 | +- Explain what changes were made and why |
| 248 | +- Include any testing steps |
| 249 | + |
| 250 | +### PR Description Template |
| 251 | + |
| 252 | +```markdown |
| 253 | +## Description |
| 254 | +Brief description of changes |
| 255 | + |
| 256 | +## Changes Made |
| 257 | +- List of specific changes |
| 258 | +- Another change |
| 259 | + |
| 260 | +## Testing |
| 261 | +How to test these changes |
| 262 | + |
| 263 | +## Related Issues |
| 264 | +Fixes #123 |
| 265 | +``` |
| 266 | + |
| 267 | +### 3. Code Review Process |
| 268 | + |
| 269 | +- A maintainer will review your PR |
| 270 | +- Address any feedback or requested changes |
| 271 | +- Once approved, your PR will be merged |
| 272 | + |
| 273 | +### CI/CD Checks |
| 274 | + |
| 275 | +Pull requests trigger automated workflows (see `.github/workflows/`): |
| 276 | +- Docker image builds |
| 277 | +- Code quality checks |
| 278 | + |
| 279 | +Ensure all checks pass before requesting review. |
| 280 | + |
| 281 | +## Getting Help |
| 282 | + |
| 283 | +- **Issues**: Report bugs or request features via [GitHub Issues](https://github.com/peermetrics/api-server/issues) |
| 284 | +- **Discussions**: Ask questions in the main [peermetrics repository](https://github.com/peermetrics/peermetrics) |
| 285 | +- **Documentation**: Check the [README](README.md) for project overview |
| 286 | + |
| 287 | +## Code Style |
| 288 | + |
| 289 | +- Follow PEP 8 guidelines for Python code |
| 290 | +- Use meaningful variable and function names |
| 291 | +- Keep line length reasonable (under 100 characters when possible) |
| 292 | +- Add docstrings to functions and classes |
| 293 | + |
| 294 | +## Additional Notes |
| 295 | + |
| 296 | +### Working with populate_db.py |
| 297 | + |
| 298 | +The `populate_db.py` script is useful for: |
| 299 | +- Generating realistic test data |
| 300 | +- Testing with different data volumes |
| 301 | +- Simulating various conference scenarios |
| 302 | + |
| 303 | +You can customize the test data by modifying files in `config_populate_db/`. |
| 304 | + |
| 305 | +### Database Tips |
| 306 | + |
| 307 | +- Use `python manage.py dbshell` to access the PostgreSQL shell |
| 308 | +- Run `python manage.py showmigrations` to see migration status |
| 309 | +- Use `python manage.py sqlmigrate app_name migration_name` to view SQL for a migration |
| 310 | + |
| 311 | +Thank you for contributing to peermetrics API server! |
0 commit comments