6.8 KiB
6.8 KiB
TJWater Server - Copilot Instructions
This is a FastAPI-based water network management system (供水管网智能管理系统) that provides hydraulic simulation, SCADA data integration, network element management, and risk analysis capabilities.
Running the Server
# Install dependencies
pip install -r requirements.txt
# Start the server (default: http://0.0.0.0:8000 with 2 workers)
python scripts/run_server.py
# Note: On Windows, the script automatically sets WindowsSelectorEventLoopPolicy
Running Tests
# Run all tests
pytest
# Run a specific test file with verbose output
pytest tests/unit/test_pipeline_health_analyzer.py -v
# Run from conftest helper
python tests/conftest.py
Architecture Overview
Core Components
-
Native Modules (
app/native/): Platform-specific compiled extensions (.sofor Linux,.pydfor Windows) providing performance-critical functionality including:- SCADA device integration
- Water distribution analysis (WDA)
- Pipe risk probability calculations
- Wrapped through
app.services.tjnetworkinterface
-
Services Layer (
app/services/):tjnetwork.py: Main network API wrapper around native modulessimulation.py: Hydraulic simulation orchestration (EPANET integration)project_info.py: Project configuration managementepanet/: EPANET hydraulic engine integration
-
API Layer (
app/api/v1/):- Network Elements: Separate endpoint modules for junctions, reservoirs, tanks, pipes, pumps, valves
- Components: Curves, patterns, controls, options, quality, visuals
- Network Features: Tags, demands, geometry, regions/DMAs
- Core Services: Auth, project, simulation, SCADA, data query, snapshots
-
Database Infrastructure (
app/infra/db/):- PostgreSQL: Primary relational database (users, audit logs, project metadata)
- TimescaleDB: Time-series extension for historical data
- InfluxDB: Optional time-series database for high-frequency SCADA data
- Connection pools initialized in
main.pylifespan context - Database instance stored in
app.state.dbfor dependency injection
-
Domain Layer (
app/domain/):models/: Enums and domain objects (e.g.,UserRole)schemas/: Pydantic models for request/response validation
-
Algorithms (
app/algorithms/):api_ex/: Analysis algorithms (k-means sensor placement, sensitivity analysis, pipeline health)data_cleaning.py: Data preprocessing utilitiessimulations.py: Simulation helpers
Security & Authentication
- Authentication: JWT-based with access tokens (30 min) and refresh tokens (7 days)
- Authorization: Role-based access control (RBAC) with 4 roles:
VIEWER: Read-only accessUSER: Read-write accessOPERATOR: Modify dataADMIN: Full permissions
- Audit Logging:
AuditMiddlewareautomatically logs POST/PUT/DELETE requests - Encryption: Fernet symmetric encryption for sensitive data (
app.core.encryption)
Default admin accounts:
admin/admin123tjwater/tjwater@123
Key Files
app/main.py: FastAPI app initialization, lifespan (DB pools), CORS, middleware, router mountingapp/api/v1/router.py: Central router aggregating all endpoint modulesapp/core/config.py: Settings management usingpydantic-settingsapp/auth/dependencies.py: Auth dependencies (get_current_active_user,get_db)app/auth/permissions.py: Permission decorators (require_role,get_current_admin).env: Environment configuration (database credentials, JWT secret, encryption key)
Important Conventions
Database Connections
- Database instances are initialized in
main.pylifespan and stored inapp.state.db - Access via dependency injection:
from app.auth.dependencies import get_db async def endpoint(db = Depends(get_db)): # Use db connection
Authentication in Endpoints
Use dependency injection for auth requirements:
from app.auth.dependencies import get_current_active_user
from app.auth.permissions import require_role, get_current_admin
from app.domain.models.role import UserRole
# Require any authenticated user
@router.get("/data")
async def get_data(current_user = Depends(get_current_active_user)):
return data
# Require specific role (USER or higher)
@router.post("/data")
async def create_data(current_user = Depends(require_role(UserRole.USER))):
return result
# Admin-only access
@router.delete("/data/{id}")
async def delete_data(id: int, current_user = Depends(get_current_admin)):
return result
API Routing Structure
- All v1 APIs are mounted under
/api/v1prefix viaapi_router - Legacy routes without version prefix are also mounted for backward compatibility
- Group related endpoints in separate router modules under
app/api/v1/endpoints/ - Use descriptive tags in
router.pyfor OpenAPI documentation grouping
Native Module Integration
- Native modules are pre-compiled for specific platforms
- Always import through
app.native.apiorapp.services.tjnetwork - The
tjnetworkservice wraps native APIs with constants like:- Element types:
JUNCTION,RESERVOIR,TANK,PIPE,PUMP,VALVE - Operations:
API_ADD,API_UPDATE,API_DELETE ChangeSetfor batch operations
- Element types:
Project Initialization
- On startup,
main.pyautomatically loads project fromproject_info.nameif set - Projects are opened via
open_project(name)fromtjnetworkservice
Audit Logging
Manual audit logging for critical operations:
from app.core.audit import log_audit_event, AuditAction
await log_audit_event(
action=AuditAction.UPDATE,
user_id=current_user.id,
username=current_user.username,
resource_type="resource_name",
resource_id=str(resource_id),
ip_address=request.client.host,
request_data=data
)
Environment Configuration
- Copy
.env.exampleto.envbefore first run - Required environment variables:
SECRET_KEY: JWT signing (generate withopenssl rand -hex 32)ENCRYPTION_KEY: Data encryption (generate with Fernet)- Database credentials for PostgreSQL, TimescaleDB, and optionally InfluxDB
Database Migrations
SQL migration scripts are in migrations/:
001_create_users_table.sql: User authentication tables002_create_audit_logs_table.sql: Audit logging tables
Apply with:
psql -U postgres -d tjwater -f migrations/001_create_users_table.sql
API Documentation
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI schema: http://localhost:8000/openapi.json
Additional Resources
SECURITY_README.md: Comprehensive security feature documentationDEPLOYMENT.md: Integration guide for security featuresreadme.md: Project overview and directory structure (in Chinese)