init: Prefect ETL sync project (RIIN/SDS → PG) with 12 sync flows
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"""RIIN 订单同步:按时间窗口(+可选筛选)拉取订单与商品。
|
||||
|
||||
对应 riin-order-sync。timeType 固定 3(商家推送时间)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from prefect import flow, task
|
||||
|
||||
from api.riin.models import OrderPageRequest
|
||||
from flows.riin._pipeline import sync_riin_records
|
||||
from flows.shared import SyncDB, create_riin_client, fetch_riin_all_pages, format_date
|
||||
|
||||
|
||||
@task(retries=3, retry_delay_seconds=[3, 6, 9])
|
||||
def extract_orders(params: dict[str, Any]) -> list[Any]:
|
||||
client = create_riin_client()
|
||||
try:
|
||||
return fetch_riin_all_pages(client, params)
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
@task
|
||||
def upsert_records(raw_orders: list[Any]) -> dict[str, int]:
|
||||
client = create_riin_client()
|
||||
try:
|
||||
return sync_riin_records(raw_orders, client, SyncDB())
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
@flow(name="riin-order-sync", log_prints=True)
|
||||
def riin_order_sync(
|
||||
order_date_begin: str,
|
||||
order_date_end: str,
|
||||
platform_type: int | None = None,
|
||||
order_status: int | None = None,
|
||||
factory_ids: list[str] | None = None,
|
||||
express_source: int | None = None,
|
||||
urgent_order: int | None = None,
|
||||
has_refund: bool | None = None,
|
||||
page_size: int = 200,
|
||||
) -> dict[str, Any]:
|
||||
if not order_date_begin or not order_date_end:
|
||||
raise ValueError("order_date_begin / order_date_end 为必填")
|
||||
|
||||
params: dict[str, Any] = {
|
||||
"time_type": 3,
|
||||
"start_time": order_date_begin,
|
||||
"end_time": order_date_end,
|
||||
"page_size": page_size,
|
||||
}
|
||||
if platform_type is not None:
|
||||
params["platform_type"] = platform_type
|
||||
if order_status is not None:
|
||||
params["order_status"] = order_status
|
||||
if factory_ids:
|
||||
params["factory_ids"] = factory_ids
|
||||
if express_source is not None:
|
||||
params["express_source"] = express_source
|
||||
if urgent_order is not None:
|
||||
params["urgent_order"] = urgent_order
|
||||
if has_refund is not None:
|
||||
params["has_refund"] = has_refund
|
||||
|
||||
print(f"RIIN 订单同步 {order_date_begin} ~ {order_date_end}")
|
||||
raw = extract_orders(params)
|
||||
stats = upsert_records(raw)
|
||||
print(f"完成: 订单 {stats['orderCount']} / 商品 {stats['goodsCount']} | {stats}")
|
||||
return {"syncRange": {"from": order_date_begin, "to": order_date_end}, **stats}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
riin_order_sync.serve(name="riin-order-sync")
|
||||
Reference in New Issue
Block a user