[scanner] 🌱 refactor: extract remaining magic numbers to named constants#19253
Conversation
Signed-off-by: Scanner Agent <scanner@kubestellar.io>
✅ Deploy Preview for kubestellarconsole ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
👋 Hey @clubanderson — thanks for opening this PR!
This is an automated message. |
|
🐝 Hi @clubanderson! I'm Trusted users — org members and contributors with write access — can mention Automation may take a moment to start, and follow-up happens through workflow activity rather than chat replies. |
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #19245 by continuing the “no magic numbers” refactor in the scanner-related alerting/notification code, extracting several remaining numeric literals into named constants to improve readability and maintainability.
Changes:
- Replaced hardcoded byte-scaling literal in
formatBytesby importing a shared unit constant fromconstants/units. - Introduced named constants for HTTP auth error status codes, DOMException quota error code, and several timing / truncation thresholds.
- Added
BYTES_PER_KIBto the shared units constants module.
Build/lint are expected to be validated by CI on the PR (per repo workflow).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| web/src/lib/formatters.ts | Uses shared unit constant for byte formatting calculations. |
| web/src/lib/constants/units.ts | Adds BYTES_PER_KIB to centralize binary byte unit scaling. |
| web/src/contexts/notifications.ts | Extracts HTTP 401/403 checks into named constants for clarity. |
| web/src/contexts/alertStorage.ts | Extracts legacy DOMException quota error code into a named constant. |
| web/src/contexts/AlertsContext.tsx | Adds named timing constants for MCP batching and alert evaluation. |
| web/src/contexts/alertRunbooks.ts | Adds named max-length constants for prompt/evidence truncation. |
Comments suppressed due to low confidence (1)
web/src/lib/constants/units.ts:8
- Now that
BYTES_PER_KIBexists, the other binary unit constants can be derived from it to avoid repeating1024(which is exactly what this PR is trying to eliminate). This also makes future refactors safer because the base only lives in one place.
/** Bytes per mebibyte (MiB) */
export const BYTES_PER_MIB = 1024 * 1024
/** Bytes per kibibyte (KiB) */
export const BYTES_PER_KIB = 1024
/** Bytes per gibibyte (GiB) */
export const BYTES_PER_GIB = 1024 * 1024 * 1024
/** Bytes per tebibyte (TiB) */
export const BYTES_PER_TIB = 1024 * BYTES_PER_GIB
| const units = binary ? IEC_UNITS : SI_UNITS | ||
| const i = Math.floor(Math.log(bytes) / Math.log(BYTES_PER_KIBIBYTE)) | ||
| const value = bytes / Math.pow(BYTES_PER_KIBIBYTE, i) | ||
| const i = Math.floor(Math.log(bytes) / Math.log(BYTES_PER_KIB)) | ||
| const value = bytes / Math.pow(BYTES_PER_KIB, i) |
|
Thank you for your contribution! Your PR has been merged. Check out what's new:
Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey |
|
Post-merge build verification passed ✅ Both Go and frontend builds compiled successfully against merge commit |
Add export * from './time' to the constants barrel file, fixing tests that failed to load due to missing time constant imports after PR #19253 refactored magic numbers into separate modules. Signed-off-by: clubanderson <club.anderson@gmail.com>
Fixes #19245
Extracts hardcoded magic numbers to descriptive named constants across 6 files:
AlertsContext.tsx— timing constants for MCP, loading, and evaluationalertRunbooks.ts— length limits for prompts and evidencenotifications.ts— HTTP status codes for auth errorsalertStorage.ts— DOMException error code constantformatters.ts— now imports BYTES_PER_KIB from units.tsunits.ts— added new BYTES_PER_KIB constantAll constants use SCREAMING_SNAKE_CASE naming convention matching existing codebase patterns.