Skip to content

refactor(models): reduce cognitive complexity in measurement models - #622

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

Starosdev merged 2 commits into
developfrom
fix/sonarqube-models

Conversation

@Starosdev

Copy link
Copy Markdown
Owner

Summary

Reduces SonarQube cognitive complexity (go:S3776) across the measurement models and threshold parsing. All flagged functions now sit at or below the allowed limit of 15.

Function File Before
NewBtrfsMetricsFromInfluxDB measurements/btrfs_metrics.go 34
NewPerformanceFromInfluxDB measurements/performance.go 32
ValidateThreshold measurements/smart_ata_attribute.go 27
parseConsumerDriveProfiles thresholds/consumer_drive_profiles.go 23
NewMDADMMetricsFromInfluxDB measurements/mdadm_metrics.go 18

Changes

Pure refactor; no behavior change.

  • The three NewXFromInfluxDB constructors had long runs of if val, ok := attrs[k]; ok && val != nil { ... } optional-field blocks. These now use the package's existing influxString / influxInt64 / influxFloat64 helpers (defined in zfs_pool_metrics.go). Assigning the helper's zero value for an absent key is equivalent to leaving the field unset, and the helpers keep the same panic-on-type-mismatch behavior as the original direct assertions.
  • ValidateThreshold — extracted thresholdValue (display-type selection), observedThresholdContains, observedFailureRate, and applyFailureRateStatus.
  • parseConsumerDriveProfiles — extracted addConsumerDriveProfile and addConsumerDriveAlias per-entry handlers.

Verification

  • go build ./webapp/backend/...
  • go vet on both packages
  • go test -count=1 ./webapp/backend/pkg/models/measurements/... ./webapp/backend/pkg/thresholds/... — pass (no InfluxDB needed)
  • gocognit -over 15 reports zero functions over the limit
  • gofmt clean

Notes

Continuation of SonarQube Group 5, after #616/#617/#618/#619/#620/#621. Remaining: web middleware/monitors (auth, logger, heartbeat, missed-ping) and the DB migration runner (Migrate, 166).

Collapse the repeated optional-field extraction in the InfluxDB
constructors using the existing influxString/influxInt64/influxFloat64
helpers, and split the threshold/profile parsers into focused helpers.
Behavior is unchanged; measurements and thresholds tests pass.

- btrfs_metrics NewBtrfsMetricsFromInfluxDB (34)
- performance NewPerformanceFromInfluxDB (32)
- mdadm_metrics NewMDADMMetricsFromInfluxDB (18)
- smart_ata_attribute ValidateThreshold (27): thresholdValue,
  observedThresholdContains, observedFailureRate, applyFailureRateStatus
- thresholds parseConsumerDriveProfiles (23): addConsumerDriveProfile,
  addConsumerDriveAlias

All flagged functions now sit at or below the cognitive complexity
limit of 15.

Resolves SonarQube go:S3776 in measurement models and threshold parsing.
@Starosdev
Starosdev merged commit 900e4c3 into develop Jun 18, 2026
14 checks passed
@Starosdev
Starosdev deleted the fix/sonarqube-models branch June 18, 2026 23:13
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))
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