-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
Conversation
46f4905
to
57b9590
Compare
superset-frontend/packages/superset-ui-core/src/currency-format/CurrencyFormatter.ts
Fixed
Show fixed
Hide fixed
superset-frontend/packages/superset-ui-core/src/currency-format/CurrencyFormatter.ts
Fixed
Show fixed
Hide fixed
Codecov Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this 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[] = [], | ||
) => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, | ||
) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
getCustomFormatter( | ||
buildCustomFormatters( | ||
metric, | ||
currencyFormats, | ||
columnFormats, | ||
numberFormat, | ||
), | ||
metric, | ||
) ?? getNumberFormatter(numberFormat); |
There was a problem hiding this comment.
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,
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
There was a problem hiding this 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
hasValidCurrency() { | ||
return this.currency?.symbol; | ||
} |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBeTruthy(); | |
expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBe(true); |
return key ? customFormatters[key] : undefined; | ||
}; | ||
|
||
export const getFormatter = ( |
There was a problem hiding this comment.
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
?
|
||
// const initialDatasource = dataset; |
There was a problem hiding this comment.
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
// const initialDatasource = dataset; | |
// const initialDatasource = dataset; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦
/testenv up |
@kgabryje Ephemeral environment spinning up at http://18.237.178.241:8080. Credentials are |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restamping, looks great!
346032c
to
a40f28e
Compare
Ephemeral environment shutdown and build artifacts deleted. |
@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 |
Ok, perfect! |
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 inconfig.py
by modifying arrayCURRENCIES
.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
ADDITIONAL INFORMATION
Current: 0.13 s
10+: 0.12 s
100+: 0.12 s
1000+: 0.13 s
CC @yousoph