init: Prefect ETL sync project (RIIN/SDS → PG) with 12 sync flows
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
"""RIIN 近期同步:拉取最近 N 天订单(默认 7 天,1~90)。
|
||||
|
||||
对应 riin-recent-sync。from = startOfDay(now - N days),to = now。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from prefect import flow, task
|
||||
|
||||
from flows.riin._pipeline import sync_riin_records
|
||||
from flows.shared import (
|
||||
SyncDB, create_riin_client, fetch_riin_all_pages, format_date, start_of_day,
|
||||
)
|
||||
|
||||
|
||||
@task(retries=3, retry_delay_seconds=[3, 6, 9])
|
||||
def extract_orders(params: dict) -> list:
|
||||
client = create_riin_client()
|
||||
try:
|
||||
return fetch_riin_all_pages(client, params)
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
@task
|
||||
def upsert_records(raw_orders: list) -> dict:
|
||||
client = create_riin_client()
|
||||
try:
|
||||
return sync_riin_records(raw_orders, client, SyncDB())
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
|
||||
@flow(name="riin-recent-sync", log_prints=True)
|
||||
def riin_recent_sync(recent_days: int = 7) -> dict:
|
||||
days = min(max(recent_days, 1), 90)
|
||||
now = datetime.now()
|
||||
from_dt = start_of_day(now - timedelta(days=days))
|
||||
|
||||
params = {
|
||||
"time_type": 3,
|
||||
"start_time": format_date(from_dt),
|
||||
"end_time": format_date(now),
|
||||
"page_size": 200,
|
||||
}
|
||||
print(f"RIIN 近期同步 近 {days} 天 {params['start_time']} ~ {params['end_time']}")
|
||||
raw = extract_orders(params)
|
||||
stats = upsert_records(raw)
|
||||
print(f"完成: 订单 {stats['orderCount']} / 商品 {stats['goodsCount']}")
|
||||
return {"syncRange": {"from": params["start_time"], "to": params["end_time"]}, "recentDays": days, **stats}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
riin_recent_sync.serve(name="riin-recent-sync", cron="30 * * * *")
|
||||
Reference in New Issue
Block a user