mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-04 17:56:38 +00:00
add status column in resource table
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
|||||||
resourcePassword,
|
resourcePassword,
|
||||||
resourcePincode,
|
resourcePincode,
|
||||||
targets,
|
targets,
|
||||||
|
targetHealthCheck,
|
||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
@@ -63,6 +64,9 @@ type JoinedRow = {
|
|||||||
targetIp: string | null;
|
targetIp: string | null;
|
||||||
targetPort: number | null;
|
targetPort: number | null;
|
||||||
targetEnabled: boolean | null;
|
targetEnabled: boolean | null;
|
||||||
|
|
||||||
|
hcHealth: string | null;
|
||||||
|
hcEnabled: boolean | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// grouped by resource with targets[])
|
// grouped by resource with targets[])
|
||||||
@@ -87,6 +91,7 @@ export type ResourceWithTargets = {
|
|||||||
ip: string;
|
ip: string;
|
||||||
port: number;
|
port: number;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
|
healthStatus?: 'healthy' | 'unhealthy' | 'unknown';
|
||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -114,6 +119,8 @@ function queryResources(accessibleResourceIds: number[], orgId: string) {
|
|||||||
targetPort: targets.port,
|
targetPort: targets.port,
|
||||||
targetEnabled: targets.enabled,
|
targetEnabled: targets.enabled,
|
||||||
|
|
||||||
|
hcHealth: targetHealthCheck.hcHealth,
|
||||||
|
hcEnabled: targetHealthCheck.hcEnabled,
|
||||||
})
|
})
|
||||||
.from(resources)
|
.from(resources)
|
||||||
.leftJoin(
|
.leftJoin(
|
||||||
@@ -129,6 +136,10 @@ function queryResources(accessibleResourceIds: number[], orgId: string) {
|
|||||||
eq(resourceHeaderAuth.resourceId, resources.resourceId)
|
eq(resourceHeaderAuth.resourceId, resources.resourceId)
|
||||||
)
|
)
|
||||||
.leftJoin(targets, eq(targets.resourceId, resources.resourceId))
|
.leftJoin(targets, eq(targets.resourceId, resources.resourceId))
|
||||||
|
.leftJoin(
|
||||||
|
targetHealthCheck,
|
||||||
|
eq(targetHealthCheck.targetId, targets.targetId)
|
||||||
|
)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
inArray(resources.resourceId, accessibleResourceIds),
|
inArray(resources.resourceId, accessibleResourceIds),
|
||||||
@@ -269,18 +280,19 @@ export async function listResources(
|
|||||||
map.set(row.resourceId, entry);
|
map.set(row.resourceId, entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push target if present
|
if (row.targetId != null && row.targetIp && row.targetPort != null && row.targetEnabled != null) {
|
||||||
if (
|
let healthStatus: 'healthy' | 'unhealthy' | 'unknown' = 'unknown';
|
||||||
row.targetId != null &&
|
|
||||||
row.targetIp &&
|
if (row.hcEnabled && row.hcHealth) {
|
||||||
row.targetPort != null &&
|
healthStatus = row.hcHealth as 'healthy' | 'unhealthy' | 'unknown';
|
||||||
row.targetEnabled != null
|
}
|
||||||
) {
|
|
||||||
entry.targets.push({
|
entry.targets.push({
|
||||||
targetId: row.targetId,
|
targetId: row.targetId,
|
||||||
ip: row.targetIp,
|
ip: row.targetIp,
|
||||||
port: row.targetPort,
|
port: row.targetPort,
|
||||||
enabled: row.targetEnabled,
|
enabled: row.targetEnabled,
|
||||||
|
healthStatus: healthStatus,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,14 @@ export default async function ResourcesPage(props: ResourcesPageProps) {
|
|||||||
: "not_protected",
|
: "not_protected",
|
||||||
enabled: resource.enabled,
|
enabled: resource.enabled,
|
||||||
domainId: resource.domainId || undefined,
|
domainId: resource.domainId || undefined,
|
||||||
ssl: resource.ssl
|
ssl: resource.ssl,
|
||||||
|
targets: resource.targets?.map(target => ({
|
||||||
|
targetId: target.targetId,
|
||||||
|
ip: target.ip,
|
||||||
|
port: target.port,
|
||||||
|
enabled: target.enabled,
|
||||||
|
healthStatus: target.healthStatus
|
||||||
|
}))
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ import {
|
|||||||
Plus,
|
Plus,
|
||||||
Search,
|
Search,
|
||||||
ChevronDown,
|
ChevronDown,
|
||||||
|
Clock,
|
||||||
|
Wifi,
|
||||||
|
WifiOff,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
@@ -70,6 +73,15 @@ import EditInternalResourceDialog from "@app/components/EditInternalResourceDial
|
|||||||
import CreateInternalResourceDialog from "@app/components/CreateInternalResourceDialog";
|
import CreateInternalResourceDialog from "@app/components/CreateInternalResourceDialog";
|
||||||
import { Alert, AlertDescription } from "@app/components/ui/alert";
|
import { Alert, AlertDescription } from "@app/components/ui/alert";
|
||||||
|
|
||||||
|
|
||||||
|
export type TargetHealth = {
|
||||||
|
targetId: number;
|
||||||
|
ip: string;
|
||||||
|
port: number;
|
||||||
|
enabled: boolean;
|
||||||
|
healthStatus?: 'healthy' | 'unhealthy' | 'unknown';
|
||||||
|
};
|
||||||
|
|
||||||
export type ResourceRow = {
|
export type ResourceRow = {
|
||||||
id: number;
|
id: number;
|
||||||
nice: string | null;
|
nice: string | null;
|
||||||
@@ -85,8 +97,52 @@ export type ResourceRow = {
|
|||||||
ssl: boolean;
|
ssl: boolean;
|
||||||
targetHost?: string;
|
targetHost?: string;
|
||||||
targetPort?: number;
|
targetPort?: number;
|
||||||
|
targets?: TargetHealth[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function getOverallHealthStatus(targets?: TargetHealth[]): 'online' | 'degraded' | 'offline' | 'unknown' {
|
||||||
|
if (!targets || targets.length === 0) {
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
const monitoredTargets = targets.filter(t => t.enabled && t.healthStatus && t.healthStatus !== 'unknown');
|
||||||
|
|
||||||
|
if (monitoredTargets.length === 0) {
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
const healthyCount = monitoredTargets.filter(t => t.healthStatus === 'healthy').length;
|
||||||
|
const unhealthyCount = monitoredTargets.filter(t => t.healthStatus === 'unhealthy').length;
|
||||||
|
|
||||||
|
if (healthyCount === monitoredTargets.length) {
|
||||||
|
return 'online';
|
||||||
|
} else if (unhealthyCount === monitoredTargets.length) {
|
||||||
|
return 'offline';
|
||||||
|
} else {
|
||||||
|
return 'degraded';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function StatusIcon({ status, className = "" }: {
|
||||||
|
status: 'online' | 'degraded' | 'offline' | 'unknown';
|
||||||
|
className?: string;
|
||||||
|
}) {
|
||||||
|
const iconClass = `h-4 w-4 ${className}`;
|
||||||
|
|
||||||
|
switch (status) {
|
||||||
|
case 'online':
|
||||||
|
return <Wifi className={`${iconClass} text-green-500`} />;
|
||||||
|
case 'degraded':
|
||||||
|
return <Wifi className={`${iconClass} text-yellow-500`} />;
|
||||||
|
case 'offline':
|
||||||
|
return <WifiOff className={`${iconClass} text-red-500`} />;
|
||||||
|
case 'unknown':
|
||||||
|
return <Clock className={`${iconClass} text-gray-400`} />;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
export type InternalResourceRow = {
|
export type InternalResourceRow = {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -150,6 +206,7 @@ const setStoredPageSize = (pageSize: number, tableId?: string): void => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default function ResourcesTable({
|
export default function ResourcesTable({
|
||||||
resources,
|
resources,
|
||||||
internalResources,
|
internalResources,
|
||||||
@@ -361,6 +418,76 @@ export default function ResourcesTable({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function TargetStatusCell({ targets }: { targets?: TargetHealth[] }) {
|
||||||
|
const overallStatus = getOverallHealthStatus(targets);
|
||||||
|
|
||||||
|
if (!targets || targets.length === 0) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<StatusIcon status="unknown" />
|
||||||
|
<span className="text-sm text-muted-foreground">No targets</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const monitoredTargets = targets.filter(t => t.enabled && t.healthStatus && t.healthStatus !== 'unknown');
|
||||||
|
const unknownTargets = targets.filter(t => !t.enabled || !t.healthStatus || t.healthStatus === 'unknown');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="sm" className="flex items-center gap-2 h-8">
|
||||||
|
<StatusIcon status={overallStatus} />
|
||||||
|
<span className="text-sm">
|
||||||
|
{overallStatus === 'online' && 'Healthy'}
|
||||||
|
{overallStatus === 'degraded' && 'Degraded'}
|
||||||
|
{overallStatus === 'offline' && 'Offline'}
|
||||||
|
{overallStatus === 'unknown' && 'Unknown'}
|
||||||
|
</span>
|
||||||
|
<ChevronDown className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="start" className="min-w-[280px]">
|
||||||
|
{monitoredTargets.length > 0 && (
|
||||||
|
<>
|
||||||
|
{monitoredTargets.map((target) => (
|
||||||
|
<DropdownMenuItem key={target.targetId} className="flex items-center justify-between gap-4">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<StatusIcon
|
||||||
|
status={target.healthStatus === 'healthy' ? 'online' : 'offline'}
|
||||||
|
className="h-3 w-3"
|
||||||
|
/>
|
||||||
|
<CopyToClipboard text={`${target.ip}:${target.port}`} />
|
||||||
|
</div>
|
||||||
|
<span className={`text-xs capitalize ${target.healthStatus === 'healthy' ? 'text-green-600' : 'text-red-600'
|
||||||
|
}`}>
|
||||||
|
{target.healthStatus}
|
||||||
|
</span>
|
||||||
|
</DropdownMenuItem>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{unknownTargets.length > 0 && (
|
||||||
|
<>
|
||||||
|
{unknownTargets.map((target) => (
|
||||||
|
<DropdownMenuItem key={target.targetId} className="flex items-center justify-between gap-4">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<StatusIcon status="unknown" className="h-3 w-3" />
|
||||||
|
<CopyToClipboard text={`${target.ip}:${target.port}`} />
|
||||||
|
</div>
|
||||||
|
<span className="text-xs text-gray-500">
|
||||||
|
{!target.enabled ? 'Disabled' : 'Not monitored'}
|
||||||
|
</span>
|
||||||
|
</DropdownMenuItem>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const proxyColumns: ColumnDef<ResourceRow>[] = [
|
const proxyColumns: ColumnDef<ResourceRow>[] = [
|
||||||
{
|
{
|
||||||
accessorKey: "name",
|
accessorKey: "name",
|
||||||
@@ -403,8 +530,8 @@ export default function ResourcesTable({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "target",
|
id: "status",
|
||||||
accessorKey: "target",
|
accessorKey: "status",
|
||||||
header: ({ column }) => {
|
header: ({ column }) => {
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
@@ -413,52 +540,21 @@ export default function ResourcesTable({
|
|||||||
column.toggleSorting(column.getIsSorted() === "asc")
|
column.toggleSorting(column.getIsSorted() === "asc")
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{t("target")}
|
{t("status")}
|
||||||
<ArrowUpDown className="ml-2 h-4 w-4" />
|
<ArrowUpDown className="ml-2 h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const resourceRow = row.original as ResourceRow & {
|
const resourceRow = row.original;
|
||||||
targets?: { ip: string; port: number }[];
|
return <TargetStatusCell targets={resourceRow.targets} />;
|
||||||
};
|
|
||||||
|
|
||||||
const targets = resourceRow.targets ?? [];
|
|
||||||
|
|
||||||
if (targets.length === 0) {
|
|
||||||
return <span className="text-muted-foreground">-</span>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const count = targets.length;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<DropdownMenu>
|
|
||||||
<DropdownMenuTrigger asChild>
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="sm"
|
|
||||||
className="flex items-center"
|
|
||||||
>
|
|
||||||
<ChevronDown className="h-4 w-4 mr-1" />
|
|
||||||
{`${count} Configurations`}
|
|
||||||
</Button>
|
|
||||||
</DropdownMenuTrigger>
|
|
||||||
|
|
||||||
<DropdownMenuContent align="start" className="min-w-[200px]">
|
|
||||||
{targets.map((target, idx) => {
|
|
||||||
return (
|
|
||||||
<DropdownMenuItem key={idx} className="flex items-center gap-2">
|
|
||||||
<CopyToClipboard
|
|
||||||
text={`${target.ip}:${target.port}`}
|
|
||||||
isLink={false}
|
|
||||||
/>
|
|
||||||
</DropdownMenuItem>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</DropdownMenuContent>
|
|
||||||
</DropdownMenu>
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
sortingFn: (rowA, rowB) => {
|
||||||
|
const statusA = getOverallHealthStatus(rowA.original.targets);
|
||||||
|
const statusB = getOverallHealthStatus(rowB.original.targets);
|
||||||
|
const statusOrder = { online: 3, degraded: 2, offline: 1, unknown: 0 };
|
||||||
|
return statusOrder[statusA] - statusOrder[statusB];
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "domain",
|
accessorKey: "domain",
|
||||||
|
|||||||
Reference in New Issue
Block a user