Files
2026-11-05 15:07:45 +08:00

514 lines
17 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 接口文档(REST API v1)
服务端:**FastAPI**(uvicorn 托管,ASGI)。本文件是**唯一的接口说明**。
Base URL:`http://127.0.0.1:5000/api/v1`
所有请求与响应均为 `application/json; charset=utf-8`(文件上传除外)。
**自动文档默认关闭**:`FastAPI(docs_url=None, redoc_url=None, openapi_url=None)`,
且 `/docs`、`/doc`、`/redoc`、`/swagger`、`/swagger-ui`、`/openapi.json`、`/api/docs`
都被显式占位成 **404**(返回下面的统一错误体,提示查看本文件)。
只有临时设了环境变量 `SZ_ENABLE_DOCS=1` 时,才会挂回 `/docs`、`/redoc`、`/openapi.json`。
所以**不要依赖 Swagger 传参**,字段以本文件为准。
---
## 0. 通用约定
### 0.1 错误响应
```json
{
"error": {
"code": "not_found",
"message": "资源不存在:/api/v1/xxx"
}
}
```
| code | HTTP | 含义 |
| --- | --- | --- |
| `bad_request` | 400 | 参数缺失或格式错误 |
| `not_found` | 404 | 资源/路径不存在 |
| `method_not_allowed` | 405 | HTTP 方法不支持 |
| `not_ready` | 409 | 任务未完成,文件还没生成 |
| `no_geometry` | 409 | 该小区在工参中缺少经纬度 |
| `payload_too_large` | 413 | 上传文件超过 200MB |
| `internal_error` | 500 | 服务端异常(日志里有堆栈) |
### 0.2 异步任务
入库、导出是耗时操作(全量入库约 1–5 分钟,导出约 10 秒),因此这两个接口
返回 **202 Accepted**,并在 `Location` 头里给出任务资源地址。客户端轮询任务资源,
直到 `status` 变为 `succeeded` 或 `failed`。
`status` 取值:`pending`(排队)→ `running`(执行中)→ `succeeded` / `failed`。
服务端用单线程池串行执行任务:DuckDB 是单写文件库,串行可以避免写锁冲突,
也避免前端连点导致内存翻倍。
### 0.3 分页
列表接口统一支持 `limit`(默认 50,最大 500)、`offset`(默认 0),返回:
```json
{ "items": [], "total": 123, "limit": 50, "offset": 0 }
```
`limit` / `offset` 由路由函数自己解析:写成 `limit=abc` 之类的非法值会**退回默认值**
(50 / 0),不会返回 422 —— 手工改 URL 调试时不会被框架的校验层拦住。
---
## 1. 元信息
### GET `/api/v1/meta`
返回服务配置,前端启动时调用一次。
```json
{
"service": "综资退网评估模板生成工具",
"version": "1.0.0",
"database": "duckdb",
"data_dir": "D:\\工作\\7.页面工具\\m.综资模板生成工具\\数据",
"output_dir": "D:\\工作\\7.页面工具\\m.综资模板生成工具\\结果\\output",
"template": "D:\\…\\数据\\频段清退评估模板.xlsx",
"order_type_prefix": "",
"neighbor_sites": 3,
"neighbor_exclude_keywords": ["微小"],
"gongcan_exclude_site_types": [],
"sources": [
{ "kind": "directory", "label": "目录扫描(子文件夹=网管批量报表 / 顶层=网页导入)" },
{ "kind": "upload", "label": "网页上传" }
],
"report_types": [
{ "kind": "gongcan", "label": "4G/5G 小区工参", "metrics": [] },
{ "kind": "demand", "label": "需求清单(拆站清单)", "metrics": [] },
{ "kind": "flow", "label": "流量/质量报表",
"metrics": ["traffic_gb","rrc_max","mr_weak_pct","access_rate","drop_rate"] }
],
"import_groups": [
{ "key": "gongcan", "label": "工参", "kinds": ["gongcan", "demand"],
"hint": "4G/5G 小区工参 + 需求清单;同一 CGI 以版本较新的一条为准,旧版本只补缺" },
{ "key": "flow", "label": "流量数据", "kinds": ["flow"],
"hint": "流量/质量报表;按(时间 × CGI)互补追加,可多次导入" }
]
}
```
`template` 是**频段清退模板的完整路径**;低效拆除模板由服务端按
`SZ_DAI_XIAO_TEMPLATE`(默认 `低效拆除评估模板.xlsx`)在同一数据目录下自行解析,
不需要前端传,也不在此接口返回。`report_types.flow.metrics` 只列前后对比用的
5 个指标,`flow` 表实际还有 15 个附加列(见 `doc/DATABASE.md` 第 3 节)。
### GET `/api/v1/stats`
库内数据量概览。
```json
{
"gongcan": 48294,
"gongcan_4g": 27518,
"gongcan_5g": 20776,
"gongcan_versions": ["202608", "202610"],
"flow": 198849,
"flow_points": 5,
"demand": 298,
"flow_time_range": { "a": "2026-08-01 00:00:00", "b": "2026-10-19 00:00:00" },
"database": "duckdb"
}
```
`gongcan` 是**单表**,行数 = 最新版本全量 + 旧版本补进来的缺失 CGI。
当前 48,294 行 = 202610 全量 46,980 + 202608 独有 1,314。
`gongcan_versions` 列出表内实际存在的版本号,可用于判断历史补数是否已生效。
### GET `/health`
```json
{ "status": "ok", "database": true }
```
---
## 2. 文件上传(网页导入)
### POST `/api/v1/files`
`multipart/form-data`,字段名 `file`(可重复,支持多文件),可选字段 `batch` 指定批次名。
```bash
curl -X POST http://127.0.0.1:5000/api/v1/files \
-F "file=@4G小区工参202610260846.xlsx" \
-F "file=@5G小区工参202610260848.xlsx"
```
响应 `201`:
```json
{
"batch": "up_20261029172031_9f3c1a",
"path": "…\\结果\\data\\uploads\\up_20261029172031_9f3c1a",
"files": [
{ "name": "4G小区工参202610260846.xlsx", "size": 20070610, "batch": "up_20261029172031_9f3c1a" }
]
}
```
非 `.xlsx/.xlsm` 会被忽略;一个可用文件都没有时返回 400。
### GET `/api/v1/files`
列出历史上传批次。
```json
{ "items": [ { "batch": "up_…", "path": "…", "files": ["a.xlsx"], "count": 1 } ], "total": 1 }
```
### DELETE `/api/v1/files/{batch}`
删除某个上传批次(204)。
### POST `/api/v1/demands/file`
上传**「生成评估模板」里要用的清单文件**(前端生成卡片最上面的「需求清单」)。
* `multipart/form-data`,字段名 `file`,单份 `.xlsx/.xlsm`;
* 只落到 `结果/data/uploads/_demand/<原文件名>`(同名会覆盖),**不入库**,
也不出现在 `GET /api/v1/files` 的上传批次里;
* 上传时立刻解析一遍,识别不了就直接 400,免得点了「生成模板」才发现选错文件。
```bash
curl -X POST http://127.0.0.1:5000/api/v1/demands/file -F "file=@拆站清单-20261103.xlsx"
```
响应 `201`:
```json
{
"path": "D:\\…\\结果\\data\\uploads\\_demand\\拆站清单-20261103.xlsx",
"name": "拆站清单-20261103.xlsx",
"rows": 306,
"order_types": ["其他类型", "频段清退"],
"message": "清单已就绪:拆站清单-20261103.xlsx(306 行)"
}
```
`rows` 是**文件里的有效行数**(CGI 为空的行已跳过),不做 `order_type` 前缀过滤;
真正参与生成的行数由 `POST /api/v1/exports` 的 `demand_count` 给出。
返回的 `path` 直接作为 `POST /api/v1/exports` 的 `demand_path` 传回去。
---
## 3. 入库
### POST `/api/v1/imports`
触发一次入库。Body:
| 字段 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `source` | string | 否 | `directory`(默认)或 `upload` |
| `path` | string | 否 | 目录绝对路径;`upload` 时可只给批次名 |
| `kinds` | string[] | 否 | 只处理指定类别,可选 `gongcan` / `flow` / `demand`;不传 = 全部 |
前端把入库拆成两个入口,就是靠 `kinds` 区分的:
| 按钮 | `kinds` | 说明 |
| --- | --- | --- |
| 导入工参 | `["gongcan", "demand"]` | 4G/5G 工参 + 需求清单 |
| 导入流量 | `["flow"]` | 6 类流量/质量报表 |
```json
{ "source": "directory", "path": "D:/工作/7.页面工具/m.综资模板生成工具/数据", "kinds": ["gongcan"] }
```
响应 `202`:
```json
{
"id": "imp_20261029172031_a1b2c3",
"source_kind": "directory",
"source_ref": "D:\\…\\数据",
"status": "pending",
"message": "",
"files": 0,
"gongcan_rows": 0,
"flow_rows": 0,
"demand_rows": 0,
"created_at": "2026-10-29 17:20:31",
"finished_at": null
}
```
`Location: /api/v1/imports/imp_20261029172031_a1b2c3`
**文件识别规则**(不依赖文件名,只看表头):
| 类型 | 识别依据 | 处理 |
| --- | --- | --- |
| `demand` | 含 `退网工单类型`、`小区名`、`CGI`;优先读工作表 `拆站清单` | 全量替换 `demand` 表 |
| `gongcan` | 含 `纬度`、`经度`、`小区名称`;有 `小区NCGI` 判为 5G,否则 4G | **单表增量**写 `gongcan`(见下) |
| `flow` | 有 `开始时间/记录开始时间/数据时间` + `CGI/NCGI/小区表示` + 至少一个指标列 | 按 `(ts, cgi)` 互补 upsert `flow`;表里有附加列(频点、使用频段、所属共站同覆盖区域名、自忙时峰值利用率 / 5G `自忙时小区PRB利用率`、MR 采样点、竞对覆盖率、三个是否标志等)就一并读取 |
| `template` | 文件名含「模板」或表头含 `区县`+`逻辑站点名称` | 跳过(不入库) |
| `unknown` | 其它 | 跳过,并在任务的 `skipped` 里说明原因 |
**工参的增量规则**:版本号从文件名日期解析(`4G小区工参202610260846` → `202610`)。
* 库里没有的 CGI → 直接插入;
* 库里已有、且本次版本**更新** → 覆盖;
* 库里已有、但本次版本**更旧** → 跳过,不覆盖。
所以「先导 10 月再导 8 月」与「先导 8 月再导 10 月」结果完全一致。
响应里的 `gongcan_rows` 是本次**实际写入**的行数,配合 `gongcan_added` / `gongcan_skipped`
可以看出本次补了多少、跳了多少。
### DELETE `/api/v1/data/{target}`
清空数据表,用于重来一遍。
| `target` | 清空的表 |
| --- | --- |
| `gongcan` | `gongcan` |
| `flow` | `flow` |
| `demand` | `demand` |
| `all` | 以上三张 |
```json
{
"target": "gongcan",
"cleared": { "gongcan": 48294 },
"total": 48294,
"message": "已清空 48294 行"
}
```
清空只删数据、不动表结构,清完直接重新导入即可。
### GET `/api/v1/imports/{id}`
轮询任务。成功时额外返回 `items`(每个文件的识别结果)与 `skipped`(跳过原因):
```json
{
"id": "imp_…",
"status": "succeeded",
"message": "工参 46980 行 / 流量 198849 行 / 清单 1616 行",
"files": 16,
"gongcan_rows": 46980,
"flow_rows": 198849,
"demand_rows": 1616,
"items": [
{ "file": "4G小区工参202610260846.xlsx", "origin": "web", "kind": "gongcan", "tech": "4G", "rows": 26291 },
{ "file": "appdbv3.a_mr_report_cell_m202610291452_1_水印.xlsx", "origin": "ftp", "kind": "flow",
"rows": 31124, "metrics": ["mr_weak_pct"], "start": "2026-09-01 00:00:00" }
],
"skipped": ["频段清退评估模板.xlsx:模板文件,不入库"]
}
```
`origin` 字段区分来源:`web` = 顶层文件(网页导入),`ftp` = 子文件夹(后续由 FTP 拉取)。
### GET `/api/v1/imports`
最近 20 条入库任务。
---
## 4. 数据查询
### GET `/api/v1/cells`
Query:`tech`(4G/5G)、`q`(CGI/小区名/站点名模糊)、`limit`、`offset`。
```json
{
"items": [
{ "cgi": "460-00-12615680-1", "cell_name": "江门白沙大道西1号D-ZRH-1",
"lat": 22.58359, "lon": 113.07452, "site_name": "江门蓬江区白沙大道西1号", "tech": "5G" }
],
"total": 46980, "limit": 50, "offset": 0
}
```
### GET `/api/v1/cells/{cgi}`
单小区详情(CGI 含 `-`,路径参数用 `path:` 转换器,直接拼即可)。不存在返回 404。
### GET `/api/v1/flows`
Query:`cgi`(可选)、`limit`、`offset`。按时间倒序。
```json
{
"items": [
{ "ts": "2026-10-19 00:00:00", "cgi": "460-00-122157-4",
"traffic_gb": 10.0129, "rrc_max": 5.71429, "mr_weak_pct": 16.0162,
"access_rate": 99.87364, "drop_rate": 0.02942,
"source": "appdbv3.a_overview_ispm_lte_cell_w202610291433_1_水印.xlsx" }
],
"total": 4, "limit": 50, "offset": 0
}
```
### GET `/api/v1/demands`
Query:`order_type`(前缀,默认取配置 `order_type_prefix`;**配置默认空串 = 不过滤,返回全部清单**)、`limit`、`offset`。
```json
{
"items": [
{ "id": 3, "county": "蓬江", "logical_site": "江门大长江(共江门宏达F拉远)F-ZLH",
"cell_name": "江门大长江(共江门宏达F拉远)F-ZLH-5", "cgi": "460-00-679666-5",
"sector": "江门大长江5", "site_name": "江门大长江",
"applicant": "史君黛", "retire_reason": "2026年全网拆闲涉及无线室硬拆清单510个小区…",
"need_date": "46045", "order_type": "频段清退" }
],
"total": 338, "limit": 50, "offset": 0
}
```
### GET `/api/v1/neighbors`
调试接口:给定 CGI,返回最近 N 个站点。
| 参数 | 说明 |
| --- | --- |
| `cgi` | 必填 |
| `k` | 默认取配置 3,最大 20 |
```json
{
"cgi": "460-00-679666-5",
"site": "江门大长江",
"lat": 22.6, "lon": 113.1,
"items": [
{ "site_name": "江门蓬江区×××", "distance_m": 312.4, "cells": 6 }
]
}
```
---
## 5. 导出模板
### POST `/api/v1/exports`
| 字段 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `template_path` | string | 否 | 默认 `数据/频段清退评估模板.xlsx` |
| `output_dir` | string | 否 | 默认 `结果/output` |
| `order_type` | string | 否 | 清单过滤前缀,默认取配置 `order_type_prefix`;**默认空串 = 不过滤,全部清单进模板** |
| `neighbor_k` | int | 否 | 周边站点数,默认 3 |
| `demand_path` | string | 否 | **本次生成改用这份清单文件**(前端「需求清单」处选择的本机 xlsx)。传了就读文件、不查也不写 `demand` 表;不传 = 用数据库里的清单。路径由 `POST /api/v1/demands/file` 返回 |
```json
{ "template_path": "D:/…/数据/频段清退评估模板.xlsx", "neighbor_k": 3 }
```
指定清单文件时:
```json
{
"template_path": "D:/…/数据/频段清退评估模板.xlsx",
"demand_path": "D:\\…\\结果\\data\\uploads\\_demand\\拆站清单-20261103.xlsx",
"neighbor_k": 3
}
```
`demand_path` 的处理规则:
| 情况 | 响应 |
| --- | --- |
| 文件不存在 | 404 `not_found` |
| 不是 `.xlsx/.xlsm` | 400 `bad_request` |
| 表头识别不出清单(缺 `小区名`/`CGI`/`退网工单类型`) | 400 `bad_request`,提示「清单文件无法识别:…」 |
| 按 `order_type` 前缀过滤后为空 | 400 `bad_request` |
`demand_count` 是**该文件按前缀过滤后的行数**,任务消息里会注明「清单来自 清单文件 xxx.xlsx」。
**一次导出可能生成两份文件**(按 `退网工单类型` 分流,详见本地 `doc/INTERNAL.md`
第 8.0 节,该文件 git 忽略):
| 清单行 | 输出文件 |
| --- | --- |
| 含「低效拆除」 | `低效拆除评估模板-YYYYMMDD.xlsx` |
| 其余(含频段清退) | `频段清退评估模板-YYYYMMDD.xlsx` |
任务记录里的 `output_path` / `output_name` 是**主文件**:两组都有数据时主文件是
频段清退模板,只有低效拆除组时主文件就是低效模板。任务 `message` 会写明
「另生成低效拆除模板 … 行」;`run_export()` 的返回值里另有 `outputs`(本次生成的
全部文件路径列表)与 `daiXiao_path` / `daiXiao_name` 等字段,供命令行与前端扩展使用。
接口的任务表只登记一个主文件,**第二份文件请到 `output_dir` 目录取**。
响应 `202`:
```json
{
"id": "exp_20261029172510_7d8e9f",
"status": "pending",
"demand_count": 338,
"row_count": 0,
"output_path": "",
"output_name": "",
"warnings": "",
"message": "",
"created_at": "2026-10-29 17:25:10",
"finished_at": null
}
```
### GET `/api/v1/exports/{id}`
```json
{
"id": "exp_…",
"status": "succeeded",
"demand_count": 298,
"row_count": 5316,
"output_path": "D:\\…\\结果\\output\\频段清退评估模板-20261030.xlsx",
"output_name": "频段清退评估模板-20261030.xlsx",
"warnings": "5 个逻辑站点在工参中找不到 CGI 或经纬度,第二项未生成:…\n第一项有 5 个逻辑站共 5 行,站内所有小区的 CGI 在工参中都找不到(或经纬度为空),已整站标红(这些行不会生成第二项)\n第二项有 42 个周边小区没有匹配到流量数据,指标列留空",
"message": "生成 196 行清单 + 3600 行周边评估(135 个逻辑站),另生成低效拆除模板 102 行(1711 行周边评估)",
"created_at": "2026-10-30 14:36:10",
"finished_at": "2026-10-30 14:36:21"
}
```
### GET `/api/v1/exports/{id}/file`
下载主文件 `.xlsx`(`Content-Disposition: attachment`)。任务未完成返回 409 `not_ready`。
若本次同时生成了第二份模板(低效拆除 / 频段清退),该接口**只提供主文件**,
另一份在 `output_dir` 目录下按文件名取(见上表与本地 `doc/INTERNAL.md` 第 8.0 节)。
### DELETE `/api/v1/exports/{id}`
删除任务记录与已生成文件(204)。
### GET `/api/v1/exports`
最近 20 条导出任务。
---
## 6. 典型调用顺序
```text
GET /api/v1/meta # 取默认路径
POST /api/v1/imports # 202
GET /api/v1/imports/{id} # 轮询至 succeeded
GET /api/v1/stats # 确认数据量
POST /api/v1/exports # 202
GET /api/v1/exports/{id} # 轮询至 succeeded
GET /api/v1/exports/{id}/file # 下载
```
命令行等价:
```bash
python -m app.run ingest --dir ../数据
python -m app.run export --out output
```