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

Add data structures for chart plugin system #6028

Merged
merged 20 commits into from Oct 9, 2018

Conversation

kristw
Copy link
Contributor

@kristw kristw commented Oct 3, 2018

Classes

  • ChartMetadata captures the definition of each visualization: name, description, thumbnail image.
  • Plugin a generic plugin class. All plugins should derive from this class. A plugin can be .configure() to set/override configuration.
  • ChartPlugin this is how each type of visualization is defined.
  • Preset holds one or more presets and or plugins.

Registry is a key-value stores that facilitate plugin installation.

  • Each item can be a value or loader.
  • Developer register the value or loader function using .registerValue(key, value) and .registerLoader(key, loader), respectively.
  • registry.get(key) will returns the value or execute the loader function and returns the result.
  • registry.getAsPromise(key) wraps the .get() result into a Promise, which is useful for handling lazy-loading imports and treat both sync and async items the same way.

ChartMetadataRegistry, ChartComponentRegistry and ChartTransformPropsRegistry are extended from the registry base classes above to hold metadata, the visualization component and transformProps (function that transform formData into compatible parameters for each chart), respectively.

Utility functions

  • makeSingleton : create a function to get a singleton of the given class (aka. getInstance)
  • isRequired : A function that throw error. Can be used for requiring certain function parameter by setting isRequired('fieldName') as default value.

After this PR is in, each visualization can be defined as a plugin using the data structures above.

Unit tests

isRequired(field)
  ✓ should throw error with the given field in the message

makeSingleton()
  makeSingleton(BaseClass)
    ✓ returns a function for getting singleton instance of a given base class
    ✓ returned function returns same instance across all calls
  makeSingleton(BaseClass, ...args)
    ✓ returns a function for getting singleton instance of a given base class constructed with the given arguments
    ✓ returned function returns same instance across all calls

Registry
  ✓ exists
  new Registry(name)
    ✓ can create a new registry when name is not given
    ✓ can create a new registry when name is given
  .has(key)
    ✓ returns true if an item with the given key exists
    ✓ returns false if an item with the given key does not exist
  .registerValue(key, value)
    ✓ registers the given value with the given key
    ✓ returns the registry itself
  .registerLoader(key, loader)
    ✓ registers the given loader with the given key
    ✓ returns the registry itself
  .get(key)
    ✓ given the key, returns the value if the item is a value
    ✓ given the key, returns the result of the loader function if the item is a loader
    ✓ returns null if the item with specified key does not exist
    ✓ If the key was registered multiple times, returns the most recent item.
  .getAsPromise(key)
    ✓ given the key, returns a promise of item value if the item is a value
    ✓ given the key, returns a promise of result of the loader function if the item is a loader
    ✓ returns a rejected promise if the item with specified key does not exist
    ✓ If the key was registered multiple times, returns a promise of the most recent item.
  .keys()
    ✓ returns an array of keys
  .entries()
    ✓ returns an array of { key, value }
  .entriesAsPromise()
    ✓ returns a Promise of an array { key, value }
  .remove(key)
    ✓ removes the item with given key
    ✓ does not throw error if the key does not exist
    ✓ returns itself
Plugin
  ✓ exists
  new Plugin()
    ✓ creates a new plugin
  .configure(config, replace)
    ✓ extends the default config with given config when replace is not set or false
    ✓ replaces the default config with given config when replace is true
    ✓ returns the plugin itself
  .resetConfig()
    ✓ resets config back to default
    ✓ returns the plugin itself

ChartPlugin
  ✓ exists
  new ChartPlugin()
    ✓ creates a new plugin
    ✓ throws an error if metadata is not specified
    ✓ throws an error if none of Chart or loadChart is specified
  .register(key)
    ✓ throws an error if key is not provided
    ✓ returns itself

Preset
  ✓ exists
  new Preset()
    ✓ creates new preset
  .register()
    ✓ register all listed presets then plugins
    ✓ returns itself

@williaster @conglei @graceguo-supercat @michellethomas @mistercrunch @xtinec

