Skip to content

fix(dashboard): guard against division by zero in statistics calculation - #53

Merged
Kuldeep2822k merged 1 commit into
mainfrom
fix/dashboard-div-by-zero
Aug 14, 2026
Merged

Kuldeep2822k merged 1 commit into
mainfrom
fix/dashboard-div-by-zero

Conversation

@Kuldeep2822k

Copy link
Copy Markdown
Owner

Summary

  • Adds explicit total > 0 guard before computing percentage metrics in dashboardCommand
  • Prevents potential NaN% rendering when calculating statistics on empty vaults
  • Adds automated test coverage in test/cli-commands.test.ts for both populated and empty vaults

Invariant Verification

  • Verified against zero-topic vaults (outputs 0.0% instead of NaN%)
  • Clean TypeScript compilation with tsc --noEmit
  • ESLint checks passing with 0 warnings
  • All 90 test cases passing

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Kuldeep2822k has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Free

Run ID: eb509f4f-90a5-425d-b9db-8f8aeb1aac4e

📥 Commits

Reviewing files that changed from the base of the PR and between bf627a8 and 30983a1.

📒 Files selected for processing (2)
  • src/cli/dashboard.ts
  • test/cli-commands.test.ts

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved dashboard statistics when no topics or cards are available.
    • Empty dashboards now display clean zero percentages instead of invalid values.
    • Percentage formatting remains consistent to one decimal place.
  • Tests

    • Added coverage for populated and empty vault dashboard scenarios.
    • Verified successful command execution and safe restoration of vault settings.

Walkthrough

The dashboard now uses explicit zero fallbacks for topic percentages. CLI tests cover formatted statistics for populated vaults and zero-valued statistics for empty vaults.

Changes

Dashboard statistics

Layer / File(s) Summary
Percentage fallback and CLI validation
src/cli/dashboard.ts, test/cli-commands.test.ts
The dashboard calculates masteredPct, learningPct, and newPct with zero-topic fallbacks. Tests verify formatted output, successful execution, zero percentages, no NaN, and vault configuration restoration.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 30983

The change prevents NaN% values for empty vaults while preserving percentage calculations for populated vaults; no actionable merge-blocking risk remains after normal checks and review.

Poem

I’m a rabbit with a tidy chart,
Zero totals now make a smart start.
Percentages stay clear and bright,
No NaN hops into sight.
Test the vault—then thump with delight!


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added core Core source code changes tests Test suite changes labels Aug 14, 2026
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Guard dashboard percentage stats against division by zero

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Prevents NaN/Infinity percentages when the dashboard runs on an empty vault.
• Computes mastered/learning/new percentages with an explicit total > 0 guard.
• Adds CLI tests to assert formatted output for populated and empty vaults.
Diagram

graph TD
  U(["User"]) --> C["CLI: dashboard"] --> V[("Vault") ] --> K["Count topics"] --> G{ "total > 0?" }
  G -->|"yes"| P["Compute %"] --> O["Print stats"]
  G -->|"no"| Z["Use 0.0%"] --> O

  subgraph Legend
    direction LR
    _user(["Actor"]) ~~~ _proc["Process"] ~~~ _db[("Data store")] ~~~ _dec{"Decision"}
  end
Loading
High-Level Assessment

The explicit total > 0 guard is the clearest and lowest-risk fix for avoiding NaN/Infinity in percentage formatting. Alternatives like Number.isFinite() checks or extracting a shared formatPercent(count,total) helper were considered but add indirection without meaningful benefit at this scale.

Files changed (2) +34 / -3

Bug fix (1) +7 / -3
dashboard.tsGuard percentage calculations when total topics is zero +7/-3

Guard percentage calculations when total topics is zero

• Introduces precomputed percentage strings for mastered/learning/new metrics with an explicit 'total > 0' check. Ensures dashboard output remains stable (0.0%) for empty vaults and avoids Infinity/NaN formatting issues.

src/cli/dashboard.ts

Tests (1) +27 / -0
cli-commands.test.tsAdd dashboard CLI regression tests for populated and empty vaults +27/-0

Add dashboard CLI regression tests for populated and empty vaults

• Adds assertions that the dashboard command prints expected totals and formatted percentages for a populated vault. Adds an empty-vault scenario test that verifies 0.0% outputs and explicitly checks that 'NaN' never appears in stdout.

test/cli-commands.test.ts

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can describe a rule in plain language on the Rules page and Qodo drafts it for you

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@Kuldeep2822k
Kuldeep2822k merged commit d68b208 into main Aug 14, 2026
10 checks passed
@Kuldeep2822k
Kuldeep2822k deleted the fix/dashboard-div-by-zero branch August 14, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core source code changes tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant