Skip to content

Commit

Permalink
Merge pull request #2651 from owid/fix-indicator-explorer-isReady
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber authored Oct 3, 2023
2 parents 6177c0e + 522b329 commit 0d1f1fe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 61 deletions.
87 changes: 45 additions & 42 deletions explorer/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -572,52 +572,55 @@ export class Explorer
config.dimensions = dimensions
if (config.ySlugs && yVariableIds) config.ySlugs += " " + yVariableIds

const inputTableTransformer = (table: OwidTable) => {
// add transformed (and intermediate) columns to the grapher table
if (uniqueSlugsInGrapherRow.length) {
const allColumnSlugs = uniq(
uniqueSlugsInGrapherRow.flatMap((slug) => [
...this.getBaseColumnsForColumnWithTransform(slug),
slug,
])
)
const existingColumnSlugs = table.columnSlugs
const outstandingColumnSlugs = allColumnSlugs.filter(
(slug) => !existingColumnSlugs.includes(slug)
)
const requiredColumnDefs = outstandingColumnSlugs
.map(
(slug) =>
this.columnDefsWithoutTableSlugByIdOrSlug[slug]
)
.filter(identity)
table = table.appendColumns(requiredColumnDefs)
}

// update column definitions with manually provided properties
table = table.updateDefs((def: OwidColumnDef) => {
const manuallyProvidedDef =
this.columnDefsWithoutTableSlugByIdOrSlug[def.slug] ?? {}
const mergedDef = { ...def, ...manuallyProvidedDef }

// update display properties
mergedDef.display = mergedDef.display ?? {}
if (manuallyProvidedDef.name)
mergedDef.display.name = manuallyProvidedDef.name
if (manuallyProvidedDef.unit)
mergedDef.display.unit = manuallyProvidedDef.unit
if (manuallyProvidedDef.shortUnit)
mergedDef.display.shortUnit = manuallyProvidedDef.shortUnit

return mergedDef
})
return table
}

grapher.setAuthoredVersion(config)
grapher.reset()
this.updateGrapherFromExplorerCommon()
grapher.updateFromObject(config)
grapher.forceDisableIntroAnimation = true
await grapher.downloadLegacyDataFromOwidVariableIds()

let grapherTable = grapher.inputTable

// add transformed (and intermediate) columns to the grapher table
if (uniqueSlugsInGrapherRow.length) {
const allColumnSlugs = uniq(
uniqueSlugsInGrapherRow.flatMap((slug) => [
...this.getBaseColumnsForColumnWithTransform(slug),
slug,
])
)
const existingColumnSlugs = grapherTable.columnSlugs
const outstandingColumnSlugs = allColumnSlugs.filter(
(slug) => !existingColumnSlugs.includes(slug)
)
const requiredColumnDefs = outstandingColumnSlugs
.map((slug) => this.columnDefsWithoutTableSlugByIdOrSlug[slug])
.filter(identity)
grapherTable = grapherTable.appendColumns(requiredColumnDefs)
}

// update column definitions with manually provided properties
grapherTable = grapherTable.updateDefs((def: OwidColumnDef) => {
const manuallyProvidedDef =
this.columnDefsWithoutTableSlugByIdOrSlug[def.slug] ?? {}
const mergedDef = { ...def, ...manuallyProvidedDef }

// update display properties
mergedDef.display = mergedDef.display ?? {}
if (manuallyProvidedDef.name)
mergedDef.display.name = manuallyProvidedDef.name
if (manuallyProvidedDef.unit)
mergedDef.display.unit = manuallyProvidedDef.unit
if (manuallyProvidedDef.shortUnit)
mergedDef.display.shortUnit = manuallyProvidedDef.shortUnit

return mergedDef
})

this.setGrapherTable(grapherTable)
await grapher.downloadLegacyDataFromOwidVariableIds(
inputTableTransformer
)
}