if (plugin instanceof Plugin) {
plugin.install();
} else {
plugin();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain a bit when plugin will be just a function ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This I could get some feedback. The issue was when someone needs to call plugin.install(...) with arguments. I couldn't figure out a nice API design to let people pass arguments, so I chose this one.

for example, core chart presets could contains

plugins: [
  () => new BarChartPlugin().install('bar'),
  () => new PieChartPlugin().install('pie'),
]

Copy link
Contributor

@williaster williaster Oct 5, 2018

Choose a reason for hiding this comment

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

could you address this by having the Plugin take the config arguments via the constructor instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha. good idea.

Copy link
Contributor Author

@kristw kristw Oct 5, 2018

Choose a reason for hiding this comment

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

Umm, actually can't, because each vis will declare the ChartPlugin instance and export, so the preset only have access to the instance.

barPlugin.js

const plugin = new ChartPlugin(...);
export default plugin;

preset.js

import barPlugin from './barPlugin';
...
plugins: [barPlugin]
...

Copy link
Contributor Author

@kristw kristw Oct 5, 2018

Choose a reason for hiding this comment

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

Umm, on second thought, I can add .configure to the Plugin class

plugins: [
  plugin1,
  plugin2.configure({ key: 'abc' }),
]

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, could the pattern instead be to export a class that has extended ChartPlugin?

// BarPlugin.js
class BarPlugin extends ChartPlugin {
  ...
}

export default BarPlugin;

// preset.js
import BarPlugin from './BarPlugin';
...
const plugins = [
  new BarPlugin({ config: 123 }),
];
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That could work too. At first I was like hmm is creating new class an overkill. Perhaps not. Just a bit more typing. and that allows override .register() function + more customization.

class BarPlugin extends ChartPlugin {
  constructor(config) {
    super({ config, metadata, Chart, transformProps });
  }
}

vs

const barPlugin = new ChartPlugin({ metadata, Chart, transformProps });
barPlugin.configure(config);

Copy link
Contributor

@conglei conglei left a comment

Choose a reason for hiding this comment

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

LGTM! and thanks for adding all the test!!

@@ -0,0 +1,16 @@
import Registry from './Registry';

export default class LoaderRegistry extends Registry {
Copy link
Contributor

Choose a reason for hiding this comment

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

would it simplify things to get rid of this class and just have the parent Registry class have first class support for loaders out of the box?

Copy link
Contributor

Choose a reason for hiding this comment

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

especially if the Registry class already supports promises

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think simple registry could be useful for sync items like metadata. We can use this for other key-value stores we may have in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I meant have Registry support sync and async all in one class. Like take either a loader or a value, and have the same promise-based API for getting either of them so you don't have to juggle different classes and different methods like get and getAsPromise.

Copy link
Contributor Author

@kristw kristw Oct 5, 2018

Choose a reason for hiding this comment

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

One distinction of the LoaderRegistry from Registry is it overrides .register() to wrap returned value in a function.

Full disclosure. I started out with Registry alone and at some point I encounter value that is a function (e.g. a class) and it gets quite confusing whether the value can be returned immediately or it is a loader that needs to be executed. If both values and loaders can be stored in the same registry, I need another mechanism to indicate which is which. Separating into two classes help simplify that it is either just values or just loaders.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am revisiting the idea of having both in same Registry again. May have to add explicit flag to keep track if it is a loader or not. You might have convinced me that one class that can do both will be better.

// use loadTransformProps for dynamic import (lazy-loading)
loadTransformProps,

// use Chart for immediate value
Copy link
Contributor

@williaster williaster Oct 5, 2018

Choose a reason for hiding this comment

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

can you explain a little more when you would want to use Chart over loadChart, or transformProps over loadTransformProps? especially since under the hood it seems that they are all transformed to loader funcs anyway?

Copy link
Contributor Author

@kristw kristw Oct 5, 2018

Choose a reason for hiding this comment

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

If the chart is very small. Use Chart, this will include Chart in the bundle instead of spinning it of into a lazy-loaded bundle.

import MyChart from './MyChart';
...
Chart: MyChart,
...

if the chart is gigantic. Use loadChart.

loadChart: () => import('./MyChart.jsx')

@codecov-io
Copy link

codecov-io commented Oct 8, 2018

Codecov Report

Merging #6028 into master will increase coverage by 0.06%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6028      +/-   ##
==========================================
+ Coverage   77.74%   77.81%   +0.06%     
==========================================
  Files          46       46              
  Lines        9412     9446      +34     
==========================================
+ Hits         7317     7350      +33     
- Misses       2095     2096       +1
Impacted Files Coverage Δ
superset/views/core.py 73.82% <0%> (-0.1%) ⬇️
superset/models/core.py 85.1% <0%> (+0.09%) ⬆️
superset/cache_util.py 54.28% <0%> (+0.11%) ⬆️
superset/connectors/sqla/models.py 81.05% <0%> (+0.59%) ⬆️
superset/db_engine_specs.py 55.87% <0%> (+1.11%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1ee08fc...18eae1b. Read the comment docs.

Copy link
Contributor

@williaster williaster left a comment

Choose a reason for hiding this comment

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

this is doooooooope, really like how it turned out! I had one comment about the behavior when registering duplicate values but otherwise LGTM 🙌

}

resetConfig() {
// The child class can set default config
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

return this;
}

configure(config, replace = false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I dig the replace 👍


registerValue(key, value) {
this.items[key] = { value };
delete this.promises[key];
Copy link
Contributor

Choose a reason for hiding this comment

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

I think my only comment at this point is whether we should throw an error if a value already exists, that would require people to consciously call .remove() and possibly raise this to their attn if they weren't aware of it?

or we could just log a warning 🤔 wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may happen a lot though for the presets. For example, registering a preset that contains all common charts then override bar chart with a new plugin. Some presets may reference another preset and override certain charts.

I think throwing an error would be too harsh.
Can add warning but then that will mean for every plugin we have to add .remove() call before .register() to avoid the warning. If every plugin does that, would a warning still be useful?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh that's a fair point, sgtm 👍

@williaster williaster merged commit cd2c46a into apache:master Oct 9, 2018
@kristw kristw deleted the kristw-loader branch October 9, 2018 19:14
betodealmeida pushed a commit to lyft/incubator-superset that referenced this pull request Oct 12, 2018
* add unit tests

* add test structure

* add unit tests for Registry

* add LoaderRegistry unit test

* add unit test for makeSingleton

* add type check

* add plugin data structures

* simplify API

* add preset tests

* update test message

* fix lint

* update makeSingleton

* update Plugin, Preset and unit test

* revise Registry code

* update unit test, add remove function

* update test

* update unit test

* update plugin unit test

* add .keys(), .entries() and .entriesAsPromise()

* update test description
bipinsoniguavus pushed a commit to ThalesGroup/incubator-superset that referenced this pull request Dec 26, 2018
* add unit tests

* add test structure

* add unit tests for Registry

* add LoaderRegistry unit test

* add unit test for makeSingleton

* add type check

* add plugin data structures

* simplify API

* add preset tests

* update test message

* fix lint

* update makeSingleton

* update Plugin, Preset and unit test

* revise Registry code

* update unit test, add remove function

* update test

* update unit test

* update plugin unit test

* add .keys(), .entries() and .entriesAsPromise()

* update test description
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.28.0 labels Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.28.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants