Repository navigation
refactor: add number formatting utilities for improved data presentation #7871
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The diff is syntactically plausible, but there are several semantic/data-presentation issues. The most important ones are below.
Critical issues
The second export reverses a ratio formula
This now computes
chat_record_count_total / chat_user_count, which is the reciprocal of the original metric. If the original metric was intentional, this is a bug. If the intended metric is “records per user”, then the Excel header and any downstream consumers must also be changed.The third export swaps two columns
Unless the Excel headers were also swapped in a part of the file not shown in this diff, the exported columns will no longer match their headers. This is likely a data-alignment bug.
_proportion_percentagechanges ratio semantics from 0–1 to 0–100total_tokens / token_countwere ratios in the range0.0–1.0.0–100, rounded to one decimal.This may be intentional, but it requires the Excel headers and any consumers of the export to be updated to indicate “%” or “percentage”. Otherwise the meaning of the column silently changes.
Formatted numbers may become text cells in Excel
format_token_number(...)andnumber_format(...)likely return strings such as"1,234"or"1.2K".format_token_numberfor ratios/averages unless that is explicitly desired. A ratio is not a token count.Potential issues
Helpers do not handle
Nonesafely_round_ratio(None, 10)raises onDecimal(None)._round_ratio(10, None)raises onNone <= 0._proportion_percentage(None, total)raises onDecimal(None).Consider guarding with
value or 0andif divisor is None or divisor <= 0.Decimal(value)can introduce binary-float artifactsIf
valueordivisorcan befloat, use:instead of
Decimal(value). For integer counts this is less important, but it is safer.Converting
Decimaltofloatcan lose precisionIn
_round_ratioand_proportion_percentage, converting theDecimalresult tofloatbefore checking.is_integer()can lose precision for very large values.Safer pattern:
Clamping percentages to 100 hides data anomalies
min(100.0, max(0.0, ...))silently caps invalid values. Ifvalue > totalindicates a data-quality problem, consider logging or exposing it rather than silently clamping.Rounding before export can make percentages not sum to 100%
This is often acceptable for display, but if exact totals are needed, keep numeric values and round only at presentation time.
Optimization suggestions
_round_ratioand_proportion_percentage.Nonevalues,value > total,openpyxlcell number formats for display._round_quotientand_percentage, since_round_ratioactually computes a rounded quotient.format_token_numberandnumber_formatexist in the importedcommon.utils.commonmodule for all supported branches/versions.Suggested fixes
For the second export, if the original metric was intended:
For the third export, restore the original column order:
For percentage columns, either update the headers to indicate
%, or change the helper back to a ratio:Overall: the main risks are the inverted formula, the swapped columns, the ratio-to-percentage semantic change, and the conversion of numeric Excel cells into formatted strings.