Skip to content

refactor(measurements): reduce cognitive complexity in smart.go - #616

Merged
Starosdev merged 2 commits into
developfrom
fix/sonarqube-cognitive-complexity
Jun 18, 2026
Merged

Starosdev merged 2 commits into
developfrom
fix/sonarqube-cognitive-complexity

Conversation

@Starosdev

Copy link
Copy Markdown
Owner

Summary

Reduces SonarQube cognitive complexity (go:S3776) across all 10 flagged functions in webapp/backend/pkg/models/measurements/smart.go. The functions ranged from complexity 17 to 43; all now sit at or below the allowed limit of 15.

Why

The file had heavy duplication: a near-identical override-application block (ShouldIgnore / forced status / custom thresholds) was copy-pasted across 7 SMART processing functions. This drove up cognitive complexity and made the logic error-prone to change in one place but not another.

Changes

Extracted shared helpers, collapsing the repeated logic:

  • applyOverrideResult — applies an override Result to an attribute's status/reason fields, returning (ignored, forcedFailure)
  • isTransientAtaAttribute — ATA transient-failure config lookup
  • newAttributeForProtocol / inflateInfluxAttribute — InfluxDB attribute grouping by protocol
  • parseInt64Field / coerceInt64 — typed InfluxDB scalar field parsing
  • suppressDeltaIfUnchanged — per-attribute delta-evaluation suppression
  • buildFarmAttributeValues — Seagate FARM page flattening

Net -91 lines. Behavior is unchanged.

Verification

  • go build ./webapp/backend/...
  • go vet ./webapp/backend/pkg/models/measurements/...
  • go test ./webapp/backend/pkg/models/measurements/... (39 tests pass)
  • gocognit -over 15 reports zero functions over the limit
  • gofmt clean

Notes

This is the first slice of SonarQube Group 5 (cognitive complexity). Remaining clusters (DB migrations, notify, repository, collector commands, detect) will follow in separate PRs.

Extract shared helpers to collapse the repeated override-application block
that appeared across 7 SMART processing functions:

- applyOverrideResult: applies an override Result to an attribute's status
- isTransientAtaAttribute: ATA transient-failure lookup
- newAttributeForProtocol / inflateInfluxAttribute: InfluxDB attribute grouping
- parseInt64Field / coerceInt64: typed InfluxDB field parsing
- suppressDeltaIfUnchanged: per-attribute delta suppression
- buildFarmAttributeValues: FARM page flattening

All 10 flagged functions now sit at or below the cognitive complexity
limit of 15. Behavior is unchanged; package tests pass.

Resolves SonarQube go:S3776 in smart.go.
Add space after // in relocated comment (gocritic commentFormatting)
and lowercase the error string (staticcheck ST1005).
@Starosdev
Starosdev merged commit b82e0f5 into develop Jun 18, 2026
14 checks passed
@Starosdev
Starosdev deleted the fix/sonarqube-cognitive-complexity branch June 18, 2026 02:01
Starosdev added a commit that referenced this pull request Jun 18, 2026
#617)

## Summary

Reduces SonarQube cognitive complexity (go:S3776) in `main()` across all
five collector command entrypoints. They ranged from complexity 17 to
47; all functions now sit at or below the allowed limit of 15.

| Command | Before | After |
|---------|--------|-------|
| collector-performance | 47 | ≤15 |
| collector-filesystem | 45 | ≤15 |
| collector-btrfs | 45 | ≤15 |
| collector-zfs | 17 | ≤15 |
| collector-mdadm | 17 | ≤15 |

## Why

Each `main()` inlined the entire cli "run" action closure plus
config-path resolution and per-flag overrides, concentrating all
branching in one giant function.

## Changes

- **btrfs / filesystem / performance** — extract `resolveConfigPath`,
`applyRunFlags`, `logRedactedSettings`, and `runCollectorAction`
helpers, mirroring the structure zfs/mdadm already used
- **zfs / mdadm** — extract the inline action into `runCollectorAction`
(the other helpers already existed)
- collapse the empty `ConfigFileMissingError` branch (S108) in
btrfs/filesystem

