Author SHA1 Message Date
jiang 18253f2fe0 docs: 编写客户版交付文档 2026-07-22 11:26:06 +08:00
jiang 02a8686222 Stop baking .env into Docker images 2026-07-21 11:42:17 +08:00
jiang 50d823ca58 fix(db): validate cached project connections 2026-07-21 11:35:18 +08:00
jiang 9f225374de fix(server): remove influxdb remnants 2026-07-17 18:33:38 +08:00
jiang 9b16e4e0a5 fix(simulation): use current user for stored schemes 2026-07-17 16:49:20 +08:00
jiang 810a39a1dc build(server): update customer compile image 2026-07-17 16:38:11 +08:00
jiang e49d21cd1b fix(scada): use project-scoped metadata 2026-07-17 16:28:46 +08:00
jiang 0a2ce81753 fix(leakage): accept display flow units 2026-07-17 11:49:00 +08:00
jiang b8f2f70013 fix(simulation): use report step for schemes 2026-07-16 15:47:26 +08:00
jiang 6f9c94a4dd fix(api): include simulation burst ids 2026-07-16 14:50:22 +08:00
jiang 9f7f5536d7 fix(simulation): use hydraulic timestep 2026-07-16 14:16:06 +08:00
jiang d33619d0d5 fix(burst-location): correct normal data window 2026-07-09 11:51:40 +08:00
jiang 6a16ea44b2 fix(burst-location): tolerate partial SCADA gaps 2026-07-09 10:49:53 +08:00
jiang e862e6c500 ci: unzip health model in image build 2026-07-08 19:02:54 +08:00
jiang 87824f3b3c fix(burst-location): use correct data sources
Simulation mode reads scheme data for both burst and normal observations. Monitoring mode reuses the burst window when no normal window is provided.
2026-07-08 17:51:08 +08:00
jiang 18943314f8 fix(burst-location): normalize simulation ids 2026-07-08 17:17:41 +08:00
jiang c7947a7481 feat(auth)!: migrate customer metadata auth
Remove local auth and user-management endpoints in favor of Keycloak-backed metadata users, project context, admin metadata APIs, and agent auth context.
2026-06-13 15:00:58 +08:00
jiang 4b02118286 refactor(metadata): drop geoserver config refs 2026-06-13 14:58:53 +08:00
jiang dc4e3de85e refactor(api): add kebab-case legacy aliases 2026-06-13 13:07:16 +08:00
jiang bb5d339dbe 删除不再需要的构建和打包工作流文件 2026-06-12 15:28:49 +08:00
jiang 33615cdcfc fix(services): lighten package initialization 2026-06-11 11:43:56 +08:00
jiang d6dda51008 fix(sync): align customer backend behavior 2026-06-11 11:41:24 +08:00
jiang 60c8fc4948 fix(scada): align native device writes 2026-06-11 11:20:25 +08:00
jiang 3de620a2ae fix(db): refresh project connection caches 2026-06-11 11:19:20 +08:00
jiang 8e07d580af fix(region): align service area endpoints 2026-06-11 11:18:09 +08:00
jiang 9be0cd34cf fix(auth): stabilize audit and token timing 2026-06-11 11:17:11 +08:00
jiang f61be3685f fix(metadata): sync project metadata endpoints 2026-06-11 11:15:59 +08:00
jiang 78af7ecfb3 fix(native): sync region dma service area 2026-06-11 10:31:08 +08:00
jiang bf8e4fb040 fix(algorithms): sync pressure and sensor logic 2026-06-11 10:29:52 +08:00
jiang 67ba9c2ac6 fix(simulation): align epanet output handling 2026-06-11 10:29:04 +08:00
jiang 21929b44fc fix(timeseries): align timezone and scada updates 2026-06-11 10:27:51 +08:00
jiang 24169bd277 fix(docker): align customer image build 2026-06-11 10:26:36 +08:00
jiang a787327ca2 feat(api): add web search and geocoding 2026-06-11 10:25:45 +08:00
jiang c676b55b70 fix(api): disable legacy unversioned routes 2026-06-11 10:24:35 +08:00
jiang 322e8156ee docs(delivery): document customer source packaging 2026-06-11 10:24:05 +08:00
jiang 1ec82971ca 强制使用 utf-8 存取 2026-03-16 15:49:49 +08:00
jiang 2d3fd353b4 添加清理 pycache 和编译扩展的功能 2026-03-16 15:06:27 +08:00
jiang 4191f2e508 添加对单个python文件的编译支持 2026-03-14 14:45:53 +08:00
jiang 3fd152e033 更新引用路径 2026-03-14 14:28:59 +08:00
jiang f6361a9eca 移除中文注释,避免 Github Action 工作流出错 2026-03-13 17:35:09 +08:00
jiang 431cf38aaf 优化打包流程,增加编译路径和忽略规则 2026-03-13 17:19:43 +08:00
jiang 220e641c0e 更新构建工作流,删除不必要的安全脚本 2026-03-13 17:14:50 +08:00
jiang ccfc78bb1c 添加构建和打包工作流 2026-03-13 16:53:18 +08:00
jiang f90c5ab6bb 更新封装路径 2026-03-13 16:46:17 +08:00
jiang 67648587c5 删除旧文件 2026-03-13 16:40:18 +08:00
jiang 42cac53319 更改编译代码的文件名 2026-03-13 16:40:02 +08:00
jiang c5f97da5c0 更新metadb引用路径 2026-03-13 16:22:13 +08:00
jiang bf088f691b 重构时序数据库连接逻辑,移除冗余代码 2026-03-13 16:18:11 +08:00
jiang ceea6e7456 元数据库目录结构变更 2026-03-13 16:15:09 +08:00
jiang 8338e11837 移除未使用的区域相关函数导入 2026-03-13 15:23:30 +08:00
jiang 59e44eb1e8 更新API请求体,移除不必要的请求参数 2026-03-13 15:23:02 +08:00
jiang cbad19e3bb 优化API文档,添加参数描述和示例 2026-03-13 15:17:06 +08:00
jiang 41173d61c8 删除网络元素相关的API端点空文件 2026-03-13 11:58:26 +08:00
jiang b58f075344 移除旧的InternalQueries类,更新管道查询逻辑 2026-03-13 11:38:36 +08:00
jiang 90a9bb62c6 重构SCADA信息获取,移除旧的数据库接口 2026-03-13 11:25:48 +08:00
jiang a310522812 删除发版无关的文件 2026-03-13 10:12:08 +08:00
344 changed files with 12454 additions and 3721201 deletions
+19
View File
@@ -0,0 +1,19 @@
.git
.github
.gitea
__pycache__/
.pytest_cache/
.mypy_cache/
.venv/
venv/
build/
dist/
package/
temp/
data/
# db_inp/
inp/
.env
*.pyc
*.dump
app/algorithms/health/model/my_survival_forest_model_quxi.joblib
+20 -9
View File
@@ -1,19 +1,16 @@
# TJWater Server 环境变量配置模板 # TJWater Server 环境变量配置模板
# 复制此文件为 .env 并填写实际值 # 复制此文件为 .env 并填写实际值
ENVIRONMENT="local" # CI/CD: 将生产 .env 的完整内容保存为 Gitea 仓库密钥 TJWATER_SERVER_ENV。
ENVIRONMENT="production"
NETWORK_NAME="tjwater" NETWORK_NAME="tjwater"
# ============================================ # ============================================
# 安全配置 (必填) # 敏感配置加密 (必填)
# ============================================ # ============================================
# JWT 密钥 - 用于生成和验证 Token # Fernet 格式,生产环境必须替换为独立密钥
# 生成方式: openssl rand -hex 32
SECRET_KEY=your-secret-key-here-change-in-production-use-openssl-rand-hex-32
# 数据加密密钥 - 用于敏感数据加密
# 生成方式: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())" # 生成方式: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
ENCRYPTION_KEY= # 用于项目数据库 DSN、GeoServer 管理密码等敏感配置
DATABASE_ENCRYPTION_KEY="rJC2VqLg4KrlSq+DGJcYm869q4v5KB2dFAeuQTe0I50=" DATABASE_ENCRYPTION_KEY="replace-with-generated-fernet-key"
# ============================================ # ============================================
# 数据库配置 (PostgreSQL) # 数据库配置 (PostgreSQL)
@@ -49,3 +46,17 @@ KEYCLOAK_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----"
KEYCLOAK_ALGORITHM=RS256 KEYCLOAK_ALGORITHM=RS256
KEYCLOAK_AUDIENCE="account" KEYCLOAK_AUDIENCE="account"
# ============================================
# Bocha Web Search API
# ============================================
BOCHA_API_KEY="sk-your-bocha-api-key"
BOCHA_WEB_SEARCH_URL="https://api.bochaai.com/v1/web-search"
BOCHA_WEB_SEARCH_TIMEOUT_SECONDS=30
# ============================================
# Tianditu Geocoding API
# ============================================
TIANDITU_GEOCODER_TOKEN="your-tianditu-geocoder-token"
TIANDITU_GEOCODER_URL="https://api.tianditu.gov.cn/geocoder"
TIANDITU_GEOCODER_TIMEOUT_SECONDS=30
-82
View File
@@ -1,82 +0,0 @@
# Copilot Instructions for TJWater Server
This repository contains the backend code for the TJWater Server, a water distribution network management system built with FastAPI.
## High-Level Architecture
The application follows a layered architecture:
- **Entry Point**: `app/main.py` initializes the FastAPI application, database connections (PostgreSQL & TimescaleDB), and middleware.
- **API Layer**: `app/api/v1` contains the route handlers.
- **Service Layer**: `app/services` contains business logic and orchestration.
- **Infrastructure Layer**: `app/infra` handles database connections (`db`), audit logging (`audit`), and external integrations.
- **Domain Layer**: `app/domain` likely contains core domain models.
- **Native/Algorithms**: `app/native` and `app/algorithms` handle specialized water network calculations (possibly using EPANET/WNTR).
## Build, Test, and Run Commands
### Environment Setup
- Dependencies are listed in `requirements.txt`.
- Configuration is managed via environment variables (see `.env.example` if available, or `app/core/config.py`).
- **Important**: Ensure `.env` is configured with correct database credentials for both PostgreSQL and TimescaleDB.
If first time setting up, you may want to create a Conda environment:
```bash
conda create -n server python=3.12
conda activate server
pip install uv
uv pip install -r requirements.txt
conda install -c conda-forge pymetis
```
### Running the Server
The preferred way to run the server locally is using the helper script which sets up the Python path correctly:
```bash
conda activate server
python scripts/run_server.py
```
Alternatively, you can run directly with uvicorn (ensure PYTHONPATH includes the root):
```bash
conda activate server
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
```
### Running Tests
Use `pytest` to run tests. The `tests/conftest.py` handles path setup.
```bash
# Run all tests
pytest
# Run a specific test file
pytest tests/unit/test_specific_file.py
# Run a specific test case
pytest tests/unit/test_specific_file.py::test_function_name
```
### Building (Optional)
The project includes scripts to compile Python modules to `.pyd` files using Cython (see `scripts/build_pyd.py`). This is likely for distribution/performance but not required for standard development.
## Key Conventions
- **Async/Await**: The codebase heavily uses `async` and `await` for I/O operations, especially database interactions.
- **Database Management**:
- Connections are managed globally in `app.infra.db` and initialized in `lifespan` (app/main.py).
- Use `app.infra.db.dynamic_manager` for project-specific database connections (multi-tenancy/dynamic projects).
- **Pydantic**: extensively used for data validation and settings management.
- **Scripts**: The `scripts/` directory contains many utility scripts for maintenance, data processing, and server management. Check there before writing new operational scripts.
- **Water Network Modeling**: Interactions with water network models often involve `epanet` or `wntr` libraries. Be aware of domain-specific terminology (nodes, links, junctions, tanks).
## Code Style
- Follow standard PEP 8 guidelines.
- No specific linter configuration was found, so default to standard Python formatting.
+73
View File
@@ -0,0 +1,73 @@
# Repository Guidelines
## Project Purpose
This repository is the customer-delivery edition of the TJWater backend. Treat it as a deployable delivery package, not as the primary internal development repository. Changes should be limited to customer-facing fixes, deployment compatibility, configuration templates, packaging, and delivery documentation.
Do not introduce internal-only experiments, debug utilities, local data, or source material that is not required for customer operation.
## Source Encapsulation Requirement
Core backend source code must be encapsulated before delivery. The sensitive implementation areas include business services, native integrations, hydraulic/network algorithms, and EPANET-related logic, especially:
- `app/services`
- `app/native`
- `app/algorithms`
- `app/infra/epanet`
Use the existing Cython packaging flow in `scripts/compile.py` for these areas. Do not ship uncompiled core `.py` files in the final customer package when compiled extension modules are expected. Keep public entry points, configuration loading, route wiring, and minimal package files readable only where required for runtime and operations.
Before removing source files with `--delete-source`, verify the build from a clean working tree or disposable copy. The delete mode is destructive by design.
## Workspace Structure
- `app/main.py` is the FastAPI entry point.
- `app/api` contains HTTP route handlers.
- `app/services` contains core business orchestration and must be protected in delivery builds.
- `app/native`, `app/algorithms`, and `app/infra/epanet` contain specialized computation and integration code that must be protected in delivery builds.
- `infra/` and `Dockerfile` contain deployment assets.
- `scripts/` contains operational and packaging helpers.
- `tests/` contains backend tests.
## Common Commands
Run commands from this repository root:
```bash
conda run -n server python -m pytest tests -q
conda run -n server python scripts/run_server.py
conda run -n server python scripts/compile.py
```
Clean compiled extensions when needed:
```bash
conda run -n server python scripts/compile.py --clean
```
Only use source deletion in a prepared delivery copy:
```bash
conda run -n server python scripts/compile.py --delete-source
```
## Change Policy
Keep changes scoped and conservative. Prefer compatibility patches, configuration adjustments, packaging fixes, and customer deployment hardening. If a change requires substantial business logic updates, make it first in the internal backend repository and then port the reviewed result here.
Do not weaken the encapsulation process to make debugging easier. If debugging requires readable source, do it in the internal repository or a non-delivery branch/copy.
## Testing Guidelines
Run the narrowest useful test command for the affected area. For delivery packaging changes, verify both:
- tests still pass before packaging;
- the packaged/compiled runtime can import and start the FastAPI app.
Do not add tests that depend on untracked customer data, local database dumps, or machine-specific files.
## Security & Delivery Rules
Never commit `.env`, production credentials, customer data, logs, generated caches, `node_modules/`, database dumps, or temporary delivery archives. Use `.env.example` or deployment documentation for required configuration.
Review Docker and deployment files carefully before delivery. Customer packages should contain only the files needed to run, operate, and diagnose the deployed service.
+90
View File
@@ -0,0 +1,90 @@
# TJWater Authentication and Metadata Management
## Ownership
Keycloak owns login identity, credentials, token issuance, and token expiry.
TJWater metadata stores only business snapshots and authorization data:
- `users.keycloak_id` is the stable identity binding.
- `users.username`, `users.email`, and `users.last_login_at` are Keycloak claim caches.
- `users.role`, `users.is_active`, and `users.is_superuser` control TJWater system access.
- `user_project_membership.project_role` controls project access.
The backend does not accept passwords, does not issue local JWTs, and does not
trust frontend-supplied user IDs.
## Login Snapshot Refresh
Every authenticated metadata-user resolution validates the Keycloak access token
and reads `sub`, `preferred_username` or `username`, and `email` claims. The
backend finds `users` by `keycloak_id = sub`, rejects inactive or missing users,
then refreshes `username`, `email`, and `last_login_at`.
This keeps local display data current without changing the identity binding.
There is no Keycloak webhook requirement; second-level user or permission sync is
out of scope unless explicitly requested later.
## Admin APIs
All admin APIs require metadata admin access: `users.is_superuser = true` or
`users.role = 'admin'`.
User and membership management:
- `GET /api/v1/admin/me`
- `POST /api/v1/admin/users/sync`
- `POST /api/v1/admin/users/sync/batch`
- `GET /api/v1/admin/users`
- `GET /api/v1/admin/users/{user_id}`
- `PATCH /api/v1/admin/users/{user_id}`
- `GET /api/v1/admin/projects/{project_id}/members`
- `POST /api/v1/admin/projects/{project_id}/members`
- `PATCH /api/v1/admin/projects/{project_id}/members/{user_id}`
- `DELETE /api/v1/admin/projects/{project_id}/members/{user_id}`
Project configuration:
- `GET /api/v1/admin/projects`
- `POST /api/v1/admin/projects`
- `PATCH /api/v1/admin/projects/{project_id}`
- `GET /api/v1/admin/projects/{project_id}/databases`
- `PUT /api/v1/admin/projects/{project_id}/databases`
- `DELETE /api/v1/admin/projects/{project_id}/databases/{db_role}`
- `POST /api/v1/admin/projects/{project_id}/databases/{db_role}/health`
## Secret Handling
Admins submit plaintext DSNs only through HTTPS admin APIs. Operators should not
write encrypted columns manually.
- `project_databases.dsn_encrypted` is encrypted with `DATABASE_ENCRYPTION_KEY`.
- Admin responses return only `has_dsn`.
- Audit logs record whether a secret was updated, but never store plaintext DSNs
or other secrets.
Generate the database encryption key with:
```bash
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
```
Keep keys stable for the lifetime of encrypted metadata. Rotating a key requires
decrypting with the old key and re-encrypting with the new key.
## Metadata Schema Patches
Apply metadata patches in order:
1. `resources/sql/004_metadata_auth_management.sql`
2. `resources/sql/005_metadata_project_configuration.sql`
`004` creates Keycloak-backed metadata users and project memberships. `005`
creates project and project database routing tables with uniqueness, role/type,
and pool-size constraints.
## Frontend System Management
`/system-admin` is shown only after `GET /api/v1/admin/me` confirms metadata
admin access. The page lets admins maintain metadata users, project members,
projects, project database routing for `biz_data` and `iot_data`, connection
health checks. This replaces direct SQL editing for normal project onboarding.
+80
View File
@@ -0,0 +1,80 @@
# Backend Naming Audit
DOC-003 audit for the customer-delivery `TJWaterServerCustomer` backend.
## Scope
Reviewed FastAPI route decorators under `app/api/v1/endpoints`, router prefixes in `app/api/v1/router.py`, and public request/response schema fields in `app/api` and `app/domain`.
The backend is mounted only under `/api/v1` from `app/main.py`; the old no-prefix router include remains commented out.
## Current Good Surface
These newer routes already follow the naming rule for public HTTP paths:
- Audit: `/api/v1/audit/logs`, `/api/v1/audit/logs/count`
- Metadata: `/api/v1/meta/project`, `/api/v1/meta/projects`, `/api/v1/meta/db/health`
- Business APIs: `/api/v1/burst-detection/detect`, `/api/v1/burst-location/locate`, `/api/v1/leakage/identify`
- Time-series APIs: `/api/v1/scada/by-ids-time-range`, `/api/v1/scada/by-ids-field-time-range`, `/api/v1/composite/clean-scada`
- Project data APIs: `/api/v1/scada-info`, `/api/v1/scheme-list`, `/api/v1/burst-locate-result`
- Web integrations: `/api/v1/web-search`, `/api/v1/geocode`
Path template parameters such as `{user_id}`, `{device_id}`, `{scheme_name}`, and `{link_id}` intentionally remain `snake_case`.
## Legacy URL Categories
### Keep With Compatibility
These now have `kebab-case` aliases. The frontend has been migrated to the replacement paths; keep the old paths as deprecated compatibility aliases for Agent planning, tests, customer scripts, or external callers:
| Current URL | Suggested replacement |
| --- | --- |
| `/api/v1/openproject/` | `/api/v1/projects/open` |
| `/api/v1/project_info/` | `/api/v1/project-info` |
| `/api/v1/getallschemes/` | `/api/v1/schemes` |
| `/api/v1/getallsensorplacements/` | `/api/v1/sensor-placement-schemes` |
| `/api/v1/sensorplacementscheme/create` | `/api/v1/sensor-placement-schemes` |
| `/api/v1/burst_analysis/` | `/api/v1/burst-analysis` |
| `/api/v1/valve_isolation_analysis/` | `/api/v1/valve-isolation-analysis` |
| `/api/v1/flushing_analysis/` | `/api/v1/flushing-analysis` |
| `/api/v1/contaminant_simulation/` | `/api/v1/contaminant-simulation` |
| `/api/v1/runsimulationmanuallybydate/` | `/api/v1/simulations/run-by-date` |
### Broad Legacy Surface
These route groups expose many command-style concatenated paths. They should not be copied into new work; replace only when a caller migration is planned:
- Project lifecycle: `listprojects`, `createproject`, `deleteproject`, `isprojectopen`, `closeproject`, `copyproject`, `importinp`, `exportinp`, `readinp`, `dumpinp`, `lockproject`, `unlockproject`
- Network object CRUD: `addjunction`, `getjunctionelevation`, `setpipediameter`, `getvalvesetting`, and similar junction/pipe/pump/tank/reservoir/valve routes
- Region/DMA/VD commands: `calculatedistrictmeteringareaforregion`, `getdistrictmeteringarea`, `generatevirtualdistrict`, and related routes
- SCADA native CRUD: `getscadadevice`, `setscadadevicedata`, `cleanscadaelement`, and related routes
- Snapshot/cache utilities: `takesnapshotforoperation`, `syncwithserver`, `clearrediskey`, `queryredis`
- Advanced simulation endpoints with underscore paths: `pressure_regulation`, `daily_scheduling_analysis`, `network_update`, `pressure_sensor_placement_kmeans`
### Direct Cleanup Candidates
These are likely safe only after confirming no caller uses them:
- `/api/v1/test_dict/`: development/test utility in `misc.py`.
- `/api/v1/takenapshotforcurrentoperation`: typo compatibility path; keep deprecated if any client may still call it.
- `/api/v1/getpumpenergyproperties//` and `/api/v1/setpumpenergyproperties//`: double-slash paths in options endpoints.
## Field Naming
Most public JSON, query, and SSE fields are already `snake_case`, including `user_id`, `scheme_name`, `scheme_type`, `start_time`, `end_time`, `device_ids`, `session_id`, and `request_id`.
Known legacy exception:
- `BurstAnalysis.burst_ID` in `app/api/v1/endpoints/simulation.py` should become `burst_id` on a new API contract. Preserve `burst_ID` only for the legacy body shape.
Headers keep standard HTTP casing:
- `X-Project-Id`
## Internal vs Customer Difference
The customer backend retains local auth/user-management routes under `/api/v1/auth` and `/api/v1/users`; the internal backend has migrated to Keycloak/metadata admin routes. Treat those Customer-only auth routes as delivery compatibility surface, not a source for new internal API naming.
## Recommendation
Do not rename existing legacy routes in place. For each active legacy route, keep the new `kebab-case` alias as the documented path, keep the old route marked deprecated, migrate remaining Agent/customer/script callers, then remove only after a documented compatibility window.
+102
View File
@@ -0,0 +1,102 @@
# tjwater-server Customer Image Packaging Notes
This repository is the customer-delivery backend package. Build delivery images from this repository root.
## Image Build
Build the customer backend image:
```bash
docker build -t tjwater-server:latest .
```
The `Dockerfile` already performs source encapsulation in the builder stage:
```bash
python scripts/compile.py
python scripts/compile.py --delete-source
```
The compiled and source-deleted `app/` directory is then copied into the final runner stage. The source deletion happens inside the Docker builder layer only; it does not delete local workspace files.
## Encapsulation Scope
`scripts/compile.py` defaults to compiling these sensitive areas:
- `app/services`
- `app/native/wndb`
- `app/algorithms`
- `app/infra/epanet/epanet.py`
These areas should not contain uncompiled `.py` source files in the delivered image. Public entry points, API route wiring, schemas, configuration, and operational files may remain readable when required for runtime.
## Verification
After building, verify the image tag:
```bash
docker image ls tjwater-server
```
Verify that core source files were removed and compiled extensions exist:
```bash
docker run --rm --entrypoint sh tjwater-server:latest -c "\
printf 'core_py_count='; \
find /app/app/services /app/app/native/wndb /app/app/algorithms /app/app/infra/epanet/epanet.py -name '*.py' 2>/dev/null | wc -l; \
printf 'core_so_count='; \
find /app/app/services /app/app/native/wndb /app/app/algorithms /app/app/infra/epanet -name '*.so' 2>/dev/null | wc -l; \
python -c 'import app.main; print(\"import_app_main=ok\")'"
```
Expected delivery result:
```text
core_py_count=0
core_so_count=<non-zero>
import_app_main=ok
```
Warnings from third-party packages during import are not necessarily build failures. Treat non-zero exit codes, failed imports, or leftover core `.py` files as blockers.
## Export For Windows Delivery
Export the image tarball to the Windows desktop from WSL:
```bash
docker save -o /mnt/c/Users/admin/Desktop/tjwater-server-latest.tar tjwater-server:latest
```
Adjust the Windows username if needed. To find available desktop paths:
```bash
find /mnt/c/Users -maxdepth 2 -type d \( -name Desktop -o -name 桌面 \) 2>/dev/null
```
On the target machine, import the image with:
```bash
docker load -i tjwater-server-latest.tar
```
## Deployment Caution
The development `infra/docker/docker-compose.yml` bind-mounts local source:
```yaml
volumes:
- ../../app:/app/app
- ../../resources:/app/resources
```
Do not use that source mount for customer delivery of the encapsulated image. Mounting `../../app` over `/app/app` replaces the compiled code inside the image with local source files and defeats the encapsulation. For delivery compose files, use the built image directly and mount only required runtime data/config paths.
## Local Safety
Do not run this destructive command in the normal working tree:
```bash
python scripts/compile.py --delete-source
```
Only use `--delete-source` in Docker builder stages or in a disposable delivery copy. The normal delivery image build already handles this safely.
+27 -9
View File
@@ -1,21 +1,39 @@
FROM continuumio/miniconda3:latest FROM condaforge/miniforge3:latest AS runtime-base
WORKDIR /app WORKDIR /app
ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \
PIP_TRUSTED_HOST=pypi.tuna.tsinghua.edu.cn \
UV_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
# 安装 Python 3.12 和 pymetis (通过 conda-forge 避免编译问题) # 安装 Python 3.12 和 pymetis (通过 conda-forge 避免编译问题)
RUN conda install -y -c conda-forge python=3.12 pymetis && \ RUN mamba install -y python=3.12 pymetis && \
conda clean -afy mamba clean -afy
COPY requirements.txt . COPY requirements.txt .
RUN pip install uv RUN pip install --no-cache-dir uv
RUN uv pip install --no-cache-dir -r requirements.txt RUN uv pip install --system --no-cache-dir -r requirements.txt
FROM runtime-base AS builder
RUN mamba install -y c-compiler cxx-compiler && \
mamba clean -afy
COPY app ./app
COPY scripts ./scripts
RUN python scripts/compile.py && \
python scripts/compile.py --delete-source
FROM runtime-base AS runner
# 将代码放入子目录 'app',将数据放入子目录 'db_inp' # 将代码放入子目录 'app',将数据放入子目录 'db_inp'
# 这样临时文件默认会生成在 /app 下,而代码在 /app/app 下,实现了分离 # 这样临时文件默认会生成在 /app 下,而代码在 /app/app 下,实现了分离
COPY app ./app COPY --from=builder /app/app ./app
COPY db_inp ./db_inp RUN python -c "from pathlib import Path; from zipfile import ZipFile; model_dir = Path('app/algorithms/health/model'); zip_path = model_dir / 'my_survival_forest_model_quxi.zip'; joblib_name = 'my_survival_forest_model_quxi.joblib'; joblib_path = model_dir / joblib_name; assert zip_path.exists(), f'Model archive not found: {zip_path}'; archive = ZipFile(zip_path); archive.extract(joblib_name, model_dir); archive.close(); assert joblib_path.exists(), f'Model file not extracted: {joblib_path}'" && \
COPY temp ./temp rm -f app/algorithms/health/model/my_survival_forest_model_quxi.zip
COPY .env . # COPY db_inp ./db_inp
RUN mkdir -p db_inp temp data inp
# 设置 PYTHONPATH 以便 uvicorn 找到 app 模块 # 设置 PYTHONPATH 以便 uvicorn 找到 app 模块
ENV PYTHONPATH=/app ENV PYTHONPATH=/app
+107
View File
@@ -0,0 +1,107 @@
# TJWaterServerCustomer 客户版后端
`TJWaterServerCustomer` 是 TJWater 客户交付版 Python 后端。该仓库应被视为可部署交付包,只保留客户运行、配置、部署、诊断和交付说明所需内容。
## 技术栈
- Python 3.12
- FastAPI / Uvicorn
- Pydantic / SQLAlchemy / psycopg
- Redis、PostgreSQL、PostGIS、TimescaleDB
- WNTR、EPANET、Cython、科学计算与空间分析依赖
- pytest
## 目录结构
```text
app/main.py FastAPI 入口
app/api/ HTTP API 路由
app/auth/ 认证和权限上下文
app/core/ 配置、日志和基础设施初始化
app/domain/ 领域模型和 Pydantic schema
app/infra/ 数据库、缓存、EPANET 和外部集成
app/services/ 核心业务服务,交付镜像中必须封装
app/algorithms/ 核心算法,交付镜像中必须封装
app/native/ 本地管网数据读写与转换,交付镜像中必须封装
scripts/compile.py Cython 封装脚本
infra/docker/ Docker Compose 编排
tests/ 后端测试
```
## 本地开发
推荐使用已有 conda 环境:
```bash
conda run -n server python -m pytest tests -q
conda run -n server python scripts/run_server.py
```
本地调试不要在正常工作树执行源码删除命令。
## 客户版镜像打包
交付镜像标签通常为:
```bash
docker build -t tjwater-server:latest .
```
`Dockerfile` 会在 builder 阶段执行:
```bash
python scripts/compile.py
python scripts/compile.py --delete-source
```
源码删除只发生在 Docker 构建层内,不会删除本地工作树源码。详细封装、验证和 Windows 导出说明见:
```text
DELIVERY_PACKAGING_NOTES.md
```
## 封装范围
`scripts/compile.py` 默认封装:
- `app/services`
- `app/native/wndb`
- `app/algorithms`
- `app/infra/epanet/epanet.py`
交付镜像中这些核心目录不应残留未编译的 `.py` 源码,应以 `.so` 扩展模块运行。
## 验证命令
构建后检查镜像:
```bash
docker image ls tjwater-server
```
检查核心源码是否已删除、FastAPI 入口是否可导入:
```bash
docker run --rm --entrypoint sh tjwater-server:latest -c "\
printf 'core_py_count='; \
find /app/app/services /app/app/native/wndb /app/app/algorithms /app/app/infra/epanet/epanet.py -name '*.py' 2>/dev/null | wc -l; \
printf 'core_so_count='; \
find /app/app/services /app/app/native/wndb /app/app/algorithms /app/app/infra/epanet -name '*.so' 2>/dev/null | wc -l; \
python -c 'import app.main; print(\"import_app_main=ok\")'"
```
期望结果:
```text
core_py_count=0
core_so_count=<非零>
import_app_main=ok
```
## 部署注意
客户交付时不要把本地 `../../app` 挂载到容器 `/app/app`,否则会覆盖镜像内已封装代码。交付 compose 文件应使用构建好的镜像,只挂载必要的运行数据和配置。
## 安全规则
不要提交 `.env`、生产凭据、客户数据、数据库 dump、日志、生成缓存、临时交付压缩包或本地运行目录。客户版仓库不应加入内部实验、调试工具或非交付源码材料。
+44 -35
View File
@@ -1,36 +1,45 @@
from app.algorithms.cleaning import flow_data_clean, pressure_data_clean """Algorithm package with side-effect-free, lazy compatibility exports."""
from app.algorithms.sensor import (
pressure_sensor_placement_sensitivity,
pressure_sensor_placement_kmeans,
)
from app.algorithms.isolation.valve import valve_isolation_analysis
from app.algorithms.leakage import LeakageIdentifier
from app.algorithms.health import PipelineHealthAnalyzer
from app.algorithms.burst_location import run_burst_location
from app.algorithms.simulation.scenarios import (
convert_to_local_unit,
burst_analysis,
valve_close_analysis,
flushing_analysis,
contaminant_simulation,
age_analysis,
pressure_regulation,
)
__all__ = [ from importlib import import_module
"flow_data_clean", from typing import Any
"pressure_data_clean",
"pressure_sensor_placement_sensitivity",
"pressure_sensor_placement_kmeans", _EXPORT_MODULES = {
"convert_to_local_unit", "flow_data_clean": "app.algorithms.cleaning",
"burst_analysis", "pressure_data_clean": "app.algorithms.cleaning",
"valve_close_analysis", "pressure_sensor_placement_sensitivity": "app.algorithms.sensor",
"flushing_analysis", "pressure_sensor_placement_kmeans": "app.algorithms.sensor",
"contaminant_simulation", "valve_isolation_analysis": "app.algorithms.isolation.valve",
"age_analysis", "LeakageIdentifier": "app.algorithms.leakage",
"pressure_regulation", "PipelineHealthAnalyzer": "app.algorithms.health",
"valve_isolation_analysis", "run_burst_location": "app.algorithms.burst_location",
"LeakageIdentifier", **{
"PipelineHealthAnalyzer", name: "app.algorithms.simulation.scenarios"
"run_burst_location", for name in (
] "convert_to_local_unit",
"burst_analysis",
"valve_close_analysis",
"flushing_analysis",
"contaminant_simulation",
"age_analysis",
"pressure_regulation",
)
},
}
__all__ = list(_EXPORT_MODULES)
def __getattr__(name: str) -> Any:
try:
module_name = _EXPORT_MODULES[name]
except KeyError as exc:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from exc
value = getattr(import_module(module_name), name)
globals()[name] = value
return value
def __dir__() -> list[str]:
return sorted({*globals(), *__all__})
@@ -121,7 +121,7 @@ def run_burst_location(
basic_pressure: float = 10.0, basic_pressure: float = 10.0,
n_workers: int = DEFAULT_N_WORKERS, n_workers: int = DEFAULT_N_WORKERS,
partition_on_full_graph: bool = True, partition_on_full_graph: bool = True,
visualize_partition: bool = True, visualize_partition: bool = False,
visualize_pause_seconds: float = 0.3, visualize_pause_seconds: float = 0.3,
final_candidates_csv_path: ( final_candidates_csv_path: (
str | None str | None
+475 -179
View File
@@ -1,18 +1,435 @@
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.impute import SimpleImputer
import os import os
from app.algorithms._utils import fill_time_gaps ID_LIKE_COLUMNS = {
"id",
"device_id",
"node_id",
"sensor_id",
"monitor_id",
"junction_id",
}
def _normalize_time_frame(data: pd.DataFrame) -> pd.DataFrame:
"""返回按时间排序的副本,并尽量将 time 列解析为时间类型。"""
data = data.copy()
if "time" in data.columns:
data["time"] = pd.to_datetime(data["time"], errors="coerce")
data = data.sort_values(["time"]).reset_index(drop=True)
return data
def _select_pressure_columns(data: pd.DataFrame) -> tuple[list[str], list[str]]:
"""区分需要清洗的数值列与需要原样保留的列。"""
value_cols: list[str] = []
keep_cols: list[str] = []
for col in data.columns:
if col == "time":
continue
col_key = col.lower()
if col_key in ID_LIKE_COLUMNS or col_key.endswith("_id"):
keep_cols.append(col)
continue
numeric = pd.to_numeric(data[col], errors="coerce")
if numeric.notna().sum() == 0 or numeric.nunique(dropna=True) <= 1:
keep_cols.append(col)
else:
value_cols.append(col)
return value_cols, keep_cols
def _robust_scale(values: pd.Series) -> float:
"""基于 MAD 计算稳健尺度。"""
series = pd.to_numeric(values, errors="coerce").dropna()
if series.empty:
return 1.0
median = series.median()
mad = (series - median).abs().median()
if pd.notna(mad) and mad > 0:
return float(1.4826 * mad)
iqr = series.quantile(0.75) - series.quantile(0.25)
if pd.notna(iqr) and iqr > 0:
return float(iqr / 1.349)
std = series.std()
if pd.notna(std) and std > 0:
return float(std)
return 1.0
def _shrink_toward_baseline(observed: float, baseline: float, scale: float) -> float:
"""把观测值向基线值收缩,scale 越小,修复越强。"""
if pd.isna(observed):
return baseline
if pd.isna(baseline):
return observed
diff = observed - baseline
weight = scale / (abs(diff) + scale)
return float(baseline + diff * weight)
def _infer_time_frequency(time_values: pd.Series | pd.Index) -> pd.Timedelta:
"""从时间序列中推断采样频率,失败时默认 15 分钟。"""
parsed = pd.to_datetime(pd.Series(time_values), errors="coerce").dropna().sort_values()
if len(parsed) < 2:
return pd.Timedelta(minutes=15)
diffs = parsed.diff().dropna()
diffs = diffs[diffs > pd.Timedelta(0)]
if diffs.empty:
return pd.Timedelta(minutes=15)
mode = diffs.mode()
return mode.iloc[0] if not mode.empty else diffs.median()
def _build_local_pressure_baseline(series: pd.Series) -> pd.Series:
"""基于局部插值与中值滤波构造平滑基线。"""
baseline = _safe_time_interpolate(series)
baseline = baseline.rolling(window=5, center=True, min_periods=1).median()
baseline = _safe_time_interpolate(baseline)
return baseline.ffill().bfill()
def _build_seasonal_pressure_baseline(series: pd.Series) -> pd.Series:
"""按一天内的同一时刻构造季节性基线,适合日周期压力数据。"""
if not isinstance(series.index, pd.DatetimeIndex):
return pd.Series(np.nan, index=series.index, dtype=float)
slot_labels = pd.Series(series.index.strftime("%H:%M:%S"), index=series.index)
return series.groupby(slot_labels).transform("median")
def _detect_pressure_spikes(series: pd.Series, local_baseline: pd.Series) -> pd.Series:
"""识别单点异常上升/下降尖峰,避免过度修正正常波动。"""
residual = series - local_baseline
neighbor_center = (series.shift(1) + series.shift(-1)) / 2
curvature = series - neighbor_center
residual_scale = max(_robust_scale(residual), 1e-6)
curvature_scale = max(_robust_scale(curvature), 1e-6)
direction_flip = ((series - series.shift(1)) * (series.shift(-1) - series) < 0).fillna(False)
return (
residual.abs() > 3.5 * residual_scale
) & (
curvature.abs() > 3.0 * curvature_scale
) & direction_flip
def _fill_pressure_gaps(
original: pd.Series,
repaired: pd.Series,
local_baseline: pd.Series,
seasonal_baseline: pd.Series,
) -> pd.Series:
"""短缺口用局部插值,长缺口优先使用同一时刻的季节性轨迹。"""
missing_mask = original.isna()
if not missing_mask.any():
return repaired
gap_groups = (missing_mask != missing_mask.shift(fill_value=False)).cumsum()
gap_lengths = missing_mask.groupby(gap_groups).transform("sum").where(missing_mask, 0)
filled = repaired.copy()
short_gap_mask = missing_mask & (gap_lengths < 4)
long_gap_mask = missing_mask & ~short_gap_mask
filled[short_gap_mask] = local_baseline[short_gap_mask]
long_gap_fill = seasonal_baseline.where(seasonal_baseline.notna(), local_baseline)
filled[long_gap_mask] = long_gap_fill[long_gap_mask]
return filled
def _clean_pressure_series(series: pd.Series) -> pd.Series:
"""清洗单个压力时间序列。"""
series = pd.to_numeric(series, errors="coerce").astype(float)
local_baseline = _build_local_pressure_baseline(series)
spike_mask = _detect_pressure_spikes(series, local_baseline)
repaired = series.copy()
repaired[spike_mask] = local_baseline[spike_mask]
seasonal_baseline = _build_seasonal_pressure_baseline(repaired)
repaired = _fill_pressure_gaps(series, repaired, local_baseline, seasonal_baseline)
if repaired.isna().any():
repaired = repaired.where(repaired.notna(), local_baseline)
return repaired.ffill().bfill()
def _format_time_column(data: pd.DataFrame) -> pd.DataFrame:
"""统一输出时间格式,方便下游直接按 ISO 字符串解析。"""
if "time" not in data.columns:
return data
formatted = data.copy()
time_values = pd.to_datetime(formatted["time"], errors="coerce")
if time_values.isna().all():
return formatted
if time_values.dt.tz is not None:
time_strings = time_values.dt.strftime("%Y-%m-%dT%H:%M:%S%z")
time_strings = time_strings.str.replace(
r"([+-]\d{2})(\d{2})$",
r"\1:\2",
regex=True,
)
else:
time_strings = time_values.dt.strftime("%Y-%m-%dT%H:%M:%S")
formatted["time"] = time_strings.where(time_values.notna(), formatted["time"])
return formatted
def _expand_snapshot_time_grid(data: pd.DataFrame, freq: pd.Timedelta) -> pd.DataFrame:
"""仅补齐时间轴,不提前填充值,避免长缺口丢失原始形状特征。"""
expanded = data.copy()
expanded["time"] = pd.to_datetime(expanded["time"], errors="coerce")
expanded = expanded.dropna(subset=["time"]).sort_values("time")
if expanded.empty:
return data
indexed = expanded.set_index("time")
full_index = pd.date_range(indexed.index.min(), indexed.index.max(), freq=freq)
indexed = indexed.reindex(full_index)
indexed.index.name = "time"
return indexed.reset_index()
def _safe_datetime_index(values: pd.Series | pd.Index | list[object]) -> pd.DatetimeIndex | None:
"""尽量把时间值标准化为 DatetimeIndex;失败则返回 None。"""
parsed = pd.to_datetime(values, errors="coerce")
try:
datetime_index = pd.DatetimeIndex(parsed)
except (TypeError, ValueError):
return None
if datetime_index.isna().all():
return None
return datetime_index
def _safe_time_interpolate(series: pd.Series) -> pd.Series:
"""仅在索引确实是 DatetimeIndex 时使用 time interpolation。"""
if isinstance(series.index, pd.DatetimeIndex):
return series.interpolate(method="time", limit_direction="both")
return series.interpolate(limit_direction="both")
def _detect_long_form_identifier(data: pd.DataFrame, value_cols: list[str], keep_cols: list[str]) -> str | None:
"""识别 time/id/value 长表结构。"""
if "time" not in data.columns or len(value_cols) != 1:
return None
identifier_candidates = [
col
for col in keep_cols
if col.lower() in ID_LIKE_COLUMNS or col.lower().endswith("_id")
]
if len(identifier_candidates) != 1:
return None
if not data["time"].duplicated().any():
return None
return identifier_candidates[0]
def _clean_long_form_pressure(
data: pd.DataFrame,
value_col: str,
identifier_col: str,
keep_cols: list[str],
fill_gaps: bool,
) -> pd.DataFrame:
"""按测点拆分 long-form 压力数据,再逐列清洗后恢复原结构。"""
data = _normalize_time_frame(data)
wide_df = (
data[[identifier_col, "time", value_col]]
.pivot(index="time", columns=identifier_col, values=value_col)
.reset_index()
)
sensor_cols = [col for col in wide_df.columns if col != "time"]
cleaned_wide = _clean_snapshot_pressure(wide_df, sensor_cols, keep_cols=[], fill_gaps=fill_gaps)
cleaned_long = cleaned_wide.melt(
id_vars="time",
var_name=identifier_col,
value_name=value_col,
)
passthrough_cols = [col for col in keep_cols if col != identifier_col]
if passthrough_cols:
metadata = data[[identifier_col] + passthrough_cols].drop_duplicates(subset=[identifier_col])
cleaned_long = cleaned_long.merge(metadata, on=identifier_col, how="left")
try:
cleaned_long[identifier_col] = cleaned_long[identifier_col].astype(data[identifier_col].dtype)
except (TypeError, ValueError):
pass
cleaned_long = cleaned_long.sort_values(["time", identifier_col]).reset_index(drop=True)
ordered_cols = ["time", identifier_col] + passthrough_cols + [value_col]
cleaned_long = cleaned_long[[col for col in ordered_cols if col in cleaned_long.columns]]
return cleaned_long
def _build_time_slot_frame(
data: pd.DataFrame, value_col: str, expected_slots: int
) -> pd.DataFrame:
"""把重复时间点整理成 time x slot 的矩阵。"""
grouped = data.groupby("time", sort=True)
times = list(grouped.groups.keys())
slot_frame = pd.DataFrame(index=pd.Index(times, name="time"), columns=range(expected_slots), dtype=float)
for time_value, group in grouped:
values = pd.to_numeric(group[value_col], errors="coerce").tolist()
for slot_idx, value in enumerate(values[:expected_slots]):
slot_frame.loc[time_value, slot_idx] = value
return slot_frame
def _slot_baseline(slot_frame: pd.DataFrame) -> pd.DataFrame:
"""对每个槽位做时间插值和平滑,得到基线轨迹。"""
baseline = pd.DataFrame(index=slot_frame.index, columns=slot_frame.columns, dtype=float)
for col in slot_frame.columns:
series = slot_frame[col].astype(float)
series = _safe_time_interpolate(series)
series = series.rolling(window=5, center=True, min_periods=1).median()
series = _safe_time_interpolate(series).ffill().bfill()
baseline[col] = series
return baseline
def _choose_insertion_position(
observed: list[float], baseline_row: pd.Series, expected_slots: int
) -> int:
"""为少一个观测值的时间组选择最合理的插入位置。"""
missing_count = expected_slots - len(observed)
if missing_count <= 0:
return 0
best_pos = 0
best_cost = float("inf")
for insert_pos in range(expected_slots):
cost = 0.0
obs_idx = 0
for slot_idx in range(expected_slots):
if slot_idx == insert_pos:
continue
obs_value = observed[obs_idx]
base_value = float(baseline_row.iloc[slot_idx])
if pd.notna(obs_value) and pd.notna(base_value):
cost += abs(obs_value - base_value)
obs_idx += 1
if cost < best_cost:
best_cost = cost
best_pos = insert_pos
return best_pos
def _clean_repeated_timestamp_pressure(
data: pd.DataFrame, value_col: str, keep_cols: list[str]
) -> pd.DataFrame:
"""针对同一时间点重复采样的压力数据进行修复。"""
data = _normalize_time_frame(data)
grouped_sizes = data.groupby("time").size()
if grouped_sizes.empty:
return data
expected_slots = int(grouped_sizes.mode().iloc[0]) if not grouped_sizes.mode().empty else int(grouped_sizes.max())
expected_slots = max(expected_slots, int(grouped_sizes.max()))
slot_frame = _build_time_slot_frame(data, value_col, expected_slots)
baseline_frame = _slot_baseline(slot_frame)
residuals = slot_frame - baseline_frame
slot_scales = {
col: max(_robust_scale(residuals[col]), 1e-6) for col in residuals.columns
}
cleaned_rows: list[dict[str, object]] = []
grouped = data.groupby("time", sort=True)
for time_value, group in grouped:
observed_values = pd.to_numeric(group[value_col], errors="coerce").tolist()
baseline_row = baseline_frame.loc[time_value]
insert_pos = _choose_insertion_position(observed_values, baseline_row, expected_slots)
cleaned_values: list[float] = []
obs_idx = 0
for slot_idx in range(expected_slots):
if slot_idx == insert_pos and len(observed_values) < expected_slots:
cleaned_values.append(float(baseline_row.iloc[slot_idx]))
continue
if obs_idx >= len(observed_values):
cleaned_values.append(float(baseline_row.iloc[slot_idx]))
continue
observed = observed_values[obs_idx]
baseline = float(baseline_row.iloc[slot_idx])
cleaned_values.append(
_shrink_toward_baseline(observed, baseline, slot_scales.get(slot_idx, 1.0))
)
obs_idx += 1
# 其余字段原样保留;常量列(如 id)直接复制第一条记录即可
template_row = group.iloc[0].to_dict()
for slot_idx, cleaned_value in enumerate(cleaned_values):
row = dict(template_row)
row["time"] = time_value
row[value_col] = cleaned_value
cleaned_rows.append(row)
cleaned_df = pd.DataFrame(cleaned_rows)
cleaned_df = cleaned_df.sort_values(["time"]).reset_index(drop=True)
ordered_cols = ["time"] + keep_cols + [value_col]
ordered_cols = [col for col in ordered_cols if col in cleaned_df.columns]
remaining_cols = [col for col in cleaned_df.columns if col not in ordered_cols]
cleaned_df = cleaned_df[ordered_cols + remaining_cols]
return _format_time_column(cleaned_df)
def _clean_snapshot_pressure(
data: pd.DataFrame, value_cols: list[str], keep_cols: list[str], fill_gaps: bool
) -> pd.DataFrame:
"""针对单条时间序列或多列快照数据进行稳健修复。"""
data = _normalize_time_frame(data)
if fill_gaps and "time" in data.columns:
freq = _infer_time_frequency(data["time"])
data = _expand_snapshot_time_grid(data, freq)
data["time"] = pd.to_datetime(data["time"], errors="coerce")
data = data.sort_values(["time"]).reset_index(drop=True)
cleaned_df = data.copy()
time_index = (
_safe_datetime_index(cleaned_df["time"])
if "time" in cleaned_df.columns
else None
)
if time_index is None:
time_index = pd.RangeIndex(start=0, stop=len(cleaned_df))
for col in value_cols:
series = pd.Series(
pd.to_numeric(cleaned_df[col], errors="coerce").to_numpy(),
index=time_index,
dtype=float,
)
cleaned_df[col] = _clean_pressure_series(series).to_numpy()
ordered_cols = ["time"] + keep_cols + value_cols
ordered_cols = [col for col in ordered_cols if col in cleaned_df.columns]
remaining_cols = [col for col in cleaned_df.columns if col not in ordered_cols]
cleaned_df = cleaned_df[ordered_cols + remaining_cols]
return _format_time_column(cleaned_df)
def clean_pressure_data_km( def clean_pressure_data_km(
input_csv_path: str, show_plot: bool = False, fill_gaps: bool = True input_csv_path: str, show_plot: bool = False, fill_gaps: bool = True
) -> str: ) -> str:
""" """
读取输入 CSV,基于 KMeans 检测异常并用滚动平均修复。输出为 <input_basename>_cleaned.xlsx(同目录)。 读取输入 CSV,基于时间结构进行稳健修复。输出为 <input_basename>_cleaned.xlsx(同目录)。
原始数据在 sheet 'raw_pressure_data',处理后数据在 sheet 'cleaned_pressusre_data' 原始数据在 sheet 'raw_pressure_data',处理后数据在 sheet 'cleaned_pressusre_data'
返回输出文件的绝对路径。 返回输出文件的绝对路径。
@@ -24,80 +441,38 @@ def clean_pressure_data_km(
# 读取 CSV # 读取 CSV
input_csv_path = os.path.abspath(input_csv_path) input_csv_path = os.path.abspath(input_csv_path)
data = pd.read_csv(input_csv_path, header=0, index_col=None, encoding="utf-8") data = pd.read_csv(input_csv_path, header=0, index_col=None, encoding="utf-8")
data = _normalize_time_frame(data)
value_cols, keep_cols = _select_pressure_columns(data)
has_repeated_time = "time" in data.columns and data["time"].duplicated().any()
identifier_col = _detect_long_form_identifier(data, value_cols, keep_cols)
# 补齐时间缺口(如果数据包含 time 列) if identifier_col is not None:
if fill_gaps and "time" in data.columns: data_repaired = _clean_long_form_pressure(
data = fill_time_gaps( data,
data, time_col="time", freq="1min", short_gap_threshold=10 value_cols[0],
identifier_col,
keep_cols,
fill_gaps,
) )
elif has_repeated_time and len(value_cols) == 1:
data_repaired = _clean_repeated_timestamp_pressure(data, value_cols[0], keep_cols)
else:
data_repaired = _clean_snapshot_pressure(data, value_cols, keep_cols, fill_gaps)
# 分离时间列和数值列 # 可选可视化(只展示首个数值列
time_col_data = None
if "time" in data.columns:
time_col_data = data["time"]
data = data.drop(columns=["time"])
# 标准化
data_norm = (data - data.mean()) / data.std()
# 聚类与异常检测
k = 3
kmeans = KMeans(n_clusters=k, init="k-means++", n_init=50, random_state=42)
clusters = kmeans.fit_predict(data_norm)
centers = kmeans.cluster_centers_
distances = np.linalg.norm(data_norm.values - centers[clusters], axis=1)
threshold = distances.mean() + 3 * distances.std()
anomaly_pos = np.where(distances > threshold)[0]
anomaly_indices = data.index[anomaly_pos]
anomaly_details = {}
for pos in anomaly_pos:
row_norm = data_norm.iloc[pos]
cluster_idx = clusters[pos]
center = centers[cluster_idx]
diff = abs(row_norm - center)
main_sensor = diff.idxmax()
anomaly_details[data.index[pos]] = main_sensor
# 修复:滚动平均(窗口可调)
data_rolled = data.rolling(window=13, center=True, min_periods=1).mean()
data_repaired = data.copy()
for pos in anomaly_pos:
label = data.index[pos]
sensor = anomaly_details[label]
data_repaired.loc[label, sensor] = data_rolled.loc[label, sensor]
# 可选可视化(使用位置作为 x 轴)
plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False plt.rcParams["axes.unicode_minus"] = False
if show_plot and value_cols:
if show_plot and len(data.columns) > 0: plot_col = value_cols[0]
n = len(data) if "time" in data_repaired.columns:
time = np.arange(n) x = pd.to_datetime(data_repaired["time"], errors="coerce")
plt.figure(figsize=(12, 8)) else:
for col in data.columns: x = np.arange(len(data_repaired))
plt.plot(time, data[col].values, marker="o", markersize=3, label=col) plt.figure(figsize=(12, 6))
for pos in anomaly_pos: plt.plot(x, pd.to_numeric(data_repaired[plot_col], errors="coerce"), label="cleaned")
sensor = anomaly_details[data.index[pos]] plt.xlabel("时间" if "time" in data_repaired.columns else "序号")
plt.plot(pos, data.iloc[pos][sensor], "ro", markersize=8)
plt.xlabel("时间点(序号)")
plt.ylabel("压力监测值") plt.ylabel("压力监测值")
plt.title("各传感器折线图(红色标记主要异常点)") plt.title(f"{plot_col} 清洗结果")
plt.legend()
plt.show()
plt.figure(figsize=(12, 8))
for col in data_repaired.columns:
plt.plot(
time, data_repaired[col].values, marker="o", markersize=3, label=col
)
for pos in anomaly_pos:
sensor = anomaly_details[data.index[pos]]
plt.plot(pos, data_repaired.iloc[pos][sensor], "go", markersize=8)
plt.xlabel("时间点(序号)")
plt.ylabel("修复后压力监测值")
plt.title("修复后各传感器折线图(绿色标记修复值)")
plt.legend() plt.legend()
plt.show() plt.show()
@@ -110,9 +485,6 @@ def clean_pressure_data_km(
# 如果原始数据包含时间列,将其添加回结果 # 如果原始数据包含时间列,将其添加回结果
data_for_save = data.copy() data_for_save = data.copy()
data_repaired_for_save = data_repaired.copy() data_repaired_for_save = data_repaired.copy()
if time_col_data is not None:
data_for_save.insert(0, "time", time_col_data)
data_repaired_for_save.insert(0, "time", time_col_data)
if os.path.exists(output_path): if os.path.exists(output_path):
os.remove(output_path) # 覆盖同名文件 os.remove(output_path) # 覆盖同名文件
@@ -126,10 +498,10 @@ def clean_pressure_data_km(
return os.path.abspath(output_path) return os.path.abspath(output_path)
def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> dict: def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> pd.DataFrame:
""" """
接收一个 DataFrame 数据结构,使用KMeans聚类检测异常并用滚动平均修复 接收一个 DataFrame 数据结构,使用时间感知的稳健修复方法清洗压力数据
返回清洗后的字典数据结构 返回清洗后的 DataFrame
Args: Args:
data: 输入 DataFrame(可包含 time 列) data: 输入 DataFrame(可包含 time 列)
@@ -137,113 +509,37 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
""" """
# 使用传入的 DataFrame # 使用传入的 DataFrame
data = data.copy() data = data.copy()
data = _normalize_time_frame(data)
value_cols, keep_cols = _select_pressure_columns(data)
has_repeated_time = "time" in data.columns and data["time"].duplicated().any()
identifier_col = _detect_long_form_identifier(data, value_cols, keep_cols)
# 补齐时间缺口(如果启用且数据包含 time 列) if identifier_col is not None:
data_filled = fill_time_gaps( data_repaired = _clean_long_form_pressure(
data, time_col="time", freq="1min", short_gap_threshold=10 data,
) value_cols[0],
identifier_col,
keep_cols,
fill_gaps=True,
)
elif has_repeated_time and len(value_cols) == 1:
data_repaired = _clean_repeated_timestamp_pressure(data, value_cols[0], keep_cols)
else:
data_repaired = _clean_snapshot_pressure(data, value_cols, keep_cols, fill_gaps=True)
# 保存 time 列用于最后合并 if show_plot and value_cols:
time_col_series = None plt.rcParams["font.sans-serif"] = ["SimHei"]
if "time" in data_filled.columns: plt.rcParams["axes.unicode_minus"] = False
time_col_series = data_filled["time"] plot_col = value_cols[0]
x = pd.to_datetime(data_repaired["time"], errors="coerce") if "time" in data_repaired.columns else np.arange(len(data_repaired))
# 移除 time 列用于后续清洗 plt.figure(figsize=(12, 6))
data_filled = data_filled.drop(columns=["time"]) plt.plot(x, pd.to_numeric(data_repaired[plot_col], errors="coerce"), label="cleaned")
plt.xlabel("时间" if "time" in data_repaired.columns else "序号")
# 标准化(使用填充后的数据)
data_norm = (data_filled - data_filled.mean()) / data_filled.std()
# 添加:处理标准化后的 NaN(例如,标准差为0的列),防止异常数据,时间段内所有数据都相同导致计算结果为 NaN
imputer = SimpleImputer(
strategy="constant", fill_value=0, keep_empty_features=True
) # 用 0 填充 NaN,包括全 NaN,并保留空特征
data_norm = pd.DataFrame(
imputer.fit_transform(data_norm),
columns=data_norm.columns,
index=data_norm.index,
)
# 聚类与异常检测
k = 3
kmeans = KMeans(n_clusters=k, init="k-means++", n_init=50, random_state=42)
clusters = kmeans.fit_predict(data_norm)
centers = kmeans.cluster_centers_
distances = np.linalg.norm(data_norm.values - centers[clusters], axis=1)
threshold = distances.mean() + 3 * distances.std()
anomaly_pos = np.where(distances > threshold)[0]
anomaly_indices = data_filled.index[anomaly_pos]
anomaly_details = {}
for pos in anomaly_pos:
row_norm = data_norm.iloc[pos]
cluster_idx = clusters[pos]
center = centers[cluster_idx]
diff = abs(row_norm - center)
main_sensor = diff.idxmax()
anomaly_details[data_filled.index[pos]] = main_sensor
# 修复:滚动平均(窗口可调)
data_rolled = data_filled.rolling(window=13, center=True, min_periods=1).mean()
data_repaired = data_filled.copy()
for pos in anomaly_pos:
label = data_filled.index[pos]
sensor = anomaly_details[label]
data_repaired.loc[label, sensor] = data_rolled.loc[label, sensor]
# 可选可视化(使用位置作为 x 轴)
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
if show_plot and len(data.columns) > 0:
n = len(data)
time = np.arange(n)
n_filled = len(data_filled)
time_filled = np.arange(n_filled)
plt.figure(figsize=(12, 8))
for col in data.columns:
plt.plot(
time, data[col].values, marker="o", markersize=3, label=col, alpha=0.5
)
for col in data_filled.columns:
plt.plot(
time_filled,
data_filled[col].values,
marker="x",
markersize=3,
label=f"{col}_filled",
linestyle="--",
)
for pos in anomaly_pos:
sensor = anomaly_details[data_filled.index[pos]]
plt.plot(pos, data_filled.iloc[pos][sensor], "ro", markersize=8)
plt.xlabel("时间点(序号)")
plt.ylabel("压力监测值") plt.ylabel("压力监测值")
plt.title("各传感器折线图(红色标记主要异常点,虚线为0值填充后)") plt.title(f"{plot_col} 清洗结果")
plt.legend() plt.legend()
plt.show() plt.show()
plt.figure(figsize=(12, 8))
for col in data_repaired.columns:
plt.plot(
time_filled, data_repaired[col].values, marker="o", markersize=3, label=col
)
for pos in anomaly_pos:
sensor = anomaly_details[data_filled.index[pos]]
plt.plot(pos, data_repaired.iloc[pos][sensor], "go", markersize=8)
plt.xlabel("时间点(序号)")
plt.ylabel("修复后压力监测值")
plt.title("修复后各传感器折线图(绿色标记修复值)")
plt.legend()
plt.show()
# 将 time 列添加回结果
if time_col_series is not None:
data_repaired.insert(0, "time", time_col_series)
# 返回清洗后的字典
return data_repaired return data_repaired
+8 -6
View File
@@ -122,12 +122,14 @@ def _worker_evaluate(raw_ratios: np.ndarray) -> float:
class LeakageIdentifier: class LeakageIdentifier:
FLOW_UNIT_TO_M3S = { FLOW_UNIT_TO_M3S = {
"m3/s": 1.0, "m3/s": 1.0,
"m3/h": 1.0 / 3600.0, "m³/s": 1.0,
"L/s": 1.0 / 1000.0, "m3/h": 1.0 / 3600.0,
"L/min": 1.0 / 60000.0, "m³/h": 1.0 / 3600.0,
} "L/s": 1.0 / 1000.0,
"L/min": 1.0 / 60000.0,
}
@classmethod @classmethod
def _flow_to_m3s(cls, value: float, unit: str) -> float: def _flow_to_m3s(cls, value: float, unit: str) -> float:
+25 -63
View File
@@ -6,104 +6,66 @@ import sklearn.cluster
import os import os
class QD_KMeans(object): class QD_KMeans(object):
def __init__(self, wn, num_monitors): def __init__(self, wn, num_monitors):
# self.inp = inp # self.inp = inp
self.cluster_num = num_monitors # 聚类中心个数,也即测压点个数 self.cluster_num = num_monitors # 聚类中心个数,也即测压点个数
self.wn=wn self.wn = wn
self.monitor_nodes = [] self.monitor_nodes = []
self.coords = [] self.coords = []
self.junction_nodes = {} # Added missing initialization self.junction_nodes = {} # Added missing initialization
def get_junctions_coordinates(self): def get_junctions_coordinates(self):
for junction_name in self.wn.junction_name_list: for junction_name in self.wn.junction_name_list:
junction = self.wn.get_node(junction_name) junction = self.wn.get_node(junction_name)
self.junction_nodes[junction_name] = junction.coordinates self.junction_nodes[junction_name] = junction.coordinates
self.coords.append(junction.coordinates ) self.coords.append(junction.coordinates)
# print(f"Total junctions: {self.junction_coordinates}") # print(f"Total junctions: {self.junction_coordinates}")
def select_monitoring_points(self): def select_monitoring_points(self):
if not self.coords: # Add check if coordinates are collected if not self.coords: # Add check if coordinates are collected
self.get_junctions_coordinates() self.get_junctions_coordinates()
coords = np.array(self.coords) coords = np.array(self.coords)
coords_normalized = (coords - coords.min(axis=0)) / (coords.max(axis=0) - coords.min(axis=0)) coords_normalized = (coords - coords.min(axis=0)) / (
kmeans = sklearn.cluster.KMeans(n_clusters= self.cluster_num, random_state=42) coords.max(axis=0) - coords.min(axis=0)
kmeans.fit(coords_normalized) )
kmeans = sklearn.cluster.KMeans(n_clusters=self.cluster_num, random_state=42)
kmeans.fit(coords_normalized)
for center in kmeans.cluster_centers_: for center in kmeans.cluster_centers_:
distances = np.sum((coords_normalized - center) ** 2, axis=1) distances = np.sum((coords_normalized - center) ** 2, axis=1)
nearest_node = self.wn.junction_name_list[np.argmin(distances)] nearest_node = self.wn.junction_name_list[np.argmin(distances)]
self.monitor_nodes.append(nearest_node) self.monitor_nodes.append(nearest_node)
return self.monitor_nodes return self.monitor_nodes
def visualize_network(self): def visualize_network(self):
"""Visualize network with monitoring points""" """Visualize network with monitoring points"""
ax=wntr.graphics.plot_network(self.wn, ax = wntr.graphics.plot_network(
node_attribute=self.monitor_nodes, self.wn,
node_size=30, node_attribute=self.monitor_nodes,
title='Optimal sensor') node_size=30,
plt.show() title="Optimal sensor",
)
plt.show()
def kmeans_sensor_placement(name: str, sensor_num: int, min_diameter: int) -> list: def kmeans_sensor_placement(name: str, sensor_num: int, min_diameter: int) -> list:
inp_name = f'./db_inp/{name}.db.inp' inp_name = f"./db_inp/{name}.db.inp"
wn= wntr.network.WaterNetworkModel(inp_name) wn = wntr.network.WaterNetworkModel(inp_name)
wn_cluster=QD_KMeans(wn, sensor_num) wn_cluster = QD_KMeans(wn, sensor_num)
# Select monitoring pointse # Select monitoring pointse
sensor_ids= wn_cluster.select_monitoring_points() sensor_ids = wn_cluster.select_monitoring_points()
# wn_cluster.visualize_network() # wn_cluster.visualize_network()
return sensor_ids return sensor_ids
if __name__ == "__main__": if __name__ == "__main__":
#sensorindex = get_ID(name='suzhouhe_2024_cloud_0817', sensor_num=30, min_diameter=500) # sensorindex = get_ID(name='suzhouhe_2024_cloud_0817', sensor_num=30, min_diameter=500)
sensorindex = kmeans_sensor_placement(name='szh', sensor_num=50, min_diameter=300) sensorindex = kmeans_sensor_placement(name="szh", sensor_num=50, min_diameter=300)
print(sensorindex) print(sensorindex)
+92 -44
View File
@@ -20,6 +20,7 @@ import geopandas as gpd
from sklearn.metrics import pairwise_distances from sklearn.metrics import pairwise_distances
import app.services.project_info as project_info import app.services.project_info as project_info
# 2025/03/12 # 2025/03/12
# Step1: 获取节点坐标 # Step1: 获取节点坐标
def getCoor(wn: wntr.network.WaterNetworkModel) -> pandas.DataFrame: def getCoor(wn: wntr.network.WaterNetworkModel) -> pandas.DataFrame:
@@ -31,7 +32,7 @@ def getCoor(wn: wntr.network.WaterNetworkModel) -> pandas.DataFrame:
# site: pandas.Series # site: pandas.Series
# index:节点名称(wn.node_name_list # index:节点名称(wn.node_name_list
# values:每个节点的坐标,格式为 tuple(如 (x, y) 或 (x, y, z) # values:每个节点的坐标,格式为 tuple(如 (x, y) 或 (x, y, z)
site = wn.query_node_attribute('coordinates') site = wn.query_node_attribute("coordinates")
# Coor: pandas.Series # Coor: pandas.Series
# index:与site相同(节点名称)。 # index:与site相同(节点名称)。
# values:坐标转换为numpy.ndarray(如array([10.5, 20.3]) # values:坐标转换为numpy.ndarray(如array([10.5, 20.3])
@@ -43,9 +44,9 @@ def getCoor(wn: wntr.network.WaterNetworkModel) -> pandas.DataFrame:
x.append(Coor.values[i][0]) # 将 x 坐标存入 x 列表。 x.append(Coor.values[i][0]) # 将 x 坐标存入 x 列表。
y.append(Coor.values[i][1]) # 将 y 坐标存入 y 列表 y.append(Coor.values[i][1]) # 将 y 坐标存入 y 列表
# xy: dict[str, list], x、y 坐标的字典 # xy: dict[str, list], x、y 坐标的字典
xy = {'x': x, 'y': y} xy = {"x": x, "y": y}
# Coor_node: pandas.DataFrame, 存储节点 x, y 坐标的 DataFrame # Coor_node: pandas.DataFrame, 存储节点 x, y 坐标的 DataFrame
Coor_node = pd.DataFrame(xy, index=wn.node_name_list, columns=['x', 'y']) Coor_node = pd.DataFrame(xy, index=wn.node_name_list, columns=["x", "y"])
return Coor_node return Coor_node
@@ -87,23 +88,23 @@ def skater_partition(G, n_clusters):
字典形式的聚类结果,键为区域编号,值为该区域内的节点列表。 字典形式的聚类结果,键为区域编号,值为该区域内的节点列表。
""" """
# 1. 获取所有节点坐标,假设每个节点都有 'pos' 属性 # 1. 获取所有节点坐标,假设每个节点都有 'pos' 属性
pos = nx.get_node_attributes(G, 'pos') pos = nx.get_node_attributes(G, "pos")
nodes = list(G.nodes()) nodes = list(G.nodes())
# 构造坐标数组:每行为 [x, y] # 构造坐标数组:每行为 [x, y]
coords = np.array([pos[node] for node in nodes]) coords = np.array([pos[node] for node in nodes])
# 2. 构造 GeoDataFrame:创建 DataFrame 并生成 geometry 列 # 2. 构造 GeoDataFrame:创建 DataFrame 并生成 geometry 列
df = pd.DataFrame(coords, columns=['x', 'y'], index=nodes) df = pd.DataFrame(coords, columns=["x", "y"], index=nodes)
# 利用 shapely 的 Point 构造空间位置 # 利用 shapely 的 Point 构造空间位置
df['geometry'] = df.apply(lambda row: Point(row['x'], row['y']), axis=1) df["geometry"] = df.apply(lambda row: Point(row["x"], row["y"]), axis=1)
gdf = gpd.GeoDataFrame(df, geometry='geometry') gdf = gpd.GeoDataFrame(df, geometry="geometry")
# 3. 构造空间权重矩阵,使用 4 近邻方法(k=4,可根据实际情况调整) # 3. 构造空间权重矩阵,使用 4 近邻方法(k=4,可根据实际情况调整)
w = ps.weights.KNN.from_array(coords, k=4) w = ps.weights.KNN.from_array(coords, k=4)
w.transform = 'R' w.transform = "R"
# 4. 调用 SKATER:新版本 API 要求传入 gdf, w 以及 attrs_name(这里使用 'x' 和 'y' 作为属性) # 4. 调用 SKATER:新版本 API 要求传入 gdf, w 以及 attrs_name(这里使用 'x' 和 'y' 作为属性)
skater = Skater(gdf, w, attrs_name=['x', 'y'], n_clusters=n_clusters) skater = Skater(gdf, w, attrs_name=["x", "y"], n_clusters=n_clusters)
skater.solve() skater.solve()
# 5. 获取聚类标签,构造成字典格式 # 5. 获取聚类标签,构造成字典格式
@@ -133,24 +134,24 @@ def spectral_partition(G, n_clusters):
键为聚类标签,值为该聚类对应的节点列表。 键为聚类标签,值为该聚类对应的节点列表。
""" """
# 1. 获取节点空间坐标,注意保证每个节点都有 'pos' 属性 # 1. 获取节点空间坐标,注意保证每个节点都有 'pos' 属性
pos_dict = nx.get_node_attributes(G, 'pos') pos_dict = nx.get_node_attributes(G, "pos")
nodes = list(G.nodes()) nodes = list(G.nodes())
coords = np.array([pos_dict[node] for node in nodes]) coords = np.array([pos_dict[node] for node in nodes])
# 2. 计算节点之间的欧氏距离矩阵 # 2. 计算节点之间的欧氏距离矩阵
D = pairwise_distances(coords, metric='euclidean') D = pairwise_distances(coords, metric="euclidean")
# 3. 计算 sigma 值:这里取所有距离的均值,当然也可以根据实际情况调整 # 3. 计算 sigma 值:这里取所有距离的均值,当然也可以根据实际情况调整
sigma = np.mean(D) sigma = np.mean(D)
# 4. 构造相似度矩阵:使用高斯核函数 # 4. 构造相似度矩阵:使用高斯核函数
# A(i, j) = exp( -d(i,j)^2 / (2*sigma^2) ) # A(i, j) = exp( -d(i,j)^2 / (2*sigma^2) )
A = np.exp(- (D ** 2) / (2 * sigma ** 2)) A = np.exp(-(D**2) / (2 * sigma**2))
# 5. 使用谱聚类进行图分区 # 5. 使用谱聚类进行图分区
clustering = SpectralClustering(n_clusters=n_clusters, clustering = SpectralClustering(
affinity='precomputed', n_clusters=n_clusters, affinity="precomputed", random_state=0
random_state=0) )
labels = clustering.fit_predict(A) labels = clustering.fit_predict(A)
# 6. 构造字典形式的分区结果 # 6. 构造字典形式的分区结果
@@ -160,6 +161,7 @@ def spectral_partition(G, n_clusters):
return groups return groups
# 2025/03/12 # 2025/03/12
# Step3: wn_func类,水力计算 # Step3: wn_func类,水力计算
# wn_func 主要用于计算: # wn_func 主要用于计算:
@@ -181,7 +183,7 @@ class wn_func(object):
self.results = wntr.sim.EpanetSimulator(wn).run_sim() # 存储运行结果 self.results = wntr.sim.EpanetSimulator(wn).run_sim() # 存储运行结果
self.wn = wn self.wn = wn
# self.qpandas.DataFrame,管道流量,索引为时间步长,列为管道名称 # self.qpandas.DataFrame,管道流量,索引为时间步长,列为管道名称
self.q = self.results.link['flowrate'] self.q = self.results.link["flowrate"]
# ReservoirIndex / Tankindex: list[str],水库 / 水箱节点名称列表 # ReservoirIndex / Tankindex: list[str],水库 / 水箱节点名称列表
ReservoirIndex = wn.reservoir_name_list ReservoirIndex = wn.reservoir_name_list
Tankindex = wn.tank_name_list Tankindex = wn.tank_name_list
@@ -191,7 +193,7 @@ class wn_func(object):
# self.nodes: list[str],所有节点的名称 # self.nodes: list[str],所有节点的名称
self.nodes = wn.node_name_list self.nodes = wn.node_name_list
# self.coordinatespandas.Series,节点坐标,索引为节点名,值为 (x, y) 坐标的 tuple # self.coordinatespandas.Series,节点坐标,索引为节点名,值为 (x, y) 坐标的 tuple
self.coordinates = wn.query_node_attribute('coordinates') self.coordinates = wn.query_node_attribute("coordinates")
# allpumps / allvalves: list[str],所有泵/阀门名称列表 # allpumps / allvalves: list[str],所有泵/阀门名称列表
allpumps = wn.pump_name_list allpumps = wn.pump_name_list
allvalves = wn.valve_name_list allvalves = wn.valve_name_list
@@ -222,17 +224,27 @@ class wn_func(object):
# 泵的起终点、tank、reservoir # 泵的起终点、tank、reservoir
# self.delnodes: list[str],需要删除的节点(包括水库、泵、阀门连接的节点) # self.delnodes: list[str],需要删除的节点(包括水库、泵、阀门连接的节点)
self.delnodes = list( self.delnodes = list(
set(ReservoirIndex).union(Tankindex, pumpstnode, pumpednode, valvestnode, valveednode, Reservoirednode)) set(ReservoirIndex).union(
Tankindex,
pumpstnode,
pumpednode,
valvestnode,
valveednode,
Reservoirednode,
)
)
# 泵、起终点为tank、reservoir的管道 # 泵、起终点为tank、reservoir的管道
# self.delpipes: list[str],需要删除的管道(包括水库、泵、阀门连接的管道) # self.delpipes: list[str],需要删除的管道(包括水库、泵、阀门连接的管道)
self.delpipes = list(set(wn.pump_name_list).union(wn.valve_name_list).union(Reservoirpipe)) self.delpipes = list(
set(wn.pump_name_list).union(wn.valve_name_list).union(Reservoirpipe)
)
self.pipes = [pipe for pipe in wn.pipe_name_list if pipe not in self.delpipes] self.pipes = [pipe for pipe in wn.pipe_name_list if pipe not in self.delpipes]
# self.L: list[float],所有管道的长度(以米为单位) # self.L: list[float],所有管道的长度(以米为单位)
self.L = wn.query_link_attribute('length')[self.pipes].tolist() self.L = wn.query_link_attribute("length")[self.pipes].tolist()
self.n = len(self.nodes) self.n = len(self.nodes)
self.m = len(self.pipes) self.m = len(self.pipes)
# self.unit_headloss: list[float],单位水头损失(headloss 数据的第一行,单位:米/km) # self.unit_headloss: list[float],单位水头损失(headloss 数据的第一行,单位:米/km)
self.unit_headloss = self.results.link['headloss'].iloc[0, :].tolist() self.unit_headloss = self.results.link["headloss"].iloc[0, :].tolist()
## ##
self.delnodes1 = list(set(ReservoirIndex).union(Tankindex)) self.delnodes1 = list(set(ReservoirIndex).union(Tankindex))
@@ -245,7 +257,9 @@ class wn_func(object):
end_node = wn.links[pipe].end_node.name end_node = wn.links[pipe].end_node.name
self.less_than_min_diameter_junction_list.extend([start_node, end_node]) self.less_than_min_diameter_junction_list.extend([start_node, end_node])
# 去重 # 去重
self.less_than_min_diameter_junction_list = list(set(self.less_than_min_diameter_junction_list)) self.less_than_min_diameter_junction_list = list(
set(self.less_than_min_diameter_junction_list)
)
# Step3.2: 计算水力距离 # Step3.2: 计算水力距离
def CtoS(self): def CtoS(self):
@@ -266,7 +280,7 @@ class wn_func(object):
q = self.q q = self.q
L = self.L L = self.L
# H1pandas.DataFrame,水头数据,索引为时间步长,列为节点名 # H1pandas.DataFrame,水头数据,索引为时间步长,列为节点名
H1 = self.results.node['head'].T H1 = self.results.node["head"].T
# hhlist[float],计算管道两端水头之差 # hhlist[float],计算管道两端水头之差
hh = [] hh = []
# 水头损失 # 水头损失
@@ -280,8 +294,18 @@ class wn_func(object):
# headlosspandas.DataFrame,管道水头损失矩阵 # headlosspandas.DataFrame,管道水头损失矩阵
headloss = pd.DataFrame(hh, index=pipes).T headloss = pd.DataFrame(hh, index=pipes).T
# s1:管道阻力系数,s2:将管道阻力系数与管道的起始节点和终止节点对应 # s1:管道阻力系数,s2:将管道阻力系数与管道的起始节点和终止节点对应
hf = pd.DataFrame(np.array([0] * (n ** 2)).reshape(n, n), index=nodes, columns=nodes, dtype=float) hf = pd.DataFrame(
weightL = pd.DataFrame(np.array([0] * (n ** 2)).reshape(n, n), index=nodes, columns=nodes, dtype=float) np.array([0] * (n**2)).reshape(n, n),
index=nodes,
columns=nodes,
dtype=float,
)
weightL = pd.DataFrame(
np.array([0] * (n**2)).reshape(n, n),
index=nodes,
columns=nodes,
dtype=float,
)
# s2为对应管道起始节点与终止节点的粗糙度系数矩阵,index代表起始节点,columns代表终止节点 # s2为对应管道起始节点与终止节点的粗糙度系数矩阵,index代表起始节点,columns代表终止节点
G = nx.DiGraph() G = nx.DiGraph()
for i in range(0, m): for i in range(0, m):
@@ -298,11 +322,16 @@ class wn_func(object):
weightL.loc[b, a] = headloss.loc[0, pipe] * L[i] weightL.loc[b, a] = headloss.loc[0, pipe] * L[i]
G.add_weighted_edges_from([(b, a, weightL.loc[b, a])]) G.add_weighted_edges_from([(b, a, weightL.loc[b, a])])
hydraulicL = pd.DataFrame(np.array([0] * (n ** 2)).reshape(n, n), index=nodes, columns=nodes, dtype=float) hydraulicL = pd.DataFrame(
np.array([0] * (n**2)).reshape(n, n),
index=nodes,
columns=nodes,
dtype=float,
)
for a in nodes: for a in nodes:
if a in G.nodes: if a in G.nodes:
d = nx.shortest_path_length(G, source=a, weight='weight') d = nx.shortest_path_length(G, source=a, weight="weight")
for b in list(d.keys()): for b in list(d.keys()):
hydraulicL.loc[a, b] = d[b] hydraulicL.loc[a, b] = d[b]
@@ -331,11 +360,17 @@ class wn_func(object):
for t in self.wn.tanks(): for t in self.wn.tanks():
self.nonjunc_index.append(t[0]) self.nonjunc_index.append(t[0])
# Connnumpy.matrix,节点-管道连接矩阵,起点 -1,终点 1 # Connnumpy.matrix,节点-管道连接矩阵,起点 -1,终点 1
Conn = np.mat(np.zeros([n, m - p - v])) # 节点和管道的关系矩阵,行为节点,列为管道,起点为-1,终点为1 Conn = np.mat(
np.zeros([n, m - p - v])
) # 节点和管道的关系矩阵,行为节点,列为管道,起点为-1,终点为1
# NConnnumpy.matrix,节点-节点连接矩阵,有管道相连的地方设为 1 # NConnnumpy.matrix,节点-节点连接矩阵,有管道相连的地方设为 1
NConn = np.mat(np.zeros([n, n])) # 节点之间的关系,之间有管道为1,反之为0 NConn = np.mat(np.zeros([n, n])) # 节点之间的关系,之间有管道为1,反之为0
# pipeslist[str],去除泵和阀门的管道列表 # pipeslist[str],去除泵和阀门的管道列表
pipes = [pipe for pipe in self.wn.pipes() if pipe not in self.wn.pumps() and pipe not in self.wn.valves()] pipes = [
pipe
for pipe in self.wn.pipes()
if pipe not in self.wn.pumps() and pipe not in self.wn.valves()
]
for pipe_name, pipe in pipes: for pipe_name, pipe in pipes:
start = self.wn.node_name_list.index(pipe.start_node_name) start = self.wn.node_name_list.index(pipe.start_node_name)
end = self.wn.node_name_list.index(pipe.end_node_name) end = self.wn.node_name_list.index(pipe.end_node_name)
@@ -345,12 +380,21 @@ class wn_func(object):
NConn[start, end] = 1 NConn[start, end] = 1
NConn[end, start] = 1 NConn[end, start] = 1
self.A = Conn self.A = Conn
link_name_list = [link for link in self.wn.link_name_list if link_name_list = [
link not in self.wn.pump_name_list and link not in self.wn.valve_name_list] link
self.A2 = pd.DataFrame(self.A, index=self.wn.node_name_list, columns=link_name_list) for link in self.wn.link_name_list
if link not in self.wn.pump_name_list
and link not in self.wn.valve_name_list
]
self.A2 = pd.DataFrame(
self.A, index=self.wn.node_name_list, columns=link_name_list
)
self.A2 = self.A2.drop(self.delnodes) self.A2 = self.A2.drop(self.delnodes)
for pipe in self.delpipes: for pipe in self.delpipes:
if pipe not in self.wn.pump_name_list and pipe not in self.wn.valve_name_list: if (
pipe not in self.wn.pump_name_list
and pipe not in self.wn.valve_name_list
):
self.A2 = self.A2.drop(columns=pipe) self.A2 = self.A2.drop(columns=pipe)
self.junc_list = self.A2.index self.junc_list = self.A2.index
self.A2 = np.mat(self.A2) # 节点管道关系 self.A2 = np.mat(self.A2) # 节点管道关系
@@ -372,10 +416,10 @@ class wn_func(object):
except EpanetException: except EpanetException:
pass pass
finally: finally:
h = result.link['headloss'][self.pipes].values[0] h = result.link["headloss"][self.pipes].values[0]
q = result.link['flowrate'][self.pipes].values[0] q = result.link["flowrate"][self.pipes].values[0]
l = self.wn.query_link_attribute('length')[self.pipes] l = self.wn.query_link_attribute("length")[self.pipes]
C = self.wn.query_link_attribute('roughness')[self.pipes] C = self.wn.query_link_attribute("roughness")[self.pipes]
# headlossnumpy.ndarray,水头损失数组 # headlossnumpy.ndarray,水头损失数组
headloss = np.array(h) headloss = np.array(h)
# 调整流量方向 # 调整流量方向
@@ -393,7 +437,7 @@ class wn_func(object):
try: try:
det = np.linalg.det(X) det = np.linalg.det(X)
except RuntimeError as e: except RuntimeError as e:
sign, logdet = slogdet(X) # 防止溢出 sign, logdet = slogdet(X) # 防止溢出
det = sign * np.exp(logdet) det = sign * np.exp(logdet)
if det != 0: if det != 0:
J_H_Cw = X.I * A * S J_H_Cw = X.I * A * S
@@ -430,7 +474,10 @@ class Sensorplacement(wn_func):
""" """
Sensorplacement 类继承了 wn_func 类,并且用于计算和优化传感器布置的位置。 Sensorplacement 类继承了 wn_func 类,并且用于计算和优化传感器布置的位置。
""" """
def __init__(self, wn: wntr.network.WaterNetworkModel, sensornum: int, min_diameter: int):
def __init__(
self, wn: wntr.network.WaterNetworkModel, sensornum: int, min_diameter: int
):
""" """
:param wn: 由wntr生成的模型 :param wn: 由wntr生成的模型
@@ -442,7 +489,9 @@ class Sensorplacement(wn_func):
# 1.某个节点到所有节点的加权距离之和 # 1.某个节点到所有节点的加权距离之和
# 2.某个节点到该组内所有节点的加权距离之和 # 2.某个节点到该组内所有节点的加权距离之和
def sensor(self, SS: pandas.DataFrame, G: networkx.Graph, group: dict[int, list[str]]): def sensor(
self, SS: pandas.DataFrame, G: networkx.Graph, group: dict[int, list[str]]
):
""" """
sensor 方法是用来根据灵敏度矩阵 SS 和加权图 G 来确定传感器布置位置的 sensor 方法是用来根据灵敏度矩阵 SS 和加权图 G 来确定传感器布置位置的
:param SS: 灵敏度矩阵,每个节点的行和列代表不同节点,矩阵元素表示节点间的灵敏度。SS.iloc[i, :] 表示第 i 行对应节点 i 到所有其他节点的灵敏度 :param SS: 灵敏度矩阵,每个节点的行和列代表不同节点,矩阵元素表示节点间的灵敏度。SS.iloc[i, :] 表示第 i 行对应节点 i 到所有其他节点的灵敏度
@@ -527,7 +576,7 @@ def get_ID(name: str, sensor_num: int, min_diameter: int) -> list[str]:
:return: 测压点节点ID :return: 测压点节点ID
""" """
# inp_file_realstr,输入文件名,表示原始水力模型文件的路径,该文件格式为 EPANET 输入文件(.inp),包含管网的结构信息、节点、管道、泵等数据 # inp_file_realstr,输入文件名,表示原始水力模型文件的路径,该文件格式为 EPANET 输入文件(.inp),包含管网的结构信息、节点、管道、泵等数据
inp_file_real = f'./db_inp/{name}.db.inp' inp_file_real = f"./db_inp/{name}.db.inp"
# sensornumint,需要布置的传感器数量 # sensornumint,需要布置的传感器数量
# sensornum = sensor_num # sensornum = sensor_num
# wn_realwntr.network.WaterNetworkModel,加载 EPANET 水力模型 # wn_realwntr.network.WaterNetworkModel,加载 EPANET 水力模型
@@ -538,7 +587,7 @@ def get_ID(name: str, sensor_num: int, min_diameter: int) -> list[str]:
results_real = sim_real.run_sim() results_real = sim_real.run_sim()
# real_Clist[float],包含所有管道粗糙度的列表 # real_Clist[float],包含所有管道粗糙度的列表
real_C = wn_real.query_link_attribute('roughness').tolist() real_C = wn_real.query_link_attribute("roughness").tolist()
# wn_fun1wn_func(继承自 object),创建 wn_func 类的实例,传入 wn_real 水力模型对象。wn_func 用于计算管网相关的水力属性,比如水力距离、灵敏度等 # wn_fun1wn_func(继承自 object),创建 wn_func 类的实例,传入 wn_real 水力模型对象。wn_func 用于计算管网相关的水力属性,比如水力距离、灵敏度等
wn_fun1 = wn_func(wn_real, min_diameter=min_diameter) wn_fun1 = wn_func(wn_real, min_diameter=min_diameter)
# nodeslist[str],管网的节点名称列表 # nodeslist[str],管网的节点名称列表
@@ -598,7 +647,6 @@ def get_ID(name: str, sensor_num: int, min_diameter: int) -> list[str]:
sensorindex, sensorindex_2 = wn_fun.sensor(SS, G, group) # 初始的sensorindex sensorindex, sensorindex_2 = wn_fun.sensor(SS, G, group) # 初始的sensorindex
# print(str(sensor_num), "个测压点,测压点位置:", sensorindex) # print(str(sensor_num), "个测压点,测压点位置:", sensorindex)
# 重新打开数据库 # 重新打开数据库
# if is_project_open(name=name): # if is_project_open(name=name):
# close_project(name=name) # close_project(name=name)
@@ -637,7 +685,7 @@ def get_ID(name: str, sensor_num: int, min_diameter: int) -> list[str]:
return sensorindex return sensorindex
if __name__ == '__main__': if __name__ == "__main__":
sensorindex = get_ID(name=project_info.name, sensor_num=20, min_diameter=300) sensorindex = get_ID(name=project_info.name, sensor_num=20, min_diameter=300)
print(sensorindex) print(sensorindex)
+3 -12
View File
@@ -29,6 +29,7 @@ import pytz
import requests import requests
import time import time
import app.services.project_info as project_info import app.services.project_info as project_info
from app.services.time_api import parse_clock_duration_seconds
url_path = 'http://10.101.15.16:9000/loong' # 内网 url_path = 'http://10.101.15.16:9000/loong' # 内网
# url_path = 'http://183.64.62.100:9057/loong' # 外网 # url_path = 'http://183.64.62.100:9057/loong' # 外网
@@ -551,21 +552,11 @@ def from_clock_to_seconds (clock: str)->int:
return hr*3600+mnt*60+seconds return hr*3600+mnt*60+seconds
def from_clock_to_seconds_2 (clock: str)->int: def from_clock_to_seconds_2 (clock: str)->int:
str_format="%H:%M:%S" return parse_clock_duration_seconds(clock)
dt=datetime.strptime(clock,str_format)
hr=dt.hour
mnt=dt.minute
seconds=dt.second
return hr*3600+mnt*60+seconds
def from_clock_to_seconds_3 (clock: str)->int: def from_clock_to_seconds_3 (clock: str)->int:
str_format = "%H:%M" # 更新时间格式以适应 "小时:分钟" 格式 return parse_clock_duration_seconds(clock)
dt = datetime.strptime(clock,str_format)
hr = dt.hour
mnt = dt.minute
seconds = dt.second
return hr * 3600 + mnt * 60
###convert datetimestring ###convert datetimestring
+22 -11
View File
@@ -9,7 +9,6 @@ from app.algorithms.simulation.runner import (
run_simulation_ex, run_simulation_ex,
from_clock_to_seconds_2, from_clock_to_seconds_2,
) )
from app.infra.epanet.epanet import Output
from app.services.scheme_management import store_scheme_info from app.services.scheme_management import store_scheme_info
from app.services.tjnetwork import ( from app.services.tjnetwork import (
ChangeSet, ChangeSet,
@@ -73,6 +72,7 @@ def burst_analysis(
modify_variable_pump_pattern: dict[str, list] = None, modify_variable_pump_pattern: dict[str, list] = None,
modify_valve_opening: dict[str, float] = None, modify_valve_opening: dict[str, float] = None,
scheme_name: str = None, scheme_name: str = None,
username: str | None = None,
) -> None: ) -> None:
""" """
爆管模拟 爆管模拟
@@ -87,6 +87,9 @@ def burst_analysis(
:param scheme_name: 方案名称 :param scheme_name: 方案名称
:return: :return:
""" """
if not username:
raise ValueError("username is required when storing burst analysis scheme")
scheme_detail: dict = { scheme_detail: dict = {
"burst_ID": burst_ID, "burst_ID": burst_ID,
"burst_size": burst_size, "burst_size": burst_size,
@@ -212,7 +215,7 @@ def burst_analysis(
name=name, name=name,
scheme_name=scheme_name, scheme_name=scheme_name,
scheme_type="burst_analysis", scheme_type="burst_analysis",
username="admin", username=username,
scheme_start_time=modify_pattern_start_time, scheme_start_time=modify_pattern_start_time,
scheme_detail=scheme_detail, scheme_detail=scheme_detail,
) )
@@ -312,6 +315,7 @@ def flushing_analysis(
drainage_node_ID: str = None, drainage_node_ID: str = None,
flushing_flow: float = 0, flushing_flow: float = 0,
scheme_name: str = None, scheme_name: str = None,
username: str | None = None,
) -> None: ) -> None:
""" """
管道冲洗模拟 管道冲洗模拟
@@ -324,6 +328,9 @@ def flushing_analysis(
:param scheme_name: 方案名称 :param scheme_name: 方案名称
:return: :return:
""" """
if not username:
raise ValueError("username is required when storing flushing analysis scheme")
scheme_detail: dict = { scheme_detail: dict = {
"duration": modify_total_duration, "duration": modify_total_duration,
"valve_opening": modify_valve_opening, "valve_opening": modify_valve_opening,
@@ -456,7 +463,7 @@ def flushing_analysis(
name=name, name=name,
scheme_name=scheme_name, scheme_name=scheme_name,
scheme_type="flushing_analysis", scheme_type="flushing_analysis",
username="admin", username=username,
scheme_start_time=modify_pattern_start_time, scheme_start_time=modify_pattern_start_time,
scheme_detail=scheme_detail, scheme_detail=scheme_detail,
) )
@@ -474,6 +481,7 @@ def contaminant_simulation(
concentration: float, # 污染源浓度,单位mg/L concentration: float, # 污染源浓度,单位mg/L
scheme_name: str = None, scheme_name: str = None,
source_pattern: str = None, # 污染源时间变化模式名称 source_pattern: str = None, # 污染源时间变化模式名称
username: str | None = None,
) -> None: ) -> None:
""" """
污染模拟 污染模拟
@@ -487,6 +495,9 @@ def contaminant_simulation(
:param scheme_name: 方案名称 :param scheme_name: 方案名称
:return: :return:
""" """
if not username:
raise ValueError("username is required when storing contaminant analysis scheme")
scheme_detail: dict = { scheme_detail: dict = {
"source": source, "source": source,
"concentration": concentration, "concentration": concentration,
@@ -609,7 +620,7 @@ def contaminant_simulation(
name=name, name=name,
scheme_name=scheme_name, scheme_name=scheme_name,
scheme_type="contaminant_analysis", scheme_type="contaminant_analysis",
username="admin", username=username,
scheme_start_time=modify_pattern_start_time, scheme_start_time=modify_pattern_start_time,
scheme_detail=scheme_detail, scheme_detail=scheme_detail,
) )
@@ -663,24 +674,24 @@ def age_analysis(
new_name, new_name,
"realtime", "realtime",
modify_pattern_start_time, modify_pattern_start_time,
modify_total_duration, duration=modify_total_duration,
downloading_prohibition=True, downloading_prohibition=True,
) )
simulation_result = json.loads(result)
output_data = simulation_result.get("output")
if not isinstance(output_data, dict):
raise RuntimeError("run_simulation_ex did not return JSON output content")
# step 2. restore the base model status # step 2. restore the base model status
# execute_undo(name) #有疑惑 # execute_undo(name) #有疑惑
if is_project_open(new_name): if is_project_open(new_name):
close_project(new_name) close_project(new_name)
delete_project(new_name) delete_project(new_name)
output = Output("./temp/{}.db.out".format(new_name))
# element_name = output.element_name()
# node_name = element_name['nodes']
# link_name = element_name['links']
nodes_age = [] nodes_age = []
node_result = output.node_results() node_result = output_data.get("node_results") or []
for node in node_result: for node in node_result:
nodes_age.append(node["result"][-1]["quality"]) nodes_age.append(node["result"][-1]["quality"])
links_age = [] links_age = []
link_result = output.link_results() link_result = output_data.get("link_results") or []
for link in link_result: for link in link_result:
links_age.append(link["result"][-1]["quality"]) links_age.append(link["result"][-1]["quality"])
age_result = {"nodes": nodes_age, "links": links_age} age_result = {"nodes": nodes_age, "links": links_age}
+695
View File
@@ -0,0 +1,695 @@
from typing import List
from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, Path, Query, Response, status
from sqlalchemy import text
from sqlalchemy.engine.url import make_url
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.ext.asyncio import create_async_engine
from app.auth.metadata_dependencies import (
get_current_metadata_admin,
get_metadata_repository,
)
from app.core.audit import AuditAction, log_audit_event
from app.domain.schemas.admin_metadata import (
AdminProjectCreateRequest,
AdminProjectResponse,
AdminProjectUpdateRequest,
MetadataUsersBatchSyncRequest,
MetadataUserResponse,
MetadataUserSyncRequest,
MetadataUserSyncResult,
MetadataUserUpdateRequest,
ProjectDatabaseHealthResponse,
ProjectDatabaseHealthRequest,
ProjectDatabaseResponse,
ProjectDatabaseUpsertRequest,
ProjectDbRole,
ProjectMemberCreateRequest,
ProjectMemberResponse,
ProjectMemberUpdateRequest,
)
from app.infra.db.metadb import models
from app.infra.db.metadb.repositories.metadata_repository import (
MetadataRepository,
ProjectDbRouting,
)
router = APIRouter()
def _project_response(project: models.Project) -> AdminProjectResponse:
return AdminProjectResponse(
project_id=project.id,
name=project.name,
code=project.code,
description=project.description,
gs_workspace=project.gs_workspace,
map_extent=project.map_extent,
status=project.status,
created_at=project.created_at,
updated_at=project.updated_at,
)
def _project_database_response(
record: models.ProjectDatabase,
) -> ProjectDatabaseResponse:
return ProjectDatabaseResponse(
id=record.id,
project_id=record.project_id,
db_role=record.db_role,
db_type=record.db_type,
pool_min_size=record.pool_min_size,
pool_max_size=record.pool_max_size,
has_dsn=bool(record.dsn_encrypted),
)
def _database_audit_payload(payload: ProjectDatabaseUpsertRequest) -> dict:
return {
"db_role": payload.db_role,
"db_type": _db_type_for_role(payload.db_role),
"pool_min_size": payload.pool_min_size,
"pool_max_size": payload.pool_max_size,
"dsn_updated": payload.dsn is not None,
}
def _to_async_sqlalchemy_url(dsn: str) -> str:
parsed = make_url(dsn)
if parsed.drivername in {"postgresql", "postgres"}:
parsed = parsed.set(drivername="postgresql+psycopg")
return parsed.render_as_string(hide_password=False)
def _db_type_for_role(db_role: str) -> str:
if db_role == "iot_data":
return "timescaledb"
return "postgresql"
def _status_for_config_value_error(exc: ValueError) -> int:
if "DATABASE_ENCRYPTION_KEY" in str(exc):
return status.HTTP_503_SERVICE_UNAVAILABLE
return status.HTTP_400_BAD_REQUEST
async def _check_database_connection(routing: ProjectDbRouting) -> None:
engine = create_async_engine(
_to_async_sqlalchemy_url(routing.dsn),
pool_size=1,
max_overflow=0,
pool_pre_ping=True,
)
try:
async with engine.connect() as conn:
await conn.execute(text("SELECT 1"))
finally:
await engine.dispose()
def _database_health_error_detail(exc: Exception) -> str:
message = str(exc)
lower_message = message.lower()
if "password authentication failed" in lower_message:
return "连通性测试失败:用户名或密码错误,请检查 DSN 中的账号密码。"
if "connection refused" in lower_message:
return "连通性测试失败:目标主机或端口拒绝连接,请检查地址、端口和服务状态。"
if "timeout" in lower_message or "timed out" in lower_message:
return "连通性测试失败:连接超时,请检查网络、防火墙和数据库服务状态。"
if "could not translate host name" in lower_message or "name or service not known" in lower_message:
return "连通性测试失败:数据库主机名无法解析,请检查 DSN 中的主机地址。"
first_line = message.splitlines()[0] if message else exc.__class__.__name__
return f"连通性测试失败:{first_line}"
async def _upsert_and_audit_metadata_user(
payload: MetadataUserSyncRequest,
*,
current_user,
metadata_repo: MetadataRepository,
response_status: int,
) -> MetadataUserResponse:
user = await metadata_repo.upsert_user_from_keycloak(
keycloak_id=payload.keycloak_id,
username=payload.username,
email=str(payload.email),
role=payload.role,
is_active=payload.is_active,
)
await log_audit_event(
action=AuditAction.UPDATE,
user_id=current_user.id,
resource_type="metadata_user",
resource_id=str(user.id),
request_data=payload.model_dump(mode="json"),
response_status=response_status,
session=metadata_repo.session,
)
return MetadataUserResponse.model_validate(user)
@router.get("/me", response_model=MetadataUserResponse)
async def get_metadata_admin_me(
current_user=Depends(get_current_metadata_admin),
) -> MetadataUserResponse:
return MetadataUserResponse.model_validate(current_user)
@router.post("/users/sync", response_model=MetadataUserResponse)
async def sync_metadata_user(
payload: MetadataUserSyncRequest,
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> MetadataUserResponse:
try:
return await _upsert_and_audit_metadata_user(
payload,
current_user=current_user,
metadata_repo=metadata_repo,
response_status=status.HTTP_200_OK,
)
except IntegrityError as exc:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="User keycloak_id, username, or email conflicts with an existing user",
) from exc
except SQLAlchemyError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Metadata database error: {exc}",
) from exc
@router.post("/users/sync/batch", response_model=List[MetadataUserSyncResult])
async def sync_metadata_users_batch(
payload: MetadataUsersBatchSyncRequest,
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> List[MetadataUserSyncResult]:
results: list[MetadataUserSyncResult] = []
for item in payload.users:
try:
user = await _upsert_and_audit_metadata_user(
item,
current_user=current_user,
metadata_repo=metadata_repo,
response_status=status.HTTP_200_OK,
)
except IntegrityError as exc:
results.append(
MetadataUserSyncResult(
keycloak_id=item.keycloak_id,
success=False,
error="User keycloak_id, username, or email conflicts with an existing user",
)
)
await metadata_repo.session.rollback()
except SQLAlchemyError as exc:
results.append(
MetadataUserSyncResult(
keycloak_id=item.keycloak_id,
success=False,
error=f"Metadata database error: {exc}",
)
)
await metadata_repo.session.rollback()
else:
results.append(
MetadataUserSyncResult(
keycloak_id=item.keycloak_id,
success=True,
user=user,
)
)
return results
@router.get("/users", response_model=List[MetadataUserResponse])
async def list_metadata_users(
skip: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=1000),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> List[MetadataUserResponse]:
users = await metadata_repo.list_users(skip=skip, limit=limit)
return [MetadataUserResponse.model_validate(user) for user in users]
@router.get("/projects", response_model=List[AdminProjectResponse])
async def list_admin_projects(
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> List[AdminProjectResponse]:
projects = await metadata_repo.list_project_records()
return [_project_response(project) for project in projects]
@router.post(
"/projects",
response_model=AdminProjectResponse,
status_code=status.HTTP_201_CREATED,
)
async def create_admin_project(
payload: AdminProjectCreateRequest,
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> AdminProjectResponse:
try:
project = await metadata_repo.create_project(
name=payload.name,
code=payload.code,
description=payload.description,
gs_workspace=payload.gs_workspace,
map_extent=payload.map_extent,
status=payload.status,
)
except IntegrityError as exc:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Project code or workspace conflicts with an existing project",
) from exc
except SQLAlchemyError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Metadata database error: {exc}",
) from exc
await log_audit_event(
action=AuditAction.CREATE,
user_id=current_user.id,
project_id=project.id,
resource_type="project",
resource_id=str(project.id),
request_data=payload.model_dump(mode="json"),
response_status=status.HTTP_201_CREATED,
session=metadata_repo.session,
)
return _project_response(project)
@router.patch(
"/projects/{project_id}",
response_model=AdminProjectResponse,
)
async def update_admin_project(
payload: AdminProjectUpdateRequest,
project_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> AdminProjectResponse:
updates = payload.model_dump(mode="json", exclude_unset=True)
try:
project = await metadata_repo.update_project(project_id, updates=updates)
except IntegrityError as exc:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Project code or workspace conflicts with an existing project",
) from exc
except SQLAlchemyError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Metadata database error: {exc}",
) from exc
if project is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
await log_audit_event(
action=AuditAction.UPDATE,
user_id=current_user.id,
project_id=project.id,
resource_type="project",
resource_id=str(project.id),
request_data=updates,
response_status=status.HTTP_200_OK,
session=metadata_repo.session,
)
return _project_response(project)
@router.get(
"/projects/{project_id}/databases",
response_model=List[ProjectDatabaseResponse],
)
async def list_project_databases(
project_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> List[ProjectDatabaseResponse]:
project = await metadata_repo.get_project_by_id(project_id)
if project is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
records = await metadata_repo.list_project_databases(project_id)
return [_project_database_response(record) for record in records]
@router.put(
"/projects/{project_id}/databases",
response_model=ProjectDatabaseResponse,
)
async def upsert_project_database(
payload: ProjectDatabaseUpsertRequest,
project_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> ProjectDatabaseResponse:
project = await metadata_repo.get_project_by_id(project_id)
if project is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
try:
routing = (
ProjectDbRouting(
project_id=project_id,
db_role=payload.db_role,
db_type=_db_type_for_role(payload.db_role),
dsn=payload.dsn,
pool_min_size=payload.pool_min_size,
pool_max_size=payload.pool_max_size,
)
if payload.dsn
else await metadata_repo.get_project_db_routing(project_id, payload.db_role)
)
if routing is None:
raise ValueError("dsn is required when creating project database config")
await _check_database_connection(routing)
except ValueError as exc:
raise HTTPException(
status_code=_status_for_config_value_error(exc),
detail=str(exc),
) from exc
except Exception as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=_database_health_error_detail(exc),
) from exc
try:
record = await metadata_repo.upsert_project_database_config(
project_id,
db_role=payload.db_role,
db_type=_db_type_for_role(payload.db_role),
dsn=payload.dsn,
pool_min_size=payload.pool_min_size,
pool_max_size=payload.pool_max_size,
)
except IntegrityError as exc:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Project database role conflicts with an existing config",
) from exc
except SQLAlchemyError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Metadata database error: {exc}",
) from exc
await log_audit_event(
action=AuditAction.CONFIG_CHANGE,
user_id=current_user.id,
project_id=project_id,
resource_type="project_database",
resource_id=payload.db_role,
request_data=_database_audit_payload(payload),
response_status=status.HTTP_200_OK,
session=metadata_repo.session,
)
return _project_database_response(record)
@router.delete(
"/projects/{project_id}/databases/{db_role}",
status_code=status.HTTP_204_NO_CONTENT,
)
async def delete_project_database(
project_id: UUID = Path(...),
db_role: ProjectDbRole = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> None:
removed = await metadata_repo.delete_project_database_config(project_id, db_role)
if not removed:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project database config not found",
)
await log_audit_event(
action=AuditAction.CONFIG_CHANGE,
user_id=current_user.id,
project_id=project_id,
resource_type="project_database",
resource_id=db_role,
request_data={"deleted": True},
response_status=status.HTTP_204_NO_CONTENT,
session=metadata_repo.session,
)
@router.post(
"/projects/{project_id}/databases/{db_role}/health",
response_model=ProjectDatabaseHealthResponse,
)
async def check_project_database_health(
response: Response,
project_id: UUID = Path(...),
db_role: ProjectDbRole = Path(...),
payload: ProjectDatabaseHealthRequest | None = None,
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> ProjectDatabaseHealthResponse:
dsn_to_test = payload.dsn if payload and payload.dsn else None
if dsn_to_test:
routing = ProjectDbRouting(
project_id=project_id,
db_role=db_role,
db_type=_db_type_for_role(db_role),
dsn=dsn_to_test,
pool_min_size=1,
pool_max_size=1,
)
else:
try:
routing = await metadata_repo.get_project_db_routing(project_id, db_role)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Project database routing DSN is invalid: {exc}",
) from exc
if routing is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project database config not found",
)
try:
await _check_database_connection(routing)
except Exception as exc: # health endpoint should return diagnostic status
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return ProjectDatabaseHealthResponse(
project_id=project_id,
db_role=db_role,
db_type=routing.db_type,
ok=False,
detail=_database_health_error_detail(exc),
)
return ProjectDatabaseHealthResponse(
project_id=project_id,
db_role=db_role,
db_type=routing.db_type,
ok=True,
detail="连通性测试通过",
)
@router.get("/users/{user_id}", response_model=MetadataUserResponse)
async def get_metadata_user(
user_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> MetadataUserResponse:
user = await metadata_repo.get_user_by_id(user_id)
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
return MetadataUserResponse.model_validate(user)
@router.patch("/users/{user_id}", response_model=MetadataUserResponse)
async def update_metadata_user(
payload: MetadataUserUpdateRequest,
user_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> MetadataUserResponse:
updates = payload.model_dump(mode="json", exclude_unset=True)
if user_id == current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Users cannot modify themselves",
)
user = await metadata_repo.update_user_admin(
user_id,
updates=updates,
)
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
await log_audit_event(
action=AuditAction.UPDATE,
user_id=current_user.id,
resource_type="metadata_user",
resource_id=str(user.id),
request_data=updates,
response_status=status.HTTP_200_OK,
session=metadata_repo.session,
)
return MetadataUserResponse.model_validate(user)
@router.get(
"/projects/{project_id}/members",
response_model=List[ProjectMemberResponse],
)
async def list_project_members(
project_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> List[ProjectMemberResponse]:
project = await metadata_repo.get_project_by_id(project_id)
if project is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Project not found"
)
members = await metadata_repo.list_project_members(project_id)
return [ProjectMemberResponse(**member.__dict__) for member in members]
@router.post(
"/projects/{project_id}/members",
response_model=ProjectMemberResponse,
status_code=status.HTTP_201_CREATED,
)
async def add_project_member(
payload: ProjectMemberCreateRequest,
project_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> ProjectMemberResponse:
if payload.user_id == current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Users cannot modify their own project membership",
)
project = await metadata_repo.get_project_by_id(project_id)
if project is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Project not found"
)
user = await metadata_repo.get_user_by_id(payload.user_id)
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
existing = await metadata_repo.get_project_membership(project_id, payload.user_id)
if existing is not None:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="User is already a project member",
)
membership = await metadata_repo.add_project_member(
project_id, payload.user_id, payload.project_role
)
await log_audit_event(
action=AuditAction.PERMISSION_CHANGE,
user_id=current_user.id,
project_id=project_id,
resource_type="project_member",
resource_id=str(payload.user_id),
request_data=payload.model_dump(mode="json"),
response_status=status.HTTP_201_CREATED,
session=metadata_repo.session,
)
return ProjectMemberResponse(
id=membership.id,
user_id=membership.user_id,
project_id=membership.project_id,
project_role=membership.project_role,
username=user.username,
email=user.email,
is_active=user.is_active,
)
@router.patch(
"/projects/{project_id}/members/{user_id}",
response_model=ProjectMemberResponse,
)
async def update_project_member(
payload: ProjectMemberUpdateRequest,
project_id: UUID = Path(...),
user_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> ProjectMemberResponse:
if user_id == current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Users cannot modify their own project membership",
)
user = await metadata_repo.get_user_by_id(user_id)
if user is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
membership = await metadata_repo.update_project_member_role(
project_id, user_id, payload.project_role
)
if membership is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Project member not found"
)
await log_audit_event(
action=AuditAction.PERMISSION_CHANGE,
user_id=current_user.id,
project_id=project_id,
resource_type="project_member",
resource_id=str(user_id),
request_data=payload.model_dump(mode="json"),
response_status=status.HTTP_200_OK,
session=metadata_repo.session,
)
return ProjectMemberResponse(
id=membership.id,
user_id=membership.user_id,
project_id=membership.project_id,
project_role=membership.project_role,
username=user.username,
email=user.email,
is_active=user.is_active,
)
@router.delete("/projects/{project_id}/members/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
async def remove_project_member(
project_id: UUID = Path(...),
user_id: UUID = Path(...),
current_user=Depends(get_current_metadata_admin),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
) -> None:
if user_id == current_user.id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Users cannot modify their own project membership",
)
removed = await metadata_repo.remove_project_member(project_id, user_id)
if not removed:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Project member not found"
)
await log_audit_event(
action=AuditAction.PERMISSION_CHANGE,
user_id=current_user.id,
project_id=project_id,
resource_type="project_member",
resource_id=str(user_id),
response_status=status.HTTP_204_NO_CONTENT,
session=metadata_repo.session,
)
+50
View File
@@ -0,0 +1,50 @@
from datetime import datetime, timezone
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from app.auth.keycloak_dependencies import get_current_keycloak_payload
from app.auth.metadata_dependencies import get_current_metadata_user
from app.auth.project_dependencies import (
ProjectContext,
get_project_context,
)
router = APIRouter()
class AgentAuthContextResponse(BaseModel):
user_id: str
keycloak_sub: str
username: str
role: str
is_superuser: bool
project_id: str
network: str
project_role: str
token_expires_at: str | None = None
@router.get("/agent/auth/context", response_model=AgentAuthContextResponse)
async def get_agent_auth_context(
ctx: ProjectContext = Depends(get_project_context),
current_user=Depends(get_current_metadata_user),
keycloak_payload: dict = Depends(get_current_keycloak_payload),
) -> AgentAuthContextResponse:
exp = keycloak_payload.get("exp")
token_expires_at = (
datetime.fromtimestamp(exp, tz=timezone.utc).isoformat()
if isinstance(exp, int)
else None
)
return AgentAuthContextResponse(
user_id=str(current_user.id),
keycloak_sub=str(current_user.keycloak_id),
username=current_user.username,
role=current_user.role,
is_superuser=current_user.is_superuser,
project_id=str(ctx.project_id),
network=ctx.project_code,
project_role=ctx.project_role,
token_expires_at=token_expires_at,
)
+37 -16
View File
@@ -3,28 +3,36 @@
仅管理员可访问 仅管理员可访问
""" """
from typing import List, Optional from typing import List, Optional
from uuid import UUID from uuid import UUID
from datetime import datetime from datetime import datetime
from fastapi import APIRouter, Depends, Query from fastapi import APIRouter, Depends, Query, Path
from app.domain.schemas.audit import AuditLogResponse from app.domain.schemas.audit import AuditLogResponse
from app.infra.repositories.audit_repository import AuditRepository from app.infra.db.metadb.repositories.audit_repository import AuditRepository
from app.auth.metadata_dependencies import ( from app.auth.metadata_dependencies import (
get_current_metadata_admin, get_current_metadata_admin,
get_current_metadata_user, get_current_metadata_user,
) )
from app.infra.db.metadata.database import get_metadata_session from app.infra.db.metadb.database import get_metadata_session
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
router = APIRouter() router = APIRouter()
async def get_audit_repository( async def get_audit_repository(
session: AsyncSession = Depends(get_metadata_session), session: AsyncSession = Depends(get_metadata_session),
) -> AuditRepository: ) -> AuditRepository:
"""获取审计日志仓储""" """获取审计日志仓储"""
return AuditRepository(session) return AuditRepository(session)
@router.get("/logs", response_model=List[AuditLogResponse])
@router.get(
"/logs",
summary="查询审计日志",
description="查询审计日志(仅管理员)",
response_model=List[AuditLogResponse],
)
async def get_audit_logs( async def get_audit_logs(
user_id: Optional[UUID] = Query(None, description="按用户ID过滤"), user_id: Optional[UUID] = Query(None, description="按用户ID过滤"),
project_id: Optional[UUID] = Query(None, description="按项目ID过滤"), project_id: Optional[UUID] = Query(None, description="按项目ID过滤"),
@@ -38,9 +46,9 @@ async def get_audit_logs(
audit_repo: AuditRepository = Depends(get_audit_repository), audit_repo: AuditRepository = Depends(get_audit_repository),
) -> List[AuditLogResponse]: ) -> List[AuditLogResponse]:
""" """
查询审计日志(仅管理员) 查询审计日志
支持按用户、时间、操作类型等条件过滤 支持按用户、时间、操作类型等条件过滤,仅管理员可访问
""" """
logs = await audit_repo.get_logs( logs = await audit_repo.get_logs(
user_id=user_id, user_id=user_id,
@@ -50,11 +58,16 @@ async def get_audit_logs(
start_time=start_time, start_time=start_time,
end_time=end_time, end_time=end_time,
skip=skip, skip=skip,
limit=limit limit=limit,
) )
return logs return logs
@router.get("/logs/count")
@router.get(
"/logs/count",
summary="获取审计日志总数",
description="获取审计日志总数(仅管理员)",
)
async def get_audit_logs_count( async def get_audit_logs_count(
user_id: Optional[UUID] = Query(None, description="按用户ID过滤"), user_id: Optional[UUID] = Query(None, description="按用户ID过滤"),
project_id: Optional[UUID] = Query(None, description="按项目ID过滤"), project_id: Optional[UUID] = Query(None, description="按项目ID过滤"),
@@ -66,7 +79,9 @@ async def get_audit_logs_count(
audit_repo: AuditRepository = Depends(get_audit_repository), audit_repo: AuditRepository = Depends(get_audit_repository),
) -> dict: ) -> dict:
""" """
获取审计日志总数(仅管理员) 获取审计日志总数
获取符合条件的审计日志的总数,仅管理员可访问
""" """
count = await audit_repo.get_log_count( count = await audit_repo.get_log_count(
user_id=user_id, user_id=user_id,
@@ -74,23 +89,29 @@ async def get_audit_logs_count(
action=action, action=action,
resource_type=resource_type, resource_type=resource_type,
start_time=start_time, start_time=start_time,
end_time=end_time end_time=end_time,
) )
return {"count": count} return {"count": count}
@router.get("/logs/my", response_model=List[AuditLogResponse])
@router.get(
"/logs/my",
summary="查询我的审计日志",
description="查询当前用户的审计日志",
response_model=List[AuditLogResponse],
)
async def get_my_audit_logs( async def get_my_audit_logs(
action: Optional[str] = Query(None, description="按操作类型过滤"), action: Optional[str] = Query(None, description="按操作类型过滤"),
start_time: Optional[datetime] = Query(None, description="开始时间"), start_time: Optional[datetime] = Query(None, description="开始时间"),
end_time: Optional[datetime] = Query(None, description="结束时间"), end_time: Optional[datetime] = Query(None, description="结束时间"),
skip: int = Query(0, ge=0), skip: int = Query(0, ge=0, description="跳过记录数"),
limit: int = Query(100, ge=1, le=1000), limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
current_user=Depends(get_current_metadata_user), current_user=Depends(get_current_metadata_user),
audit_repo: AuditRepository = Depends(get_audit_repository), audit_repo: AuditRepository = Depends(get_audit_repository),
) -> List[AuditLogResponse]: ) -> List[AuditLogResponse]:
""" """
查询当前用户的审计日志 查询当前用户的审计日志
普通用户只能查看自己的操作记录 普通用户只能查看自己的操作记录
""" """
logs = await audit_repo.get_logs( logs = await audit_repo.get_logs(
@@ -99,6 +120,6 @@ async def get_my_audit_logs(
start_time=start_time, start_time=start_time,
end_time=end_time, end_time=end_time,
skip=skip, skip=skip,
limit=limit limit=limit,
) )
return logs return logs
-186
View File
@@ -1,186 +0,0 @@
from typing import Annotated
from datetime import timedelta
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from app.core.config import settings
from app.core.security import create_access_token, create_refresh_token, verify_password
from app.domain.schemas.user import UserCreate, UserResponse, UserLogin, Token
from app.infra.repositories.user_repository import UserRepository
from app.auth.dependencies import get_user_repository, get_current_active_user
from app.domain.schemas.user import UserInDB
import logging
logger = logging.getLogger(__name__)
router = APIRouter()
@router.post("/register", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
async def register(
user_data: UserCreate,
user_repo: UserRepository = Depends(get_user_repository)
) -> UserResponse:
"""
用户注册
创建新用户账号
"""
# 检查用户名和邮箱是否已存在
if await user_repo.user_exists(username=user_data.username):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Username already registered"
)
if await user_repo.user_exists(email=user_data.email):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email already registered"
)
# 创建用户
try:
user = await user_repo.create_user(user_data)
if not user:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create user"
)
return UserResponse.model_validate(user)
except Exception as e:
logger.error(f"Error during user registration: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Registration failed"
)
@router.post("/login", response_model=Token)
async def login(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
user_repo: UserRepository = Depends(get_user_repository)
) -> Token:
"""
用户登录(OAuth2 标准格式)
返回 JWT Access Token 和 Refresh Token
"""
# 验证用户(支持用户名或邮箱登录)
user = await user_repo.get_user_by_username(form_data.username)
if not user:
# 尝试用邮箱登录
user = await user_repo.get_user_by_email(form_data.username)
if not user or not verify_password(form_data.password, user.hashed_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
if not user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Inactive user account"
)
# 生成 Token
access_token = create_access_token(subject=user.username)
refresh_token = create_refresh_token(subject=user.username)
return Token(
access_token=access_token,
refresh_token=refresh_token,
token_type="bearer",
expires_in=settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
)
@router.post("/login/simple", response_model=Token)
async def login_simple(
username: str,
password: str,
user_repo: UserRepository = Depends(get_user_repository)
) -> Token:
"""
简化版登录接口(保持向后兼容)
直接使用 username 和 password 参数
"""
# 验证用户
user = await user_repo.get_user_by_username(username)
if not user:
user = await user_repo.get_user_by_email(username)
if not user or not verify_password(password, user.hashed_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password"
)
if not user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Inactive user account"
)
# 生成 Token
access_token = create_access_token(subject=user.username)
refresh_token = create_refresh_token(subject=user.username)
return Token(
access_token=access_token,
refresh_token=refresh_token,
token_type="bearer",
expires_in=settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
)
@router.get("/me", response_model=UserResponse)
async def get_current_user_info(
current_user: UserInDB = Depends(get_current_active_user)
) -> UserResponse:
"""
获取当前登录用户信息
"""
return UserResponse.model_validate(current_user)
@router.post("/refresh", response_model=Token)
async def refresh_token(
refresh_token: str,
user_repo: UserRepository = Depends(get_user_repository)
) -> Token:
"""
刷新 Access Token
使用 Refresh Token 获取新的 Access Token
"""
from jose import jwt, JWTError
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate refresh token",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(refresh_token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
username: str = payload.get("sub")
token_type: str = payload.get("type")
if username is None or token_type != "refresh":
raise credentials_exception
except JWTError:
raise credentials_exception
# 验证用户仍然存在且激活
user = await user_repo.get_user_by_username(username)
if not user or not user.is_active:
raise credentials_exception
# 生成新的 Access Token
new_access_token = create_access_token(subject=user.username)
return Token(
access_token=new_access_token,
refresh_token=refresh_token, # 保持原 refresh token
token_type="bearer",
expires_in=settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
)
+80 -20
View File
@@ -1,7 +1,7 @@
from datetime import datetime from datetime import datetime
from typing import Any from typing import Any
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Query, Path, Body
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from app.auth.keycloak_dependencies import get_current_keycloak_username from app.auth.keycloak_dependencies import get_current_keycloak_username
@@ -15,7 +15,8 @@ router = APIRouter()
class BurstDetectionRequest(BaseModel): class BurstDetectionRequest(BaseModel):
network: str """爆管检测请求模型"""
network: str = Field(..., description="管网名称(或数据库名称)")
observed_pressure_data: ( observed_pressure_data: (
dict[str, list[Any]] | list[dict[str, Any]] | list[list[Any]] | None dict[str, list[Any]] | list[dict[str, Any]] | list[list[Any]] | None
) = Field( ) = Field(
@@ -26,45 +27,104 @@ class BurstDetectionRequest(BaseModel):
"或二维数组 [[t1_s1, t1_s2], [t2_s1, t2_s2], ...]。" "或二维数组 [[t1_s1, t1_s2], [t2_s1, t2_s2], ...]。"
), ),
) )
points_per_day: int = 1440 points_per_day: int = Field(1440, description="每天的数据点数")
mu: int = 100 mu: int = Field(100, description="异常值检测的参数")
iforest_params: dict[str, Any] | None = None iforest_params: dict[str, Any] | None = Field(None, description="隔离森林算法参数")
scada_start: datetime | None = None scada_start: datetime | None = Field(None, description="SCADA数据起始时间")
scada_end: datetime | None = None scada_end: datetime | None = Field(None, description="SCADA数据结束时间")
sensor_nodes: list[str] | None = None sensor_nodes: list[str] | None = Field(None, description="传感器节点列表")
scheme_name: str | None = None scheme_name: str | None = Field(None, description="方案名称")
data_source: str = "monitoring" data_source: str = Field("monitoring", description="数据来源:monitoring(监测)或simulation(模拟)")
simulation_scheme_name: str | None = None simulation_scheme_name: str | None = Field(None, description="模拟方案名称")
simulation_scheme_type: str | None = None simulation_scheme_type: str | None = Field(None, description="模拟方案类型")
@router.post("/detect/") @router.post(
"/detect/",
summary="执行爆管检测",
description="基于压力观测数据和其他参数执行爆管检测分析"
)
async def detect_burst( async def detect_burst(
data: BurstDetectionRequest, data: BurstDetectionRequest = Body(..., description="爆管检测请求数据"),
username: str = Depends(get_current_keycloak_username), username: str = Depends(get_current_keycloak_username),
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
执行爆管检测分析。
使用异常检测算法(隔离森林)识别压力时间序列中的异常,
将其作为潜在的爆管事件。
Args:
data: 包含管网名称(或数据库名称)、压力数据及相关参数的请求体
username: 当前认证用户名
Returns:
包含检测结果的字典
Raises:
HTTPException: 当处理过程中发生错误时
"""
try: try:
return run_burst_detection(**data.model_dump(), username=username) return run_burst_detection(**data.model_dump(), username=username)
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))
@router.get("/schemes/") @router.get(
"/schemes/",
summary="查询爆管检测方案列表",
description="获取指定网络的所有爆管检测方案"
)
async def query_burst_detection_schemes( async def query_burst_detection_schemes(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
query_date: datetime | None = None, query_date: datetime | None = Query(None, description="查询日期(可选)"),
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""
获取爆管检测方案列表。
查询指定网络的所有已配置的爆管检测方案,
可按日期进行筛选。
Args:
network: 管网名称(或数据库名称)
query_date: 查询日期(可选)
Returns:
爆管检测方案列表
Raises:
HTTPException: 当查询失败时
"""
try: try:
return list_burst_detection_schemes(network=network, query_date=query_date) return list_burst_detection_schemes(network=network, query_date=query_date)
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))
@router.get("/schemes/{scheme_name}") @router.get(
"/schemes/{scheme_name}",
summary="获取爆管检测方案详情",
description="获取指定爆管检测方案的详细信息"
)
async def query_burst_detection_scheme_detail( async def query_burst_detection_scheme_detail(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
scheme_name: str, scheme_name: str = Path(..., description="爆管检测方案名称"),
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
获取爆管检测方案详情。
查询指定爆管检测方案的完整配置和参数信息。
Args:
network: 管网名称(或数据库名称)
scheme_name: 爆管检测方案名称
Returns:
包含方案详情的字典
Raises:
HTTPException: 当查询失败时
"""
try: try:
return get_burst_detection_scheme_detail(network=network, scheme_name=scheme_name) return get_burst_detection_scheme_detail(network=network, scheme_name=scheme_name)
except Exception as exc: except Exception as exc:
+91 -25
View File
@@ -3,8 +3,8 @@ from datetime import datetime
from typing import Literal from typing import Literal
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Query, Path, Body
from pydantic import BaseModel from pydantic import BaseModel, Field
from app.auth.keycloak_dependencies import get_current_keycloak_username from app.auth.keycloak_dependencies import get_current_keycloak_username
from app.services.burst_location import ( from app.services.burst_location import (
@@ -17,48 +17,114 @@ router = APIRouter()
class BurstLocationRequest(BaseModel): class BurstLocationRequest(BaseModel):
network: str """爆管定位请求模型"""
data_source: Literal["monitoring", "simulation"] = "monitoring" network: str = Field(..., description="管网名称(或数据库名称)")
pressure_scada_ids: list[str] | None = None data_source: Literal["monitoring", "simulation"] = Field("monitoring", description="数据来源:monitoring(监测)或simulation(模拟)")
burst_pressure: dict[str, float] | list[dict[str, Any]] | None = None pressure_scada_ids: list[str] | None = Field(None, description="压力SCADA传感器ID列表")
normal_pressure: dict[str, float] | list[dict[str, Any]] | None = None burst_pressure: dict[str, float] | list[dict[str, Any]] | None = Field(None, description="爆管时的压力数据")
burst_leakage: float normal_pressure: dict[str, float] | list[dict[str, Any]] | None = Field(None, description="正常时的压力数据")
flow_scada_ids: list[str] | None = None burst_leakage: float = Field(..., description="爆管时的漏水量")
burst_flow: dict[str, float] | list[dict[str, Any]] | None = None flow_scada_ids: list[str] | None = Field(None, description="流量SCADA传感器ID列表")
normal_flow: dict[str, float] | list[dict[str, Any]] | None = None burst_flow: dict[str, float] | list[dict[str, Any]] | None = Field(None, description="爆管时的流量数据")
min_dpressure: float = 2.0 normal_flow: dict[str, float] | list[dict[str, Any]] | None = Field(None, description="正常时的流量数据")
basic_pressure: float = 10.0 min_dpressure: float = Field(2.0, description="最小压力差(bar")
scada_burst_start: datetime | None = None basic_pressure: float = Field(10.0, description="基准压力(bar")
scada_burst_end: datetime | None = None scada_burst_start: datetime | None = Field(None, description="爆管/模拟方案开始时间")
use_scada_flow: bool = False scada_burst_end: datetime | None = Field(None, description="爆管/模拟方案结束时间")
scheme_name: str | None = None scada_normal_start: datetime | None = Field(None, description="监测数据正常工况开始时间")
simulation_scheme_name: str | None = None scada_normal_end: datetime | None = Field(None, description="监测数据正常工况结束时间")
simulation_scheme_type: str | None = None use_scada_flow: bool = Field(False, description="是否使用SCADA流量数据")
scheme_name: str | None = Field(None, description="方案名称")
simulation_scheme_name: str | None = Field(None, description="模拟方案名称")
simulation_scheme_type: str | None = Field(None, description="模拟方案类型")
@router.post("/locate/") @router.post(
"/locate/",
summary="执行爆管定位",
description="基于压力和流量数据定位管网中的爆管位置"
)
async def locate_burst( async def locate_burst(
data: BurstLocationRequest, data: BurstLocationRequest = Body(..., description="爆管定位请求数据"),
username: str = Depends(get_current_keycloak_username), username: str = Depends(get_current_keycloak_username),
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
执行爆管定位分析。
使用压力和流量SCADA数据,通过对比爆管和正常状态下的数据差异,
定位管网中的爆管位置。
Args:
data: 包含管网名称(或数据库名称)、压力、流量数据及相关参数的请求体
username: 当前认证用户名
Returns:
包含定位结果的字典
Raises:
HTTPException: 当数据类型或值不正确时
"""
try: try:
return run_burst_location_by_network(**data.model_dump(), username=username) return run_burst_location_by_network(**data.model_dump(), username=username)
except (TypeError, ValueError) as exc: except (TypeError, ValueError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))
@router.get("/schemes/") @router.get(
"/schemes/",
summary="查询爆管定位方案列表",
description="获取指定网络的所有爆管定位方案"
)
async def query_burst_schemes( async def query_burst_schemes(
network: str, query_date: datetime | None = None network: str = Query(..., description="管网名称(或数据库名称)"),
query_date: datetime | None = Query(None, description="查询日期(可选)")
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""
获取爆管定位方案列表。
查询指定网络的所有已配置的爆管定位方案,
可按日期进行筛选。
Args:
network: 管网名称(或数据库名称)
query_date: 查询日期(可选)
Returns:
爆管定位方案列表
Raises:
HTTPException: 当查询失败时
"""
try: try:
return list_burst_location_schemes(network=network, query_date=query_date) return list_burst_location_schemes(network=network, query_date=query_date)
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))
@router.get("/schemes/{scheme_name}") @router.get(
async def query_burst_scheme_detail(network: str, scheme_name: str) -> dict[str, Any]: "/schemes/{scheme_name}",
summary="获取爆管定位方案详情",
description="获取指定爆管定位方案的详细信息"
)
async def query_burst_scheme_detail(
network: str = Query(..., description="管网名称(或数据库名称)"),
scheme_name: str = Path(..., description="爆管定位方案名称")
) -> dict[str, Any]:
"""
获取爆管定位方案详情。
查询指定爆管定位方案的完整配置和参数信息。
Args:
network: 管网名称(或数据库名称)
scheme_name: 爆管定位方案名称
Returns:
包含方案详情的字典
Raises:
HTTPException: 当查询失败时
"""
try: try:
return get_burst_location_scheme_detail(network=network, scheme_name=scheme_name) return get_burst_location_scheme_detail(network=network, scheme_name=scheme_name)
except Exception as exc: except Exception as exc:
+27 -7
View File
@@ -1,16 +1,26 @@
from fastapi import APIRouter from fastapi import APIRouter, Query
from app.infra.cache.redis_client import redis_client from app.infra.cache.redis_client import redis_client
router = APIRouter() router = APIRouter()
@router.post("/clearrediskey/") @router.post("/clearrediskey/", summary="清除单个缓存键", description="根据键名清除单个Redis缓存")
async def fastapi_clear_redis_key(key: str): async def fastapi_clear_redis_key(key: str = Query(..., description="缓存键名")):
"""
清除单个缓存键
根据指定的键名删除Redis中对应的缓存
"""
redis_client.delete(key) redis_client.delete(key)
return True return True
@router.post("/clearrediskeys/") @router.post("/clearrediskeys/", summary="清除匹配的缓存键", description="根据模式清除匹配的Redis缓存键")
async def fastapi_clear_redis_keys(keys: str): async def fastapi_clear_redis_keys(keys: str = Query(..., description="缓存键模式(支持通配符)")):
"""
清除匹配的缓存键
根据指定的模式删除Redis中所有匹配的缓存键
"""
# delete keys contains the key # delete keys contains the key
matched_keys = redis_client.keys(f"*{keys}*") matched_keys = redis_client.keys(f"*{keys}*")
if matched_keys: if matched_keys:
@@ -19,14 +29,24 @@ async def fastapi_clear_redis_keys(keys: str):
return True return True
@router.post("/clearallredis/") @router.post("/clearallredis/", summary="清除所有缓存", description="清空整个Redis数据库的所有缓存")
async def fastapi_clear_all_redis(): async def fastapi_clear_all_redis():
"""
清除所有缓存
清空Redis数据库中的所有缓存键值对
"""
redis_client.flushdb() redis_client.flushdb()
return True return True
@router.get("/queryredis/") @router.get("/queryredis/", summary="查询缓存键列表", description="获取Redis中所有的缓存键")
async def fastapi_query_redis(): async def fastapi_query_redis():
"""
查询缓存键列表
获取Redis数据库中所有的缓存键列表
"""
# Helper to decode bytes to str for JSON response if needed, # Helper to decode bytes to str for JSON response if needed,
# but original just returned keys (which might be bytes in redis-py unless decode_responses=True) # but original just returned keys (which might be bytes in redis-py unless decode_responses=True)
# create_redis_client usually sets decode_responses=False by default. # create_redis_client usually sets decode_responses=False by default.
+43 -13
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -13,28 +13,58 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getcontrolschema/") @router.get("/getcontrolschema/", summary="获取控制架构", description="获取网络中控制对象的架构定义")
async def fastapi_get_control_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_control_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取控制架构。
返回指定网络中控制对象的属性架构定义。
"""
return get_control_schema(network) return get_control_schema(network)
@router.get("/getcontrolproperties/") @router.get("/getcontrolproperties/", summary="获取控制属性", description="获取指定网络中的控制属性信息")
async def fastapi_get_control_properties(network: str) -> dict[str, Any]: async def fastapi_get_control_properties(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取控制属性。
返回指定网络中的控制对象属性信息。
"""
return get_control(network) return get_control(network)
@router.post("/setcontrolproperties/", response_model=None) @router.post("/setcontrolproperties/", response_model=None, summary="设置控制属性", description="更新指定网络中的控制属性")
async def fastapi_set_control_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_control_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置控制属性。
更新指定网络中的控制属性值。
"""
props = await req.json() props = await req.json()
return set_control(network, ChangeSet(props)) return set_control(network, ChangeSet(props))
@router.get("/getruleschema/") @router.get("/getruleschema/", summary="获取规则架构", description="获取网络中规则对象的架构定义")
async def fastapi_get_rule_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_rule_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取规则架构。
返回指定网络中规则对象的属性架构定义。
"""
return get_rule_schema(network) return get_rule_schema(network)
@router.get("/getruleproperties/") @router.get("/getruleproperties/", summary="获取规则属性", description="获取指定网络中的规则属性信息")
async def fastapi_get_rule_properties(network: str) -> dict[str, Any]: async def fastapi_get_rule_properties(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取规则属性。
返回指定网络中的规则对象属性信息。
"""
return get_rule(network) return get_rule(network)
@router.post("/setruleproperties/", response_model=None) @router.post("/setruleproperties/", response_model=None, summary="设置规则属性", description="更新指定网络中的规则属性")
async def fastapi_set_rule_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_rule_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置规则属性。
更新指定网络中的规则属性值。
"""
props = await req.json() props = await req.json()
return set_rule(network, ChangeSet(props)) return set_rule(network, ChangeSet(props))
+58 -15
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -14,39 +14,82 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getcurveschema") @router.get("/getcurveschema", summary="获取曲线架构", description="获取网络中曲线对象的架构定义")
async def fastapi_get_curve_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_curve_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取曲线架构。
返回指定网络中曲线对象的属性架构定义。
"""
return get_curve_schema(network) return get_curve_schema(network)
@router.post("/addcurve/", response_model=None) @router.post("/addcurve/", response_model=None, summary="添加曲线", description="在网络中添加一条新的曲线")
async def fastapi_add_curve(network: str, curve: str, req: Request) -> ChangeSet: async def fastapi_add_curve(
network: str = Query(..., description="管网名称(或数据库名称)"),
curve: str = Query(..., description="曲线ID"),
req: Request = None
) -> ChangeSet:
"""添加曲线。
在指定网络中创建一条新的曲线,并设置其初始属性。
"""
props = await req.json() props = await req.json()
ps = { ps = {
"id": curve, "id": curve,
} | props } | props
return add_curve(network, ChangeSet(ps)) return add_curve(network, ChangeSet(ps))
@router.post("/deletecurve/", response_model=None) @router.post("/deletecurve/", response_model=None, summary="删除曲线", description="从网络中删除指定的曲线")
async def fastapi_delete_curve(network: str, curve: str) -> ChangeSet: async def fastapi_delete_curve(
network: str = Query(..., description="管网名称(或数据库名称)"),
curve: str = Query(..., description="曲线ID")
) -> ChangeSet:
"""删除曲线。
从指定网络中删除指定的曲线及其相关数据。
"""
ps = {"id": curve} ps = {"id": curve}
return delete_curve(network, ChangeSet(ps)) return delete_curve(network, ChangeSet(ps))
@router.get("/getcurveproperties/") @router.get("/getcurveproperties/", summary="获取曲线属性", description="获取指定曲线的属性信息")
async def fastapi_get_curve_properties(network: str, curve: str) -> dict[str, Any]: async def fastapi_get_curve_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
curve: str = Query(..., description="曲线ID")
) -> dict[str, Any]:
"""获取曲线属性。
返回指定曲线的所有属性信息。
"""
return get_curve(network, curve) return get_curve(network, curve)
@router.post("/setcurveproperties/", response_model=None) @router.post("/setcurveproperties/", response_model=None, summary="设置曲线属性", description="更新指定曲线的属性")
async def fastapi_set_curve_properties( async def fastapi_set_curve_properties(
network: str, curve: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
curve: str = Query(..., description="曲线ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""设置曲线属性。
更新指定曲线的属性值。
"""
props = await req.json() props = await req.json()
ps = {"id": curve} | props ps = {"id": curve} | props
return set_curve(network, ChangeSet(ps)) return set_curve(network, ChangeSet(ps))
@router.get("/getcurves/") @router.get("/getcurves/", summary="获取所有曲线", description="获取网络中的所有曲线列表")
async def fastapi_get_curves(network: str) -> list[str]: async def fastapi_get_curves(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[str]:
"""获取所有曲线。
返回指定网络中的所有曲线ID列表。
"""
return get_curves(network) return get_curves(network)
@router.get("/iscurve/") @router.get("/iscurve/", summary="检查曲线存在性", description="检查指定的曲线是否存在")
async def fastapi_is_curve(network: str, curve: str) -> bool: async def fastapi_is_curve(
network: str = Query(..., description="管网名称(或数据库名称)"),
curve: str = Query(..., description="曲线ID")
) -> bool:
"""检查曲线是否存在。
判断指定的曲线是否在网络中存在。
"""
return is_curve(network, curve) return is_curve(network, curve)
+87 -25
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -19,57 +19,119 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/gettimeschema") @router.get("/gettimeschema", summary="获取时间选项架构", description="获取网络中时间选项的架构定义")
async def fastapi_get_time_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_time_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取时间选项架构。
返回指定网络中时间相关选项的属性架构定义。
"""
return get_time_schema(network) return get_time_schema(network)
@router.get("/gettimeproperties/") @router.get("/gettimeproperties/", summary="获取时间选项属性", description="获取指定网络中的时间选项属性信息")
async def fastapi_get_time_properties(network: str) -> dict[str, Any]: async def fastapi_get_time_properties(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取时间选项属性。
返回指定网络中的时间相关选项属性。
"""
return get_time(network) return get_time(network)
@router.post("/settimeproperties/", response_model=None) @router.post("/settimeproperties/", response_model=None, summary="设置时间选项属性", description="更新指定网络中的时间选项属性")
async def fastapi_set_time_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_time_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置时间选项属性。
更新指定网络中的时间相关选项属性值。
"""
props = await req.json() props = await req.json()
return set_time(network, ChangeSet(props)) return set_time(network, ChangeSet(props))
@router.get("/getenergyschema/") @router.get("/getenergyschema/", summary="获取能耗选项架构", description="获取网络中能耗选项的架构定义")
async def fastapi_get_energy_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_energy_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取能耗选项架构。
返回指定网络中能耗相关选项的属性架构定义。
"""
return get_energy_schema(network) return get_energy_schema(network)
@router.get("/getenergyproperties/") @router.get("/getenergyproperties/", summary="获取能耗选项属性", description="获取指定网络中的能耗选项属性信息")
async def fastapi_get_energy_properties(network: str) -> dict[str, Any]: async def fastapi_get_energy_properties(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取能耗选项属性。
返回指定网络中的能耗相关选项属性。
"""
return get_energy(network) return get_energy(network)
@router.post("/setenergyproperties/", response_model=None) @router.post("/setenergyproperties/", response_model=None, summary="设置能耗选项属性", description="更新指定网络中的能耗选项属性")
async def fastapi_set_energy_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_energy_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置能耗选项属性。
更新指定网络中的能耗相关选项属性值。
"""
props = await req.json() props = await req.json()
return set_energy(network, ChangeSet(props)) return set_energy(network, ChangeSet(props))
@router.get("/getpumpenergyschema/") @router.get("/getpumpenergyschema/", summary="获取泵能耗选项架构", description="获取网络中泵能耗选项的架构定义")
async def fastapi_get_pump_energy_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_pump_energy_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取泵能耗选项架构。
返回指定网络中泵能耗相关选项的属性架构定义。
"""
return get_pump_energy_schema(network) return get_pump_energy_schema(network)
@router.get("/getpumpenergyproperties//") @router.get("/getpumpenergyproperties//", summary="获取泵能耗属性", description="获取指定泵的能耗属性信息")
async def fastapi_get_pump_energy_proeprties(network: str, pump: str) -> dict[str, Any]: async def fastapi_get_pump_energy_proeprties(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="泵ID")
) -> dict[str, Any]:
"""获取泵能耗属性。
返回指定泵的能耗相关属性。
"""
return get_pump_energy(network, pump) return get_pump_energy(network, pump)
@router.get("/setpumpenergyproperties//", response_model=None) @router.get("/setpumpenergyproperties//", response_model=None, summary="设置泵能耗属性", description="更新指定泵的能耗属性")
async def fastapi_set_pump_energy_properties( async def fastapi_set_pump_energy_properties(
network: str, pump: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="泵ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""设置泵能耗属性。
更新指定泵的能耗相关属性值。
"""
props = await req.json() props = await req.json()
ps = {"id": pump} | props ps = {"id": pump} | props
return set_pump_energy(network, ChangeSet(ps)) return set_pump_energy(network, ChangeSet(ps))
@router.get("/getoptionschema/") @router.get("/getoptionschema/", summary="获取选项架构", description="获取网络中选项对象的架构定义")
async def fastapi_get_option_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_option_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取选项架构。
返回指定网络中选项对象的属性架构定义。
"""
return get_option_v3_schema(network) return get_option_v3_schema(network)
@router.get("/getoptionproperties/") @router.get("/getoptionproperties/", summary="获取选项属性", description="获取指定网络中的选项属性信息")
async def fastapi_get_option_properties(network: str) -> dict[str, Any]: async def fastapi_get_option_properties(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取选项属性。
返回指定网络中的选项对象属性信息。
"""
return get_option_v3(network) return get_option_v3(network)
@router.post("/setoptionproperties/", response_model=None) @router.post("/setoptionproperties/", response_model=None, summary="设置选项属性", description="更新指定网络中的选项属性")
async def fastapi_set_option_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_option_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置选项属性。
更新指定网络中的选项属性值。
"""
props = await req.json() props = await req.json()
return set_option_v3(network, ChangeSet(props)) return set_option_v3(network, ChangeSet(props))
+58 -15
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -14,39 +14,82 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getpatternschema") @router.get("/getpatternschema", summary="获取模式架构", description="获取网络中模式对象的架构定义")
async def fastapi_get_pattern_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_pattern_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取模式架构。
返回指定网络中模式对象的属性架构定义。
"""
return get_pattern_schema(network) return get_pattern_schema(network)
@router.post("/addpattern/", response_model=None) @router.post("/addpattern/", response_model=None, summary="添加模式", description="在网络中添加一个新的模式")
async def fastapi_add_pattern(network: str, pattern: str, req: Request) -> ChangeSet: async def fastapi_add_pattern(
network: str = Query(..., description="管网名称(或数据库名称)"),
pattern: str = Query(..., description="模式ID"),
req: Request = None
) -> ChangeSet:
"""添加模式。
在指定网络中创建一个新的模式,并设置其初始属性。
"""
props = await req.json() props = await req.json()
ps = { ps = {
"id": pattern, "id": pattern,
} | props } | props
return add_pattern(network, ChangeSet(ps)) return add_pattern(network, ChangeSet(ps))
@router.post("/deletepattern/", response_model=None) @router.post("/deletepattern/", response_model=None, summary="删除模式", description="从网络中删除指定的模式")
async def fastapi_delete_pattern(network: str, pattern: str) -> ChangeSet: async def fastapi_delete_pattern(
network: str = Query(..., description="管网名称(或数据库名称)"),
pattern: str = Query(..., description="模式ID")
) -> ChangeSet:
"""删除模式。
从指定网络中删除指定的模式及其相关数据。
"""
ps = {"id": pattern} ps = {"id": pattern}
return delete_pattern(network, ChangeSet(ps)) return delete_pattern(network, ChangeSet(ps))
@router.get("/getpatternproperties/") @router.get("/getpatternproperties/", summary="获取模式属性", description="获取指定模式的属性信息")
async def fastapi_get_pattern_properties(network: str, pattern: str) -> dict[str, Any]: async def fastapi_get_pattern_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
pattern: str = Query(..., description="模式ID")
) -> dict[str, Any]:
"""获取模式属性。
返回指定模式的所有属性信息。
"""
return get_pattern(network, pattern) return get_pattern(network, pattern)
@router.post("/setpatternproperties/", response_model=None) @router.post("/setpatternproperties/", response_model=None, summary="设置模式属性", description="更新指定模式的属性")
async def fastapi_set_pattern_properties( async def fastapi_set_pattern_properties(
network: str, pattern: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
pattern: str = Query(..., description="模式ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""设置模式属性。
更新指定模式的属性值。
"""
props = await req.json() props = await req.json()
ps = {"id": pattern} | props ps = {"id": pattern} | props
return set_pattern(network, ChangeSet(ps)) return set_pattern(network, ChangeSet(ps))
@router.get("/ispattern/") @router.get("/ispattern/", summary="检查模式存在性", description="检查指定的模式是否存在")
async def fastapi_is_pattern(network: str, pattern: str) -> bool: async def fastapi_is_pattern(
network: str = Query(..., description="管网名称(或数据库名称)"),
pattern: str = Query(..., description="模式ID")
) -> bool:
"""检查模式是否存在。
判断指定的模式是否在网络中存在。
"""
return is_pattern(network, pattern) return is_pattern(network, pattern)
@router.get("/getpatterns/") @router.get("/getpatterns/", summary="获取所有模式", description="获取网络中的所有模式列表")
async def fastapi_get_patterns(network: str) -> list[str]: async def fastapi_get_patterns(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[str]:
"""获取所有模式。
返回指定网络中的所有模式ID列表。
"""
return get_patterns(network) return get_patterns(network)
+201 -51
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -32,116 +32,266 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getqualityschema/") @router.get("/getqualityschema/", summary="获取水质架构", description="获取网络中水质对象的架构定义")
async def fastapi_get_quality_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_quality_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取水质架构。
返回指定网络中水质对象的属性架构定义。
"""
return get_quality_schema(network) return get_quality_schema(network)
@router.get("/getqualityproperties/") @router.get("/getqualityproperties/", summary="获取水质属性", description="获取指定节点的水质属性信息")
async def fastapi_get_quality_properties(network: str, node: str) -> dict[str, Any]: async def fastapi_get_quality_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> dict[str, Any]:
"""获取水质属性。
返回指定节点的水质属性信息。
"""
return get_quality(network, node) return get_quality(network, node)
@router.post("/setqualityproperties/", response_model=None) @router.post("/setqualityproperties/", response_model=None, summary="设置水质属性", description="更新指定节点的水质属性")
async def fastapi_set_quality_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_quality_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置水质属性。
更新指定节点的水质属性值。
"""
props = await req.json() props = await req.json()
return set_quality(network, ChangeSet(props)) return set_quality(network, ChangeSet(props))
@router.get("/getemitterschema") @router.get("/getemitterschema", summary="获取发射器架构", description="获取网络中发射器对象的架构定义")
async def fastapi_get_emitter_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_emitter_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取发射器架构。
返回指定网络中发射器对象的属性架构定义。
"""
return get_emitter_schema(network) return get_emitter_schema(network)
@router.get("/getemitterproperties/") @router.get("/getemitterproperties/", summary="获取发射器属性", description="获取指定连接点的发射器属性信息")
async def fastapi_get_emitter_properties(network: str, junction: str) -> dict[str, Any]: async def fastapi_get_emitter_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="连接点ID")
) -> dict[str, Any]:
"""获取发射器属性。
返回指定连接点的发射器属性信息。
"""
return get_emitter(network, junction) return get_emitter(network, junction)
@router.post("/setemitterproperties/", response_model=None) @router.post("/setemitterproperties/", response_model=None, summary="设置发射器属性", description="更新指定连接点的发射器属性")
async def fastapi_set_emitter_properties( async def fastapi_set_emitter_properties(
network: str, junction: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="连接点ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""设置发射器属性。
更新指定连接点的发射器属性值。
"""
props = await req.json() props = await req.json()
ps = {"junction": junction} | props ps = {"junction": junction} | props
return set_emitter(network, ChangeSet(ps)) return set_emitter(network, ChangeSet(ps))
@router.get("/getsourcechema/") @router.get("/getsourcechema/", summary="获取水源架构", description="获取网络中水源对象的架构定义")
async def fastapi_get_source_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_source_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取水源架构。
返回指定网络中水源对象的属性架构定义。
"""
return get_source_schema(network) return get_source_schema(network)
@router.get("/getsource/") @router.get("/getsource/", summary="获取水源属性", description="获取指定节点的水源属性信息")
async def fastapi_get_source(network: str, node: str) -> dict[str, Any]: async def fastapi_get_source(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> dict[str, Any]:
"""获取水源属性。
返回指定节点的水源属性信息。
"""
return get_source(network, node) return get_source(network, node)
@router.post("/setsource/", response_model=None) @router.post("/setsource/", response_model=None, summary="设置水源属性", description="更新指定节点的水源属性")
async def fastapi_set_source(network: str, req: Request) -> ChangeSet: async def fastapi_set_source(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置水源属性。
更新指定节点的水源属性值。
"""
props = await req.json() props = await req.json()
return set_source(network, ChangeSet(props)) return set_source(network, ChangeSet(props))
@router.post("/addsource/", response_model=None) @router.post("/addsource/", response_model=None, summary="添加水源", description="在网络中添加一个新的水源")
async def fastapi_add_source(network: str, req: Request) -> ChangeSet: async def fastapi_add_source(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加水源。
在指定网络中创建一个新的水源,并设置其初始属性。
"""
props = await req.json() props = await req.json()
return add_source(network, ChangeSet(props)) return add_source(network, ChangeSet(props))
@router.post("/deletesource/", response_model=None) @router.post("/deletesource/", response_model=None, summary="删除水源", description="从网络中删除指定节点的水源")
async def fastapi_delete_source(network: str, node: str) -> ChangeSet: async def fastapi_delete_source(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> ChangeSet:
"""删除水源。
从指定网络中删除指定节点的水源。
"""
props = {"node": node} props = {"node": node}
return delete_source(network, ChangeSet(props)) return delete_source(network, ChangeSet(props))
@router.get("/getreactionschema/") @router.get("/getreactionschema/", summary="获取反应架构", description="获取网络中反应对象的架构定义")
async def fastapi_get_reaction_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_reaction_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取反应架构。
返回指定网络中反应对象的属性架构定义。
"""
return get_reaction_schema(network) return get_reaction_schema(network)
@router.get("/getreaction/") @router.get("/getreaction/", summary="获取反应属性", description="获取指定网络中的反应属性信息")
async def fastapi_get_reaction(network: str) -> dict[str, Any]: async def fastapi_get_reaction(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取反应属性。
返回指定网络中的反应属性信息。
"""
return get_reaction(network) return get_reaction(network)
@router.post("/setreaction/", response_model=None) @router.post("/setreaction/", response_model=None, summary="设置反应属性", description="更新指定网络中的反应属性")
async def fastapi_set_reaction(network: str, req: Request) -> ChangeSet: async def fastapi_set_reaction(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置反应属性。
更新指定网络中的反应属性值。
"""
props = await req.json() props = await req.json()
return set_reaction(network, ChangeSet(props)) return set_reaction(network, ChangeSet(props))
@router.get("/getpipereactionschema/") @router.get("/getpipereactionschema/", summary="获取管道反应架构", description="获取网络中管道反应对象的架构定义")
async def fastapi_get_pipe_reaction_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_pipe_reaction_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取管道反应架构。
返回指定网络中管道反应对象的属性架构定义。
"""
return get_pipe_reaction_schema(network) return get_pipe_reaction_schema(network)
@router.get("/getpipereaction/") @router.get("/getpipereaction/", summary="获取管道反应属性", description="获取指定管道的反应属性信息")
async def fastapi_get_pipe_reaction(network: str, pipe: str) -> dict[str, Any]: async def fastapi_get_pipe_reaction(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> dict[str, Any]:
"""获取管道反应属性。
返回指定管道的反应属性信息。
"""
return get_pipe_reaction(network, pipe) return get_pipe_reaction(network, pipe)
@router.post("/setpipereaction/", response_model=None) @router.post("/setpipereaction/", response_model=None, summary="设置管道反应属性", description="更新指定管道的反应属性")
async def fastapi_set_pipe_reaction(network: str, req: Request) -> ChangeSet: async def fastapi_set_pipe_reaction(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置管道反应属性。
更新指定管道的反应属性值。
"""
props = await req.json() props = await req.json()
return set_pipe_reaction(network, ChangeSet(props)) return set_pipe_reaction(network, ChangeSet(props))
@router.get("/gettankreactionschema/") @router.get("/gettankreactionschema/", summary="获取水池反应架构", description="获取网络中水池反应对象的架构定义")
async def fastapi_get_tank_reaction_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_tank_reaction_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取水池反应架构。
返回指定网络中水池反应对象的属性架构定义。
"""
return get_tank_reaction_schema(network) return get_tank_reaction_schema(network)
@router.get("/gettankreaction/") @router.get("/gettankreaction/", summary="获取水池反应属性", description="获取指定水池的反应属性信息")
async def fastapi_get_tank_reaction(network: str, tank: str) -> dict[str, Any]: async def fastapi_get_tank_reaction(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水池ID")
) -> dict[str, Any]:
"""获取水池反应属性。
返回指定水池的反应属性信息。
"""
return get_tank_reaction(network, tank) return get_tank_reaction(network, tank)
@router.post("/settankreaction/", response_model=None) @router.post("/settankreaction/", response_model=None, summary="设置水池反应属性", description="更新指定水池的反应属性")
async def fastapi_set_tank_reaction(network: str, req: Request) -> ChangeSet: async def fastapi_set_tank_reaction(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置水池反应属性。
更新指定水池的反应属性值。
"""
props = await req.json() props = await req.json()
return set_tank_reaction(network, ChangeSet(props)) return set_tank_reaction(network, ChangeSet(props))
@router.get("/getmixingschema/") @router.get("/getmixingschema/", summary="获取混合架构", description="获取网络中混合对象的架构定义")
async def fastapi_get_mixing_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_mixing_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取混合架构。
返回指定网络中混合对象的属性架构定义。
"""
return get_mixing_schema(network) return get_mixing_schema(network)
@router.get("/getmixing/") @router.get("/getmixing/", summary="获取混合属性", description="获取指定水池的混合属性信息")
async def fastapi_get_mixing(network: str, tank: str) -> dict[str, Any]: async def fastapi_get_mixing(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水池ID")
) -> dict[str, Any]:
"""获取混合属性。
返回指定水池的混合属性信息。
"""
return get_mixing(network, tank) return get_mixing(network, tank)
@router.post("/setmixing/", response_model=None) @router.post("/setmixing/", response_model=None, summary="设置混合属性", description="更新指定水池的混合属性")
async def fastapi_set_mixing(network: str, req: Request) -> ChangeSet: async def fastapi_set_mixing(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置混合属性。
更新指定水池的混合属性值。
"""
props = await req.json() props = await req.json()
return api.set_mixing(network, ChangeSet(props)) return api.set_mixing(network, ChangeSet(props))
@router.post("/addmixing/", response_model=None) @router.post("/addmixing/", response_model=None, summary="添加混合", description="在网络中添加一个新的混合")
async def fastapi_add_mixing(network: str, req: Request) -> ChangeSet: async def fastapi_add_mixing(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加混合。
在指定网络中创建一个新的混合,并设置其初始属性。
"""
props = await req.json() props = await req.json()
return add_mixing(network, ChangeSet(props)) return add_mixing(network, ChangeSet(props))
@router.post("/deletemixing/", response_model=None) @router.post("/deletemixing/", response_model=None, summary="删除混合", description="从网络中删除指定的混合")
async def fastapi_delete_mixing(network: str, req: Request) -> ChangeSet: async def fastapi_delete_mixing(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""删除混合。
从指定网络中删除指定的混合及其相关数据。
"""
props = await req.json() props = await req.json()
return delete_mixing(network, ChangeSet(props)) return delete_mixing(network, ChangeSet(props))
+117 -31
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request, Response from fastapi import APIRouter, Request, Query, Path, Body, Response
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -24,71 +24,157 @@ import json
router = APIRouter() router = APIRouter()
@router.get("/getvertexschema/") @router.get("/getvertexschema/", summary="获取图形元素架构", description="获取网络中图形元素对象的架构定义")
async def fastapi_get_vertex_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_vertex_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取图形元素架构。
返回指定网络中图形元素对象的属性架构定义。
"""
return get_vertex_schema(network) return get_vertex_schema(network)
@router.get("/getvertexproperties/") @router.get("/getvertexproperties/", summary="获取图形元素属性", description="获取指定图形元素的属性信息")
async def fastapi_get_vertex_properties(network: str, link: str) -> dict[str, Any]: async def fastapi_get_vertex_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="图形元素链接")
) -> dict[str, Any]:
"""获取图形元素属性。
返回指定图形元素的所有属性信息。
"""
return get_vertex(network, link) return get_vertex(network, link)
@router.post("/setvertexproperties/", response_model=None) @router.post("/setvertexproperties/", response_model=None, summary="设置图形元素属性", description="更新指定图形元素的属性")
async def fastapi_set_vertex_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_vertex_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置图形元素属性。
更新指定图形元素的属性值。
"""
props = await req.json() props = await req.json()
return set_vertex(network, ChangeSet(props)) return set_vertex(network, ChangeSet(props))
@router.post("/addvertex/", response_model=None) @router.post("/addvertex/", response_model=None, summary="添加图形元素", description="在网络中添加一个新的图形元素")
async def fastapi_add_vertex(network: str, req: Request) -> ChangeSet: async def fastapi_add_vertex(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加图形元素。
在指定网络中创建一个新的图形元素,并设置其初始属性。
"""
props = await req.json() props = await req.json()
return add_vertex(network, ChangeSet(props)) return add_vertex(network, ChangeSet(props))
@router.post("/deletevertex/", response_model=None) @router.post("/deletevertex/", response_model=None, summary="删除图形元素", description="从网络中删除指定的图形元素")
async def fastapi_delete_vertex(network: str, req: Request) -> ChangeSet: async def fastapi_delete_vertex(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""删除图形元素。
从指定网络中删除指定的图形元素及其相关数据。
"""
props = await req.json() props = await req.json()
return delete_vertex(network, ChangeSet(props)) return delete_vertex(network, ChangeSet(props))
@router.get("/getallvertexlinks/", response_class=PlainTextResponse) @router.get("/getallvertexlinks/", response_class=PlainTextResponse, summary="获取所有图形元素链接", description="获取网络中的所有图形元素链接列表")
async def fastapi_get_all_vertex_links(network: str) -> list[str]: async def fastapi_get_all_vertex_links(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[str]:
"""获取所有图形元素链接。
返回指定网络中的所有图形元素链接列表。
"""
return json.dumps(get_all_vertex_links(network)) return json.dumps(get_all_vertex_links(network))
@router.get("/getallvertices/", response_class=PlainTextResponse) @router.get("/getallvertices/", response_class=PlainTextResponse, summary="获取所有图形元素", description="获取网络中的所有图形元素详细信息")
async def fastapi_get_all_vertices(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_vertices(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[dict[str, Any]]:
"""获取所有图形元素。
返回指定网络中的所有图形元素详细信息。
"""
return json.dumps(get_all_vertices(network)) return json.dumps(get_all_vertices(network))
@router.get("/getlabelschema/") @router.get("/getlabelschema/", summary="获取标签架构", description="获取网络中标签对象的架构定义")
async def fastapi_get_label_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_label_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取标签架构。
返回指定网络中标签对象的属性架构定义。
"""
return get_label_schema(network) return get_label_schema(network)
@router.get("/getlabelproperties/") @router.get("/getlabelproperties/", summary="获取标签属性", description="获取指定坐标处的标签属性信息")
async def fastapi_get_label_properties( async def fastapi_get_label_properties(
network: str, x: float, y: float network: str = Query(..., description="管网名称(或数据库名称)"),
x: float = Query(..., description="X坐标"),
y: float = Query(..., description="Y坐标")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""获取标签属性。
返回指定坐标处的标签属性信息。
"""
return get_label(network, x, y) return get_label(network, x, y)
@router.post("/setlabelproperties/", response_model=None) @router.post("/setlabelproperties/", response_model=None, summary="设置标签属性", description="更新指定标签的属性")
async def fastapi_set_label_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_label_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置标签属性。
更新指定标签的属性值。
"""
props = await req.json() props = await req.json()
return set_label(network, ChangeSet(props)) return set_label(network, ChangeSet(props))
@router.post("/addlabel/", response_model=None) @router.post("/addlabel/", response_model=None, summary="添加标签", description="在网络中添加一个新的标签")
async def fastapi_add_label(network: str, req: Request) -> ChangeSet: async def fastapi_add_label(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加标签。
在指定网络中创建一个新的标签,并设置其初始属性。
"""
props = await req.json() props = await req.json()
return add_label(network, ChangeSet(props)) return add_label(network, ChangeSet(props))
@router.post("/deletelabel/", response_model=None) @router.post("/deletelabel/", response_model=None, summary="删除标签", description="从网络中删除指定的标签")
async def fastapi_delete_label(network: str, req: Request) -> ChangeSet: async def fastapi_delete_label(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""删除标签。
从指定网络中删除指定的标签及其相关数据。
"""
props = await req.json() props = await req.json()
return delete_label(network, ChangeSet(props)) return delete_label(network, ChangeSet(props))
@router.get("/getbackdropschema/") @router.get("/getbackdropschema/", summary="获取背景架构", description="获取网络中背景对象的架构定义")
async def fastapi_get_backdrop_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_backdrop_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""获取背景架构。
返回指定网络中背景对象的属性架构定义。
"""
return get_backdrop_schema(network) return get_backdrop_schema(network)
@router.get("/getbackdropproperties/") @router.get("/getbackdropproperties/", summary="获取背景属性", description="获取指定网络的背景属性信息")
async def fastapi_get_backdrop_properties(network: str) -> dict[str, Any]: async def fastapi_get_backdrop_properties(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取背景属性。
返回指定网络的背景属性信息。
"""
return get_backdrop(network) return get_backdrop(network)
@router.post("/setbackdropproperties/", response_model=None) @router.post("/setbackdropproperties/", response_model=None, summary="设置背景属性", description="更新指定网络的背景属性")
async def fastapi_set_backdrop_properties(network: str, req: Request) -> ChangeSet: async def fastapi_set_backdrop_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置背景属性。
更新指定网络的背景属性值。
"""
props = await req.json() props = await req.json()
return set_backdrop(network, ChangeSet(props)) return set_backdrop(network, ChangeSet(props))
-388
View File
@@ -1,388 +0,0 @@
from typing import Any, List, Dict, Optional
import logging
from datetime import datetime, timedelta, timezone, time as dt_time
import msgpack
from fastapi import APIRouter
from pydantic import BaseModel
from py_linq import Enumerable
import app.infra.db.influxdb.api as influxdb_api
import app.services.time_api as time_api
from app.infra.cache.redis_client import redis_client, encode_datetime, decode_datetime
router = APIRouter()
logger = logging.getLogger(__name__)
# Basic Node/Link Latest Record Queries
@router.get("/querynodelatestrecordbyid/")
async def fastapi_query_node_latest_record_by_id(id: str) -> Any:
return influxdb_api.query_latest_record_by_ID(id, type="node")
@router.get("/querylinklatestrecordbyid/")
async def fastapi_query_link_latest_record_by_id(id: str) -> Any:
return influxdb_api.query_latest_record_by_ID(id, type="link")
@router.get("/queryscadalatestrecordbyid/")
async def fastapi_query_scada_latest_record_by_id(id: str) -> Any:
return influxdb_api.query_latest_record_by_ID(id, type="scada")
# Time-based Queries
@router.get("/queryallrecordsbytime/")
async def fastapi_query_all_records_by_time(querytime: str) -> dict[str, list]:
results: tuple = influxdb_api.query_all_records_by_time(query_time=querytime)
return {"nodes": results[0], "links": results[1]}
@router.get("/queryallrecordsbytimeproperty/")
async def fastapi_query_all_record_by_time_property(
querytime: str, type: str, property: str, bucket: str = "realtime_simulation_result"
) -> dict[str, list]:
results: tuple = influxdb_api.query_all_record_by_time_property(
query_time=querytime, type=type, property=property, bucket=bucket
)
return {"results": results}
@router.get("/queryallschemerecordsbytimeproperty/")
async def fastapi_query_all_scheme_record_by_time_property(
querytime: str,
type: str,
property: str,
schemename: str,
bucket: str = "scheme_simulation_result",
) -> dict[str, list]:
"""
查询指定方案某一时刻的所有记录,查询 'node''link' 的某一属性值
"""
results: list = influxdb_api.query_all_scheme_record_by_time_property(
query_time=querytime,
type=type,
property=property,
scheme_name=schemename,
bucket=bucket,
)
return {"results": results}
@router.get("/querysimulationrecordsbyidtime/")
async def fastapi_query_simulation_record_by_ids_time(
id: str, querytime: str, type: str, bucket: str = "realtime_simulation_result"
) -> dict[str, list]:
results: tuple = influxdb_api.query_simulation_result_by_ID_time(
ID=id, type=type, query_time=querytime, bucket=bucket
)
return {"results": results}
@router.get("/queryschemesimulationrecordsbyidtime/")
async def fastapi_query_scheme_simulation_record_by_ids_time(
scheme_name: str,
id: str,
querytime: str,
type: str,
bucket: str = "scheme_simulation_result",
) -> dict[str, list]:
results: tuple = influxdb_api.query_scheme_simulation_result_by_ID_time(
scheme_name=scheme_name, ID=id, type=type, query_time=querytime, bucket=bucket
)
return {"results": results}
# Date-based Queries with Caching
@router.get("/queryallrecordsbydate/")
async def fastapi_query_all_records_by_date(querydate: str) -> dict:
is_today_or_future = time_api.is_today_or_future(querydate)
logger.info(f"isToday or future: {is_today_or_future}")
cache_key = f"queryallrecordsbydate_{querydate}"
if not is_today_or_future:
data = redis_client.get(cache_key)
if data:
results = msgpack.unpackb(data, object_hook=decode_datetime)
logger.info("return from cache redis")
return results
logger.info("query from influxdb")
nodes_links: tuple = influxdb_api.query_all_records_by_date(query_date=querydate)
results = {"nodes": nodes_links[0], "links": nodes_links[1]}
if not is_today_or_future:
logger.info("save to cache redis")
redis_client.set(cache_key, msgpack.packb(results, default=encode_datetime))
logger.info("return results")
return results
@router.get("/queryallrecordsbytimerange/")
async def fastapi_query_all_records_by_time_range(
starttime: str, endtime: str
) -> dict[str, list]:
cache_key = f"queryallrecordsbytimerange_{starttime}_{endtime}"
if not time_api.is_today_or_future(starttime):
data = redis_client.get(cache_key)
if data:
loaded_dict = msgpack.unpackb(data, object_hook=decode_datetime)
return loaded_dict
nodes_links: tuple = influxdb_api.query_all_records_by_time_range(
starttime=starttime, endtime=endtime
)
results = {"nodes": nodes_links[0], "links": nodes_links[1]}
if not time_api.is_today_or_future(starttime):
redis_client.set(cache_key, msgpack.packb(results, default=encode_datetime))
return results
@router.get("/queryallrecordsbydatewithtype/")
async def fastapi_query_all_records_by_date_with_type(
querydate: str, querytype: str
) -> list:
cache_key = f"queryallrecordsbydatewithtype_{querydate}_{querytype}"
data = redis_client.get(cache_key)
if data:
loaded_dict = msgpack.unpackb(data, object_hook=decode_datetime)
return loaded_dict
results = influxdb_api.query_all_records_by_date_with_type(
query_date=querydate, query_type=querytype
)
packed = msgpack.packb(results, default=encode_datetime)
redis_client.set(cache_key, packed)
return results
@router.get("/queryallrecordsbyidsdatetype/")
async def fastapi_query_all_records_by_ids_date_type(
ids: str, querydate: str, querytype: str
) -> list:
cache_key = f"queryallrecordsbydatewithtype_{querydate}_{querytype}"
data = redis_client.get(cache_key)
results = []
if data:
results = msgpack.unpackb(data, object_hook=decode_datetime)
else:
results = influxdb_api.query_all_records_by_date_with_type(
query_date=querydate, query_type=querytype
)
packed = msgpack.packb(results, default=encode_datetime)
redis_client.set(cache_key, packed)
query_ids = ids.split(",")
# Using Enumerable from py_linq as in original code
e_results = Enumerable(results)
lst_results = e_results.where(lambda x: x["ID"] in query_ids).to_list()
return lst_results
@router.get("/queryallrecordsbydateproperty/")
async def fastapi_query_all_records_by_date_property(
querydate: str, querytype: str, property: str
) -> list[dict]:
cache_key = f"queryallrecordsbydateproperty_{querydate}_{querytype}_{property}"
data = redis_client.get(cache_key)
if data:
loaded_dict = msgpack.unpackb(data, object_hook=decode_datetime)
return loaded_dict
result_dict = influxdb_api.query_all_record_by_date_property(
query_date=querydate, type=querytype, property=property
)
packed = msgpack.packb(result_dict, default=encode_datetime)
redis_client.set(cache_key, packed)
return result_dict
# Curve Queries
@router.get("/querynodecurvebyidpropertydaterange/")
async def fastapi_query_node_curve_by_id_property_daterange(
id: str, prop: str, startdate: str, enddate: str
):
return influxdb_api.query_curve_by_ID_property_daterange(
id, type="node", property=prop, start_date=startdate, end_date=enddate
)
@router.get("/querylinkcurvebyidpropertydaterange/")
async def fastapi_query_link_curve_by_id_property_daterange(
id: str, prop: str, startdate: str, enddate: str
):
return influxdb_api.query_curve_by_ID_property_daterange(
id, type="link", property=prop, start_date=startdate, end_date=enddate
)
# SCADA Data Queries
@router.get("/queryscadadatabydeviceidandtime/")
async def fastapi_query_scada_data_by_device_id_and_time(ids: str, querytime: str):
query_ids = ids.split(",")
logger.info(querytime)
return influxdb_api.query_SCADA_data_by_device_ID_and_time(
query_ids_list=query_ids, query_time=querytime
)
@router.get("/queryscadadatabydeviceidandtimerange/")
async def fastapi_query_scada_data_by_device_id_and_time_range(
ids: str, starttime: str, endtime: str
):
print(f"query_ids: {ids}, starttime: {starttime}, endtime: {endtime}")
query_ids = ids.split(",")
return influxdb_api.query_SCADA_data_by_device_ID_and_timerange(
query_ids_list=query_ids, start_time=starttime, end_time=endtime
)
@router.get("/queryfillingscadadatabydeviceidandtimerange/")
async def fastapi_query_filling_scada_data_by_device_id_and_time_range(
ids: str, starttime: str, endtime: str
):
print(f"query_ids: {ids}, starttime: {starttime}, endtime: {endtime}")
query_ids = ids.split(",")
return influxdb_api.query_filling_SCADA_data_by_device_ID_and_timerange(
query_ids_list=query_ids, start_time=starttime, end_time=endtime
)
@router.get("/querycleaningscadadatabydeviceidandtimerange/")
async def fastapi_query_cleaning_scada_data_by_device_id_and_time_range(
ids: str, starttime: str, endtime: str
):
print(f"query_ids: {ids}, starttime: {starttime}, endtime: {endtime}")
query_ids = ids.split(",")
return influxdb_api.query_cleaning_SCADA_data_by_device_ID_and_timerange(
query_ids_list=query_ids, start_time=starttime, end_time=endtime
)
@router.get("/querysimulationscadadatabydeviceidandtimerange/")
async def fastapi_query_simulation_scada_data_by_device_id_and_time_range(
ids: str, starttime: str, endtime: str
):
print(f"query_ids: {ids}, starttime: {starttime}, endtime: {endtime}")
query_ids = ids.split(",")
return influxdb_api.query_simulation_SCADA_data_by_device_ID_and_timerange(
query_ids_list=query_ids, start_time=starttime, end_time=endtime
)
@router.get("/querycleanedscadadatabydeviceidandtimerange/")
async def fastapi_query_cleaned_scada_data_by_device_id_and_time_range(
ids: str, starttime: str, endtime: str
):
print(f"query_ids: {ids}, starttime: {starttime}, endtime: {endtime}")
query_ids = ids.split(",")
return influxdb_api.query_cleaned_SCADA_data_by_device_ID_and_timerange(
query_ids_list=query_ids, start_time=starttime, end_time=endtime
)
@router.get("/queryscadadatabydeviceidanddate/")
async def fastapi_query_scada_data_by_device_id_and_date(ids: str, querydate: str):
query_ids = ids.split(",")
return influxdb_api.query_SCADA_data_by_device_ID_and_date(
query_ids_list=query_ids, query_date=querydate
)
@router.get("/queryallscadarecordsbydate/")
async def fastapi_query_all_scada_records_by_date(querydate: str):
is_today_or_future = time_api.is_today_or_future(querydate)
logger.info(f"isToday or future: {is_today_or_future}")
cache_key = f"queryallscadarecordsbydate_{querydate}"
if not is_today_or_future:
data = redis_client.get(cache_key)
if data:
loaded_dict = msgpack.unpackb(data, object_hook=decode_datetime)
logger.info("return from cache redis")
return loaded_dict
logger.info("query from influxdb")
result_dict = influxdb_api.query_all_SCADA_records_by_date(query_date=querydate)
if not is_today_or_future:
logger.info("save to cache redis")
packed = msgpack.packb(result_dict, default=encode_datetime)
redis_client.set(cache_key, packed)
logger.info("return results")
return result_dict
@router.get("/queryallschemeallrecords/")
async def fastapi_query_all_scheme_all_records(
schemetype: str, schemename: str, querydate: str
) -> tuple:
cache_key = f"queryallschemeallrecords_{schemetype}_{schemename}_{querydate}"
data = redis_client.get(cache_key)
if data:
loaded_dict = msgpack.unpackb(data, object_hook=decode_datetime)
return loaded_dict
results = influxdb_api.query_scheme_all_record(
scheme_type=schemetype, scheme_name=schemename, query_date=querydate
)
packed = msgpack.packb(results, default=encode_datetime)
redis_client.set(cache_key, packed)
return results
@router.get("/queryschemeallrecordsproperty/")
async def fastapi_query_all_scheme_all_records_property(
schemetype: str, schemename: str, querydate: str, querytype: str, queryproperty: str
) -> Optional[List]:
cache_key = f"queryallschemeallrecords_{schemetype}_{schemename}_{querydate}"
data = redis_client.get(cache_key)
all_results = None
if data:
all_results = msgpack.unpackb(data, object_hook=decode_datetime)
else:
all_results = influxdb_api.query_scheme_all_record(
scheme_type=schemetype, scheme_name=schemename, query_date=querydate
)
packed = msgpack.packb(all_results, default=encode_datetime)
redis_client.set(cache_key, packed)
results = None
if querytype == "node":
results = all_results[0]
elif querytype == "link":
results = all_results[1]
return results
@router.get("/queryinfluxdbbuckets/")
async def fastapi_query_influxdb_buckets():
return influxdb_api.query_buckets()
@router.get("/queryinfluxdbbucketmeasurements/")
async def fastapi_query_influxdb_bucket_measurements(bucket: str):
return influxdb_api.query_measurements(bucket=bucket)
############################################################
# download history data
############################################################
class Download_History_Data_Manually(BaseModel):
"""
download_date:样式如 datetime(2025, 5, 4)
"""
download_date: datetime
@router.post("/download_history_data_manually/")
async def fastapi_download_history_data_manually(
data: Download_History_Data_Manually,
) -> None:
item = data.dict()
tz = timezone(timedelta(hours=8))
begin_dt = datetime.combine(item.get("download_date").date(), dt_time.min).replace(
tzinfo=tz
)
end_dt = datetime.combine(item.get("download_date").date(), dt_time(23, 59, 59)).replace(
tzinfo=tz
)
begin_time = begin_dt.isoformat()
end_time = end_dt.isoformat()
influxdb_api.download_history_data_manually(
begin_time=begin_time, end_time=end_time
)
+82 -9
View File
@@ -1,5 +1,5 @@
from typing import List, Any from typing import List, Any
from fastapi import APIRouter, Request, HTTPException from fastapi import APIRouter, Request, HTTPException, Query, Body
from app.services.tjnetwork import ( from app.services.tjnetwork import (
ChangeSet, ChangeSet,
get_all_extension_data_keys, get_all_extension_data_keys,
@@ -10,20 +10,93 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getallextensiondatakeys/") @router.get(
async def get_all_extension_data_keys_endpoint(network: str) -> list[str]: "/getallextensiondatakeys/",
summary="获取所有扩展数据键",
description="获取指定网络的所有扩展数据的键列表"
)
async def get_all_extension_data_keys_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[str]:
"""
获取所有扩展数据键。
返回指定网络中所有可用的扩展数据键。
Args:
network: 管网名称(或数据库名称)
Returns:
扩展数据键列表
"""
return get_all_extension_data_keys(network) return get_all_extension_data_keys(network)
@router.get("/getallextensiondata/") @router.get(
async def get_all_extension_data_endpoint(network: str) -> dict[str, Any]: "/getallextensiondata/",
summary="获取所有扩展数据",
description="获取指定网络的所有扩展数据"
)
async def get_all_extension_data_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, Any]:
"""
获取所有扩展数据。
返回指定网络的所有扩展数据及其值。
Args:
network: 管网名称(或数据库名称)
Returns:
扩展数据字典
"""
return get_all_extension_data(network) return get_all_extension_data(network)
@router.get("/getextensiondata/") @router.get(
async def get_extension_data_endpoint(network: str, key: str) -> str | None: "/getextensiondata/",
summary="获取指定扩展数据",
description="获取指定网络中指定键的扩展数据值"
)
async def get_extension_data_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
key: str = Query(..., description="扩展数据键")
) -> str | None:
"""
获取指定扩展数据。
返回指定网络中指定键对应的扩展数据值。
Args:
network: 管网名称(或数据库名称)
key: 扩展数据键
Returns:
扩展数据值,如果不存在返回None
"""
return get_extension_data(network, key) return get_extension_data(network, key)
@router.post("/setextensiondata/", response_model=None) @router.post(
async def set_extension_data_endpoint(network: str, req: Request) -> ChangeSet: "/setextensiondata/",
response_model=None,
summary="设置扩展数据",
description="设置指定网络中的扩展数据"
)
async def set_extension_data_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
设置扩展数据。
在指定网络中设置扩展数据,并返回变更集信息。
Args:
network: 管网名称(或数据库名称)
req: 包含扩展数据的请求体
Returns:
变更集信息
"""
props = await req.json() props = await req.json()
print(props) print(props)
cs = set_extension_data(network, ChangeSet(props)) cs = set_extension_data(network, ChangeSet(props))
+29
View File
@@ -0,0 +1,29 @@
from typing import Any
from fastapi import APIRouter, HTTPException, status
from app.services.geocoding import (
TiandituGeocodeRequest,
TiandituGeocodingAPIError,
TiandituGeocodingConfigError,
geocode_tianditu,
)
router = APIRouter()
@router.post(
"/tianditu/geocode",
summary="Tianditu Geocoding",
description="调用天地图地理编码服务,将结构化地址转换为经纬度",
)
async def tianditu_geocode(request: TiandituGeocodeRequest) -> dict[str, Any]:
try:
return await geocode_tianditu(request)
except TiandituGeocodingConfigError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=str(exc),
) from exc
except TiandituGeocodingAPIError as exc:
raise HTTPException(status_code=exc.status_code, detail=exc.detail) from exc
+90 -26
View File
@@ -2,8 +2,8 @@ import os
from typing import Any from typing import Any
from datetime import datetime from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Query, Path, Body
from pydantic import BaseModel from pydantic import BaseModel, Field
from app.auth.keycloak_dependencies import get_current_keycloak_username from app.auth.keycloak_dependencies import get_current_keycloak_username
from app.services.leakage_identifier import ( from app.services.leakage_identifier import (
@@ -17,50 +17,114 @@ DEFAULT_N_WORKERS = max(1, min((os.cpu_count() or 1) - 1, 4))
class LeakageIdentifyRequest(BaseModel): class LeakageIdentifyRequest(BaseModel):
network: str """漏损识别请求模型"""
observed_pressure_data: str | dict[str, list[Any]] | list[dict[str, Any]] | None = ( network: str = Field(..., description="管网名称(或数据库名称)")
None observed_pressure_data: str | dict[str, list[Any]] | list[dict[str, Any]] | None = Field(
None, description="观测的压力数据"
) )
start_time: float = 0 start_time: float = Field(0, description="起始时间(小时)")
duration: float = 24 duration: float = Field(24, description="持续时间(小时)")
timestep: float = 5 timestep: float = Field(5, description="时间步长(分钟)")
q_sum: float = 0.2 q_sum: float = Field(0.2, description="总流量(m3/s")
q_sum_unit: str = "m3/s" q_sum_unit: str = Field("m3/s", description="流量单位")
output_dir: str = "db_inp" output_dir: str = Field("db_inp", description="输出目录")
pop_size: int = 50 pop_size: int = Field(50, description="种群大小")
max_gen: int = 100 max_gen: int = Field(100, description="最大代数")
n_workers: int = DEFAULT_N_WORKERS n_workers: int = Field(DEFAULT_N_WORKERS, description="工作线程数")
output_flow_unit: str = "m3/s" output_flow_unit: str = Field("m3/s", description="输出流量单位")
dma_count: int | None = None dma_count: int | None = Field(None, description="DMA区域数量")
scada_start: datetime | None = None scada_start: datetime | None = Field(None, description="SCADA数据起始时间")
scada_end: datetime | None = None scada_end: datetime | None = Field(None, description="SCADA数据结束时间")
sensor_nodes: list[str] | None = None sensor_nodes: list[str] | None = Field(None, description="传感器节点列表")
scheme_name: str | None = None scheme_name: str | None = Field(None, description="方案名称")
@router.post("/identify/") @router.post(
"/identify/",
summary="执行漏损识别",
description="基于压力观测数据和遗传算法识别管网中的漏损位置和大小"
)
async def identify_leakage( async def identify_leakage(
data: LeakageIdentifyRequest, data: LeakageIdentifyRequest = Body(..., description="漏损识别请求数据"),
username: str = Depends(get_current_keycloak_username), username: str = Depends(get_current_keycloak_username),
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
执行漏损识别分析。
使用遗传算法对比模型计算和实测压力数据,
识别管网中的漏损节点和漏水量。
Args:
data: 包含管网名称(或数据库名称)、压力数据及优化参数的请求体
username: 当前认证用户名
Returns:
包含识别结果的字典
Raises:
HTTPException: 当处理过程中发生错误时
"""
try: try:
return run_leakage_identification(**data.model_dump(), username=username) return run_leakage_identification(**data.model_dump(), username=username)
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))
@router.get("/schemes/") @router.get(
"/schemes/",
summary="查询漏损识别方案列表",
description="获取指定网络的所有漏损识别方案"
)
async def query_leakage_schemes( async def query_leakage_schemes(
network: str, query_date: datetime | None = None network: str = Query(..., description="管网名称(或数据库名称)"),
query_date: datetime | None = Query(None, description="查询日期(可选)")
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""
获取漏损识别方案列表。
查询指定网络的所有已配置的漏损识别方案,
可按日期进行筛选。
Args:
network: 管网名称(或数据库名称)
query_date: 查询日期(可选)
Returns:
漏损识别方案列表
Raises:
HTTPException: 当查询失败时
"""
try: try:
return list_leakage_identify_schemes(network=network, query_date=query_date) return list_leakage_identify_schemes(network=network, query_date=query_date)
except Exception as exc: except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc)) raise HTTPException(status_code=400, detail=str(exc))
@router.get("/schemes/{scheme_name}") @router.get(
async def query_leakage_scheme_detail(network: str, scheme_name: str) -> dict[str, Any]: "/schemes/{scheme_name}",
summary="获取漏损识别方案详情",
description="获取指定漏损识别方案的详细信息"
)
async def query_leakage_scheme_detail(
network: str = Query(..., description="管网名称(或数据库名称)"),
scheme_name: str = Path(..., description="漏损识别方案名称")
) -> dict[str, Any]:
"""
获取漏损识别方案详情。
查询指定漏损识别方案的完整配置和参数信息。
Args:
network: 管网名称(或数据库名称)
scheme_name: 漏损识别方案名称
Returns:
包含方案详情的字典
Raises:
HTTPException: 当查询失败时
"""
try: try:
return get_leakage_identify_scheme_detail( return get_leakage_identify_scheme_detail(
network=network, scheme_name=scheme_name network=network, scheme_name=scheme_name
+41 -22
View File
@@ -1,5 +1,6 @@
import logging import logging
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status, Query, Path
import psycopg
from psycopg import AsyncConnection from psycopg import AsyncConnection
from sqlalchemy import text from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
@@ -15,55 +16,52 @@ from app.auth.project_dependencies import (
from app.auth.metadata_dependencies import get_current_metadata_user from app.auth.metadata_dependencies import get_current_metadata_user
from app.core.config import settings from app.core.config import settings
from app.domain.schemas.metadata import ( from app.domain.schemas.metadata import (
GeoServerConfigResponse,
ProjectMetaResponse, ProjectMetaResponse,
ProjectSummaryResponse, ProjectSummaryResponse,
) )
from app.infra.repositories.metadata_repository import MetadataRepository from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository
router = APIRouter() router = APIRouter()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@router.get("/meta/project", response_model=ProjectMetaResponse) @router.get("/meta/project", summary="获取项目元数据", description="获取当前项目的元数据和配置信息", response_model=ProjectMetaResponse)
async def get_project_metadata( async def get_project_metadata(
ctx: ProjectContext = Depends(get_project_context), ctx: ProjectContext = Depends(get_project_context),
metadata_repo: MetadataRepository = Depends(get_metadata_repository), metadata_repo: MetadataRepository = Depends(get_metadata_repository),
): ):
"""
获取项目元数据
返回当前项目的完整元数据
"""
project = await metadata_repo.get_project_by_id(ctx.project_id) project = await metadata_repo.get_project_by_id(ctx.project_id)
if not project: if not project:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Project not found" status_code=status.HTTP_404_NOT_FOUND, detail="Project not found"
) )
geoserver = await metadata_repo.get_geoserver_config(ctx.project_id)
geoserver_payload = (
GeoServerConfigResponse(
gs_base_url=geoserver.gs_base_url,
gs_admin_user=geoserver.gs_admin_user,
gs_datastore_name=geoserver.gs_datastore_name,
default_extent=geoserver.default_extent,
srid=geoserver.srid,
)
if geoserver
else None
)
return ProjectMetaResponse( return ProjectMetaResponse(
project_id=project.id, project_id=project.id,
name=project.name, name=project.name,
code=project.code, code=project.code,
description=project.description, description=project.description,
gs_workspace=project.gs_workspace, gs_workspace=project.gs_workspace,
map_extent=project.map_extent,
status=project.status, status=project.status,
project_role=ctx.project_role, project_role=ctx.project_role,
geoserver=geoserver_payload,
) )
@router.get("/meta/projects", response_model=list[ProjectSummaryResponse]) @router.get("/meta/projects", summary="列出用户项目", description="获取当前用户有权限的所有项目列表", response_model=list[ProjectSummaryResponse])
async def list_user_projects( async def list_user_projects(
current_user=Depends(get_current_metadata_user), current_user=Depends(get_current_metadata_user),
metadata_repo: MetadataRepository = Depends(get_metadata_repository), metadata_repo: MetadataRepository = Depends(get_metadata_repository),
): ):
"""
列出用户的所有项目
返回当前用户有权限访问的项目摘要列表
"""
try: try:
projects = await metadata_repo.list_projects_for_user(current_user.id) projects = await metadata_repo.list_projects_for_user(current_user.id)
except SQLAlchemyError as exc: except SQLAlchemyError as exc:
@@ -90,12 +88,33 @@ async def list_user_projects(
] ]
@router.get("/meta/db/health") @router.get("/meta/db/health", summary="检查数据库健康状态", description="检查项目数据库连接的健康状况")
async def project_db_health( async def project_db_health(
pg_session: AsyncSession = Depends(get_project_pg_session), pg_session: AsyncSession = Depends(get_project_pg_session),
ts_conn: AsyncConnection = Depends(get_project_timescale_connection), ts_conn: AsyncConnection = Depends(get_project_timescale_connection),
): ):
await pg_session.execute(text("SELECT 1")) """
async with ts_conn.cursor() as cur: 检查数据库健康状态
await cur.execute("SELECT 1")
检查PostgreSQL和TimescaleDB数据库的连接状态
"""
try:
await pg_session.execute(text("SELECT 1"))
except SQLAlchemyError as exc:
logger.error("Project PostgreSQL health check failed", exc_info=True)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Project PostgreSQL health check failed: {exc}",
) from exc
try:
async with ts_conn.cursor() as cur:
await cur.execute("SELECT 1")
except psycopg.Error as exc:
logger.error("Project TimescaleDB health check failed", exc_info=True)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Project TimescaleDB health check failed: {exc}",
) from exc
return {"postgres": "ok", "timescale": "ok"} return {"postgres": "ok", "timescale": "ok"}
+29 -19
View File
@@ -1,6 +1,5 @@
from typing import Any from typing import Any
import random from fastapi import APIRouter, Query
from fastapi import APIRouter
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi import status from fastapi import status
from pydantic import BaseModel from pydantic import BaseModel
@@ -12,8 +11,13 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getjson/") @router.get("/getjson/", summary="获取JSON示例", description="获取JSON格式响应示例")
async def fastapi_get_json(): async def fastapi_get_json():
"""
获取JSON示例
返回示例JSON格式的响应
"""
return JSONResponse( return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
content={ content={
@@ -24,32 +28,38 @@ async def fastapi_get_json():
) )
@router.get("/getallsensorplacements/") @router.get("/sensor-placement-schemes", summary="获取所有传感器位置", description="获取网络中所有传感器的放置位置信息")
async def fastapi_get_all_sensor_placements(network: str) -> list[dict[Any, Any]]: @router.get("/getallsensorplacements/", summary="获取所有传感器位置(旧路径)", description="获取网络中所有传感器的放置位置信息", deprecated=True)
async def fastapi_get_all_sensor_placements(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[dict[Any, Any]]:
"""
获取所有传感器位置
返回网络中所有传感器的放置位置及其配置信息
"""
return get_all_sensor_placements(network) return get_all_sensor_placements(network)
@router.get("/getallburstlocateresults/") @router.get("/getallburstlocateresults/", summary="获取所有爆管定位结果", description="获取网络中所有爆管定位的分析结果")
async def fastapi_get_all_burst_locate_results(network: str) -> list[dict[Any, Any]]: async def fastapi_get_all_burst_locate_results(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[dict[Any, Any]]:
"""
获取所有爆管定位结果
返回网络中所有的爆管定位分析结果
"""
return get_all_burst_locate_results(network) return get_all_burst_locate_results(network)
class Item(BaseModel): class Item(BaseModel):
"""测试数据模型"""
str_info: str str_info: str
@router.post("/test_dict/") @router.post("/test_dict/", summary="测试字典处理", description="测试处理字典类型数据")
async def fastapi_test_dict(data: Item) -> dict[str, str]: async def fastapi_test_dict(data: Item) -> dict[str, str]:
"""
测试字典处理
接收Item模型,返回其字典格式
"""
item = data.dict() item = data.dict()
return item return item
@router.get("/getrealtimedata/")
async def fastapi_get_realtimedata():
data = [random.randint(0, 100) for _ in range(100)]
return data
@router.get("/getsimulationresult/")
async def fastapi_get_simulationresult():
data = [random.randint(0, 100) for _ in range(100)]
return data
+88 -13
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -17,21 +17,54 @@ router = APIRouter()
# demand 9.[DEMANDS] # demand 9.[DEMANDS]
############################################################ ############################################################
@router.get("/getdemandschema") @router.get(
async def fastapi_get_demand_schema(network: str) -> dict[str, dict[str, Any]]: "/getdemandschema",
summary="获取需水量属性架构",
description="获取指定水网中需水量(Demand)的属性架构定义"
)
async def fastapi_get_demand_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""
获取需水量属性架构。
返回指定水网的需水量属性架构,包括所有可配置的属性及其类型定义。
"""
return get_demand_schema(network) return get_demand_schema(network)
@router.get("/getdemandproperties/") @router.get(
async def fastapi_get_demand_properties(network: str, junction: str) -> dict[str, Any]: "/getdemandproperties/",
summary="获取需水量属性",
description="获取指定水网中节点的需水量属性信息"
)
async def fastapi_get_demand_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点ID")
) -> dict[str, Any]:
"""
获取节点的需水量属性。
返回指定节点的所有需水量信息,包括需水量值、水压等级等。
"""
return get_demand(network, junction) return get_demand(network, junction)
# example: set_demand(p, ChangeSet({'junction': 'j1', 'demands': [{'demand': 10.0, 'pattern': None, 'category': 'x'}, {'demand': 20.0, 'pattern': None, 'category': None}]})) # example: set_demand(p, ChangeSet({'junction': 'j1', 'demands': [{'demand': 10.0, 'pattern': None, 'category': 'x'}, {'demand': 20.0, 'pattern': None, 'category': None}]}))
@router.post("/setdemandproperties/", response_model=None) @router.post(
"/setdemandproperties/",
response_model=None,
summary="设置需水量属性",
description="设置指定水网中节点的需水量属性信息"
)
async def fastapi_set_demand_properties( async def fastapi_set_demand_properties(
network: str, junction: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
设置节点的需水量属性。
修改指定节点的需水量信息。请求体应包含需水量值、水压等级等属性。
"""
props = await req.json() props = await req.json()
ps = {"junction": junction} | props ps = {"junction": junction} | props
return set_demand(network, ChangeSet(ps)) return set_demand(network, ChangeSet(ps))
@@ -39,26 +72,68 @@ async def fastapi_set_demand_properties(
############################################################ ############################################################
# water distribution 36.[Water Distribution] # water distribution 36.[Water Distribution]
############################################################ ############################################################
@router.get("/calculatedemandtonodes/") @router.get(
"/calculatedemandtonodes/",
summary="计算需水量到节点分配",
description="将总需水量按指定方式分配到多个节点"
)
async def fastapi_calculate_demand_to_nodes( async def fastapi_calculate_demand_to_nodes(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> dict[str, float]: ) -> dict[str, float]:
"""
计算需水量到节点分配。
将指定的总需水量均匀或按比例分配到指定的节点列表中。
请求体格式:
{
"demand": 需水量值(float),
"nodes": 节点ID列表(list[str])
}
"""
props = await req.json() props = await req.json()
demand = props["demand"] demand = props["demand"]
nodes = props["nodes"] nodes = props["nodes"]
return calculate_demand_to_nodes(network, demand, nodes) return calculate_demand_to_nodes(network, demand, nodes)
@router.get("/calculatedemandtoregion/") @router.get(
"/calculatedemandtoregion/",
summary="计算需水量到区域分配",
description="将总需水量按区域特征分配到该区域内的节点"
)
async def fastapi_calculate_demand_to_region( async def fastapi_calculate_demand_to_region(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> dict[str, float]: ) -> dict[str, float]:
"""
计算需水量到区域分配。
根据区域内节点的特征(如面积、人口等)将总需水量分配到该区域的各个节点。
请求体格式:
{
"demand": 需水量值(float),
"region": 区域ID(str)
}
"""
props = await req.json() props = await req.json()
demand = props["demand"] demand = props["demand"]
region = props["region"] region = props["region"]
return calculate_demand_to_region(network, demand, region) return calculate_demand_to_region(network, demand, region)
@router.get("/calculatedemandtonetwork/") @router.get(
"/calculatedemandtonetwork/",
summary="计算需水量到整网分配",
description="将需水量均匀分配到整个水网的所有需水节点"
)
async def fastapi_calculate_demand_to_network( async def fastapi_calculate_demand_to_network(
network: str, demand: float network: str = Query(..., description="管网名称(或数据库名称)"),
demand: float = Query(..., description="总需水量(m³/h)", gt=0)
) -> dict[str, float]: ) -> dict[str, float]:
"""
计算需水量到整网分配。
将指定的需水量均匀分配到整个水网的所有需水节点。
"""
return calculate_demand_to_network(network, demand) return calculate_demand_to_network(network, demand)
+281 -59
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -44,110 +44,291 @@ router = APIRouter()
# type # type
############################################################ ############################################################
@router.get("/isnode/") @router.get(
async def fastapi_is_node(network: str, node: str) -> bool: "/isnode/",
summary="检查节点有效性",
description="检查指定ID是否为水网中的有效节点"
)
async def fastapi_is_node(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> bool:
"""检查指定ID是否为节点。"""
return is_node(network, node) return is_node(network, node)
@router.get("/isjunction/") @router.get(
async def fastapi_is_junction(network: str, node: str) -> bool: "/isjunction/",
summary="检查是否为接点",
description="检查指定ID是否为水网中的接点(需求点)"
)
async def fastapi_is_junction(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> bool:
"""检查指定ID是否为接点。"""
return is_junction(network, node) return is_junction(network, node)
@router.get("/isreservoir/") @router.get(
async def fastapi_is_reservoir(network: str, node: str) -> bool: "/isreservoir/",
summary="检查是否为水源",
description="检查指定ID是否为水网中的水源(水库/河流)"
)
async def fastapi_is_reservoir(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> bool:
"""检查指定ID是否为水源。"""
return is_reservoir(network, node) return is_reservoir(network, node)
@router.get("/istank/") @router.get(
async def fastapi_is_tank(network: str, node: str) -> bool: "/istank/",
summary="检查是否为蓄水池",
description="检查指定ID是否为水网中的蓄水池"
)
async def fastapi_is_tank(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> bool:
"""检查指定ID是否为蓄水池。"""
return is_tank(network, node) return is_tank(network, node)
@router.get("/islink/") @router.get(
async def fastapi_is_link(network: str, link: str) -> bool: "/islink/",
summary="检查管线有效性",
description="检查指定ID是否为水网中的有效管线"
)
async def fastapi_is_link(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> bool:
"""检查指定ID是否为管线。"""
return is_link(network, link) return is_link(network, link)
@router.get("/ispipe/") @router.get(
async def fastapi_is_pipe(network: str, link: str) -> bool: "/ispipe/",
summary="检查是否为管道",
description="检查指定ID是否为水网中的管道"
)
async def fastapi_is_pipe(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> bool:
"""检查指定ID是否为管道。"""
return is_pipe(network, link) return is_pipe(network, link)
@router.get("/ispump/") @router.get(
async def fastapi_is_pump(network: str, link: str) -> bool: "/ispump/",
summary="检查是否为泵",
description="检查指定ID是否为水网中的泵"
)
async def fastapi_is_pump(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> bool:
"""检查指定ID是否为泵。"""
return is_pump(network, link) return is_pump(network, link)
@router.get("/isvalve/") @router.get(
async def fastapi_is_valve(network: str, link: str) -> bool: "/isvalve/",
summary="检查是否为阀门",
description="检查指定ID是否为水网中的阀门"
)
async def fastapi_is_valve(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> bool:
"""检查指定ID是否为阀门。"""
return is_valve(network, link) return is_valve(network, link)
@router.get("/getnodetype/") @router.get(
async def fastapi_get_node_type(network: str, node: str) -> str: "/getnodetype/",
summary="获取节点类型",
description="获取指定节点的类型(接点/水源/蓄水池)"
)
async def fastapi_get_node_type(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> str:
"""获取节点的类型标识。"""
return get_node_type(network, node) return get_node_type(network, node)
@router.get("/getlinktype/") @router.get(
async def fastapi_get_link_type(network: str, link: str) -> str: "/getlinktype/",
summary="获取管线类型",
description="获取指定管线的类型(管道/泵/阀门)"
)
async def fastapi_get_link_type(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> str:
"""获取管线的类型标识。"""
return get_link_type(network, link) return get_link_type(network, link)
@router.get("/getelementtype/") @router.get(
async def fastapi_get_element_type(network: str, element: str) -> str: "/getelementtype/",
summary="获取元素类型",
description="获取指定元素的类型(节点或管线)"
)
async def fastapi_get_element_type(
network: str = Query(..., description="管网名称(或数据库名称)"),
element: str = Query(..., description="元素ID")
) -> str:
"""获取元素的类型标识。"""
return get_element_type(network, element) return get_element_type(network, element)
@router.get("/getelementtypevalue/") @router.get(
async def fastapi_get_element_type_value(network: str, element: str) -> int: "/getelementtypevalue/",
summary="获取元素类型值",
description="获取指定元素的类型数值标识"
)
async def fastapi_get_element_type_value(
network: str = Query(..., description="管网名称(或数据库名称)"),
element: str = Query(..., description="元素ID")
) -> int:
"""获取元素的类型数值。"""
return get_element_type_value(network, element) return get_element_type_value(network, element)
@router.get("/getnodes/") @router.get(
async def fastapi_get_nodes(network: str) -> list[str]: "/getnodes/",
summary="获取所有节点",
description="获取指定水网中的所有节点ID列表"
)
async def fastapi_get_nodes(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[str]:
"""获取水网中所有节点的ID列表。"""
return get_nodes(network) return get_nodes(network)
@router.get("/getlinks/") @router.get(
async def fastapi_get_links(network: str) -> list[str]: "/getlinks/",
summary="获取所有管线",
description="获取指定水网中的所有管线ID列表"
)
async def fastapi_get_links(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[str]:
"""获取水网中所有管线的ID列表。"""
return get_links(network) return get_links(network)
@router.get("/getnodelinks/") @router.get(
def get_node_links_endpoint(network: str, node: str) -> list[str]: "/getnodelinks/",
summary="获取节点的关联管线",
description="获取指定节点连接的所有管线ID列表"
)
def get_node_links_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> list[str]:
"""获取节点关联的所有管线。"""
return get_node_links(network, node) return get_node_links(network, node)
############################################################ ############################################################
# Node & Link properties # Node & Link properties
############################################################ ############################################################
@router.get("/getnodeproperties/") @router.get(
async def fast_get_node_properties(network: str, node: str) -> dict[str, Any]: "/getnodeproperties/",
summary="获取节点属性",
description="获取指定节点的所有属性信息"
)
async def fast_get_node_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> dict[str, Any]:
"""获取节点的完整属性信息。"""
return get_node_properties(network, node) return get_node_properties(network, node)
@router.get("/getlinkproperties/") @router.get(
async def fast_get_link_properties(network: str, link: str) -> dict[str, Any]: "/getlinkproperties/",
summary="获取管线属性",
description="获取指定管线的所有属性信息"
)
async def fast_get_link_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> dict[str, Any]:
"""获取管线的完整属性信息。"""
return get_link_properties(network, link) return get_link_properties(network, link)
@router.get("/getscadaproperties/") @router.get(
async def fast_get_scada_properties(network: str, scada: str) -> dict[str, Any]: "/getscadaproperties/",
summary="获取SCADA点属性",
description="获取指定SCADA点的属性信息"
)
async def fast_get_scada_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
scada: str = Query(..., description="SCADA点ID")
) -> dict[str, Any]:
"""获取SCADA点的属性信息。"""
return get_scada_info(network, scada) return get_scada_info(network, scada)
@router.get("/getallscadaproperties/") @router.get(
async def fast_get_all_scada_properties(network: str) -> list[dict[str, Any]]: "/getallscadaproperties/",
summary="获取所有SCADA点属性",
description="获取指定水网中所有SCADA点的属性信息"
)
async def fast_get_all_scada_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""获取水网中所有SCADA点的属性列表。"""
return get_all_scada_info(network) return get_all_scada_info(network)
@router.get("/getelementpropertieswithtype/") @router.get(
"/getelementpropertieswithtype/",
summary="获取指定类型元素属性",
description="获取指定类型的元素属性信息"
)
async def fast_get_element_properties_with_type( async def fast_get_element_properties_with_type(
network: str, elementtype: str, element: str network: str = Query(..., description="管网名称(或数据库名称)"),
elementtype: str = Query(..., description="元素类型"),
element: str = Query(..., description="元素ID")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""获取指定类型元素的属性。"""
return get_element_properties_with_type(network, elementtype, element) return get_element_properties_with_type(network, elementtype, element)
@router.get("/getelementproperties/") @router.get(
async def fast_get_element_properties(network: str, element: str) -> dict[str, Any]: "/getelementproperties/",
summary="获取元素属性",
description="获取指定元素的属性信息"
)
async def fast_get_element_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
element: str = Query(..., description="元素ID")
) -> dict[str, Any]:
"""获取元素的完整属性信息。"""
return get_element_properties(network, element) return get_element_properties(network, element)
############################################################ ############################################################
# title 1.[TITLE] # title 1.[TITLE]
############################################################ ############################################################
@router.get("/gettitleschema/") @router.get(
async def fast_get_title_schema(network: str) -> dict[str, dict[str, Any]]: "/gettitleschema/",
summary="获取标题属性架构",
description="获取指定水网的标题(标题)属性架构定义"
)
async def fast_get_title_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""获取水网标题的属性架构。"""
return get_title_schema(network) return get_title_schema(network)
@router.get("/gettitle/") @router.get(
async def fast_get_title(network: str) -> dict[str, Any]: "/gettitle/",
summary="获取水网标题属性",
description="获取指定水网的标题(Title)信息"
)
async def fast_get_title(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
"""获取水网的标题属性。"""
return get_title(network) return get_title(network)
@router.get("/settitle/", response_model=None) @router.get(
async def fastapi_set_title(network: str, req: Request) -> ChangeSet: "/settitle/",
response_model=None,
summary="设置水网标题属性",
description="设置指定水网的标题(Title)信息"
)
async def fastapi_set_title(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置水网的标题属性。"""
props = await req.json() props = await req.json()
return set_title(network, ChangeSet(props)) return set_title(network, ChangeSet(props))
@@ -155,18 +336,41 @@ async def fastapi_set_title(network: str, req: Request) -> ChangeSet:
# status 10.[STATUS] # status 10.[STATUS]
############################################################ ############################################################
@router.get("/getstatusschema") @router.get(
async def fastapi_get_status_schema(network: str) -> dict[str, dict[str, Any]]: "/getstatusschema",
summary="获取状态属性架构",
description="获取指定水网的状态(Status)属性架构定义"
)
async def fastapi_get_status_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""获取水网状态的属性架构。"""
return get_status_schema(network) return get_status_schema(network)
@router.get("/getstatus/") @router.get(
async def fastapi_get_status(network: str, link: str) -> dict[str, Any]: "/getstatus/",
summary="获取管线状态",
description="获取指定管线的状态信息"
)
async def fastapi_get_status(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> dict[str, Any]:
"""获取管线的状态属性。"""
return get_status(network, link) return get_status(network, link)
@router.post("/setstatus/", response_model=None) @router.post(
"/setstatus/",
response_model=None,
summary="设置管线状态",
description="设置指定管线的状态信息"
)
async def fastapi_set_status_properties( async def fastapi_set_status_properties(
network: str, link: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""设置管线的状态属性。"""
props = await req.json() props = await req.json()
ps = {"link": link} | props ps = {"link": link} | props
return set_status(network, ChangeSet(ps)) return set_status(network, ChangeSet(ps))
@@ -175,8 +379,17 @@ async def fastapi_set_status_properties(
# General Deletion # General Deletion
############################################################ ############################################################
@router.post("/deletenode/", response_model=None) @router.post(
async def fastapi_delete_node(network: str, node: str) -> ChangeSet: "/deletenode/",
response_model=None,
summary="删除节点",
description="删除指定的节点(接点/水源/蓄水池)"
)
async def fastapi_delete_node(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> ChangeSet:
"""删除指定的节点。自动识别节点类型并调用相应的删除操作。"""
ps = {"id": node} ps = {"id": node}
if is_junction(network, node): if is_junction(network, node):
return delete_junction(network, ChangeSet(ps)) return delete_junction(network, ChangeSet(ps))
@@ -186,8 +399,17 @@ async def fastapi_delete_node(network: str, node: str) -> ChangeSet:
return delete_tank(network, ChangeSet(ps)) return delete_tank(network, ChangeSet(ps))
return ChangeSet() # Should probably raise error or return empty return ChangeSet() # Should probably raise error or return empty
@router.post("/deletelink/", response_model=None) @router.post(
async def fastapi_delete_link(network: str, link: str) -> ChangeSet: "/deletelink/",
response_model=None,
summary="删除管线",
description="删除指定的管线(管道/泵/阀门)"
)
async def fastapi_delete_link(
network: str = Query(..., description="管网名称(或数据库名称)"),
link: str = Query(..., description="管线ID")
) -> ChangeSet:
"""删除指定的管线。自动识别管线类型并调用相应的删除操作。"""
ps = {"id": link} ps = {"id": link}
if is_pipe(network, link): if is_pipe(network, link):
return delete_pipe(network, ChangeSet(ps)) return delete_pipe(network, ChangeSet(ps))
+60 -14
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request, Depends from fastapi import APIRouter, Request, Depends, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -10,7 +10,7 @@ from app.services.tjnetwork import (
get_network_node_coords, get_network_node_coords,
get_node_coord, get_node_coord,
) )
from app.auth.dependencies import get_current_user as verify_token from app.auth.metadata_dependencies import get_current_metadata_user
from app.infra.cache.redis_client import redis_client, encode_datetime, decode_datetime from app.infra.cache.redis_client import redis_client, encode_datetime, decode_datetime
import msgpack import msgpack
@@ -34,19 +34,44 @@ router = APIRouter()
# props = await req.json() # props = await req.json()
# return set_coord(network, ChangeSet(props)) # return set_coord(network, ChangeSet(props))
@router.get("/getnodecoord/") @router.get(
async def fastapi_get_node_coord(network: str, node: str) -> dict[str, float] | None: "/getnodecoord/",
summary="获取节点坐标",
description="获取指定节点的地理坐标(X, Y)"
)
async def fastapi_get_node_coord(
network: str = Query(..., description="管网名称(或数据库名称)"),
node: str = Query(..., description="节点ID")
) -> dict[str, float] | None:
"""获取节点的地理坐标信息。"""
return get_node_coord(network, node) return get_node_coord(network, node)
# Additional geometry queries found in main.py logic (implicit or explicit) # Additional geometry queries found in main.py logic (implicit or explicit)
@router.get("/getnetworkinextent/") @router.get(
"/getnetworkinextent/",
summary="获取范围内的网络元素",
description="获取指定地理范围内的网络节点和管线"
)
async def fastapi_get_network_in_extent( async def fastapi_get_network_in_extent(
network: str, x1: float, y1: float, x2: float, y2: float network: str = Query(..., description="管网名称(或数据库名称)"),
x1: float = Query(..., description="范围左下角X坐标", alias="x1"),
y1: float = Query(..., description="范围左下角Y坐标", alias="y1"),
x2: float = Query(..., description="范围右上角X坐标", alias="x2"),
y2: float = Query(..., description="范围右上角Y坐标", alias="y2")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""获取地理范围内的网络几何信息。"""
return get_network_in_extent(network, x1, y1, x2, y2) return get_network_in_extent(network, x1, y1, x2, y2)
@router.get("/getnetworkgeometries/", dependencies=[Depends(verify_token)]) @router.get(
async def fastapi_get_network_geometries(network: str) -> dict[str, Any] | None: "/getnetworkgeometries/",
dependencies=[Depends(get_current_metadata_user)],
summary="获取完整网络几何信息",
description="获取整个水网的所有节点、管线和SCADA点的几何信息(需要身份验证)"
)
async def fastapi_get_network_geometries(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, Any] | None:
"""获取完整的网络几何信息,包括所有节点、管线和SCADA点。结果从缓存返回。"""
cache_key = f"getnetworkgeometries_{network}" cache_key = f"getnetworkgeometries_{network}"
data = redis_client.get(cache_key) data = redis_client.get(cache_key)
if data: if data:
@@ -64,18 +89,39 @@ async def fastapi_get_network_geometries(network: str) -> dict[str, Any] | None:
redis_client.set(cache_key, msgpack.packb(results, default=encode_datetime)) redis_client.set(cache_key, msgpack.packb(results, default=encode_datetime))
return results return results
@router.get("/getmajornodecoords/") @router.get(
"/getmajornodecoords/",
summary="获取主要节点坐标",
description="获取直径大于等于指定值的节点坐标"
)
async def fastapi_get_majornode_coords( async def fastapi_get_majornode_coords(
network: str, diameter: int network: str = Query(..., description="管网名称(或数据库名称)"),
diameter: int = Query(..., description="最小直径(mm)", gt=0)
) -> dict[str, dict[str, float]]: ) -> dict[str, dict[str, float]]:
"""获取主要节点的坐标。只返回直径大于等于指定值的节点。"""
return get_major_node_coords(network, diameter) return get_major_node_coords(network, diameter)
@router.get("/getmajorpipenodes/") @router.get(
async def fastapi_get_major_pipe_nodes(network: str, diameter: int) -> list[str] | None: "/getmajorpipenodes/",
summary="获取主要管道节点",
description="获取直径大于等于指定值的管道的节点ID"
)
async def fastapi_get_major_pipe_nodes(
network: str = Query(..., description="管网名称(或数据库名称)"),
diameter: int = Query(..., description="最小直径(mm)", gt=0)
) -> list[str] | None:
"""获取主要管道节点。只返回直径大于等于指定值的管道。"""
return get_major_pipe_nodes(network, diameter) return get_major_pipe_nodes(network, diameter)
@router.get("/getnetworklinknodes/") @router.get(
async def fastapi_get_network_link_nodes(network: str) -> list[str] | None: "/getnetworklinknodes/",
summary="获取网络管线节点",
description="获取指定水网所有管线的起点和终点节点"
)
async def fastapi_get_network_link_nodes(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[str] | None:
"""获取网络中所有管线的连接节点。"""
return get_network_link_nodes(network) return get_network_link_nodes(network)
# @router.get("/getallcoords/") # @router.get("/getallcoords/")
+278 -37
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -13,108 +13,349 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getjunctionschema") @router.get("/getjunctionschema", summary="获取节点架构", description="获取指定项目的节点属性架构和数据类型定义。")
async def fast_get_junction_schema(network: str) -> dict[str, dict[str, Any]]: async def fast_get_junction_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取节点架构信息。
返回指定项目的节点属性架构,包括所有属性的类型和约束信息。
Args:
network: 管网名称(或数据库名称)
"""
return get_junction_schema(network) return get_junction_schema(network)
@router.post("/addjunction/", response_model=None) @router.post("/addjunction/", response_model=None, summary="添加节点", description="在供水网络中添加新的节点,指定节点ID和空间坐标。")
async def fastapi_add_junction( async def fastapi_add_junction(
network: str, junction: str, x: float, y: float, z: float network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
x: float = Query(..., description="X 坐标"),
y: float = Query(..., description="Y 坐标"),
z: float = Query(..., description="标高(海拔高度)")
) -> ChangeSet: ) -> ChangeSet:
"""
添加新节点到供水网络。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
x: X 坐标值
y: Y 坐标值
z: 标高(海拔高度)
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "x": x, "y": y, "elevation": z} ps = {"id": junction, "x": x, "y": y, "elevation": z}
return add_junction(network, ChangeSet(ps)) return add_junction(network, ChangeSet(ps))
@router.post("/deletejunction/", response_model=None) @router.post("/deletejunction/", response_model=None, summary="删除节点", description="从供水网络中删除指定的节点。")
async def fastapi_delete_junction(network: str, junction: str) -> ChangeSet: async def fastapi_delete_junction(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> ChangeSet:
"""
删除指定的节点。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction} ps = {"id": junction}
return delete_junction(network, ChangeSet(ps)) return delete_junction(network, ChangeSet(ps))
@router.get("/getjunctionelevation/") @router.get("/getjunctionelevation/", summary="获取节点标高", description="获取指定节点的标高(海拔高度)。")
async def fastapi_get_junction_elevation(network: str, junction: str) -> float: async def fastapi_get_junction_elevation(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> float:
"""
获取节点的标高值。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
float: 节点标高值
"""
ps = get_junction(network, junction) ps = get_junction(network, junction)
return ps["elevation"] return ps["elevation"]
@router.get("/getjunctionx/") @router.get("/getjunctionx/", summary="获取节点 X 坐标", description="获取指定节点的 X 坐标值。")
async def fastapi_get_junction_x(network: str, junction: str) -> float: async def fastapi_get_junction_x(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> float:
"""
获取节点的 X 坐标。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
float: 节点 X 坐标值
"""
ps = get_junction(network, junction) ps = get_junction(network, junction)
return ps["x"] return ps["x"]
@router.get("/getjunctiony/") @router.get("/getjunctiony/", summary="获取节点 Y 坐标", description="获取指定节点的 Y 坐标值。")
async def fastapi_get_junction_y(network: str, junction: str) -> float: async def fastapi_get_junction_y(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> float:
"""
获取节点的 Y 坐标。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
float: 节点 Y 坐标值
"""
ps = get_junction(network, junction) ps = get_junction(network, junction)
return ps["y"] return ps["y"]
@router.get("/getjunctioncoord/") @router.get("/getjunctioncoord/", summary="获取节点坐标", description="获取指定节点的 X 和 Y 坐标。")
async def fastapi_get_junction_coord(network: str, junction: str) -> dict[str, float]: async def fastapi_get_junction_coord(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> dict[str, float]:
"""
获取节点的坐标信息。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
dict: 包含 x 和 y 坐标的字典
"""
ps = get_junction(network, junction) ps = get_junction(network, junction)
coord = {"x": ps["x"], "y": ps["y"]} coord = {"x": ps["x"], "y": ps["y"]}
return coord return coord
@router.get("/getjunctiondemand/") @router.get("/getjunctiondemand/", summary="获取节点需水量", description="获取指定节点的需水量。")
async def fastapi_get_junction_demand(network: str, junction: str) -> float: async def fastapi_get_junction_demand(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> float:
"""
获取节点的需水量。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
float: 节点的需水量值
"""
ps = get_junction(network, junction) ps = get_junction(network, junction)
return ps["demand"] return ps["demand"]
@router.get("/getjunctionpattern/") @router.get("/getjunctionpattern/", summary="获取节点需水模式", description="获取指定节点的需水模式标识。")
async def fastapi_get_junction_pattern(network: str, junction: str) -> str: async def fastapi_get_junction_pattern(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> str:
"""
获取节点的需水模式。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
str: 节点的需水模式标识
"""
ps = get_junction(network, junction) ps = get_junction(network, junction)
return ps["pattern"] return ps["pattern"]
@router.post("/setjunctionelevation/", response_model=None) @router.post("/setjunctionelevation/", response_model=None, summary="设置节点标高", description="设置指定节点的标高值。")
async def fastapi_set_junction_elevation( async def fastapi_set_junction_elevation(
network: str, junction: str, elevation: float network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
elevation: float = Query(..., description="标高(海拔高度)")
) -> ChangeSet: ) -> ChangeSet:
"""
设置节点的标高。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
elevation: 标高值
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "elevation": elevation} ps = {"id": junction, "elevation": elevation}
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
@router.post("/setjunctionx/", response_model=None) @router.post("/setjunctionx/", response_model=None, summary="设置节点 X 坐标", description="设置指定节点的 X 坐标值。")
async def fastapi_set_junction_x(network: str, junction: str, x: float) -> ChangeSet: async def fastapi_set_junction_x(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
x: float = Query(..., description="X 坐标值")
) -> ChangeSet:
"""
设置节点的 X 坐标。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
x: X 坐标值
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "x": x} ps = {"id": junction, "x": x}
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
@router.post("/setjunctiony/", response_model=None) @router.post("/setjunctiony/", response_model=None, summary="设置节点 Y 坐标", description="设置指定节点的 Y 坐标值。")
async def fastapi_set_junction_y(network: str, junction: str, y: float) -> ChangeSet: async def fastapi_set_junction_y(
network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
y: float = Query(..., description="Y 坐标值")
) -> ChangeSet:
"""
设置节点的 Y 坐标。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
y: Y 坐标值
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "y": y} ps = {"id": junction, "y": y}
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
@router.post("/setjunctioncoord/", response_model=None) @router.post("/setjunctioncoord/", response_model=None, summary="设置节点坐标", description="设置指定节点的 X 和 Y 坐标。")
async def fastapi_set_junction_coord( async def fastapi_set_junction_coord(
network: str, junction: str, x: float, y: float network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
x: float = Query(..., description="X 坐标值"),
y: float = Query(..., description="Y 坐标值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置节点的坐标。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
x: X 坐标值
y: Y 坐标值
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "x": x, "y": y} ps = {"id": junction, "x": x, "y": y}
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
@router.post("/setjunctiondemand/", response_model=None) @router.post("/setjunctiondemand/", response_model=None, summary="设置节点需水量", description="设置指定节点的需水量。")
async def fastapi_set_junction_demand( async def fastapi_set_junction_demand(
network: str, junction: str, demand: float network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
demand: float = Query(..., description="需水量值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置节点的需水量。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
demand: 需水量值
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "demand": demand} ps = {"id": junction, "demand": demand}
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
@router.post("/setjunctionpattern/", response_model=None) @router.post("/setjunctionpattern/", response_model=None, summary="设置节点需水模式", description="设置指定节点的需水模式标识。")
async def fastapi_set_junction_pattern( async def fastapi_set_junction_pattern(
network: str, junction: str, pattern: str network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
pattern: str = Query(..., description="需水模式标识")
) -> ChangeSet: ) -> ChangeSet:
"""
设置节点的需水模式。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
pattern: 需水模式标识
Returns:
ChangeSet: 包含变更信息的结果
"""
ps = {"id": junction, "pattern": pattern} ps = {"id": junction, "pattern": pattern}
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
@router.get("/getjunctionproperties/") @router.get("/getjunctionproperties/", summary="获取节点属性", description="获取指定节点的所有属性信息。")
async def fastapi_get_junction_properties( async def fastapi_get_junction_properties(
network: str, junction: str network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
获取节点的完整属性信息。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
Returns:
dict: 包含节点所有属性的字典
"""
return get_junction(network, junction) return get_junction(network, junction)
@router.get("/getalljunctionproperties/") @router.get("/getalljunctionproperties/", summary="获取所有节点属性", description="获取指定项目中所有节点的属性信息。")
async def fastapi_get_all_junction_properties(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_junction_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取所有节点的属性信息列表。
此端点返回指定项目中所有节点的详细属性。缓存查询结果以提高性能。
Args:
network: 管网名称(或数据库名称)
Returns:
list: 包含所有节点属性的列表
"""
# 缓存查询结果提高性能 # 缓存查询结果提高性能
# global redis_client # Redis logic removed for clean split, can be re-added if needed or imported # global redis_client # Redis logic removed for clean split, can be re-added if needed or imported
results = get_all_junctions(network) results = get_all_junctions(network)
return results return results
@router.post("/setjunctionproperties/", response_model=None) @router.post("/setjunctionproperties/", response_model=None, summary="批量设置节点属性", description="批量设置指定节点的多个属性。")
async def fastapi_set_junction_properties( async def fastapi_set_junction_properties(
network: str, junction: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
junction: str = Query(..., description="节点 ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
批量设置节点属性。
允许一次性设置节点的多个属性,如坐标、标高、需水量等。
Args:
network: 管网名称(或数据库名称)
junction: 节点 ID
req: 包含属性和值的 JSON 请求体
Returns:
ChangeSet: 包含变更信息的结果
"""
props = await req.json() props = await req.json()
ps = {"id": junction} | props ps = {"id": junction} | props
return set_junction(network, ChangeSet(ps)) return set_junction(network, ChangeSet(ps))
+317 -49
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -14,22 +14,50 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getpipeschema") @router.get("/getpipeschema", summary="获取管道模式", description="获取管道对象的模式定义,包含所有可用字段及其类型")
async def fastapi_get_pipe_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_pipe_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取管道数据模式定义。
Args:
network: 管网名称(或数据库名称)
Returns:
包含管道模式信息的字典
"""
return get_pipe_schema(network) return get_pipe_schema(network)
@router.post("/addpipe/", response_model=None) @router.post("/addpipe/", response_model=None, summary="添加管道", description="向网络中添加新的管道,需要提供管道的基本参数如长度、管径、粗糙度等")
async def fastapi_add_pipe( async def fastapi_add_pipe(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str, pipe: str = Query(..., description="管道标识符"),
node1: str, node1: str = Query(..., description="管道起始节点ID"),
node2: str, node2: str = Query(..., description="管道终止节点ID"),
length: float = 0, length: float = Query(0, description="管道长度(单位:米)"),
diameter: float = 0, diameter: float = Query(0, description="管道管径(单位:毫米)"),
roughness: float = 0, roughness: float = Query(0, description="管道粗糙度"),
minor_loss: float = 0, minor_loss: float = Query(0, description="管道局部阻力系数"),
status: str = PIPE_STATUS_OPEN, status: str = Query(PIPE_STATUS_OPEN, description="管道状态(开启/关闭)"),
) -> ChangeSet: ) -> ChangeSet:
"""
添加新管道到网络。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
node1: 起始节点ID
node2: 终止节点ID
length: 管道长度
diameter: 管道管径
roughness: 管道粗糙度
minor_loss: 局部阻力系数
status: 管道状态
Returns:
ChangeSet对象,包含本次操作的变更信息
"""
ps = { ps = {
"id": pipe, "id": pipe,
"node1": node1, "node1": node1,
@@ -42,102 +70,342 @@ async def fastapi_add_pipe(
} }
return add_pipe(network, ChangeSet(ps)) return add_pipe(network, ChangeSet(ps))
@router.post("/deletepipe/", response_model=None) @router.post("/deletepipe/", response_model=None, summary="删除管道", description="从网络中删除指定的管道")
async def fastapi_delete_pipe(network: str, pipe: str) -> ChangeSet: async def fastapi_delete_pipe(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="要删除的管道ID")
) -> ChangeSet:
"""
删除管道。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
ChangeSet对象,包含本次删除操作的变更信息
"""
ps = {"id": pipe} ps = {"id": pipe}
return delete_pipe(network, ChangeSet(ps)) return delete_pipe(network, ChangeSet(ps))
@router.get("/getpipenode1/") @router.get("/getpipenode1/", summary="获取管道起始节点", description="获取指定管道的起始节点ID")
async def fastapi_get_pipe_node1(network: str, pipe: str) -> str | None: async def fastapi_get_pipe_node1(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> str | None:
"""
获取管道的起始节点。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
起始节点ID,如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["node1"] return ps["node1"]
@router.get("/getpipenode2/") @router.get("/getpipenode2/", summary="获取管道终止节点", description="获取指定管道的终止节点ID")
async def fastapi_get_pipe_node2(network: str, pipe: str) -> str | None: async def fastapi_get_pipe_node2(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> str | None:
"""
获取管道的终止节点。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
终止节点ID,如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["node2"] return ps["node2"]
@router.get("/getpipelength/") @router.get("/getpipelength/", summary="获取管道长度", description="获取指定管道的长度")
async def fastapi_get_pipe_length(network: str, pipe: str) -> float | None: async def fastapi_get_pipe_length(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> float | None:
"""
获取管道长度。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
管道长度(单位:米),如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["length"] return ps["length"]
@router.get("/getpipediameter/") @router.get("/getpipediameter/", summary="获取管道管径", description="获取指定管道的管径")
async def fastapi_get_pipe_diameter(network: str, pipe: str) -> float | None: async def fastapi_get_pipe_diameter(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> float | None:
"""
获取管道管径。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
管道管径(单位:毫米),如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["diameter"] return ps["diameter"]
@router.get("/getpiperoughness/") @router.get("/getpiperoughness/", summary="获取管道粗糙度", description="获取指定管道的粗糙度")
async def fastapi_get_pipe_roughness(network: str, pipe: str) -> float | None: async def fastapi_get_pipe_roughness(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> float | None:
"""
获取管道粗糙度。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
管道粗糙度值,如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["roughness"] return ps["roughness"]
@router.get("/getpipeminorloss/") @router.get("/getpipeminorloss/", summary="获取管道局部阻力系数", description="获取指定管道的局部阻力系数")
async def fastapi_get_pipe_minor_loss(network: str, pipe: str) -> float | None: async def fastapi_get_pipe_minor_loss(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> float | None:
"""
获取管道局部阻力系数。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
局部阻力系数,如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["minor_loss"] return ps["minor_loss"]
@router.get("/getpipestatus/") @router.get("/getpipestatus/", summary="获取管道状态", description="获取指定管道的状态(开启或关闭)")
async def fastapi_get_pipe_status(network: str, pipe: str) -> str | None: async def fastapi_get_pipe_status(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> str | None:
"""
获取管道状态。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
管道状态(开启/关闭),如果不存在则返回None
"""
ps = get_pipe(network, pipe) ps = get_pipe(network, pipe)
return ps["status"] return ps["status"]
@router.post("/setpipenode1/", response_model=None) @router.post("/setpipenode1/", response_model=None, summary="设置管道起始节点", description="设置指定管道的起始节点")
async def fastapi_set_pipe_node1(network: str, pipe: str, node1: str) -> ChangeSet: async def fastapi_set_pipe_node1(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
node1: str = Query(..., description="新的起始节点ID")
) -> ChangeSet:
"""
设置管道起始节点。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
node1: 新的起始节点ID
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "node1": node1} ps = {"id": pipe, "node1": node1}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.post("/setpipenode2/", response_model=None) @router.post("/setpipenode2/", response_model=None, summary="设置管道终止节点", description="设置指定管道的终止节点")
async def fastapi_set_pipe_node2(network: str, pipe: str, node2: str) -> ChangeSet: async def fastapi_set_pipe_node2(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
node2: str = Query(..., description="新的终止节点ID")
) -> ChangeSet:
"""
设置管道终止节点。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
node2: 新的终止节点ID
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "node2": node2} ps = {"id": pipe, "node2": node2}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.post("/setpipelength/", response_model=None) @router.post("/setpipelength/", response_model=None, summary="设置管道长度", description="设置指定管道的长度")
async def fastapi_set_pipe_length(network: str, pipe: str, length: float) -> ChangeSet: async def fastapi_set_pipe_length(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
length: float = Query(..., description="新的管道长度(单位:米)")
) -> ChangeSet:
"""
设置管道长度。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
length: 新的管道长度
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "length": length} ps = {"id": pipe, "length": length}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.post("/setpipediameter/", response_model=None) @router.post("/setpipediameter/", response_model=None, summary="设置管道管径", description="设置指定管道的管径")
async def fastapi_set_pipe_diameter( async def fastapi_set_pipe_diameter(
network: str, pipe: str, diameter: float network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
diameter: float = Query(..., description="新的管道管径(单位:毫米)")
) -> ChangeSet: ) -> ChangeSet:
"""
设置管道管径。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
diameter: 新的管道管径
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "diameter": diameter} ps = {"id": pipe, "diameter": diameter}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.post("/setpiperoughness/", response_model=None) @router.post("/setpiperoughness/", response_model=None, summary="设置管道粗糙度", description="设置指定管道的粗糙度")
async def fastapi_set_pipe_roughness( async def fastapi_set_pipe_roughness(
network: str, pipe: str, roughness: float network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
roughness: float = Query(..., description="新的管道粗糙度值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置管道粗糙度。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
roughness: 新的管道粗糙度
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "roughness": roughness} ps = {"id": pipe, "roughness": roughness}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.post("/setpipeminorloss/", response_model=None) @router.post("/setpipeminorloss/", response_model=None, summary="设置管道局部阻力系数", description="设置指定管道的局部阻力系数")
async def fastapi_set_pipe_minor_loss( async def fastapi_set_pipe_minor_loss(
network: str, pipe: str, minor_loss: float network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
minor_loss: float = Query(..., description="新的局部阻力系数值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置管道局部阻力系数。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
minor_loss: 新的局部阻力系数
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "minor_loss": minor_loss} ps = {"id": pipe, "minor_loss": minor_loss}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.post("/setpipestatus/", response_model=None) @router.post("/setpipestatus/", response_model=None, summary="设置管道状态", description="设置指定管道的状态(开启或关闭)")
async def fastapi_set_pipe_status(network: str, pipe: str, status: str) -> ChangeSet: async def fastapi_set_pipe_status(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
status: str = Query(..., description="新的管道状态(开启/关闭)")
) -> ChangeSet:
"""
设置管道状态。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
status: 新的管道状态
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pipe, "status": status} ps = {"id": pipe, "status": status}
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
@router.get("/getpipeproperties/") @router.get("/getpipeproperties/", summary="获取管道属性", description="获取指定管道的所有属性信息")
async def fastapi_get_pipe_properties(network: str, pipe: str) -> dict[str, Any]: async def fastapi_get_pipe_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID")
) -> dict[str, Any]:
"""
获取管道的所有属性。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
Returns:
包含管道所有属性的字典
"""
return get_pipe(network, pipe) return get_pipe(network, pipe)
@router.get("/getallpipeproperties/") @router.get("/getallpipeproperties/", summary="获取所有管道属性", description="获取网络中所有管道的属性信息列表")
async def fastapi_get_all_pipe_properties(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_pipe_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取网络中所有管道的属性。
Args:
network: 管网名称(或数据库名称)
Returns:
包含所有管道属性的字典列表
"""
# 缓存查询结果提高性能 # 缓存查询结果提高性能
# global redis_client # global redis_client
results = get_all_pipes(network) results = get_all_pipes(network)
return results return results
@router.post("/setpipeproperties/", response_model=None) @router.post("/setpipeproperties/", response_model=None, summary="设置管道属性", description="批量设置指定管道的多个属性")
async def fastapi_set_pipe_properties( async def fastapi_set_pipe_properties(
network: str, pipe: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
pipe: str = Query(..., description="管道ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
批量设置管道属性。
Args:
network: 管网名称(或数据库名称)
pipe: 管道ID
req: 请求体,包含要设置的属性及其值
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
props = await req.json() props = await req.json()
ps = {"id": pipe} | props ps = {"id": pipe} | props
return set_pipe(network, ChangeSet(ps)) return set_pipe(network, ChangeSet(ps))
+155 -21
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -13,57 +13,191 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getpumpschema") @router.get("/getpumpschema", summary="获取水泵模式", description="获取水泵对象的模式定义,包含所有可用字段及其类型")
async def fastapi_get_pump_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_pump_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取水泵数据模式定义。
Args:
network: 管网名称(或数据库名称)
Returns:
包含水泵模式信息的字典
"""
return get_pump_schema(network) return get_pump_schema(network)
@router.post("/addpump/", response_model=None) @router.post("/addpump/", response_model=None, summary="添加水泵", description="向网络中添加新的水泵,需要提供水泵的基本参数如功率等")
async def fastapi_add_pump( async def fastapi_add_pump(
network: str, pump: str, node1: str, node2: str, power: float = 0.0 network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵标识符"),
node1: str = Query(..., description="水泵起始节点ID"),
node2: str = Query(..., description="水泵终止节点ID"),
power: float = Query(0.0, description="水泵功率(单位:千瓦)")
) -> ChangeSet: ) -> ChangeSet:
"""
添加新水泵到网络。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
node1: 起始节点ID
node2: 终止节点ID
power: 水泵功率
Returns:
ChangeSet对象,包含本次操作的变更信息
"""
ps = {"id": pump, "node1": node1, "node2": node2, "power": power} ps = {"id": pump, "node1": node1, "node2": node2, "power": power}
return add_pump(network, ChangeSet(ps)) return add_pump(network, ChangeSet(ps))
@router.post("/deletepump/", response_model=None) @router.post("/deletepump/", response_model=None, summary="删除水泵", description="从网络中删除指定的水泵")
async def fastapi_delete_pump(network: str, pump: str) -> ChangeSet: async def fastapi_delete_pump(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="要删除的水泵ID")
) -> ChangeSet:
"""
删除水泵。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
Returns:
ChangeSet对象,包含本次删除操作的变更信息
"""
ps = {"id": pump} ps = {"id": pump}
return delete_pump(network, ChangeSet(ps)) return delete_pump(network, ChangeSet(ps))
@router.get("/getpumpnode1/") @router.get("/getpumpnode1/", summary="获取水泵起始节点", description="获取指定水泵的起始节点ID")
async def fastapi_get_pump_node1(network: str, pump: str) -> str | None: async def fastapi_get_pump_node1(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵ID")
) -> str | None:
"""
获取水泵的起始节点。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
Returns:
起始节点ID,如果不存在则返回None
"""
ps = get_pump(network, pump) ps = get_pump(network, pump)
return ps["node1"] return ps["node1"]
@router.get("/getpumpnode2/") @router.get("/getpumpnode2/", summary="获取水泵终止节点", description="获取指定水泵的终止节点ID")
async def fastapi_get_pump_node2(network: str, pump: str) -> str | None: async def fastapi_get_pump_node2(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵ID")
) -> str | None:
"""
获取水泵的终止节点。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
Returns:
终止节点ID,如果不存在则返回None
"""
ps = get_pump(network, pump) ps = get_pump(network, pump)
return ps["node2"] return ps["node2"]
@router.post("/setpumpnode1/", response_model=None) @router.post("/setpumpnode1/", response_model=None, summary="设置水泵起始节点", description="设置指定水泵的起始节点")
async def fastapi_set_pump_node1(network: str, pump: str, node1: str) -> ChangeSet: async def fastapi_set_pump_node1(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵ID"),
node1: str = Query(..., description="新的起始节点ID")
) -> ChangeSet:
"""
设置水泵起始节点。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
node1: 新的起始节点ID
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pump, "node1": node1} ps = {"id": pump, "node1": node1}
return set_pump(network, ChangeSet(ps)) return set_pump(network, ChangeSet(ps))
@router.post("/setpumpnode2/", response_model=None) @router.post("/setpumpnode2/", response_model=None, summary="设置水泵终止节点", description="设置指定水泵的终止节点")
async def fastapi_set_pump_node2(network: str, pump: str, node2: str) -> ChangeSet: async def fastapi_set_pump_node2(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵ID"),
node2: str = Query(..., description="新的终止节点ID")
) -> ChangeSet:
"""
设置水泵终止节点。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
node2: 新的终止节点ID
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
ps = {"id": pump, "node2": node2} ps = {"id": pump, "node2": node2}
return set_pump(network, ChangeSet(ps)) return set_pump(network, ChangeSet(ps))
@router.get("/getpumpproperties/") @router.get("/getpumpproperties/", summary="获取水泵属性", description="获取指定水泵的所有属性信息")
async def fastapi_get_pump_properties(network: str, pump: str) -> dict[str, Any]: async def fastapi_get_pump_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵ID")
) -> dict[str, Any]:
"""
获取水泵的所有属性。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
Returns:
包含水泵所有属性的字典
"""
return get_pump(network, pump) return get_pump(network, pump)
@router.get("/getallpumpproperties/") @router.get("/getallpumpproperties/", summary="获取所有水泵属性", description="获取网络中所有水泵的属性信息列表")
async def fastapi_get_all_pump_properties(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_pump_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取网络中所有水泵的属性。
Args:
network: 管网名称(或数据库名称)
Returns:
包含所有水泵属性的字典列表
"""
# 缓存查询结果提高性能 # 缓存查询结果提高性能
# global redis_client # global redis_client
results = get_all_pumps(network) results = get_all_pumps(network)
return results return results
@router.post("/setpumpproperties/", response_model=None) @router.post("/setpumpproperties/", response_model=None, summary="设置水泵属性", description="批量设置指定水泵的多个属性")
async def fastapi_set_pump_properties( async def fastapi_set_pump_properties(
network: str, pump: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
pump: str = Query(..., description="水泵ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
批量设置水泵属性。
Args:
network: 管网名称(或数据库名称)
pump: 水泵ID
req: 请求体,包含要设置的属性及其值
Returns:
ChangeSet对象,包含本次修改的变更信息
"""
props = await req.json() props = await req.json()
ps = {"id": pump} | props ps = {"id": pump} | props
return set_pump(network, ChangeSet(ps)) return set_pump(network, ChangeSet(ps))
+348 -95
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -44,64 +44,95 @@ router = APIRouter()
# region 32 # region 32
############################################################ ############################################################
@router.get("/calculateregion/") @router.get(
async def fastapi_calculate_region(network: str, time_index: int) -> dict[str, Any]: "/getregionschema/",
return calculate_region(network, time_index) summary="获取区域属性架构",
description="获取指定水网的区域属性架构定义"
@router.get("/getregionschema/") )
async def fastapi_get_region_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_region_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""获取区域的属性架构。"""
return get_region_schema(network) return get_region_schema(network)
@router.get("/getregion/") @router.get(
async def fastapi_get_region(network: str, id: str) -> dict[str, Any]: "/getregion/",
summary="获取区域信息",
description="获取指定ID的区域详细信息"
)
async def fastapi_get_region(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="区域ID")
) -> dict[str, Any]:
"""获取区域的详细信息。"""
return get_region(network, id) return get_region(network, id)
@router.post("/setregion/", response_model=None) @router.post(
async def fastapi_set_region(network: str, req: Request) -> ChangeSet: "/setregion/",
response_model=None,
summary="设置区域属性",
description="修改指定区域的属性信息"
)
async def fastapi_set_region(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置区域属性。"""
props = await req.json() props = await req.json()
return set_region(network, ChangeSet(props)) return set_region(network, ChangeSet(props))
@router.post("/addregion/", response_model=None) @router.post(
async def fastapi_add_region(network: str, req: Request) -> ChangeSet: "/addregion/",
response_model=None,
summary="添加新区域",
description="向水网添加一个新的区域"
)
async def fastapi_add_region(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加新的区域。"""
props = await req.json() props = await req.json()
return add_region(network, ChangeSet(props)) return add_region(network, ChangeSet(props))
@router.post("/deleteregion/", response_model=None) @router.post(
async def fastapi_delete_region(network: str, req: Request) -> ChangeSet: "/deleteregion/",
response_model=None,
summary="删除区域",
description="删除指定的区域"
)
async def fastapi_delete_region(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""删除区域。"""
props = await req.json() props = await req.json()
return delete_region(network, ChangeSet(props)) return delete_region(network, ChangeSet(props))
@router.get("/getallregions/")
async def fastapi_get_all_regions(network: str) -> list[dict[str, Any]]:
return get_all_regions(network)
@router.post("/generateregion/", response_model=None)
async def fastapi_generate_region(
network: str, inflate_delta: float
) -> ChangeSet:
return generate_region(network, inflate_delta)
############################################################ ############################################################
# district_metering_area 33 # district_metering_area 33
############################################################ ############################################################
@router.get("/calculatedistrictmeteringarea/") @router.get(
async def fastapi_calculate_district_metering_area( "/calculatedistrictmeteringareaforregion/",
network: str, req: Request summary="计算区域内DMA分区",
) -> list[list[str]]: description="为指定区域计算区域计量(DMA)分区方案"
props = await req.json() )
nodes = props["nodes"]
part_count = props["part_count"]
part_type = props["part_type"]
return calculate_district_metering_area(
network, nodes, part_count, part_type
)
@router.get("/calculatedistrictmeteringareaforregion/")
async def fastapi_calculate_district_metering_area_for_region( async def fastapi_calculate_district_metering_area_for_region(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> list[list[str]]: ) -> list[list[str]]:
"""
计算区域内DMA分区。
请求体格式:
{
"region": 区域ID(str),
"part_count": 分区数量(int),
"part_type": 分区类型(int)
}
"""
props = await req.json() props = await req.json()
region = props["region"] region = props["region"]
part_count = props["part_count"] part_count = props["part_count"]
@@ -110,32 +141,77 @@ async def fastapi_calculate_district_metering_area_for_region(
network, region, part_count, part_type network, region, part_count, part_type
) )
@router.get("/calculatedistrictmeteringareafornetwork/") @router.get(
"/calculatedistrictmeteringareafornetwork/",
summary="计算整网DMA分区",
description="为整个水网计算区域计量(DMA)分区方案"
)
async def fastapi_calculate_district_metering_area_for_network( async def fastapi_calculate_district_metering_area_for_network(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> list[list[str]]: ) -> list[list[str]]:
"""
计算整网DMA分区。
请求体格式:
{
"part_count": 分区数量(int),
"part_type": 分区类型(int)
}
"""
props = await req.json() props = await req.json()
part_count = props["part_count"] part_count = props["part_count"]
part_type = props["part_type"] part_type = props["part_type"]
return calculate_district_metering_area_for_network(network, part_count, part_type) return calculate_district_metering_area_for_network(network, part_count, part_type)
@router.get("/getdistrictmeteringareaschema/") @router.get(
"/getdistrictmeteringareaschema/",
summary="获取DMA属性架构",
description="获取指定水网的区域计量(DMA)属性架构定义"
)
async def fastapi_get_district_metering_area_schema( async def fastapi_get_district_metering_area_schema(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
) -> dict[str, dict[str, Any]]: ) -> dict[str, dict[str, Any]]:
"""获取DMA的属性架构。"""
return get_district_metering_area_schema(network) return get_district_metering_area_schema(network)
@router.get("/getdistrictmeteringarea/") @router.get(
async def fastapi_get_district_metering_area(network: str, id: str) -> dict[str, Any]: "/getdistrictmeteringarea/",
summary="获取DMA信息",
description="获取指定ID的区域计量(DMA)详细信息"
)
async def fastapi_get_district_metering_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="DMA ID")
) -> dict[str, Any]:
"""获取DMA的详细信息。"""
return get_district_metering_area(network, id) return get_district_metering_area(network, id)
@router.post("/setdistrictmeteringarea/", response_model=None) @router.post(
async def fastapi_set_district_metering_area(network: str, req: Request) -> ChangeSet: "/setdistrictmeteringarea/",
response_model=None,
summary="设置DMA属性",
description="修改指定DMA的属性信息"
)
async def fastapi_set_district_metering_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置DMA属性。"""
props = await req.json() props = await req.json()
return set_district_metering_area(network, ChangeSet(props)) return set_district_metering_area(network, ChangeSet(props))
@router.post("/adddistrictmeteringarea/", response_model=None) @router.post(
async def fastapi_add_district_metering_area(network: str, req: Request) -> ChangeSet: "/adddistrictmeteringarea/",
response_model=None,
summary="添加新DMA",
description="向水网添加一个新的区域计量(DMA)"
)
async def fastapi_add_district_metering_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加新的DMA。"""
props = await req.json() props = await req.json()
# boundary should be [(x,y), (x,y)] # boundary should be [(x,y), (x,y)]
boundary = props.get("boundary", []) boundary = props.get("boundary", [])
@@ -146,33 +222,73 @@ async def fastapi_add_district_metering_area(network: str, req: Request) -> Chan
props["boundary"] = newBoundary props["boundary"] = newBoundary
return add_district_metering_area(network, ChangeSet(props)) return add_district_metering_area(network, ChangeSet(props))
@router.post("/deletedistrictmeteringarea/", response_model=None) @router.post(
"/deletedistrictmeteringarea/",
response_model=None,
summary="删除DMA",
description="删除指定的区域计量(DMA)"
)
async def fastapi_delete_district_metering_area( async def fastapi_delete_district_metering_area(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""删除DMA。"""
props = await req.json() props = await req.json()
return delete_district_metering_area(network, ChangeSet(props)) return delete_district_metering_area(network, ChangeSet(props))
@router.get("/getalldistrictmeteringareaids/") @router.get(
async def fastapi_get_all_district_metering_area_ids(network: str) -> list[str]: "/getalldistrictmeteringareaids/",
summary="获取所有DMA ID",
description="获取指定水网中所有DMA的ID列表"
)
async def fastapi_get_all_district_metering_area_ids(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[str]:
"""获取所有DMA的ID列表。"""
return get_all_district_metering_area_ids(network) return get_all_district_metering_area_ids(network)
@router.get("/getalldistrictmeteringareas/") @router.get(
async def getalldistrictmeteringareas(network: str) -> list[dict[str, Any]]: "/getalldistrictmeteringareas/",
summary="获取所有DMA",
description="获取指定水网中所有DMA的详细信息"
)
async def getalldistrictmeteringareas(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""获取所有DMA的详细信息列表。"""
return get_all_district_metering_areas(network) return get_all_district_metering_areas(network)
@router.post("/generatedistrictmeteringarea/", response_model=None) @router.post(
"/generatedistrictmeteringarea/",
response_model=None,
summary="生成DMA分区",
description="根据参数自动生成水网的DMA分区方案"
)
async def fastapi_generate_district_metering_area( async def fastapi_generate_district_metering_area(
network: str, part_count: int, part_type: int, inflate_delta: float network: str = Query(..., description="管网名称(或数据库名称)"),
part_count: int = Query(..., description="分区数量", gt=0),
part_type: int = Query(..., description="分区类型"),
inflate_delta: float = Query(..., description="膨胀参数")
) -> ChangeSet: ) -> ChangeSet:
"""生成DMA分区。"""
return generate_district_metering_area( return generate_district_metering_area(
network, part_count, part_type, inflate_delta network, part_count, part_type, inflate_delta
) )
@router.post("/generatesubdistrictmeteringarea/", response_model=None) @router.post(
"/generatesubdistrictmeteringarea/",
response_model=None,
summary="生成DMA子分区",
description="为指定DMA生成子DMA分区"
)
async def fastapi_generate_sub_district_metering_area( async def fastapi_generate_sub_district_metering_area(
network: str, dma: str, part_count: int, part_type: int, inflate_delta: float network: str = Query(..., description="管网名称(或数据库名称)"),
dma: str = Query(..., description="DMA ID"),
part_count: int = Query(..., description="分区数量", gt=0),
part_type: int = Query(..., description="分区类型"),
inflate_delta: float = Query(..., description="膨胀参数")
) -> ChangeSet: ) -> ChangeSet:
"""生成DMA子分区。"""
return generate_sub_district_metering_area( return generate_sub_district_metering_area(
network, dma, part_count, part_type, inflate_delta network, dma, part_count, part_type, inflate_delta
) )
@@ -182,43 +298,104 @@ async def fastapi_generate_sub_district_metering_area(
# service_area 34 # service_area 34
############################################################ ############################################################
@router.get("/calculateservicearea/") @router.get(
"/calculateservicearea/",
summary="计算服务区",
description="计算指定水网的服务区分区,返回全部时间步结果"
)
async def fastapi_calculate_service_area( async def fastapi_calculate_service_area(
network: str, time_index: int network: str = Query(..., description="管网名称(或数据库名称)"),
) -> dict[str, Any]: ) -> list[dict[str, list[str]]]:
return calculate_service_area(network, time_index) """计算服务区分区,返回全部时间步结果。"""
return calculate_service_area(network)
@router.get("/getserviceareaschema/") @router.get(
async def fastapi_get_service_area_schema(network: str) -> dict[str, dict[str, Any]]: "/getserviceareaschema/",
summary="获取服务区属性架构",
description="获取指定水网的服务区属性架构定义"
)
async def fastapi_get_service_area_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""获取服务区的属性架构。"""
return get_service_area_schema(network) return get_service_area_schema(network)
@router.get("/getservicearea/") @router.get(
async def fastapi_get_service_area(network: str, id: str) -> dict[str, Any]: "/getservicearea/",
summary="获取服务区信息",
description="获取指定ID的服务区详细信息"
)
async def fastapi_get_service_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="服务区ID")
) -> dict[str, Any]:
"""获取服务区的详细信息。"""
return get_service_area(network, id) return get_service_area(network, id)
@router.post("/setservicearea/", response_model=None) @router.post(
async def fastapi_set_service_area(network: str, req: Request) -> ChangeSet: "/setservicearea/",
response_model=None,
summary="设置服务区属性",
description="修改指定服务区的属性信息"
)
async def fastapi_set_service_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置服务区属性。"""
props = await req.json() props = await req.json()
return set_service_area(network, ChangeSet(props)) return set_service_area(network, ChangeSet(props))
@router.post("/addservicearea/", response_model=None) @router.post(
async def fastapi_add_service_area(network: str, req: Request) -> ChangeSet: "/addservicearea/",
response_model=None,
summary="添加新服务区",
description="向水网添加一个新的服务区"
)
async def fastapi_add_service_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加新的服务区。"""
props = await req.json() props = await req.json()
return add_service_area(network, ChangeSet(props)) return add_service_area(network, ChangeSet(props))
@router.post("/deleteservicearea/", response_model=None) @router.post(
async def fastapi_delete_service_area(network: str, req: Request) -> ChangeSet: "/deleteservicearea/",
response_model=None,
summary="删除服务区",
description="删除指定的服务区"
)
async def fastapi_delete_service_area(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""删除服务区。"""
props = await req.json() props = await req.json()
return delete_service_area(network, ChangeSet(props)) return delete_service_area(network, ChangeSet(props))
@router.get("/getallserviceareas/") @router.get(
async def fastapi_get_all_service_areas(network: str) -> list[dict[str, Any]]: "/getallserviceareas/",
summary="获取所有服务区",
description="获取指定水网中的所有服务区信息"
)
async def fastapi_get_all_service_areas(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""获取所有服务区的信息列表。"""
return get_all_service_areas(network) return get_all_service_areas(network)
@router.post("/generateservicearea/", response_model=None) @router.post(
"/generateservicearea/",
response_model=None,
summary="生成服务区分区",
description="根据参数自动生成水网的服务区分区"
)
async def fastapi_generate_service_area( async def fastapi_generate_service_area(
network: str, inflate_delta: float network: str = Query(..., description="管网名称(或数据库名称)"),
inflate_delta: float = Query(..., description="膨胀参数")
) -> ChangeSet: ) -> ChangeSet:
"""生成服务区分区。"""
return generate_service_area(network, inflate_delta) return generate_service_area(network, inflate_delta)
@@ -226,52 +403,128 @@ async def fastapi_generate_service_area(
# virtual_district 35 # virtual_district 35
############################################################ ############################################################
@router.get("/calculatevirtualdistrict/") @router.get(
"/calculatevirtualdistrict/",
summary="计算虚拟分区",
description="根据指定的压力监测节点作为中心节点计算虚拟分区方案"
)
async def fastapi_calculate_virtual_district( async def fastapi_calculate_virtual_district(
network: str, centers: list[str] network: str = Query(..., description="管网名称(或数据库名称)"),
centers: list[str] = Query(..., description="压力监测节点ID列表")
) -> dict[str, list[Any]]: ) -> dict[str, list[Any]]:
"""计算虚拟分区。"""
return calculate_virtual_district(network, centers) return calculate_virtual_district(network, centers)
@router.get("/getvirtualdistrictschema/") @router.get(
"/getvirtualdistrictschema/",
summary="获取虚拟分区属性架构",
description="获取指定水网的虚拟分区属性架构定义"
)
async def fastapi_get_virtual_district_schema( async def fastapi_get_virtual_district_schema(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
) -> dict[str, dict[str, Any]]: ) -> dict[str, dict[str, Any]]:
"""获取虚拟分区的属性架构。"""
return get_virtual_district_schema(network) return get_virtual_district_schema(network)
@router.get("/getvirtualdistrict/") @router.get(
async def fastapi_get_virtual_district(network: str, id: str) -> dict[str, Any]: "/getvirtualdistrict/",
summary="获取虚拟分区信息",
description="获取指定ID的虚拟分区详细信息"
)
async def fastapi_get_virtual_district(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="虚拟分区ID")
) -> dict[str, Any]:
"""获取虚拟分区的详细信息。"""
return get_virtual_district(network, id) return get_virtual_district(network, id)
@router.post("/setvirtualdistrict/", response_model=None) @router.post(
async def fastapi_set_virtual_district(network: str, req: Request) -> ChangeSet: "/setvirtualdistrict/",
response_model=None,
summary="设置虚拟分区属性",
description="修改指定虚拟分区的属性信息"
)
async def fastapi_set_virtual_district(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置虚拟分区属性。"""
props = await req.json() props = await req.json()
return set_virtual_district(network, ChangeSet(props)) return set_virtual_district(network, ChangeSet(props))
@router.post("/addvirtualdistrict/", response_model=None) @router.post(
async def fastapi_add_virtual_district(network: str, req: Request) -> ChangeSet: "/addvirtualdistrict/",
response_model=None,
summary="添加新虚拟分区",
description="向水网添加一个新的虚拟分区"
)
async def fastapi_add_virtual_district(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""添加新的虚拟分区。"""
props = await req.json() props = await req.json()
return add_virtual_district(network, ChangeSet(props)) return add_virtual_district(network, ChangeSet(props))
@router.post("/deletevirtualdistrict/", response_model=None) @router.post(
async def fastapi_delete_virtual_district(network: str, req: Request) -> ChangeSet: "/deletevirtualdistrict/",
response_model=None,
summary="删除虚拟分区",
description="删除指定的虚拟分区"
)
async def fastapi_delete_virtual_district(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""删除虚拟分区。"""
props = await req.json() props = await req.json()
return delete_virtual_district(network, ChangeSet(props)) return delete_virtual_district(network, ChangeSet(props))
@router.get("/getallvirtualdistrict/") @router.get(
async def fastapi_get_all_virtual_district(network: str) -> list[dict[str, Any]]: "/getallvirtualdistrict/",
summary="获取所有虚拟分区",
description="获取指定水网中的所有虚拟分区信息"
)
async def fastapi_get_all_virtual_district(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""获取所有虚拟分区的信息列表。"""
return get_all_virtual_districts(network) return get_all_virtual_districts(network)
@router.post("/generatevirtualdistrict/", response_model=None) @router.post(
"/generatevirtualdistrict/",
response_model=None,
summary="生成虚拟分区",
description="根据参数自动生成虚拟分区方案"
)
async def fastapi_generate_virtual_district( async def fastapi_generate_virtual_district(
network: str, inflate_delta: float, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
inflate_delta: float = Query(..., description="膨胀参数"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""生成虚拟分区。"""
props = await req.json() props = await req.json()
return generate_virtual_district(network, props["centers"], inflate_delta) return generate_virtual_district(network, props["centers"], inflate_delta)
@router.get("/calculatedistrictmeteringareafornodes/") @router.get(
"/calculatedistrictmeteringareafornodes/",
summary="计算节点DMA分区",
description="为指定节点集计算区域计量(DMA)分区方案"
)
async def fastapi_calculate_district_metering_area_for_nodes( async def fastapi_calculate_district_metering_area_for_nodes(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> list[list[str]]: ) -> list[list[str]]:
"""
计算节点DMA分区。
请求体格式:
{
"nodes": 节点ID列表(list[str]),
"part_count": 分区数量(int),
"part_type": 分区类型(int)
}
"""
props = await req.json() props = await req.json()
nodes = props["nodes"] nodes = props["nodes"]
part_count = props["part_count"] part_count = props["part_count"]
+343 -35
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -13,102 +13,410 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getreservoirschema") @router.get(
async def fast_get_reservoir_schema(network: str) -> dict[str, dict[str, Any]]: "/getreservoirschema",
summary="获取水库模式",
description="获取指定供水网络中所有水库的模式/属性字段定义"
)
async def fast_get_reservoir_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取水库模式定义。
该端点返回指定网络中水库对象的模式定义,包括所有可用的属性字段。
Args:
network: 管网名称(或数据库名称)
Returns:
水库属性的模式定义字典
"""
return get_reservoir_schema(network) return get_reservoir_schema(network)
@router.post("/addreservoir/", response_model=None) @router.post(
"/addreservoir/",
response_model=None,
summary="添加水库",
description="在指定供水网络中添加新的水库/水源节点"
)
async def fastapi_add_reservoir( async def fastapi_add_reservoir(
network: str, reservoir: str, x: float, y: float, head: float network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
x: float = Query(..., description="水库的X坐标"),
y: float = Query(..., description="水库的Y坐标"),
head: float = Query(..., description="水库的水头/总水头(米)")
) -> ChangeSet: ) -> ChangeSet:
"""
添加新的水库/水源节点。
在指定的供水网络中创建一个新的水库,并设置其坐标和水头参数。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
x: 水库的X坐标位置
y: 水库的Y坐标位置
head: 水库的供水水头(以米为单位)
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir, "x": x, "y": y, "head": head} ps = {"id": reservoir, "x": x, "y": y, "head": head}
return add_reservoir(network, ChangeSet(ps)) return add_reservoir(network, ChangeSet(ps))
@router.post("/deletereservoir/", response_model=None) @router.post(
async def fastapi_delete_reservoir(network: str, reservoir: str) -> ChangeSet: "/deletereservoir/",
response_model=None,
summary="删除水库",
description="从指定供水网络中删除指定的水库/水源节点"
)
async def fastapi_delete_reservoir(
network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="要删除的水库的唯一标识符")
) -> ChangeSet:
"""
删除指定的水库节点。
从指定的供水网络中删除一个水库及其相关的所有连接关系。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir} ps = {"id": reservoir}
return delete_reservoir(network, ChangeSet(ps)) return delete_reservoir(network, ChangeSet(ps))
@router.get("/getreservoirhead/") @router.get(
async def fastapi_get_reservoir_head(network: str, reservoir: str) -> float | None: "/getreservoirhead/",
summary="获取水库水头",
description="获取指定水库的供水水头/总水头值"
)
async def fastapi_get_reservoir_head(
network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符")
) -> float | None:
"""
获取水库的水头参数。
返回指定水库的供水水头(总水头),单位为米。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
水库的水头值(米),如果水库不存在则返回None
"""
ps = get_reservoir(network, reservoir) ps = get_reservoir(network, reservoir)
return ps["head"] return ps["head"]
@router.get("/getreservoirpattern/") @router.get(
async def fastapi_get_reservoir_pattern(network: str, reservoir: str) -> str | None: "/getreservoirpattern/",
summary="获取水库模式",
description="获取指定水库的运行模式/供水模式"
)
async def fastapi_get_reservoir_pattern(
network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符")
) -> str | None:
"""
获取水库的运行模式。
返回指定水库的供水模式,如固定水头模式、时间序列模式等。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
水库的运行模式字符串,如果水库不存在则返回None
"""
ps = get_reservoir(network, reservoir) ps = get_reservoir(network, reservoir)
return ps["pattern"] return ps["pattern"]
@router.get("/getreservoirx/") @router.get(
"/getreservoirx/",
summary="获取水库X坐标",
description="获取指定水库的X坐标位置"
)
async def fastapi_get_reservoir_x( async def fastapi_get_reservoir_x(
network: str, reservoir: str network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符")
) -> dict[str, float] | None: ) -> dict[str, float] | None:
"""
获取水库的X坐标。
返回指定水库的X轴坐标值。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
水库的X坐标值,如果水库不存在则返回None
"""
ps = get_reservoir(network, reservoir) ps = get_reservoir(network, reservoir)
return ps["x"] return ps["x"]
@router.get("/getreservoiry/") @router.get(
"/getreservoiry/",
summary="获取水库Y坐标",
description="获取指定水库的Y坐标位置"
)
async def fastapi_get_reservoir_y( async def fastapi_get_reservoir_y(
network: str, reservoir: str network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符")
) -> dict[str, float] | None: ) -> dict[str, float] | None:
"""
获取水库的Y坐标。
返回指定水库的Y轴坐标值。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
水库的Y坐标值,如果水库不存在则返回None
"""
ps = get_reservoir(network, reservoir) ps = get_reservoir(network, reservoir)
return ps["y"] return ps["y"]
@router.get("/getreservoircoord/") @router.get(
"/getreservoircoord/",
summary="获取水库坐标",
description="获取指定水库的平面坐标(X和Y坐标)"
)
async def fastapi_get_reservoir_coord( async def fastapi_get_reservoir_coord(
network: str, reservoir: str network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符")
) -> dict[str, float] | None: ) -> dict[str, float] | None:
"""
获取水库的坐标。
返回指定水库的平面坐标,包含水库ID、X坐标和Y坐标。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
包含water库ID和X、Y坐标的字典,如果水库不存在则返回None
"""
ps = get_reservoir(network, reservoir) ps = get_reservoir(network, reservoir)
coord = {"id": reservoir, "x": ps["x"], "y": ps["y"]} coord = {"id": reservoir, "x": ps["x"], "y": ps["y"]}
return coord return coord
@router.post("/setreservoirhead/", response_model=None) @router.post(
"/setreservoirhead/",
response_model=None,
summary="设置水库水头",
description="更新指定水库的供水水头/总水头值"
)
async def fastapi_set_reservoir_head( async def fastapi_set_reservoir_head(
network: str, reservoir: str, head: float network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
head: float = Query(..., description="新的水头值(米)")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水库的水头参数。
更新指定水库的供水水头(总水头)值。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
head: 新的水头值(以米为单位)
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir, "head": head} ps = {"id": reservoir, "head": head}
return set_reservoir(network, ChangeSet(ps)) return set_reservoir(network, ChangeSet(ps))
@router.post("/setreservoirpattern/", response_model=None) @router.post(
"/setreservoirpattern/",
response_model=None,
summary="设置水库模式",
description="更新指定水库的运行模式/供水模式"
)
async def fastapi_set_reservoir_pattern( async def fastapi_set_reservoir_pattern(
network: str, reservoir: str, pattern: str network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
pattern: str = Query(..., description="新的运行模式")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水库的运行模式。
更新指定水库的供水模式,如固定水头模式、时间序列模式等。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
pattern: 新的运行模式字符串
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir, "pattern": pattern} ps = {"id": reservoir, "pattern": pattern}
return set_reservoir(network, ChangeSet(ps)) return set_reservoir(network, ChangeSet(ps))
@router.post("/setreservoirx/", response_model=None) @router.post(
async def fastapi_set_reservoir_x(network: str, reservoir: str, x: float) -> ChangeSet: "/setreservoirx/",
response_model=None,
summary="设置水库X坐标",
description="更新指定水库的X坐标位置"
)
async def fastapi_set_reservoir_x(
network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
x: float = Query(..., description="新的X坐标值")
) -> ChangeSet:
"""
设置水库的X坐标。
更新指定水库的X轴坐标值。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
x: 新的X坐标值
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir, "x": x} ps = {"id": reservoir, "x": x}
return set_reservoir(network, ChangeSet(ps)) return set_reservoir(network, ChangeSet(ps))
@router.post("/setreservoiry/", response_model=None) @router.post(
async def fastapi_set_reservoir_y(network: str, reservoir: str, y: float) -> ChangeSet: "/setreservoiry/",
response_model=None,
summary="设置水库Y坐标",
description="更新指定水库的Y坐标位置"
)
async def fastapi_set_reservoir_y(
network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
y: float = Query(..., description="新的Y坐标值")
) -> ChangeSet:
"""
设置水库的Y坐标。
更新指定水库的Y轴坐标值。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
y: 新的Y坐标值
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir, "y": y} ps = {"id": reservoir, "y": y}
return set_reservoir(network, ChangeSet(ps)) return set_reservoir(network, ChangeSet(ps))
@router.post("/setreservoircoord/", response_model=None) @router.post(
"/setreservoircoord/",
response_model=None,
summary="设置水库坐标",
description="更新指定水库的平面坐标(X和Y坐标)"
)
async def fastapi_set_reservoir_coord( async def fastapi_set_reservoir_coord(
network: str, reservoir: str, x: float, y: float network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
x: float = Query(..., description="新的X坐标值"),
y: float = Query(..., description="新的Y坐标值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水库的坐标。
更新指定水库的平面坐标,包括X和Y坐标。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
x: 新的X坐标值
y: 新的Y坐标值
Returns:
包含操作变更集的ChangeSet对象
"""
ps = {"id": reservoir, "x": x, "y": y} ps = {"id": reservoir, "x": x, "y": y}
return set_reservoir(network, ChangeSet(ps)) return set_reservoir(network, ChangeSet(ps))
@router.get("/getreservoirproperties/") @router.get(
"/getreservoirproperties/",
summary="获取水库属性",
description="获取指定水库的所有属性"
)
async def fastapi_get_reservoir_properties( async def fastapi_get_reservoir_properties(
network: str, reservoir: str network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
获取水库的所有属性。
返回指定水库的完整属性信息,包括ID、坐标、水头、模式等所有属性。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
Returns:
包含水库所有属性的字典
"""
return get_reservoir(network, reservoir) return get_reservoir(network, reservoir)
@router.get("/getallreservoirproperties/") @router.get(
async def fastapi_get_all_reservoir_properties(network: str) -> list[dict[str, Any]]: "/getallreservoirproperties/",
# 缓存查询结果提高性能 summary="获取所有水库属性",
# global redis_client description="获取指定供水网络中所有水库的属性"
)
async def fastapi_get_all_reservoir_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取所有水库的属性。
返回指定供水网络中所有水库的完整属性信息列表。
Args:
network: 管网名称(或数据库名称)
Returns:
包含所有水库属性的字典列表
"""
results = get_all_reservoirs(network) results = get_all_reservoirs(network)
return results return results
@router.post("/setreservoirproperties/", response_model=None) @router.post(
"/setreservoirproperties/",
response_model=None,
summary="设置水库属性",
description="批量更新指定水库的多个属性"
)
async def fastapi_set_reservoir_properties( async def fastapi_set_reservoir_properties(
network: str, reservoir: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
reservoir: str = Query(..., description="水库的唯一标识符"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
设置水库的多个属性。
批量更新指定水库的属性。属性通过JSON请求体传递。
Args:
network: 管网名称(或数据库名称)
reservoir: 水库的唯一标识符
req: HTTP请求对象,包含JSON格式的属性数据
Returns:
包含操作变更集的ChangeSet对象
"""
props = await req.json() props = await req.json()
ps = {"id": reservoir} | props ps = {"id": reservoir} | props
return set_reservoir(network, ChangeSet(ps)) return set_reservoir(network, ChangeSet(ps))
+41 -9
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -15,20 +15,52 @@ router = APIRouter()
# tag 8.[TAGS] # tag 8.[TAGS]
############################################################ ############################################################
@router.get("/gettagschema/") @router.get(
async def fastapi_get_tag_schema(network: str) -> dict[str, dict[str, Any]]: "/gettagschema/",
summary="获取标签属性架构",
description="获取指定水网的标签(Tag)属性架构定义"
)
async def fastapi_get_tag_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""获取标签的属性架构。"""
return get_tag_schema(network) return get_tag_schema(network)
@router.get("/gettag/") @router.get(
async def fastapi_get_tag(network: str, t_type: str, id: str) -> dict[str, Any]: "/gettag/",
summary="获取标签信息",
description="获取指定类型和ID的标签信息"
)
async def fastapi_get_tag(
network: str = Query(..., description="管网名称(或数据库名称)"),
t_type: str = Query(..., description="标签类型"),
id: str = Query(..., description="元素ID")
) -> dict[str, Any]:
"""获取标签信息。"""
return get_tag(network, t_type, id) return get_tag(network, t_type, id)
@router.get("/gettags/") @router.get(
async def fastapi_get_tags(network: str) -> list[dict[str, Any]]: "/gettags/",
summary="获取所有标签",
description="获取指定水网中的所有标签信息"
)
async def fastapi_get_tags(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""获取水网中所有标签的列表。"""
tags = get_tags(network) tags = get_tags(network)
return tags return tags
@router.post("/settag/", response_model=None) @router.post(
async def fastapi_set_tag(network: str, req: Request) -> ChangeSet: "/settag/",
response_model=None,
summary="设置标签",
description="为指定元素设置或修改标签信息"
)
async def fastapi_set_tag(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""设置标签信息。"""
props = await req.json() props = await req.json()
return set_tag(network, ChangeSet(props)) return set_tag(network, ChangeSet(props))
+435 -66
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -13,23 +13,50 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/gettankschema") @router.get("/gettankschema", summary="获取水箱模式", description="获取指定网络的水箱数据结构模式定义")
async def fast_get_tank_schema(network: str) -> dict[str, dict[str, Any]]: async def fast_get_tank_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[str, Any]]:
"""
获取水箱的数据结构模式。
Args:
network: 管网名称(或数据库名称)
Returns:
包含水箱属性的模式定义字典
"""
return get_tank_schema(network) return get_tank_schema(network)
@router.post("/addtank/", response_model=None) @router.post("/addtank/", summary="新增水箱", description="向指定网络中新增一个水箱", response_model=None)
async def fastapi_add_tank( async def fastapi_add_tank(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str, tank: str = Query(..., description="水箱ID"),
x: float, x: float = Query(..., description="X坐标"),
y: float, y: float = Query(..., description="Y坐标"),
elevation: float, elevation: float = Query(..., description="标高"),
init_level: float = 0, init_level: float = Query(0, description="初始水位"),
min_level: float = 0, min_level: float = Query(0, description="最小水位"),
max_level: float = 0, max_level: float = Query(0, description="最大水位"),
diameter: float = 0, diameter: float = Query(0, description="直径"),
min_vol: float = 0, min_vol: float = Query(0, description="最小体积"),
) -> ChangeSet: ) -> ChangeSet:
"""
创建新水箱。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
x: X坐标
y: Y坐标
elevation: 水箱标高
init_level: 初始水位,默认为0
min_level: 最小水位,默认为0
max_level: 最大水位,默认为0
diameter: 水箱直径,默认为0
min_vol: 最小体积,默认为0
Returns:
包含变更信息的ChangeSet对象
"""
ps = { ps = {
"id": tank, "id": tank,
"x": x, "x": x,
@@ -43,155 +70,497 @@ async def fastapi_add_tank(
} }
return add_tank(network, ChangeSet(ps)) return add_tank(network, ChangeSet(ps))
@router.post("/deletetank/", response_model=None) @router.post("/deletetank/", summary="删除水箱", description="删除指定网络中的水箱", response_model=None)
async def fastapi_delete_tank(network: str, tank: str) -> ChangeSet: async def fastapi_delete_tank(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> ChangeSet:
"""
删除指定的水箱。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank} ps = {"id": tank}
return delete_tank(network, ChangeSet(ps)) return delete_tank(network, ChangeSet(ps))
@router.get("/gettankelevation/") @router.get("/gettankelevation/", summary="获取水箱标高", description="获取指定水箱的标高值")
async def fastapi_get_tank_elevation(network: str, tank: str) -> float | None: async def fastapi_get_tank_elevation(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float | None:
"""
获取水箱的标高。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱标高值,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["elevation"] return ps["elevation"]
@router.get("/gettankinitlevel/") @router.get("/gettankinitlevel/", summary="获取水箱初始水位", description="获取指定水箱的初始水位值")
async def fastapi_get_tank_init_level(network: str, tank: str) -> float | None: async def fastapi_get_tank_init_level(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float | None:
"""
获取水箱的初始水位。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱初始水位值,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["init_level"] return ps["init_level"]
@router.get("/gettankminlevel/") @router.get("/gettankminlevel/", summary="获取水箱最小水位", description="获取指定水箱的最小水位值")
async def fastapi_get_tank_min_level(network: str, tank: str) -> float | None: async def fastapi_get_tank_min_level(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float | None:
"""
获取水箱的最小水位。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱最小水位值,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["min_level"] return ps["min_level"]
@router.get("/gettankmaxlevel/") @router.get("/gettankmaxlevel/", summary="获取水箱最大水位", description="获取指定水箱的最大水位值")
async def fastapi_get_tank_max_level(network: str, tank: str) -> float | None: async def fastapi_get_tank_max_level(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float | None:
"""
获取水箱的最大水位。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱最大水位值,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["max_level"] return ps["max_level"]
@router.get("/gettankdiameter/") @router.get("/gettankdiameter/", summary="获取水箱直径", description="获取指定水箱的直径值")
async def fastapi_get_tank_diameter(network: str, tank: str) -> float | None: async def fastapi_get_tank_diameter(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float | None:
"""
获取水箱的直径。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱直径值,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["diameter"] return ps["diameter"]
@router.get("/gettankminvol/") @router.get("/gettankminvol/", summary="获取水箱最小体积", description="获取指定水箱的最小体积值")
async def fastapi_get_tank_min_vol(network: str, tank: str) -> float | None: async def fastapi_get_tank_min_vol(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float | None:
"""
获取水箱的最小体积。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱最小体积值,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["min_vol"] return ps["min_vol"]
@router.get("/gettankvolcurve/") @router.get("/gettankvolcurve/", summary="获取水箱容积曲线", description="获取指定水箱的容积曲线标识")
async def fastapi_get_tank_vol_curve(network: str, tank: str) -> str | None: async def fastapi_get_tank_vol_curve(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> str | None:
"""
获取水箱的容积曲线。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱容积曲线标识,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["vol_curve"] return ps["vol_curve"]
@router.get("/gettankoverflow/") @router.get("/gettankoverflow/", summary="获取水箱溢流口", description="获取指定水箱的溢流口配置")
async def fastapi_get_tank_overflow(network: str, tank: str) -> str | None: async def fastapi_get_tank_overflow(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> str | None:
"""
获取水箱的溢流口配置。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱溢流口配置,如果不存在则返回None
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["overflow"] return ps["overflow"]
@router.get("/gettankx/") @router.get("/gettankx/", summary="获取水箱X坐标", description="获取指定水箱的X坐标值")
async def fastapi_get_tank_x(network: str, tank: str) -> float: async def fastapi_get_tank_x(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float:
"""
获取水箱的X坐标。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱X坐标值
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["x"] return ps["x"]
@router.get("/gettanky/") @router.get("/gettanky/", summary="获取水箱Y坐标", description="获取指定水箱的Y坐标值")
async def fastapi_get_tank_y(network: str, tank: str) -> float: async def fastapi_get_tank_y(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> float:
"""
获取水箱的Y坐标。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
水箱Y坐标值
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
return ps["y"] return ps["y"]
@router.get("/gettankcoord/") @router.get("/gettankcoord/", summary="获取水箱坐标", description="获取指定水箱的X和Y坐标")
async def fastapi_get_tank_coord(network: str, tank: str) -> dict[str, float]: async def fastapi_get_tank_coord(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> dict[str, float]:
"""
获取水箱的坐标。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
包含x和y坐标的字典
"""
ps = get_tank(network, tank) ps = get_tank(network, tank)
coord = {"x": ps["x"], "y": ps["y"]} coord = {"x": ps["x"], "y": ps["y"]}
return coord return coord
@router.post("/settankelevation/", response_model=None) @router.post("/settankelevation/", summary="设置水箱标高", description="设置指定水箱的标高值", response_model=None)
async def fastapi_set_tank_elevation( async def fastapi_set_tank_elevation(
network: str, tank: str, elevation: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
elevation: float = Query(..., description="新的标高值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的标高。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
elevation: 新的标高值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "elevation": elevation} ps = {"id": tank, "elevation": elevation}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankinitlevel/", response_model=None) @router.post("/settankinitlevel/", summary="设置水箱初始水位", description="设置指定水箱的初始水位值", response_model=None)
async def fastapi_set_tank_init_level( async def fastapi_set_tank_init_level(
network: str, tank: str, init_level: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
init_level: float = Query(..., description="新的初始水位值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的初始水位。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
init_level: 新的初始水位值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "init_level": init_level} ps = {"id": tank, "init_level": init_level}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankminlevel/", response_model=None) @router.post("/settankminlevel/", summary="设置水箱最小水位", description="设置指定水箱的最小水位值", response_model=None)
async def fastapi_set_tank_min_level( async def fastapi_set_tank_min_level(
network: str, tank: str, min_level: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
min_level: float = Query(..., description="新的最小水位值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的最小水位。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
min_level: 新的最小水位值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "min_level": min_level} ps = {"id": tank, "min_level": min_level}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankmaxlevel/", response_model=None) @router.post("/settankmaxlevel/", summary="设置水箱最大水位", description="设置指定水箱的最大水位值", response_model=None)
async def fastapi_set_tank_max_level( async def fastapi_set_tank_max_level(
network: str, tank: str, max_level: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
max_level: float = Query(..., description="新的最大水位值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的最大水位。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
max_level: 新的最大水位值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "max_level": max_level} ps = {"id": tank, "max_level": max_level}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("settankdiameter//", response_model=None) @router.post("/settankdiameter/", summary="设置水箱直径", description="设置指定水箱的直径值", response_model=None)
async def fastapi_set_tank_diameter( async def fastapi_set_tank_diameter(
network: str, tank: str, diameter: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
diameter: float = Query(..., description="新的直径值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的直径。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
diameter: 新的直径值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "diameter": diameter} ps = {"id": tank, "diameter": diameter}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankminvol/", response_model=None) @router.post("/settankminvol/", summary="设置水箱最小体积", description="设置指定水箱的最小体积值", response_model=None)
async def fastapi_set_tank_min_vol( async def fastapi_set_tank_min_vol(
network: str, tank: str, min_vol: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
min_vol: float = Query(..., description="新的最小体积值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的最小体积。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
min_vol: 新的最小体积值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "min_vol": min_vol} ps = {"id": tank, "min_vol": min_vol}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankvolcurve/", response_model=None) @router.post("/settankvolcurve/", summary="设置水箱容积曲线", description="设置指定水箱的容积曲线标识", response_model=None)
async def fastapi_set_tank_vol_curve( async def fastapi_set_tank_vol_curve(
network: str, tank: str, vol_curve: str network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
vol_curve: str = Query(..., description="新的容积曲线标识")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的容积曲线。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
vol_curve: 新的容积曲线标识
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "vol_curve": vol_curve} ps = {"id": tank, "vol_curve": vol_curve}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankoverflow/", response_model=None) @router.post("/settankoverflow/", summary="设置水箱溢流口", description="设置指定水箱的溢流口配置", response_model=None)
async def fastapi_set_tank_overflow( async def fastapi_set_tank_overflow(
network: str, tank: str, overflow: str network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
overflow: str = Query(..., description="新的溢流口配置")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的溢流口配置。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
overflow: 新的溢流口配置
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "overflow": overflow} ps = {"id": tank, "overflow": overflow}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankx/", response_model=None) @router.post("/settankx/", summary="设置水箱X坐标", description="设置指定水箱的X坐标值", response_model=None)
async def fastapi_set_tank_x(network: str, tank: str, x: float) -> ChangeSet: async def fastapi_set_tank_x(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
x: float = Query(..., description="新的X坐标值")
) -> ChangeSet:
"""
设置水箱的X坐标。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
x: 新的X坐标值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "x": x} ps = {"id": tank, "x": x}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settanky/", response_model=None) @router.post("/settanky/", summary="设置水箱Y坐标", description="设置指定水箱的Y坐标值", response_model=None)
async def fastapi_set_tank_y(network: str, tank: str, y: float) -> ChangeSet: async def fastapi_set_tank_y(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
y: float = Query(..., description="新的Y坐标值")
) -> ChangeSet:
"""
设置水箱的Y坐标。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
y: 新的Y坐标值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "y": y} ps = {"id": tank, "y": y}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.post("/settankcoord/", response_model=None) @router.post("/settankcoord/", summary="设置水箱坐标", description="设置指定水箱的X和Y坐标", response_model=None)
async def fastapi_set_tank_coord( async def fastapi_set_tank_coord(
network: str, tank: str, x: float, y: float network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
x: float = Query(..., description="新的X坐标值"),
y: float = Query(..., description="新的Y坐标值")
) -> ChangeSet: ) -> ChangeSet:
"""
设置水箱的坐标。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
x: 新的X坐标值
y: 新的Y坐标值
Returns:
包含变更信息的ChangeSet对象
"""
ps = {"id": tank, "x": x, "y": y} ps = {"id": tank, "x": x, "y": y}
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
@router.get("/gettankproperties/") @router.get("/gettankproperties/", summary="获取水箱属性", description="获取指定水箱的所有属性")
async def fastapi_get_tank_properties(network: str, tank: str) -> dict[str, Any]: async def fastapi_get_tank_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID")
) -> dict[str, Any]:
"""
获取水箱的所有属性。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
Returns:
包含水箱所有属性的字典
"""
return get_tank(network, tank) return get_tank(network, tank)
@router.get("/getalltankproperties/") @router.get("/getalltankproperties/", summary="获取所有水箱属性", description="获取指定网络中所有水箱的属性")
async def fastapi_get_all_tank_properties(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_tank_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取网络中所有水箱的属性。
Args:
network: 管网名称(或数据库名称)
Returns:
包含所有水箱属性的字典列表
"""
# 缓存查询结果提高性能 # 缓存查询结果提高性能
# global redis_client # global redis_client
results = get_all_tanks(network) results = get_all_tanks(network)
return results return results
@router.post("/settankproperties/", response_model=None) @router.post("/settankproperties/", summary="设置水箱属性", description="批量设置指定水箱的多个属性", response_model=None)
async def fastapi_set_tank_properties( async def fastapi_set_tank_properties(
network: str, tank: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
tank: str = Query(..., description="水箱ID"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
批量设置水箱的属性。
Args:
network: 管网名称(或数据库名称)
tank: 水箱ID
req: 包含水箱属性的请求体(JSON格式)
Returns:
包含变更信息的ChangeSet对象
"""
props = await req.json() props = await req.json()
ps = {"id": tank} | props ps = {"id": tank} | props
return set_tank(network, ChangeSet(ps)) return set_tank(network, ChangeSet(ps))
+249 -42
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query, Path, Body
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import ( from app.services.tjnetwork import (
Any, Any,
@@ -14,21 +14,42 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getvalveschema") @router.get(
async def fastapi_get_valve_schema(network: str) -> dict[str, dict[str, Any]]: "/getvalveschema",
summary="获取阀门架构",
description="获取指定水网中所有阀门的架构和字段定义",
)
async def fastapi_get_valve_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取阀门架构。
返回指定水网中所有阀门类型的完整架构定义,包括字段名称、类型和默认值。
"""
return get_valve_schema(network) return get_valve_schema(network)
@router.post("/addvalve/", response_model=None) @router.post(
"/addvalve/",
response_model=None,
summary="添加阀门",
description="在指定的水网中添加新的阀门",
)
async def fastapi_add_valve( async def fastapi_add_valve(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str, valve: str = Query(..., description="阀门ID"),
node1: str, node1: str = Query(..., description="起点节点ID"),
node2: str, node2: str = Query(..., description="终点节点ID"),
diameter: float = 0, diameter: float = Query(0, description="阀门直径(mm"),
v_type: str = VALVES_TYPE_PRV, v_type: str = Query(VALVES_TYPE_PRV, description="阀门类型"),
setting: float = 0, setting: float = Query(0, description="阀门开度/设置值"),
minor_loss: float = 0, minor_loss: float = Query(0, description="损失系数"),
) -> ChangeSet: ) -> ChangeSet:
"""
添加新的阀门。
在指定的水网中创建一个新的阀门,设置其连接的两个节点、直径、类型、开度和损失系数。
"""
ps = { ps = {
"id": valve, "id": valve,
"node1": node1, "node1": node1,
@@ -41,85 +62,271 @@ async def fastapi_add_valve(
return add_valve(network, ChangeSet(ps)) return add_valve(network, ChangeSet(ps))
@router.post("/deletevalve/", response_model=None) @router.post(
async def fastapi_delete_valve(network: str, valve: str) -> ChangeSet: "/deletevalve/",
response_model=None,
summary="删除阀门",
description="从指定的水网中删除指定的阀门",
)
async def fastapi_delete_valve(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> ChangeSet:
"""
删除阀门。
从指定的水网中删除指定ID的阀门。
"""
ps = {"id": valve} ps = {"id": valve}
return delete_valve(network, ChangeSet(ps)) return delete_valve(network, ChangeSet(ps))
@router.get("/getvalvenode1/") @router.get(
async def fastapi_get_valve_node1(network: str, valve: str) -> str | None: "/getvalvenode1/",
summary="获取阀门起点节点",
description="获取指定阀门连接的起点节点ID",
)
async def fastapi_get_valve_node1(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> str | None:
"""
获取阀门的起点节点。
返回指定阀门连接的起点(第一个)节点的ID。
"""
ps = get_valve(network, valve) ps = get_valve(network, valve)
return ps["node1"] return ps["node1"]
@router.get("/getvalvenode2/") @router.get(
async def fastapi_get_valve_node2(network: str, valve: str) -> str | None: "/getvalvenode2/",
summary="获取阀门终点节点",
description="获取指定阀门连接的终点节点ID",
)
async def fastapi_get_valve_node2(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> str | None:
"""
获取阀门的终点节点。
返回指定阀门连接的终点(第二个)节点的ID。
"""
ps = get_valve(network, valve) ps = get_valve(network, valve)
return ps["node2"] return ps["node2"]
@router.get("/getvalvediameter/") @router.get(
async def fastapi_get_valve_diameter(network: str, valve: str) -> float | None: "/getvalvediameter/",
summary="获取阀门直径",
description="获取指定阀门的直径",
)
async def fastapi_get_valve_diameter(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> float | None:
"""
获取阀门的直径。
返回指定阀门的直径值(单位:mm)。
"""
ps = get_valve(network, valve) ps = get_valve(network, valve)
return ps["diameter"] return ps["diameter"]
@router.get("/getvalvetype/") @router.get(
async def fastapi_get_valve_type(network: str, valve: str) -> str | None: "/getvalvetype/",
summary="获取阀门类型",
description="获取指定阀门的类型",
)
async def fastapi_get_valve_type(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> str | None:
"""
获取阀门的类型。
返回指定阀门的类型(例如:减压阀、调节阀等)。
"""
ps = get_valve(network, valve) ps = get_valve(network, valve)
return ps["type"] return ps["type"]
@router.get("/getvalvesetting/") @router.get(
async def fastapi_get_valve_setting(network: str, valve: str) -> float | None: "/getvalvesetting/",
summary="获取阀门开度",
description="获取指定阀门的开度/设置值",
)
async def fastapi_get_valve_setting(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> float | None:
"""
获取阀门的开度。
返回指定阀门的开度/设置值。
"""
ps = get_valve(network, valve) ps = get_valve(network, valve)
return ps["setting"] return ps["setting"]
@router.get("/getvalveminorloss/") @router.get(
async def fastapi_get_valve_minor_loss(network: str, valve: str) -> float | None: "/getvalveminorloss/",
summary="获取阀门损失系数",
description="获取指定阀门的损失系数",
)
async def fastapi_get_valve_minor_loss(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> float | None:
"""
获取阀门的损失系数。
返回指定阀门的损失系数值,用于计算流体通过阀门的压力损失。
"""
ps = get_valve(network, valve) ps = get_valve(network, valve)
return ps["minor_loss"] return ps["minor_loss"]
@router.post("/setvalvenode1/", response_model=None) @router.post(
async def fastapi_set_valve_node1(network: str, valve: str, node1: str) -> ChangeSet: "/setvalvenode1/",
response_model=None,
summary="设置阀门起点节点",
description="设置指定阀门的起点节点",
)
async def fastapi_set_valve_node1(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
node1: str = Query(..., description="新的起点节点ID"),
) -> ChangeSet:
"""
设置阀门的起点节点。
更新指定阀门的起点节点连接。
"""
ps = {"id": valve, "node1": node1} ps = {"id": valve, "node1": node1}
return set_valve(network, ChangeSet(ps)) return set_valve(network, ChangeSet(ps))
@router.post("/setvalvenode2/", response_model=None) @router.post(
async def fastapi_set_valve_node2(network: str, valve: str, node2: str) -> ChangeSet: "/setvalvenode2/",
response_model=None,
summary="设置阀门终点节点",
description="设置指定阀门的终点节点",
)
async def fastapi_set_valve_node2(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
node2: str = Query(..., description="新的终点节点ID"),
) -> ChangeSet:
"""
设置阀门的终点节点。
更新指定阀门的终点节点连接。
"""
ps = {"id": valve, "node2": node2} ps = {"id": valve, "node2": node2}
return set_valve(network, ChangeSet(ps)) return set_valve(network, ChangeSet(ps))
@router.post("/setvalvenodediameter/", response_model=None) @router.post(
"/setvalvenodediameter/",
response_model=None,
summary="设置阀门直径",
description="设置指定阀门的直径",
)
async def fastapi_set_valve_diameter( async def fastapi_set_valve_diameter(
network: str, valve: str, diameter: float network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
diameter: float = Query(..., description="新的直径值(mm"),
) -> ChangeSet: ) -> ChangeSet:
"""
设置阀门的直径。
更新指定阀门的直径值。
"""
ps = {"id": valve, "diameter": diameter} ps = {"id": valve, "diameter": diameter}
return set_valve(network, ChangeSet(ps)) return set_valve(network, ChangeSet(ps))
@router.post("/setvalvetype/", response_model=None) @router.post(
async def fastapi_set_valve_type(network: str, valve: str, type: str) -> ChangeSet: "/setvalvetype/",
response_model=None,
summary="设置阀门类型",
description="设置指定阀门的类型",
)
async def fastapi_set_valve_type(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
type: str = Query(..., description="新的阀门类型"),
) -> ChangeSet:
"""
设置阀门的类型。
更新指定阀门的类型(例如:减压阀、调节阀等)。
"""
ps = {"id": valve, "type": type} ps = {"id": valve, "type": type}
return set_valve(network, ChangeSet(ps)) return set_valve(network, ChangeSet(ps))
@router.post("/setvalvesetting/", response_model=None) @router.post(
"/setvalvesetting/",
response_model=None,
summary="设置阀门开度",
description="设置指定阀门的开度/设置值",
)
async def fastapi_set_valve_setting( async def fastapi_set_valve_setting(
network: str, valve: str, setting: float network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
setting: float = Query(..., description="新的开度值"),
) -> ChangeSet: ) -> ChangeSet:
"""
设置阀门的开度。
更新指定阀门的开度/设置值。
"""
ps = {"id": valve, "setting": setting} ps = {"id": valve, "setting": setting}
return set_valve(network, ChangeSet(ps)) return set_valve(network, ChangeSet(ps))
@router.get("/getvalveproperties/") @router.get(
async def fastapi_get_valve_properties(network: str, valve: str) -> dict[str, Any]: "/getvalveproperties/",
summary="获取阀门所有属性",
description="获取指定阀门的所有属性",
)
async def fastapi_get_valve_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
) -> dict[str, Any]:
"""
获取阀门的所有属性。
返回指定阀门的完整属性集合,包括ID、节点、直径、类型、开度和损失系数。
"""
return get_valve(network, valve) return get_valve(network, valve)
@router.get("/getallvalveproperties/") @router.get(
async def fastapi_get_all_valve_properties(network: str) -> list[dict[str, Any]]: "/getallvalveproperties/",
summary="获取所有阀门属性",
description="获取指定水网中所有阀门的属性",
)
async def fastapi_get_all_valve_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取所有阀门的属性。
返回指定水网中所有阀门的完整属性列表。
"""
# 缓存查询结果提高性能 # 缓存查询结果提高性能
# global redis_client # global redis_client
results = get_all_valves(network) results = get_all_valves(network)
return results return results
@router.post("/setvalveproperties/", response_model=None) @router.post(
"/setvalveproperties/",
response_model=None,
summary="批量设置阀门属性",
description="批量设置指定阀门的多个属性",
)
async def fastapi_set_valve_properties( async def fastapi_set_valve_properties(
network: str, valve: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
valve: str = Query(..., description="阀门ID"),
req: Request = None,
) -> ChangeSet: ) -> ChangeSet:
"""
批量设置阀门的属性。
更新指定阀门的一个或多个属性,通过JSON请求体传递要更新的属性。
"""
props = await req.json() props = await req.json()
ps = {"id": valve} | props ps = {"id": valve} | props
return set_valve(network, ChangeSet(ps)) return set_valve(network, ChangeSet(ps))
+391 -39
View File
@@ -1,7 +1,10 @@
import json import json
from fastapi import APIRouter, Request, HTTPException from fastapi import APIRouter, Request, HTTPException, Query, Path, Body, Depends
from fastapi.responses import PlainTextResponse from fastapi.responses import PlainTextResponse
from typing import Any, Dict from typing import Any, Dict, List
from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository
from app.auth.project_dependencies import get_metadata_repository
from app.domain.schemas.metadata import ProjectMetaResponse
import app.services.project_info as project_info import app.services.project_info as project_info
from app.infra.db.postgresql.database import get_database_instance as get_pg_db from app.infra.db.postgresql.database import get_database_instance as get_pg_db
from app.infra.db.timescaledb.database import get_database_instance as get_ts_db from app.infra.db.timescaledb.database import get_database_instance as get_ts_db
@@ -39,30 +42,97 @@ inpDir = "data/" # Assuming data directory exists or is defined somewhere.
router = APIRouter() router = APIRouter()
lockedPrjs: Dict[str, str] = {} lockedPrjs: Dict[str, str] = {}
@router.get("/listprojects/") @router.get("/project-info", summary="获取项目信息", description="从数据库获取项目的详细信息,包括地图范围等。", response_model=ProjectMetaResponse)
@router.get("/project_info/", summary="获取项目信息(旧路径)", description="从数据库获取项目的详细信息,包括地图范围等。", response_model=ProjectMetaResponse, deprecated=True)
async def get_project_info_endpoint(
network: str = Query(..., description="管网名称(或项目代码)"),
metadata_repo: MetadataRepository = Depends(get_metadata_repository),
):
"""
获取项目信息
- **network**: 管网名称(或项目代码)
"""
project_detail = await metadata_repo.get_project_detail_by_code(network)
if not project_detail:
raise HTTPException(status_code=404, detail=f"Project {network} not found")
return ProjectMetaResponse(
project_id=project_detail.project_id,
name=project_detail.name,
code=project_detail.code,
description=project_detail.description,
gs_workspace=project_detail.gs_workspace,
map_extent=project_detail.map_extent,
status=project_detail.status,
project_role="viewer",
)
@router.get("/listprojects/", summary="获取项目列表", description="获取服务器上所有可用的供水管网项目名称列表。")
async def list_projects_endpoint() -> list[str]: async def list_projects_endpoint() -> list[str]:
"""
获取项目列表
返回所有已创建项目的名称列表。
"""
return list_project() return list_project()
@router.get("/haveproject/") @router.get("/haveproject/", summary="检查项目是否存在", description="检查指定名称的项目是否存在。")
async def have_project_endpoint(network: str): async def have_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
检查项目是否存在
- **network**: 管网名称(或数据库名称)
"""
return have_project(network) return have_project(network)
@router.post("/createproject/") @router.post("/createproject/", summary="创建新项目", description="创建一个新的供水管网项目。如果项目已存在,可能会覆盖或报错(取决于底层实现)。")
async def create_project_endpoint(network: str): async def create_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
创建新项目
- **network**: 管网名称(或数据库名称)
"""
create_project(network) create_project(network)
return network return network
@router.post("/deleteproject/") @router.post("/deleteproject/", summary="删除项目", description="永久删除指定的供水管网项目。此操作不可恢复。")
async def delete_project_endpoint(network: str): async def delete_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
删除项目
- **network**: 管网名称(或数据库名称)
"""
delete_project(network) delete_project(network)
return True return True
@router.get("/isprojectopen/") @router.get("/isprojectopen/", summary="检查项目是否已打开", description="检查指定项目是否已被加载到内存中。")
async def is_project_open_endpoint(network: str): async def is_project_open_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
检查项目是否已打开
- **network**: 管网名称(或数据库名称)
"""
return is_project_open(network) return is_project_open(network)
@router.post("/openproject/") @router.post("/projects/open", summary="打开项目", description="将指定项目加载到内存中,并初始化数据库连接池。")
async def open_project_endpoint(network: str): @router.post("/openproject/", summary="打开项目(旧路径)", description="将指定项目加载到内存中,并初始化数据库连接池。", deprecated=True)
async def open_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
打开项目
- **network**: 管网名称(或数据库名称)
"""
open_project(network) open_project(network)
# 尝试连接指定数据库 # 尝试连接指定数据库
@@ -88,18 +158,43 @@ async def open_project_endpoint(network: str):
return network return network
@router.post("/closeproject/") @router.post("/closeproject/", summary="关闭项目", description="将指定项目从内存中卸载,释放资源。")
async def close_project_endpoint(network: str): async def close_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
关闭项目
- **network**: 管网名称(或数据库名称)
"""
close_project(network) close_project(network)
return True return True
@router.post("/copyproject/") @router.post("/copyproject/", summary="复制项目", description="将现有项目复制为新项目。")
async def copy_project_endpoint(source: str, target: str): async def copy_project_endpoint(
source: str = Query(..., description="管网名称(或数据库名称)"),
target: str = Query(..., description="管网名称(或数据库名称)")
):
"""
复制项目
- **source**: 管网名称(或数据库名称)
- **target**: 管网名称(或数据库名称)
"""
copy_project(source, target) copy_project(source, target)
return True return True
@router.post("/importinp/") @router.post("/importinp/", summary="导入 INP 文件内容", description="将 INP 格式的文本内容导入到指定项目中。")
async def import_inp_endpoint(network: str, req: Request): async def import_inp_endpoint(
req: Request,
network: str = Query(..., description="管网名称(或数据库名称)")
):
"""
导入 INP 文件内容
- **network**: 管网名称(或数据库名称)
- **req**: 请求体,需包含 `{"inp": "..."}` 结构
"""
jo_root = await req.json() jo_root = await req.json()
inp_text = jo_root["inp"] inp_text = jo_root["inp"]
ps = {"inp": inp_text} ps = {"inp": inp_text}
@@ -107,8 +202,17 @@ async def import_inp_endpoint(network: str, req: Request):
print(ret) print(ret)
return ret return ret
@router.get("/exportinp/", response_model=None) @router.get("/exportinp/", response_model=None, summary="导出项目为 ChangeSet", description="导出项目的变更集 (ChangeSet),包含顶点、SCADA 元素、DMA、SA、VD 等信息。")
async def export_inp_endpoint(network: str, version: str) -> ChangeSet: async def export_inp_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
version: str = Query(..., description="版本号 (通常用于增量更新)")
) -> ChangeSet:
"""
导出项目为 ChangeSet
- **network**: 管网名称(或数据库名称)
- **version**: 版本号
"""
cs = export_inp(network, version) cs = export_inp(network, version)
op = cs.operations[0] op = cs.operations[0]
open_project(network) open_project(network)
@@ -131,30 +235,75 @@ async def export_inp_endpoint(network: str, version: str) -> ChangeSet:
return cs return cs
@router.post("/readinp/") @router.post("/readinp/", summary="读取 INP 文件到项目", description="从服务器文件系统中读取指定的 INP 文件并加载到项目中。")
async def read_inp_endpoint(network: str, inp: str) -> bool: async def read_inp_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
inp: str = Query(..., description="INP 文件名 (不包含路径)")
) -> bool:
"""
读取 INP 文件到项目
- **network**: 管网名称(或数据库名称)
- **inp**: INP 文件名
"""
read_inp(network, inp) read_inp(network, inp)
return True return True
@router.get("/dumpinp/") @router.get("/dumpinp/", summary="导出项目到 INP 文件", description="将项目当前状态保存为 INP 文件到服务器文件系统。")
async def dump_inp_endpoint(network: str, inp: str) -> bool: async def dump_inp_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
inp: str = Query(..., description="目标文件名")
) -> bool:
"""
导出项目到 INP 文件
- **network**: 管网名称(或数据库名称)
- **inp**: 目标文件名
"""
dump_inp(network, inp) dump_inp(network, inp)
return True return True
@router.get("/isprojectlocked/") @router.get("/isprojectlocked/", summary="检查项目是否被锁定", description="检查指定项目是否处于锁定状态。")
async def is_project_locked_endpoint(network: str, req: Request): async def is_project_locked_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
检查项目是否被锁定
- **network**: 管网名称(或数据库名称)
"""
return network in lockedPrjs.keys() return network in lockedPrjs.keys()
@router.get("/isprojectlockedbyme/") @router.get("/isprojectlockedbyme/", summary="检查项目是否被当前用户锁定", description="检查指定项目是否被当前客户端 (IP) 锁定。")
async def is_project_locked_by_me_endpoint(network: str, req: Request): async def is_project_locked_by_me_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
检查项目是否被当前用户锁定
- **network**: 管网名称(或数据库名称)
"""
client_host = req.client.host client_host = req.client.host
return lockedPrjs.get(network) == client_host return lockedPrjs.get(network) == client_host
# 0 successfully locked # 0 successfully locked
# 1 already locked by you # 1 already locked by you
# 2 locked by others # 2 locked by others
@router.post("/lockproject/") @router.post("/lockproject/", summary="锁定项目", description="锁定指定项目以防止并发修改。")
async def lock_project_endpoint(network: str, req: Request): async def lock_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
锁定项目
返回值:
- **0**: 锁定成功
- **1**: 已被当前用户锁定
- **2**: 已被其他用户锁定
"""
client_host = req.client.host client_host = req.client.host
if not network in lockedPrjs.keys(): if not network in lockedPrjs.keys():
lockedPrjs[network] = client_host lockedPrjs[network] = client_host
@@ -165,8 +314,16 @@ async def lock_project_endpoint(network: str, req: Request):
else: else:
return 2 return 2
@router.post("/unlockproject/") @router.post("/unlockproject/", summary="解锁项目", description="释放对项目的锁定。")
def unlock_project_endpoint(network: str, req: Request): def unlock_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
解锁项目
只有锁定者才能解锁。
"""
client_host = req.client.host client_host = req.client.host
if lockedPrjs.get(network) == client_host: if lockedPrjs.get(network) == client_host:
print("delete key") print("delete key")
@@ -176,8 +333,17 @@ def unlock_project_endpoint(network: str, req: Request):
return False return False
# inp file operations # inp file operations
@router.post("/uploadinp/", status_code=status.HTTP_200_OK) @router.post("/uploadinp/", status_code=status.HTTP_200_OK, summary="上传 INP 文件", description="上传 INP 文件到服务器数据目录。")
async def fastapi_upload_inp(afile: bytes, name: str): async def fastapi_upload_inp(
afile: bytes = Body(..., description="文件二进制内容"),
name: str = Query(..., description="保存的文件名")
):
"""
上传 INP 文件
- **afile**: 文件内容
- **name**: 文件名
"""
if not os.path.exists(inpDir): if not os.path.exists(inpDir):
os.makedirs(inpDir, exist_ok=True) os.makedirs(inpDir, exist_ok=True)
@@ -186,8 +352,16 @@ async def fastapi_upload_inp(afile: bytes, name: str):
f.write(afile) f.write(afile)
return True return True
@router.get("/downloadinp/", status_code=status.HTTP_200_OK) @router.get("/downloadinp/", status_code=status.HTTP_200_OK, summary="下载 INP 文件", description="从服务器数据目录下载指定的 INP 文件。")
async def fastapi_download_inp(name: str, response: Response): async def fastapi_download_inp(
name: str = Query(..., description="文件名"),
response: Response = None
):
"""
下载 INP 文件
- **name**: 文件名
"""
filePath = inpDir + name filePath = inpDir + name
if os.path.exists(filePath): if os.path.exists(filePath):
return FileResponse( return FileResponse(
@@ -198,8 +372,186 @@ async def fastapi_download_inp(name: str, response: Response):
return True return True
# DingZQ, 2024-12-28, convert v3 to v2 # DingZQ, 2024-12-28, convert v3 to v2
@router.get("/convertv3tov2/", response_model=None) @router.get("/convertv3tov2/", response_model=None, summary="转换 INP V3 为 V2", description="将 EPANET 3.0 格式的 INP 内容转换为 2.x 格式。")
async def fastapi_convert_v3_to_v2(req: Request) -> ChangeSet: async def fastapi_convert_v3_to_v2(
req: Request
) -> ChangeSet:
"""
转换 INP V3 为 V2
- **req**: 请求体,需包含 `{"inp": "..."}` 结构
"""
network = "v3Tov2"
jo_root = await req.json()
inp = jo_root["inp"]
cs = convert_inp_v3_to_v2(inp)
op = cs.operations[0]
open_project(network)
op["vertex"] = json.dumps(get_all_vertices(network))
op["scada"] = json.dumps(get_all_scada_elements(network))
op["dma"] = json.dumps(get_all_district_metering_areas(network))
op["sa"] = json.dumps(get_all_service_areas(network))
op["vd"] = json.dumps(get_all_virtual_districts(network))
op["legend"] = get_extension_data(network, "legend")
db = get_extension_data(network, "scada_db")
print(db)
scada_db = ""
if db:
scada_db = db
print(scada_db)
op["scada_db"] = scada_db
close_project(network)
return cs
@router.post("/readinp/", summary="读取 INP 文件到项目", description="从服务器文件系统中读取指定的 INP 文件并加载到项目中。")
async def read_inp_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
inp: str = Query(..., description="INP 文件名 (不包含路径)")
) -> bool:
"""
读取 INP 文件到项目
- **network**: 管网名称(或数据库名称)
- **inp**: INP 文件名
"""
read_inp(network, inp)
return True
@router.get("/dumpinp/", summary="导出项目到 INP 文件", description="将项目当前状态保存为 INP 文件到服务器文件系统。")
async def dump_inp_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
inp: str = Query(..., description="目标文件名")
) -> bool:
"""
导出项目到 INP 文件
- **network**: 管网名称(或数据库名称)
- **inp**: 目标文件名
"""
dump_inp(network, inp)
return True
@router.get("/isprojectlocked/", summary="检查项目是否被锁定", description="检查指定项目是否处于锁定状态。")
async def is_project_locked_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
检查项目是否被锁定
- **network**: 管网名称(或数据库名称)
"""
return network in lockedPrjs.keys()
@router.get("/isprojectlockedbyme/", summary="检查项目是否被当前用户锁定", description="检查指定项目是否被当前客户端 (IP) 锁定。")
async def is_project_locked_by_me_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
检查项目是否被当前用户锁定
- **network**: 管网名称(或数据库名称)
"""
client_host = req.client.host
return lockedPrjs.get(network) == client_host
# 0 successfully locked
# 1 already locked by you
# 2 locked by others
@router.post("/lockproject/", summary="锁定项目", description="锁定指定项目以防止并发修改。")
async def lock_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
锁定项目
返回值:
- **0**: 锁定成功
- **1**: 已被当前用户锁定
- **2**: 已被其他用户锁定
"""
client_host = req.client.host
if not network in lockedPrjs.keys():
lockedPrjs[network] = client_host
return 0
else:
if lockedPrjs.get(network) == client_host:
return 1
else:
return 2
@router.post("/unlockproject/", summary="解锁项目", description="释放对项目的锁定。")
def unlock_project_endpoint(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
):
"""
解锁项目
只有锁定者才能解锁。
"""
client_host = req.client.host
if lockedPrjs.get(network) == client_host:
print("delete key")
del lockedPrjs[network]
return True
return False
# inp file operations
@router.post("/uploadinp/", status_code=status.HTTP_200_OK, summary="上传 INP 文件", description="上传 INP 文件到服务器数据目录。")
async def fastapi_upload_inp(
afile: bytes = Body(..., description="文件二进制内容"),
name: str = Query(..., description="保存的文件名")
):
"""
上传 INP 文件
- **afile**: 文件内容
- **name**: 文件名
"""
if not os.path.exists(inpDir):
os.makedirs(inpDir, exist_ok=True)
filePath = inpDir + str(name)
with open(filePath, "wb") as f:
f.write(afile)
return True
@router.get("/downloadinp/", status_code=status.HTTP_200_OK, summary="下载 INP 文件", description="从服务器数据目录下载指定的 INP 文件。")
async def fastapi_download_inp(
name: str = Query(..., description="文件名"),
response: Response = None
):
"""
下载 INP 文件
- **name**: 文件名
"""
filePath = inpDir + name
if os.path.exists(filePath):
return FileResponse(
filePath, media_type="application/octet-stream", filename="inp.inp"
)
else:
response.status_code = status.HTTP_400_BAD_REQUEST
return True
# DingZQ, 2024-12-28, convert v3 to v2
@router.get("/convertv3tov2/", response_model=None, summary="转换 INP V3 为 V2", description="将 EPANET 3.0 格式的 INP 内容转换为 2.x 格式。")
async def fastapi_convert_v3_to_v2(
req: Request
) -> ChangeSet:
"""
转换 INP V3 为 V2
- **req**: 请求体,需包含 `{"inp": "..."}` 结构
"""
network = "v3Tov2" network = "v3Tov2"
jo_root = await req.json() jo_root = await req.json()
inp = jo_root["inp"] inp = jo_root["inp"]
+22 -12
View File
@@ -1,7 +1,7 @@
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Path, Query
from psycopg import AsyncConnection from psycopg import AsyncConnection
from app.infra.db.postgresql.scada_info import ScadaRepository from app.infra.db.postgresql.scada import ScadaInfoRepository
from app.infra.db.postgresql.scheme import SchemeRepository from app.infra.db.postgresql.scheme import SchemeRepository
from app.auth.project_dependencies import get_project_pg_connection from app.auth.project_dependencies import get_project_pg_connection
@@ -11,18 +11,21 @@ router = APIRouter()
async def get_database_connection( async def get_database_connection(
conn: AsyncConnection = Depends(get_project_pg_connection), conn: AsyncConnection = Depends(get_project_pg_connection),
): ):
"""获取数据库连接"""
yield conn yield conn
@router.get("/scada-info") @router.get("/scada-info", summary="获取SCADA信息", description="使用连接池查询所有SCADA信息")
async def get_scada_info_with_connection( async def get_scada_info_with_connection(
conn: AsyncConnection = Depends(get_database_connection), conn: AsyncConnection = Depends(get_database_connection),
): ):
""" """
使用连接池查询所有SCADA信息 获取所有SCADA信息
返回项目中所有的SCADA设备信息
""" """
try: try:
scada_data = await ScadaRepository.get_scadas(conn) scada_data = await ScadaInfoRepository.get_scadas(conn)
return {"success": True, "data": scada_data, "count": len(scada_data)} return {"success": True, "data": scada_data, "count": len(scada_data)}
except Exception as e: except Exception as e:
raise HTTPException( raise HTTPException(
@@ -30,12 +33,14 @@ async def get_scada_info_with_connection(
) )
@router.get("/scheme-list") @router.get("/scheme-list", summary="获取方案列表", description="使用连接池查询所有方案信息")
async def get_scheme_list_with_connection( async def get_scheme_list_with_connection(
conn: AsyncConnection = Depends(get_database_connection), conn: AsyncConnection = Depends(get_database_connection),
): ):
""" """
使用连接池查询所有方案信息 获取所有方案信息
返回项目中所有方案的详细信息
""" """
try: try:
scheme_data = await SchemeRepository.get_schemes(conn) scheme_data = await SchemeRepository.get_schemes(conn)
@@ -44,12 +49,14 @@ async def get_scheme_list_with_connection(
raise HTTPException(status_code=500, detail=f"查询方案信息时发生错误: {str(e)}") raise HTTPException(status_code=500, detail=f"查询方案信息时发生错误: {str(e)}")
@router.get("/burst-locate-result") @router.get("/burst-locate-result", summary="获取爆管定位结果", description="使用连接池查询所有爆管定位结果")
async def get_burst_locate_result_with_connection( async def get_burst_locate_result_with_connection(
conn: AsyncConnection = Depends(get_database_connection), conn: AsyncConnection = Depends(get_database_connection),
): ):
""" """
使用连接池查询所有爆管定位结果 获取所有爆管定位结果
返回项目中所有的爆管定位分析结果
""" """
try: try:
burst_data = await SchemeRepository.get_burst_locate_results(conn) burst_data = await SchemeRepository.get_burst_locate_results(conn)
@@ -60,13 +67,16 @@ async def get_burst_locate_result_with_connection(
) )
@router.get("/burst-locate-result/{burst_incident}") @router.get("/burst-locate-result/{burst_incident}", summary="按事件查询爆管定位结果", description="根据爆管事件ID查询对应的爆管定位结果")
async def get_burst_locate_result_by_incident( async def get_burst_locate_result_by_incident(
burst_incident: str, burst_incident: str = Path(..., description="爆管事件ID"),
conn: AsyncConnection = Depends(get_database_connection), conn: AsyncConnection = Depends(get_database_connection),
): ):
""" """
根据 burst_incident 查询爆管定位结果 根据爆管事件ID查询爆管定位结果
参数:
burst_incident: 爆管事件的唯一标识符
""" """
try: try:
return await SchemeRepository.get_burst_locate_result_by_incident( return await SchemeRepository.get_burst_locate_result_by_incident(
+94 -11
View File
@@ -1,5 +1,5 @@
from typing import Any, List, Dict from typing import Any, List, Dict
from fastapi import APIRouter from fastapi import APIRouter, Query, Path
from app.services.tjnetwork import ( from app.services.tjnetwork import (
get_pipe_risk_probability_now, get_pipe_risk_probability_now,
get_pipe_risk_probability, get_pipe_risk_probability,
@@ -10,35 +10,118 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getpiperiskprobabilitynow/") @router.get(
"/getpiperiskprobabilitynow/",
summary="获取管道当前风险概率",
description="获取指定管道当前时刻的风险概率值"
)
async def fastapi_get_pipe_risk_probability_now( async def fastapi_get_pipe_risk_probability_now(
network: str, pipe_id: str network: str = Query(..., description="管网名称(或数据库名称)"),
pipe_id: str = Query(..., description="管道ID")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
获取管道当前风险概率。
查询指定管道在当前时刻的风险概率值。
Args:
network: 管网名称(或数据库名称)
pipe_id: 管道ID
Returns:
包含风险概率信息的字典
"""
return get_pipe_risk_probability_now(network, pipe_id) return get_pipe_risk_probability_now(network, pipe_id)
@router.get("/getpiperiskprobability/") @router.get(
"/getpiperiskprobability/",
summary="获取管道风险概率历史",
description="获取指定管道的风险概率历史数据"
)
async def fastapi_get_pipe_risk_probability( async def fastapi_get_pipe_risk_probability(
network: str, pipe_id: str network: str = Query(..., description="管网名称(或数据库名称)"),
pipe_id: str = Query(..., description="管道ID")
) -> dict[str, Any]: ) -> dict[str, Any]:
"""
获取管道风险概率历史。
查询指定管道的历史风险概率数据。
Args:
network: 管网名称(或数据库名称)
pipe_id: 管道ID
Returns:
包含风险概率历史的字典
"""
return get_pipe_risk_probability(network, pipe_id) return get_pipe_risk_probability(network, pipe_id)
@router.get("/getpipesriskprobability/") @router.get(
"/getpipesriskprobability/",
summary="批量获取多条管道风险概率",
description="批量获取多条管道的风险概率值"
)
async def fastapi_get_pipes_risk_probability( async def fastapi_get_pipes_risk_probability(
network: str, pipe_ids: str network: str = Query(..., description="管网名称(或数据库名称)"),
pipe_ids: str = Query(..., description="逗号分隔的管道ID列表")
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""
批量获取多条管道风险概率。
查询多条指定管道的风险概率值。
Args:
network: 管网名称(或数据库名称)
pipe_ids: 逗号分隔的管道ID列表(例如:pipe1,pipe2,pipe3
Returns:
包含多条管道风险概率的列表
"""
pipeids = pipe_ids.split(",") pipeids = pipe_ids.split(",")
return get_pipes_risk_probability(network, pipeids) return get_pipes_risk_probability(network, pipeids)
@router.get("/getnetworkpiperiskprobabilitynow/") @router.get(
"/getnetworkpiperiskprobabilitynow/",
summary="获取整个网络的管道风险概率",
description="获取指定网络中所有管道的当前风险概率值"
)
async def fastapi_get_network_pipe_risk_probability_now( async def fastapi_get_network_pipe_risk_probability_now(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
"""
获取整个网络的管道风险概率。
查询指定网络中所有管道在当前时刻的风险概率值。
Args:
network: 管网名称(或数据库名称)
Returns:
包含网络内所有管道风险概率的列表
"""
return get_network_pipe_risk_probability_now(network) return get_network_pipe_risk_probability_now(network)
@router.get("/getpiperiskprobabilitygeometries/") @router.get(
async def fastapi_get_pipe_risk_probability_geometries(network: str) -> dict[str, Any]: "/getpiperiskprobabilitygeometries/",
summary="获取管道风险几何信息",
description="获取指定网络中管道的风险相关几何数据"
)
async def fastapi_get_pipe_risk_probability_geometries(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, Any]:
"""
获取管道风险几何信息。
查询指定网络中管道的地理和风险相关的几何数据。
Args:
network: 管网名称(或数据库名称)
Returns:
包含几何信息和风险数据的字典
"""
return get_pipe_risk_probability_geometries(network) return get_pipe_risk_probability_geometries(network)
+415 -57
View File
@@ -1,5 +1,5 @@
from typing import Any from typing import Any
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query
from app.services.tjnetwork import ( from app.services.tjnetwork import (
ChangeSet, ChangeSet,
get_scada_info, get_scada_info,
@@ -31,139 +31,497 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getscadaproperties/") @router.get("/getscadaproperties/", summary="获取SCADA属性", tags=["SCADA基础"])
async def fast_get_scada_properties(network: str, scada: str) -> dict[str, Any]: async def fast_get_scada_properties(
network: str = Query(..., description="管网名称(或数据库名称)"),
scada: str = Query(..., description="SCADA设备ID")
) -> dict[str, Any]:
"""
获取单个SCADA设备的属性信息
根据管网名称和SCADA设备ID获取该设备的完整属性。
Args:
network: 管网名称(或数据库名称)
scada: SCADA设备ID
Returns:
SCADA设备的属性字典
"""
return get_scada_info(network, scada) return get_scada_info(network, scada)
@router.get("/getallscadaproperties/") @router.get("/getallscadaproperties/", summary="获取所有SCADA属性", tags=["SCADA基础"])
async def fast_get_all_scada_properties(network: str) -> list[dict[str, Any]]: async def fast_get_all_scada_properties(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取指定管网所有SCADA设备的属性信息
查询该管网下所有已配置的SCADA设备的属性列表。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA设备属性列表
"""
return get_all_scada_info(network) return get_all_scada_info(network)
############################################################ ############################################################
# scada_device 29 # scada_device 设备管理
############################################################ ############################################################
@router.get("/getscadadeviceschema/") @router.get("/getscadadeviceschema/", summary="获取SCADA设备架构", tags=["SCADA设备"])
async def fastapi_get_scada_device_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_scada_device_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取SCADA设备的数据架构
返回SCADA设备表的字段定义和类型信息。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA设备的字段架构信息
"""
return get_scada_device_schema(network) return get_scada_device_schema(network)
@router.get("/getscadadevice/") @router.get("/getscadadevice/", summary="获取SCADA设备", tags=["SCADA设备"])
async def fastapi_get_scada_device(network: str, id: str) -> dict[str, Any]: async def fastapi_get_scada_device(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="SCADA设备ID")
) -> dict[str, Any]:
"""
获取单个SCADA设备的信息
根据设备ID查询该设备的详细信息。
Args:
network: 管网名称(或数据库名称)
id: SCADA设备ID
Returns:
SCADA设备信息
"""
return get_scada_device(network, id) return get_scada_device(network, id)
@router.post("/setscadadevice/", response_model=None) @router.post("/setscadadevice/", response_model=None, summary="更新SCADA设备", tags=["SCADA设备"])
async def fastapi_set_scada_device(network: str, req: Request) -> ChangeSet: async def fastapi_set_scada_device(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
更新SCADA设备信息
修改指定SCADA设备的属性。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含要更新的设备属性
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return set_scada_device(network, ChangeSet(props)) return set_scada_device(network, ChangeSet(props))
@router.post("/addscadadevice/", response_model=None) @router.post("/addscadadevice/", response_model=None, summary="添加SCADA设备", tags=["SCADA设备"])
async def fastapi_add_scada_device(network: str, req: Request) -> ChangeSet: async def fastapi_add_scada_device(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
添加新的SCADA设备
在指定管网中添加一个新的SCADA设备。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含新设备的属性
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return add_scada_device(network, ChangeSet(props)) return add_scada_device(network, ChangeSet(props))
@router.post("/deletescadadevice/", response_model=None) @router.post("/deletescadadevice/", response_model=None, summary="删除SCADA设备", tags=["SCADA设备"])
async def fastapi_delete_scada_device(network: str, req: Request) -> ChangeSet: async def fastapi_delete_scada_device(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
删除SCADA设备
从指定管网中删除一个SCADA设备。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含要删除的设备ID
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return delete_scada_device(network, ChangeSet(props)) return delete_scada_device(network, ChangeSet(props))
@router.post("/cleanscadadevice/", response_model=None) @router.post("/cleanscadadevice/", response_model=None, summary="清空SCADA设备表", tags=["SCADA设备"])
async def fastapi_clean_scada_device(network: str) -> ChangeSet: async def fastapi_clean_scada_device(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> ChangeSet:
"""
清空SCADA设备表
删除指定管网中所有的SCADA设备。
Args:
network: 管网名称(或数据库名称)
Returns:
变更集合信息
"""
return clean_scada_device(network) return clean_scada_device(network)
@router.get("/getallscadadeviceids/") @router.get("/getallscadadeviceids/", summary="获取所有SCADA设备ID", tags=["SCADA设备"])
async def fastapi_get_all_scada_device_ids(network: str) -> list[str]: async def fastapi_get_all_scada_device_ids(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[str]:
"""
获取指定管网所有SCADA设备的ID列表
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA设备ID列表
"""
return get_all_scada_device_ids(network) return get_all_scada_device_ids(network)
@router.get("/getallscadadevices/") @router.get("/getallscadadevices/", summary="获取所有SCADA设备", tags=["SCADA设备"])
async def fastapi_get_all_scada_devices(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_scada_devices(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取指定管网所有SCADA设备的完整信息
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA设备信息列表
"""
return get_all_scada_devices(network) return get_all_scada_devices(network)
############################################################ ############################################################
# scada_device_data 30 # scada_device_data 设备数据管理
############################################################ ############################################################
@router.get("/getscadadevicedataschema/") @router.get("/getscadadevicedataschema/", summary="获取SCADA设备数据架构", tags=["SCADA设备数据"])
async def fastapi_get_scada_device_data_schema( async def fastapi_get_scada_device_data_schema(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
) -> dict[str, dict[str, Any]]: ) -> dict[str, dict[str, Any]]:
"""
获取SCADA设备数据的表结构
返回SCADA设备数据表的字段定义和类型信息。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA设备数据的字段架构信息
"""
return get_scada_device_data_schema(network) return get_scada_device_data_schema(network)
@router.get("/getscadadevicedata/") @router.get("/getscadadevicedata/", summary="获取SCADA设备数据", tags=["SCADA设备数据"])
async def fastapi_get_scada_device_data(network: str, device_id: str) -> dict[str, Any]: async def fastapi_get_scada_device_data(
network: str = Query(..., description="管网名称(或数据库名称)"),
device_id: str = Query(..., description="SCADA设备ID")
) -> dict[str, Any]:
"""
获取单个SCADA设备的数据
查询指定设备的监测数据或配置数据。
Args:
network: 管网名称(或数据库名称)
device_id: SCADA设备ID
Returns:
SCADA设备数据
"""
return get_scada_device_data(network, device_id) return get_scada_device_data(network, device_id)
@router.post("/setscadadevicedata/", response_model=None) @router.post("/setscadadevicedata/", response_model=None, summary="更新SCADA设备数据", tags=["SCADA设备数据"])
async def fastapi_set_scada_device_data(network: str, req: Request) -> ChangeSet: async def fastapi_set_scada_device_data(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
更新SCADA设备数据
修改指定SCADA设备的数据。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含要更新的数据
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return set_scada_device_data(network, ChangeSet(props)) return set_scada_device_data(network, ChangeSet(props))
@router.post("/addscadadevicedata/", response_model=None) @router.post("/addscadadevicedata/", response_model=None, summary="添加SCADA设备数据", tags=["SCADA设备数据"])
async def fastapi_add_scada_device_data(network: str, req: Request) -> ChangeSet: async def fastapi_add_scada_device_data(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
添加新的SCADA设备数据
为指定SCADA设备添加新的数据记录。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含新数据的内容
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return add_scada_device_data(network, ChangeSet(props)) return add_scada_device_data(network, ChangeSet(props))
@router.post("/deletescadadevicedata/", response_model=None) @router.post("/deletescadadevicedata/", response_model=None, summary="删除SCADA设备数据", tags=["SCADA设备数据"])
async def fastapi_delete_scada_device_data(network: str, req: Request) -> ChangeSet: async def fastapi_delete_scada_device_data(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
删除SCADA设备数据
删除指定SCADA设备的数据记录。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含要删除的数据ID
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return delete_scada_device_data(network, ChangeSet(props)) return delete_scada_device_data(network, ChangeSet(props))
@router.post("/cleanscadadevicedata/", response_model=None) @router.post("/cleanscadadevicedata/", response_model=None, summary="清空SCADA设备数据表", tags=["SCADA设备数据"])
async def fastapi_clean_scada_device_data(network: str) -> ChangeSet: async def fastapi_clean_scada_device_data(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> ChangeSet:
"""
清空SCADA设备数据表
删除指定管网中所有SCADA设备的数据。
Args:
network: 管网名称(或数据库名称)
Returns:
变更集合信息
"""
return clean_scada_device_data(network) return clean_scada_device_data(network)
############################################################ ############################################################
# scada_element 31 # scada_element SCADA元素映射
############################################################ ############################################################
@router.get("/getscadaelementschema/") @router.get("/getscadaelementschema/", summary="获取SCADA元素架构", tags=["SCADA元素映射"])
async def fastapi_get_scada_element_schema( async def fastapi_get_scada_element_schema(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
) -> dict[str, dict[str, Any]]: ) -> dict[str, dict[str, Any]]:
"""
获取SCADA元素映射的表结构
返回SCADA元素映射表的字段定义和类型信息。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA元素映射的字段架构信息
"""
return get_scada_element_schema(network) return get_scada_element_schema(network)
@router.get("/getscadaelements/") @router.get("/getscadaelements/", summary="获取所有SCADA元素映射", tags=["SCADA元素映射"])
async def fastapi_get_scada_elements(network: str) -> list[dict[str, Any]]: async def fastapi_get_scada_elements(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取指定管网所有SCADA元素映射
查询所有SCADA设备与管网元素(节点/管道)的映射关系。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA元素映射列表
"""
return get_all_scada_elements(network) return get_all_scada_elements(network)
@router.get("/getscadaelement/") @router.get("/getscadaelement/", summary="获取单个SCADA元素映射", tags=["SCADA元素映射"])
async def fastapi_get_scada_element(network: str, id: str) -> dict[str, Any]: async def fastapi_get_scada_element(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="SCADA元素映射ID")
) -> dict[str, Any]:
"""
获取单个SCADA元素映射的信息
根据ID查询特定的SCADA设备与管网元素的映射关系。
Args:
network: 管网名称(或数据库名称)
id: SCADA元素映射ID
Returns:
SCADA元素映射信息
"""
return get_scada_element(network, id) return get_scada_element(network, id)
@router.post("/setscadaelement/", response_model=None) @router.post("/setscadaelement/", response_model=None, summary="更新SCADA元素映射", tags=["SCADA元素映射"])
async def fastapi_set_scada_element(network: str, req: Request) -> ChangeSet: async def fastapi_set_scada_element(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
更新SCADA元素映射
修改SCADA设备与管网元素的映射关系。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含要更新的映射信息
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return set_scada_element(network, ChangeSet(props)) return set_scada_element(network, ChangeSet(props))
@router.post("/addscadaelement/", response_model=None) @router.post("/addscadaelement/", response_model=None, summary="添加SCADA元素映射", tags=["SCADA元素映射"])
async def fastapi_add_scada_element(network: str, req: Request) -> ChangeSet: async def fastapi_add_scada_element(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
添加新的SCADA元素映射
创建SCADA设备与管网元素的新映射关系。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含新映射的信息
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return add_scada_element(network, ChangeSet(props)) return add_scada_element(network, ChangeSet(props))
@router.post("/deletescadaelement/", response_model=None) @router.post("/deletescadaelement/", response_model=None, summary="删除SCADA元素映射", tags=["SCADA元素映射"])
async def fastapi_delete_scada_element(network: str, req: Request) -> ChangeSet: async def fastapi_delete_scada_element(
network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet:
"""
删除SCADA元素映射
移除SCADA设备与管网元素的映射关系。
Args:
network: 管网名称(或数据库名称)
req: 请求体,包含要删除的映射ID
Returns:
变更集合信息
"""
props = await req.json() props = await req.json()
return delete_scada_element(network, ChangeSet(props)) return delete_scada_element(network, ChangeSet(props))
@router.post("/cleanscadaelement/", response_model=None) @router.post("/cleanscadaelement/", response_model=None, summary="清空SCADA元素映射表", tags=["SCADA元素映射"])
async def fastapi_clean_scada_element(network: str) -> ChangeSet: async def fastapi_clean_scada_element(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> ChangeSet:
"""
清空SCADA元素映射表
删除指定管网中所有的SCADA元素映射。
Args:
network: 管网名称(或数据库名称)
Returns:
变更集合信息
"""
return clean_scada_element(network) return clean_scada_element(network)
############################################################ ############################################################
# scada_info 38 # scada_info SCADA信息
############################################################ ############################################################
@router.get("/getscadainfoschema/") @router.get("/getscadainfoschema/", summary="获取SCADA信息架构", tags=["SCADA信息"])
async def fastapi_get_scada_info_schema(network: str) -> dict[str, dict[str, Any]]: async def fastapi_get_scada_info_schema(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> dict[str, dict[str, Any]]:
"""
获取SCADA信息表的结构
返回SCADA信息表的字段定义和类型信息。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA信息的字段架构信息
"""
return get_scada_info_schema(network) return get_scada_info_schema(network)
@router.get("/getscadainfo/") @router.get("/getscadainfo/", summary="获取SCADA信息", tags=["SCADA信息"])
async def fastapi_get_scada_info(network: str, id: str) -> dict[str, Any]: async def fastapi_get_scada_info(
network: str = Query(..., description="管网名称(或数据库名称)"),
id: str = Query(..., description="SCADA信息ID")
) -> dict[str, Any]:
"""
获取单个SCADA信息
根据ID查询SCADA的详细配置信息。
Args:
network: 管网名称(或数据库名称)
id: SCADA信息ID
Returns:
SCADA信息详情
"""
return get_scada_info(network, id) return get_scada_info(network, id)
@router.get("/getallscadainfo/") @router.get("/getallscadainfo/", summary="获取所有SCADA信息", tags=["SCADA信息"])
async def fastapi_get_all_scada_info(network: str) -> list[dict[str, Any]]: async def fastapi_get_all_scada_info(
network: str = Query(..., description="管网名称(或数据库名称)")
) -> list[dict[str, Any]]:
"""
获取指定管网所有SCADA的信息
查询该管网下所有已配置的SCADA的完整信息。
Args:
network: 管网名称(或数据库名称)
Returns:
SCADA信息列表
"""
return get_all_scada_info(network) return get_all_scada_info(network)
+23 -7
View File
@@ -1,17 +1,33 @@
from fastapi import APIRouter from fastapi import APIRouter, Query
from typing import Any, List, Dict from typing import Any, List, Dict
from app.services.tjnetwork import get_scheme_schema, get_scheme, get_all_schemes from app.services.tjnetwork import get_scheme_schema, get_scheme, get_all_schemes
router = APIRouter() router = APIRouter()
@router.get("/getschemeschema/") @router.get("/getschemeschema/", summary="获取方案模式", description="获取指定网络的方案模式定义")
async def fastapi_get_scheme_schema(network: str) -> dict[str, dict[Any, Any]]: async def fastapi_get_scheme_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[Any, Any]]:
"""
获取方案模式定义
返回指定网络的方案模式结构定义
"""
return get_scheme_schema(network) return get_scheme_schema(network)
@router.get("/getscheme/") @router.get("/getscheme/", summary="获取单个方案", description="根据名称获取指定的方案信息")
async def fastapi_get_scheme(network: str, schema_name: str) -> dict[Any, Any]: async def fastapi_get_scheme(network: str = Query(..., description="管网名称(或数据库名称)"), schema_name: str = Query(..., description="方案名称")) -> dict[Any, Any]:
"""
获取单个方案详情
返回指定网络中指定名称的方案详细信息
"""
return get_scheme(network, schema_name) return get_scheme(network, schema_name)
@router.get("/getallschemes/") @router.get("/schemes", summary="获取所有方案", description="获取指定网络的所有方案信息")
async def fastapi_get_all_schemes(network: str) -> list[dict[Any, Any]]: @router.get("/getallschemes/", summary="获取所有方案(旧路径)", description="获取指定网络的所有方案信息", deprecated=True)
async def fastapi_get_all_schemes(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[dict[Any, Any]]:
"""
获取所有方案列表
返回指定网络中所有可用的方案
"""
return get_all_schemes(network) return get_all_schemes(network)
+456 -287
View File
@@ -4,13 +4,11 @@ import json
import os import os
import shutil import shutil
import threading import threading
import pandas as pd from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Query, Path, Body
from fastapi import APIRouter, HTTPException, File, UploadFile, Query
from fastapi.responses import PlainTextResponse from fastapi.responses import PlainTextResponse
import app.infra.db.influxdb.api as influxdb_api from app.auth.keycloak_dependencies import get_current_keycloak_username
import app.services.simulation as simulation import app.services.simulation as simulation
import app.services.globals as globals import app.services.globals as globals
from app.infra.cache.redis_client import redis_client
from app.services.tjnetwork import ( from app.services.tjnetwork import (
run_project, run_project,
run_project_return_dict, run_project_return_dict,
@@ -30,8 +28,7 @@ from app.algorithms.sensor import (
pressure_sensor_placement_sensitivity, pressure_sensor_placement_sensitivity,
pressure_sensor_placement_kmeans, pressure_sensor_placement_kmeans,
) )
import app.algorithms.cleaning.flow as flow_data_clean
import app.algorithms.cleaning.pressure as pressure_data_clean
from app.services.network_import import network_update from app.services.network_import import network_update
from app.services.simulation_ops import ( from app.services.simulation_ops import (
project_management, project_management,
@@ -39,176 +36,194 @@ from app.services.simulation_ops import (
daily_scheduling_simulation, daily_scheduling_simulation,
) )
from app.services.valve_isolation import analyze_valve_isolation from app.services.valve_isolation import analyze_valve_isolation
from pydantic import BaseModel from app.services.time_api import (
parse_aware_time,
parse_clock_duration_seconds,
parse_utc_time,
)
from pydantic import BaseModel, Field, field_validator
router = APIRouter() router = APIRouter()
class RunSimulationManuallyByDate(BaseModel): class RunSimulationManuallyByDate(BaseModel):
name: str name: str = Field(..., description="管网名称(或数据库名称)")
simulation_date: str start_time: str = Field(..., description="开始时间 (ISO 8601 / RFC3339,必须显式带时区)")
start_time: str duration: int = Field(..., gt=0, description="持续时间 (分钟)")
duration: int
@field_validator("start_time")
@classmethod
def validate_start_time_timezone(cls, value: str) -> str:
parse_aware_time(value, field_name="start_time")
return value
class BurstAnalysis(BaseModel): class BurstAnalysis(BaseModel):
name: str name: str = Field(..., description="管网名称(或数据库名称)")
modify_pattern_start_time: str modify_pattern_start_time: str = Field(..., description="模式修改开始时间 (ISO 8601)")
burst_ID: List[str] | str | None = None burst_ID: List[str] | str | None = Field(None, description="爆管节点/管段ID列表")
burst_size: List[float] | float | int | None = None burst_size: List[float] | float | int | None = Field(None, description="爆管流量大小")
modify_total_duration: int = 900 modify_total_duration: int = Field(900, description="模拟总时长 (秒)")
modify_fixed_pump_pattern: Optional[dict[str, list]] = None modify_fixed_pump_pattern: Optional[dict[str, list]] = Field(None, description="定速泵模式修改")
modify_variable_pump_pattern: Optional[dict[str, list]] = None modify_variable_pump_pattern: Optional[dict[str, list]] = Field(None, description="变速泵模式修改")
modify_valve_opening: Optional[dict[str, float]] = None modify_valve_opening: Optional[dict[str, float]] = Field(None, description="阀门开度修改")
scheme_name: Optional[str] = None scheme_name: Optional[str] = Field(None, description="方案名称")
class SchedulingAnalysis(BaseModel): class SchedulingAnalysis(BaseModel):
network: str network: str = Field(..., description="管网名称(或数据库名称)")
start_time: str start_time: str = Field(..., description="开始时间")
pump_control: dict pump_control: dict = Field(..., description="泵控制策略")
tank_id: str tank_id: str = Field(..., description="水箱ID")
water_plant_output_id: str water_plant_output_id: str = Field(..., description="水厂出水ID")
time_delta: Optional[int] = 300 time_delta: Optional[int] = Field(300, description="时间步长 (秒)")
class PressureRegulation(BaseModel): class PressureRegulation(BaseModel):
network: str network: str = Field(..., description="管网名称(或数据库名称)")
start_time: str start_time: str = Field(..., description="开始时间")
pump_control: dict pump_control: dict = Field(..., description="泵控制策略")
tank_init_level: Optional[dict] = None tank_init_level: Optional[dict] = Field(None, description="水箱初始水位")
duration: Optional[int] = 900 duration: Optional[int] = Field(900, description="持续时间 (秒)")
scheme_name: Optional[str] = None scheme_name: Optional[str] = Field(None, description="方案名称")
class ProjectManagement(BaseModel): class ProjectManagement(BaseModel):
network: str network: str = Field(..., description="管网名称(或数据库名称)")
start_time: str start_time: str = Field(..., description="开始时间")
pump_control: dict pump_control: dict = Field(..., description="泵控制策略")
tank_init_level: Optional[dict] = None tank_init_level: Optional[dict] = Field(None, description="水箱初始水位")
region_demand: Optional[dict] = None region_demand: Optional[dict] = Field(None, description="区域需水量控制")
class DailySchedulingAnalysis(BaseModel): class DailySchedulingAnalysis(BaseModel):
network: str network: str = Field(..., description="管网名称(或数据库名称)")
start_time: str start_time: str = Field(..., description="开始时间")
pump_control: dict pump_control: dict = Field(..., description="泵控制策略")
reservoir_id: str reservoir_id: str = Field(..., description="水库ID")
tank_id: str tank_id: str = Field(..., description="水箱ID")
water_plant_output_id: str water_plant_output_id: str = Field(..., description="水厂出水ID")
time_delta: Optional[int] = 300 time_delta: Optional[int] = Field(300, description="时间步长 (秒)")
class PumpFailureState(BaseModel): class PumpFailureState(BaseModel):
time: str time: str = Field(..., description="故障发生时间")
pump_status: dict pump_status: dict = Field(..., description="泵状态字典")
class PressureSensorPlacement(BaseModel): class PressureSensorPlacement(BaseModel):
name: str name: str = Field(..., description="管网名称(或数据库名称)")
scheme_name: str scheme_name: str = Field(..., description="方案名称")
sensor_number: int sensor_number: int = Field(..., description="传感器数量")
min_diameter: int = 0 min_diameter: int = Field(0, description="最小管径限制")
username: str username: str = Field(..., description="用户名")
def run_simulation_manually_by_date( def run_simulation_manually_by_date(
network_name: str, base_date: datetime, start_time: str, duration: int network_name: str, start_time: datetime, duration: int
) -> None: ) -> None:
time_parts = list(map(int, start_time.split(":"))) end_datetime = start_time + timedelta(minutes=duration)
if len(time_parts) == 2: time_properties = simulation.get_time(network_name)
start_hour, start_minute = time_parts hydraulic_step_seconds = parse_clock_duration_seconds(
start_second = 0 time_properties["HYDRAULIC TIMESTEP"],
elif len(time_parts) == 3: field_name="HYDRAULIC TIMESTEP",
start_hour, start_minute, start_second = time_parts
else:
raise ValueError("Invalid start_time format. Use HH:MM or HH:MM:SS")
start_datetime = base_date.replace(
hour=start_hour, minute=start_minute, second=start_second
) )
end_datetime = start_datetime + timedelta(minutes=duration) if hydraulic_step_seconds <= 0:
current_time = start_datetime raise ValueError("HYDRAULIC TIMESTEP must be greater than 0.")
hydraulic_step = timedelta(seconds=hydraulic_step_seconds)
current_time = start_time
while current_time < end_datetime: while current_time < end_datetime:
iso_time = current_time.strftime("%Y-%m-%dT%H:%M:%S") + "+08:00"
simulation.run_simulation( simulation.run_simulation(
name=network_name, name=network_name,
simulation_type="realtime", simulation_type="realtime",
modify_pattern_start_time=iso_time, modify_pattern_start_time=current_time.isoformat(timespec="seconds"),
) )
current_time += timedelta(minutes=15) current_time += hydraulic_step
# 必须用这个PlainTextResponse,不然每个key都有引号 # 必须用这个PlainTextResponse,不然每个key都有引号
@router.get("/runproject/", response_class=PlainTextResponse) @router.get("/runproject/", response_class=PlainTextResponse, summary="运行项目模拟", description="基于指定的管网项目运行标准水力模拟,返回纯文本格式的模拟报告。")
async def run_project_endpoint(network: str) -> str: async def run_project_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")) -> str:
lock_key = "exclusive_api_lock" """
timeout = 120 # 锁自动过期时间(秒) 运行项目模拟
# 尝试获取锁(NX=True: 不存在时设置,EX=timeout: 过期时间) - **network**: 管网名称(或数据库名称)
acquired = redis_client.set(lock_key, "locked", nx=True, ex=timeout)
运行指定管网项目的标准水力模拟并返回文本报告。
if not acquired: """
raise HTTPException(status_code=409, detail="is in simulation") return run_project(network)
else:
try:
return run_project(network)
finally:
# 手动释放锁(可选,依赖过期时间自动释放更安全)
redis_client.delete(lock_key)
# DingZQ, 2025-02-04, 返回dict[str, Any] # DingZQ, 2025-02-04, 返回dict[str, Any]
# output 和 report # output 和 report
# output 是 json # output 是 json
# report 是 text # report 是 text
@router.get("/runprojectreturndict/") @router.get("/runprojectreturndict/", summary="运行项目模拟(返回字典)", description="基于指定的管网项目运行标准水力模拟,返回JSON格式的字典,包含输出数据和报告文本。")
async def run_project_return_dict_endpoint(network: str) -> dict[str, Any]: async def run_project_return_dict_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, Any]:
lock_key = "exclusive_api_lock" """
timeout = 120 # 锁自动过期时间(秒) 运行项目模拟(返回字典)
# 尝试获取锁(NX=True: 不存在时设置,EX=timeout: 过期时间) - **network**: 管网名称(或数据库名称)
acquired = redis_client.set(lock_key, "locked", nx=True, ex=timeout)
返回字典包含:
if not acquired: - output: JSON格式的模拟输出数据
raise HTTPException(status_code=409, detail="is in simulation") - report: 文本格式的模拟报告
else:
try: 运行指定管网项目的标准水力模拟并返回字典结果。
return run_project_return_dict(network) """
finally: return run_project_return_dict(network)
# 手动释放锁(可选,依赖过期时间自动释放更安全)
redis_client.delete(lock_key)
# put in inp folder, name without extension # put in inp folder, name without extension
@router.get("/runinp/") @router.get("/runinp/", summary="运行INP文件", description="运行指定INP文件格式的管网模型进行水力模拟。INP文件应该放在inp文件夹中,参数为文件名不含扩展名。")
async def run_inp_endpoint(network: str) -> str: async def run_inp_endpoint(network: str = Query(..., description="inp文件名(不含扩展名)")) -> str:
"""
运行INP文件
- **network**: inp文件名(不含扩展名)
从inp文件夹中读取指定的INP文件并运行模拟。
"""
return run_inp(network) return run_inp(network)
# path is absolute path # path is absolute path
@router.get("/dumpoutput/") @router.get("/dumpoutput/", summary="导出模拟输出", description="导出指定路径的模拟输出文件内容。参数应为绝对路径。")
async def dump_output_endpoint(output: str) -> str: async def dump_output_endpoint(output: str = Query(..., description="模拟输出文件的绝对路径")) -> str:
"""
导出模拟输出
- **output**: 模拟输出文件的绝对路径
读取并返回指定路径的模拟输出内容。
"""
return dump_output(output) return dump_output(output)
# Analysis Endpoints # Analysis Endpoints
@router.get("/burstanalysis/") @router.get("/burst-analysis", summary="爆管分析(高级)", description="高级版本的爆管分析,支持在指定时间点修改泵控制模式和阀门开度,以分析这些改变对爆管影响的作用。支持固定泵和变速泵的独立控制。")
async def burst_analysis_endpoint( @router.get("/burst_analysis/", summary="爆管分析(高级,旧路径)", description="高级版本的爆管分析,支持在指定时间点修改泵控制模式和阀门开度,以分析这些改变对爆管影响的作用。支持固定泵和变速泵的独立控制。", deprecated=True)
network: str, pipe_id: str, start_time: str, end_time: str, burst_flow: float
):
return burst_analysis(network, pipe_id, start_time, end_time, burst_flow)
@router.get("/burst_analysis/")
async def fastapi_burst_analysis( async def fastapi_burst_analysis(
network: str = Query(...), network: str = Query(..., description="管网名称(或数据库名称)"),
modify_pattern_start_time: str = Query(...), modify_pattern_start_time: str = Query(..., description="模式修改开始时间(ISO 8601格式)"),
burst_ID: list[str] = Query(...), burst_ID: list[str] = Query(..., description="爆管节点/管段ID列表"),
burst_size: list[float] = Query(...), burst_size: list[float] = Query(..., description="对应各爆管点的爆管流量大小列表(L/s)"),
modify_total_duration: int = Query(...), modify_total_duration: int = Query(..., description="模拟总时长(秒)"),
scheme_name: str = Query(...), scheme_name: str = Query(..., description="分析方案名称"),
username: str = Depends(get_current_keycloak_username),
) -> str: ) -> str:
"""
爆管分析(高级版本)
- **network**: 管网名称(或数据库名称)
- **modify_pattern_start_time**: 模式修改开始时间
- **burst_ID**: 爆管节点/管段ID列表
- **burst_size**: 爆管流量大小列表(与burst_ID对应)
- **modify_total_duration**: 模拟总时长(秒)
- **scheme_name**: 分析方案名称
支持在指定时间修改泵控制模式和阀门开度。
"""
burst_analysis( burst_analysis(
name=network, name=network,
modify_pattern_start_time=modify_pattern_start_time, modify_pattern_start_time=modify_pattern_start_time,
@@ -216,78 +231,108 @@ async def fastapi_burst_analysis(
burst_size=burst_size, burst_size=burst_size,
modify_total_duration=modify_total_duration, modify_total_duration=modify_total_duration,
scheme_name=scheme_name, scheme_name=scheme_name,
username=username,
) )
return "success" return "success"
@router.get("/valvecloseanalysis/") @router.get("/valve_close_analysis/", response_class=PlainTextResponse, summary="阀门关闭分析(高级)", description="高级版本的阀门关闭分析,支持同时关闭多个阀门,并在指定持续时间内进行模拟。返回纯文本格式的分析结果。")
async def valve_close_analysis_endpoint(
network: str, valve_id: str, start_time: str, end_time: str
):
return valve_close_analysis(network, valve_id, start_time, end_time)
@router.get("/valve_close_analysis/", response_class=PlainTextResponse)
async def fastapi_valve_close_analysis( async def fastapi_valve_close_analysis(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
start_time: str, start_time: str = Query(..., description="阀门关闭开始时间(ISO 8601格式)"),
valves: List[str] = Query(...), valves: List[str] = Query(..., description="要关闭的阀门ID列表"),
duration: int | None = None, duration: int | None = Query(None, description="模拟持续时间(秒),默认900秒"),
scheme_name: str = Query(..., description="阀门关闭方案名称"),
) -> str: ) -> str:
"""
阀门关闭分析(高级版本)
- **network**: 管网名称(或数据库名称)
- **start_time**: 阀门关闭开始时间
- **valves**: 要关闭的阀门ID列表
- **duration**: 模拟持续时间(秒,可选,默认900)
- **scheme_name**: 阀门关闭方案名称
支持同时关闭多个阀门进行分析。
"""
result = valve_close_analysis( result = valve_close_analysis(
name=network, name=network,
modify_pattern_start_time=start_time, modify_pattern_start_time=start_time,
modify_total_duration=duration or 900, modify_total_duration=duration or 900,
modify_valve_opening={valve_id: 0.0 for valve_id in valves}, modify_valve_opening={valve_id: 0.0 for valve_id in valves},
scheme_name=scheme_name,
) )
return result or "success" return result or "success"
@router.get("/valve_isolation_analysis/") @router.get("/valve-isolation-analysis", summary="阀门隔离分析", description="分析当发生突发事件时,通过关闭指定阀门进行隔离,确定哪些阀门必须关闭、哪些可选关闭,以及隔离的可行性。")
@router.get("/valve_isolation_analysis/", summary="阀门隔离分析(旧路径)", description="分析当发生突发事件时,通过关闭指定阀门进行隔离,确定哪些阀门必须关闭、哪些可选关闭,以及隔离的可行性。", deprecated=True)
async def valve_isolation_endpoint( async def valve_isolation_endpoint(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
accident_element: List[str] = Query(...), accident_element: List[str] = Query(..., description="发生事故的管段/节点ID列表"),
disabled_valves: List[str] = Query(None), disabled_valves: List[str] = Query(None, description="已故障的阀门ID列表(可选)"),
): ):
result = { """
"accident_element": "P461309", 阀门隔离分析
"accident_elements": ["P461309"],
"affected_nodes": [ - **network**: 管网名称(或数据库名称)
"J316629_A", - **accident_element**: 发生事故的管段/节点ID列表
"J317037_B", - **disabled_valves**: 已故障的阀门ID列表(可选)
"J317060_B",
"J408189_B", 返回隔离方案,包括:
"J499996", - must_close_valves: 必须关闭的阀门列表
"J524940", - optional_valves: 可选关闭的阀门列表
"J535933", - affected_nodes: 受影响的节点列表
"J58841", - isolatable: 是否可以有效隔离
], """
"isolatable": True, # result = {
"must_close_valves": ["210521658", "V12974", "V12986", "V12993"], # "accident_element": "P461309",
"optional_valves": [], # "accident_elements": ["P461309"],
} # "affected_nodes": [
# "J316629_A",
# "J317037_B",
# "J317060_B",
# "J408189_B",
# "J499996",
# "J524940",
# "J535933",
# "J58841",
# ],
# "isolatable": True,
# "must_close_valves": ["210521658", "V12974", "V12986", "V12993"],
# "optional_valves": [],
# }
result = analyze_valve_isolation(network, accident_element, disabled_valves) result = analyze_valve_isolation(network, accident_element, disabled_valves)
return result return result
@router.get("/flushinganalysis/") @router.get("/flushing-analysis", response_class=PlainTextResponse, summary="冲洗分析(高级)", description="高级版本的冲洗分析,支持同时开启多个阀门进行冲洗,指定排污节点,并设置固定的冲洗流量。返回纯文本格式的分析结果。")
async def flushing_analysis_endpoint( @router.get("/flushing_analysis/", response_class=PlainTextResponse, summary="冲洗分析(高级,旧路径)", description="高级版本的冲洗分析,支持同时开启多个阀门进行冲洗,指定排污节点,并设置固定的冲洗流量。返回纯文本格式的分析结果。", deprecated=True)
network: str, pipe_id: str, start_time: str, duration: float, flow: float
):
return flushing_analysis(network, pipe_id, start_time, duration, flow)
@router.get("/flushing_analysis/", response_class=PlainTextResponse)
async def fastapi_flushing_analysis( async def fastapi_flushing_analysis(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
start_time: str, start_time: str = Query(..., description="冲洗开始时间(ISO 8601格式)"),
valves: List[str] = Query(...), valves: List[str] = Query(..., description="要开启的阀门ID列表"),
valves_k: List[float] = Query(...), valves_k: List[float] = Query(..., description="对应各阀门的开度列表(0-1"),
drainage_node_ID: str = Query(...), drainage_node_ID: str = Query(..., description="排污节点ID"),
flush_flow: float = 0, flush_flow: float = Query(0, description="冲洗流量(L/s),0表示自动计算"),
duration: int | None = None, duration: int | None = Query(None, description="模拟持续时间(秒),默认900秒"),
scheme_name: str | None = None, scheme_name: str = Query(..., description="冲洗方案名称"),
username: str = Depends(get_current_keycloak_username),
) -> str: ) -> str:
"""
冲洗分析(高级版本)
- **network**: 管网名称(或数据库名称)
- **start_time**: 冲洗开始时间
- **valves**: 要开启的阀门ID列表
- **valves_k**: 各阀门的开度列表(0-1,与valves对应)
- **drainage_node_ID**: 排污节点ID
- **flush_flow**: 冲洗流量(L/s
- **duration**: 模拟持续时间(秒,可选,默认900)
- **scheme_name**: 冲洗方案名称
支持多阀联合冲洗操作。
"""
valve_opening = { valve_opening = {
valve_id: float(valves_k[idx]) for idx, valve_id in enumerate(valves) valve_id: float(valves_k[idx]) for idx, valve_id in enumerate(valves)
} }
@@ -299,20 +344,36 @@ async def fastapi_flushing_analysis(
drainage_node_ID=drainage_node_ID, drainage_node_ID=drainage_node_ID,
flushing_flow=flush_flow, flushing_flow=flush_flow,
scheme_name=scheme_name, scheme_name=scheme_name,
username=username,
) )
return result or "success" return result or "success"
@router.get("/contaminant_simulation/", response_class=PlainTextResponse) @router.get("/contaminant-simulation", response_class=PlainTextResponse, summary="污染物模拟", description="对管网中的污染物扩散进行模拟,评估污染源对管网的影响范围和浓度分布。支持指定污染源位置、污染浓度和扩散模式。")
@router.get("/contaminant_simulation/", response_class=PlainTextResponse, summary="污染物模拟(旧路径)", description="对管网中的污染物扩散进行模拟,评估污染源对管网的影响范围和浓度分布。支持指定污染源位置、污染浓度和扩散模式。", deprecated=True)
async def fastapi_contaminant_simulation( async def fastapi_contaminant_simulation(
network: str, network: str = Query(..., description="管网名称(或数据库名称)"),
start_time: str, start_time: str = Query(..., description="污染开始时间(ISO 8601格式)"),
source: str, source: str = Query(..., description="污染源节点ID"),
concentration: float, concentration: float = Query(..., description="污染浓度(mg/L"),
duration: int, duration: int = Query(..., description="模拟持续时间(秒)"),
scheme_name: str | None = None, scheme_name: str = Query(..., description="模拟方案名称"),
pattern: str | None = None, pattern: str | None = Query(None, description="污染源模式ID(可选)"),
username: str = Depends(get_current_keycloak_username),
) -> str: ) -> str:
"""
污染物模拟
- **network**: 管网名称(或数据库名称)
- **start_time**: 污染开始时间
- **source**: 污染源节点ID
- **concentration**: 污染浓度(mg/L
- **duration**: 模拟持续时间(秒)
- **scheme_name**: 模拟方案名称
- **pattern**: 污染源模式ID(可选)
用于评估管网中污染物的传播和影响范围。
"""
result = contaminant_simulation( result = contaminant_simulation(
name=network, name=network,
modify_pattern_start_time=start_time, modify_pattern_start_time=start_time,
@@ -321,19 +382,26 @@ async def fastapi_contaminant_simulation(
source=source, source=source,
concentration=concentration, concentration=concentration,
source_pattern=pattern, source_pattern=pattern,
username=username,
) )
return result or "success" return result or "success"
@router.get("/ageanalysis/") @router.get("/age_analysis/", response_class=PlainTextResponse, summary="水龄分析(高级)", description="高级版本的水龄分析,在指定时间点进行分析,支持自定义模拟持续时间。返回纯文本格式的分析结果。")
async def age_analysis_endpoint(network: str):
return age_analysis(network)
@router.get("/age_analysis/", response_class=PlainTextResponse)
async def fastapi_age_analysis( async def fastapi_age_analysis(
network: str, start_time: str, end_time: str, duration: int network: str = Query(..., description="管网名称(或数据库名称)"),
start_time: str = Query(..., description="分析开始时间(ISO 8601格式)"),
duration: int = Query(..., description="模拟持续时间(秒)"),
) -> str: ) -> str:
"""
水龄分析(高级版本)
- **network**: 管网名称(或数据库名称)
- **start_time**: 分析开始时间
- **duration**: 模拟持续时间(秒)
分析指定时间段内管网中各节点的水体停留时间。
"""
result = age_analysis(network, start_time, duration) result = age_analysis(network, start_time, duration)
return result or "success" return result or "success"
@@ -343,15 +411,39 @@ async def fastapi_age_analysis(
# return scheduling_analysis(network) # return scheduling_analysis(network)
@router.get("/pressureregulation/") @router.get("/pressureregulation/", summary="压力调节(基础)", description="对管网的压力进行调节分析,通过控制泵的运行来维持目标节点的目标压力。此为基础版本。")
async def pressure_regulation_endpoint( async def pressure_regulation_endpoint(
network: str, target_node: str, target_pressure: float network: str = Query(..., description="管网名称(或数据库名称)"),
target_node: str = Query(..., description="目标节点ID"),
target_pressure: float = Query(..., description="目标压力值(kPa"),
): ):
"""
压力调节(基础版本)
- **network**: 管网名称(或数据库名称)
- **target_node**: 目标节点ID
- **target_pressure**: 目标压力值(kPa
通过泵控制维持目标节点的压力。
"""
return pressure_regulation(network, target_node, target_pressure) return pressure_regulation(network, target_node, target_pressure)
@router.post("/pressure_regulation/") @router.post("/pressure_regulation/", summary="压力调节(高级)", description="高级版本的压力调节分析,通过JSON请求体提供详细的控制参数,包括固定泵和变速泵的独立控制、水箱初始水位等。")
async def fastapi_pressure_regulation(data: PressureRegulation) -> str: async def fastapi_pressure_regulation(data: PressureRegulation = Body(..., description="压力调节控制参数")) -> str:
"""
压力调节(高级版本)
请求体参数:
- **network**: 管网名称(或数据库名称)
- **start_time**: 控制开始时间
- **pump_control**: 泵控制策略字典
- **tank_init_level**: 水箱初始水位字典(可选)
- **duration**: 模拟持续时间(秒,可选,默认900)
- **scheme_name**: 控制方案名称(可选)
支持固定泵和变速泵的独立控制。
"""
item = data.dict() item = data.dict()
simulation.query_corresponding_element_id_and_query_id(item["network"]) simulation.query_corresponding_element_id_and_query_id(item["network"])
fixed_pumps = set(globals.fixed_pumps_id.keys()) fixed_pumps = set(globals.fixed_pumps_id.keys())
@@ -375,13 +467,20 @@ async def fastapi_pressure_regulation(data: PressureRegulation) -> str:
return "success" return "success"
@router.get("/projectmanagement/") @router.post("/project_management/", summary="项目管理(高级)", description="高级版本的项目管理,通过JSON请求体提供详细的控制参数,包括泵控制策略、水箱初始水位和区域需水量控制。")
async def project_management_endpoint(network: str): async def fastapi_project_management(data: ProjectManagement = Body(..., description="项目管理控制参数")) -> str:
return project_management(network) """
项目管理(高级版本)
@router.post("/project_management/") 请求体参数:
async def fastapi_project_management(data: ProjectManagement) -> str: - **network**: 管网名称(或数据库名称)
- **start_time**: 管理开始时间
- **pump_control**: 泵控制策略字典
- **tank_init_level**: 水箱初始水位字典(可选)
- **region_demand**: 区域需水量控制字典(可选)
支持多维度的项目管理。
"""
item = data.dict() item = data.dict()
return project_management( return project_management(
prj_name=item["network"], prj_name=item["network"],
@@ -397,8 +496,21 @@ async def fastapi_project_management(data: ProjectManagement) -> str:
# return daily_scheduling_analysis(network) # return daily_scheduling_analysis(network)
@router.post("/scheduling_analysis/") @router.post("/scheduling_analysis/", summary="排程分析", description="对管网的供水排程进行分析,优化泵的运行时间和出水流量,平衡水厂出水、水箱进出水,满足用户需求。")
async def fastapi_scheduling_analysis(data: SchedulingAnalysis) -> str: async def fastapi_scheduling_analysis(data: SchedulingAnalysis = Body(..., description="排程分析参数")) -> str:
"""
排程分析
请求体参数:
- **network**: 管网名称(或数据库名称)
- **start_time**: 分析开始时间
- **pump_control**: 泵控制策略字典
- **tank_id**: 水箱ID
- **water_plant_output_id**: 水厂出水ID
- **time_delta**: 时间步长(秒,可选,默认300)
用于优化供水排程。
"""
item = data.dict() item = data.dict()
return scheduling_simulation( return scheduling_simulation(
item["network"], item["network"],
@@ -410,8 +522,22 @@ async def fastapi_scheduling_analysis(data: SchedulingAnalysis) -> str:
) )
@router.post("/daily_scheduling_analysis/") @router.post("/daily_scheduling_analysis/", summary="日排程分析", description="对管网的每日供水排程进行分析,优化水库、水厂、水箱和用户需求的协调,制定合理的每日排程方案。")
async def fastapi_daily_scheduling_analysis(data: DailySchedulingAnalysis) -> str: async def fastapi_daily_scheduling_analysis(data: DailySchedulingAnalysis = Body(..., description="日排程分析参数")) -> str:
"""
日排程分析
请求体参数:
- **network**: 管网名称(或数据库名称)
- **start_time**: 分析开始时间
- **pump_control**: 泵控制策略字典
- **reservoir_id**: 水库ID
- **tank_id**: 水箱ID
- **water_plant_output_id**: 水厂出水ID
- **time_delta**: 时间步长(秒,可选,默认300)
用于制定每日供水排程方案。
"""
item = data.dict() item = data.dict()
return daily_scheduling_simulation( return daily_scheduling_simulation(
item["network"], item["network"],
@@ -423,8 +549,15 @@ async def fastapi_daily_scheduling_analysis(data: DailySchedulingAnalysis) -> st
) )
@router.post("/network_project/") @router.post("/network_project/", summary="导入网络项目", description="通过上传INP格式的管网文件导入新的网络项目。系统将自动处理文件并执行模拟。")
async def fastapi_network_project(file: UploadFile = File()) -> str: async def fastapi_network_project(file: UploadFile = File(..., description="INP格式的管网文件")) -> str:
"""
导入网络项目
- **file**: 上传的INP格式管网文件
系统将上传的文件保存到inp文件夹并执行模拟。
"""
temp_file_dir = "./inp/" temp_file_dir = "./inp/"
if not os.path.exists(temp_file_dir): if not os.path.exists(temp_file_dir):
os.mkdir(temp_file_dir) os.mkdir(temp_file_dir)
@@ -435,13 +568,15 @@ async def fastapi_network_project(file: UploadFile = File()) -> str:
return run_inp(temp_file_name) return run_inp(temp_file_name)
@router.get("/networkupdate/") @router.post("/network_update/", summary="管网更新(高级)", description="通过上传更新文件对管网进行高级的更新操作。系统将处理更新文件并应用到数据库。")
async def network_update_endpoint(network: str): async def fastapi_network_update(file: UploadFile = File(..., description="包含管网更新信息的文件")) -> str:
return network_update(network) """
管网更新(高级版本)
@router.post("/network_update/") - **file**: 包含管网更新信息的文件
async def fastapi_network_update(file: UploadFile = File()) -> str:
系统将处理上传的文件并应用管网更新。
"""
default_folder = "./" default_folder = "./"
temp_file_name = f'network_update_{datetime.now().strftime("%Y%m%d")}' temp_file_name = f'network_update_{datetime.now().strftime("%Y%m%d")}'
temp_file_path = os.path.join(default_folder, temp_file_name) temp_file_path = os.path.join(default_folder, temp_file_name)
@@ -459,8 +594,17 @@ async def fastapi_network_update(file: UploadFile = File()) -> str:
# return pump_failure(network, pump_id, time) # return pump_failure(network, pump_id, time)
@router.post("/pump_failure/") @router.post("/pump_failure/", summary="泵故障管理", description="记录和管理泵的故障状态,包括故障发生时间和受影响的泵列表。系统将记录故障日志并更新泵状态。")
async def fastapi_pump_failure(data: PumpFailureState) -> str: async def fastapi_pump_failure(data: PumpFailureState = Body(..., description="泵故障状态信息")) -> str:
"""
泵故障管理
请求体参数:
- **time**: 故障发生时间
- **pump_status**: 泵状态字典,包含第一阶段和第二阶段泵的故障状态
系统将验证泵信息的有效性并更新故障状态文件。
"""
item = data.dict() item = data.dict()
with open("./pump_failure_message.txt", "a", encoding="utf-8-sig") as f1: with open("./pump_failure_message.txt", "a", encoding="utf-8-sig") as f1:
f1.write("[{}] {}\n".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), item)) f1.write("[{}] {}\n".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), item))
@@ -494,19 +638,46 @@ async def fastapi_pump_failure(data: PumpFailureState) -> str:
return json.dumps("SUCCESS") return json.dumps("SUCCESS")
@router.get("/pressuresensorplacementsensitivity/") @router.get("/pressuresensorplacementsensitivity/", summary="压力传感器放置-灵敏度分析(基础)", description="基于灵敏度分析方法,为指定管网项目确定最优的压力传感器放置位置。此为基础版本。")
async def pressure_sensor_placement_sensitivity_endpoint( async def pressure_sensor_placement_sensitivity_endpoint(
name: str, scheme_name: str, sensor_number: int, min_diameter: int, username: str name: str = Query(..., description="管网名称(或数据库名称)"),
scheme_name: str = Query(..., description="放置方案名称"),
sensor_number: int = Query(..., description="传感器数量"),
min_diameter: int = Query(..., description="最小管径限制(毫米)"),
username: str = Query(..., description="用户名"),
): ):
"""
压力传感器放置-灵敏度分析(基础版本)
- **name**: 管网名称(或数据库名称)
- **scheme_name**: 放置方案名称
- **sensor_number**: 传感器数量
- **min_diameter**: 最小管径限制(毫米)
- **username**: 用户名
基于灵敏度分析方法确定传感器放置位置。
"""
return pressure_sensor_placement_sensitivity( return pressure_sensor_placement_sensitivity(
name, scheme_name, sensor_number, min_diameter, username name, scheme_name, sensor_number, min_diameter, username
) )
@router.post("/pressure_sensor_placement_sensitivity/") @router.post("/pressure_sensor_placement_sensitivity/", summary="压力传感器放置-灵敏度分析(高级)", description="高级版本的压力传感器放置分析,通过JSON请求体提供详细参数。基于灵敏度分析方法确定最优放置位置。")
async def fastapi_pressure_sensor_placement_sensitivity( async def fastapi_pressure_sensor_placement_sensitivity(
data: PressureSensorPlacement, data: PressureSensorPlacement = Body(..., description="传感器放置分析参数"),
) -> None: ) -> None:
"""
压力传感器放置-灵敏度分析(高级版本)
请求体参数:
- **name**: 管网名称(或数据库名称)
- **scheme_name**: 放置方案名称
- **sensor_number**: 传感器数量
- **min_diameter**: 最小管径限制(毫米)
- **username**: 用户名
基于灵敏度分析方法确定压力传感器的最优放置位置。
"""
item = data.dict() item = data.dict()
pressure_sensor_placement_sensitivity( pressure_sensor_placement_sensitivity(
name=item["name"], name=item["name"],
@@ -517,19 +688,46 @@ async def fastapi_pressure_sensor_placement_sensitivity(
) )
@router.get("/pressuresensorplacementkmeans/") @router.get("/pressuresensorplacementkmeans/", summary="压力传感器放置-KMeans聚类分析(基础)", description="基于KMeans聚类算法,为指定管网项目确定压力传感器的最优放置位置。此为基础版本。")
async def pressure_sensor_placement_kmeans_endpoint( async def pressure_sensor_placement_kmeans_endpoint(
name: str, scheme_name: str, sensor_number: int, min_diameter: int, username: str name: str = Query(..., description="管网名称(或数据库名称)"),
scheme_name: str = Query(..., description="放置方案名称"),
sensor_number: int = Query(..., description="传感器数量"),
min_diameter: int = Query(..., description="最小管径限制(毫米)"),
username: str = Query(..., description="用户名"),
): ):
"""
压力传感器放置-KMeans聚类分析(基础版本)
- **name**: 管网名称(或数据库名称)
- **scheme_name**: 放置方案名称
- **sensor_number**: 传感器数量
- **min_diameter**: 最小管径限制(毫米)
- **username**: 用户名
基于KMeans聚类算法确定传感器放置位置。
"""
return pressure_sensor_placement_kmeans( return pressure_sensor_placement_kmeans(
name, scheme_name, sensor_number, min_diameter, username name, scheme_name, sensor_number, min_diameter, username
) )
@router.post("/pressure_sensor_placement_kmeans/") @router.post("/pressure_sensor_placement_kmeans/", summary="压力传感器放置-KMeans聚类分析(高级)", description="高级版本的压力传感器放置分析,通过JSON请求体提供详细参数。基于KMeans聚类算法确定最优放置位置。")
async def fastapi_pressure_sensor_placement_kmeans( async def fastapi_pressure_sensor_placement_kmeans(
data: PressureSensorPlacement, data: PressureSensorPlacement = Body(..., description="传感器放置分析参数"),
) -> None: ) -> None:
"""
压力传感器放置-KMeans聚类分析(高级版本)
请求体参数:
- **name**: 管网名称(或数据库名称)
- **scheme_name**: 放置方案名称
- **sensor_number**: 传感器数量
- **min_diameter**: 最小管径限制(毫米)
- **username**: 用户名
基于KMeans聚类算法确定压力传感器的最优放置位置。
"""
item = data.dict() item = data.dict()
pressure_sensor_placement_kmeans( pressure_sensor_placement_kmeans(
name=item["name"], name=item["name"],
@@ -540,16 +738,32 @@ async def fastapi_pressure_sensor_placement_kmeans(
) )
@router.post("/sensorplacementscheme/create") @router.post("/sensor-placement-schemes", summary="传感器放置方案创建", description="创建新的传感器放置方案,支持灵敏度分析和KMeans聚类两种方法。根据指定的方法自动计算最优的传感器放置位置。")
@router.post("/sensorplacementscheme/create", summary="传感器放置方案创建(旧路径)", description="创建新的传感器放置方案,支持灵敏度分析和KMeans聚类两种方法。根据指定的方法自动计算最优的传感器放置位置。", deprecated=True)
async def fastapi_pressure_sensor_placement( async def fastapi_pressure_sensor_placement(
network: str = Query(...), network: str = Query(..., description="管网名称(或数据库名称)"),
scheme_name: str = Query(...), scheme_name: str = Query(..., description="放置方案名称"),
sensor_type: str = Query(...), sensor_type: str = Query(..., description="传感器类型"),
method: str = Query(...), method: str = Query(..., description="放置方法('sensitivity''kmeans'"),
sensor_count: int = Query(...), sensor_count: int = Query(..., description="传感器数量"),
min_diameter: int = Query(0), min_diameter: int = Query(0, description="最小管径限制(毫米),默认0"),
user_name: str = Query(...), user_name: str = Query(..., description="用户名"),
) -> str: ) -> str:
"""
传感器放置方案创建
- **network**: 管网名称(或数据库名称)
- **scheme_name**: 放置方案名称
- **sensor_type**: 传感器类型
- **method**: 放置方法('sensitivity''kmeans'
- **sensor_count**: 传感器数量
- **min_diameter**: 最小管径限制(毫米,默认0)
- **user_name**: 用户名
支持两种放置方法:
- sensitivity: 基于灵敏度分析
- kmeans: 基于KMeans聚类
"""
if method not in ["sensitivity", "kmeans"]: if method not in ["sensitivity", "kmeans"]:
raise HTTPException( raise HTTPException(
status_code=400, detail="Invalid method. Must be 'sensitivity' or 'kmeans'" status_code=400, detail="Invalid method. Must be 'sensitivity' or 'kmeans'"
@@ -573,68 +787,23 @@ async def fastapi_pressure_sensor_placement(
return "success" return "success"
@router.post("/scadadevicedatacleaning/") @router.post("/simulations/run-by-date", summary="手动运行日期指定模拟", description="根据指定的开始时间和持续时间,手动运行水力模拟。开始时间必须是显式带时区的 ISO 8601 / RFC3339 时间。")
async def fastapi_scada_device_data_cleaning( @router.post("/runsimulationmanuallybydate/", summary="手动运行日期指定模拟(旧路径)", description="根据指定的开始时间和持续时间,手动运行水力模拟。开始时间必须是显式带时区的 ISO 8601 / RFC3339 时间。", deprecated=True)
network: str = Query(...),
ids_list: List[str] = Query(...),
start_time: str = Query(...),
end_time: str = Query(...),
user_name: str = Query(...),
) -> str:
item = {
"network": network,
"ids": ids_list,
"start_time": start_time,
"end_time": end_time,
"user_name": user_name,
}
query_ids_list = item["ids"][0].split(",")
scada_data = influxdb_api.query_SCADA_data_by_device_ID_and_timerange(
query_ids_list=query_ids_list,
start_time=item["start_time"],
end_time=item["end_time"],
)
scada_device_info = influxdb_api.query_pg_scada_info(item["network"])
scada_device_info_dict = {info["id"]: info for info in scada_device_info}
type_groups: dict[str, list[str]] = {}
for device_id in query_ids_list:
device_info = scada_device_info_dict.get(device_id, {})
device_type = device_info.get("type", "unknown")
type_groups.setdefault(device_type, []).append(device_id)
for device_type, device_ids in type_groups.items():
if device_type not in ["pressure", "pipe_flow"]:
continue
type_scada_data = {
device_id: scada_data[device_id]
for device_id in device_ids
if device_id in scada_data
}
if not type_scada_data:
continue
time_list = [record["time"] for record in next(iter(type_scada_data.values()))]
df = pd.DataFrame({"time": time_list})
for device_id in device_ids:
if device_id in type_scada_data:
values = [record["value"] for record in type_scada_data[device_id]]
df[device_id] = values
if device_type == "pressure":
cleaned_value_df = pressure_data_clean.clean_pressure_data_df_km(df)
elif device_type == "pipe_flow":
cleaned_value_df = flow_data_clean.clean_flow_data_df_kf(df)
cleaned_value_df = pd.DataFrame(cleaned_value_df)
cleaned_df = pd.concat([df["time"], cleaned_value_df], axis=1)
influxdb_api.import_multicolumn_data_from_dict(
data_dict=cleaned_df.to_dict("list"),
raw=False,
)
return "success"
@router.post("/runsimulationmanuallybydate/")
async def fastapi_run_simulation_manually_by_date( async def fastapi_run_simulation_manually_by_date(
data: RunSimulationManuallyByDate, data: RunSimulationManuallyByDate = Body(..., description="模拟运行参数"),
) -> dict[str, str]: ) -> dict[str, str]:
item = data.dict() """
手动运行日期指定模拟
请求体参数:
- **name**: 管网名称(或数据库名称)
- **start_time**: 开始时间(ISO 8601 / RFC3339,必须显式带时区)
- **duration**: 模拟持续时间(分钟)
系统将从指定时间开始,按15分钟间隔多次运行模拟。
每次模拟间隔15分钟,直至达到指定的总持续时间。
"""
item = data.model_dump()
try: try:
simulation.query_corresponding_element_id_and_query_id(item["name"]) simulation.query_corresponding_element_id_and_query_id(item["name"])
simulation.query_corresponding_pattern_id_and_query_id(item["name"]) simulation.query_corresponding_pattern_id_and_query_id(item["name"])
@@ -661,10 +830,10 @@ async def fastapi_run_simulation_manually_by_date(
globals.source_outflow_region_id, globals.source_outflow_region_id,
globals.realtime_region_pipe_flow_and_demand_id, globals.realtime_region_pipe_flow_and_demand_id,
) )
base_date = datetime.strptime(item["simulation_date"], "%Y-%m-%d") start_time = parse_utc_time(item["start_time"], field_name="start_time")
run_simulation_manually_by_date( run_simulation_manually_by_date(
item["name"], base_date, item["start_time"], item["duration"] item["name"], start_time, item["duration"]
) )
return {"status": "success"} return {"status": "success"}
except Exception as exc: except Exception as exc:
return {"status": "error", "message": str(exc)} raise HTTPException(status_code=500, detail=str(exc)) from exc
+132 -39
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query
from app.services.tjnetwork import ( from app.services.tjnetwork import (
ChangeSet, ChangeSet,
get_current_operation, get_current_operation,
@@ -22,90 +22,183 @@ from app.services.tjnetwork import (
router = APIRouter() router = APIRouter()
@router.get("/getcurrentoperationid/") @router.get("/getcurrentoperationid/", summary="获取当前操作ID", description="获取网络当前的操作ID")
async def get_current_operation_id_endpoint(network: str) -> int: async def get_current_operation_id_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")) -> int:
"""
获取当前操作ID
返回网络当前正在执行的操作ID
"""
return get_current_operation(network) return get_current_operation(network)
@router.post("/undo/") @router.post("/undo/", summary="撤销操作", description="撤销网络上最后的一个操作")
async def undo_endpoint(network: str): async def undo_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")):
"""
撤销操作
撤销网络上最近执行的一个操作
"""
return execute_undo(network) return execute_undo(network)
@router.post("/redo/") @router.post("/redo/", summary="重做操作", description="重做网络上被撤销的操作")
async def redo_endpoint(network: str): async def redo_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")):
"""
重做操作
重做网络上被撤销的操作
"""
return execute_redo(network) return execute_redo(network)
@router.get("/getsnapshots/") @router.get("/getsnapshots/", summary="获取快照列表", description="获取网络中的所有快照")
async def list_snapshot_endpoint(network: str) -> list[tuple[int, str]]: async def list_snapshot_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[tuple[int, str]]:
"""
获取快照列表
返回网络中所有可用的快照及其信息
"""
return list_snapshot(network) return list_snapshot(network)
@router.get("/havesnapshot/") @router.get("/havesnapshot/", summary="检查快照是否存在", description="检查指定标签的快照是否存在")
async def have_snapshot_endpoint(network: str, tag: str) -> bool: async def have_snapshot_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), tag: str = Query(..., description="快照标签")) -> bool:
"""
检查快照是否存在
返回指定标签的快照是否存在
"""
return have_snapshot(network, tag) return have_snapshot(network, tag)
@router.get("/havesnapshotforoperation/") @router.get("/havesnapshotforoperation/", summary="检查操作快照是否存在", description="检查指定操作ID的快照是否存在")
async def have_snapshot_for_operation_endpoint(network: str, operation: int) -> bool: async def have_snapshot_for_operation_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), operation: int = Query(..., description="操作ID")) -> bool:
"""
检查操作快照是否存在
返回指定操作ID的快照是否存在
"""
return have_snapshot_for_operation(network, operation) return have_snapshot_for_operation(network, operation)
@router.get("/havesnapshotforcurrentoperation/") @router.get("/havesnapshotforcurrentoperation/", summary="检查当前操作快照是否存在", description="检查当前操作的快照是否存在")
async def have_snapshot_for_current_operation_endpoint(network: str) -> bool: async def have_snapshot_for_current_operation_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")) -> bool:
"""
检查当前操作快照是否存在
返回当前操作的快照是否存在
"""
return have_snapshot_for_current_operation(network) return have_snapshot_for_current_operation(network)
@router.post("/takesnapshotforoperation/") @router.post("/takesnapshotforoperation/", summary="为操作创建快照", description="为指定的操作创建快照")
async def take_snapshot_for_operation_endpoint( async def take_snapshot_for_operation_endpoint(
network: str, operation: int, tag: str network: str = Query(..., description="管网名称(或数据库名称)"),
operation: int = Query(..., description="操作ID"),
tag: str = Query(..., description="快照标签")
) -> None: ) -> None:
"""
为操作创建快照
为指定操作创建一个带标签的快照
"""
return take_snapshot_for_operation(network, operation, tag) return take_snapshot_for_operation(network, operation, tag)
@router.post("/takesnapshotforcurrentoperation") @router.post("/takesnapshotforcurrentoperation", summary="为当前操作创建快照", description="为当前操作创建快照")
async def take_snapshot_for_current_operation_endpoint(network: str, tag: str) -> None: async def take_snapshot_for_current_operation_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), tag: str = Query(..., description="快照标签")) -> None:
"""
为当前操作创建快照
为网络当前操作创建一个快照
"""
return take_snapshot_for_current_operation(network, tag) return take_snapshot_for_current_operation(network, tag)
# 兼容旧拼写: takenapshotforcurrentoperation # 兼容旧拼写: takenapshotforcurrentoperation
@router.post("/takenapshotforcurrentoperation") @router.post("/takenapshotforcurrentoperation", summary="为当前操作创建快照(兼容模式)", description="为当前操作创建快照(兼容旧的API路径)")
async def take_snapshot_for_current_operation_legacy_endpoint( async def take_snapshot_for_current_operation_legacy_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), tag: str = Query(..., description="快照标签")) -> None:
network: str, tag: str """
) -> None: 为当前操作创建快照(兼容模式)
兼容旧的API路径,为网络当前操作创建一个快照
"""
return take_snapshot_for_current_operation(network, tag) return take_snapshot_for_current_operation(network, tag)
@router.post("/takesnapshot/") @router.post("/takesnapshot/", summary="创建快照", description="为网络创建一个快照")
async def take_snapshot_endpoint(network: str, tag: str) -> None: async def take_snapshot_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), tag: str = Query(..., description="快照标签")) -> None:
"""
创建快照
为网络创建一个带标签的快照
"""
return take_snapshot(network, tag) return take_snapshot(network, tag)
@router.post("/picksnapshot/", response_model=None) @router.post("/picksnapshot/", summary="选择快照", description="选择并恢复到指定的快照", response_model=None)
async def pick_snapshot_endpoint(network: str, tag: str, discard: bool = False) -> ChangeSet: async def pick_snapshot_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), tag: str = Query(..., description="快照标签"), discard: bool = Query(False, description="是否丢弃当前更改")) -> ChangeSet:
"""
选择快照
选择并恢复到指定的快照
"""
return pick_snapshot(network, tag, discard) return pick_snapshot(network, tag, discard)
@router.post("/pickoperation/", response_model=None) @router.post("/pickoperation/", summary="选择操作", description="选择并恢复到指定的操作", response_model=None)
async def pick_operation_endpoint( async def pick_operation_endpoint(
network: str, operation: int, discard: bool = False network: str = Query(..., description="管网名称(或数据库名称)"),
operation: int = Query(..., description="操作ID"),
discard: bool = Query(False, description="是否丢弃当前更改")
) -> ChangeSet: ) -> ChangeSet:
"""
选择操作
选择并恢复到指定的操作
"""
return pick_operation(network, operation, discard) return pick_operation(network, operation, discard)
@router.get("/syncwithserver/", response_model=None) @router.get("/syncwithserver/", summary="与服务器同步", description="将网络与服务器同步到指定操作", response_model=None)
async def sync_with_server_endpoint(network: str, operation: int) -> ChangeSet: async def sync_with_server_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), operation: int = Query(..., description="目标操作ID")) -> ChangeSet:
"""
与服务器同步
将网络与服务器同步到指定的操作
"""
return sync_with_server(network, operation) return sync_with_server(network, operation)
@router.post("/batch/", response_model=None) @router.post("/batch/", summary="执行批量命令", description="执行多个网络操作命令", response_model=None)
async def execute_batch_commands_endpoint(network: str, req: Request) -> ChangeSet: async def execute_batch_commands_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), req: Request = None) -> ChangeSet:
"""
执行批量命令
在网络上执行多个操作命令
"""
jo_root = await req.json() jo_root = await req.json()
cs: ChangeSet = ChangeSet() cs: ChangeSet = ChangeSet()
cs.operations = jo_root["operations"] cs.operations = jo_root["operations"]
rcs = execute_batch_commands(network, cs) rcs = execute_batch_commands(network, cs)
return rcs return rcs
@router.post("/compressedbatch/", response_model=None) @router.post("/compressedbatch/", summary="执行压缩批量命令", description="执行压缩的批量命令", response_model=None)
async def execute_compressed_batch_commands_endpoint( async def execute_compressed_batch_commands_endpoint(
network: str, req: Request network: str = Query(..., description="管网名称(或数据库名称)"),
req: Request = None
) -> ChangeSet: ) -> ChangeSet:
"""
执行压缩批量命令
执行压缩格式的批量命令
"""
jo_root = await req.json() jo_root = await req.json()
cs: ChangeSet = ChangeSet() cs: ChangeSet = ChangeSet()
cs.operations = jo_root["operations"] cs.operations = jo_root["operations"]
return execute_batch_command(network, cs) return execute_batch_command(network, cs)
@router.get("/getrestoreoperation/") @router.get("/getrestoreoperation/", summary="获取恢复操作ID", description="获取网络的恢复操作ID")
async def get_restore_operation_endpoint(network: str) -> int: async def get_restore_operation_endpoint(network: str = Query(..., description="管网名称(或数据库名称)")) -> int:
"""
获取恢复操作ID
返回网络的恢复操作ID
"""
return get_restore_operation(network) return get_restore_operation(network)
@router.post("/setrestoreoperation/") @router.post("/setrestoreoperation/", summary="设置恢复操作ID", description="设置网络的恢复操作ID")
async def set_restore_operation_endpoint(network: str, operation: int) -> None: async def set_restore_operation_endpoint(network: str = Query(..., description="管网名称(或数据库名称)"), operation: int = Query(..., description="操作ID")) -> None:
"""
设置恢复操作ID
设置网络的恢复操作ID
"""
return set_restore_operation(network, operation) return set_restore_operation(network, operation)
+95 -37
View File
@@ -8,21 +8,36 @@ from .dependencies import get_timescale_connection, get_postgres_connection
router = APIRouter() router = APIRouter()
@router.get("/composite/scada-simulation") @router.get("/composite/scada-simulation", summary="获取SCADA关联的模拟数据")
async def get_scada_associated_simulation_data( async def get_scada_associated_simulation_data(
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
device_ids: str, device_ids: str = Query(..., description="SCADA设备ID列表,逗号分隔"),
scheme_type: str = Query(None, description="指定方案名称,若为空则查询实时数据"), scheme_type: str = Query(None, description="方案类型,若为空则查询实时数据"),
scheme_name: str = Query(None, description="指定方案名称,若为空则查询实时数据"), scheme_name: str = Query(None, description="方案名称,若为空则查询实时数据"),
timescale_conn: AsyncConnection = Depends(get_timescale_connection), timescale_conn: AsyncConnection = Depends(get_timescale_connection),
postgres_conn: AsyncConnection = Depends(get_postgres_connection), postgres_conn: AsyncConnection = Depends(get_postgres_connection),
): ):
""" """
获取 SCADA 关联的 link/node 模拟值 获取SCADA关联的link/node模拟值
根据传入的SCADA device_ids,找到关联的link/node
并根据对应的type,查询对应的模拟数据。支持查询实时或方案数据。
根据传入的 SCADA device_ids,找到关联的 link/node Args:
并根据对应的 type,查询对应的模拟数据 start_time: 查询开始时间
end_time: 查询结束时间
device_ids: SCADA设备ID列表,用逗号分隔
scheme_type: 方案类型,若为空则查询实时数据
scheme_name: 方案名称,若为空则查询实时数据
timescale_conn: TimescaleDB连接
postgres_conn: PostgreSQL连接
Returns:
SCADA关联的模拟数据
Raises:
HTTPException: 当查询参数无效时返回400错误,未找到数据时返回404错误
""" """
try: try:
device_ids_list = ( device_ids_list = (
@@ -58,26 +73,37 @@ async def get_scada_associated_simulation_data(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.get("/composite/element-simulation") @router.get("/composite/element-simulation", summary="获取管网元素的模拟数据")
async def get_feature_simulation_data( async def get_feature_simulation_data(
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
feature_infos: str = Query( feature_infos: str = Query(
..., description="特征信息,格式: id1:type1,id2:type2type为pipe或junction" ..., description="特征信息,格式: id1:type1,id2:type2type为pipe(管道)或junction(节点)"
), ),
scheme_type: str = Query(None, description="指定方案类型,若为空则查询实时数据"), scheme_type: str = Query(None, description="方案类型,若为空则查询实时数据"),
scheme_name: str = Query(None, description="指定方案名称,若为空则查询实时数据"), scheme_name: str = Query(None, description="方案名称,若为空则查询实时数据"),
timescale_conn: AsyncConnection = Depends(get_timescale_connection), timescale_conn: AsyncConnection = Depends(get_timescale_connection),
): ):
""" """
获取 link/node 模拟值 获取link/node模拟值
根据传入的 featureInfos,找到关联的 link/node 根据传入的featureInfos,找到关联的link/node
并根据对应的 type,查询对应的模拟数据 并根据对应的type,查询对应的模拟数据。支持查询实时或方案数据。
Args: Args:
start_time: 查询开始时间
end_time: 查询结束时间
feature_infos: 格式为 "element_id1:type1,element_id2:type2" feature_infos: 格式为 "element_id1:type1,element_id2:type2"
例如: "P1:pipe,J1:junction" 例如: "P1:pipe,J1:junction"
scheme_type: 方案类型,若为空则查询实时数据
scheme_name: 方案名称,若为空则查询实时数据
timescale_conn: TimescaleDB连接
Returns:
管网元素的模拟数据
Raises:
HTTPException: 当feature_infos为空返回400错误,未找到数据返回404错误,其他错误返回400错误
""" """
try: try:
feature_infos_list = [] feature_infos_list = []
@@ -117,20 +143,34 @@ async def get_feature_simulation_data(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.get("/composite/element-scada") @router.get("/composite/element-scada", summary="获取管网元素关联的SCADA监测数据")
async def get_element_associated_scada_data( async def get_element_associated_scada_data(
element_id: str, element_id: str = Query(..., description="管网元素ID(管道或节点)"),
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
use_cleaned: bool = Query(False, description="是否使用清洗后的数据"), use_cleaned: bool = Query(False, description="是否使用清洗后的数据"),
timescale_conn: AsyncConnection = Depends(get_timescale_connection), timescale_conn: AsyncConnection = Depends(get_timescale_connection),
postgres_conn: AsyncConnection = Depends(get_postgres_connection), postgres_conn: AsyncConnection = Depends(get_postgres_connection),
): ):
""" """
获取 link/node 关联的 SCADA 监测值 获取link/node关联的SCADA监测值
根据传入的link/node id,匹配SCADA信息,
如果存在关联的SCADA device_id,获取实际的监测数据。
根据传入的 link/node id,匹配 SCADA 信息, Args:
如果存在关联的 SCADA device_id,获取实际的监测数据 element_id: 管网元素ID
start_time: 查询开始时间
end_time: 查询结束时间
use_cleaned: 是否使用清洗后的数据,默认为False使用原始数据
timescale_conn: TimescaleDB连接
postgres_conn: PostgreSQL连接
Returns:
管网元素关联的SCADA监测数据
Raises:
HTTPException: 当查询参数无效时返回400错误,未找到关联数据返回404错误
""" """
try: try:
result = await CompositeQueries.get_element_associated_scada_data( result = await CompositeQueries.get_element_associated_scada_data(
@@ -145,18 +185,32 @@ async def get_element_associated_scada_data(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.post("/composite/clean-scada") @router.post("/composite/clean-scada", summary="清洗SCADA监测数据")
async def clean_scada_data( async def clean_scada_data(
device_ids: str, device_ids: str = Query(..., description="设备ID列表或 'all' 表示清洗所有设备"),
start_time: datetime = Query(...), start_time: datetime = Query(..., description="清洗数据的开始时间"),
end_time: datetime = Query(...), end_time: datetime = Query(..., description="清洗数据的结束时间"),
timescale_conn: AsyncConnection = Depends(get_timescale_connection), timescale_conn: AsyncConnection = Depends(get_timescale_connection),
postgres_conn: AsyncConnection = Depends(get_postgres_connection), postgres_conn: AsyncConnection = Depends(get_postgres_connection),
): ):
""" """
清洗 SCADA 数据 清洗SCADA监测数据
根据device_ids查询monitored_value,清洗后更新cleaned_value。
支持清洗指定设备或所有设备的数据。
根据 device_ids 查询 monitored_value,清洗后更新 cleaned_value Args:
device_ids: 设备ID列表,用逗号分隔,或 'all' 表示清洗所有设备
start_time: 清洗数据的开始时间
end_time: 清洗数据的结束时间
timescale_conn: TimescaleDB连接
postgres_conn: PostgreSQL连接
Returns:
清洗结果信息
Raises:
HTTPException: 当清洗过程出现错误时返回400错误
""" """
try: try:
if device_ids == "all": if device_ids == "all":
@@ -174,24 +228,28 @@ async def clean_scada_data(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.get("/composite/pipeline-health-prediction") @router.get("/composite/pipeline-health-prediction", summary="预测管道健康状况")
async def predict_pipeline_health( async def predict_pipeline_health(
query_time: datetime = Query(..., description="查询时间"), query_time: datetime = Query(..., description="查询时间"),
network_name: str = Query(..., description="管网数据库名称"), network_name: str = Query(..., description="管网名称(或数据库名称"),
timescale_conn: AsyncConnection = Depends(get_timescale_connection), timescale_conn: AsyncConnection = Depends(get_timescale_connection),
): ):
""" """
预测管道健康状况 预测管道健康状况
根据管网名称和当前时间,查询管道信息和实时数据, 根据管网名称和当前时间,查询管道信息和实时数据,
使用随机生存森林模型预测管道的生存概率 使用随机生存森林模型预测管道的生存概率
Args: Args:
query_time: 查询时间 query_time: 查询时间
db_name: 管网数据库名称 network_name: 管网名称(或数据库名称
timescale_conn: TimescaleDB连接
Returns: Returns:
预测结果列表,每个元素包含 link_id 和对应的生存函数 预测结果列表,每个元素包含 link_id 和对应的生存函数
Raises:
HTTPException: 当模型文件不存在返回404错误,其他错误返回400或500错误
""" """
try: try:
return await CompositeQueries.predict_pipeline_health( return await CompositeQueries.predict_pipeline_health(
+200 -37
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Query, Path, Body
from typing import List from typing import List
from datetime import datetime from datetime import datetime
from psycopg import AsyncConnection from psycopg import AsyncConnection
@@ -8,42 +8,108 @@ from .dependencies import get_timescale_connection
router = APIRouter() router = APIRouter()
TIME_WITH_TZ_DESC = "ISO 8601 / RFC 3339 时间,必须显式带时区;可直接传 UTC+8,服务端会先转换为 UTC 再处理。"
TIME_RANGE_START_DESC = f"时间范围开始时间。{TIME_WITH_TZ_DESC}"
TIME_RANGE_END_DESC = f"时间范围结束时间。{TIME_WITH_TZ_DESC}"
@router.post("/realtime/links/batch", status_code=201)
@router.post("/realtime/links/batch", status_code=201, summary="批量插入实时管道数据")
async def insert_realtime_links( async def insert_realtime_links(
data: List[dict], conn: AsyncConnection = Depends(get_timescale_connection) data: List[dict] = Body(..., description="管道数据列表,每项包含管道ID、时间戳等信息"),
conn: AsyncConnection = Depends(get_timescale_connection)
): ):
"""
批量插入实时管道数据
将管道的实时监测数据批量插入时间序列数据库。
Args:
data: 管道数据列表
Returns:
插入成功的记录数
"""
await RealtimeRepository.insert_links_batch(conn, data) await RealtimeRepository.insert_links_batch(conn, data)
return {"message": f"Inserted {len(data)} records"} return {"message": f"Inserted {len(data)} records"}
@router.get("/realtime/links") @router.get(
"/realtime/links",
summary="查询实时管道数据",
description="按时间范围查询实时管道数据。start_time 和 end_time 必须显式带时区;允许传 UTC+8,服务端会先归一化为 UTC 再执行查询。",
)
async def get_realtime_links( async def get_realtime_links(
start_time: datetime, start_time: datetime = Query(..., description=TIME_RANGE_START_DESC),
end_time: datetime, end_time: datetime = Query(..., description=TIME_RANGE_END_DESC),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
查询指定时间范围内的实时管道数据
根据时间范围查询所有实时管道的监测值。传入时间必须显式包含时区,
可以直接使用 UTC+8,服务端会先统一转换为 UTC 再参与数据库查询。
Args:
start_time: 查询开始时间
end_time: 查询结束时间
Returns:
实时管道数据列表
"""
return await RealtimeRepository.get_links_by_time_range(conn, start_time, end_time) return await RealtimeRepository.get_links_by_time_range(conn, start_time, end_time)
@router.delete("/realtime/links") @router.delete(
"/realtime/links",
summary="删除实时管道数据",
description="按时间范围删除实时管道数据。start_time 和 end_time 必须显式带时区;允许传 UTC+8,服务端按请求中的绝对时间删除对应 UTC 数据。",
)
async def delete_realtime_links( async def delete_realtime_links(
start_time: datetime, start_time: datetime = Query(..., description=TIME_RANGE_START_DESC),
end_time: datetime, end_time: datetime = Query(..., description=TIME_RANGE_END_DESC),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
删除指定时间范围内的实时管道数据
删除在指定时间范围内的所有实时管道监测数据。
Args:
start_time: 删除开始时间
end_time: 删除结束时间
Returns:
删除结果信息
"""
await RealtimeRepository.delete_links_by_time_range(conn, start_time, end_time) await RealtimeRepository.delete_links_by_time_range(conn, start_time, end_time)
return {"message": "Deleted successfully"} return {"message": "Deleted successfully"}
@router.patch("/realtime/links/{link_id}/field") @router.patch("/realtime/links/{link_id}/field", summary="更新实时管道字段")
async def update_realtime_link_field( async def update_realtime_link_field(
link_id: str, link_id: str = Path(..., description="管道ID"),
time: datetime, time: datetime = Query(..., description=f"要更新记录的时间戳。{TIME_WITH_TZ_DESC}"),
field: str, field: str = Query(..., description="要更新的字段名称"),
value: float, value: float = Query(..., description="更新的字段值"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
更新指定管道的字段值
更新实时管道在特定时间的某个字段数据。
Args:
link_id: 管道ID
time: 数据时间戳
field: 字段名称
value: 字段新值
Returns:
更新结果信息
Raises:
HTTPException: 当字段不存在或更新失败时返回400错误
"""
try: try:
await RealtimeRepository.update_link_field(conn, time, link_id, field, value) await RealtimeRepository.update_link_field(conn, time, link_id, field, value)
return {"message": "Updated successfully"} return {"message": "Updated successfully"}
@@ -51,55 +117,133 @@ async def update_realtime_link_field(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.post("/realtime/nodes/batch", status_code=201) @router.post("/realtime/nodes/batch", status_code=201, summary="批量插入实时节点数据")
async def insert_realtime_nodes( async def insert_realtime_nodes(
data: List[dict], conn: AsyncConnection = Depends(get_timescale_connection) data: List[dict] = Body(..., description="节点数据列表,每项包含节点ID、时间戳等信息"),
conn: AsyncConnection = Depends(get_timescale_connection)
): ):
"""
批量插入实时节点数据
将节点的实时监测数据批量插入时间序列数据库。
Args:
data: 节点数据列表
Returns:
插入成功的记录数
"""
await RealtimeRepository.insert_nodes_batch(conn, data) await RealtimeRepository.insert_nodes_batch(conn, data)
return {"message": f"Inserted {len(data)} records"} return {"message": f"Inserted {len(data)} records"}
@router.get("/realtime/nodes") @router.get(
"/realtime/nodes",
summary="查询实时节点数据",
description="按时间范围查询实时节点数据。start_time 和 end_time 必须显式带时区;允许传 UTC+8,服务端会先归一化为 UTC 再执行查询。",
)
async def get_realtime_nodes( async def get_realtime_nodes(
start_time: datetime, start_time: datetime = Query(..., description=TIME_RANGE_START_DESC),
end_time: datetime, end_time: datetime = Query(..., description=TIME_RANGE_END_DESC),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
查询指定时间范围内的实时节点数据
根据时间范围查询所有实时节点的监测值。传入时间必须显式包含时区,
可以直接使用 UTC+8,服务端会先统一转换为 UTC 再参与数据库查询。
Args:
start_time: 查询开始时间
end_time: 查询结束时间
Returns:
实时节点数据列表
"""
return await RealtimeRepository.get_nodes_by_time_range(conn, start_time, end_time) return await RealtimeRepository.get_nodes_by_time_range(conn, start_time, end_time)
@router.delete("/realtime/nodes") @router.delete(
"/realtime/nodes",
summary="删除实时节点数据",
description="按时间范围删除实时节点数据。start_time 和 end_time 必须显式带时区;允许传 UTC+8,服务端按请求中的绝对时间删除对应 UTC 数据。",
)
async def delete_realtime_nodes( async def delete_realtime_nodes(
start_time: datetime, start_time: datetime = Query(..., description=TIME_RANGE_START_DESC),
end_time: datetime, end_time: datetime = Query(..., description=TIME_RANGE_END_DESC),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
删除指定时间范围内的实时节点数据
删除在指定时间范围内的所有实时节点监测数据。
Args:
start_time: 删除开始时间
end_time: 删除结束时间
Returns:
删除结果信息
"""
await RealtimeRepository.delete_nodes_by_time_range(conn, start_time, end_time) await RealtimeRepository.delete_nodes_by_time_range(conn, start_time, end_time)
return {"message": "Deleted successfully"} return {"message": "Deleted successfully"}
@router.post("/realtime/simulation/store", status_code=201)
@router.post("/realtime/simulation/store", status_code=201, summary="存储实时模拟结果")
async def store_realtime_simulation_result( async def store_realtime_simulation_result(
node_result_list: List[dict], node_result_list: List[dict] = Body(..., description="节点模拟结果列表"),
link_result_list: List[dict], link_result_list: List[dict] = Body(..., description="管道模拟结果列表"),
result_start_time: str, result_start_time: str = Query(..., description=f"模拟结果开始时间。{TIME_WITH_TZ_DESC}"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""Store realtime simulation results to TimescaleDB""" """
存储实时模拟结果到时间序列数据库
将节点和管道的实时模拟计算结果批量存储到TimescaleDB数据库。
Args:
node_result_list: 节点模拟结果列表
link_result_list: 管道模拟结果列表
result_start_time: 模拟结果对应的起始时间
Returns:
存储结果信息
"""
await RealtimeRepository.store_realtime_simulation_result( await RealtimeRepository.store_realtime_simulation_result(
conn, node_result_list, link_result_list, result_start_time conn, node_result_list, link_result_list, result_start_time
) )
return {"message": "Simulation results stored successfully"} return {"message": "Simulation results stored successfully"}
@router.get("/realtime/query/by-time-property") @router.get(
"/realtime/query/by-time-property",
summary="按时间和属性查询实时数据",
description="查询指定时间点的实时属性值。query_time 必须显式带时区;允许传 UTC+8,服务端会先归一化为 UTC 再执行查询。",
)
async def query_realtime_records_by_time_property( async def query_realtime_records_by_time_property(
query_time: str, query_time: str = Query(..., description=f"查询时间。{TIME_WITH_TZ_DESC}"),
type: str, type: str = Query(..., description="数据类型,pipe(管道)或 junction(节点)"),
property: str, property: str = Query(..., description="要查询的属性名称"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""Query all realtime records by time and property""" """
按指定时间和属性查询所有实时监测数据
查询在特定时间点,所有指定类型元素的特定属性值。
Args:
query_time: 查询时间
type: 元素类型(pipe或junction
property: 属性名称
Returns:
查询结果列表
Raises:
HTTPException: 当查询参数无效时返回400错误
"""
try: try:
results = await RealtimeRepository.query_all_record_by_time_property( results = await RealtimeRepository.query_all_record_by_time_property(
conn, query_time, type, property conn, query_time, type, property
@@ -109,14 +253,33 @@ async def query_realtime_records_by_time_property(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.get("/realtime/query/by-id-time") @router.get(
"/realtime/query/by-id-time",
summary="按ID和时间查询实时模拟数据",
description="查询指定元素在某一时间点的实时模拟结果。query_time 必须显式带时区;允许传 UTC+8,服务端会先归一化为 UTC 再执行查询。",
)
async def query_realtime_simulation_by_id_time( async def query_realtime_simulation_by_id_time(
id: str, id: str = Query(..., description="元素ID(管道ID或节点ID"),
type: str, type: str = Query(..., description="元素类型,pipe(管道)或 junction(节点)"),
query_time: str, query_time: str = Query(..., description=f"查询时间。{TIME_WITH_TZ_DESC}"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""Query realtime simulation results by id and time""" """
按指定ID和时间查询实时模拟结果
查询特定元素在某一时间点的实时模拟数据。
Args:
id: 元素ID
type: 元素类型(pipe或junction
query_time: 查询时间
Returns:
模拟结果数据
Raises:
HTTPException: 当查询参数无效时返回400错误
"""
try: try:
results = await RealtimeRepository.query_simulation_result_by_id_time( results = await RealtimeRepository.query_simulation_result_by_id_time(
conn, id, type, query_time conn, id, type, query_time
+99 -21
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Query, Path, Body
from typing import List from typing import List
from datetime import datetime from datetime import datetime
from psycopg import AsyncConnection from psycopg import AsyncConnection
@@ -9,21 +9,48 @@ from .dependencies import get_timescale_connection
router = APIRouter() router = APIRouter()
@router.post("/scada/batch", status_code=201) @router.post("/scada/batch", status_code=201, summary="批量插入SCADA监测数据")
async def insert_scada_data( async def insert_scada_data(
data: List[dict], conn: AsyncConnection = Depends(get_timescale_connection) data: List[dict] = Body(..., description="SCADA设备监测数据列表"),
conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
批量插入SCADA监测数据
将多个设备的实时监测数据批量插入时间序列数据库。
Args:
data: SCADA设备监测数据列表,每项包含device_id、时间戳和监测值等信息
Returns:
插入成功的记录数
"""
await ScadaRepository.insert_scada_batch(conn, data) await ScadaRepository.insert_scada_batch(conn, data)
return {"message": f"Inserted {len(data)} records"} return {"message": f"Inserted {len(data)} records"}
@router.get("/scada/by-ids-time-range") @router.get("/scada/by-ids-time-range", summary="按设备ID和时间范围查询SCADA数据")
async def get_scada_by_ids_time_range( async def get_scada_by_ids_time_range(
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
device_ids: str, device_ids: str = Query(
..., description="设备ID列表,逗号分隔,如 'device1,device2,device3'"
),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
按设备ID和时间范围查询SCADA监测数据
查询多个设备在指定时间范围内的所有监测数据。
Args:
start_time: 查询开始时间
end_time: 查询结束时间
device_ids: 设备ID列表,用逗号分隔
Returns:
SCADA监测数据列表
"""
device_ids_list = ( device_ids_list = (
[id.strip() for id in device_ids.split(",") if id.strip()] if device_ids else [] [id.strip() for id in device_ids.split(",") if id.strip()] if device_ids else []
) )
@@ -32,14 +59,35 @@ async def get_scada_by_ids_time_range(
) )
@router.get("/scada/by-ids-field-time-range") @router.get(
"/scada/by-ids-field-time-range", summary="按设备ID、字段和时间范围查询SCADA数据"
)
async def get_scada_field_by_ids_time_range( async def get_scada_field_by_ids_time_range(
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
field: str, field: str = Query(..., description="要查询的字段名称"),
device_ids: str, device_ids: str = Query(
..., description="设备ID列表,逗号分隔,如 'device1,device2,device3'"
),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
按设备ID、字段和时间范围查询特定SCADA数据
查询多个设备在指定时间范围内的特定字段监测数据。
Args:
start_time: 查询开始时间
end_time: 查询结束时间
field: 字段名称
device_ids: 设备ID列表,用逗号分隔
Returns:
SCADA字段数据列表
Raises:
HTTPException: 当字段不存在或查询参数无效时返回400错误
"""
try: try:
device_ids_list = ( device_ids_list = (
[id.strip() for id in device_ids.split(",") if id.strip()] [id.strip() for id in device_ids.split(",") if id.strip()]
@@ -53,14 +101,31 @@ async def get_scada_field_by_ids_time_range(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.patch("/scada/{device_id}/field") @router.patch("/scada/{device_id}/field", summary="更新SCADA设备字段")
async def update_scada_field( async def update_scada_field(
device_id: str, device_id: str = Path(..., description="设备ID"),
time: datetime, time: datetime = Query(..., description="更新数据的时间戳"),
field: str, field: str = Query(..., description="要更新的字段名称"),
value: float, value: float = Query(..., description="更新的字段值"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
更新指定设备的字段值
更新SCADA设备在特定时间的某个字段监测数据。
Args:
device_id: 设备ID
time: 数据时间戳
field: 字段名称
value: 字段新值
Returns:
更新结果信息
Raises:
HTTPException: 当字段不存在或更新失败时返回400错误
"""
try: try:
await ScadaRepository.update_scada_field(conn, time, device_id, field, value) await ScadaRepository.update_scada_field(conn, time, device_id, field, value)
return {"message": "Updated successfully"} return {"message": "Updated successfully"}
@@ -68,13 +133,26 @@ async def update_scada_field(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.delete("/scada/by-id-time-range") @router.delete("/scada/by-id-time-range", summary="按设备ID和时间范围删除SCADA数据")
async def delete_scada_data( async def delete_scada_data(
device_id: str, device_id: str = Query(..., description="设备ID"),
start_time: datetime, start_time: datetime = Query(..., description="删除开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="删除结束时间"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
删除指定设备和时间范围内的SCADA数据
删除在指定时间范围内的特定设备监测数据。
Args:
device_id: 设备ID
start_time: 删除开始时间
end_time: 删除结束时间
Returns:
删除结果信息
"""
await ScadaRepository.delete_scada_by_id_time_range( await ScadaRepository.delete_scada_by_id_time_range(
conn, device_id, start_time, end_time conn, device_id, start_time, end_time
) )
+261 -69
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException, Query, Path, Body
from typing import List from typing import List
from datetime import datetime from datetime import datetime
from psycopg import AsyncConnection from psycopg import AsyncConnection
@@ -9,37 +9,82 @@ from .dependencies import get_timescale_connection
router = APIRouter() router = APIRouter()
@router.post("/scheme/links/batch", status_code=201) @router.post("/scheme/links/batch", status_code=201, summary="批量插入方案管道数据")
async def insert_scheme_links( async def insert_scheme_links(
data: List[dict], conn: AsyncConnection = Depends(get_timescale_connection) data: List[dict] = Body(..., description="方案管道数据列表"),
conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
批量插入方案管道数据
将特定方案的管道模拟数据批量插入时间序列数据库。
Args:
data: 方案管道数据列表
Returns:
插入成功的记录数
"""
await SchemeRepository.insert_links_batch(conn, data) await SchemeRepository.insert_links_batch(conn, data)
return {"message": f"Inserted {len(data)} records"} return {"message": f"Inserted {len(data)} records"}
@router.get("/scheme/links") @router.get("/scheme/links", summary="查询方案管道数据")
async def get_scheme_links( async def get_scheme_links(
scheme_type: str, scheme_type: str = Query(..., description="方案类型"),
scheme_name: str, scheme_name: str = Query(..., description="方案名称"),
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
查询指定方案和时间范围内的管道数据
根据方案和时间范围查询管道的模拟值。
Args:
scheme_type: 方案类型
scheme_name: 方案名称
start_time: 查询开始时间
end_time: 查询结束时间
Returns:
方案管道数据列表
"""
return await SchemeRepository.get_links_by_scheme_and_time_range( return await SchemeRepository.get_links_by_scheme_and_time_range(
conn, scheme_type, scheme_name, start_time, end_time conn, scheme_type, scheme_name, start_time, end_time
) )
@router.get("/scheme/links/{link_id}/field") @router.get("/scheme/links/{link_id}/field", summary="查询方案管道字段数据")
async def get_scheme_link_field( async def get_scheme_link_field(
scheme_type: str, link_id: str = Path(..., description="管道ID"),
scheme_name: str, scheme_type: str = Query(..., description="方案类型"),
link_id: str, scheme_name: str = Query(..., description="方案名称"),
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
field: str, field: str = Query(..., description="要查询的字段名称"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
查询指定方案管道的特定字段数据
查询特定方案中指定管道在时间范围内的特定字段值。
Args:
link_id: 管道ID
scheme_type: 方案类型
scheme_name: 方案名称
start_time: 查询开始时间
end_time: 查询结束时间
field: 字段名称
Returns:
字段数据列表
Raises:
HTTPException: 当查询参数无效时返回400错误
"""
try: try:
return await SchemeRepository.get_link_field_by_scheme_and_time_range( return await SchemeRepository.get_link_field_by_scheme_and_time_range(
conn, scheme_type, scheme_name, start_time, end_time, link_id, field conn, scheme_type, scheme_name, start_time, end_time, link_id, field
@@ -48,16 +93,35 @@ async def get_scheme_link_field(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.patch("/scheme/links/{link_id}/field") @router.patch("/scheme/links/{link_id}/field", summary="更新方案管道字段")
async def update_scheme_link_field( async def update_scheme_link_field(
scheme_type: str, link_id: str = Path(..., description="管道ID"),
scheme_name: str, scheme_type: str = Query(..., description="方案类型"),
link_id: str, scheme_name: str = Query(..., description="方案名称"),
time: datetime, time: datetime = Query(..., description="更新数据的时间戳"),
field: str, field: str = Query(..., description="要更新的字段名称"),
value: float, value: float = Query(..., description="更新的字段值"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
更新指定方案管道的字段值
更新特定方案中指定管道在某个时间的字段数据。
Args:
link_id: 管道ID
scheme_type: 方案类型
scheme_name: 方案名称
time: 数据时间戳
field: 字段名称
value: 字段新值
Returns:
更新结果信息
Raises:
HTTPException: 当字段不存在或更新失败时返回400错误
"""
try: try:
await SchemeRepository.update_link_field( await SchemeRepository.update_link_field(
conn, time, scheme_type, scheme_name, link_id, field, value conn, time, scheme_type, scheme_name, link_id, field, value
@@ -67,38 +131,83 @@ async def update_scheme_link_field(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.delete("/scheme/links") @router.delete("/scheme/links", summary="删除方案管道数据")
async def delete_scheme_links( async def delete_scheme_links(
scheme_type: str, scheme_type: str = Query(..., description="方案类型"),
scheme_name: str, scheme_name: str = Query(..., description="方案名称"),
start_time: datetime, start_time: datetime = Query(..., description="删除开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="删除结束时间"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
删除指定方案和时间范围内的管道数据
删除在指定方案和时间范围内的所有管道模拟数据。
Args:
scheme_type: 方案类型
scheme_name: 方案名称
start_time: 删除开始时间
end_time: 删除结束时间
Returns:
删除结果信息
"""
await SchemeRepository.delete_links_by_scheme_and_time_range( await SchemeRepository.delete_links_by_scheme_and_time_range(
conn, scheme_type, scheme_name, start_time, end_time conn, scheme_type, scheme_name, start_time, end_time
) )
return {"message": "Deleted successfully"} return {"message": "Deleted successfully"}
@router.post("/scheme/nodes/batch", status_code=201) @router.post("/scheme/nodes/batch", status_code=201, summary="批量插入方案节点数据")
async def insert_scheme_nodes( async def insert_scheme_nodes(
data: List[dict], conn: AsyncConnection = Depends(get_timescale_connection) data: List[dict] = Body(..., description="方案节点数据列表"),
conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
批量插入方案节点数据
将特定方案的节点模拟数据批量插入时间序列数据库。
Args:
data: 方案节点数据列表
Returns:
插入成功的记录数
"""
await SchemeRepository.insert_nodes_batch(conn, data) await SchemeRepository.insert_nodes_batch(conn, data)
return {"message": f"Inserted {len(data)} records"} return {"message": f"Inserted {len(data)} records"}
@router.get("/scheme/nodes/{node_id}/field") @router.get("/scheme/nodes/{node_id}/field", summary="查询方案节点字段数据")
async def get_scheme_node_field( async def get_scheme_node_field(
scheme_type: str, node_id: str = Path(..., description="节点ID"),
scheme_name: str, scheme_type: str = Query(..., description="方案类型"),
node_id: str, scheme_name: str = Query(..., description="方案名称"),
start_time: datetime, start_time: datetime = Query(..., description="查询开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="查询结束时间"),
field: str, field: str = Query(..., description="要查询的字段名称"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
查询指定方案节点的特定字段数据
查询特定方案中指定节点在时间范围内的特定字段值。
Args:
node_id: 节点ID
scheme_type: 方案类型
scheme_name: 方案名称
start_time: 查询开始时间
end_time: 查询结束时间
field: 字段名称
Returns:
字段数据列表
Raises:
HTTPException: 当查询参数无效时返回400错误
"""
try: try:
return await SchemeRepository.get_node_field_by_scheme_and_time_range( return await SchemeRepository.get_node_field_by_scheme_and_time_range(
conn, scheme_type, scheme_name, start_time, end_time, node_id, field conn, scheme_type, scheme_name, start_time, end_time, node_id, field
@@ -107,16 +216,35 @@ async def get_scheme_node_field(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.patch("/scheme/nodes/{node_id}/field") @router.patch("/scheme/nodes/{node_id}/field", summary="更新方案节点字段")
async def update_scheme_node_field( async def update_scheme_node_field(
scheme_type: str, node_id: str = Path(..., description="节点ID"),
scheme_name: str, scheme_type: str = Query(..., description="方案类型"),
node_id: str, scheme_name: str = Query(..., description="方案名称"),
time: datetime, time: datetime = Query(..., description="更新数据的时间戳"),
field: str, field: str = Query(..., description="要更新的字段名称"),
value: float, value: float = Query(..., description="更新的字段值"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
更新指定方案节点的字段值
更新特定方案中指定节点在某个时间的字段数据。
Args:
node_id: 节点ID
scheme_type: 方案类型
scheme_name: 方案名称
time: 数据时间戳
field: 字段名称
value: 字段新值
Returns:
更新结果信息
Raises:
HTTPException: 当字段不存在或更新失败时返回400错误
"""
try: try:
await SchemeRepository.update_node_field( await SchemeRepository.update_node_field(
conn, time, scheme_type, scheme_name, node_id, field, value conn, time, scheme_type, scheme_name, node_id, field, value
@@ -126,30 +254,58 @@ async def update_scheme_node_field(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.delete("/scheme/nodes") @router.delete("/scheme/nodes", summary="删除方案节点数据")
async def delete_scheme_nodes( async def delete_scheme_nodes(
scheme_type: str, scheme_type: str = Query(..., description="方案类型"),
scheme_name: str, scheme_name: str = Query(..., description="方案名称"),
start_time: datetime, start_time: datetime = Query(..., description="删除开始时间"),
end_time: datetime, end_time: datetime = Query(..., description="删除结束时间"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""
删除指定方案和时间范围内的节点数据
删除在指定方案和时间范围内的所有节点模拟数据。
Args:
scheme_type: 方案类型
scheme_name: 方案名称
start_time: 删除开始时间
end_time: 删除结束时间
Returns:
删除结果信息
"""
await SchemeRepository.delete_nodes_by_scheme_and_time_range( await SchemeRepository.delete_nodes_by_scheme_and_time_range(
conn, scheme_type, scheme_name, start_time, end_time conn, scheme_type, scheme_name, start_time, end_time
) )
return {"message": "Deleted successfully"} return {"message": "Deleted successfully"}
@router.post("/scheme/simulation/store", status_code=201) @router.post("/scheme/simulation/store", status_code=201, summary="存储方案模拟结果")
async def store_scheme_simulation_result( async def store_scheme_simulation_result(
scheme_type: str, scheme_type: str = Query(..., description="方案类型"),
scheme_name: str, scheme_name: str = Query(..., description="方案名称"),
node_result_list: List[dict], node_result_list: List[dict] = Body(..., description="节点模拟结果列表"),
link_result_list: List[dict], link_result_list: List[dict] = Body(..., description="管道模拟结果列表"),
result_start_time: str, result_start_time: str = Query(..., description="模拟结果开始时间"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""Store scheme simulation results to TimescaleDB""" """
存储方案模拟结果到时间序列数据库
将特定方案的节点和管道模拟计算结果批量存储到TimescaleDB数据库。
Args:
scheme_type: 方案类型
scheme_name: 方案名称
node_result_list: 节点模拟结果列表
link_result_list: 管道模拟结果列表
result_start_time: 模拟结果对应的起始时间
Returns:
存储结果信息
"""
await SchemeRepository.store_scheme_simulation_result( await SchemeRepository.store_scheme_simulation_result(
conn, conn,
scheme_type, scheme_type,
@@ -161,16 +317,35 @@ async def store_scheme_simulation_result(
return {"message": "Scheme simulation results stored successfully"} return {"message": "Scheme simulation results stored successfully"}
@router.get("/scheme/query/by-scheme-time-property") @router.get(
"/scheme/query/by-scheme-time-property", summary="按方案、时间和属性查询数据"
)
async def query_scheme_records_by_scheme_time_property( async def query_scheme_records_by_scheme_time_property(
scheme_type: str, scheme_type: str = Query(..., description="方案类型"),
scheme_name: str, scheme_name: str = Query(..., description="方案名称"),
query_time: str, query_time: str = Query(..., description="查询时间"),
type: str, type: str = Query(..., description="元素类型,pipe(管道)或 junction(节点)"),
property: str, property: str = Query(..., description="要查询的属性名称"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""Query all scheme records by scheme, time and property""" """
按指定方案、时间和属性查询所有方案数据
查询在特定方案和时间点,所有指定类型元素的特定属性值。
Args:
scheme_type: 方案类型
scheme_name: 方案名称
query_time: 查询时间
type: 元素类型(pipe或junction
property: 属性名称
Returns:
查询结果列表
Raises:
HTTPException: 当查询参数无效时返回400错误
"""
try: try:
results = await SchemeRepository.query_all_record_by_scheme_time_property( results = await SchemeRepository.query_all_record_by_scheme_time_property(
conn, scheme_type, scheme_name, query_time, type, property conn, scheme_type, scheme_name, query_time, type, property
@@ -180,16 +355,33 @@ async def query_scheme_records_by_scheme_time_property(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@router.get("/scheme/query/by-id-time") @router.get("/scheme/query/by-id-time", summary="按ID和时间查询方案模拟数据")
async def query_scheme_simulation_by_id_time( async def query_scheme_simulation_by_id_time(
scheme_type: str, scheme_type: str = Query(..., description="方案类型"),
scheme_name: str, scheme_name: str = Query(..., description="方案名称"),
id: str, id: str = Query(..., description="元素ID(管道ID或节点ID"),
type: str, type: str = Query(..., description="元素类型,pipe(管道)或 junction(节点)"),
query_time: str, query_time: str = Query(..., description="查询时间"),
conn: AsyncConnection = Depends(get_timescale_connection), conn: AsyncConnection = Depends(get_timescale_connection),
): ):
"""Query scheme simulation results by id and time""" """
按指定ID和时间查询方案模拟结果
查询特定方案中的元素在某一时间点的模拟数据。
Args:
scheme_type: 方案类型
scheme_name: 方案名称
id: 元素ID
type: 元素类型(pipe或junction
query_time: 查询时间
Returns:
模拟结果数据
Raises:
HTTPException: 当查询参数无效时返回400错误
"""
try: try:
result = await SchemeRepository.query_scheme_simulation_result_by_id_time( result = await SchemeRepository.query_scheme_simulation_result_by_id_time(
conn, scheme_type, scheme_name, id, type, query_time conn, scheme_type, scheme_name, id, type, query_time
-180
View File
@@ -1,180 +0,0 @@
"""
用户管理 API 接口
演示权限控制的使用
"""
from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from app.domain.schemas.user import UserResponse, UserUpdate, UserCreate
from app.domain.models.role import UserRole
from app.domain.schemas.user import UserInDB
from app.infra.repositories.user_repository import UserRepository
from app.auth.dependencies import get_user_repository, get_current_active_user
from app.auth.permissions import get_current_admin, require_role, check_resource_owner
router = APIRouter()
@router.get("/", response_model=List[UserResponse])
async def list_users(
skip: int = 0,
limit: int = 100,
current_user: UserInDB = Depends(require_role(UserRole.ADMIN)),
user_repo: UserRepository = Depends(get_user_repository)
) -> List[UserResponse]:
"""
获取用户列表(仅管理员)
"""
users = await user_repo.get_all_users(skip=skip, limit=limit)
return [UserResponse.model_validate(user) for user in users]
@router.get("/{user_id}", response_model=UserResponse)
async def get_user(
user_id: int,
current_user: UserInDB = Depends(get_current_active_user),
user_repo: UserRepository = Depends(get_user_repository)
) -> UserResponse:
"""
获取用户详情
管理员可查看所有用户,普通用户只能查看自己
"""
# 检查权限
if not check_resource_owner(user_id, current_user):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to view this user"
)
user = await user_repo.get_user_by_id(user_id)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return UserResponse.model_validate(user)
@router.put("/{user_id}", response_model=UserResponse)
async def update_user(
user_id: int,
user_update: UserUpdate,
current_user: UserInDB = Depends(get_current_active_user),
user_repo: UserRepository = Depends(get_user_repository)
) -> UserResponse:
"""
更新用户信息
管理员可更新所有用户,普通用户只能更新自己(且不能修改角色)
"""
# 检查用户是否存在
target_user = await user_repo.get_user_by_id(user_id)
if not target_user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
# 权限检查
is_owner = current_user.id == user_id
is_admin = UserRole(current_user.role).has_permission(UserRole.ADMIN)
if not is_owner and not is_admin:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to update this user"
)
# 非管理员不能修改角色和激活状态
if not is_admin:
if user_update.role is not None:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Only admins can change user roles"
)
if user_update.is_active is not None:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Only admins can change user active status"
)
# 更新用户
updated_user = await user_repo.update_user(user_id, user_update)
if not updated_user:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update user"
)
return UserResponse.model_validate(updated_user)
@router.delete("/{user_id}")
async def delete_user(
user_id: int,
current_user: UserInDB = Depends(get_current_admin),
user_repo: UserRepository = Depends(get_user_repository)
) -> dict:
"""
删除用户(仅管理员)
"""
# 不能删除自己
if current_user.id == user_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="You cannot delete your own account"
)
success = await user_repo.delete_user(user_id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return {"message": "User deleted successfully"}
@router.post("/{user_id}/activate")
async def activate_user(
user_id: int,
current_user: UserInDB = Depends(get_current_admin),
user_repo: UserRepository = Depends(get_user_repository)
) -> UserResponse:
"""
激活用户(仅管理员)
"""
user_update = UserUpdate(is_active=True)
updated_user = await user_repo.update_user(user_id, user_update)
if not updated_user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return UserResponse.model_validate(updated_user)
@router.post("/{user_id}/deactivate")
async def deactivate_user(
user_id: int,
current_user: UserInDB = Depends(get_current_admin),
user_repo: UserRepository = Depends(get_user_repository)
) -> UserResponse:
"""
停用用户(仅管理员)
"""
# 不能停用自己
if current_user.id == user_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="You cannot deactivate your own account"
)
user_update = UserUpdate(is_active=False)
updated_user = await user_repo.update_user(user_id, user_update)
if not updated_user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return UserResponse.model_validate(updated_user)
+22 -7
View File
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request, Query
from typing import Any, List, Dict, Union from typing import Any, List, Dict, Union
from app.services.tjnetwork import Any, get_all_users, get_user, get_user_schema from app.services.tjnetwork import Any, get_all_users, get_user, get_user_schema
@@ -8,14 +8,29 @@ router = APIRouter()
# user 39 # user 39
########################################################### ###########################################################
@router.get("/getuserschema/") @router.get("/getuserschema/", summary="获取用户模式", description="获取指定网络的用户模式定义")
async def fastapi_get_user_schema(network: str) -> dict[str, dict[Any, Any]]: async def fastapi_get_user_schema(network: str = Query(..., description="管网名称(或数据库名称)")) -> dict[str, dict[Any, Any]]:
"""
获取用户模式定义
返回指定网络的用户模式结构定义
"""
return get_user_schema(network) return get_user_schema(network)
@router.get("/getuser/") @router.get("/getuser/", summary="获取单个用户", description="获取指定网络中的单个用户信息")
async def fastapi_get_user(network: str, user_name: str) -> dict[Any, Any]: async def fastapi_get_user(network: str = Query(..., description="管网名称(或数据库名称)"), user_name: str = Query(..., description="用户名")) -> dict[Any, Any]:
"""
获取用户信息
返回指定网络中指定用户名的详细信息
"""
return get_user(network, user_name) return get_user(network, user_name)
@router.get("/getallusers/") @router.get("/getallusers/", summary="获取所有用户", description="获取指定网络的所有用户列表")
async def fastapi_get_all_users(network: str) -> list[dict[Any, Any]]: async def fastapi_get_all_users(network: str = Query(..., description="管网名称(或数据库名称)")) -> list[dict[Any, Any]]:
"""
获取所有用户列表
返回指定网络中所有用户的信息
"""
return get_all_users(network) return get_all_users(network)
+29
View File
@@ -0,0 +1,29 @@
from typing import Any
from fastapi import APIRouter, HTTPException, status
from app.services.web_search import (
BochaSearchAPIError,
BochaSearchConfigError,
WebSearchRequest,
search_bocha_web,
)
router = APIRouter()
@router.post(
"/web-search",
summary="Web Search",
description="调用 Bocha Web Search API 获取实时网页搜索结果",
)
async def web_search(request: WebSearchRequest) -> dict[str, Any]:
try:
return await search_bocha_web(request)
except BochaSearchConfigError as exc:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=str(exc),
) from exc
except BochaSearchAPIError as exc:
raise HTTPException(status_code=exc.status_code, detail=exc.detail) from exc
+11 -7
View File
@@ -1,12 +1,12 @@
from fastapi import APIRouter from fastapi import APIRouter
from app.api.v1.endpoints import ( from app.api.v1.endpoints import (
auth, admin_metadata,
agent_auth,
project, project,
simulation, simulation,
scada, scada,
extension, extension,
snapshots, snapshots,
data_query,
users, users,
schemes, schemes,
misc, misc,
@@ -15,9 +15,10 @@ from app.api.v1.endpoints import (
leakage, leakage,
burst_detection, burst_detection,
burst_location, burst_location,
user_management, # 新增:用户管理
audit, # 新增:审计日志 audit, # 新增:审计日志
meta, meta,
web_search,
geocoding,
) )
from app.api.v1.endpoints.network import ( from app.api.v1.endpoints.network import (
general, general,
@@ -52,8 +53,10 @@ from app.api.v1.endpoints.timeseries import (
api_router = APIRouter() api_router = APIRouter()
# Core Services # Core Services
api_router.include_router(auth.router, prefix="/auth", tags=["Auth"]) api_router.include_router(agent_auth.router, tags=["Agent Auth"])
api_router.include_router(user_management.router, prefix="/users", tags=["User Management"]) # 新增 api_router.include_router(
admin_metadata.router, prefix="/admin", tags=["Metadata Admin"]
)
api_router.include_router(audit.router, prefix="/audit", tags=["Audit Logs"]) # 新增 api_router.include_router(audit.router, prefix="/audit", tags=["Audit Logs"]) # 新增
api_router.include_router(meta.router, tags=["Metadata"]) api_router.include_router(meta.router, tags=["Metadata"])
api_router.include_router(project.router, tags=["Project"]) api_router.include_router(project.router, tags=["Project"])
@@ -83,14 +86,15 @@ api_router.include_router(visuals.router, tags=["Visuals"])
# Simulation & Data # Simulation & Data
api_router.include_router(simulation.router, tags=["Simulation Control"]) api_router.include_router(simulation.router, tags=["Simulation Control"])
api_router.include_router(data_query.router, tags=["Data Query & InfluxDB"]) api_router.include_router(scada.router)
api_router.include_router(scada.router, tags=["SCADA"])
api_router.include_router(snapshots.router, tags=["Snapshots"]) api_router.include_router(snapshots.router, tags=["Snapshots"])
api_router.include_router(users.router, tags=["Users"]) api_router.include_router(users.router, tags=["Users"])
api_router.include_router(schemes.router, tags=["Schemes"]) api_router.include_router(schemes.router, tags=["Schemes"])
api_router.include_router(misc.router, tags=["Misc"]) api_router.include_router(misc.router, tags=["Misc"])
api_router.include_router(risk.router, tags=["Risk"]) api_router.include_router(risk.router, tags=["Risk"])
api_router.include_router(cache.router, tags=["Cache"]) api_router.include_router(cache.router, tags=["Cache"])
api_router.include_router(web_search.router, tags=["Web Search"])
api_router.include_router(geocoding.router, tags=["Geocoding"])
api_router.include_router(leakage.router, prefix="/leakage", tags=["Leakage"]) api_router.include_router(leakage.router, prefix="/leakage", tags=["Leakage"])
api_router.include_router( api_router.include_router(
burst_detection.router, prefix="/burst-detection", tags=["Burst Detection"] burst_detection.router, prefix="/burst-detection", tags=["Burst Detection"]
-100
View File
@@ -1,100 +0,0 @@
from typing import Annotated, Optional
from fastapi import Depends, HTTPException, status, Request
from fastapi.security import OAuth2PasswordBearer
from jose import jwt, JWTError
from app.core.config import settings
from app.domain.schemas.user import UserInDB, TokenPayload
from app.infra.repositories.user_repository import UserRepository
from app.infra.db.postgresql.database import Database
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/auth/login")
# 数据库依赖
async def get_db(request: Request) -> Database:
"""
获取数据库实例
从 FastAPI app.state 中获取在启动时初始化的数据库连接
"""
if not hasattr(request.app.state, "db"):
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Database not initialized",
)
return request.app.state.db
async def get_user_repository(db: Database = Depends(get_db)) -> UserRepository:
"""获取用户仓储实例"""
return UserRepository(db)
async def get_current_user(
token: str = Depends(oauth2_scheme),
user_repo: UserRepository = Depends(get_user_repository),
) -> UserInDB:
"""
获取当前登录用户
从 JWT Token 中解析用户信息,并从数据库验证
"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
)
username: str = payload.get("sub")
token_type: str = payload.get("type", "access")
if username is None:
raise credentials_exception
if token_type != "access":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token type. Access token required.",
headers={"WWW-Authenticate": "Bearer"},
)
except JWTError:
raise credentials_exception
# 从数据库获取用户
user = await user_repo.get_user_by_username(username)
if user is None:
raise credentials_exception
return user
async def get_current_active_user(
current_user: UserInDB = Depends(get_current_user),
) -> UserInDB:
"""
获取当前活跃用户(必须是激活状态)
"""
if not current_user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user"
)
return current_user
async def get_current_superuser(
current_user: UserInDB = Depends(get_current_user),
) -> UserInDB:
"""
获取当前超级管理员用户
"""
if not current_user.is_superuser:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough privileges. Superuser access required.",
)
return current_user
+26 -43
View File
@@ -8,35 +8,41 @@ from jose import JWTError, jwt
from app.core.config import settings from app.core.config import settings
oauth2_optional = OAuth2PasswordBearer( oauth2_optional = OAuth2PasswordBearer(
tokenUrl=f"{settings.API_V1_STR}/auth/login", auto_error=False tokenUrl="keycloak", auto_error=False
) )
# logger = logging.getLogger(__name__) # logger = logging.getLogger(__name__)
async def get_current_keycloak_sub( def _decode_keycloak_token(token: str) -> dict:
if not settings.KEYCLOAK_PUBLIC_KEY:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Keycloak public key is not configured",
)
key = settings.KEYCLOAK_PUBLIC_KEY.replace("\\n", "\n")
return jwt.decode(
token,
key,
algorithms=[settings.KEYCLOAK_ALGORITHM],
audience=settings.KEYCLOAK_AUDIENCE or None,
)
async def get_current_keycloak_payload(
token: str | None = Depends(oauth2_optional), token: str | None = Depends(oauth2_optional),
) -> UUID: ) -> dict:
if not token: if not token:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated", detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
if settings.KEYCLOAK_PUBLIC_KEY:
key = settings.KEYCLOAK_PUBLIC_KEY.replace("\\n", "\n")
algorithms = [settings.KEYCLOAK_ALGORITHM]
else:
key = settings.SECRET_KEY
algorithms = [settings.ALGORITHM]
try: try:
payload = jwt.decode( return _decode_keycloak_token(token)
token,
key,
algorithms=algorithms,
audience=settings.KEYCLOAK_AUDIENCE or None,
)
except JWTError as exc: except JWTError as exc:
# logger.warning("Keycloak token validation failed: %s", exc) # logger.warning("Keycloak token validation failed: %s", exc)
raise HTTPException( raise HTTPException(
@@ -45,6 +51,10 @@ async def get_current_keycloak_sub(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) from exc ) from exc
async def get_current_keycloak_sub(
payload: dict = Depends(get_current_keycloak_payload),
) -> UUID:
sub = payload.get("sub") sub = payload.get("sub")
if not sub: if not sub:
raise HTTPException( raise HTTPException(
@@ -64,35 +74,8 @@ async def get_current_keycloak_sub(
async def get_current_keycloak_username( async def get_current_keycloak_username(
token: str | None = Depends(oauth2_optional), payload: dict = Depends(get_current_keycloak_payload),
) -> str: ) -> str:
if not token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"},
)
if settings.KEYCLOAK_PUBLIC_KEY:
key = settings.KEYCLOAK_PUBLIC_KEY.replace("\\n", "\n")
algorithms = [settings.KEYCLOAK_ALGORITHM]
else:
key = settings.SECRET_KEY
algorithms = [settings.ALGORITHM]
try:
payload = jwt.decode(
token,
key,
algorithms=algorithms,
audience=settings.KEYCLOAK_AUDIENCE or None,
)
except JWTError as exc:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token",
headers={"WWW-Authenticate": "Bearer"},
) from exc
username = payload.get("preferred_username") or payload.get("username") username = payload.get("preferred_username") or payload.get("username")
if not username: if not username:
raise HTTPException( raise HTTPException(
+49 -5
View File
@@ -6,10 +6,9 @@ from fastapi import Depends, HTTPException, status
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.keycloak_dependencies import get_current_keycloak_sub from app.auth.keycloak_dependencies import get_current_keycloak_payload
from app.core.config import settings from app.infra.db.metadb.database import get_metadata_session
from app.infra.db.metadata.database import get_metadata_session from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository
from app.infra.repositories.metadata_repository import MetadataRepository
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -20,10 +19,40 @@ async def get_metadata_repository(
return MetadataRepository(session) return MetadataRepository(session)
def _keycloak_sub_from_payload(payload: dict) -> UUID:
sub = payload.get("sub")
if not sub:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing subject claim",
headers={"WWW-Authenticate": "Bearer"},
)
try:
return UUID(str(sub))
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid subject claim",
headers={"WWW-Authenticate": "Bearer"},
) from exc
def _username_from_payload(payload: dict) -> str | None:
username = payload.get("preferred_username") or payload.get("username")
return str(username) if username else None
def _email_from_payload(payload: dict) -> str | None:
email = payload.get("email")
return str(email) if email else None
async def get_current_metadata_user( async def get_current_metadata_user(
keycloak_sub: UUID = Depends(get_current_keycloak_sub), keycloak_payload: dict = Depends(get_current_keycloak_payload),
metadata_repo: MetadataRepository = Depends(get_metadata_repository), metadata_repo: MetadataRepository = Depends(get_metadata_repository),
): ):
keycloak_sub = _keycloak_sub_from_payload(keycloak_payload)
try: try:
user = await metadata_repo.get_user_by_keycloak_id(keycloak_sub) user = await metadata_repo.get_user_by_keycloak_id(keycloak_sub)
except SQLAlchemyError as exc: except SQLAlchemyError as exc:
@@ -39,6 +68,21 @@ async def get_current_metadata_user(
raise HTTPException( raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user" status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user"
) )
try:
user = await metadata_repo.refresh_user_keycloak_snapshot(
user,
username=_username_from_payload(keycloak_payload),
email=_email_from_payload(keycloak_payload),
)
except SQLAlchemyError as exc:
logger.error(
"Metadata DB error while refreshing current user snapshot",
exc_info=True,
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Metadata database error: {exc}",
) from exc
return user return user
-106
View File
@@ -1,106 +0,0 @@
"""
权限控制依赖项和装饰器
基于角色的访问控制(RBAC
"""
from typing import Callable
from fastapi import Depends, HTTPException, status
from app.domain.models.role import UserRole
from app.domain.schemas.user import UserInDB
from app.auth.dependencies import get_current_active_user
def require_role(required_role: UserRole):
"""
要求特定角色或更高权限
用法:
@router.get("/admin-only")
async def admin_endpoint(user: UserInDB = Depends(require_role(UserRole.ADMIN))):
...
Args:
required_role: 需要的最低角色
Returns:
依赖函数
"""
async def role_checker(
current_user: UserInDB = Depends(get_current_active_user)
) -> UserInDB:
user_role = UserRole(current_user.role)
if not user_role.has_permission(required_role):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"Insufficient permissions. Required role: {required_role.value}, "
f"Your role: {user_role.value}"
)
return current_user
return role_checker
# 预定义的权限检查依赖
require_admin = require_role(UserRole.ADMIN)
require_operator = require_role(UserRole.OPERATOR)
require_user = require_role(UserRole.USER)
def get_current_admin(
current_user: UserInDB = Depends(require_admin)
) -> UserInDB:
"""
获取当前管理员用户
等同于 Depends(require_role(UserRole.ADMIN))
"""
return current_user
def get_current_operator(
current_user: UserInDB = Depends(require_operator)
) -> UserInDB:
"""
获取当前操作员用户(或更高权限)
等同于 Depends(require_role(UserRole.OPERATOR))
"""
return current_user
def check_resource_owner(user_id: int, current_user: UserInDB) -> bool:
"""
检查是否是资源拥有者或管理员
Args:
user_id: 资源拥有者ID
current_user: 当前用户
Returns:
是否有权限
"""
# 管理员可以访问所有资源
if UserRole(current_user.role).has_permission(UserRole.ADMIN):
return True
# 检查是否是资源拥有者
return current_user.id == user_id
def require_owner_or_admin(user_id: int):
"""
要求是资源拥有者或管理员
Args:
user_id: 资源拥有者ID
Returns:
依赖函数
"""
async def owner_or_admin_checker(
current_user: UserInDB = Depends(get_current_active_user)
) -> UserInDB:
if not check_resource_owner(user_id, current_user):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You don't have permission to access this resource"
)
return current_user
return owner_or_admin_checker
+4 -2
View File
@@ -11,8 +11,8 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.keycloak_dependencies import get_current_keycloak_sub from app.auth.keycloak_dependencies import get_current_keycloak_sub
from app.core.config import settings from app.core.config import settings
from app.infra.db.dynamic_manager import project_connection_manager from app.infra.db.dynamic_manager import project_connection_manager
from app.infra.db.metadata.database import get_metadata_session from app.infra.db.metadb.database import get_metadata_session
from app.infra.repositories.metadata_repository import MetadataRepository from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository
DB_ROLE_BIZ_DATA = "biz_data" DB_ROLE_BIZ_DATA = "biz_data"
DB_ROLE_IOT_DATA = "iot_data" DB_ROLE_IOT_DATA = "iot_data"
@@ -25,6 +25,7 @@ logger = logging.getLogger(__name__)
@dataclass(frozen=True) @dataclass(frozen=True)
class ProjectContext: class ProjectContext:
project_id: UUID project_id: UUID
project_code: str
user_id: UUID user_id: UUID
project_role: str project_role: str
@@ -85,6 +86,7 @@ async def get_project_context(
return ProjectContext( return ProjectContext(
project_id=project.id, project_id=project.id,
project_code=project.code,
user_id=user.id, user_id=user.id,
project_role=membership_role, project_role=membership_role,
) )
+2 -2
View File
@@ -66,8 +66,8 @@ async def log_audit_event(
response_status: 响应状态码 response_status: 响应状态码
session: 元数据库会话(可选) session: 元数据库会话(可选)
""" """
from app.infra.db.metadata.database import SessionLocal from app.infra.db.metadb.database import SessionLocal
from app.infra.repositories.audit_repository import AuditRepository from app.infra.db.metadb.repositories.audit_repository import AuditRepository
if request_data: if request_data:
request_data = sanitize_sensitive_data(request_data) request_data = sanitize_sensitive_data(request_data)
+49 -17
View File
@@ -6,20 +6,13 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings): class Settings(BaseSettings):
PROJECT_NAME: str = "TJWater Server" PROJECT_NAME: str = "TJWater Server"
ENVIRONMENT: str = "local" ENVIRONMENT: str = "production"
API_V1_STR: str = "/api/v1" API_V1_STR: str = "/api/v1"
# JWT 配置 NETWORK_NAME: str = "default_network"
SECRET_KEY: str = (
"your-secret-key-here-change-in-production-use-openssl-rand-hex-32"
)
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
# 数据加密密钥 (使用 Fernet) # 敏感配置加密密钥 (Fernet)
ENCRYPTION_KEY: str = "" # 必须从环境变量设置 DATABASE_ENCRYPTION_KEY: str = ""
DATABASE_ENCRYPTION_KEY: str = "" # project_databases.dsn_encrypted 专用
# Database Config (PostgreSQL) # Database Config (PostgreSQL)
DB_NAME: str = "tjwater" DB_NAME: str = "tjwater"
@@ -34,11 +27,6 @@ class Settings(BaseSettings):
TIMESCALEDB_DB_PORT: str = "5433" TIMESCALEDB_DB_PORT: str = "5433"
TIMESCALEDB_DB_USER: str = "postgres" TIMESCALEDB_DB_USER: str = "postgres"
TIMESCALEDB_DB_PASSWORD: str = "password" TIMESCALEDB_DB_PASSWORD: str = "password"
# InfluxDB
INFLUXDB_URL: str = "http://localhost:8086"
INFLUXDB_TOKEN: str = "token"
INFLUXDB_ORG: str = "org"
INFLUXDB_BUCKET: str = "bucket"
# Metadata Database Config (PostgreSQL) # Metadata Database Config (PostgreSQL)
METADATA_DB_NAME: str = "system_hub" METADATA_DB_NAME: str = "system_hub"
@@ -57,11 +45,21 @@ class Settings(BaseSettings):
PROJECT_TS_POOL_MIN_SIZE: int = 1 PROJECT_TS_POOL_MIN_SIZE: int = 1
PROJECT_TS_POOL_MAX_SIZE: int = 10 PROJECT_TS_POOL_MAX_SIZE: int = 10
# Keycloak JWT (optional override) # Keycloak access token verification
KEYCLOAK_PUBLIC_KEY: str = "" KEYCLOAK_PUBLIC_KEY: str = ""
KEYCLOAK_ALGORITHM: str = "RS256" KEYCLOAK_ALGORITHM: str = "RS256"
KEYCLOAK_AUDIENCE: str = "" KEYCLOAK_AUDIENCE: str = ""
# Bocha Web Search API
BOCHA_API_KEY: str = ""
BOCHA_WEB_SEARCH_URL: str = "https://api.bochaai.com/v1/web-search"
BOCHA_WEB_SEARCH_TIMEOUT_SECONDS: float = 30.0
# Tianditu Geocoding API
TIANDITU_GEOCODER_TOKEN: str = ""
TIANDITU_GEOCODER_URL: str = "https://api.tianditu.gov.cn/geocoder"
TIANDITU_GEOCODER_TIMEOUT_SECONDS: float = 30.0
@property @property
def SQLALCHEMY_DATABASE_URI(self) -> str: def SQLALCHEMY_DATABASE_URI(self) -> str:
db_password = quote_plus(self.DB_PASSWORD) db_password = quote_plus(self.DB_PASSWORD)
@@ -116,3 +114,37 @@ def get_pg_config() -> dict:
def get_pg_password() -> str: def get_pg_password() -> str:
"""Return PostgreSQL password (use with care).""" """Return PostgreSQL password (use with care)."""
return settings.DB_PASSWORD return settings.DB_PASSWORD
def get_timescaledb_pgconn_string(
db_name: Optional[str] = None,
db_host: Optional[str] = None,
db_port: Optional[str] = None,
db_user: Optional[str] = None,
db_password: Optional[str] = None,
) -> str:
"""Return TimescaleDB connection string in psycopg conninfo format."""
resolved_db_name = db_name or settings.TIMESCALEDB_DB_NAME
resolved_db_host = db_host or settings.TIMESCALEDB_DB_HOST
resolved_db_port = db_port or settings.TIMESCALEDB_DB_PORT
resolved_db_user = db_user or settings.TIMESCALEDB_DB_USER
resolved_db_password = db_password or settings.TIMESCALEDB_DB_PASSWORD
return (
f"dbname={resolved_db_name} host={resolved_db_host} port={resolved_db_port} "
f"user={resolved_db_user} password={resolved_db_password}"
)
def get_timescaledb_pg_config() -> dict:
"""Return TimescaleDB configuration except password."""
return {
"name": settings.TIMESCALEDB_DB_NAME,
"host": settings.TIMESCALEDB_DB_HOST,
"port": settings.TIMESCALEDB_DB_PORT,
"user": settings.TIMESCALEDB_DB_USER,
}
def get_timescaledb_pg_password() -> str:
"""Return TimescaleDB password (use with care)."""
return settings.TIMESCALEDB_DB_PASSWORD
+3 -7
View File
@@ -20,10 +20,10 @@ class Encryptor:
key: 加密密钥,如果为 None 则从环境变量读取 key: 加密密钥,如果为 None 则从环境变量读取
""" """
if key is None: if key is None:
key_str = os.getenv("ENCRYPTION_KEY") or settings.ENCRYPTION_KEY key_str = os.getenv("DATABASE_ENCRYPTION_KEY") or settings.DATABASE_ENCRYPTION_KEY
if not key_str: if not key_str:
raise ValueError( raise ValueError(
"ENCRYPTION_KEY not found in environment variables or .env. " "DATABASE_ENCRYPTION_KEY not found in environment variables or .env. "
"Generate one using: Encryptor.generate_key()" "Generate one using: Encryptor.generate_key()"
) )
key = key_str.encode() key = key_str.encode()
@@ -80,15 +80,13 @@ _database_encryptor: Optional[Encryptor] = None
def is_encryption_configured() -> bool: def is_encryption_configured() -> bool:
return bool(os.getenv("ENCRYPTION_KEY") or settings.ENCRYPTION_KEY) return is_database_encryption_configured()
def is_database_encryption_configured() -> bool: def is_database_encryption_configured() -> bool:
return bool( return bool(
os.getenv("DATABASE_ENCRYPTION_KEY") os.getenv("DATABASE_ENCRYPTION_KEY")
or settings.DATABASE_ENCRYPTION_KEY or settings.DATABASE_ENCRYPTION_KEY
or os.getenv("ENCRYPTION_KEY")
or settings.ENCRYPTION_KEY
) )
@@ -107,8 +105,6 @@ def get_database_encryptor() -> Encryptor:
key_str = ( key_str = (
os.getenv("DATABASE_ENCRYPTION_KEY") os.getenv("DATABASE_ENCRYPTION_KEY")
or settings.DATABASE_ENCRYPTION_KEY or settings.DATABASE_ENCRYPTION_KEY
or os.getenv("ENCRYPTION_KEY")
or settings.ENCRYPTION_KEY
) )
if not key_str: if not key_str:
raise ValueError( raise ValueError(
-91
View File
@@ -1,91 +0,0 @@
from datetime import datetime, timedelta
from typing import Optional, Union, Any
from jose import jwt
from passlib.context import CryptContext
from app.core.config import settings
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def create_access_token(
subject: Union[str, Any], expires_delta: Optional[timedelta] = None
) -> str:
"""
创建 JWT Access Token
Args:
subject: 用户标识(通常是用户名或用户ID)
expires_delta: 过期时间增量
Returns:
JWT token 字符串
"""
if expires_delta:
expire = datetime.now() + expires_delta
else:
expire = datetime.now() + timedelta(
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
)
to_encode = {
"exp": expire,
"sub": str(subject),
"type": "access",
"iat": datetime.now(),
}
encoded_jwt = jwt.encode(
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
)
return encoded_jwt
def create_refresh_token(subject: Union[str, Any]) -> str:
"""
创建 JWT Refresh Token(长期有效)
Args:
subject: 用户标识
Returns:
JWT refresh token 字符串
"""
expire = datetime.now() + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
to_encode = {
"exp": expire,
"sub": str(subject),
"type": "refresh",
"iat": datetime.now(),
}
encoded_jwt = jwt.encode(
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
)
return encoded_jwt
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""
验证密码
Args:
plain_password: 明文密码
hashed_password: 密码哈希
Returns:
是否匹配
"""
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
"""
生成密码哈希
Args:
password: 明文密码
Returns:
bcrypt 哈希字符串
"""
return pwd_context.hash(password)
+3 -1
View File
@@ -1,7 +1,9 @@
""" """
This module is reserved for future implementation of advanced cryptographic operations. This module is reserved for future implementation of advanced cryptographic operations.
Current basic encryption (Fernet) and password hashing are implemented in `app.core.encryption` and `app.core.security`. Current Fernet encryption helpers are implemented in `app.core.encryption`.
Login credentials are owned by Keycloak; this backend does not hash or store
local passwords.
Future expansion may include: Future expansion may include:
- Asymmetric encryption (RSA/ECC) for secure communication - Asymmetric encryption (RSA/ECC) for secure communication
- Key management and rotation services - Key management and rotation services
-36
View File
@@ -1,36 +0,0 @@
from enum import Enum
class UserRole(str, Enum):
"""用户角色枚举"""
ADMIN = "ADMIN" # 管理员 - 完全权限
OPERATOR = "OPERATOR" # 操作员 - 可修改数据
USER = "USER" # 普通用户 - 读写权限
VIEWER = "VIEWER" # 观察者 - 仅查询权限
def __str__(self):
return self.value
@classmethod
def get_hierarchy(cls) -> dict:
"""
获取角色层级(数字越大权限越高)
"""
return {
cls.VIEWER: 1,
cls.USER: 2,
cls.OPERATOR: 3,
cls.ADMIN: 4,
}
def has_permission(self, required_role: 'UserRole') -> bool:
"""
检查当前角色是否有足够权限
Args:
required_role: 需要的最低角色
Returns:
True if has permission
"""
hierarchy = self.get_hierarchy()
return hierarchy[self] >= hierarchy[required_role]
+134
View File
@@ -0,0 +1,134 @@
from datetime import datetime
from typing import Literal
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field, model_validator
BusinessRole = Literal["admin", "user", "operator", "viewer"]
ProjectRole = Literal["owner", "admin", "member", "viewer"]
ProjectStatus = Literal["active", "inactive", "archived"]
ProjectDbRole = Literal["biz_data", "iot_data"]
class MetadataUserSyncRequest(BaseModel):
keycloak_id: UUID
username: str = Field(..., min_length=1, max_length=50)
email: str = Field(..., min_length=1, max_length=100)
role: BusinessRole = "user"
is_active: bool = True
class MetadataUsersBatchSyncRequest(BaseModel):
users: list[MetadataUserSyncRequest] = Field(..., min_length=1, max_length=500)
class MetadataUserUpdateRequest(BaseModel):
role: BusinessRole | None = None
is_active: bool | None = None
class MetadataUserResponse(BaseModel):
id: UUID
keycloak_id: UUID
username: str
email: str
role: str
is_active: bool
is_superuser: bool
created_at: datetime
updated_at: datetime
last_login_at: datetime | None = None
model_config = ConfigDict(from_attributes=True)
class MetadataUserSyncResult(BaseModel):
keycloak_id: UUID
user: MetadataUserResponse | None = None
success: bool
error: str | None = None
class ProjectMemberCreateRequest(BaseModel):
user_id: UUID
project_role: ProjectRole = "viewer"
class ProjectMemberUpdateRequest(BaseModel):
project_role: ProjectRole
class ProjectMemberResponse(BaseModel):
id: UUID
user_id: UUID
project_id: UUID
project_role: str
username: str
email: str
is_active: bool
class AdminProjectCreateRequest(BaseModel):
name: str = Field(..., min_length=1, max_length=100)
code: str = Field(..., min_length=1, max_length=50)
description: str | None = None
gs_workspace: str = Field(..., min_length=1, max_length=100)
map_extent: dict | None = None
status: ProjectStatus = "active"
class AdminProjectUpdateRequest(BaseModel):
name: str | None = Field(default=None, min_length=1, max_length=100)
code: str | None = Field(default=None, min_length=1, max_length=50)
description: str | None = None
gs_workspace: str | None = Field(default=None, min_length=1, max_length=100)
map_extent: dict | None = None
status: ProjectStatus | None = None
class AdminProjectResponse(BaseModel):
project_id: UUID
name: str
code: str
description: str | None = None
gs_workspace: str
map_extent: dict | None = None
status: str
created_at: datetime
updated_at: datetime
class ProjectDatabaseUpsertRequest(BaseModel):
db_role: ProjectDbRole
dsn: str | None = Field(default=None, min_length=1)
pool_min_size: int = Field(default=2, ge=1)
pool_max_size: int = Field(default=10, ge=1)
@model_validator(mode="after")
def validate_pool_bounds(self):
if self.pool_max_size < self.pool_min_size:
raise ValueError("pool_max_size must be greater than or equal to pool_min_size")
return self
class ProjectDatabaseResponse(BaseModel):
id: UUID
project_id: UUID
db_role: str
db_type: str
pool_min_size: int
pool_max_size: int
has_dsn: bool
class ProjectDatabaseHealthRequest(BaseModel):
dsn: str | None = Field(default=None, min_length=1)
class ProjectDatabaseHealthResponse(BaseModel):
project_id: UUID
db_role: str
db_type: str
ok: bool
detail: str
+3 -11
View File
@@ -4,30 +4,22 @@ from uuid import UUID
from pydantic import BaseModel from pydantic import BaseModel
class GeoServerConfigResponse(BaseModel):
gs_base_url: Optional[str]
gs_admin_user: Optional[str]
gs_datastore_name: str
default_extent: Optional[dict]
srid: int
class ProjectMetaResponse(BaseModel): class ProjectMetaResponse(BaseModel):
project_id: UUID project_id: UUID
name: str name: str
code: str code: str
description: Optional[str] description: Optional[str] = None
gs_workspace: str gs_workspace: str
map_extent: Optional[dict] = None
status: str status: str
project_role: str project_role: str
geoserver: Optional[GeoServerConfigResponse]
class ProjectSummaryResponse(BaseModel): class ProjectSummaryResponse(BaseModel):
project_id: UUID project_id: UUID
name: str name: str
code: str code: str
description: Optional[str] description: Optional[str] = None
gs_workspace: str gs_workspace: str
status: str status: str
project_role: str project_role: str
-68
View File
@@ -1,68 +0,0 @@
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, EmailStr, Field, ConfigDict
from app.domain.models.role import UserRole
# ============================================
# Request Schemas (输入)
# ============================================
class UserCreate(BaseModel):
"""用户注册"""
username: str = Field(..., min_length=3, max_length=50,
description="用户名,3-50个字符")
email: EmailStr = Field(..., description="邮箱地址")
password: str = Field(..., min_length=6, max_length=100,
description="密码,至少6个字符")
role: UserRole = Field(default=UserRole.USER, description="用户角色")
class UserLogin(BaseModel):
"""用户登录"""
username: str = Field(..., description="用户名或邮箱")
password: str = Field(..., description="密码")
class UserUpdate(BaseModel):
"""用户信息更新"""
email: Optional[EmailStr] = None
password: Optional[str] = Field(None, min_length=6, max_length=100)
role: Optional[UserRole] = None
is_active: Optional[bool] = None
# ============================================
# Response Schemas (输出)
# ============================================
class UserResponse(BaseModel):
"""用户信息响应(不含密码)"""
id: int
username: str
email: str
role: UserRole
is_active: bool
is_superuser: bool
created_at: datetime
updated_at: datetime
model_config = ConfigDict(from_attributes=True)
class UserInDB(UserResponse):
"""数据库中的用户(含密码哈希)"""
hashed_password: str
# ============================================
# Token Schemas
# ============================================
class Token(BaseModel):
"""JWT Token 响应"""
access_token: str
refresh_token: Optional[str] = None
token_type: str = "bearer"
expires_in: int = Field(..., description="过期时间(秒)")
class TokenPayload(BaseModel):
"""JWT Token Payload"""
sub: str = Field(..., description="用户ID或用户名")
exp: Optional[int] = None
iat: Optional[int] = None
type: str = Field(default="access", description="token类型: access 或 refresh")
+36 -19
View File
@@ -15,8 +15,8 @@ import logging
from jose import JWTError, jwt from jose import JWTError, jwt
from app.core.config import settings from app.core.config import settings
from app.infra.db.metadata.database import SessionLocal from app.infra.db.metadb.database import SessionLocal
from app.infra.repositories.metadata_repository import MetadataRepository from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -33,8 +33,6 @@ class AuditMiddleware(BaseHTTPMiddleware):
# 需要审计的路径前缀 # 需要审计的路径前缀
AUDIT_PATHS = [ AUDIT_PATHS = [
# "/api/v1/auth/",
# "/api/v1/users/",
# "/api/v1/projects/", # "/api/v1/projects/",
# "/api/v1/networks/", # "/api/v1/networks/",
] ]
@@ -61,11 +59,20 @@ class AuditMiddleware(BaseHTTPMiddleware):
"/api/v1/openproject/", "/api/v1/openproject/",
"/openproject/", "/openproject/",
} }
EXCLUDED_PATH_PREFIXES = (
)
async def dispatch(self, request: Request, call_next: Callable) -> Response: async def dispatch(self, request: Request, call_next: Callable) -> Response:
# 提取开始时间 # 提取开始时间
start_time = time.time() start_time = time.time()
# 流式 Copilot 请求前置排除,避免读取/改写 body 影响 SSE 生命周期
if self._is_excluded_path(request.url.path):
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
# 1. 预判是否需要读取Body (针对写操作) # 1. 预判是否需要读取Body (针对写操作)
# 注意:我们暂时移除早期的 return,因为需要等待路由匹配后才能检查 Tag # 注意:我们暂时移除早期的 return,因为需要等待路由匹配后才能检查 Tag
should_capture_body = request.method in ["POST", "PUT", "PATCH"] should_capture_body = request.method in ["POST", "PUT", "PATCH"]
@@ -74,13 +81,24 @@ class AuditMiddleware(BaseHTTPMiddleware):
if should_capture_body: if should_capture_body:
try: try:
# 注意:读取 body 后需要重新设置,避免影响后续处理 # 注意:读取 body 后需要重新设置,避免影响后续处理
original_receive = request._receive
body = await request.body() body = await request.body()
if body: if body:
request_data = json.loads(body.decode()) request_data = json.loads(body.decode())
# 重新构造请求以供后续使用 # 重新构造请求以供后续使用:仅回放一次,后续回落原始 receive
body_sent = False
async def receive(): async def receive():
return {"type": "http.request", "body": body} nonlocal body_sent
if not body_sent:
body_sent = True
return {
"type": "http.request",
"body": body,
"more_body": False,
}
return await original_receive()
request._receive = receive request._receive = receive
except Exception as e: except Exception as e:
@@ -90,7 +108,7 @@ class AuditMiddleware(BaseHTTPMiddleware):
response = await call_next(request) response = await call_next(request)
# 3. 决定是否审计 # 3. 决定是否审计
if request.url.path in self.EXCLUDED_PATHS: if self._is_excluded_path(request.url.path):
process_time = time.time() - start_time process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time) response.headers["X-Process-Time"] = str(process_time)
return response return response
@@ -150,6 +168,11 @@ class AuditMiddleware(BaseHTTPMiddleware):
return response return response
def _is_excluded_path(self, path: str) -> bool:
if path in self.EXCLUDED_PATHS:
return True
return any(path.startswith(prefix) for prefix in self.EXCLUDED_PATH_PREFIXES)
def _resolve_project_id(self, request: Request) -> UUID | None: def _resolve_project_id(self, request: Request) -> UUID | None:
project_header = request.headers.get("X-Project-Id") project_header = request.headers.get("X-Project-Id")
if not project_header: if not project_header:
@@ -168,20 +191,14 @@ class AuditMiddleware(BaseHTTPMiddleware):
return None return None
sub = None sub = None
try: try:
key = ( if not settings.KEYCLOAK_PUBLIC_KEY:
settings.KEYCLOAK_PUBLIC_KEY.replace("\\n", "\n") return None
if settings.KEYCLOAK_PUBLIC_KEY
else settings.SECRET_KEY key = settings.KEYCLOAK_PUBLIC_KEY.replace("\\n", "\n")
)
algorithms = (
[settings.KEYCLOAK_ALGORITHM]
if settings.KEYCLOAK_PUBLIC_KEY
else [settings.ALGORITHM]
)
payload = jwt.decode( payload = jwt.decode(
token, token,
key, key,
algorithms=algorithms, algorithms=[settings.KEYCLOAK_ALGORITHM],
audience=settings.KEYCLOAK_AUDIENCE or None, audience=settings.KEYCLOAK_AUDIENCE or None,
) )
sub = payload.get("sub") sub = payload.get("sub")
@@ -196,7 +213,7 @@ class AuditMiddleware(BaseHTTPMiddleware):
keycloak_id = UUID(sub) keycloak_id = UUID(sub)
user = await repo.get_user_by_keycloak_id(keycloak_id) user = await repo.get_user_by_keycloak_id(keycloak_id)
except ValueError: except ValueError:
user = await repo.get_user_by_username(sub) return None
if user and user.is_active: if user and user.is_active:
return user.id return user.id
return None return None
+98 -32
View File
@@ -24,6 +24,17 @@ logger = logging.getLogger(__name__)
class PgEngineEntry: class PgEngineEntry:
engine: AsyncEngine engine: AsyncEngine
sessionmaker: async_sessionmaker[AsyncSession] sessionmaker: async_sessionmaker[AsyncSession]
connection_url: str
pool_min_size: int
pool_max_size: int
@dataclass(frozen=True)
class PoolEntry:
pool: AsyncConnectionPool
connection_url: str
pool_min_size: int
pool_max_size: int
@dataclass(frozen=True) @dataclass(frozen=True)
@@ -35,17 +46,17 @@ class CacheKey:
class ProjectConnectionManager: class ProjectConnectionManager:
def __init__(self) -> None: def __init__(self) -> None:
self._pg_cache: Dict[CacheKey, PgEngineEntry] = OrderedDict() self._pg_cache: Dict[CacheKey, PgEngineEntry] = OrderedDict()
self._ts_cache: Dict[CacheKey, AsyncConnectionPool] = OrderedDict() self._ts_cache: Dict[CacheKey, PoolEntry] = OrderedDict()
self._pg_raw_cache: Dict[CacheKey, AsyncConnectionPool] = OrderedDict() self._pg_raw_cache: Dict[CacheKey, PoolEntry] = OrderedDict()
self._pg_lock = asyncio.Lock() self._pg_lock = asyncio.Lock()
self._ts_lock = asyncio.Lock() self._ts_lock = asyncio.Lock()
self._pg_raw_lock = asyncio.Lock() self._pg_raw_lock = asyncio.Lock()
def _normalize_pg_url(self, url: str) -> str: def _normalize_pg_url(self, url: str) -> str:
parsed = make_url(url) parsed = make_url(url)
if parsed.drivername == "postgresql": if parsed.drivername in {"postgresql", "postgres"}:
parsed = parsed.set(drivername="postgresql+psycopg") parsed = parsed.set(drivername="postgresql+psycopg")
return str(parsed) return parsed.render_as_string(hide_password=False)
async def get_pg_sessionmaker( async def get_pg_sessionmaker(
self, self,
@@ -56,15 +67,29 @@ class ProjectConnectionManager:
pool_max_size: int, pool_max_size: int,
) -> async_sessionmaker[AsyncSession]: ) -> async_sessionmaker[AsyncSession]:
async with self._pg_lock: async with self._pg_lock:
key = CacheKey(project_id=project_id, db_role=db_role)
entry = self._pg_cache.get(key)
if entry:
self._pg_cache.move_to_end(key)
return entry.sessionmaker
normalized_url = self._normalize_pg_url(connection_url) normalized_url = self._normalize_pg_url(connection_url)
pool_min_size = max(1, pool_min_size) pool_min_size = max(1, pool_min_size)
pool_max_size = max(pool_min_size, pool_max_size) pool_max_size = max(pool_min_size, pool_max_size)
key = CacheKey(project_id=project_id, db_role=db_role)
entry = self._pg_cache.get(key)
if entry:
if (
entry.connection_url == normalized_url
and entry.pool_min_size == pool_min_size
and entry.pool_max_size == pool_max_size
):
self._pg_cache.move_to_end(key)
return entry.sessionmaker
await entry.engine.dispose()
logger.info(
"Rebuilding PostgreSQL engine for project %s (%s) due to config change",
project_id,
db_role,
)
self._pg_cache.pop(key, None)
engine = create_async_engine( engine = create_async_engine(
normalized_url, normalized_url,
pool_size=pool_min_size, pool_size=pool_min_size,
@@ -75,6 +100,9 @@ class ProjectConnectionManager:
self._pg_cache[key] = PgEngineEntry( self._pg_cache[key] = PgEngineEntry(
engine=engine, engine=engine,
sessionmaker=sessionmaker, sessionmaker=sessionmaker,
connection_url=normalized_url,
pool_min_size=pool_min_size,
pool_max_size=pool_max_size,
) )
await self._evict_pg_if_needed() await self._evict_pg_if_needed()
logger.info( logger.info(
@@ -91,14 +119,28 @@ class ProjectConnectionManager:
pool_max_size: int, pool_max_size: int,
) -> AsyncConnectionPool: ) -> AsyncConnectionPool:
async with self._ts_lock: async with self._ts_lock:
key = CacheKey(project_id=project_id, db_role=db_role)
pool = self._ts_cache.get(key)
if pool:
self._ts_cache.move_to_end(key)
return pool
pool_min_size = max(1, pool_min_size) pool_min_size = max(1, pool_min_size)
pool_max_size = max(pool_min_size, pool_max_size) pool_max_size = max(pool_min_size, pool_max_size)
key = CacheKey(project_id=project_id, db_role=db_role)
entry = self._ts_cache.get(key)
if entry:
if (
entry.connection_url == connection_url
and entry.pool_min_size == pool_min_size
and entry.pool_max_size == pool_max_size
):
self._ts_cache.move_to_end(key)
return entry.pool
await entry.pool.close()
logger.info(
"Rebuilding TimescaleDB pool for project %s (%s) due to config change",
project_id,
db_role,
)
self._ts_cache.pop(key, None)
pool = AsyncConnectionPool( pool = AsyncConnectionPool(
conninfo=connection_url, conninfo=connection_url,
min_size=pool_min_size, min_size=pool_min_size,
@@ -107,7 +149,12 @@ class ProjectConnectionManager:
kwargs={"row_factory": dict_row}, kwargs={"row_factory": dict_row},
) )
await pool.open() await pool.open()
self._ts_cache[key] = pool self._ts_cache[key] = PoolEntry(
pool=pool,
connection_url=connection_url,
pool_min_size=pool_min_size,
pool_max_size=pool_max_size,
)
await self._evict_ts_if_needed() await self._evict_ts_if_needed()
logger.info( logger.info(
"Created TimescaleDB pool for project %s (%s)", project_id, db_role "Created TimescaleDB pool for project %s (%s)", project_id, db_role
@@ -123,14 +170,28 @@ class ProjectConnectionManager:
pool_max_size: int, pool_max_size: int,
) -> AsyncConnectionPool: ) -> AsyncConnectionPool:
async with self._pg_raw_lock: async with self._pg_raw_lock:
key = CacheKey(project_id=project_id, db_role=db_role)
pool = self._pg_raw_cache.get(key)
if pool:
self._pg_raw_cache.move_to_end(key)
return pool
pool_min_size = max(1, pool_min_size) pool_min_size = max(1, pool_min_size)
pool_max_size = max(pool_min_size, pool_max_size) pool_max_size = max(pool_min_size, pool_max_size)
key = CacheKey(project_id=project_id, db_role=db_role)
entry = self._pg_raw_cache.get(key)
if entry:
if (
entry.connection_url == connection_url
and entry.pool_min_size == pool_min_size
and entry.pool_max_size == pool_max_size
):
self._pg_raw_cache.move_to_end(key)
return entry.pool
await entry.pool.close()
logger.info(
"Rebuilding PostgreSQL pool for project %s (%s) due to config change",
project_id,
db_role,
)
self._pg_raw_cache.pop(key, None)
pool = AsyncConnectionPool( pool = AsyncConnectionPool(
conninfo=connection_url, conninfo=connection_url,
min_size=pool_min_size, min_size=pool_min_size,
@@ -139,7 +200,12 @@ class ProjectConnectionManager:
kwargs={"row_factory": dict_row}, kwargs={"row_factory": dict_row},
) )
await pool.open() await pool.open()
self._pg_raw_cache[key] = pool self._pg_raw_cache[key] = PoolEntry(
pool=pool,
connection_url=connection_url,
pool_min_size=pool_min_size,
pool_max_size=pool_max_size,
)
await self._evict_pg_raw_if_needed() await self._evict_pg_raw_if_needed()
logger.info( logger.info(
"Created PostgreSQL pool for project %s (%s)", project_id, db_role "Created PostgreSQL pool for project %s (%s)", project_id, db_role
@@ -158,8 +224,8 @@ class ProjectConnectionManager:
async def _evict_ts_if_needed(self) -> None: async def _evict_ts_if_needed(self) -> None:
while len(self._ts_cache) > settings.PROJECT_TS_CACHE_SIZE: while len(self._ts_cache) > settings.PROJECT_TS_CACHE_SIZE:
key, pool = self._ts_cache.popitem(last=False) key, entry = self._ts_cache.popitem(last=False)
await pool.close() await entry.pool.close()
logger.info( logger.info(
"Evicted TimescaleDB pool for project %s (%s)", "Evicted TimescaleDB pool for project %s (%s)",
key.project_id, key.project_id,
@@ -168,8 +234,8 @@ class ProjectConnectionManager:
async def _evict_pg_raw_if_needed(self) -> None: async def _evict_pg_raw_if_needed(self) -> None:
while len(self._pg_raw_cache) > settings.PROJECT_PG_CACHE_SIZE: while len(self._pg_raw_cache) > settings.PROJECT_PG_CACHE_SIZE:
key, pool = self._pg_raw_cache.popitem(last=False) key, entry = self._pg_raw_cache.popitem(last=False)
await pool.close() await entry.pool.close()
logger.info( logger.info(
"Evicted PostgreSQL pool for project %s (%s)", "Evicted PostgreSQL pool for project %s (%s)",
key.project_id, key.project_id,
@@ -188,8 +254,8 @@ class ProjectConnectionManager:
self._pg_cache.clear() self._pg_cache.clear()
async with self._ts_lock: async with self._ts_lock:
for key, pool in list(self._ts_cache.items()): for key, entry in list(self._ts_cache.items()):
await pool.close() await entry.pool.close()
logger.info( logger.info(
"Closed TimescaleDB pool for project %s (%s)", "Closed TimescaleDB pool for project %s (%s)",
key.project_id, key.project_id,
@@ -198,8 +264,8 @@ class ProjectConnectionManager:
self._ts_cache.clear() self._ts_cache.clear()
async with self._pg_raw_lock: async with self._pg_raw_lock:
for key, pool in list(self._pg_raw_cache.items()): for key, entry in list(self._pg_raw_cache.items()):
await pool.close() await entry.pool.close()
logger.info( logger.info(
"Closed PostgreSQL pool for project %s (%s)", "Closed PostgreSQL pool for project %s (%s)",
key.project_id, key.project_id,
File diff suppressed because it is too large Load Diff
-6
View File
@@ -1,6 +0,0 @@
# influxdb数据库连接信息
url = "http://127.0.0.1:8086" # 替换为你的InfluxDB实例地址
token = "kMPX2V5HsbzPpUT2B9HPBu1sTG1Emf-lPlT2UjxYnGAuocpXq_f_0lK4HHs-TbbKyjsZpICkMsyXG_V2D7P7yQ==" # 替换为你的InfluxDB Token
# _ENCODED_TOKEN = "eEdETTVSWnFSSkF1ekFHUy1vdFhVZEMyTkZkWTc1cUpBalJMcUFCNHA1V2NJSUFsSVVwT3BUOF95QTE2QU9IbUpXZXJ3UV8wOGd3Yjg0c3k0MmpuWlE9PQ=="
# token = base64.b64decode(_ENCODED_TOKEN).decode("utf-8")
org = "TJWATERORG" # 替换为你的Organization名称
-33
View File
@@ -1,33 +0,0 @@
from influxdb_client import InfluxDBClient, Point, WriteOptions
from influxdb_client.client.query_api import QueryApi
import influxdb_info
# 配置 InfluxDB 连接
url = influxdb_info.url
token = influxdb_info.token
org = influxdb_info.org
bucket = "SCADA_data"
# 创建 InfluxDB 客户端
client = InfluxDBClient(url=url, token=token, org=org)
# 创建查询 API 对象
query_api = client.query_api()
# 构建查询语句
query = f'''
from(bucket: "{bucket}")
|> range(start: -1h)
'''
# 执行查询
result = query_api.query(query)
print(result)
# 处理查询结果
for table in result:
for record in table.records:
print(f"Time: {record.get_time()}, Value: {record.get_value()}, Measurement: {record.get_measurement()}, Field: {record.get_field()}")
# 关闭客户端连接
client.close()
@@ -42,6 +42,7 @@ class Project(Base):
code: Mapped[str] = mapped_column(String(50), unique=True) code: Mapped[str] = mapped_column(String(50), unique=True)
description: Mapped[str | None] = mapped_column(Text, nullable=True) description: Mapped[str | None] = mapped_column(Text, nullable=True)
gs_workspace: Mapped[str] = mapped_column(String(100), unique=True) gs_workspace: Mapped[str] = mapped_column(String(100), unique=True)
map_extent: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
status: Mapped[str] = mapped_column(String(20), default="active") status: Mapped[str] = mapped_column(String(20), default="active")
created_at: Mapped[datetime] = mapped_column( created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow DateTime(timezone=True), default=datetime.utcnow
@@ -63,26 +64,6 @@ class ProjectDatabase(Base):
pool_max_size: Mapped[int] = mapped_column(Integer, default=10) pool_max_size: Mapped[int] = mapped_column(Integer, default=10)
class ProjectGeoServerConfig(Base):
__tablename__ = "project_geoserver_configs"
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
project_id: Mapped[UUID] = mapped_column(
PGUUID(as_uuid=True), unique=True, index=True
)
gs_base_url: Mapped[str | None] = mapped_column(Text, nullable=True)
gs_admin_user: Mapped[str | None] = mapped_column(String(50), nullable=True)
gs_admin_password_encrypted: Mapped[str | None] = mapped_column(
Text, nullable=True
)
gs_datastore_name: Mapped[str] = mapped_column(String(100), default="ds_postgis")
default_extent: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
srid: Mapped[int] = mapped_column(Integer, default=4326)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=datetime.utcnow
)
class UserProjectMembership(Base): class UserProjectMembership(Base):
__tablename__ = "user_project_membership" __tablename__ = "user_project_membership"
@@ -6,7 +6,7 @@ from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.domain.schemas.audit import AuditLogResponse from app.domain.schemas.audit import AuditLogResponse
from app.infra.db.metadata import models from app.infra.db.metadb import models
class AuditRepository: class AuditRepository:
@@ -0,0 +1,489 @@
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Optional, List
from uuid import UUID, uuid4
from cryptography.fernet import InvalidToken
from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.encryption import (
get_database_encryptor,
is_database_encryption_configured,
)
from app.infra.db.metadb import models
def _normalize_postgres_dsn(dsn: str) -> str:
if not dsn or "://" not in dsn:
return dsn
scheme, rest = dsn.split("://", 1)
if scheme not in ("postgresql", "postgres", "postgresql+psycopg"):
return dsn
if "@" not in rest:
return dsn
userinfo, hostinfo = rest.rsplit("@", 1)
if ":" not in userinfo:
return dsn
username, password = userinfo.split(":", 1)
if "@" not in password:
return dsn
password = password.replace("@", "%40")
return f"{scheme}://{username}:{password}@{hostinfo}"
@dataclass(frozen=True)
class ProjectDbRouting:
project_id: UUID
db_role: str
db_type: str
dsn: str
pool_min_size: int
pool_max_size: int
@dataclass(frozen=True)
class ProjectSummary:
project_id: UUID
name: str
code: str
description: Optional[str]
gs_workspace: str
map_extent: Optional[dict]
status: str
project_role: str
@dataclass(frozen=True)
class ProjectDetail:
project_id: UUID
name: str
code: str
description: Optional[str]
gs_workspace: str
map_extent: Optional[dict]
status: str
@dataclass(frozen=True)
class ProjectMemberSummary:
id: UUID
user_id: UUID
project_id: UUID
project_role: str
username: str
email: str
is_active: bool
def _utcnow() -> datetime:
return datetime.now(timezone.utc)
def _encrypt_database_secret(value: str) -> str:
if not is_database_encryption_configured():
raise ValueError("DATABASE_ENCRYPTION_KEY is not configured")
return get_database_encryptor().encrypt(value)
class MetadataRepository:
"""元数据访问层(system_hub"""
def __init__(self, session: AsyncSession):
self.session = session
async def get_user_by_keycloak_id(self, keycloak_id: UUID) -> Optional[models.User]:
result = await self.session.execute(
select(models.User).where(models.User.keycloak_id == keycloak_id)
)
return result.scalar_one_or_none()
async def get_user_by_username(self, username: str) -> Optional[models.User]:
result = await self.session.execute(
select(models.User).where(models.User.username == username)
)
return result.scalar_one_or_none()
async def get_user_by_id(self, user_id: UUID) -> Optional[models.User]:
result = await self.session.execute(
select(models.User).where(models.User.id == user_id)
)
return result.scalar_one_or_none()
async def list_users(self, skip: int = 0, limit: int = 100) -> List[models.User]:
result = await self.session.execute(
select(models.User)
.order_by(models.User.created_at.desc())
.offset(skip)
.limit(limit)
)
return list(result.scalars().all())
async def upsert_user_from_keycloak(
self,
*,
keycloak_id: UUID,
username: str,
email: str,
role: str,
is_active: bool,
) -> models.User:
user = await self.get_user_by_keycloak_id(keycloak_id)
if user is None:
user = models.User(
id=uuid4(),
keycloak_id=keycloak_id,
username=username,
email=email,
role=role,
is_active=is_active,
is_superuser=False,
)
self.session.add(user)
else:
user.username = username
user.email = email
user.role = role
user.is_active = is_active
await self.session.commit()
await self.session.refresh(user)
return user
async def refresh_user_keycloak_snapshot(
self,
user: models.User,
*,
username: str | None,
email: str | None,
last_login_at: datetime | None = None,
) -> models.User:
if username:
user.username = username
if email:
user.email = email
user.last_login_at = last_login_at or _utcnow()
user.updated_at = _utcnow()
await self.session.commit()
await self.session.refresh(user)
return user
async def update_user_admin(
self,
user_id: UUID,
*,
updates: dict,
) -> Optional[models.User]:
user = await self.get_user_by_id(user_id)
if user is None:
return None
if "role" in updates:
user.role = updates["role"]
if "is_active" in updates:
user.is_active = updates["is_active"]
await self.session.commit()
await self.session.refresh(user)
return user
async def get_project_by_id(self, project_id: UUID) -> Optional[models.Project]:
result = await self.session.execute(
select(models.Project).where(models.Project.id == project_id)
)
return result.scalar_one_or_none()
async def get_project_by_code(self, code: str) -> Optional[models.Project]:
result = await self.session.execute(
select(models.Project).where(models.Project.code == code)
)
return result.scalar_one_or_none()
async def list_project_records(self) -> List[models.Project]:
result = await self.session.execute(
select(models.Project).order_by(models.Project.name)
)
return list(result.scalars().all())
async def create_project(
self,
*,
name: str,
code: str,
description: str | None,
gs_workspace: str,
map_extent: dict | None,
status: str,
) -> models.Project:
project = models.Project(
id=uuid4(),
name=name,
code=code,
description=description,
gs_workspace=gs_workspace,
map_extent=map_extent,
status=status,
created_at=_utcnow(),
updated_at=_utcnow(),
)
self.session.add(project)
await self.session.commit()
await self.session.refresh(project)
return project
async def update_project(
self,
project_id: UUID,
*,
updates: dict,
) -> Optional[models.Project]:
project = await self.get_project_by_id(project_id)
if project is None:
return None
for field in (
"name",
"code",
"description",
"gs_workspace",
"map_extent",
"status",
):
if field in updates:
setattr(project, field, updates[field])
project.updated_at = _utcnow()
await self.session.commit()
await self.session.refresh(project)
return project
async def get_project_detail_by_code(self, code: str) -> Optional[ProjectDetail]:
project = await self.get_project_by_code(code)
if not project:
return None
return ProjectDetail(
project_id=project.id,
name=project.name,
code=project.code,
description=project.description,
gs_workspace=project.gs_workspace,
map_extent=project.map_extent,
status=project.status,
)
async def get_membership_role(
self, project_id: UUID, user_id: UUID
) -> Optional[str]:
result = await self.session.execute(
select(models.UserProjectMembership.project_role).where(
models.UserProjectMembership.project_id == project_id,
models.UserProjectMembership.user_id == user_id,
)
)
return result.scalar_one_or_none()
async def list_project_members(
self, project_id: UUID
) -> List[ProjectMemberSummary]:
stmt = (
select(models.UserProjectMembership, models.User)
.join(models.User, models.User.id == models.UserProjectMembership.user_id)
.where(models.UserProjectMembership.project_id == project_id)
.order_by(models.User.username)
)
result = await self.session.execute(stmt)
return [
ProjectMemberSummary(
id=membership.id,
user_id=membership.user_id,
project_id=membership.project_id,
project_role=membership.project_role,
username=user.username,
email=user.email,
is_active=user.is_active,
)
for membership, user in result.all()
]
async def get_project_membership(
self, project_id: UUID, user_id: UUID
) -> Optional[models.UserProjectMembership]:
result = await self.session.execute(
select(models.UserProjectMembership).where(
models.UserProjectMembership.project_id == project_id,
models.UserProjectMembership.user_id == user_id,
)
)
return result.scalar_one_or_none()
async def add_project_member(
self, project_id: UUID, user_id: UUID, project_role: str
) -> models.UserProjectMembership:
membership = models.UserProjectMembership(
id=uuid4(),
user_id=user_id,
project_id=project_id,
project_role=project_role,
)
self.session.add(membership)
await self.session.commit()
await self.session.refresh(membership)
return membership
async def update_project_member_role(
self, project_id: UUID, user_id: UUID, project_role: str
) -> Optional[models.UserProjectMembership]:
membership = await self.get_project_membership(project_id, user_id)
if membership is None:
return None
membership.project_role = project_role
await self.session.commit()
await self.session.refresh(membership)
return membership
async def remove_project_member(self, project_id: UUID, user_id: UUID) -> bool:
result = await self.session.execute(
delete(models.UserProjectMembership).where(
models.UserProjectMembership.project_id == project_id,
models.UserProjectMembership.user_id == user_id,
)
)
await self.session.commit()
return bool(result.rowcount)
async def list_project_databases(
self, project_id: UUID
) -> List[models.ProjectDatabase]:
result = await self.session.execute(
select(models.ProjectDatabase)
.where(models.ProjectDatabase.project_id == project_id)
.order_by(models.ProjectDatabase.db_role)
)
return list(result.scalars().all())
async def get_project_database_config(
self, project_id: UUID, db_role: str
) -> Optional[models.ProjectDatabase]:
result = await self.session.execute(
select(models.ProjectDatabase).where(
models.ProjectDatabase.project_id == project_id,
models.ProjectDatabase.db_role == db_role,
)
)
return result.scalar_one_or_none()
async def upsert_project_database_config(
self,
project_id: UUID,
*,
db_role: str,
db_type: str,
dsn: str | None,
pool_min_size: int,
pool_max_size: int,
) -> models.ProjectDatabase:
record = await self.get_project_database_config(project_id, db_role)
if record is None:
if dsn is None:
raise ValueError("dsn is required when creating project database config")
record = models.ProjectDatabase(
id=uuid4(),
project_id=project_id,
db_role=db_role,
db_type=db_type,
dsn_encrypted=_encrypt_database_secret(dsn),
pool_min_size=pool_min_size,
pool_max_size=pool_max_size,
)
self.session.add(record)
else:
record.db_type = db_type
if dsn is not None:
record.dsn_encrypted = _encrypt_database_secret(dsn)
record.pool_min_size = pool_min_size
record.pool_max_size = pool_max_size
await self.session.commit()
await self.session.refresh(record)
return record
async def delete_project_database_config(
self, project_id: UUID, db_role: str
) -> bool:
result = await self.session.execute(
delete(models.ProjectDatabase).where(
models.ProjectDatabase.project_id == project_id,
models.ProjectDatabase.db_role == db_role,
)
)
await self.session.commit()
return bool(result.rowcount)
async def get_project_db_routing(
self, project_id: UUID, db_role: str
) -> Optional[ProjectDbRouting]:
result = await self.session.execute(
select(models.ProjectDatabase).where(
models.ProjectDatabase.project_id == project_id,
models.ProjectDatabase.db_role == db_role,
)
)
record = result.scalar_one_or_none()
if not record:
return None
if not is_database_encryption_configured():
raise ValueError("DATABASE_ENCRYPTION_KEY is not configured")
encryptor = get_database_encryptor()
try:
dsn = encryptor.decrypt(record.dsn_encrypted)
except InvalidToken:
raise ValueError(
"Failed to decrypt project DB DSN: DATABASE_ENCRYPTION_KEY mismatch "
"or invalid dsn_encrypted value"
)
dsn = _normalize_postgres_dsn(dsn)
return ProjectDbRouting(
project_id=record.project_id,
db_role=record.db_role,
db_type=record.db_type,
dsn=dsn,
pool_min_size=record.pool_min_size,
pool_max_size=record.pool_max_size,
)
async def list_projects_for_user(self, user_id: UUID) -> List[ProjectSummary]:
stmt = (
select(models.Project, models.UserProjectMembership.project_role)
.join(
models.UserProjectMembership,
models.UserProjectMembership.project_id == models.Project.id,
)
.where(models.UserProjectMembership.user_id == user_id)
.order_by(models.Project.name)
)
result = await self.session.execute(stmt)
return [
ProjectSummary(
project_id=project.id,
name=project.name,
code=project.code,
description=project.description,
gs_workspace=project.gs_workspace,
map_extent=project.map_extent,
status=project.status,
project_role=role,
)
for project, role in result.all()
]
async def list_all_projects(self) -> List[ProjectSummary]:
result = await self.session.execute(
select(models.Project).order_by(models.Project.name)
)
return [
ProjectSummary(
project_id=project.id,
name=project.name,
code=project.code,
description=project.description,
gs_workspace=project.gs_workspace,
map_extent=project.map_extent,
status=project.status,
project_role="owner",
)
for project in result.scalars().all()
]
@@ -1,83 +0,0 @@
import time
from typing import List, Optional
from fastapi.logger import logger
import app.core.config as postgresql_info
import psycopg
class InternalQueries:
@staticmethod
def get_links_by_property(
fields: Optional[List[str]] = None,
property_conditions: Optional[dict] = None,
db_name: str = None,
max_retries: int = 3,
) -> List[dict]:
"""
查询pg数据库中,pipes 的指定字段记录或根据属性筛选
:param fields: 要查询的字段列表 ["id", "diameter", "status"]默认查询所有字段
:param property: 可选的筛选条件字典 {"status": "Open"} {"diameter": 300}
:param db_name: 数据库名称
:param max_retries: 最大重试次数
:return: 包含所有记录的列表,每条记录为一个字典
"""
# 如果未指定字段,查询所有字段
if not fields:
fields = [
"id",
"node1",
"node2",
"length",
"diameter",
"roughness",
"minor_loss",
"status",
]
for attempt in range(max_retries):
try:
conn_string = (
postgresql_info.get_pgconn_string(db_name=db_name)
if db_name
else postgresql_info.get_pgconn_string()
)
with psycopg.Connection.connect(conn_string) as conn:
with conn.cursor() as cur:
# 构建SELECT子句
select_fields = ", ".join(fields)
base_query = f"""
SELECT {select_fields}
FROM public.pipes
"""
# 如果提供了筛选条件,构建WHERE子句
if property_conditions:
conditions = []
params = []
for key, value in property_conditions.items():
conditions.append(f"{key} = %s")
params.append(value)
query = base_query + " WHERE " + " AND ".join(conditions)
cur.execute(query, params)
else:
cur.execute(base_query)
records = cur.fetchall()
# 将查询结果转换为字典列表
pipes = []
for record in records:
pipe_dict = {}
for idx, field in enumerate(fields):
pipe_dict[field] = record[idx]
pipes.append(pipe_dict)
return pipes
break # 成功
except Exception as e:
logger.error(f"查询尝试 {attempt + 1} 失败: {e}")
if attempt < max_retries - 1:
time.sleep(1)
else:
raise
+51
View File
@@ -0,0 +1,51 @@
from typing import Any
from psycopg import AsyncConnection
def _optional_text(value: Any) -> str | None:
return str(value).strip() if value is not None else None
def _optional_float(value: Any) -> float | None:
return float(value) if value is not None else None
class ScadaInfoRepository:
"""Read SCADA metadata from the current project's business database."""
@staticmethod
async def get_scadas(conn: AsyncConnection) -> list[dict[str, Any]]:
async with conn.cursor() as cur:
await cur.execute(
"""
SELECT id,
type,
associated_element_id,
api_query_id,
transmission_mode,
transmission_frequency,
reliability,
x_coor,
y_coor
FROM public.scada_info
"""
)
records = await cur.fetchall()
return [
{
"id": str(record["id"]).strip(),
"type": str(record["type"]).strip().lower(),
"associated_element_id": _optional_text(
record["associated_element_id"]
),
"api_query_id": record["api_query_id"],
"transmission_mode": record["transmission_mode"],
"transmission_frequency": record["transmission_frequency"],
"reliability": _optional_float(record["reliability"]),
"x": _optional_float(record["x_coor"]),
"y": _optional_float(record["y_coor"]),
}
for record in records
]
-36
View File
@@ -1,36 +0,0 @@
from typing import List, Optional, Any
from psycopg import AsyncConnection
class ScadaRepository:
@staticmethod
async def get_scadas(conn: AsyncConnection) -> List[dict]:
"""
查询pg数据库中,scada_info 的所有记录
:param conn: 异步数据库连接
:return: 包含所有记录的列表,每条记录为一个字典
"""
async with conn.cursor() as cur:
await cur.execute(
"""
SELECT id, type, associated_element_id, transmission_mode, transmission_frequency, reliability
FROM public.scada_info
"""
)
records = await cur.fetchall()
# 将查询结果转换为字典列表(假设 record 是字典)
scada_infos = []
for record in records:
scada_infos.append(
{
"id": record["id"], # 使用字典键
"type": record["type"],
"associated_element_id": record["associated_element_id"],
"transmission_mode": record["transmission_mode"],
"transmission_frequency": record["transmission_frequency"],
"reliability": record["reliability"],
}
)
return scada_infos
-1
View File
@@ -1,3 +1,2 @@
from .database import * from .database import *
from .timescaledb_info import *
from .composite_queries import CompositeQueries from .composite_queries import CompositeQueries
+155 -154
View File
@@ -1,15 +1,16 @@
import time import time
from typing import List, Optional, Any, Dict, Tuple
from datetime import datetime, timedelta from datetime import datetime, timedelta
from psycopg import AsyncConnection from typing import Any, Dict, List, Optional, Tuple
import pandas as pd
import numpy as np import numpy as np
import pandas as pd
from psycopg import AsyncConnection
import app.native.wndb as wndb
from app.algorithms.cleaning.flow import clean_flow_data_df_kf from app.algorithms.cleaning.flow import clean_flow_data_df_kf
from app.algorithms.cleaning.pressure import clean_pressure_data_df_km from app.algorithms.cleaning.pressure import clean_pressure_data_df_km
from app.algorithms.health.analyzer import PipelineHealthAnalyzer from app.algorithms.health.analyzer import PipelineHealthAnalyzer
from app.infra.db.postgresql.scada import ScadaInfoRepository
from app.infra.db.postgresql.internal_queries import InternalQueries
from app.infra.db.postgresql.scada_info import ScadaRepository as PostgreScadaRepository
from app.infra.db.timescaledb.repositories.realtime import RealtimeRepository from app.infra.db.timescaledb.repositories.realtime import RealtimeRepository
from app.infra.db.timescaledb.repositories.scheme import SchemeRepository from app.infra.db.timescaledb.repositories.scheme import SchemeRepository
from app.infra.db.timescaledb.repositories.scada import ScadaRepository from app.infra.db.timescaledb.repositories.scada import ScadaRepository
@@ -20,6 +21,13 @@ class CompositeQueries:
复合查询类提供跨表查询功能 复合查询类提供跨表查询功能
""" """
@staticmethod
async def _get_project_scada_index(
postgres_conn: AsyncConnection,
) -> Dict[str, Dict[str, Any]]:
scadas = await ScadaInfoRepository.get_scadas(postgres_conn)
return {scada["id"]: scada for scada in scadas}
@staticmethod @staticmethod
async def get_scada_associated_realtime_simulation_data( async def get_scada_associated_realtime_simulation_data(
timescale_conn: AsyncConnection, timescale_conn: AsyncConnection,
@@ -48,30 +56,22 @@ class CompositeQueries:
ValueError: SCADA 设备未找到或字段无效时 ValueError: SCADA 设备未找到或字段无效时
""" """
result = {} result = {}
# 1. 查询所有 SCADA 信息 scada_by_id = await CompositeQueries._get_project_scada_index(postgres_conn)
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
for device_id in device_ids: for device_id in device_ids:
# 2. 根据 device_id 找到对应的 SCADA 信息 target_scada = scada_by_id.get(device_id)
target_scada = None
for scada in scada_infos:
if scada["id"] == device_id:
target_scada = scada
break
if not target_scada: if not target_scada:
raise ValueError(f"SCADA device {device_id} not found") raise ValueError(f"SCADA device {device_id} not found")
# 3. 根据 type 和 associated_element_id 查询对应的模拟数据
element_id = target_scada["associated_element_id"] element_id = target_scada["associated_element_id"]
scada_type = target_scada["type"] scada_type = target_scada["type"]
if scada_type.lower() == "pipe_flow": if scada_type == "pipe_flow":
# 查询 link 模拟数据 # 查询 link 模拟数据
res = await RealtimeRepository.get_link_field_by_time_range( res = await RealtimeRepository.get_link_field_by_time_range(
timescale_conn, start_time, end_time, element_id, "flow" timescale_conn, start_time, end_time, element_id, "flow"
) )
elif scada_type.lower() == "pressure": elif scada_type == "pressure":
# 查询 node 模拟数据 # 查询 node 模拟数据
res = await RealtimeRepository.get_node_field_by_time_range( res = await RealtimeRepository.get_node_field_by_time_range(
timescale_conn, start_time, end_time, element_id, "pressure" timescale_conn, start_time, end_time, element_id, "pressure"
@@ -114,25 +114,17 @@ class CompositeQueries:
ValueError: SCADA 设备未找到或字段无效时 ValueError: SCADA 设备未找到或字段无效时
""" """
result = {} result = {}
# 1. 查询所有 SCADA 信息 scada_by_id = await CompositeQueries._get_project_scada_index(postgres_conn)
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
for device_id in device_ids: for device_id in device_ids:
# 2. 根据 device_id 找到对应的 SCADA 信息 target_scada = scada_by_id.get(device_id)
target_scada = None
for scada in scada_infos:
if scada["id"] == device_id:
target_scada = scada
break
if not target_scada: if not target_scada:
raise ValueError(f"SCADA device {device_id} not found") raise ValueError(f"SCADA device {device_id} not found")
# 3. 根据 type 和 associated_element_id 查询对应的模拟数据
element_id = target_scada["associated_element_id"] element_id = target_scada["associated_element_id"]
scada_type = target_scada["type"] scada_type = target_scada["type"]
if scada_type.lower() == "pipe_flow": if scada_type == "pipe_flow":
# 查询 link 模拟数据 # 查询 link 模拟数据
res = await SchemeRepository.get_link_field_by_scheme_and_time_range( res = await SchemeRepository.get_link_field_by_scheme_and_time_range(
timescale_conn, timescale_conn,
@@ -143,7 +135,7 @@ class CompositeQueries:
element_id, element_id,
"flow", "flow",
) )
elif scada_type.lower() == "pressure": elif scada_type == "pressure":
# 查询 node 模拟数据 # 查询 node 模拟数据
res = await SchemeRepository.get_node_field_by_scheme_and_time_range( res = await SchemeRepository.get_node_field_by_scheme_and_time_range(
timescale_conn, timescale_conn,
@@ -165,19 +157,19 @@ class CompositeQueries:
@staticmethod @staticmethod
async def get_realtime_simulation_data( async def get_realtime_simulation_data(
timescale_conn: AsyncConnection, timescale_conn: AsyncConnection,
featureInfos: List[Tuple[str, str]], feature_infos: List[Tuple[str, str]],
start_time: datetime, start_time: datetime,
end_time: datetime, end_time: datetime,
) -> Dict[str, List[Dict[str, Any]]]: ) -> Dict[str, List[Dict[str, Any]]]:
""" """
获取 link/node 模拟值 获取 link/node 模拟值
根据传入的 featureInfos找到关联的 link/node 根据传入的 feature_infos找到关联的 link/node
并根据对应的 type查询对应的模拟数据 并根据对应的 type查询对应的模拟数据
Args: Args:
timescale_conn: TimescaleDB 异步连接 timescale_conn: TimescaleDB 异步连接
featureInfos: 传入的 feature 信息列表包含 (element_id, type) feature_infos: 传入的 feature 信息列表包含 (element_id, type)
start_time: 开始时间 start_time: 开始时间
end_time: 结束时间 end_time: 结束时间
@@ -188,20 +180,20 @@ class CompositeQueries:
ValueError: SCADA 设备未找到或字段无效时 ValueError: SCADA 设备未找到或字段无效时
""" """
result = {} result = {}
for feature_id, type in featureInfos: for feature_id, feature_type in feature_infos:
if type.lower() == "pipe": if feature_type.lower() == "pipe":
# 查询 link 模拟数据 # 查询 link 模拟数据
res = await RealtimeRepository.get_link_field_by_time_range( res = await RealtimeRepository.get_link_field_by_time_range(
timescale_conn, start_time, end_time, feature_id, "flow" timescale_conn, start_time, end_time, feature_id, "flow"
) )
elif type.lower() == "junction": elif feature_type.lower() == "junction":
# 查询 node 模拟数据 # 查询 node 模拟数据
res = await RealtimeRepository.get_node_field_by_time_range( res = await RealtimeRepository.get_node_field_by_time_range(
timescale_conn, start_time, end_time, feature_id, "pressure" timescale_conn, start_time, end_time, feature_id, "pressure"
) )
else: else:
raise ValueError(f"Unknown type: {type}") raise ValueError(f"Unknown type: {feature_type}")
# 添加 scada_id 到每个数据项 # 添加 scada_id 到每个数据项
for item in res: for item in res:
item["feature_id"] = feature_id item["feature_id"] = feature_id
@@ -211,7 +203,7 @@ class CompositeQueries:
@staticmethod @staticmethod
async def get_scheme_simulation_data( async def get_scheme_simulation_data(
timescale_conn: AsyncConnection, timescale_conn: AsyncConnection,
featureInfos: List[Tuple[str, str]], feature_infos: List[Tuple[str, str]],
start_time: datetime, start_time: datetime,
end_time: datetime, end_time: datetime,
scheme_type: str, scheme_type: str,
@@ -220,12 +212,12 @@ class CompositeQueries:
""" """
获取 link/node scheme 模拟值 获取 link/node scheme 模拟值
根据传入的 featureInfos找到关联的 link/node 根据传入的 feature_infos找到关联的 link/node
并根据对应的 type查询对应的模拟数据 并根据对应的 type查询对应的模拟数据
Args: Args:
timescale_conn: TimescaleDB 异步连接 timescale_conn: TimescaleDB 异步连接
featureInfos: 传入的 feature 信息列表包含 (element_id, type) feature_infos: 传入的 feature 信息列表包含 (element_id, type)
start_time: 开始时间 start_time: 开始时间
end_time: 结束时间 end_time: 结束时间
scheme_type: 工况类型 scheme_type: 工况类型
@@ -238,8 +230,8 @@ class CompositeQueries:
ValueError: 当类型无效时 ValueError: 当类型无效时
""" """
result = {} result = {}
for feature_id, type in featureInfos: for feature_id, feature_type in feature_infos:
if type.lower() == "pipe": if feature_type.lower() == "pipe":
# 查询 link 模拟数据 # 查询 link 模拟数据
res = await SchemeRepository.get_link_field_by_scheme_and_time_range( res = await SchemeRepository.get_link_field_by_scheme_and_time_range(
timescale_conn, timescale_conn,
@@ -250,7 +242,7 @@ class CompositeQueries:
feature_id, feature_id,
"flow", "flow",
) )
elif type.lower() == "junction": elif feature_type.lower() == "junction":
# 查询 node 模拟数据 # 查询 node 模拟数据
res = await SchemeRepository.get_node_field_by_scheme_and_time_range( res = await SchemeRepository.get_node_field_by_scheme_and_time_range(
timescale_conn, timescale_conn,
@@ -262,7 +254,7 @@ class CompositeQueries:
"pressure", "pressure",
) )
else: else:
raise ValueError(f"Unknown type: {type}") raise ValueError(f"Unknown type: {feature_type}")
# 添加 feature_id 到每个数据项 # 添加 feature_id 到每个数据项
for item in res: for item in res:
item["feature_id"] = feature_id item["feature_id"] = feature_id
@@ -299,32 +291,27 @@ class CompositeQueries:
ValueError: 当元素类型无效时 ValueError: 当元素类型无效时
""" """
# 1. 查询所有 SCADA 信息 scada_by_id = await CompositeQueries._get_project_scada_index(postgres_conn)
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn) associated_scada = next(
(
# 2. 根据 element_type 和 element_id 找到关联的 SCADA 设备 scada
associated_scada = None for scada in scada_by_id.values()
for scada in scada_infos: if scada["associated_element_id"] == element_id
if scada["associated_element_id"] == element_id: ),
associated_scada = scada None,
break )
if not associated_scada: if not associated_scada:
# 没有找到关联的 SCADA 设备
return None return None
# 3. 通过 SCADA device_id 获取监测数据
device_id = associated_scada["id"] device_id = associated_scada["id"]
# 根据 use_cleaned 参数选择字段
data_field = "cleaned_value" if use_cleaned else "monitored_value" data_field = "cleaned_value" if use_cleaned else "monitored_value"
# 保证 device_id 以列表形式传递
res = await ScadaRepository.get_scada_field_by_id_time_range( res = await ScadaRepository.get_scada_field_by_id_time_range(
timescale_conn, [device_id], start_time, end_time, data_field timescale_conn, [device_id], start_time, end_time, data_field
) )
# 将 device_id 替换为 element_id 返回
return {element_id: res.get(device_id, [])} return {element_id: res.get(device_id, [])}
@staticmethod @staticmethod
@@ -348,107 +335,124 @@ class CompositeQueries:
end_time: 结束时间 end_time: 结束时间
Returns: Returns:
"success" 或错误信息 "success"
Raises:
ValueError: 当前项目没有可清洗设备或指定时间范围内没有监测数据
""" """
try: scada_by_id = await CompositeQueries._get_project_scada_index(postgres_conn)
# 获取所有 SCADA 信息 supported_types = {"pressure", "pipe_flow", "flow"}
scada_infos = await PostgreScadaRepository.get_scadas(postgres_conn)
# 将列表转换为字典,以 device_id 为键
scada_device_info_dict = {info["id"]: info for info in scada_infos}
# 如果 device_ids 为空,则处理所有 SCADA 设备 if device_ids:
if not device_ids: device_ids = [str(device_id).strip() for device_id in device_ids]
device_ids = list(scada_device_info_dict.keys()) missing_metadata_ids = [
device_id
for device_id in device_ids
if device_id not in scada_by_id
]
if missing_metadata_ids:
raise ValueError(
f"当前项目中有 {len(missing_metadata_ids)} 个 SCADA 设备缺少元数据"
)
# 批量查询所有设备的数据 unsupported_ids = [
data = await ScadaRepository.get_scada_field_by_id_time_range( device_id
timescale_conn, device_ids, start_time, end_time, "monitored_value" for device_id in device_ids
if scada_by_id[device_id]["type"] not in supported_types
]
if unsupported_ids:
raise ValueError(
f"当前项目中有 {len(unsupported_ids)} 个 SCADA 设备类型不支持清洗"
)
else:
device_ids = [
device_id
for device_id, info in scada_by_id.items()
if info["type"] in supported_types
]
if not device_ids:
raise ValueError("当前项目没有可清洗的 SCADA 设备")
data = await ScadaRepository.get_scada_field_by_id_time_range(
timescale_conn, device_ids, start_time, end_time, "monitored_value"
)
if not data:
raise ValueError("指定时间范围内没有 SCADA 监测数据")
normalized_data = {
str(device_id): records for device_id, records in data.items()
}
missing_data_ids = [
device_id for device_id in device_ids if not normalized_data.get(device_id)
]
if missing_data_ids:
raise ValueError(
f"指定时间范围内有 {len(missing_data_ids)} 个 SCADA 设备没有监测数据"
) )
if not data: all_records = [
return "error: fetch none scada data" # 没有数据,直接返回 {
"time": record["time"],
"device_id": device_id,
"value": record["value"],
}
for device_id, records in normalized_data.items()
for record in records
]
if not all_records:
raise ValueError("指定时间范围内没有 SCADA 监测数据")
# 将嵌套字典转换为 DataFrame,使用 time 作为索引 df_long = pd.DataFrame(all_records)
# data 格式: {device_id: [{"time": "...", "value": ...}, ...]} df = df_long.pivot(index="time", columns="device_id", values="value")
all_records = []
for device_id, records in data.items(): pressure_ids = [
for record in records: device_id
all_records.append( for device_id in df.columns
{ if scada_by_id[device_id]["type"] == "pressure"
"time": record["time"], ]
"device_id": device_id, flow_ids = [
"value": record["value"], device_id
} for device_id in df.columns
if scada_by_id[device_id]["type"] in {"pipe_flow", "flow"}
]
updated_rows = 0
for grouped_ids, cleaning_function in (
(pressure_ids, clean_pressure_data_df_km),
(flow_ids, clean_flow_data_df_kf),
):
if not grouped_ids:
continue
source_df = df[grouped_ids].reset_index()
cleaned_df = cleaning_function(source_df)
time_values = cleaned_df["time"].tolist()
for device_id in grouped_ids:
if device_id not in cleaned_df.columns:
raise ValueError(f"设备 {device_id} 的清洗结果缺少数据列")
cleaned_values = cleaned_df[device_id].tolist()
for time_value, value in zip(time_values, cleaned_values):
time_dt = (
time_value
if isinstance(time_value, datetime)
else datetime.fromisoformat(str(time_value))
) )
await ScadaRepository.update_scada_field(
timescale_conn,
time_dt,
device_id,
"cleaned_value",
value,
)
updated_rows += 1
if not all_records: if updated_rows == 0:
return "error: fetch none scada data" # 没有数据,直接返回 raise ValueError("SCADA 数据清洗未产生任何数据库更新")
# 创建 DataFrame 并透视,使 device_id 成为列 return "success"
df_long = pd.DataFrame(all_records)
df = df_long.pivot(index="time", columns="device_id", values="value")
# 根据type分类设备
pressure_ids = [
id
for id in df.columns
if scada_device_info_dict.get(id, {}).get("type") == "pressure"
]
flow_ids = [
id
for id in df.columns
if scada_device_info_dict.get(id, {}).get("type") == "pipe_flow"
]
# 处理pressure数据
if pressure_ids:
pressure_df = df[pressure_ids]
# 重置索引,将 time 变为普通列
pressure_df = pressure_df.reset_index()
# 调用清洗方法
cleaned_df = clean_pressure_data_df_km(pressure_df)
# 将清洗后的数据写回数据库
for device_id in pressure_ids:
if device_id in cleaned_df.columns:
cleaned_values = cleaned_df[device_id].tolist()
time_values = cleaned_df["time"].tolist()
for i, time_str in enumerate(time_values):
time_dt = datetime.fromisoformat(time_str)
value = cleaned_values[i]
await ScadaRepository.update_scada_field(
timescale_conn,
time_dt,
device_id,
"cleaned_value",
value,
)
# 处理flow数据
if flow_ids:
flow_df = df[flow_ids]
# 重置索引,将 time 变为普通列
flow_df = flow_df.reset_index()
# 调用清洗方法
cleaned_df = clean_flow_data_df_kf(flow_df)
# 将清洗后的数据写回数据库
for device_id in flow_ids:
if device_id in cleaned_df.columns:
cleaned_values = cleaned_df[device_id].tolist()
time_values = cleaned_df["time"].tolist()
for i, time_str in enumerate(time_values):
time_dt = datetime.fromisoformat(time_str)
value = cleaned_values[i]
await ScadaRepository.update_scada_field(
timescale_conn,
time_dt,
device_id,
"cleaned_value",
value,
)
return "success"
except Exception as e:
return f"error: {str(e)}"
@staticmethod @staticmethod
async def predict_pipeline_health( async def predict_pipeline_health(
@@ -493,10 +497,7 @@ class CompositeQueries:
# 批量查询这些管道的详细信息 # 批量查询这些管道的详细信息
fields = ["id", "diameter", "node1", "node2"] fields = ["id", "diameter", "node1", "node2"]
all_links = InternalQueries.get_links_by_property( all_links = wndb.get_pipes_by_property(network_name, fields=fields)
fields=fields,
db_name=network_name,
)
# 转换为字典以快速查找 # 转换为字典以快速查找
links_dict = {link["id"]: link for link in all_links} links_dict = {link["id"]: link for link in all_links}
+5 -5
View File
@@ -3,7 +3,7 @@ from contextlib import asynccontextmanager
from typing import AsyncGenerator, Dict, Optional from typing import AsyncGenerator, Dict, Optional
import psycopg_pool import psycopg_pool
from psycopg.rows import dict_row from psycopg.rows import dict_row
import app.infra.db.timescaledb.timescaledb_info as timescaledb_info from app.core.config import get_timescaledb_pgconn_string
# Configure logging # Configure logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -21,9 +21,9 @@ class Database:
# Get connection string, handling default case where target_db_name might be None # Get connection string, handling default case where target_db_name might be None
if target_db_name: if target_db_name:
conn_string = timescaledb_info.get_pgconn_string(db_name=target_db_name) conn_string = get_timescaledb_pgconn_string(db_name=target_db_name)
else: else:
conn_string = timescaledb_info.get_pgconn_string() conn_string = get_timescaledb_pgconn_string()
try: try:
self.pool = psycopg_pool.AsyncConnectionPool( self.pool = psycopg_pool.AsyncConnectionPool(
@@ -54,8 +54,8 @@ class Database:
"""Get the TimescaleDB connection string.""" """Get the TimescaleDB connection string."""
target_db_name = db_name or self.db_name target_db_name = db_name or self.db_name
if target_db_name: if target_db_name:
return timescaledb_info.get_pgconn_string(db_name=target_db_name) return get_timescaledb_pgconn_string(db_name=target_db_name)
return timescaledb_info.get_pgconn_string() return get_timescaledb_pgconn_string()
@asynccontextmanager @asynccontextmanager
async def get_connection(self) -> AsyncGenerator: async def get_connection(self) -> AsyncGenerator:
+38 -40
View File
@@ -6,10 +6,11 @@ import psycopg
from psycopg import sql from psycopg import sql
from psycopg.rows import dict_row from psycopg.rows import dict_row
import time import time
from app.core.config import get_timescaledb_pgconn_string
from app.infra.db.timescaledb.repositories.scheme import SchemeRepository from app.infra.db.timescaledb.repositories.scheme import SchemeRepository
from app.infra.db.timescaledb.repositories.realtime import RealtimeRepository from app.infra.db.timescaledb.repositories.realtime import RealtimeRepository
import app.infra.db.timescaledb.timescaledb_info as timescaledb_info
from app.infra.db.timescaledb.repositories.scada import ScadaRepository from app.infra.db.timescaledb.repositories.scada import ScadaRepository
from app.services.time_api import parse_utc_time
class InternalStorage: class InternalStorage:
@@ -25,9 +26,9 @@ class InternalStorage:
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
conn_string = ( conn_string = (
timescaledb_info.get_pgconn_string(db_name=db_name) get_timescaledb_pgconn_string(db_name=db_name)
if db_name if db_name
else timescaledb_info.get_pgconn_string() else get_timescaledb_pgconn_string()
) )
with psycopg.Connection.connect(conn_string) as conn: with psycopg.Connection.connect(conn_string) as conn:
RealtimeRepository.store_realtime_simulation_result_sync( RealtimeRepository.store_realtime_simulation_result_sync(
@@ -49,6 +50,7 @@ class InternalStorage:
link_result_list: List[dict], link_result_list: List[dict],
result_start_time: str, result_start_time: str,
num_periods: int = 1, num_periods: int = 1,
result_timestep_seconds: int | None = None,
db_name: str = None, db_name: str = None,
max_retries: int = 3, max_retries: int = 3,
): ):
@@ -56,9 +58,9 @@ class InternalStorage:
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
conn_string = ( conn_string = (
timescaledb_info.get_pgconn_string(db_name=db_name) get_timescaledb_pgconn_string(db_name=db_name)
if db_name if db_name
else timescaledb_info.get_pgconn_string() else get_timescaledb_pgconn_string()
) )
with psycopg.Connection.connect(conn_string) as conn: with psycopg.Connection.connect(conn_string) as conn:
SchemeRepository.store_scheme_simulation_result_sync( SchemeRepository.store_scheme_simulation_result_sync(
@@ -69,6 +71,7 @@ class InternalStorage:
link_result_list, link_result_list,
result_start_time, result_start_time,
num_periods, num_periods,
result_timestep_seconds,
) )
break # 成功 break # 成功
except Exception as e: except Exception as e:
@@ -89,17 +92,16 @@ class InternalQueries:
) -> dict: ) -> dict:
"""查询指定时间点的 SCADA 数据""" """查询指定时间点的 SCADA 数据"""
# 解析时间,假设是北京时间 target_time = parse_utc_time(query_time, field_name="query_time")
beijing_time = datetime.fromisoformat(query_time) start_time = target_time - timedelta(seconds=1)
start_time = beijing_time - timedelta(seconds=1) end_time = target_time + timedelta(seconds=1)
end_time = beijing_time + timedelta(seconds=1)
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
conn_string = ( conn_string = (
timescaledb_info.get_pgconn_string(db_name=db_name) get_timescaledb_pgconn_string(db_name=db_name)
if db_name if db_name
else timescaledb_info.get_pgconn_string() else get_timescaledb_pgconn_string()
) )
with psycopg.Connection.connect(conn_string) as conn: with psycopg.Connection.connect(conn_string) as conn:
rows = ScadaRepository.get_scada_by_ids_time_range_sync( rows = ScadaRepository.get_scada_by_ids_time_range_sync(
@@ -132,21 +134,15 @@ class InternalQueries:
max_retries: int = 3, max_retries: int = 3,
) -> dict[str, list[dict]]: ) -> dict[str, list[dict]]:
"""查询指定时间窗的 SCADA 数据,返回 {device_id: [{time, value}, ...]}。""" """查询指定时间窗的 SCADA 数据,返回 {device_id: [{time, value}, ...]}。"""
start_dt = ( start_dt = parse_utc_time(start_time, field_name="start_time")
datetime.fromisoformat(start_time) end_dt = parse_utc_time(end_time, field_name="end_time")
if isinstance(start_time, str)
else start_time
)
end_dt = (
datetime.fromisoformat(end_time) if isinstance(end_time, str) else end_time
)
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
conn_string = ( conn_string = (
timescaledb_info.get_pgconn_string(db_name=db_name) get_timescaledb_pgconn_string(db_name=db_name)
if db_name if db_name
else timescaledb_info.get_pgconn_string() else get_timescaledb_pgconn_string()
) )
with psycopg.Connection.connect(conn_string) as conn: with psycopg.Connection.connect(conn_string) as conn:
rows = ScadaRepository.get_scada_by_ids_time_range_sync( rows = ScadaRepository.get_scada_by_ids_time_range_sync(
@@ -235,17 +231,18 @@ class InternalQueries:
scheme_type: str | None = None, scheme_type: str | None = None,
scheme_name: str | None = None, scheme_name: str | None = None,
) -> dict[str, list[dict]]: ) -> dict[str, list[dict]]:
if not element_ids: normalized_element_ids = list(
dict.fromkeys(
normalized
for normalized in (str(element_id).strip() for element_id in element_ids)
if normalized
)
)
if not normalized_element_ids:
return {} return {}
start_dt = ( start_dt = parse_utc_time(start_time, field_name="start_time")
datetime.fromisoformat(start_time) end_dt = parse_utc_time(end_time, field_name="end_time")
if isinstance(start_time, str)
else start_time
)
end_dt = (
datetime.fromisoformat(end_time) if isinstance(end_time, str) else end_time
)
table_name, valid_fields = InternalQueries._resolve_simulation_table(element_type) table_name, valid_fields = InternalQueries._resolve_simulation_table(element_type)
if field not in valid_fields: if field not in valid_fields:
raise ValueError(f"Invalid field for {element_type}: {field}") raise ValueError(f"Invalid field for {element_type}: {field}")
@@ -257,17 +254,17 @@ class InternalQueries:
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
conn_string = ( conn_string = (
timescaledb_info.get_pgconn_string(db_name=db_name) get_timescaledb_pgconn_string(db_name=db_name)
if db_name if db_name
else timescaledb_info.get_pgconn_string() else get_timescaledb_pgconn_string()
) )
with psycopg.Connection.connect(conn_string) as conn: with psycopg.Connection.connect(conn_string) as conn:
with conn.cursor(row_factory=dict_row) as cur: with conn.cursor(row_factory=dict_row) as cur:
if schema_name == "scheme": if schema_name == "scheme":
query = sql.SQL( query = sql.SQL(
"SELECT id, time, {} FROM {}.{} " "SELECT btrim(id::text) AS id, time, {} FROM {}.{} "
"WHERE scheme_type = %s AND scheme_name = %s " "WHERE scheme_type = %s AND scheme_name = %s "
"AND time >= %s AND time <= %s AND id = ANY(%s)" "AND time >= %s AND time <= %s AND btrim(id::text) = ANY(%s)"
).format( ).format(
sql.Identifier(field), sql.Identifier(field),
sql.Identifier(schema_name), sql.Identifier(schema_name),
@@ -280,25 +277,26 @@ class InternalQueries:
scheme_name, scheme_name,
start_dt, start_dt,
end_dt, end_dt,
element_ids, normalized_element_ids,
), ),
) )
else: else:
query = sql.SQL( query = sql.SQL(
"SELECT id, time, {} FROM {}.{} " "SELECT btrim(id::text) AS id, time, {} FROM {}.{} "
"WHERE time >= %s AND time <= %s AND id = ANY(%s)" "WHERE time >= %s AND time <= %s AND btrim(id::text) = ANY(%s)"
).format( ).format(
sql.Identifier(field), sql.Identifier(field),
sql.Identifier(schema_name), sql.Identifier(schema_name),
sql.Identifier(table_name), sql.Identifier(table_name),
) )
cur.execute(query, (start_dt, end_dt, element_ids)) cur.execute(query, (start_dt, end_dt, normalized_element_ids))
rows = cur.fetchall() rows = cur.fetchall()
result: dict[str, list[dict]] = { result: dict[str, list[dict]] = {
element_id: [] for element_id in element_ids element_id: [] for element_id in normalized_element_ids
} }
for row in rows: for row in rows:
result.setdefault(row["id"], []).append( element_id = str(row["id"]).strip()
result.setdefault(element_id, []).append(
{"time": row["time"].isoformat(), "value": row[field]} {"time": row["time"].isoformat(), "value": row[field]}
) )
for element_id in result: for element_id in result:
@@ -1,10 +1,8 @@
from typing import List, Any, Dict from typing import List, Any, Dict
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta
from collections import defaultdict from collections import defaultdict
from psycopg import AsyncConnection, Connection, sql from psycopg import AsyncConnection, Connection, sql
from app.services.time_api import parse_utc_time
# 定义UTC+8时区
UTC_8 = timezone(timedelta(hours=8))
class RealtimeRepository: class RealtimeRepository:
@@ -102,10 +100,12 @@ class RealtimeRepository:
async def get_links_by_time_range( async def get_links_by_time_range(
conn: AsyncConnection, start_time: datetime, end_time: datetime conn: AsyncConnection, start_time: datetime, end_time: datetime
) -> List[dict]: ) -> List[dict]:
normalized_start_time = parse_utc_time(start_time, field_name="start_time")
normalized_end_time = parse_utc_time(end_time, field_name="end_time")
async with conn.cursor() as cur: async with conn.cursor() as cur:
await cur.execute( await cur.execute(
"SELECT * FROM realtime.link_simulation WHERE time >= %s AND time <= %s", "SELECT * FROM realtime.link_simulation WHERE time >= %s AND time <= %s",
(start_time, end_time), (normalized_start_time, normalized_end_time),
) )
return await cur.fetchall() return await cur.fetchall()
@@ -298,10 +298,12 @@ class RealtimeRepository:
async def get_nodes_by_time_range( async def get_nodes_by_time_range(
conn: AsyncConnection, start_time: datetime, end_time: datetime conn: AsyncConnection, start_time: datetime, end_time: datetime
) -> List[dict]: ) -> List[dict]:
normalized_start_time = parse_utc_time(start_time, field_name="start_time")
normalized_end_time = parse_utc_time(end_time, field_name="end_time")
async with conn.cursor() as cur: async with conn.cursor() as cur:
await cur.execute( await cur.execute(
"SELECT * FROM realtime.node_simulation WHERE time >= %s AND time <= %s", "SELECT * FROM realtime.node_simulation WHERE time >= %s AND time <= %s",
(start_time, end_time), (normalized_start_time, normalized_end_time),
) )
return await cur.fetchall() return await cur.fetchall()
@@ -397,24 +399,9 @@ class RealtimeRepository:
link_result_list: List of link simulation results link_result_list: List of link simulation results
result_start_time: Start time for the results (ISO format string) result_start_time: Start time for the results (ISO format string)
""" """
# Convert result_start_time string to datetime if needed simulation_time = parse_utc_time(
if isinstance(result_start_time, str): result_start_time, field_name="result_start_time"
# 如果是ISO格式字符串,解析并转换为UTC+8 )
if result_start_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(
result_start_time.replace("Z", "+00:00")
)
simulation_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
simulation_time = datetime.fromisoformat(result_start_time)
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
else:
simulation_time = result_start_time
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
# Prepare node data for batch insert # Prepare node data for batch insert
node_data = [] node_data = []
@@ -475,24 +462,9 @@ class RealtimeRepository:
link_result_list: List of link simulation results link_result_list: List of link simulation results
result_start_time: Start time for the results (ISO format string) result_start_time: Start time for the results (ISO format string)
""" """
# Convert result_start_time string to datetime if needed simulation_time = parse_utc_time(
if isinstance(result_start_time, str): result_start_time, field_name="result_start_time"
# 如果是ISO格式字符串,解析并转换为UTC+8 )
if result_start_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(
result_start_time.replace("Z", "+00:00")
)
simulation_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
simulation_time = datetime.fromisoformat(result_start_time)
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
else:
simulation_time = result_start_time
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
# Prepare node data for batch insert # Prepare node data for batch insert
node_data = [] node_data = []
@@ -556,21 +528,7 @@ class RealtimeRepository:
Returns: Returns:
List of records matching the criteria List of records matching the criteria
""" """
# Convert query_time string to datetime target_time = parse_utc_time(query_time, field_name="query_time")
if isinstance(query_time, str):
if query_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(query_time.replace("Z", "+00:00"))
target_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
target_time = datetime.fromisoformat(query_time)
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
else:
target_time = query_time
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
# Create time range: query_time ± 1 second # Create time range: query_time ± 1 second
start_time = target_time - timedelta(seconds=1) start_time = target_time - timedelta(seconds=1)
@@ -614,21 +572,7 @@ class RealtimeRepository:
Returns: Returns:
List of records matching the criteria List of records matching the criteria
""" """
# Convert query_time string to datetime target_time = parse_utc_time(query_time, field_name="query_time")
if isinstance(query_time, str):
if query_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(query_time.replace("Z", "+00:00"))
target_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
target_time = datetime.fromisoformat(query_time)
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
else:
target_time = query_time
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
# Create time range: query_time ± 1 second # Create time range: query_time ± 1 second
start_time = target_time - timedelta(seconds=1) start_time = target_time - timedelta(seconds=1)
@@ -89,12 +89,17 @@ class ScadaRepository:
if field not in valid_fields: if field not in valid_fields:
raise ValueError(f"Invalid field: {field}") raise ValueError(f"Invalid field: {field}")
query = sql.SQL( update_query = sql.SQL(
"UPDATE scada.scada_data SET {} = %s WHERE time = %s AND device_id = %s" "UPDATE scada.scada_data SET {} = %s WHERE time = %s AND device_id = %s"
).format(sql.Identifier(field)) ).format(sql.Identifier(field))
insert_query = sql.SQL(
"INSERT INTO scada.scada_data (time, device_id, {}) VALUES (%s, %s, %s)"
).format(sql.Identifier(field))
async with conn.cursor() as cur: async with conn.cursor() as cur:
await cur.execute(query, (value, time, device_id)) await cur.execute(update_query, (value, time, device_id))
if cur.rowcount == 0:
await cur.execute(insert_query, (time, device_id, value))
@staticmethod @staticmethod
async def delete_scada_by_id_time_range( async def delete_scada_by_id_time_range(
+40 -90
View File
@@ -1,14 +1,26 @@
from typing import List, Any, Dict from typing import List, Any, Dict
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta
from collections import defaultdict from collections import defaultdict
from psycopg import AsyncConnection, Connection, sql from psycopg import AsyncConnection, Connection, sql
import app.services.globals as globals import app.services.globals as globals
from app.services.time_api import parse_clock_duration_seconds, parse_utc_time
# 定义UTC+8时区
UTC_8 = timezone(timedelta(hours=8))
class SchemeRepository: class SchemeRepository:
@staticmethod
def _get_result_timestep(result_timestep_seconds: int | None) -> timedelta:
if result_timestep_seconds is not None:
if result_timestep_seconds <= 0:
raise ValueError("result_timestep_seconds must be greater than 0.")
return timedelta(seconds=result_timestep_seconds)
timestep_seconds = parse_clock_duration_seconds(
globals.hydraulic_timestep,
field_name="HYDRAULIC TIMESTEP",
)
if timestep_seconds <= 0:
raise ValueError("HYDRAULIC TIMESTEP must be greater than 0.")
return timedelta(seconds=timestep_seconds)
# --- Link Simulation --- # --- Link Simulation ---
@@ -454,6 +466,7 @@ class SchemeRepository:
link_result_list: List[Dict[str, any]], link_result_list: List[Dict[str, any]],
result_start_time: str, result_start_time: str,
num_periods: int = 1, num_periods: int = 1,
result_timestep_seconds: int | None = None,
): ):
""" """
Store scheme simulation results to TimescaleDB. Store scheme simulation results to TimescaleDB.
@@ -466,39 +479,20 @@ class SchemeRepository:
link_result_list: List of link simulation results link_result_list: List of link simulation results
result_start_time: Start time for the results (ISO format string) result_start_time: Start time for the results (ISO format string)
""" """
# Convert result_start_time string to datetime if needed simulation_time = parse_utc_time(
if isinstance(result_start_time, str): result_start_time, field_name="result_start_time"
# 如果是ISO格式字符串,解析并转换为UTC+8
if result_start_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(
result_start_time.replace("Z", "+00:00")
)
simulation_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
simulation_time = datetime.fromisoformat(result_start_time)
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
else:
simulation_time = result_start_time
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
timestep_parts = globals.hydraulic_timestep.split(":")
timestep = timedelta(
hours=int(timestep_parts[0]),
minutes=int(timestep_parts[1]),
seconds=int(timestep_parts[2]),
) )
timestep = SchemeRepository._get_result_timestep(result_timestep_seconds)
# Prepare node data for batch insert # Prepare node data for batch insert
node_data = [] node_data = []
for node_result in node_result_list: for node_result in node_result_list:
node_id = node_result.get("node") node_id = node_result.get("node")
for period_index in range(num_periods): result_rows = node_result.get("result", [])
for period_index in range(min(num_periods, len(result_rows))):
current_time = simulation_time + (timestep * period_index) current_time = simulation_time + (timestep * period_index)
data = node_result.get("result", [])[period_index] data = result_rows[period_index]
node_data.append( node_data.append(
{ {
"time": current_time, "time": current_time,
@@ -516,9 +510,10 @@ class SchemeRepository:
link_data = [] link_data = []
for link_result in link_result_list: for link_result in link_result_list:
link_id = link_result.get("link") link_id = link_result.get("link")
for period_index in range(num_periods): result_rows = link_result.get("result", [])
for period_index in range(min(num_periods, len(result_rows))):
current_time = simulation_time + (timestep * period_index) current_time = simulation_time + (timestep * period_index)
data = link_result.get("result", [])[period_index] data = result_rows[period_index]
link_data.append( link_data.append(
{ {
"time": current_time, "time": current_time,
@@ -552,6 +547,7 @@ class SchemeRepository:
link_result_list: List[Dict[str, any]], link_result_list: List[Dict[str, any]],
result_start_time: str, result_start_time: str,
num_periods: int = 1, num_periods: int = 1,
result_timestep_seconds: int | None = None,
): ):
""" """
Store scheme simulation results to TimescaleDB (sync version). Store scheme simulation results to TimescaleDB (sync version).
@@ -564,39 +560,20 @@ class SchemeRepository:
link_result_list: List of link simulation results link_result_list: List of link simulation results
result_start_time: Start time for the results (ISO format string) result_start_time: Start time for the results (ISO format string)
""" """
# Convert result_start_time string to datetime if needed simulation_time = parse_utc_time(
if isinstance(result_start_time, str): result_start_time, field_name="result_start_time"
# 如果是ISO格式字符串,解析并转换为UTC+8
if result_start_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(
result_start_time.replace("Z", "+00:00")
)
simulation_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
simulation_time = datetime.fromisoformat(result_start_time)
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
else:
simulation_time = result_start_time
if simulation_time.tzinfo is None:
simulation_time = simulation_time.replace(tzinfo=UTC_8)
timestep_parts = globals.hydraulic_timestep.split(":")
timestep = timedelta(
hours=int(timestep_parts[0]),
minutes=int(timestep_parts[1]),
seconds=int(timestep_parts[2]),
) )
timestep = SchemeRepository._get_result_timestep(result_timestep_seconds)
# Prepare node data for batch insert # Prepare node data for batch insert
node_data = [] node_data = []
for node_result in node_result_list: for node_result in node_result_list:
node_id = node_result.get("node") node_id = node_result.get("node")
for period_index in range(num_periods): result_rows = node_result.get("result", [])
for period_index in range(min(num_periods, len(result_rows))):
current_time = simulation_time + (timestep * period_index) current_time = simulation_time + (timestep * period_index)
data = node_result.get("result", [])[period_index] data = result_rows[period_index]
node_data.append( node_data.append(
{ {
"time": current_time, "time": current_time,
@@ -614,9 +591,10 @@ class SchemeRepository:
link_data = [] link_data = []
for link_result in link_result_list: for link_result in link_result_list:
link_id = link_result.get("link") link_id = link_result.get("link")
for period_index in range(num_periods): result_rows = link_result.get("result", [])
for period_index in range(min(num_periods, len(result_rows))):
current_time = simulation_time + (timestep * period_index) current_time = simulation_time + (timestep * period_index)
data = link_result.get("result", [])[period_index] data = result_rows[period_index]
link_data.append( link_data.append(
{ {
"time": current_time, "time": current_time,
@@ -664,21 +642,7 @@ class SchemeRepository:
Returns: Returns:
List of records matching the criteria List of records matching the criteria
""" """
# Convert query_time string to datetime target_time = parse_utc_time(query_time, field_name="query_time")
if isinstance(query_time, str):
if query_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(query_time.replace("Z", "+00:00"))
target_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
target_time = datetime.fromisoformat(query_time)
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
else:
target_time = query_time
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
# Create time range: query_time ± 1 second # Create time range: query_time ± 1 second
start_time = target_time - timedelta(seconds=1) start_time = target_time - timedelta(seconds=1)
@@ -727,21 +691,7 @@ class SchemeRepository:
Returns: Returns:
List of records matching the criteria List of records matching the criteria
""" """
# Convert query_time string to datetime target_time = parse_utc_time(query_time, field_name="query_time")
if isinstance(query_time, str):
if query_time.endswith("Z"):
# UTC时间,转换为UTC+8
utc_time = datetime.fromisoformat(query_time.replace("Z", "+00:00"))
target_time = utc_time.astimezone(UTC_8)
else:
# 假设已经是UTC+8时间
target_time = datetime.fromisoformat(query_time)
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
else:
target_time = query_time
if target_time.tzinfo is None:
target_time = target_time.replace(tzinfo=UTC_8)
# Create time range: query_time ± 1 second # Create time range: query_time ± 1 second
start_time = target_time - timedelta(seconds=1) start_time = target_time - timedelta(seconds=1)
@@ -1,36 +0,0 @@
from dotenv import load_dotenv
import os
load_dotenv()
pg_name = os.getenv("TIMESCALEDB_DB_NAME")
pg_host = os.getenv("TIMESCALEDB_DB_HOST")
pg_port = os.getenv("TIMESCALEDB_DB_PORT")
pg_user = os.getenv("TIMESCALEDB_DB_USER")
pg_password = os.getenv("TIMESCALEDB_DB_PASSWORD")
def get_pgconn_string(
db_name=pg_name,
db_host=pg_host,
db_port=pg_port,
db_user=pg_user,
db_password=pg_password,
):
"""返回 PostgreSQL 连接字符串"""
return f"dbname={db_name} host={db_host} port={db_port} user={db_user} password={db_password}"
def get_pg_config():
"""返回 PostgreSQL 配置变量的字典"""
return {
"name": pg_name,
"host": pg_host,
"port": pg_port,
"user": pg_user,
}
def get_pg_password():
"""返回密码(谨慎使用)"""
return pg_password
+83 -35
View File
@@ -8,6 +8,7 @@ from datetime import datetime
import subprocess import subprocess
import logging import logging
from typing import Any from typing import Any
import uuid
sys.path.append("..") sys.path.append("..")
from app.native.wndb import project from app.native.wndb import project
@@ -281,8 +282,6 @@ class Output:
def _dump_output(path: str) -> dict[str, Any]: def _dump_output(path: str) -> dict[str, Any]:
opt = Output(path) opt = Output(path)
data = opt.dump() data = opt.dump()
with open(path + ".json", "w") as f:
json.dump(data, f)
return data return data
@@ -302,25 +301,43 @@ def dump_output_binary(path: str) -> str:
return str(bast64_data, "utf-8") return str(bast64_data, "utf-8")
def _safe_remove(path: str) -> None:
try:
if os.path.exists(path):
os.remove(path)
except Exception:
logging.warning("failed to remove temp file: %s", path)
def _make_isolated_run_paths(base_name: str, cwd: str) -> tuple[str, str, str]:
# 确保保存临时文件的目录存在
db_inp_dir = os.path.join(cwd, "db_inp")
temp_dir = os.path.join(cwd, "temp")
os.makedirs(db_inp_dir, exist_ok=True)
os.makedirs(temp_dir, exist_ok=True)
# 进程号 + UUID 生成唯一后缀,避免并发进程互相覆盖临时文件。
token = f"{os.getpid()}_{uuid.uuid4().hex}"
inp = os.path.join(db_inp_dir, f"{base_name}.db.{token}.inp")
rpt = os.path.join(temp_dir, f"{base_name}.db.{token}.rpt")
opt = os.path.join(temp_dir, f"{base_name}.db.{token}.opt")
return inp, rpt, opt
# DingZQ, 2025-02-04, 返回dict[str, Any] # DingZQ, 2025-02-04, 返回dict[str, Any]
def run_project_return_dict(name: str, readable_output: bool = False) -> dict[str, Any]: def run_project_return_dict(name: str, readable_output: bool = True) -> dict[str, Any]:
if not project.have_project(name): if not project.have_project(name):
raise Exception(f"Not found project [{name}]") raise Exception(f"Not found project [{name}]")
dir = os.path.abspath(os.getcwd()) cwd = os.path.abspath(os.getcwd())
db_inp = os.path.join(os.path.join(dir, "db_inp"), name + ".db.inp") inp, rpt, opt = _make_isolated_run_paths(name, cwd)
inp_out.dump_inp(name, db_inp, "2") inp_out.dump_inp(name, inp, "2")
input = name + ".db"
if platform.system() == "Windows": if platform.system() == "Windows":
exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe") exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe")
else: else:
exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet") exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet")
inp = os.path.join(os.path.join(dir, "db_inp"), input + ".inp")
rpt = os.path.join(os.path.join(dir, "temp"), input + ".rpt")
opt = os.path.join(os.path.join(dir, "temp"), input + ".opt")
command = f"{exe} {inp} {rpt} {opt}"
if platform.system() != "Windows": if platform.system() != "Windows":
if not os.access(exe, os.X_OK): if not os.access(exe, os.X_OK):
@@ -334,12 +351,17 @@ def run_project_return_dict(name: str, readable_output: bool = False) -> dict[st
lib_dir = os.path.dirname(exe) lib_dir = os.path.dirname(exe)
env["LD_LIBRARY_PATH"] = f"{lib_dir}:{env.get('LD_LIBRARY_PATH', '')}" env["LD_LIBRARY_PATH"] = f"{lib_dir}:{env.get('LD_LIBRARY_PATH', '')}"
# 使用 subprocess 替代 os.system 以传递 env process = subprocess.run([exe, inp, rpt, opt], env=env, capture_output=True, text=True)
process = subprocess.run(command, shell=True, env=env)
result = process.returncode result = process.returncode
if result != 0: if result != 0:
logging.error(f"EPANET failed with return code {result}")
logging.error(f"EPANET stdout: {process.stdout}")
logging.error(f"EPANET stderr: {process.stderr}")
data["simulation_result"] = "failed" data["simulation_result"] = "failed"
data["error_code"] = result
data["stdout"] = process.stdout
data["stderr"] = process.stderr
else: else:
data["simulation_result"] = "successful" data["simulation_result"] = "successful"
if readable_output: if readable_output:
@@ -347,33 +369,41 @@ def run_project_return_dict(name: str, readable_output: bool = False) -> dict[st
else: else:
data["output"] = dump_output_binary(opt) data["output"] = dump_output_binary(opt)
data["report"] = dump_report(rpt) data["input_file"] = inp
data["report_file"] = rpt
data["output_file"] = opt
if os.path.exists(rpt):
data["report"] = dump_report(rpt)
else:
logging.error(f"EPANET report file not found: {rpt}")
data["report"] = f"Error: EPANET report file not found. Simulation return code: {result}. Check server logs for stdout/stderr."
# 返回内容后删除仿真临时文件,避免临时文件堆积。
_safe_remove(inp)
_safe_remove(rpt)
_safe_remove(opt)
return data return data
# original code # original code
def run_project(name: str, readable_output: bool = False) -> str: def run_project(name: str, readable_output: bool = True) -> str:
if not project.have_project(name): if not project.have_project(name):
raise Exception(f"Not found project [{name}]") raise Exception(f"Not found project [{name}]")
dir = os.path.abspath(os.getcwd()) cwd = os.path.abspath(os.getcwd())
db_inp = os.path.join(os.path.join(dir, "db_inp"), name + ".db.inp") inp, rpt, opt = _make_isolated_run_paths(name, cwd)
inp_out.dump_inp(name, db_inp, "2") inp_out.dump_inp(name, inp, "2")
input = name + ".db"
if platform.system() == "Windows": if platform.system() == "Windows":
exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe") exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe")
else: else:
exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet") exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet")
inp = os.path.join(os.path.join(dir, "db_inp"), input + ".inp")
rpt = os.path.join(os.path.join(dir, "temp"), input + ".rpt")
opt = os.path.join(os.path.join(dir, "temp"), input + ".opt")
command = f"{exe} {inp} {rpt} {opt}"
logging.info(f"Run simulation at {datetime.now()}") logging.info(f"Run simulation at {datetime.now()}")
logging.info(command) logging.info("%s %s %s %s", exe, inp, rpt, opt)
if platform.system() != "Windows": if platform.system() != "Windows":
if not os.access(exe, os.X_OK): if not os.access(exe, os.X_OK):
@@ -388,7 +418,7 @@ def run_project(name: str, readable_output: bool = False) -> str:
env["LD_LIBRARY_PATH"] = f"{lib_dir}:{env.get('LD_LIBRARY_PATH', '')}" env["LD_LIBRARY_PATH"] = f"{lib_dir}:{env.get('LD_LIBRARY_PATH', '')}"
# DingZQ, 2025-06-02, 使用subprocess替代os.system # DingZQ, 2025-06-02, 使用subprocess替代os.system
process = subprocess.run(command, shell=True, env=env) process = subprocess.run([exe, inp, rpt, opt], env=env)
result = process.returncode result = process.returncode
# logging.info(f"Simulation result: {result}") # logging.info(f"Simulation result: {result}")
@@ -402,27 +432,35 @@ def run_project(name: str, readable_output: bool = False) -> str:
logging.info("simulation successful") logging.info("simulation successful")
if readable_output: if readable_output:
data |= _dump_output(opt) data["output"] = _dump_output(opt)
else: else:
data["output"] = dump_output_binary(opt) data["output"] = dump_output_binary(opt)
data["input_file"] = inp
data["report_file"] = rpt
data["output_file"] = opt
data["report"] = dump_report(rpt) data["report"] = dump_report(rpt)
# 返回内容后删除仿真临时文件,避免临时文件堆积。
_safe_remove(inp)
_safe_remove(rpt)
_safe_remove(opt)
# logging.info(f"Report: {data['report']}") # logging.info(f"Report: {data['report']}")
return json.dumps(data) return json.dumps(data)
def run_inp(name: str) -> str: def run_inp(name: str, readable_output: bool = True) -> str:
dir = os.path.abspath(os.getcwd()) cwd = os.path.abspath(os.getcwd())
if platform.system() == "Windows": if platform.system() == "Windows":
exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe") exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe")
else: else:
exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet") exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet")
inp = os.path.join(os.path.join(dir, "inp"), name + ".inp") source_inp = os.path.join(cwd, "inp", name + ".inp")
rpt = os.path.join(os.path.join(dir, "temp"), name + ".rpt") token = f"{os.getpid()}_{uuid.uuid4().hex}"
opt = os.path.join(os.path.join(dir, "temp"), name + ".opt") rpt = os.path.join(cwd, "temp", f"{name}.{token}.rpt")
command = f"{exe} {inp} {rpt} {opt}" opt = os.path.join(cwd, "temp", f"{name}.{token}.opt")
if platform.system() != "Windows": if platform.system() != "Windows":
if not os.access(exe, os.X_OK): if not os.access(exe, os.X_OK):
@@ -436,16 +474,26 @@ def run_inp(name: str) -> str:
lib_dir = os.path.dirname(exe) lib_dir = os.path.dirname(exe)
env["LD_LIBRARY_PATH"] = f"{lib_dir}:{env.get('LD_LIBRARY_PATH', '')}" env["LD_LIBRARY_PATH"] = f"{lib_dir}:{env.get('LD_LIBRARY_PATH', '')}"
process = subprocess.run(command, shell=True, env=env) process = subprocess.run([exe, source_inp, rpt, opt], env=env)
result = process.returncode result = process.returncode
if result != 0: if result != 0:
data["simulation_result"] = "failed" data["simulation_result"] = "failed"
else: else:
data["simulation_result"] = "successful" data["simulation_result"] = "successful"
# data |= _dump_output(opt) if readable_output:
data["output"] = dump_output_binary(opt) data["output"] = _dump_output(opt)
else:
data["output"] = dump_output_binary(opt)
data["input_file"] = source_inp
data["report_file"] = rpt
data["output_file"] = opt
data["report"] = dump_report(rpt) data["report"] = dump_report(rpt)
# 返回内容后删除仿真临时文件,避免临时文件堆积。
_safe_remove(source_inp)
_safe_remove(rpt)
_safe_remove(opt)
return json.dumps(data) return json.dumps(data)

Some files were not shown because too many files have changed in this diff Show More