Skip to content
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

Fix: show latest release by deployment and environment #169

Merged
merged 9 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ const DeploymentTable: React.FC<{
environments: Array<Environment & { targets: Target[] }>;
deployments: Array<
Deployment & {
latestRelease: {
activeReleases: Array<{
id: string;
name: string;
version: string;
createdAt: Date;
} | null;
environmentId: string;
}> | null;
}
>;
workspaceSlug: string;
Expand Down Expand Up @@ -178,6 +179,9 @@ const DeploymentTable: React.FC<{
</td>

{environments.map((env, envIdx) => {
const release =
r.activeReleases?.find((r) => r.environmentId === env.id) ??
null;
return (
<td
key={env.id}
Expand All @@ -189,7 +193,7 @@ const DeploymentTable: React.FC<{
)}
>
<ReleaseCell
release={r.latestRelease}
release={release}
environment={env}
deployment={r}
workspaceSlug={workspaceSlug}
Expand Down
43 changes: 28 additions & 15 deletions packages/api/src/router/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Tx } from "@ctrlplane/db";
import _ from "lodash";
import { isPresent } from "ts-is-present";
import { z } from "zod";

import {
Expand Down Expand Up @@ -30,7 +32,7 @@ import { JobStatus } from "@ctrlplane/validators/jobs";
import { createTRPCRouter, protectedProcedure } from "../trpc";
import { deploymentVariableRouter } from "./deployment-variable";

const latestReleaseSubQuery = (db: Tx) =>
const latestActiveReleaseSubQuery = (db: Tx) =>
db
.select({
id: release.id,
Expand All @@ -39,13 +41,15 @@ const latestReleaseSubQuery = (db: Tx) =>
createdAt: release.createdAt,
name: release.name,
config: release.config,
environmentId: releaseJobTrigger.environmentId,

rank: sql<number>`ROW_NUMBER() OVER (PARTITION BY deployment_id ORDER BY created_at DESC)`.as(
rank: sql<number>`ROW_NUMBER() OVER (PARTITION BY ${release.deploymentId}, ${releaseJobTrigger.environmentId} ORDER BY ${release.createdAt} DESC)`.as(
"rank",
),
})
.from(release)
.as("release");
.innerJoin(releaseJobTrigger, eq(releaseJobTrigger.releaseId, release.id))
.as("active_release");
zacharyblasczyk marked this conversation as resolved.
Show resolved Hide resolved

export const deploymentRouter = createTRPCRouter({
variable: deploymentVariableRouter,
Expand All @@ -63,7 +67,7 @@ export const deploymentRouter = createTRPCRouter({
id: job.id,
status: job.status,
targetId: releaseJobTrigger.targetId,
rank: sql<number>`ROW_NUMBER() OVER (PARTITION BY release_job_trigger.target_id ORDER BY job.created_at DESC)`.as(
rank: sql<number>`ROW_NUMBER() OVER (PARTITION BY ${releaseJobTrigger.targetId} ORDER BY ${job.createdAt} DESC)`.as(
"rank",
),
})
Expand Down Expand Up @@ -203,20 +207,26 @@ export const deploymentRouter = createTRPCRouter({
.on({ type: "system", id: input }),
})
.query(async ({ ctx, input }) => {
const latestRelease = latestReleaseSubQuery(ctx.db);
const activeRelease = latestActiveReleaseSubQuery(ctx.db);
return ctx.db
.select()
.from(deployment)
.leftJoin(
latestRelease,
activeRelease,
and(
eq(latestRelease.deploymentId, deployment.id),
eq(latestRelease.rank, 1),
eq(activeRelease.deploymentId, deployment.id),
eq(activeRelease.rank, 1),
),
)
.where(eq(deployment.systemId, input))
.then((r) =>
r.map((row) => ({ ...row.deployment, latestRelease: row.release })),
.then((ts) =>
_.chain(ts)
.groupBy((t) => t.deployment.id)
.map((t) => ({
...t[0]!.deployment,
activeReleases: t.map((a) => a.active_release).filter(isPresent),
}))
zacharyblasczyk marked this conversation as resolved.
Show resolved Hide resolved
.value(),
);
}),

Expand Down Expand Up @@ -304,21 +314,24 @@ export const deploymentRouter = createTRPCRouter({
})
.input(z.string().uuid())
.query(async ({ ctx, input }) => {
const latestRelease = latestReleaseSubQuery(ctx.db);
const activeRelease = latestActiveReleaseSubQuery(ctx.db);
return ctx.db
.select()
.from(deployment)
.innerJoin(system, eq(system.id, deployment.systemId))
.leftJoin(
latestRelease,
activeRelease,
and(
eq(latestRelease.deploymentId, deployment.id),
eq(latestRelease.rank, 1),
eq(activeRelease.deploymentId, deployment.id),
eq(activeRelease.rank, 1),
),
)
.where(eq(system.workspaceId, input))
.then((r) =>
r.map((row) => ({ ...row.deployment, latestRelease: row.release })),
r.map((row) => ({
...row.deployment,
latestRelease: row.active_release,
zacharyblasczyk marked this conversation as resolved.
Show resolved Hide resolved
})),
);
}),
});
Loading