Refactor ProfileService, MetadataProfileRespurce, MetricProfileResource: Replace String Literals with Constants - #24
Open
gulati-aakriti wants to merge 5 commits into
Open
Conversation
Reviewer's GuideRefactors ProfileService to centralize all profile-related log messages, error strings, and API response parsing into shared constants classes, improving consistency and maintainability of profile installation and retrieval behavior. File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The
PROFILE_NOT_FOUNDconstant was changed to include a%splaceholder; double-check all existing usages outside this diff to ensure they are now usingString.formatand won’t log a literal%sor throw a formatting error. - Consider removing the
// Made with Bobcomment fromProfileResponseConstantsto keep source comments focused on implementation details and avoid non-technical annotations in production code.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `PROFILE_NOT_FOUND` constant was changed to include a `%s` placeholder; double-check all existing usages outside this diff to ensure they are now using `String.format` and won’t log a literal `%s` or throw a formatting error.
- Consider removing the `// Made with Bob` comment from `ProfileResponseConstants` to keep source comments focused on implementation details and avoid non-technical annotations in production code.
## Individual Comments
### Comment 1
<location path="src/main/java/com/kruize/optimizer/service/ProfileService.java" line_range="254" />
<code_context>
kruizeClient.createLayer(profileDefinition);
break;
default:
- throw new IllegalArgumentException("Unknown profile type: " + profileType);
+ throw new IllegalArgumentException(String.format(MessageConstants.ERROR_UNKNOWN_PROFILE_TYPE, profileType));
</code_context>
<issue_to_address>
**suggestion:** Repeated `Unknown profile type` switch defaults suggest the type should be modeled more strongly.
Multiple `switch` statements on `profileType` all fall back to `ERROR_UNKNOWN_PROFILE_TYPE`, which implies a closed set of values (metadata/metric/layer). Consider modelling `profileType` as an enum (or reusing `ProfileType` consistently) instead of strings so the compiler enforces valid values and you can eliminate these repeated runtime checks.
Suggested implementation:
```java
private Object loadProfileFromLocal(ProfileType profileType, String profileName, String profileVersion) {
```
To fully apply the suggestion across the codebase, the following additional changes are likely needed in this file and related classes:
1. Ensure `ProfileType` is available in this file:
- If not already imported, add `import com.kruize.optimizer.model.ProfileType;` (or the correct package for the existing `ProfileType` enum) near the top of `ProfileService.java`.
2. Update all methods and fields that currently use `String profileType` to use `ProfileType` instead:
- Method parameters, local variables, and fields of type `String` that represent profile types should be changed to `ProfileType`.
- For example, any `switch (profileType)` that currently switches on a `String` should now switch on `ProfileType`.
3. Update call sites of `loadProfileFromLocal`:
- Wherever `loadProfileFromLocal` is called with a `String profileType`, convert the string to the enum, e.g.:
- `loadProfileFromLocal(ProfileType.valueOf(profileTypeString.toUpperCase(Locale.ROOT)), profileName, profileVersion);`
- Or better, through a safer factory like `ProfileType.fromValue(profileTypeString)` if such a helper exists.
4. Update `getResourcePath` (and similar helpers) to accept `ProfileType`:
- If `getResourcePath` currently has a signature like `getResourcePath(String profileType, ...)`, update it to `getResourcePath(ProfileType profileType, ...)`.
- Inside that method, use `profileType.name()` or `profileType.getValue()` (depending on how the enum is defined) to build paths or messages.
5. Align other `switch` statements and defaults:
- For all other `switch` statements on `profileType` in this file (and possibly others), ensure they switch on `ProfileType` instead of `String`.
- Once `ProfileType` is an enum, consider either:
- Removing the `default` branch and letting the compiler catch unhandled enum constants, or
- Keeping the `default` but changing it to throw an `IllegalStateException` to represent an unexpected/unhandled enum value rather than an "unknown" string value.
These changes will ensure `profileType` is modeled as a closed, compiler-enforced set of values and reduce the need for repeated runtime "unknown profile type" checks.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
…rce and MetricProfileResource
shekhar316
reviewed
Jun 3, 2026
shekhar316
reviewed
Jun 3, 2026
shekhar316
requested changes
Jun 3, 2026
Contributor
|
@dinogun can we please review and merge this? Thank you. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Earlier ProfileService was using hard coded messages and now it has been changed to usage from Constants file.
Changes
Fixes # (issue)
https://github.com/kruize/kruize-optimizer/issues/9
Type of change
How has this been tested?
Please describe the tests that were run to verify your changes and steps to reproduce. Please specify any test configuration required.
Test Configuration
Checklist 🎯
Summary by Sourcery
Refactor profile handling to use centralized constants for messages and API error parsing.
Bug Fixes:
Enhancements: