Skip to content

Commit

Permalink
feat(multi-dim): change indicator config format
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Jul 30, 2024
1 parent 9a9c507 commit fed44bb
Show file tree
Hide file tree
Showing 11 changed files with 1,872 additions and 1,807 deletions.
81 changes: 78 additions & 3 deletions baker/MultiDimBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import yaml from "yaml"
import fs from "fs-extra"
import path from "path"
import findProjectBaseDir from "../settings/findBaseDir.js"
import { MultiDimDataPageConfigType } from "../site/multiDim/MultiDimDataPageTypes.js"
import {
IndicatorEntryAfterPreProcessing,
IndicatorEntryBeforePreProcessing,
MultiDimDataPageConfigPreProcessed,
MultiDimDataPageConfigRaw,
MultiIndicatorEntry,
} from "../site/multiDim/MultiDimDataPageTypes.js"
import { MultiDimDataPageConfig } from "../site/multiDim/MultiDimDataPageConfig.js"
import * as db from "../db/db.js"
import { renderToHtmlPage } from "./siteRenderers.js"
import { MultiDimDataPage } from "../site/multiDim/MultiDimDataPage.js"
import React from "react"
import { BAKED_BASE_URL } from "../settings/clientSettings.js"
import { getTagToSlugMap } from "./GrapherBakingUtils.js"
import { getVariableIdsByCatalogPath } from "../db/model/Variable.js"

// TODO Make this dynamic
const baseDir = findProjectBaseDir(__dirname)
Expand All @@ -21,7 +28,7 @@ const readMultiDimConfig = (filename: string) =>
fs.readFileSync(path.join(MULTI_DIM_CONFIG_DIR, filename), "utf8")
)

const MULTI_DIM_SITES_BY_SLUG: Record<string, MultiDimDataPageConfigType> = {
const MULTI_DIM_SITES_BY_SLUG: Record<string, MultiDimDataPageConfigRaw> = {
"mdd-causes-of-death": readMultiDimConfig("causes-of-death.yml"),
"mdd-energy": readMultiDimConfig("energy.yml"),
"mdd-life-expectancy": readMultiDimConfig("life-expectancy.json"),
Expand All @@ -33,6 +40,72 @@ interface BakingAdditionalContext {
tagToSlugMap: Record<string, string>
}

const resolveMultiDimDataPageCatalogPathsToIndicatorIds = async (
knex: db.KnexReadonlyTransaction,
rawConfig: MultiDimDataPageConfigRaw
): Promise<MultiDimDataPageConfigPreProcessed> => {
const allCatalogPaths = rawConfig.views
.flatMap((view) =>
Object.values(view.indicators).flatMap((indicatorOrIndicators) =>
Array.isArray(indicatorOrIndicators)
? indicatorOrIndicators
: [indicatorOrIndicators]
)
)
.filter((indicator) => typeof indicator === "string")

const catalogPathToIndicatorIdMap = await getVariableIdsByCatalogPath(
allCatalogPaths,
knex
)

const missingCatalogPaths = new Set(
allCatalogPaths.filter(
(indicator) => !catalogPathToIndicatorIdMap.has(indicator)
)
)

if (missingCatalogPaths.size > 0) {
console.warn(
`Could not find the following catalog paths in the database: ${Array.from(
missingCatalogPaths
).join(", ")}`
)
}

const resolveSingleField = (
indicator: IndicatorEntryBeforePreProcessing
) => {
if (typeof indicator === "string") {
const indicatorId = catalogPathToIndicatorIdMap.get(indicator)
return indicatorId ?? undefined
} else {
return indicator
}
}

const resolveField = (
indicator: MultiIndicatorEntry<IndicatorEntryBeforePreProcessing>
): MultiIndicatorEntry<IndicatorEntryAfterPreProcessing> => {
if (Array.isArray(indicator)) {
return indicator.map(resolveSingleField)
} else {
return resolveSingleField(indicator)
}
}

for (const view of rawConfig.views) {
view.indicators = Object.fromEntries(
Object.entries(view.indicators).map(([key, value]) => [
key,
resolveField(value),
])
) as any
}

return rawConfig as MultiDimDataPageConfigPreProcessed
}

export const renderMultiDimDataPageBySlug = async (
knex: db.KnexReadonlyTransaction,
slug: string
Expand All @@ -43,7 +116,9 @@ export const renderMultiDimDataPageBySlug = async (
const tagToSlugMap = await getTagToSlugMap(knex)
const bakingContext = { tagToSlugMap }

const config = MultiDimDataPageConfig.fromObject(rawConfig)
const preProcessedConfig =
await resolveMultiDimDataPageCatalogPathsToIndicatorIds(knex, rawConfig)
const config = MultiDimDataPageConfig.fromObject(preProcessedConfig)
return renderMultiDimDataPage(config, bakingContext)
}

Expand Down
Loading

0 comments on commit fed44bb

Please sign in to comment.