feat: system filter options#265
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🧹 Nitpick comments (7)
internal/repo/query.go (1)
309-313: Consider simplifying theFilter.ValuesAPI
Values *[]stringrequires callers to pre-allocate a slice and pass its address so the repo can write into it via*filter.Values = .... This is a somewhat unusual pattern in Go — it's common to return the data as the function's result instead (e.g.,GetFilterOptions(...) (map[string][]string, error)or haveFilterreceive results by reassigning). The current shape is workable but makes call sites noisier and failure modes (nil pointer) easy to hit. Also, the struct-level doc only describesColumn; a short comment onValues(input/output, non-nil required, etc.) would help consumers.apis/cmk/cmk-ui.yaml (2)
701-724: Remove404response and consider a more specificoperationId
- The endpoint has no path parameters and returns aggregated filter values; there is no addressable resource that can be "not found", so the
"404"response mapping (Line 719-720) is misleading. Consider dropping it.operationId: getFiltersis quite generic for an OpenAPI spec that mixes multiple resource types. Something likegetSystemFilterswould be more self-describing and consistent with neighbors likegetAllSystems.
1534-1552:SystemFiltersfields have no constraints
region,type, andkeyConfigurationNameitem strings have nomaxLength, unlike the equivalent fields in theSystemschema (region/typeusemaxLength: 50,keyConfigurationNameusesmaxLength: 255). For consistency and to bound payload size on a UI-facing endpoint, consider mirroring those limits on the item schemas.internal/manager/system_test.go (1)
1538-1553: Guard against nil pointer dereferences and extend coverage
assert.Len(t, *filters.Type, 2)(and the same forRegion/KeyConfigurationName) panics the test if the field is unexpectedlynil. Preferrequire.NotNil(t, filters.Type)before dereferencing, so a regression surfaces as a failure rather than a panic.- Consider adding a case with a system that has no linked
KeyConfiguration(i.e.KeyConfigurationID == nil) to verify the LEFT JOIN + distinct-non-null filtering inSystemManager.GetFiltersstill returns the system's region/type and does not include a null entry inKeyConfigurationName. This is the main edge case the new SQL path has to handle correctly.internal/manager/system.go (2)
450-457: Return zero-valueSystemFilterson error.
GetFilterOptionscan fail partway through (its implementation runs aUNION ALLand scans into the provided slices), yet the method unconditionally returns aSystemFilterswith pointers totypes/regions/keyConfigNames. Callers that forget to check the error (or log-and-continue) will see a non-nil response with possibly partial data, which can leak through the controller as a successful payload.Prefer the standard Go pattern:
🛠 Proposed fix
- err := m.repo.GetFilterOptions(ctx, model.System{}, filters, *query) - - return cmkapi.SystemFilters{ - KeyConfigurationName: &keyConfigNames, - Region: ®ions, - Type: &types, - }, err + if err := m.repo.GetFilterOptions(ctx, model.System{}, filters, *query); err != nil { + return cmkapi.SystemFilters{}, errs.Wrap(ErrQuerySystemList, err) + } + + return cmkapi.SystemFilters{ + KeyConfigurationName: &keyConfigNames, + Region: ®ions, + Type: &types, + }, nilWrapping with a dedicated sentinel (similar to
ErrQuerySystemListused inGetAllSystems) also keeps the controller’s error-mapping consistent with the rest of this file.
445-449: Use consistent table qualification for JOIN columns; avoid mixingfmt.Sprintfliterals with unqualified field constants.The filters use inconsistent column qualification:
repo.TypeFieldandrepo.RegionFieldare left bare, whileKeyConfiguration.nameis table-qualified viafmt.Sprintf. Whilemodel.KeyConfigurationdoesn't currently havetypeorregioncolumns (no ambiguity today), this mixed approach is fragile and harder to maintain.Qualify all three columns consistently—either all bare or all table-qualified like:
Column: fmt.Sprintf("%s.%s", model.System{}.TableName(), repo.TypeField)or use a structured table-qualified helper. This pattern also reduces the risk of column-name typos, since these strings are interpolated directly into SQL.
internal/repo/sql/repo.go (1)
601-609: Consider deterministic ordering of filter values.Without
ORDER BY, returned values can vary between executions. Sorting at SQL level keeps API responses stable.♻️ Suggested adjustment
- unionQuery := strings.Join(unionParts, " UNION ALL ") + unionQuery := fmt.Sprintf( + "SELECT column_name, value FROM (%s) AS filter_values ORDER BY column_name, value", + strings.Join(unionParts, " UNION ALL "), + )
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5114f171-55df-43ff-829c-9d2e0029469e
📒 Files selected for processing (13)
apis/cmk/cmk-ui.yamlinternal/api/cmkapi/cmkapi.gointernal/authz/api_endpoint_mapping.gointernal/authz/repo/authz_repo.gointernal/controllers/cmk/system_controller.gointernal/controllers/cmk/system_controller_test.gointernal/manager/system.gointernal/manager/system_test.gointernal/manager/tenant_test.gointernal/repo/mock/repo.gointernal/repo/query.gointernal/repo/repository.gointernal/repo/sql/repo.go
382863f to
c9c5b96
Compare
c9c5b96 to
22541fd
Compare
ed227f8 to
139cb06
Compare
139cb06 to
fec877b
Compare
Summary by CodeRabbit
New Features
/systems/filterOptionsendpoint that returns available filter values for systems including region, type, and key configuration name. Use this endpoint to discover valid filter options when querying systems.Tests