Skip to content

Commit

Permalink
updatge
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Nov 25, 2024
1 parent 7905c34 commit 47c2446
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 623 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ReleaseCell } from "./ReleaseCell";
const DeploymentsTable: React.FC<{ targetId: string }> = ({ targetId }) => {
const resourceId = targetId;
const jobs = api.job.byResourceId.useQuery(resourceId);
const deployments = api.deployment.byTargetId.useQuery({ resourceId });
const deployments = api.deployment.byTargetId.useQuery(resourceId);
return (
<Table className="w-full min-w-max border-separate border-spacing-0">
<TableBody>
Expand Down Expand Up @@ -110,7 +110,7 @@ export default function TargetPage({
const resourceId = params.targetId;
const target = api.resource.byId.useQuery(resourceId);
const jobs = api.job.byResourceId.useQuery(resourceId);
const deployments = api.deployment.byTargetId.useQuery({ resourceId });
const deployments = api.deployment.byTargetId.useQuery(resourceId);

const unlockTarget = api.resource.unlock.useMutation();
const lockTarget = api.resource.lock.useMutation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function VariablePage({
params: { targetId: string };
}) {
const resourceId = params.targetId;
const deployments = api.deployment.byTargetId.useQuery({ resourceId });
const deployments = api.deployment.byTargetId.useQuery(resourceId);
const variables = api.deployment.variable.byTargetId.useQuery(resourceId);
return (
<div className="">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,24 @@ const DeploymentCard: React.FC<{
export const EnvironmentNode: React.FC<EnvironmentNodeProps> = (node) => {
const { data } = node;

const latestActiveReleasesQ = api.deployment.byTargetId.useQuery(
{
resourceId: data.environment.resource.id,
environmentIds: [data.environment.id],
deploymentIds: data.environment.deployments.map((d) => d.id),
jobsPerDeployment: 1,
showAllStatuses: true,
},
{ refetchInterval: 5_000 },
);
const resourceId = data.environment.resource.id;
const environmentId = data.environment.id;
const latestActiveReleasesQ =
api.resource.activeReleases.byResourceAndEnvironmentId.useQuery(
{ resourceId, environmentId },
{ refetchInterval: 5_000 },
);

const latestActiveReleases = latestActiveReleasesQ.data ?? [];

const isInProgress = latestActiveReleases.some(
(r) => r.releaseJobTrigger?.job.status === JobStatus.InProgress,
(r) => r.releaseJobTrigger.job.status === JobStatus.InProgress,
);
const isPending = latestActiveReleases.some(
(r) => r.releaseJobTrigger?.job.status === JobStatus.Pending,
(r) => r.releaseJobTrigger.job.status === JobStatus.Pending,
);
const isCompleted = latestActiveReleases.every(
(r) => r.releaseJobTrigger?.job.status === JobStatus.Completed,
(r) => r.releaseJobTrigger.job.status === JobStatus.Completed,
);

return (
Expand Down Expand Up @@ -101,16 +98,15 @@ export const EnvironmentNode: React.FC<EnvironmentNodeProps> = (node) => {
data.environment.deployments.map((deployment) => {
const latestActiveRelease = latestActiveReleases.find(
(r) =>
r.releaseJobTrigger?.release.deploymentId === deployment.id,
r.releaseJobTrigger.release.deploymentId === deployment.id,
);
return (
<DeploymentCard
key={deployment.id}
deploymentName={deployment.name}
job={latestActiveRelease?.releaseJobTrigger?.job}
job={latestActiveRelease?.releaseJobTrigger.job}
releaseVersion={
latestActiveRelease?.releaseJobTrigger?.release.version ??
""
latestActiveRelease?.releaseJobTrigger.release.version ?? ""
}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const DeploymentsContent: React.FC<{ targetId: string }> = ({
targetId,
}) => {
const resourceId = targetId;
const deployments = api.deployment.byTargetId.useQuery({ resourceId });
const deployments = api.deployment.byTargetId.useQuery(resourceId);
const targetValues = api.deployment.variable.byTargetId.useQuery(resourceId);

if (!deployments.data || deployments.data.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const VariableContent: React.FC<{
targetVariables: SCHEMA.ResourceVariable[];
}> = ({ targetId, targetVariables }) => {
const resourceId = targetId;
const deployments = api.deployment.byTargetId.useQuery({ resourceId });
const deployments = api.deployment.byTargetId.useQuery(resourceId);
const variables = api.deployment.variable.byTargetId.useQuery(resourceId);
return (
<div className="space-y-8 overflow-y-auto">
Expand Down
62 changes: 10 additions & 52 deletions packages/api/src/router/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
inArray,
isNotNull,
isNull,
lte,
sql,
takeFirst,
takeFirstOrNull,
Expand Down Expand Up @@ -613,33 +612,18 @@ export const deploymentRouter = createTRPCRouter({
}),

byTargetId: protectedProcedure
.input(
z.object({
resourceId: z.string().uuid(),
environmentIds: z.array(z.string().uuid()).optional(),
deploymentIds: z.array(z.string().uuid()).optional(),
jobsPerDeployment: z.number().optional().default(30),
showAllStatuses: z.boolean().optional().default(false),
}),
)
.input(z.string().uuid())
.meta({
authorizationCheck: ({ canUser, input }) =>
canUser
.perform(Permission.DeploymentList)
.on({ type: "resource", id: input.resourceId }),
.on({ type: "resource", id: input }),
})
.query(async ({ ctx, input }) => {
const {
resourceId,
environmentIds,
deploymentIds,
jobsPerDeployment,
showAllStatuses,
} = input;
const tg = await ctx.db
.select()
.from(resource)
.where(and(eq(resource.id, resourceId), isNull(resource.deletedAt)))
.where(and(eq(resource.id, input), isNull(resource.deletedAt)))
.then(takeFirst);

const envs = await ctx.db
Expand All @@ -650,23 +634,9 @@ export const deploymentRouter = createTRPCRouter({
and(
eq(system.workspaceId, tg.workspaceId),
isNotNull(environment.resourceFilter),
environmentIds != null
? inArray(environment.id, environmentIds)
: undefined,
),
);

const rankSubquery = ctx.db
.select({
rank: sql<number>`ROW_NUMBER() OVER (PARTITION BY ${release.deploymentId} ORDER BY ${release.createdAt} DESC)`.as(
"rank",
),
rankReleaseId: release.id,
rankDeploymentId: release.deploymentId,
})
.from(release)
.as("rank_subquery");

return Promise.all(
envs.map((env) =>
ctx.db
Expand All @@ -675,13 +645,6 @@ export const deploymentRouter = createTRPCRouter({
.innerJoin(system, eq(system.id, deployment.systemId))
.innerJoin(environment, eq(environment.systemId, system.id))
.leftJoin(release, eq(release.deploymentId, deployment.id))
.leftJoin(
rankSubquery,
and(
eq(rankSubquery.rankDeploymentId, release.deploymentId),
eq(rankSubquery.rankReleaseId, release.id),
),
)
.innerJoin(
resource,
resourceMatchesMetadata(ctx.db, env.environment.resourceFilter),
Expand All @@ -697,23 +660,18 @@ export const deploymentRouter = createTRPCRouter({
.leftJoin(job, eq(releaseJobTrigger.jobId, job.id))
.where(
and(
eq(resource.id, resourceId),
eq(resource.id, input),
eq(environment.id, env.environment.id),
isNull(resource.deletedAt),
showAllStatuses
? undefined
: inArray(job.status, [
JobStatus.Completed,
JobStatus.Pending,
JobStatus.InProgress,
]),
deploymentIds != null
? inArray(deployment.id, deploymentIds)
: undefined,
lte(rankSubquery.rank, jobsPerDeployment),
inArray(job.status, [
JobStatus.Completed,
JobStatus.Pending,
JobStatus.InProgress,
]),
),
)
.orderBy(deployment.id, releaseJobTrigger.createdAt)
.limit(500)
.then((r) =>
r.map((row) => ({
...row.deployment,
Expand Down
89 changes: 89 additions & 0 deletions packages/api/src/router/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,95 @@ export const resourceRouter = createTRPCRouter({
}),
),

activeReleases: createTRPCRouter({
byResourceAndEnvironmentId: protectedProcedure
.input(
z.object({
resourceId: z.string().uuid(),
environmentId: z.string().uuid(),
}),
)
.meta({
authorizationCheck: ({ canUser, input }) =>
canUser
.perform(Permission.ResourceGet)
.on({ type: "resource", id: input.resourceId }),
})
.query(async ({ ctx, input }) => {
const { resourceId, environmentId } = input;
const rankSubquery = ctx.db
.select({
rank: sql<number>`ROW_NUMBER() OVER (PARTITION BY ${schema.release.deploymentId} ORDER BY ${schema.release.createdAt} DESC)`.as(
"rank",
),
rankReleaseId: schema.release.id,
rankDeploymentId: schema.release.deploymentId,
})
.from(schema.release)
.as("rank_subquery");

return ctx.db
.select()
.from(schema.deployment)
.innerJoin(
schema.system,
eq(schema.system.id, schema.deployment.systemId),
)
.innerJoin(
schema.environment,
eq(schema.environment.systemId, schema.system.id),
)
.innerJoin(
schema.release,
eq(schema.release.deploymentId, schema.deployment.id),
)
.innerJoin(
rankSubquery,
and(
eq(rankSubquery.rankDeploymentId, schema.release.deploymentId),
eq(rankSubquery.rankReleaseId, schema.release.id),
),
)
.innerJoin(
schema.releaseJobTrigger,
and(
eq(schema.releaseJobTrigger.releaseId, schema.release.id),
eq(schema.releaseJobTrigger.environmentId, schema.environment.id),
),
)
.innerJoin(
schema.resource,
eq(schema.resource.id, schema.releaseJobTrigger.resourceId),
)
.innerJoin(
schema.job,
eq(schema.releaseJobTrigger.jobId, schema.job.id),
)
.where(
and(
eq(schema.resource.id, resourceId),
eq(schema.environment.id, environmentId),
isNull(schema.resource.deletedAt),
eq(rankSubquery.rank, 1),
),
)
.orderBy(schema.deployment.id, schema.releaseJobTrigger.createdAt)
.then((r) =>
r.map((row) => ({
...row.deployment,
environment: row.environment,
system: row.system,
releaseJobTrigger: {
...row.release_job_trigger,
job: row.job,
release: row.release,
resourceId: row.resource.id,
},
})),
);
}),
}),

relationships: protectedProcedure
.meta({
authorizationCheck: ({ canUser, input }) =>
Expand Down
14 changes: 14 additions & 0 deletions packages/db/drizzle/0042_absent_cerebro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS "job_resource_relationship" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"job_id" uuid NOT NULL,
"resource_identifier" text NOT NULL
);
--> statement-breakpoint
DROP TABLE "deployment_resource_relationship";--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "job_resource_relationship" ADD CONSTRAINT "job_resource_relationship_job_id_job_id_fk" FOREIGN KEY ("job_id") REFERENCES "public"."job"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "job_resource_relationship_job_id_resource_identifier_index" ON "job_resource_relationship" USING btree ("job_id","resource_identifier");
1 change: 0 additions & 1 deletion packages/db/drizzle/0042_lonely_warstar.sql

This file was deleted.

Loading

0 comments on commit 47c2446

Please sign in to comment.