Skip to content

Commit

Permalink
🔨 extract fetching functionality into FetchingGrapher
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Jan 10, 2025
1 parent 22c3e09 commit 4949ca3
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 177 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
},
"Prettier-SQL.keywordCase": "upper",
"Prettier-SQL.SQLFlavourOverride": "mysql",
"Prettier-SQL.expressionWidth": 80
"Prettier-SQL.expressionWidth": 80,
"prettier.semi": false
}
3 changes: 2 additions & 1 deletion adminSiteClient/GrapherConfigGridEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ export class GrapherConfigGridEditor extends React.Component<GrapherConfigGridEd
// the same country selection as you zap through the variables
this.grapher.clearSelection()
this.grapher.updateFromObject(newConfig)
this.grapher.downloadData()
// TODO: ensure data is downloaded
// this.grapher.downloadData()
} else this.grapherElement = <Grapher {...newConfig} />
}

Expand Down
6 changes: 3 additions & 3 deletions baker/GrapherImageBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function bakeGrapherToSvgAndPng(
optimizeSvgs = false
) {
const grapher = initGrapherForSvgExport(jsonConfig)
grapher.receiveOwidData(vardata)
// grapher.receiveOwidData(vardata)
const outPath = path.join(outDir, grapher.slug as string)

let svgCode = grapher.staticSVG
Expand Down Expand Up @@ -125,7 +125,7 @@ export async function bakeGrapherToSvg(
if (fs.existsSync(outPath) && !overwriteExisting) return
const variableIds = grapher.dimensions.map((d) => d.variableId)
const vardata = await getDataForMultipleVariables(variableIds)

Check warning on line 127 in baker/GrapherImageBaker.tsx

View workflow job for this annotation

GitHub Actions / eslint

'vardata' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 127 in baker/GrapherImageBaker.tsx

View workflow job for this annotation

GitHub Actions / eslint

'vardata' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 127 in baker/GrapherImageBaker.tsx

View workflow job for this annotation

GitHub Actions / eslint

'vardata' is assigned a value but never used. Allowed unused vars must match /^_/u
grapher.receiveOwidData(vardata)
// grapher.receiveOwidData(vardata)

let svgCode = grapher.staticSVG
if (optimizeSvgs) svgCode = await optimizeSvg(svgCode)
Expand Down Expand Up @@ -234,6 +234,6 @@ export async function grapherToSVG(
const grapher = new Grapher({ ...jsonConfig, manuallyProvideData: true })
grapher.isExportingToSvgOrPng = true
grapher.shouldIncludeDetailsInStaticExport = false
grapher.receiveOwidData(vardata)
// grapher.receiveOwidData(vardata)
return grapher.staticSVG
}
3 changes: 2 additions & 1 deletion baker/updateChartEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ const obtainAvailableEntitiesForGrapherConfig = async (
await getVariableDataUsingCache(variableId),
])
)
grapher.receiveOwidData(variableData)
// TODO: make sure that data is loaded here
// grapher.receiveOwidData(variableData)

// If the grapher has a chart tab, then the available entities there are the "most interesting" ones to us
if (grapher.hasChartTab) {
Expand Down
2 changes: 1 addition & 1 deletion devTools/svgTester/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export async function renderSvg(
{ shouldHashQueryStr: false, separator: "?" }
)

grapher.receiveOwidData(configAndData.variableData)
// grapher.receiveOwidData(configAndData.variableData)
const durationReceiveData = Date.now() - timeStart

const svg = grapher.staticSVG
Expand Down
10 changes: 5 additions & 5 deletions packages/@ourworldindata/explorer/src/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ export class Explorer
grapher.setAuthoredVersion(config)
grapher.reset()
grapher.updateFromObject(config)
grapher.downloadData()
// grapher.downloadData()
}

@action.bound async updateGrapherFromExplorerUsingVariableIds() {
Expand Down Expand Up @@ -745,11 +745,11 @@ export class Explorer
if (dimensions.length === 0) {
// If dimensions are empty, explicitly set the table to an empty table
// so we don't end up confusingly showing stale data from a previous chart
grapher.receiveOwidData(new Map())
// grapher.receiveOwidData(new Map())
} else {
await grapher.downloadLegacyDataFromOwidVariableIds(
inputTableTransformer
)
// await grapher.downloadLegacyDataFromOwidVariableIds(
// inputTableTransformer
// )
}
}

Expand Down
170 changes: 170 additions & 0 deletions packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import {
GrapherInterface,
MultipleOwidVariableDataDimensionsMap,
OwidVariableDataMetadataDimensions,
} from "@ourworldindata/types"
import React from "react"
import { Grapher } from "./Grapher.js"
import { loadVariableDataAndMetadata } from "./loadVariable.js"
import { legacyToOwidTableAndDimensions } from "./LegacyToOwidTable.js"
import { OwidTable } from "@ourworldindata/core-table"

