From b6ecde11dde2d419201464d81932845715969fb0 Mon Sep 17 00:00:00 2001 From: yueli Date: Fri, 4 Sep 2026 15:19:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20warehouse/tree=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=89=93=E7=82=B9=E6=97=A5=E5=BF=97=EF=BC=8C?= =?UTF-8?q?=E4=BF=9D=E7=95=99=20O(N)=20build=5Ftree=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inventory-backend/app/api/v1/warehouse.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/inventory-backend/app/api/v1/warehouse.py b/inventory-backend/app/api/v1/warehouse.py index 9208e95..51718fa 100644 --- a/inventory-backend/app/api/v1/warehouse.py +++ b/inventory-backend/app/api/v1/warehouse.py @@ -36,33 +36,19 @@ def get_tree(): """ 获取库位树形结构 """ - import time - from flask import current_app - t0 = time.time() try: # 查询所有库位,按 name 升序排序 all_locations = SysWarehouseLocation.query.order_by(SysWarehouseLocation.name.asc()).all() - t1 = time.time() - # 构建树形结构 + # 构建树形结构(O(N) 内存组装,见 build_tree) tree_data = build_tree(all_locations, parent_id=None) - t2 = time.time() - resp = jsonify({ + return jsonify({ 'code': 200, 'msg': 'success', 'data': tree_data }) - t3 = time.time() - - print( - f"[warehouse/tree 打点] 阶段1-查库={t1 - t0:.3f}s | 阶段2-build_tree+to_dict={t2 - t1:.3f}s | " - f"阶段3-jsonify序列化={t3 - t2:.3f}s | 总={t3 - t0:.3f}s (rows={len(all_locations)}, tree_top={len(tree_data)})", - flush=True - ) - return resp except Exception as e: - print(f"[warehouse/tree 打点] 异常: {e}", flush=True) return jsonify({ 'code': 500, 'msg': str(e),