Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 (grapher) let chart configs inherit default values / TAS-565 #3782

Merged
merged 14 commits into from
Sep 3, 2024

Conversation

sophiamersmann
Copy link
Member

@sophiamersmann sophiamersmann commented Jul 9, 2024

Summary

Implements chart config inheritance and makes all chart configs inherit values from a parent config with default values.

Details

  • For all standalone charts, the full config is the patch config merged with the default layer
    • Whenever we save a new chart or update the existing one, we first compute the patch config by diffing against the parent/default config and then compute the full config by merging the patch with the parent
    • We usually operate on the full config, but we're baking the full config without defaults so that we're not baking the defaults again and again and again (makes a difference on explorer pages where we potentially bake tens/hundreds of configs)
    • We use the full patch config for version history. The way we currently do revisions doesn't always work1. Using the full config for version history fixes this, but has the disadvantage that "old" default values (that have since been updated) are copied over when a config is restored.
  • The default grapher config is derived from the schema and lives in the codebase. It can be generated by running the generate-default-object-from-schema.ts with the --save-ts flag
    • Whenever Grapher defaults are updated, the default config should be regenerated, and all full configs of stand-alone charts should be recomputed
    • We could have also stored the default layer in the database, but it's convenient to have it at hand in the codebase (where it's also type-checked). If some external entity needs the default config, they can access https://files.ourworldindata.org/schemas/grapher-schema.default.latest.json
  • Two new functions operate on chart configs and implement inheritance:
    • mergeGrapherConfigs: merges two or more patch configs
      • Some keys are excluded from inheritance ($schema, id, slug, version, isPublished)
      • Configs with different schema versions can't be merged
    • diffGrapherConfigs: diffs a given config against some reference config, where only values that are different in both configs are kept

Caveats

  • I left the chart revisions as is but now store the full config in the chart revisions table whenever a chart is saved – this could be a bit confusing
  • As I said above, storing the full config in version history comes at the cost of copying over old default values when a chart is restored – how big of a problem is that?

SVG tester

I dumped two new set of configs and reference charts for the SVG tester:

  • Patch configs
    • The config changes are due to diffing against the default layer (meaning, default values are removed)
    • There is one chart that is rendered differently, but it seems to be unrelated to the changes in this PR.
  • Full configs
    • Few charts are added and removed, probably because I refreshed the database in between
    • Other than that, no changes

To do

  • Add some real-world tests it's ok as is
  • Add a GitHub workflow that re-generates the default config and checks if the diff is clean
  • Delete DEFAULT_GRAPHER_CONFIG_SCHEMA

Footnotes

  1. When restoring a config from version history, we re-apply an old config version. But that doesn't take default values into account. For example, let's say the v1 version of a chart relies on the default value for hasMapTab and has thus no map tab. In v2, a map tab is added (the config now contains {..., hasMapTab: true}. Restoring v1 doesn't switch hasMapTab back to false since hasMapTab isn't contained in the v1 version of the config (since we were relying on the default).

Copy link
Member Author

sophiamersmann commented Jul 9, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @sophiamersmann and the rest of your teammates on Graphite Graphite

@owidbot
Copy link
Contributor

owidbot commented Jul 9, 2024

Quick links (staging server):

Site Admin Wizard

Login: ssh owid@staging-site-inherit-defaults

SVG tester:

Number of differences (default views): 0 ✅
Number of differences (all views): 0 ✅

Edited: 2024-08-09 16:04:41 UTC
Execution time: 1.23 seconds

@sophiamersmann sophiamersmann changed the base branch from grapher-schema-order to db-chart-configs July 9, 2024 14:20
@sophiamersmann sophiamersmann force-pushed the inherit-defaults branch 2 times, most recently from 0bde167 to 3dd6700 Compare July 9, 2024 16:00
@sophiamersmann sophiamersmann force-pushed the inherit-defaults branch 3 times, most recently from b4e2aba to 9b63a0e Compare July 10, 2024 12:14
@sophiamersmann sophiamersmann force-pushed the inherit-defaults branch 2 times, most recently from 1218965 to 06ec46a Compare July 10, 2024 13:03
@sophiamersmann sophiamersmann force-pushed the db-chart-configs branch 2 times, most recently from 7673208 to be4487e Compare July 15, 2024 13:53
@sophiamersmann sophiamersmann force-pushed the inherit-defaults branch 5 times, most recently from 6820c13 to 1595f80 Compare July 16, 2024 12:43
@sophiamersmann sophiamersmann changed the title 🔨 (grapher) charts inherit defaults 🔨 (grapher) let chart configs inherit default values / TAS-565 Jul 16, 2024
Copy link

Copy link
Contributor

@danyx23 danyx23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@sophiamersmann sophiamersmann changed the base branch from db-chart-configs to graphite-base/3782 September 3, 2024 07:25
@danyx23 danyx23 changed the base branch from graphite-base/3782 to master September 3, 2024 07:59
Copy link
Member Author

sophiamersmann commented Sep 3, 2024

Merge activity

@sophiamersmann sophiamersmann merged commit 3cc05db into master Sep 3, 2024
25 checks passed
@sophiamersmann sophiamersmann deleted the inherit-defaults branch September 3, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants