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

feat: Implement currencies formatter for saved metrics #24517

Merged
merged 14 commits into from
Jun 28, 2023

Conversation

kgabryje
Copy link
Member

@kgabryje kgabryje commented Jun 26, 2023

SUMMARY

WARNING: This PR might be considered a breaking change. Currently the d3formats defined in dataset editor for saved metrics worked only in Table and Pivot Table charts. Now they will also work in viz types listed below and they take priority over d3 formats defined in control panel. That means that charts that use metrics that have custom formats defined on dataset level might change their displayed number formats.

Enables users to set a currency formatter for saved metrics in Datasource edit modal.

Currently supported currencies are: "USD", "EUR", "GBP", "INR", "MXN", "JPY", "CNY" (+ user can manually type other currency codes in the currency picker). More currency codes can be added in config.py by modifying array CURRENCIES.

Currently supported charts are: Table, Pivot Table, Big Number (+ with trendline), (Time)Series Echarts, Pie, Gauge, Funnel, Treemap.

The number is formatted using the d3 format specified either for saved metric in datasource editor (takes priority) or in control panel. If user picks a d3 formatter that adds % or currency signs, the special signs are ignored.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Screen.Recording.2023-06-26.at.17.45.13.mov

TESTING INSTRUCTIONS

  1. If you're running this PR locally, run the migration first
  2. Open 1 of the supported charts
  3. Open datasource editor, add/edit a saved metric and set a currency
  4. Use the metric with currency in the chart
  5. Verify that values are formatted with currency sign

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided:
      Current: 0.13 s
      10+: 0.12 s
      100+: 0.12 s
      1000+: 0.13 s
  • Introduces new feature or API
  • Removes existing feature or API

CC @yousoph

@kgabryje kgabryje requested a review from a team as a code owner June 26, 2023 15:47
@codecov
Copy link

codecov bot commented Jun 26, 2023

Codecov Report

Merging #24517 (adc04ee) into master (a846e8a) will increase coverage by 0.02%.
The diff coverage is 71.54%.

❗ Current head adc04ee differs from pull request most recent head d59b9de. Consider uploading reports for the commit d59b9de to get more accurate results

@@            Coverage Diff             @@
##           master   #24517      +/-   ##
==========================================
+ Coverage   58.22%   58.24%   +0.02%     
==========================================
  Files        1903     1906       +3     
  Lines       74026    74129     +103     
  Branches     8118     8155      +37     
==========================================
+ Hits        43100    43178      +78     
- Misses      28815    28832      +17     
- Partials     2111     2119       +8     
Flag Coverage Δ
hive 53.90% <85.00%> (+0.01%) ⬆️
javascript 55.78% <68.93%> (+0.03%) ⬆️
presto 53.80% <85.00%> (+0.01%) ⬆️
python 60.91% <85.00%> (+0.01%) ⬆️
unit 54.66% <85.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ackages/superset-ui-chart-controls/src/fixtures.ts 100.00% <ø> (ø)
...d/packages/superset-ui-chart-controls/src/types.ts 100.00% <ø> (ø)
...ges/superset-ui-core/src/query/types/Datasource.ts 100.00% <ø> (ø)
...ackages/superset-ui-core/src/query/types/Metric.ts 100.00% <ø> (ø)
...rts/src/BigNumber/BigNumberTotal/transformProps.ts 0.00% <0.00%> (ø)
...lugin-chart-echarts/src/Timeseries/transformers.ts 56.20% <ø> (ø)
...plugins/plugin-chart-echarts/src/utils/forecast.ts 93.22% <ø> (ø)
...d/plugins/plugin-chart-echarts/src/utils/series.ts 85.48% <ø> (ø)
...s/plugin-chart-pivot-table/src/PivotTableChart.tsx 0.00% <0.00%> (ø)
...gin-chart-pivot-table/src/plugin/transformProps.ts 42.30% <0.00%> (ø)
... and 29 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

First pass comments. Really neat feature, I think this must be in the top 10 list of feature requests of all time, thanks for working on this! 🙂

d3Format: string | undefined,
labelMap: Record<string, string[]> = {},
seriesNames: string[] = [],
) => {
Copy link
Member

Choose a reason for hiding this comment

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

It seems the return type is { [key: string]: CurrencyFormatter | NumberFormatter }, could we add that explicitly here to make it easier to read?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point

customFormatters: Record<string, NumberFormatter | CurrencyFormatter>,
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
key?: string,
) => {
Copy link
Member

Choose a reason for hiding this comment

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

same here

Comment on lines 152 to 160
getCustomFormatter(
buildCustomFormatters(
metric,
currencyFormats,
columnFormats,
numberFormat,
),
metric,
) ?? getNumberFormatter(numberFormat);
Copy link
Member

Choose a reason for hiding this comment

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

At the risk of over-abstraction, would it make sense to have yet one more helper that simplifies this call chain? This code block seems to be repeated a few times so abstracting it would make it slightly more DRY and less heavy on the eyes. Something like

  const numberFormatter =
    getFormatter(
      metric,
      currencyFormats,
      columnFormats,
      numberFormat,
    );

Copy link
Member Author

Choose a reason for hiding this comment

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

💯

@kgabryje kgabryje requested review from eschutho and geido June 27, 2023 15:06
Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

LGTM with some optional comments

Comment on lines 56 to 58
hasValidCurrency() {
return this.currency?.symbol;
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm going on about return types again 😆 To be precise, shouldn't this be

  hasValidCurrency(): boolean {
    return Boolean(this.currency?.symbol);
  }

// @ts-ignore
currency: { symbol: 'USD' },
});
expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBeTruthy();
Copy link
Member

Choose a reason for hiding this comment

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

...and if we made that change up there, this would be

Suggested change
expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBeTruthy();
expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBe(true);

return key ? customFormatters[key] : undefined;
};

export const getFormatter = (
Copy link
Member

Choose a reason for hiding this comment

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

For the sake of consistency, should this be getValueFormatter?

Comment on lines 99 to 100

// const initialDatasource = dataset;
Copy link
Member

Choose a reason for hiding this comment

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

I assume this is redundant

Suggested change
// const initialDatasource = dataset;
// const initialDatasource = dataset;

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦

@kgabryje
Copy link
Member Author

/testenv up

@github-actions
Copy link
Contributor

@kgabryje Ephemeral environment spinning up at http://18.237.178.241:8080. Credentials are admin/admin. Please allow several minutes for bootstrapping and startup.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

Restamping, looks great!

@kgabryje kgabryje added the risk:breaking-change Issues or PRs that will introduce breaking changes label Jun 28, 2023
@kgabryje kgabryje merged commit 83ff4cd into apache:master Jun 28, 2023
29 checks passed
@github-actions
Copy link
Contributor

Ephemeral environment shutdown and build artifacts deleted.

@eschutho
Copy link
Member

eschutho commented Jul 5, 2023

@kgabryje given that this could be considered a breaking change, wdyt about trying to cherry this PR into 3.0, along with #24594? cc @michael-s-molina and @rusackas

@michael-s-molina
Copy link
Member

@kgabryje given that this could be considered a breaking change, wdyt about trying to cherry this PR into 3.0, along with #24594? cc @michael-s-molina and @rusackas

@eschutho This is already included in 3.0

@eschutho
Copy link
Member

eschutho commented Jul 6, 2023

Ok, perfect!

@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.0.0 labels Mar 8, 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 risk:breaking-change Issues or PRs that will introduce breaking changes size/XL 🚢 3.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants