Skip to content

Commit

Permalink
fix: Env router change
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Nov 13, 2024
1 parent 0b23f50 commit f280605
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type {
Deployment,
Environment,
Resource,
Workspace,
} from "@ctrlplane/db/schema";
import type { RouterOutputs } from "@ctrlplane/api";
import type { Deployment, Workspace } from "@ctrlplane/db/schema";
import type { JobCondition } from "@ctrlplane/validators/jobs";
import Link from "next/link";
import { IconCircleFilled } from "@tabler/icons-react";
Expand All @@ -22,6 +18,8 @@ import { DeploymentOptionsDropdown } from "~/app/[workspaceSlug]/_components/Dep
import { api } from "~/trpc/server";
import { Release } from "./TableCells";

type Environment = RouterOutputs["environment"]["bySystemId"][number];

const Icon: React.FC<{ children?: React.ReactNode; className?: string }> = ({
children,
className,
Expand All @@ -39,7 +37,7 @@ const Icon: React.FC<{ children?: React.ReactNode; className?: string }> = ({
const EnvIcon: React.FC<{
isFirst?: boolean;
isLast?: boolean;
environment: Environment & { targets: Resource[] };
environment: Environment;
workspaceSlug: string;
systemSlug: string;
}> = ({ environment: env, isFirst, isLast, workspaceSlug, systemSlug }) => {
Expand All @@ -61,7 +59,7 @@ const EnvIcon: React.FC<{
variant="outline"
className="rounded-full text-muted-foreground"
>
{env.targets.length}
{env.resources.length}
</Badge>
</div>
</Link>
Expand All @@ -72,7 +70,7 @@ const EnvIcon: React.FC<{
const ReleaseCell: React.FC<{
workspace: Workspace;
systemSlug: string;
environment: Environment & { targets: Resource[] };
environment: Environment;
release: {
id: string;
name: string;
Expand Down Expand Up @@ -119,7 +117,7 @@ const ReleaseCell: React.FC<{
})
: { items: null };
const releaseJobTriggers = items ?? [];
const hasTargets = env.targets.length > 0;
const hasResources = env.resources.length > 0;
const hasRelease = release != null;
const jc = releaseJobTriggers
.filter(
Expand All @@ -131,7 +129,7 @@ const ReleaseCell: React.FC<{
.map((releaseJobTrigger) => ({ ...releaseJobTrigger }));
return (
<>
{hasRelease && hasTargets && (
{hasRelease && hasResources && (
<Release
releaseId={release.id}
environment={env}
Expand All @@ -145,8 +143,8 @@ const ReleaseCell: React.FC<{
/>
)}

{!hasTargets && hasRelease && (
<div className="text-center text-xs text-muted">No targets</div>
{!hasResources && hasRelease && (
<div className="text-center text-xs text-muted">No resources</div>
)}

{!hasRelease && (
Expand All @@ -159,7 +157,7 @@ const ReleaseCell: React.FC<{
const DeploymentTable: React.FC<{
workspace: Workspace;
systemSlug: string;
environments: Array<Environment & { targets: Resource[] }>;
environments: Environment[];
deployments: Array<
Deployment & {
activeReleases: Array<{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import type { RouterOutputs } from "@ctrlplane/api";
import type * as schema from "@ctrlplane/db/schema";
import { useParams, useRouter } from "next/navigation";
import { IconFilter, IconGraph, IconSettings } from "@tabler/icons-react";
Expand Down Expand Up @@ -28,13 +29,11 @@ import { DeployButton } from "../../DeployButton";
import { Release } from "../../TableCells";
import { ReleaseDistributionGraphPopover } from "./ReleaseDistributionPopover";

type Environment = RouterOutputs["environment"]["bySystemId"][number];

type DeploymentPageContentProps = {
deployment: schema.Deployment & { releaseChannels: schema.ReleaseChannel[] };
environments: {
id: string;
name: string;
targets: { id: string }[];
}[];
environments: Environment[];
releaseChannel: schema.ReleaseChannel | null;
};

Expand Down Expand Up @@ -154,7 +153,7 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
variant="outline"
className="rounded-full px-1.5 font-light text-muted-foreground"
>
{env.targets.length}
{env.resources.length}
</Badge>
</div>
</TableHead>
Expand Down Expand Up @@ -197,7 +196,7 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
(t) => t.environmentId === env.id,
);

const hasTargets = env.targets.length > 0;
const hasResources = env.resources.length > 0;
const isAlreadyDeployed =
environmentReleaseReleaseJobTriggers.length > 0;
const hasJobAgent = deployment.jobAgentId != null;
Expand All @@ -212,7 +211,7 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
const showDeployButton =
!isAlreadyDeployed &&
hasJobAgent &&
hasTargets &&
hasResources &&
!isBlockedByReleaseChannel;

return (
Expand Down Expand Up @@ -275,8 +274,8 @@ export const DeploymentPageContent: React.FC<DeploymentPageContentProps> = ({
"No job agent"}
{!isBlockedByReleaseChannel &&
hasJobAgent &&
!hasTargets &&
"No targets"}
!hasResources &&
"No resources"}
</div>
)}
</div>
Expand Down
22 changes: 11 additions & 11 deletions packages/api/src/router/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
updateEnvironment,
} from "@ctrlplane/db/schema";
import { getEventsForEnvironmentDeleted, handleEvent } from "@ctrlplane/events";
import { dispatchJobsForNewTargets } from "@ctrlplane/job-dispatch";
import { dispatchJobsForNewResources } from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";

import { createTRPCRouter, protectedProcedure } from "../trpc";
Expand Down Expand Up @@ -154,7 +154,7 @@ export const environmentRouter = createTRPCRouter({
envs.map(async (e) => ({
...e.environment,
system: e.system,
targets:
resources:
e.environment.resourceFilter != null
? await ctx.db
.select()
Expand Down Expand Up @@ -226,19 +226,19 @@ export const environmentRouter = createTRPCRouter({
.then(takeFirst);

const { resourceFilter } = input.data;
const isUpdatingTargetFilter = resourceFilter != null;
if (isUpdatingTargetFilter) {
const hasTargetFiltersChanged = !_.isEqual(
const isUpdatingResourceFilter = resourceFilter != null;
if (isUpdatingResourceFilter) {
const hasResourceFiltersChanged = !_.isEqual(
oldEnv.environment.resourceFilter,
resourceFilter,
);

if (hasTargetFiltersChanged) {
if (hasResourceFiltersChanged) {
const oldQuery = resourceMatchesMetadata(
ctx.db,
oldEnv.environment.resourceFilter,
);
const newTargets = await ctx.db
const newResources = await ctx.db
.select({ id: resource.id })
.from(resource)
.where(
Expand All @@ -249,14 +249,14 @@ export const environmentRouter = createTRPCRouter({
),
);

if (newTargets.length > 0) {
await dispatchJobsForNewTargets(
if (newResources.length > 0) {
await dispatchJobsForNewResources(
ctx.db,
newTargets.map((t) => t.id),
newResources.map((r) => r.id),
input.id,
);
console.log(
`Found ${newTargets.length} new targets for environment ${input.id}`,
`Found ${newResources.length} new resources for environment ${input.id}`,
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/job-dispatch/src/new-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { createReleaseJobTriggers } from "./release-job-trigger.js";
/**
* Dispatches jobs for new targets added to an environment.
*/
export async function dispatchJobsForNewTargets(
export async function dispatchJobsForNewResources(
db: Tx,
newTargetIds: string[],
newResourceIds: string[],
envId: string,
): Promise<void> {
const releaseJobTriggers = await createReleaseJobTriggers(db, "new_resource")
.targets(newTargetIds)
.targets(newResourceIds)
.environments([envId])
.insert();
if (releaseJobTriggers.length === 0) return;
Expand Down
4 changes: 2 additions & 2 deletions packages/job-dispatch/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { logger } from "@ctrlplane/logger";
import { variablesAES256 } from "@ctrlplane/secrets";

import { dispatchJobsForNewTargets } from "./new-target.js";
import { dispatchJobsForNewResources } from "./new-target.js";

const log = logger.child({ label: "upsert-targets" });

Expand Down Expand Up @@ -58,7 +58,7 @@ const dispatchNewTargets = async (db: Tx, newTargets: Resource[]) => {
)
.then((tgs) => {
if (tgs.length === 0) return;
dispatchJobsForNewTargets(
dispatchJobsForNewResources(
db,
tgs.map((t) => t.id),
env.id,
Expand Down

0 comments on commit f280605

Please sign in to comment.