-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Init View Filters #159
Closed
Closed
Init View Filters #159
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a80167
Init View Group Filters
zacharyblasczyk 7d65102
format
zacharyblasczyk 2a3e63c
drop 0018
zacharyblasczyk d237c69
Merge remote-tracking branch 'origin' into zacharyb/eng-236
zacharyblasczyk 08d9403
0019 migrate
zacharyblasczyk 62697ce
fix
zacharyblasczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
import _ from "lodash"; | ||
import { z } from "zod"; | ||
|
||
import { | ||
and, | ||
asc, | ||
count, | ||
eq, | ||
inArray, | ||
sql, | ||
takeFirst, | ||
takeFirstOrNull, | ||
} from "@ctrlplane/db"; | ||
import { | ||
createTargetViewMetadataGroup, | ||
target, | ||
targetMetadata, | ||
targetView, | ||
targetViewMetadataGroup, | ||
targetViewMetadataGroupKey, | ||
updateTargetViewMetadataGroup, | ||
} from "@ctrlplane/db/schema"; | ||
import { Permission } from "@ctrlplane/validators/auth"; | ||
|
||
import { createTRPCRouter, protectedProcedure } from "../trpc"; | ||
|
||
export const targetViewMetadataGroupRouter = createTRPCRouter({ | ||
groups: protectedProcedure | ||
.meta({ | ||
authorizationCheck: ({ canUser, input }) => | ||
canUser | ||
.perform(Permission.TargetViewMetadataGroupList) | ||
.on({ type: "workspace", id: input }), | ||
}) | ||
.input(z.string().uuid()) | ||
.query(async ({ ctx, input }) => { | ||
const groups = await ctx.db | ||
.select({ | ||
group: targetViewMetadataGroup, | ||
keys: sql<string[]>`array_agg(${targetViewMetadataGroupKey.key})`.as( | ||
"keys", | ||
), | ||
targets: count(target.id).as("targets"), | ||
}) | ||
.from(targetViewMetadataGroup) | ||
.leftJoin( | ||
targetViewMetadataGroupKey, | ||
eq(targetViewMetadataGroup.id, targetViewMetadataGroupKey.groupId), | ||
) | ||
.leftJoin(targetView, eq(targetViewMetadataGroup.viewId, targetView.id)) | ||
.leftJoin(target, eq(targetView.workspaceId, target.workspaceId)) | ||
.leftJoin( | ||
targetMetadata, | ||
and( | ||
eq(target.id, targetMetadata.targetId), | ||
inArray( | ||
targetMetadata.key, | ||
sql`array_agg(${targetViewMetadataGroupKey.key})`, | ||
), | ||
), | ||
) | ||
.where(eq(targetViewMetadataGroup.viewId, input)) | ||
.groupBy(targetViewMetadataGroup.id) | ||
.orderBy(asc(targetViewMetadataGroup.name)); | ||
|
||
return groups; | ||
}), | ||
|
||
byId: protectedProcedure | ||
.meta({ | ||
authorizationCheck: ({ canUser, input }) => | ||
canUser | ||
.perform(Permission.TargetViewMetadataGroupGet) | ||
.on({ type: "targetViewMetadataGroup", id: input }), | ||
}) | ||
.input(z.string().uuid()) | ||
.query(async ({ ctx, input }) => { | ||
const group = await ctx.db | ||
.select({ | ||
group: targetViewMetadataGroup, | ||
keys: sql<string[]>`array_agg(${targetViewMetadataGroupKey.key})`.as( | ||
"keys", | ||
), | ||
}) | ||
.from(targetViewMetadataGroup) | ||
.leftJoin( | ||
targetViewMetadataGroupKey, | ||
eq(targetViewMetadataGroup.id, targetViewMetadataGroupKey.groupId), | ||
) | ||
.where(eq(targetViewMetadataGroup.id, input)) | ||
.groupBy(targetViewMetadataGroup.id) | ||
.then(takeFirstOrNull); | ||
|
||
if (group == null) throw new Error("Group not found"); | ||
|
||
const targetMetadataAgg = ctx.db | ||
.select({ | ||
id: target.id, | ||
metadata: sql<Record<string, string>>`jsonb_object_agg( | ||
${targetMetadata.key}, | ||
${targetMetadata.value} | ||
)`.as("metadata"), | ||
}) | ||
.from(target) | ||
.innerJoin(targetView, eq(target.workspaceId, targetView.workspaceId)) | ||
.innerJoin( | ||
targetMetadata, | ||
and( | ||
eq(target.id, targetMetadata.targetId), | ||
inArray(targetMetadata.key, group.keys), | ||
), | ||
) | ||
.where(eq(targetView.id, group.group.viewId)) | ||
.groupBy(target.id) | ||
.having( | ||
sql<number>`COUNT(DISTINCT ${targetMetadata.key}) = ${group.keys.length}`, | ||
) | ||
.as("target_metadata_agg"); | ||
|
||
const combinations = await ctx.db | ||
.with(targetMetadataAgg) | ||
.select({ | ||
metadata: targetMetadataAgg.metadata, | ||
targets: sql<number>`COUNT(*)`.as("targets"), | ||
}) | ||
.from(targetMetadataAgg) | ||
.groupBy(targetMetadataAgg.metadata); | ||
|
||
return { | ||
...group.group, | ||
keys: group.keys, | ||
combinations, | ||
}; | ||
}), | ||
|
||
create: protectedProcedure | ||
.meta({ | ||
authorizationCheck: ({ canUser, input }) => | ||
canUser | ||
.perform(Permission.TargetViewMetadataGroupCreate) | ||
.on({ type: "workspace", id: input.viewId }), | ||
}) | ||
.input( | ||
createTargetViewMetadataGroup.extend({ | ||
keys: z.array(z.string()), | ||
}), | ||
) | ||
.mutation(async ({ ctx, input }) => { | ||
return ctx.db.transaction(async (tx) => { | ||
const [group] = await tx | ||
.insert(targetViewMetadataGroup) | ||
.values(createTargetViewMetadataGroup.parse(input)) | ||
.returning(); | ||
|
||
if (!group) throw new Error("Group creation failed"); | ||
|
||
await tx.insert(targetViewMetadataGroupKey).values( | ||
input.keys.map((key) => ({ | ||
groupId: group.id, | ||
key, | ||
})), | ||
); | ||
|
||
return group; | ||
}); | ||
}), | ||
|
||
update: protectedProcedure | ||
.meta({ | ||
authorizationCheck: ({ canUser, input }) => | ||
canUser | ||
.perform(Permission.TargetViewMetadataGroupUpdate) | ||
.on({ type: "targetViewMetadataGroup", id: input.id }), | ||
}) | ||
.input( | ||
z.object({ | ||
id: z.string().uuid(), | ||
data: updateTargetViewMetadataGroup.extend({ | ||
keys: z.array(z.string()).optional(), | ||
}), | ||
}), | ||
) | ||
.mutation(async ({ ctx, input }) => { | ||
return ctx.db.transaction(async (tx) => { | ||
if (input.data.name || input.data.viewId) { | ||
await tx | ||
.update(targetViewMetadataGroup) | ||
.set(updateTargetViewMetadataGroup.parse(input.data)) | ||
.where(eq(targetViewMetadataGroup.id, input.id)); | ||
} | ||
|
||
if (input.data.keys) { | ||
await tx | ||
.delete(targetViewMetadataGroupKey) | ||
.where(eq(targetViewMetadataGroupKey.groupId, input.id)); | ||
|
||
await tx.insert(targetViewMetadataGroupKey).values( | ||
input.data.keys.map((key) => ({ | ||
groupId: input.id, | ||
key, | ||
})), | ||
); | ||
} | ||
|
||
return tx | ||
.select() | ||
.from(targetViewMetadataGroup) | ||
.where(eq(targetViewMetadataGroup.id, input.id)) | ||
.then(takeFirst); | ||
}); | ||
}), | ||
|
||
delete: protectedProcedure | ||
.meta({ | ||
authorizationCheck: ({ canUser, input }) => | ||
canUser | ||
.perform(Permission.TargetViewMetadataGroupDelete) | ||
.on({ type: "targetViewMetadataGroup", id: input }), | ||
}) | ||
.input(z.string().uuid()) | ||
.mutation(({ ctx, input }) => | ||
ctx.db | ||
.delete(targetViewMetadataGroup) | ||
.where(eq(targetViewMetadataGroup.id, input)), | ||
), | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
ALTER TYPE "scope_type" ADD VALUE 'targetViewMetadataGroup';--> statement-breakpoint | ||
CREATE TABLE IF NOT EXISTS "target_view_metadata_group" ( | ||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, | ||
"view_id" uuid NOT NULL, | ||
"name" text NOT NULL | ||
); | ||
--> statement-breakpoint | ||
CREATE TABLE IF NOT EXISTS "target_view_metadata_group_key" ( | ||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, | ||
"group_id" uuid NOT NULL, | ||
"key" text NOT NULL | ||
); | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "target_view_metadata_group" ADD CONSTRAINT "target_view_metadata_group_view_id_target_view_id_fk" FOREIGN KEY ("view_id") REFERENCES "public"."target_view"("id") ON DELETE cascade ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
--> statement-breakpoint | ||
DO $$ BEGIN | ||
ALTER TABLE "target_view_metadata_group_key" ADD CONSTRAINT "target_view_metadata_group_key_group_id_target_view_metadata_group_id_fk" FOREIGN KEY ("group_id") REFERENCES "public"."target_view_metadata_group"("id") ON DELETE cascade ON UPDATE no action; | ||
EXCEPTION | ||
WHEN duplicate_object THEN null; | ||
END $$; | ||
--> statement-breakpoint | ||
CREATE UNIQUE INDEX IF NOT EXISTS "target_view_metadata_group_view_id_name_index" ON "target_view_metadata_group" USING btree ("view_id","name");--> statement-breakpoint | ||
CREATE UNIQUE INDEX IF NOT EXISTS "target_view_metadata_group_key_group_id_key_index" ON "target_view_metadata_group_key" USING btree ("group_id","key"); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Review the complex query in the
byId
procedure for performanceThe query in the
byId
procedure (lines 96-118) includes multiple joins and aggregations, which could affect performance on large datasets. Consider analyzing the query execution plan and optimizing it if necessary, perhaps by adding appropriate indexes or simplifying the query.[performance]