6.9 KiB
2026-01-16 工作日志
背景
需要在 stateflow 模块提供一种通用查询能力:按“已完成的状态节点(state_id)+ 工艺参数 key/value”反查相关的 BusinessObject 列表。
典型场景:查找所有 已完成“画图中”节点,且工艺参数“设计师名称”为“AAA”的业务对象(并可通过 content_type/object_id 追溯到实际业务对象,如 printing.plateorder)。
今日任务
- 为
GET /api/v1/stateflow/business-objects/增加过滤条件:state_id+param_key+param_value - 增加 PostgreSQL JSONB 查询性能优化:为
StateLogParameterRecord.parameters添加 GIN(jsonb_path_ops)索引 - 补齐/更新测试用例,验证过滤行为与撤销记录处理
- 输出该查询能力的单独说明文档(用途/出入参/示例/性能与索引)
- Shipment 模块:为
Shipment增加area字段并贯通所有相关 API(create/update/返回/文档/测试) - Printing/定时任务:实现“PlateOrder 图片按 URL 上传到腾讯云图库”的能力(上传函数/按 plate_order_id 批量上传/每日 03:00 定时任务/限速/失败落库)
今日完成
1) BusinessObject 查询能力增强(按已完成节点 + 工艺参数过滤)
在 api_v1/views/stateflow/business_object.py 的 BusinessObjectFilterSet 中新增过滤参数:
state_id:状态节点 ID(表示“已完成该节点”,仅匹配StateFlowRecord.is_cancelled=False)param_key:工艺参数 keyparam_value:工艺参数 value
典型用法(示例:查 printing.plateorder 类型):
GET /api/v1/stateflow/business-objects/?content_type_str=printing.plateorder&state_id=<id>¶m_key=设计师名称¶m_value=AAA&limit=500&offset=0
实现方式使用 Exists(OuterRef) 子查询,避免 join + distinct 的重复行问题,性能更稳定。
2) PostgreSQL JSONB 索引优化
新增迁移:stateflow/migrations/0022_statelogparameterrecord_parameters_gin_index.py
- 为
StateLogParameterRecord.parameters增加GIN(jsonb_path_ops)索引 - 主要加速
parameters__contains={key: value}(即jsonb @> ...)这类查询
3) 测试补齐
在 stateflow/tests/test_business_object_crud_and_filters_api.py 增加用例,覆盖:
- 命中:已完成节点 + 参数匹配
- 不命中:参数值不一致
- 排除:已撤销的
StateFlowRecord不参与匹配
另外,为保证 stateflow 测试集可持续运行,恢复了 stateflow.services.clone_business_object() 的实现(此前曾临时禁用导致测试失败)。
4) 单独说明文档(stateflow 查询能力)
新增文档:docs/stateflow_business_objects_query_by_completed_state_and_param.md,包含:
- 能力用途/背景(“已完成某节点 + 工艺参数 key/value”反查
BusinessObject) - 接口定义与查询参数(
state_id/param_key/param_value与content_type_str的配套用法) - 过滤语义(排除撤销记录、参数绑定范围)
- 返回结构(与
BusinessObjectListSerializer对齐) - 性能建议(GIN 索引与验证方式)
5) Shipment:增加 area 字段并贯通 API(create/update/返回/文档/测试)
新增字段:
shipment.models.Shipment.area:CharField(max_length=30, blank=True, default='')- 迁移:
shipment/migrations/0008_add_area_to_shipment.py
接口/序列化器调整:
api_v1/views/shipment/serializers.pyShipmentSerializer:响应中新增areaShipmentCreateNormalSerializer/ShipmentCreateExternalSerializer:支持写入area- 新增
ShipmentUpdateSerializer:用于更新入口(PATCH/PUT)
api_v1/views/shipment/views.py- 普通创建与 external 创建:将
area透传并落库 ShipmentDetailView:新增PATCH/PUT /api/v1/shipment/shipments/<id>/,支持更新area并回显(同时保持 merchant 隔离与 customer 校验)
- 普通创建与 external 创建:将
shipment/services.py:create_shipment/create_external_shipment支持area并写入
测试与文档:
api_v1/views/shipment/test_api.py:补齐 create 回显 + PATCH 更新回显用例docs/shipment_api.md:补齐area的请求/响应示例与字段说明
6) Printing:PlateOrder 图片上传腾讯云图库(函数 + 定时任务 + 限速 + 失败落库)
依赖:
pyproject.toml:加入tencentcloud-sdk-python(TIIA SDK)
上传函数(基础能力):
api_v1/utils/tencentcloud_tiia.pyupload_image_url_to_tencent_tiia(image_url, entity_id):调用 TIIACreateImage,通过 URL 上传图片到图库- 腾讯云凭证/配置改为从
flower/settings.py读取(支持.env注入) SimpleRateLimiter:简单限速器,确保不超过 10 次/秒(可由TENCENTCLOUD_TIIA_QPS配置)
按 PlateOrder 批量上传(包装函数):
api_v1/utils/tencentcloud_tiia.pyupload_plate_order_images_to_tencent_tiia(plate_order_id, rate_limiter=...)- 从
PlateOrder.plate_image(JSONField)提取所有图片 URL/Path(兼容url/path/imageUrl/字符串),逐张上传 entity_id统一使用str(plate_order_id)
每日定时任务(03:00 跑“昨天创建的订单”):
printing/tasks.pyupload_yesterday_plate_order_images_to_tencent_tiia- 扫描昨天创建的 PlateOrder,逐单上传;遇错记录失败 ID 并继续
flower/settings.pyCELERY_BEAT_SCHEDULE新增daily_plate_order_tiia_image_upload(03:00)
失败记录表(用于可追踪/可重试):
printing.models.PlateOrderTiiaUploadFailure- 字段:
run_date/plate_order_id/error/details/attempts/last_attempt_at - 唯一约束:
(run_date, plate_order_id),重复失败会累加attempts - 迁移:
printing/migrations/0031_plateorder_tiia_upload_failure.py
- 字段:
printing/admin.py:在 admin 中注册失败记录表,便于排查
测试:
api_v1/test_tencentcloud_tiia_plate_order_upload.py:mock uploader,验证包装函数能提取并上传多张图片、entity_id 绑定正确printing/test_tiia_upload_task.py:mock uploader + sleep,验证“记录失败 ID 但不中断继续执行”
补充配置项(settings):
flower/settings.pyTENCENTCLOUD_SECRET_ID / TENCENTCLOUD_SECRET_KEY / TENCENTCLOUD_TOKENTENCENTCLOUD_TIIA_GROUP_ID / TENCENTCLOUD_TIIA_REGION / TENCENTCLOUD_TIIA_ENDPOINTTENCENTCLOUD_TIIA_PIC_NAME_PREFIXTENCENTCLOUD_TIIA_QPS(默认 10,用于任务限速)
7) 临时跑批(一次性全量上传现有 PlateOrder)
为支持上线前手工验证/补数据,提供了可直接在 manage.py shell 中执行的一次性跑批脚本(遍历全部 PlateOrder,逐单上传 plate_image 里的所有图片,遇错记录失败 ID 并继续)。
说明:
entity_id使用plate_order_id- 通过
SimpleRateLimiter(qps=10)遵守腾讯云限速 - 失败会写入
PlateOrderTiiaUploadFailure(便于后续排查与重试)