Skip to content

Commit

Permalink
enhance(multi-dim): bake variable id -> config uuid map
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Sep 3, 2024
1 parent 8298b85 commit 52c85bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
23 changes: 23 additions & 0 deletions baker/MultiDimBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import React from "react"
import { BAKED_BASE_URL } from "../settings/clientSettings.js"
import { getTagToSlugMap } from "./GrapherBakingUtils.js"
import {
getGrapherConfigIdsForVariables,
getVariableIdsByCatalogPath,
getVariableMetadata,
} from "../db/model/Variable.js"
Expand All @@ -38,6 +39,20 @@ import {
getMultiDimDataPageBySlug,
} from "../db/model/MultiDimDataPage.js"

const getGrapherConfigIdsByVariableIds = async (
knex: db.KnexReadonlyTransaction,
variableIds: number[]
) => {
const rows = await getGrapherConfigIdsForVariables(knex, variableIds)

return Object.fromEntries(
rows.map((row) => [
row.id,
row.grapherConfigIdAdmin ?? row.grapherConfigIdETL,
])
)
}

const resolveMultiDimDataPageCatalogPathsToIndicatorIds = async (
knex: db.KnexReadonlyTransaction,
rawConfig: MultiDimDataPageConfigRaw
Expand Down Expand Up @@ -206,6 +221,13 @@ export const renderMultiDimDataPageFromConfig = async (
await resolveMultiDimDataPageCatalogPathsToIndicatorIds(knex, rawConfig)
const config = MultiDimDataPageConfig.fromObject(preProcessedConfig)

// GET VARIABLE GRAPHER CONFIG UUIDS
const relevantVariableIds = getRelevantVariableIds(preProcessedConfig)
const variableIdToGrapherConfigMap = await getGrapherConfigIdsByVariableIds(
knex,
Array.from(relevantVariableIds)
)

// FAQs
const variableMetaDict =
await getRelevantVariableMetadata(preProcessedConfig)
Expand All @@ -224,6 +246,7 @@ export const renderMultiDimDataPageFromConfig = async (
const props = {
configObj: config.config,
tagToSlugMap: minimalTagToSlugMap,
variableIdToGrapherConfigMap,
faqEntries,
primaryTopic,
}
Expand Down
19 changes: 19 additions & 0 deletions db/model/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ interface VariableWithGrapherConfigs {
etl?: ChartConfigPair
}

type VariableWithGrapherConfigIds = Pick<
DbRawVariable,
"id" | "grapherConfigIdAdmin" | "grapherConfigIdETL"
>
export async function getGrapherConfigIdsForVariables(
knex: db.KnexReadonlyTransaction,
variableIds: number[]
): Promise<VariableWithGrapherConfigIds[]> {
const rows: VariableWithGrapherConfigIds[] = await knex(VariablesTableName)
.select([
"id",
"grapherConfigIdAdmin",
"grapherConfigIdETL",
] as (keyof DbRawVariable)[])
.whereIn("id", variableIds)

return rows
}

export async function getGrapherConfigsForVariable(
knex: db.KnexReadonlyTransaction,
variableId: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Dimension {
name: string
group?: string
description?: string
multi_select?: boolean
// multi_select?: boolean
choices: Choice[]
}

Expand Down Expand Up @@ -83,6 +83,7 @@ export interface MultiDimDataPageProps {
tagToSlugMap?: Record<string, string>
faqEntries?: FaqEntryKeyedByGdocIdAndFragmentId
primaryTopic?: PrimaryTopic | undefined
variableIdToGrapherConfigMap?: Record<number, string>

initialQueryStr?: string
canonicalUrl?: string
Expand Down

0 comments on commit 52c85bc

Please sign in to comment.