From f6bbdbec0ce95cbd873fbeca93473b22dd7609bb Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Fri, 31 Jan 2025 14:03:33 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20explorer=20view=20indexing?= =?UTF-8?q?=20when=20metadata=20is=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baker/algolia/utils/explorerViews.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/baker/algolia/utils/explorerViews.ts b/baker/algolia/utils/explorerViews.ts index 7c3d0e4440..803e9267bb 100644 --- a/baker/algolia/utils/explorerViews.ts +++ b/baker/algolia/utils/explorerViews.ts @@ -470,14 +470,13 @@ async function enrichWithTableData( return enrichedRecords } -function enrichRecordWithIndicatorData( +async function enrichRecordWithIndicatorData( record: IndicatorUnenrichedExplorerViewRecord, indicatorMetadataDictionary: ExplorerIndicatorMetadataDictionary -): IndicatorEnrichedExplorerViewRecord { - const allEntityNames = at( - indicatorMetadataDictionary, - record.yVariableIds - ).flatMap((meta) => meta.entityNames) +): Promise { + const allEntityNames = at(indicatorMetadataDictionary, record.yVariableIds) + .filter(Boolean) + .flatMap((meta) => meta.entityNames) const uniqueNonEmptyEntityNames = uniq(allEntityNames).filter( (name): name is string => !!name @@ -486,6 +485,13 @@ function enrichRecordWithIndicatorData( const firstYIndicator = record.yVariableIds[0] const indicatorInfo = indicatorMetadataDictionary[firstYIndicator] + if (!indicatorInfo) { + await logErrorAndMaybeCaptureInSentry({ + name: "ExplorerViewIndicatorMissing", + message: `Explorer with slug "${record.explorerSlug}" has a view with missing indicator metadata: ${record.viewQueryParams}.`, + }) + return + } const viewTitle = record.viewTitle || @@ -505,16 +511,16 @@ function enrichRecordWithIndicatorData( } } -const enrichWithIndicatorMetadata = async ( +async function enrichWithIndicatorMetadata( indicatorBaseRecords: IndicatorUnenrichedExplorerViewRecord[], indicatorMetadataDictionary: ExplorerIndicatorMetadataDictionary -): Promise => { - return indicatorBaseRecords.map((indicatorBaseRecord) => +): Promise { + return pMap(indicatorBaseRecords, (indicatorBaseRecord) => enrichRecordWithIndicatorData( indicatorBaseRecord, indicatorMetadataDictionary ) - ) + ).then((r) => r.filter(Boolean) as IndicatorEnrichedExplorerViewRecord[]) } function processSubtitles( @@ -658,6 +664,7 @@ export const getExplorerViewRecordsForExplorer = async ( indicatorBaseRecords, trx ) + console.log("Fetched indicator metadata for explorer", slug) const enrichedIndicatorRecords = await enrichWithIndicatorMetadata( @@ -739,6 +746,7 @@ export const getExplorerViewRecords = async ( console.log("(Skipping grapher views)") } const publishedExplorersWithTags = await getExplorersWithInheritedTags(trx) + // .then((es) => es.filter((e) => e.slug === "covid")) const pageviews = await getAnalyticsPageviewsByUrlObj(trx) const explorerAdminServer = new ExplorerAdminServer(GIT_CMS_DIR)