mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-09 05:58:17 +02:00
add targets and refactor endpoints
This commit is contained in:
@@ -10,10 +10,19 @@ import { fromError } from "zod-validation-error";
|
||||
import logger from "@server/logger";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
|
||||
const listTargetsParamsSchema = z.strictObject({
|
||||
const resourceTargetsParamsSchema = z.strictObject({
|
||||
resourceId: z.coerce.number().int().positive()
|
||||
});
|
||||
|
||||
const providerTargetsParamsSchema = z.strictObject({
|
||||
providerId: z.coerce.number().int().positive()
|
||||
});
|
||||
|
||||
const listTargetsParamsSchema = z.union([
|
||||
resourceTargetsParamsSchema,
|
||||
providerTargetsParamsSchema
|
||||
]);
|
||||
|
||||
const listTargetsSchema = z.strictObject({
|
||||
limit: z
|
||||
.string()
|
||||
@@ -29,7 +38,7 @@ const listTargetsSchema = z.strictObject({
|
||||
.pipe(z.int().nonnegative())
|
||||
});
|
||||
|
||||
function queryTargets(resourceId: number) {
|
||||
function queryTargets(owner: { resourceId: number } | { providerId: number }) {
|
||||
const baseQuery = db
|
||||
.select({
|
||||
targetId: targets.targetId,
|
||||
@@ -39,6 +48,7 @@ function queryTargets(resourceId: number) {
|
||||
port: targets.port,
|
||||
enabled: targets.enabled,
|
||||
resourceId: targets.resourceId,
|
||||
providerId: targets.providerId,
|
||||
siteId: targets.siteId,
|
||||
siteType: sites.type,
|
||||
siteName: sites.name,
|
||||
@@ -71,7 +81,11 @@ function queryTargets(resourceId: number) {
|
||||
targetHealthCheck,
|
||||
eq(targetHealthCheck.targetId, targets.targetId)
|
||||
)
|
||||
.where(eq(targets.resourceId, resourceId));
|
||||
.where(
|
||||
"providerId" in owner
|
||||
? eq(targets.providerId, owner.providerId)
|
||||
: eq(targets.resourceId, owner.resourceId)
|
||||
);
|
||||
|
||||
return baseQuery;
|
||||
}
|
||||
@@ -94,7 +108,7 @@ registry.registerPath({
|
||||
description: "List targets for a resource.",
|
||||
tags: [OpenAPITags.PublicResourceLegacy],
|
||||
request: {
|
||||
params: listTargetsParamsSchema,
|
||||
params: resourceTargetsParamsSchema,
|
||||
query: listTargetsSchema
|
||||
},
|
||||
responses: {
|
||||
@@ -121,7 +135,34 @@ registry.registerPath({
|
||||
description: "List targets for a resource.",
|
||||
tags: [OpenAPITags.PublicResource, OpenAPITags.Target],
|
||||
request: {
|
||||
params: listTargetsParamsSchema,
|
||||
params: resourceTargetsParamsSchema,
|
||||
query: listTargetsSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Successful response",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
data: z.record(z.string(), z.any()).nullable(),
|
||||
success: z.boolean(),
|
||||
error: z.boolean(),
|
||||
message: z.string(),
|
||||
status: z.number()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "get",
|
||||
path: "/ai-provider/{providerId}/targets",
|
||||
description: "List targets for an AI provider.",
|
||||
tags: [OpenAPITags.AiProvider],
|
||||
request: {
|
||||
params: providerTargetsParamsSchema,
|
||||
query: listTargetsSchema
|
||||
},
|
||||
responses: {
|
||||
@@ -168,14 +209,18 @@ export async function listTargets(
|
||||
)
|
||||
);
|
||||
}
|
||||
const { resourceId } = parsedParams.data;
|
||||
const owner = parsedParams.data;
|
||||
const ownerCondition =
|
||||
"providerId" in owner
|
||||
? eq(targets.providerId, owner.providerId)
|
||||
: eq(targets.resourceId, owner.resourceId);
|
||||
|
||||
const baseQuery = queryTargets(resourceId);
|
||||
const baseQuery = queryTargets(owner);
|
||||
|
||||
const countQuery = db
|
||||
.select({ count: sql<number>`cast(count(*) as integer)` })
|
||||
.from(targets)
|
||||
.where(eq(targets.resourceId, resourceId));
|
||||
.where(ownerCondition);
|
||||
|
||||
const targetsList = await baseQuery.limit(limit).offset(offset);
|
||||
const totalCountResult = await countQuery;
|
||||
|
||||
Reference in New Issue
Block a user