Skip to content

Consolidate duplicate formatNumber implementations#2683

Closed
Copilot wants to merge 4 commits intomainfrom
copilot/consolidate-formatnumber-implementation
Closed

Consolidate duplicate formatNumber implementations#2683
Copilot wants to merge 4 commits intomainfrom
copilot/consolidate-formatnumber-implementation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 28, 2025

Consolidate formatNumber() duplicate implementations

This PR consolidates two similar number formatting functions into a single shared implementation.

Changes completed:

  • Explore repository and understand the duplicate implementations
  • Verify current tests pass
  • Replace formatNumberForDisplay() in pkg/console/render.go with the sophisticated version from logs.go
  • Export the function as FormatNumber()
  • Update pkg/cli/logs.go to import and use console.FormatNumber()
  • Update pkg/cli/audit.go to use console.FormatNumber()
  • Update pkg/cli/audit_report.go to use console.FormatNumber()
  • Remove duplicate formatNumber() from logs.go
  • Update test in pkg/cli/logs_test.go to use the exported function
  • Update tests in pkg/console/render_formatting_test.go for new precision
  • Update test in pkg/console/render_test.go for new precision
  • Fix error message in pkg/console/render_test.go to match expectation
  • Merge main branch and resolve conflicts
  • Run all tests to ensure functionality is preserved (all tests pass ✓)
  • Run linter to ensure code quality (all validations pass ✓)

Key differences between implementations:

  • logs.go version: More sophisticated with 3-tier precision (2 decimals for 1-9.99, 1 decimal for 10-99.9, 0 decimals for 100+)
  • render.go version: Simpler with 2-tier precision

Impact:

  • ~26 lines of duplicate code removed
  • Consistent number formatting across the entire application with sophisticated precision handling
  • Single source of truth for number formatting logic in pkg/console/render.go
  • Function is now exported as console.FormatNumber() and used in:
    • pkg/cli/logs.go (2 call sites)
    • pkg/cli/audit.go (3 call sites)
    • pkg/cli/audit_report.go (2 call sites)
    • pkg/console/render.go (for struct tag formatting)

Merge with main:

  • Resolved conflicts with main branch
  • Adopted timeutil.FormatDuration() from main instead of local formatDuration()
  • Kept console.FormatNumber() consolidation from this PR

Testing:

  • All unit tests pass ✓
  • Verified number formatting for edge cases
  • Linter validation passes ✓
Original prompt

This section details on the original issue you should resolve

<issue_title>[task] Consolidate formatNumber() duplicate implementations</issue_title>
<issue_description>## Objective
Consolidate two similar number formatting functions into a single shared implementation.

Context

Related to #2668 (Duplicate or Near-Duplicate Functions)

Currently there are two implementations with 90% code overlap:

  • pkg/cli/logs.go:1843-1883 (41 lines) - More sophisticated precision handling
  • pkg/console/render.go:542-566 (25 lines) - Simpler thresholds

Both format integers as human-readable strings (K/M/B suffixes) but use different precision logic.

Approach

  1. Consolidate into pkg/console/render.go as FormatNumber(n int) string
  2. Use the more sophisticated version from logs.go (better precision thresholds)
  3. Update logs.go to import from the console package
  4. Remove duplicate implementation from logs.go
  5. Ensure consistent number formatting across the entire application

Files to Modify

  • Primary: pkg/console/render.go
    • Replace existing formatNumberForDisplay() (lines 542-566) with enhanced version from logs.go
    • Export as FormatNumber()
  • Update: pkg/cli/logs.go
    • Remove formatNumber() function (lines 1843-1883)
    • Import and use console.FormatNumber()
    • Update all call sites
  • Update: Test files for both packages

Acceptance Criteria

  • Single FormatNumber() function exists in pkg/console/render.go
  • Function uses the sophisticated precision handling from logs.go
  • Function correctly formats: 0, <1000, 1K-999K, 1M-999M, 1B+
  • logs.go imports and uses console.FormatNumber()
  • All existing tests pass
  • Unit tests verify formatting for edge cases (0, 999, 1000, 1500, 999999, 1000000, etc.)
  • 25-40 lines of duplicate code removed

Estimated Effort

1 hour

Priority

P1 - High Impact (Quick Win)
Related to #2668

AI generated by Plan Command for #2668</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Consolidate duplicate implementations of formatNumber() Consolidate duplicate formatNumber implementations Oct 28, 2025
Copilot AI requested a review from pelikhan October 28, 2025 12:45
@pelikhan pelikhan marked this pull request as ready for review October 28, 2025 12:45
Copilot AI review requested due to automatic review settings October 28, 2025 12:45
@github-actions
Copy link
Copy Markdown
Contributor

Agentic Changeset Generator triggered by this pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR consolidates duplicate number formatting logic by merging two similar implementations (~90% code overlap) into a single exported function. The consolidated version uses the more sophisticated 3-tier precision logic from logs.go (2 decimals for 1-9.99, 1 decimal for 10-99.9, 0 decimals for 100+) and is now available as console.FormatNumber() for consistent formatting across the application.

Key Changes:

  • Exported console.FormatNumber() with enhanced precision handling in pkg/console/render.go
  • Removed duplicate 41-line formatNumber() function from pkg/cli/logs.go
  • Updated 7 call sites across multiple files to use the consolidated function

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/console/render.go Replaced formatNumberForDisplay() with exported FormatNumber() using sophisticated 3-tier precision logic
pkg/console/render_test.go Updated test expectation to reflect new precision behavior (1.5k → 1.50k)
pkg/console/render_formatting_test.go Updated multiple test cases to expect 2-decimal precision for numbers in 1-9.99k/M/B range
pkg/cli/logs.go Removed duplicate formatNumber() function and updated call sites to use console.FormatNumber()
pkg/cli/logs_test.go Added console package import and updated test to use console.FormatNumber()
pkg/cli/audit.go Updated 3 call sites to use console.FormatNumber()
pkg/cli/audit_report.go Updated 2 call sites to use console.FormatNumber()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/console/render_test.go Outdated
@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot merge main

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 28, 2025

@copilot merge main

Merged main in 9b62816. Resolved conflicts by adopting timeutil.FormatDuration() from main while keeping the console.FormatNumber() consolidation. All tests pass.

@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot

@pelikhan pelikhan closed this Oct 28, 2025
@pelikhan pelikhan deleted the copilot/consolidate-formatnumber-implementation branch October 30, 2025 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[task] Consolidate formatNumber() duplicate implementations

3 participants