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

Table: Fix units showing in footer after reductions without units #82081

Merged
merged 6 commits into from
Feb 23, 2024

Conversation

codeincarnate
Copy link
Collaborator

This PR fixes an issue in which table footer items will display units even in cases where the reduction will not preserve units. i.e. in cases where counts are applied of percentages are calculated.

This does so by adding a preservesUnits property to reducer registry items so that it's possible to determine if unit formatting should be applied after reduction.

Fixes #81670

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@codeincarnate codeincarnate requested a review from a team as a code owner February 7, 2024 12:48
@codeincarnate codeincarnate requested review from nmarrs and baldm0mma and removed request for a team February 7, 2024 12:48
@grafana-delivery-bot grafana-delivery-bot bot added this to the 10.4.x milestone Feb 7, 2024
@codeincarnate
Copy link
Collaborator Author

Did notice that this messes up percentage formatting where percents are rounded. Currently considering what to do there.

@nmarrs nmarrs changed the title Table Panel: Fix units showing in footer after reductions without units Table: Fix units showing in footer after reductions without units Feb 8, 2024
Copy link
Contributor

@nmarrs nmarrs left a comment

Choose a reason for hiding this comment

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

Overall LGTM - not sure on edge case you're encountering with rounded percents (do you have a dashboard you can share to replicate that?)

In testing each reducer type the units are preserved / ignored as I would expect 👍
debug-Panel Title-2024-02-08 12_20_29.json.txt

@nmarrs
Copy link
Contributor

nmarrs commented Feb 8, 2024

This is the approach we've taken before to address this (at least in XY Chart)

If we move forward with these changes we may need to do a review and cleanup of handling like this (can be follow-up)

@codeincarnate
Copy link
Collaborator Author

Thanks for the review Nathan! Interesting that another go was taken at this. I'll follow up so that we can unify approaches. I believe we should go with the option presented here so that it can be handled in a uniform way regardless of usage.


// If the reducer preserves units then format the
// end value with the field display processor
if (reducerInfo.preservesUnits) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just wanted to leave a comment here that this would be the area to refactor and pull out into a separate function in this part so we can use in other places (i.e. XY chart, etc.)

@codeincarnate
Copy link
Collaborator Author

codeincarnate commented Feb 12, 2024

Oof, did run into one issue. Panel crashes when the reducer is undefined 😢. Fixing that now.

@codeincarnate
Copy link
Collaborator Author

codeincarnate commented Feb 12, 2024

Here's what I was seeing the the Difference Percent"option

Old:
Screenshot 2024-02-12 at 2 10 55 PM

New:
Screenshot 2024-02-12 at 2 14 35 PM

Basically we've been applying at least some rounding to these numbers. This is a change in behavior, that being said this may be better suited as an option of some kind, such as setting number of significant digits. I can see arguments for rounding and for not rounding depending on use case. Would definitely be interested to thoughts on that 🙂

@codeincarnate
Copy link
Collaborator Author

Fix the issue when there's an undefined value for the footer 🥳

@@ -41,6 +41,7 @@ export interface FieldReducerInfo extends RegistryItem {
// Internal details
emptyInputResult?: unknown; // typically null, but some things like 'count' & 'sum' should be zero
standard: boolean; // The most common stats can all be calculated in a single pass
preservesUnits: boolean; // Whether this reducer preserves units, certain ones don't e.g. count, distinct count, etc,
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the comment here.

}

// Calculate the reduction
const format = field.display ?? getDisplayProcessor({ field, theme });
Copy link
Contributor

Choose a reason for hiding this comment

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

Also appreciate the more verbose renaming here.

Copy link
Contributor

@baldm0mma baldm0mma left a comment

Choose a reason for hiding this comment

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

lgtm

@nmarrs
Copy link
Contributor

nmarrs commented Feb 16, 2024

Re: percent issue is there a way for us to set the resulting value's unit to percent? This reminds me of some work that we've done / issues that we ran into previously with an issue with bar chart percent stacking 😬

IMO in it's current iteration it is better than before so I would be fine with us merging and creating an issue (or two) to follow up on:

  1. percent issue
  2. refactoring handling of this previously (in XY etc) into a single flow

@aangelisc aangelisc modified the milestones: 10.4.x, 11.0.x Feb 20, 2024
@codeincarnate
Copy link
Collaborator Author

@nmarrs cool, sounds good there. I'll go ahead and get this merged up 😄

@codeincarnate codeincarnate merged commit a4cc417 into main Feb 23, 2024
18 checks passed
@codeincarnate codeincarnate deleted the codeincarnate/fix-table-footer-units branch February 23, 2024 04:28
yuri-tceretian pushed a commit that referenced this pull request Feb 26, 2024
…2081)

* Add preservesUnits property to reducer registry items

* Hide units when appropriate

* Prettier

* some code cleanup

* Prevent error when no stat is selected.

---------

Co-authored-by: nmarrs <nathanielmarrs@gmail.com>
chalapat pushed a commit to nokia/grafana that referenced this pull request Feb 27, 2024
…afana#82081)

* Add preservesUnits property to reducer registry items

* Hide units when appropriate

* Prettier

* some code cleanup

* Prevent error when no stat is selected.

---------

Co-authored-by: nmarrs <nathanielmarrs@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Table footer -> Count formats integer-counts as the units of the fields above (dollars etc)
5 participants