Skip to content

Commit

Permalink
fix: Check existence from identifiers (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Oct 21, 2024
1 parent 3af3eae commit e6e9666
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/event-worker/src/target-scan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const createTargetScanWorker = () =>
logger.info(
`Upserting ${targets.length} targets for provider ${tp.target_provider.id}`,
);
await upsertTargets(db, tp.target_provider.id, targets);
await upsertTargets(db, targets);
} catch (error: any) {
logger.error(
`Error upserting targets for provider ${tp.target_provider.id}: ${error.message}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { SiKubernetes, SiTerraform } from "@icons-pack/react-simple-icons";
import { IconServer, IconTarget, IconUsersGroup } from "@tabler/icons-react";
import {
IconServer,
IconTarget,
IconUserDollar,
IconUsersGroup,
} from "@tabler/icons-react";

export const TargetIcon: React.FC<{ version: string; kind?: string }> = ({
version,
kind,
}) => {
if (kind?.toLowerCase().includes("shared"))
return <IconUsersGroup className="h-4 w-4 shrink-0 text-blue-300" />;
if (kind?.toLowerCase().includes("customer"))
return <IconUserDollar className="h-4 w-4 shrink-0 text-amber-500" />;
if (version.includes("kubernetes"))
return <SiKubernetes className="h-4 w-4 shrink-0 text-blue-300" />;
if (version.includes("vm") || version.includes("compute"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const PATCH = async (
workspaceId: provider.workspaceId,
}));

const targets = await upsertTargets(db, provider.id, targetsToInsert);
const targets = await upsertTargets(db, targetsToInsert);

return NextResponse.json({ targets });
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export const PATCH = async (

const targets = await upsertTargets(
db,
null,
parsedTargets.map((t) => ({ ...t, workspaceId })),
);

Expand Down
30 changes: 23 additions & 7 deletions packages/job-dispatch/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
eq,
inArray,
isNotNull,
isNull,
} from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import {
Expand All @@ -24,14 +23,19 @@ import { dispatchJobsForNewTargets } from "./new-target.js";

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

const getExistingTargets = (db: Tx, providerId: string | null) =>
const getExistingTargets = (
db: Tx,
workspaceId: string,
identifiers: string[],
) =>
db
.select()
.from(target)
.where(
providerId === null
? isNull(target.providerId)
: eq(target.providerId, providerId),
and(
eq(target.workspaceId, workspaceId),
inArray(target.identifier, identifiers),
),
);

const dispatchNewTargets = async (db: Tx, newTargets: Target[]) => {
Expand Down Expand Up @@ -74,11 +78,23 @@ const dispatchNewTargets = async (db: Tx, newTargets: Target[]) => {

export const upsertTargets = async (
tx: Tx,
providerId: string | null,
targetsToInsert: Array<InsertTarget & { metadata?: Record<string, string> }>,
) => {
try {
const targetsBeforeInsert = await getExistingTargets(tx, providerId);
const targetsBeforeInsertPromises = _.chain(targetsToInsert)
.groupBy((t) => t.workspaceId)
.map(async (t) =>
getExistingTargets(
tx,
t[0]!.workspaceId,
t.map((t) => t.identifier),
),
)
.value();

const targetsBeforeInsert = (
await Promise.all(targetsBeforeInsertPromises)
).flat();

const targets = await tx
.insert(target)
Expand Down

0 comments on commit e6e9666

Please sign in to comment.