export interface FetchingGrapherProps {
config?: GrapherInterface
configUrl?: string
queryString?: string
dataApiUrl: string
adminBaseUrl: string
bakedGrapherURL: string
}
export function FetchingGrapher(
props: FetchingGrapherProps
): JSX.Element | null {
// if config is not provided, fetch it from configUrl
console.log("FetchingGrapher")

Check warning on line 24 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 24 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 24 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

const [config, setConfig] = React.useState<GrapherInterface | undefined>(
props.config
)

const [inputTable, setInputTable] = React.useState<OwidTable | undefined>(
undefined
)

React.useEffect(() => {
async function fetchConfigAndLoadData(): Promise<void> {
console.log("fetchConfigAndLoadData", props.configUrl)

Check warning on line 36 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 36 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 36 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement
if (!config && props.configUrl) {
const fetchedConfig = await fetch(props.configUrl).then((res) =>
res.json()
)
setConfig(fetchedConfig)
}
console.log("fetchConfigAndLoadData: config", config)

Check warning on line 43 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 43 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 43 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement
if (!config) return
const dimensions = config.dimensions || []
if (dimensions.length === 0) return
const variables = dimensions.map((d) => d.variableId)
const variablesDataMap = await loadVariablesDataSite(
variables,
props.dataApiUrl
)
const inputTable = legacyToOwidTableAndDimensions(
variablesDataMap,
dimensions
)
console.log("setting input table")

Check warning on line 56 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 56 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

Check warning on line 56 in packages/@ourworldindata/grapher/src/core/FetchingGrapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement
setInputTable(inputTable)
}
void fetchConfigAndLoadData()
}, [props.configUrl, config, props.dataApiUrl])

if (!config) return null
if (!inputTable) return null
return (
<Grapher
table={inputTable}
queryStr={props.queryString}
dataApiUrl={props.dataApiUrl}
adminBaseUrl={props.adminBaseUrl}
bakedGrapherURL={props.bakedGrapherURL}
/>
)
}

// async function loadVariablesDataAdmin(
// variableFetchBaseUrl: string | undefined,
// variableIds: number[]
// ): Promise<MultipleOwidVariableDataDimensionsMap> {
// const dataFetchPath = (variableId: number): string =>
// variableFetchBaseUrl
// ? `${variableFetchBaseUrl}/v1/variableById/data/${variableId}`
// : `/api/data/variables/data/${variableId}.json`
// const metadataFetchPath = (variableId: number): string =>
// variableFetchBaseUrl
// ? `${variableFetchBaseUrl}/v1/variableById/metadata/${variableId}`
// : `/api/data/variables/metadata/${variableId}.json`

// const loadVariableDataPromises = variableIds.map(async (variableId) => {
// const dataPromise = window.admin.getJSON(
// dataFetchPath(variableId)
// ) as Promise<OwidVariableMixedData>
// const metadataPromise = window.admin.getJSON(
// metadataFetchPath(variableId)
// ) as Promise<OwidVariableWithSourceAndDimension>
// const [data, metadata] = await Promise.all([
// dataPromise,
// metadataPromise,
// ])
// return { data, metadata: { ...metadata, id: variableId } }
// })
// const variablesData: OwidVariableDataMetadataDimensions[] =
// await Promise.all(loadVariableDataPromises)
// const variablesDataMap = new Map(
// variablesData.map((data) => [data.metadata.id, data])
// )
// return variablesDataMap
// }

async function loadVariablesDataSite(
variableIds: number[],
dataApiUrl: string
): Promise<MultipleOwidVariableDataDimensionsMap> {
const loadVariableDataPromises = variableIds.map((variableId) =>
loadVariableDataAndMetadata(variableId, dataApiUrl)
)
const variablesData: OwidVariableDataMetadataDimensions[] =
await Promise.all(loadVariableDataPromises)
const variablesDataMap = new Map(
variablesData.map((data) => [data.metadata.id, data])
)
return variablesDataMap
}

// function downloadData(): void {
// if (this.manuallyProvideData) {
// } else if (this.owidDataset) {
// this._receiveOwidDataAndApplySelection(this.owidDataset)
// } else void this.downloadLegacyDataFromOwidVariableIds()
// }

// async function downloadLegacyDataFromOwidVariableIds(
// inputTableTransformer?: ChartTableTransformer
// ): Promise<void> {
// if (this.variableIds.length === 0)
// // No data to download
// return

// try {
// let variablesDataMap: MultipleOwidVariableDataDimensionsMap

// const startMark = performance.now()
// if (this.useAdminAPI) {
// // TODO grapher model: switch this to downloading multiple data and metadata files
// variablesDataMap = await loadVariablesDataAdmin(
// this.dataApiUrlForAdmin,
// this.variableIds
// )
// } else {
// variablesDataMap = await loadVariablesDataSite(
// this.variableIds,
// this.dataApiUrl
// )
// }
// this.createPerformanceMeasurement("downloadVariablesData", startMark)

// this._receiveOwidDataAndApplySelection(
// variablesDataMap,
// inputTableTransformer
// )
// } catch (err) {
// // eslint-disable-next-line no-console
// console.log(`Error fetching '${err}'`)
// console.error(err)
// Bugsnag?.notify(`Error fetching variables: ${err}`, (event) => {
// event.addMetadata("context", {
// variableIds: this.variableIds,
// })
// })
// }
// }
Loading

0 comments on commit 4949ca3

Please sign in to comment.