@action.bound private updateGrapherFromExplorerUsingColumnSlugs() {
Expand Down
46 changes: 27 additions & 19 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,6 @@ export class Grapher
@observable sortBy?: SortBy
@observable sortOrder?: SortOrder
@observable sortColumnSlug?: string
// TODO: this is a crude fix that is used to turn off the intro
// animation in maps (fading colors in from gray) because
// they end up with the wrong target colors (i.e. the colors
// are initially correct but then the animation screws them up).
// This flag can be removed once the animation bug is properly fixed.
@observable forceDisableIntroAnimation: boolean = false

owidDataset?: MultipleOwidVariableDataDimensionsMap = undefined // This is used for passing data for testing

Expand Down Expand Up @@ -770,26 +764,32 @@ export class Grapher
}

@action.bound
async downloadLegacyDataFromOwidVariableIds(): Promise<void> {
async downloadLegacyDataFromOwidVariableIds(
inputTableTransformer?: (table: OwidTable) => OwidTable
): Promise<void> {
if (this.variableIds.length === 0)
// No data to download
return

try {
let variablesDataMap: MultipleOwidVariableDataDimensionsMap
if (this.useAdminAPI) {
// TODO grapher model: switch this to downloading multiple data and metadata files
const variablesDataMap = await loadVariablesDataAdmin(
variablesDataMap = await loadVariablesDataAdmin(
this.dataApiUrlForAdmin,
this.variableIds
)
this._receiveOwidDataAndApplySelection(variablesDataMap)
} else {
const variablesDataMap = await loadVariablesDataSite(
variablesDataMap = await loadVariablesDataSite(
this.variableIds,
this.dataApiUrl
)
this._receiveOwidDataAndApplySelection(variablesDataMap)
}

this._receiveOwidDataAndApplySelection(
variablesDataMap,
inputTableTransformer
)
} catch (err) {
// eslint-disable-next-line no-console
console.log(`Error fetching '${err}'`)
Expand All @@ -811,15 +811,19 @@ export class Grapher

@action.bound private _setInputTable(
json: MultipleOwidVariableDataDimensionsMap,
legacyConfig: Partial<LegacyGrapherInterface>
legacyConfig: Partial<LegacyGrapherInterface>,
inputTableTransformer?: (table: OwidTable) => OwidTable
): void {
// TODO grapher model: switch this to downloading multiple data and metadata files
const { dimensions, table } = legacyToOwidTableAndDimensions(
json,
legacyConfig
)

this.inputTable = table
if (inputTableTransformer)
this.inputTable = inputTableTransformer(table)
else this.inputTable = table

// We need to reset the dimensions because some of them may have changed slugs in the legacy
// transformation (can happen when columns use targetTime)
this.setDimensionsFromConfigs(dimensions)
Expand All @@ -833,24 +837,28 @@ export class Grapher
} else this.applyOriginalSelectionAsAuthored()
}

@action rebuildInputOwidTable(): void {
@action rebuildInputOwidTable(
inputTableTransformer?: (table: OwidTable) => OwidTable
): void {
// TODO grapher model: switch this to downloading multiple data and metadata files
if (!this.legacyVariableDataJson) return
this._setInputTable(
this.legacyVariableDataJson,
this.legacyConfigAsAuthored
this.legacyConfigAsAuthored,
inputTableTransformer
)
}

@observable
private legacyVariableDataJson?: MultipleOwidVariableDataDimensionsMap

@action.bound private _receiveOwidDataAndApplySelection(
json: MultipleOwidVariableDataDimensionsMap
json: MultipleOwidVariableDataDimensionsMap,
inputTableTransformer?: (table: OwidTable) => OwidTable
): void {
this.legacyVariableDataJson = json

this.rebuildInputOwidTable()
this.rebuildInputOwidTable(inputTableTransformer)
}

@action.bound appendNewEntitySelectionOptions(): void {
Expand Down Expand Up @@ -1031,7 +1039,7 @@ export class Grapher
this.disposers.push(
reaction(
() => this.variableIds,
this.downloadLegacyDataFromOwidVariableIds
() => this.downloadLegacyDataFromOwidVariableIds()
)
)
const disposers = [
Expand Down Expand Up @@ -1614,7 +1622,7 @@ export class Grapher
}

@computed get disableIntroAnimation(): boolean {
return this.isExportingtoSvgOrPng || this.forceDisableIntroAnimation
return this.isExportingtoSvgOrPng
}

@computed get mapConfig(): MapConfig {
Expand Down

0 comments on commit 0d1f1fe

Please sign in to comment.