fix: 파이프라인 3대 핵심 버그 수정

- generate-report: Harness 4 groundTruth 주입 레이어 추가 (IG/YT/FB/NaverBlog/NaverPlace/GangnamUnni 필드 강제 주입, diagnosis 폴백, qualityReport DB 저장)
- discover-channels: CLINIC_NOT_REGISTERED 조기 종료 제거 + clinics 캐시 fast-path 추가 (14일 TTL, Firecrawl fallback 재활성화)
- collect-channel-data: silent skip → {status, reason, attemptedAt} 구조적 기록 (naverBlog/naverPlace/googleMaps/gangnamUnni)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Haewon KamandClaude Opus 4.6 committed 2026-04-10 13:41:05 +09:00
1 parent aabba1534b
commit c753d8593f
3 files changed
+398 -36

No files matched your search

@@ -414,7 +414,14 @@ Deno.serve(async (req) => {
}
if (!gangnamUnniUrl) {
throw new Error("강남언니 URL을 찾을 수 없습니다 (검색 실패)");
console.log("[gangnamUnni] URL not found via any search — marking skipped");
channelData.gangnamUnni = {
status: "skipped",
reason: "URL_NOT_FOUND",
reasonDetail: "Firecrawl search across 3 query variants returned no gangnamunni.com/hospitals/ link",
attemptedAt: new Date().toISOString(),
};
return;
}
const scrapeRes = await fetchWithRetry("https://api.firecrawl.dev/v1/scrape", {
@@ -438,11 +445,21 @@ Deno.serve(async (req) => {
waitFor: 5000,
}),
}, { label: "firecrawl-gangnamunni", timeoutMs: 60000 });
if (!scrapeRes.ok) throw new Error(`Firecrawl 강남언니 scrape failed: ${scrapeRes.status}`);
if (!scrapeRes.ok) {
channelData.gangnamUnni = {
status: "skipped",
reason: "SCRAPE_FAILED",
reasonDetail: `Firecrawl returned HTTP ${scrapeRes.status}`,
attemptedAt: new Date().toISOString(),
sourceUrl: gangnamUnniUrl,
};
return;
}
const data = await scrapeRes.json();
const hospital = data.data?.json;
if (hospital?.hospitalName) {
channelData.gangnamUnni = {
status: "ok",
name: hospital.hospitalName,
rawRating: hospital.rating,
rating: typeof hospital.rating === 'number' && hospital.rating > 0 ? hospital.rating : null,
@@ -452,9 +469,21 @@ Deno.serve(async (req) => {
badges: hospital.badges || [], sourceUrl: gangnamUnniUrl,
};
} else {
throw new Error("강남언니 scrape returned no hospital data");
channelData.gangnamUnni = {
status: "skipped",
reason: "EMPTY_SCRAPE_RESULT",
reasonDetail: "Firecrawl scraped the page but could not extract hospital data",
attemptedAt: new Date().toISOString(),
sourceUrl: gangnamUnniUrl,
};
}
}));
} else {
channelData.gangnamUnni = {
status: "skipped",
reason: !FIRECRAWL_API_KEY ? "FIRECRAWL_API_KEY_MISSING" : "CLINIC_NAME_MISSING",
attemptedAt: new Date().toISOString(),
};
}
// ─── 5. Naver Blog + Place ───
@@ -490,6 +519,7 @@ Deno.serve(async (req) => {
const totalMatch = xml.match(/<totalCount>(\d+)<\/totalCount>/) || xml.match(/<managedCount>(\d+)<\/managedCount>/);
const totalPosts = totalMatch ? Number(totalMatch[1]) : items.length;
channelData.naverBlog = {
status: "ok",
officialBlogUrl, officialBlogHandle,
totalResults: totalPosts,
posts: items.slice(0, 10).map(i => ({
@@ -500,13 +530,28 @@ Deno.serve(async (req) => {
};
console.log(`[naverBlog] RSS: ${items.length} posts from verified handle ${officialBlogHandle}`);
} catch (e) {
console.warn(`[naverBlog] RSS fetch failed:`, e);
// Fallback: at minimum expose the official URL even without post data
channelData.naverBlog = { officialBlogUrl, officialBlogHandle, totalResults: 0, posts: [], officialContent: null };
const reason = e instanceof Error ? e.message : String(e);
console.warn(`[naverBlog] RSS fetch failed:`, reason);
// Expose the official URL even without post data so the frontend can still link out
channelData.naverBlog = {
status: "skipped",
reason: "RSS_FETCH_FAILED",
reasonDetail: reason,
attemptedAt: new Date().toISOString(),
officialBlogUrl, officialBlogHandle,
totalResults: 0, posts: [], officialContent: null,
};
}
}));
} else {
console.log(`[naverBlog] No verified handle in DB — skipping`);
console.log(`[naverBlog] No verified handle in DB — marking skipped`);
channelData.naverBlog = {
status: "skipped",
reason: "NO_VERIFIED_HANDLE",
attemptedAt: new Date().toISOString(),
totalResults: 0,
posts: [],
};
}
// naverPlace: use stored verified data if available, otherwise search once and save
@@ -564,7 +609,7 @@ Deno.serve(async (req) => {
}
if (found) {
channelData.naverPlace = found;
channelData.naverPlace = { status: "ok", ...found };
// Save to clinics.verified_channels so future runs skip the search
if (inputClinicId) {
const { data: clinicRow } = await supabase.from('clinics').select('verified_channels').eq('id', inputClinicId).single();
@@ -576,9 +621,22 @@ Deno.serve(async (req) => {
}
}
} else {
console.log(`[naverPlace] No confident match found — skipping to avoid wrong data`);
console.log(`[naverPlace] No confident match found — marking skipped`);
channelData.naverPlace = {
status: "skipped",
reason: "NO_CONFIDENT_MATCH",
reasonDetail: `Tried ${queries.length} queries but none matched the domain or exact clinic name`,
attemptedAt: new Date().toISOString(),
attemptedQueries: queries,
};
}
}));
} else {
channelData.naverPlace = {
status: "skipped",
reason: "NAVER_API_CREDENTIALS_MISSING",
attemptedAt: new Date().toISOString(),
};
}
// ─── 6. Google Maps (Google Places API New) ───
@@ -587,6 +645,7 @@ Deno.serve(async (req) => {
const place = await searchGooglePlace(clinicName, address || undefined, GOOGLE_PLACES_API_KEY);
if (place) {
channelData.googleMaps = {
status: "ok",
name: place.name, rating: place.rating, reviewCount: place.reviewCount,
address: place.address, phone: place.phone,
clinicWebsite: place.clinicWebsite,
@@ -596,9 +655,20 @@ Deno.serve(async (req) => {
topReviews: place.topReviews,
};
} else {
throw new Error("Google Maps: no matching place found");
channelData.googleMaps = {
status: "skipped",
reason: "PLACE_NOT_FOUND",
attemptedAt: new Date().toISOString(),
searchQuery: clinicName,
};
}
}));
} else {
channelData.googleMaps = {
status: "skipped",
reason: !GOOGLE_PLACES_API_KEY ? "API_KEY_MISSING" : "CLINIC_NAME_MISSING",
attemptedAt: new Date().toISOString(),
};
}
// ─── 7. Market Analysis (Perplexity) ───
@@ -841,34 +911,41 @@ Deno.serve(async (req) => {
});
}
// Helper: only snapshot channels that actually collected data.
// Skipped channels (status === 'skipped') still live in channelData for the
// generate-report step to render their skip reason, but shouldn't pollute
// the time-series channel_snapshots table.
const isCollected = (d: Record<string, unknown> | undefined): boolean =>
!!d && d.status !== "skipped";
const guData = channelData.gangnamUnni as Record<string, unknown> | undefined;
if (guData) {
if (isCollected(guData)) {
snapshotInserts.push({
clinic_id: clinicId, run_id: runId, channel: 'gangnamUnni',
handle: guData.name, rating: guData.rating, rating_scale: 10,
reviews: guData.totalReviews,
health_score: computeHealthScore('gangnamUnni', guData),
handle: guData!.name, rating: guData!.rating, rating_scale: 10,
reviews: guData!.totalReviews,
health_score: computeHealthScore('gangnamUnni', guData!),
details: guData,
});
}
const gmData = channelData.googleMaps as Record<string, unknown> | undefined;
if (gmData) {
if (isCollected(gmData)) {
snapshotInserts.push({
clinic_id: clinicId, run_id: runId, channel: 'googleMaps',
handle: gmData.name, rating: gmData.rating, rating_scale: 5,
reviews: gmData.reviewCount,
health_score: computeHealthScore('googleMaps', gmData),
handle: gmData!.name, rating: gmData!.rating, rating_scale: 5,
reviews: gmData!.reviewCount,
health_score: computeHealthScore('googleMaps', gmData!),
details: gmData,
});
}
const nbData = channelData.naverBlog as Record<string, unknown> | undefined;
if (nbData) {
if (isCollected(nbData)) {
snapshotInserts.push({
clinic_id: clinicId, run_id: runId, channel: 'naverBlog',
handle: nbData.officialBlogHandle,
health_score: computeHealthScore('naverBlog', nbData),
handle: nbData!.officialBlogHandle,
health_score: computeHealthScore('naverBlog', nbData!),
details: nbData,
});
}