Skip to content

Commit

Permalink
🔨 (grapher) charts inherit defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Jul 9, 2024
1 parent f062e76 commit c5e8003
Show file tree
Hide file tree
Showing 5 changed files with 543 additions and 0 deletions.
165 changes: 165 additions & 0 deletions db/migration/1720449698768-AddDefaultChartConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { MigrationInterface, QueryRunner } from "typeorm"
import {
diffGrapherConfigs,
GrapherInterface,
mergeGrapherConfigs,
} from "@ourworldindata/utils"

const uuid = "d4fd6977-3dc1-11ef-8ef2-0242ac120002"

const defaultConfig = {
$schema: "https://files.ourworldindata.org/schemas/grapher-schema.004.json",
version: 1,
isPublished: false,
type: "LineChart",
logo: "owid",
tab: "chart",
hasChartTab: true,
hasMapTab: false,
minTime: "earliest",
maxTime: "latest",
xAxis: {
scaleType: "linear",
canChangeScaleType: false,
facetDomain: "shared",
removePointsOutsideDomain: false,
},
yAxis: {
scaleType: "linear",
canChangeScaleType: false,
facetDomain: "shared",
removePointsOutsideDomain: false,
},
baseColorScheme: "default",
invertColorScheme: false,
colorScale: {
baseColorScheme: "default",
colorSchemeInvert: false,
binningStrategy: "ckmeans",
binningStrategyBinCount: 5,
equalSizeBins: true,
customNumericColorsActive: false,
},
addCountryMode: "add-country",
entityType: "country or region",
entityTypePlural: "countries",
matchingEntitiesOnly: false,
missingDataStrategy: "auto",
selectedFacetStrategy: "none",
facettingLabelByYVariables: "metric",
stackMode: "absolute",
sortBy: "total",
sortOrder: "desc",
scatterPointLabelStrategy: "year",
compareEndPointsOnly: false,
zoomToSelection: false,
showYearLabels: false,
showNoDataArea: true,
hideLegend: false,
hideLogo: false,
hideTimeline: false,
hideRelativeToggle: true,
hideConnectedScatterLines: false,
hideLinesOutsideTolerance: false,
hideTotalValueLabel: false,
hideScatterLabels: false,
hideFacetControl: true,
hideAnnotationFieldsInTitle: {
entity: false,
time: false,
changeInPrefix: false,
},
map: {
projection: "World",
time: "latest",
timeTolerance: 0,
toleranceStrategy: "closest",
colorScale: {
baseColorScheme: "default",
colorSchemeInvert: false,
binningStrategy: "ckmeans",
binningStrategyBinCount: 5,
equalSizeBins: true,
customNumericColorsActive: false,
},
tooltipUseCustomLabels: false,
hideTimeline: false,
},
} as unknown as GrapherInterface // TODO: type

export class AddDefaultChartConfig1720449698768 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const defaultConfigJson = JSON.stringify(defaultConfig)

// add default chart config as a new row in the chart_configs table
await queryRunner.query(
`-- sql
INSERT INTO chart_configs (id, patchConfig, config) VALUES (UUID_TO_BIN(?, 1), ?, ?)
`,
[uuid, defaultConfigJson, defaultConfigJson]
)

const charts = (await queryRunner.query(
`-- sql
SELECT id, uuid, config FROM chart_configs
WHERE uuid != ?
`,
[uuid]
)) as { id: string; uuid: string; config: string }[]

for (const chart of charts) {
const originalConfig = JSON.parse(chart.config)

// if the schema is missing, we assume it's the current one
if (!originalConfig["$schema"]) {
originalConfig["$schema"] =
"https://files.ourworldindata.org/schemas/grapher-schema.004.json"
}

const patchConfig = diffGrapherConfigs(
originalConfig,
defaultConfig
)
const fullConfig = mergeGrapherConfigs(
defaultConfig,
originalConfig
)

await queryRunner.query(
`-- sql
UPDATE chart_configs
SET
patchConfig = ?,
config = ?
WHERE id = ?
`,
[
JSON.stringify(patchConfig),
JSON.stringify(fullConfig),
chart.id,
]
)
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`-- sql
DELETE FROM chart_configs
WHERE uuid = ?
`,
[uuid]
)

// recover the original configs from the charts table
await queryRunner.query(
`-- sql
UPDATE chart_configs cc
JOIN charts c
SET
cc.patchConfig = c.config
cc.config = c.config
`
)
}
}
4 changes: 4 additions & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
maxBy,
memoize,
merge,
mergeWith,
min,
minBy,
noop,
Expand Down Expand Up @@ -102,6 +103,7 @@ export {
isNil,
isNull,
isNumber,
isPlainObject,
isString,
isUndefined,
keyBy,
Expand All @@ -110,6 +112,7 @@ export {
maxBy,
memoize,
merge,
mergeWith,
min,
minBy,
noop,
Expand Down Expand Up @@ -1748,6 +1751,7 @@ export function filterValidStringValues<ValidValue extends string>(
}

// TODO: type this correctly once we have moved types into their own top level package
// TODO: remove in favour of the new function
export function mergePartialGrapherConfigs<T extends Record<string, any>>(
...grapherConfigs: (T | undefined)[]
): T {
Expand Down
Loading

0 comments on commit c5e8003

Please sign in to comment.