-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Metrics UI] Replace uses of any introduced by Lodash 4
#75507
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
Conversation
|
Pinging @elastic/logs-metrics-ui (Team:logs-metrics-ui) |
weltenwort
left a comment
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.
👀 Reviewing on behalf of @elastic/logs-metrics-ui...
weltenwort
left a comment
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.
Thanks for tackling this! 👍 I left some suggestions about avoiding assertions by reformulating in a readable manner. ✍️
In other places I questioned the safety of the assertions, because if the intention of the any removal is to improve type safety and the reduction of runtime errors, then simply replacing as any with ! in many places doesn't achieve that.
If that's not the goal of this PR then please feel free to ignore my nitpicky questions. 😇
| const dateFormatter = useMemo(() => { | ||
| const firstSeries = data ? first(data.series) : null; | ||
| return firstSeries && firstSeries.rows.length > 0 | ||
| ? niceTimeFormatter([ | ||
| (first(firstSeries.rows) as any).timestamp, | ||
| (last(firstSeries.rows) as any).timestamp, | ||
| ]) | ||
| ? niceTimeFormatter([first(firstSeries.rows)!.timestamp, last(firstSeries.rows)!.timestamp]) | ||
| : (value: number) => `${value}`; | ||
| }, [data]); |
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.
This is an interesting one - even though it's semantically correct, first's and last's type signatures don't allow the compiler to infer it correctly. We could, however, try to reformulate it to something that is both readable and fully type-checked by relying on the ?. operator and the fact that first and last can handle undefined values. What do you think about
const dateFormatter = useMemo(() => {
const firstSeries = first(data?.series);
const firstTimestamp = first(firstSeries?.rows)?.timestamp;
const lastTimestamp = last(firstSeries?.rows)?.timestamp;
if (firstTimestamp == null || lastTimestamp == null) {
return (value: number) => `${value}`;
}
return niceTimeFormatter([firstTimestamp, lastTimestamp]);
}, [data?.series]);?
| const { criteria } = params; | ||
| if (criteria && data) { | ||
| const firstSeries = first(data.series) as any; | ||
| const series = firstSeries.rows.reduce((acc: any, row: any) => { | ||
| const firstSeries = first(data.series)!; | ||
| const series = firstSeries.rows.reduce((acc, row) => { |
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.
Isn't the assertion first(data.series)! risky without checking data.series.length? How about we bind the series to a name early and check it for undefined directly?
const { criteria } = params;
const firstSeries = first(data?.series);
if (criteria && firstSeries) {
const series = firstSeries.rows.reduce((acc, row) => {
x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/color_from_value.ts
Outdated
Show resolved
Hide resolved
x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/color_from_value.ts
Outdated
Show resolved
Hide resolved
x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/nodes_to_wafflemap.ts
Outdated
Show resolved
Hide resolved
...a/server/lib/alerting/inventory_metric_threshold/preview_inventory_metric_threshold_alert.ts
Show resolved
Hide resolved
x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts
Show resolved
Hide resolved
x-pack/plugins/infra/server/lib/alerting/metric_threshold/preview_metric_threshold_alert.ts
Show resolved
Hide resolved
x-pack/plugins/infra/server/lib/alerting/metric_threshold/preview_metric_threshold_alert.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com>
…a into 70728-typecheck-remove-any
…emove-any # Conflicts: # x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts
|
I think applying some suggestions locally and some through the GitHub UI confused GitHub and now it's not showing the up-to-date code changes. Going to force push to fix |
weltenwort
left a comment
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.
Thanks for investing the time!
x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts
Outdated
Show resolved
Hide resolved
…shold/evaluate_condition.ts Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com>
|
@elasticmachine merge upstream |
💚 Build SucceededBuild metricsasync chunks size
History
To update your PR or re-run it, just comment with: |
|
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
|
Looks like this PR has a backport PR but it still hasn't been merged. Please merge it ASAP to keep the branches relatively in sync. |
…76108) Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Summary
Closes #70728
Replaces the
anycastings introduced by the Lodash 4 upgrade. A lot of adding!to the end offirst()andlast()calls, because Typescript doesn't realize thatif (someArray.length > 0)means thatfirst/lastcalled onsomeArraywon't returnundefined. But there were a few cases of just needing to clarify some of the typings.