fix(performance): 메타 광고비가 0 으로 들어오던 문제 — 기간 조회 응답의 0 인 날은 하루씩 다시 조회

메타가 기간(10/01~10/21)으로 물으면 지출·노출·클릭을 0 으로 주고(링크 클릭·구매는 정상),
하루만 물으면 정상 값을 준다(운영 진단 스크립트로 확인). 지출·노출 0 인데 링크 클릭·구매가 있는 날만
그날 하루를 다시 물어 바꾼다. 집행 없는 계정·진짜 0 인 날은 추가 호출 없음.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 16:34:01 +09:00
parent d7db21a36b
commit e3e3b42c28
3 changed files with 61 additions and 0 deletions
@@ -499,3 +499,36 @@ def test_connection_mentions_conversion(monkeypatch):
"account_status": 1}))
msg = ma.MetaAdsSource().test_connection(_ctx(account_ids="333333"))
assert "그날 환율로 원화로 바꿔 넣습니다" in msg and "가져올 수 없습니다" not in msg
def test_period_zero_spend_refetches_single_days(monkeypatch):
"""2026-10-21 운영 — 기간으로 물으면 메타가 지출·노출·클릭을 0 으로(링크 클릭·구매는 정상) 주고, 하루만 물으면 정상.
그런 날만 하루씩 다시 물어 바꾼다. 집행 없는 계정(행 없음)·진짜 0 인 날(링크 클릭·구매도 0)은 다시 묻지 않는다."""
real = {"2026-10-19": ("700000", "25000", "900"), "2026-10-20": ("817212", "29103", "983")}
def handler(request: httpx.Request) -> httpx.Response:
tr = json.loads(request.url.params["time_range"])
if "1111111111" not in str(request.url):
return httpx.Response(200, json={"data": []}) # 집행 없는 계정
if tr["since"] == tr["until"]: # 하루 조회 = 정상 값
spend, impr, clicks = real[tr["since"]]
return httpx.Response(200, json={"data": [_row(tr["since"], spend, impressions=impr, clicks=clicks,
inline_link_clicks="941",
actions=[{"action_type": "omni_purchase", "value": "50"}],
action_values=[{"action_type": "omni_purchase", "value": "2713820"}])]})
rows = [_row(d, "0", impressions="0", clicks="0", inline_link_clicks="941", # 기간 조회 = 핵심 칸 0
actions=[{"action_type": "omni_purchase", "value": "50"}],
action_values=[{"action_type": "omni_purchase", "value": "2713820"}]) for d in real]
rows.append(_row("2026-10-21", "0", impressions="0", clicks="0", inline_link_clicks="0")) # 진짜 0 인 날
return httpx.Response(200, json={"data": rows})
seen = _install(monkeypatch, handler)
days = {d.day.isoformat(): d for d in ma.MetaAdsSource().fetch_days(_ctx(), date(2026, 10, 19), date(2026, 10, 21))}
assert days["2026-10-20"].amount == 817212 and days["2026-10-20"].metrics["노출수"] == 29103
assert days["2026-10-20"].metrics["클릭수"] == 983 and days["2026-10-20"].roas == Decimal("332.08")
assert days["2026-10-19"].amount == 700000
assert days["2026-10-21"].amount == 0
singles = [json.loads(r.url.params["time_range"]) for r in seen]
singles = [t["since"] for t in singles if t["since"] == t["until"]]
assert sorted(singles) == ["2026-10-19", "2026-10-20"] # 이상한 날만, 계정 1 에만
assert len(seen) == 2 + 2 # 기간 2(계정 2개) + 하루 2