fix: Optimize release metadata keys query#161
Conversation
WalkthroughThe pull request introduces significant changes to the Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (1)
apps/webservice/src/app/[workspaceSlug]/_components/release-condition/MetadataConditionRender.tsx (1)
95-109: Handle empty metadata keys gracefullyIf
filteredMetadataKeysis empty while not loading, the popover will display an empty list, which might confuse users. Consider displaying a message indicating that no metadata keys were found.You can modify the rendering logic as follows:
{!loadingMetadataKeys && filteredMetadataKeys.length === 0 ? ( <div className="p-2 text-sm text-muted-foreground"> No metadata keys found </div> ) : ( filteredMetadataKeys.map((k) => ( <Button variant="ghost" size="sm" key={k} className="w-full rounded-none text-left" onClick={() => { setKey(k); setOpen(false); }} > <div className="w-full">{k}</div> </Button> )) )}
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (2)
- apps/webservice/src/app/[workspaceSlug]/_components/release-condition/MetadataConditionRender.tsx (4 hunks)
- packages/api/src/router/release.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (3)
packages/api/src/router/release.ts (3)
420-437: Verify authorization checks align with the intended permissionsEnsure that performing
Permission.ReleaseGeton asystementity provides the correct level of access for fetching release metadata keys. If there are specific permissions for metadata access, consider using those instead to maintain precise access control.
439-457: Verify authorization checks align with the intended permissionsSimilarly, confirm that performing
Permission.ReleaseGeton aworkspaceentity is appropriate for accessing workspace-level release metadata keys. Adjust the permission if a more specific one is applicable.
431-436: Optimize query performance by indexing relevant columnsTo improve query performance, especially on large datasets, consider adding indexes on the columns used in the
WHEREandJOINclauses, such asreleaseMetadata.releaseId,deployment.systemId, andsystem.workspaceId. This can speed up data retrieval and reduce query execution time.[performance]
Also applies to: 450-456
| const loadingMetadataKeys = | ||
| workspaceQ.isLoading || | ||
| systemQ.isLoading || | ||
| workspaceMetadataKeys.isLoading || | ||
| systemMetadataKeys.isLoading; |
There was a problem hiding this comment.
Add error handling for query failures
Currently, the code does not handle potential errors from the queries. To enhance user experience, consider checking for errors and providing appropriate feedback if any query fails.
You can check the isError property of your queries and display an error message or fallback UI:
if (
workspaceQ.isError ||
systemQ.isError ||
workspaceMetadataKeys.isError ||
systemMetadataKeys.isError
) {
return <div className="p-2 text-sm text-red-500">Error loading metadata keys.</div>;
}| const systemQ = api.system.bySlug.useQuery( | ||
| { workspaceSlug, systemSlug: systemSlug ?? "" }, | ||
| { enabled: systemSlug != null }, | ||
| ); |
There was a problem hiding this comment.
Avoid passing empty systemSlug when it is null
When systemSlug is null, the query is still receiving systemSlug: "", which may not be necessary and could lead to unintended behavior. It's better to conditionally include systemSlug only when it is defined.
Consider modifying the query parameters as follows:
const systemQ = api.system.bySlug.useQuery(
- { workspaceSlug, systemSlug: systemSlug ?? "" },
+ systemSlug ? { workspaceSlug, systemSlug } : undefined,
{ enabled: systemSlug != null },
);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const systemQ = api.system.bySlug.useQuery( | |
| { workspaceSlug, systemSlug: systemSlug ?? "" }, | |
| { enabled: systemSlug != null }, | |
| ); | |
| const systemQ = api.system.bySlug.useQuery( | |
| systemSlug ? { workspaceSlug, systemSlug } : undefined, | |
| { enabled: systemSlug != null }, | |
| ); |
| bySystem: protectedProcedure | ||
| .meta({ | ||
| authorizationCheck: ({ canUser, input }) => | ||
| canUser.perform(Permission.ReleaseGet).on({ | ||
| type: "system", | ||
| id: input, | ||
| }), | ||
| }) | ||
| .input(z.string().uuid()) | ||
| .query(async ({ input, ctx }) => | ||
| ctx.db | ||
| .selectDistinct({ key: releaseMetadata.key }) | ||
| .from(release) | ||
| .innerJoin(releaseMetadata, eq(releaseMetadata.releaseId, release.id)) | ||
| .innerJoin(deployment, eq(release.deploymentId, deployment.id)) | ||
| .where(eq(deployment.systemId, input)) | ||
| .then((r) => r.map((row) => row.key)), | ||
| ), |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Consider refactoring duplicated code in metadataKeys procedures
The bySystem and byWorkspace procedures share nearly identical query logic, differing mainly in the authorization checks and the where clause conditions. To enhance maintainability and reduce code duplication, consider abstracting the common query logic into a shared helper function or method that accepts parameters for the varying parts, such as the entity type and ID.
Also applies to: 439-457
Summary by CodeRabbit
New Features
Bug Fixes
Documentation