Behavior unchanged. The performance collector keeps its extra `profile`
flag handling.

## Verification

- [x] `go build ./collector/cmd/...`
- [x] `go vet ./collector/cmd/...`
- [x] `go test ./collector/...` (all pass)
- [x] `gocognit -over 15` reports zero functions over the limit
- [x] `gofmt` clean

## Notes

Continuation of SonarQube Group 5 (cognitive complexity), after #616
(smart.go). Remaining clusters: notify, repository, DB migrations,
detect.
Starosdev added a commit that referenced this pull request Jun 18, 2026
…#618)

## Summary

Reduces SonarQube cognitive complexity (go:S3776) across the notify
package. All flagged functions now sit at or below the allowed limit of
15.

| Function | File | Before | After |
|----------|------|--------|-------|
| ShouldNotify | notify.go | 58 | ≤15 |
| SendShoutrrrNotification | notify.go | 19 | ≤15 |
| SendSMTPNotification | smtp_sender.go | 30 | ≤15 |
| sendSMTPRecipient | smtp_sender.go | 16 | ≤15 |
| maskAppriseURL | mask.go | 24 | ≤15 |
| mockSMTPServer.serve | notify_test.go | 17 | ≤15 |

## Changes

Pure extraction of focused helpers; no behavior change.

- **notify.go** — `requiredNotifyStatuses`, `deviceProtocol`,
`isCriticalAttribute`, `collectFailingAttributes`,
`hasQualifyingFailure`; `appendQueryParam`, `joinSendErrors`
- **smtp_sender.go** — `buildSMTPConfig`, `negotiateSMTP`,
`resolveClientHost`, `startTLSIfSupported`, `smtpHeaders`,
`buildSMTPMessage`
- **mask.go** — `maskMailtoURL`, `maskTelegramURL`,
`maskURLPathSegments` (the discord/slack cases now share one
path-segment masker)
- **notify_test.go** — `handleSMTPData`

The `ShouldNotify` device-protocol detection was duplicated (override
lookup + critical check); it is now a single `deviceProtocol` helper.

## Verification

- [x] `go build ./webapp/backend/...`
- [x] `go vet ./webapp/backend/pkg/notify/...`
- [x] `go test ./webapp/backend/pkg/notify/...` (pass)
- [x] `gocognit -over 15` reports zero functions over the limit
- [x] `gofmt` clean

## Notes

Continuation of SonarQube Group 5 (cognitive complexity), after #616
(smart.go) and #617 (collector commands). Remaining clusters:
repository, DB migrations, detect.
Starosdev pushed a commit that referenced this pull request Jun 20, 2026
## [1.65.0-beta.1](v1.64.0...v1.65.0-beta.1) (2026-06-20)

### Features

* **api:** expose and update heartbeat_enabled on notify URLs ([99bb242](99bb242))
* **heartbeat:** filter notify URLs by heartbeat_enabled ([24f359a](24f359a))
* **notify:** add heartbeat_enabled field to notify_urls ([6432a2c](6432a2c))
* **notify:** LoadHeartbeatDatabaseUrls filters by heartbeat_enabled ([51d8662](51d8662))
* **thresholds:** detect Power-On Hours 16-bit counter rollover ([#595](#595)) ([#625](#625)) ([390de8b](390de8b))
* **ui:** add contextual help links for settings docs ([#492](#492)) ([8ceb12b](8ceb12b))
* **ui:** add contextual help links pointing to docs folder ([#492](#492)) ([7b484a0](7b484a0))
* **ui:** add heartbeat toggle per notification URL ([e6bbfea](e6bbfea))
* **ui:** add navigation visibility settings for ZFS, MDADM, Btrfs, Workload ([#588](#588)) ([fca1ae3](fca1ae3))
* **ui:** group MDADM and Workload pages by host ([#579](#579)) ([3e3aa73](3e3aa73))

### Bug Fixes

* **api:** return 404 when heartbeat toggle targets non-existent notify URL ([8c375c2](8c375c2))
* **backend:** pass SmartInfo by pointer to syncDeviceSelfTests (hugeParam) ([e027c33](e027c33))
* **backend:** satisfy gocritic hugeParam and govet fieldalignment lint ([be05416](be05416))
* **db:** simplify migration func to satisfy unlambda linter ([8158a34](8158a34))
* **ui:** style learn more link in Consumer Drive Profiles hint ([#492](#492)) ([ba5b6c3](ba5b6c3))

### Refactoring

* **collector:** reduce cognitive complexity in collector commands ([#617](#617)) ([dedf386](dedf386)), closes [#616](#616)
* **collector:** reduce cognitive complexity in device detection ([#619](#619)) ([b2e5a9d](b2e5a9d))
* **database:** reduce cognitive complexity in repository ([#621](#621)) ([4990d88](4990d88))
* **database:** reduce cognitive complexity in the migration runner ([#624](#624)) ([037c82d](037c82d))
* **frontend:** reduce cognitive complexity in components ([#620](#620)) ([278cd44](278cd44))
* **measurements:** reduce cognitive complexity in smart.go ([#616](#616)) ([b82e0f5](b82e0f5))
* **models:** reduce cognitive complexity in measurement models ([#622](#622)) ([900e4c3](900e4c3))
* **notify:** reduce cognitive complexity in notification senders ([#618](#618)) ([249d836](249d836)), closes [#616](#616) [#617](#617)
* **web:** reduce cognitive complexity in middleware and monitors ([#623](#623)) ([8695dc8](8695dc8))
Starosdev added a commit that referenced this pull request Jun 29, 2026
…ntrols, settings fix (#629)

## Summary

Promotes the 1.65.0 beta line to master (`1.64.0 -> 1.65.0`). Scope
below is limited to changes that are actually new versus master;
features merged to master earlier (per-URL heartbeat, navigation
visibility, host grouping, ATA self-test history, contextual help) are
already shipped and are NOT part of this delta.

**Features**
- Power-On Hours 16-bit counter rollover detection (#595, #625) - new
`smart_rollover_test.go`, thresholds logic
- Dashboard layout controls: max column count + card density
('comfortable' | 'compact') (#471)

**Fixes**
- Preserve server-capability flags on settings save: `server_version`
and `collector_trigger_enabled` were dropped from the `POST
/api/settings` response, so the "Run collectors" button vanished after
any settings change. Backend now echoes both flags (shared
`collectorTriggerEnabled()` helper); frontend config setter preserves
them with a current-value fallback. Adds handler regression tests.

**Refactors (cognitive complexity)**
- smart.go, measurement models, repository, migration runner,
middleware/monitors, notify senders, SMTP sender, device detection,
collector commands, frontend components (#616-#624)


## Database / Upgrade Notes

- One new migration vs master: `m20260617000000` - inserts
`dashboard_columns=2` and `dashboard_density=comfortable` settings.
Additive, idempotent, auto-runs on startup.
- No `scrutiny.yaml` / `collector.yaml` schema changes. No API route
changes (hub/spoke collectors stay compatible).
- No manual user action required for 1.64.0 -> 1.65.0.

## Test plan

- [x] `go build ./...` clean
- [x] `go vet ./webapp/backend/... ./collector/...` clean
- [x] Backend + collector tests pass (30 pkgs, 0 fail); web package
fresh `-count=1` pass
- [x] `npm run lint` all pass
- [x] `npm run build:prod` succeeds (warnings only: pre-existing scss
budget + CommonJS bailouts)
- [x] Beta Docker + Publish Beta CI green on HEAD
- [x] Settings fix verified live on beta-omnibus (zeus dev)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
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.

1 participant