diff --git a/frontend/src/modules/HostStatus.test.ts b/frontend/src/modules/HostStatus.test.ts index b7100a05d..8e816b8c8 100644 --- a/frontend/src/modules/HostStatus.test.ts +++ b/frontend/src/modules/HostStatus.test.ts @@ -7,6 +7,7 @@ import { isVariableHost, locationHostPart, sortByHostStatus, + sortByHostStatusWithHealth, } from "./HostStatus"; const row = (name: string, enabled: boolean, meta: HostMeta | null) => ({ name, original: { enabled, meta } }); @@ -41,6 +42,25 @@ describe("sorting the status column", () => { expect([...rows].sort(sortByHostStatus).map((r) => r.name)).toEqual(["error", "warning", "disabled", "online"]); }); + it("ranks a proxy host whose upstream is down as a warning, with the health of its id", () => { + const down = health([target({ locationIndex: null })]); + const rows = [ + { name: "online", original: { id: 1, enabled: true, meta: { nginxOnline: true } } }, + { name: "upstream down", original: { id: 2, enabled: true, meta: { nginxOnline: true } } }, + { name: "disabled", original: { id: 3, enabled: false, meta: null } }, + ]; + expect([...rows].sort(sortByHostStatusWithHealth({ 2: down })).map((r) => r.name)).toEqual([ + "upstream down", + "disabled", + "online", + ]); + expect([...rows].sort(sortByHostStatusWithHealth(undefined)).map((r) => r.name)).toEqual([ + "disabled", + "online", + "upstream down", + ]); + }); + it("ranks a disabled host as disabled whatever its stored status", () => { expect(hostStatusRank(false, { nginxOnline: false })).toBe(hostStatusRank(false, null)); }); diff --git a/frontend/src/modules/HostStatus.ts b/frontend/src/modules/HostStatus.ts index adeec409b..69594b811 100644 --- a/frontend/src/modules/HostStatus.ts +++ b/frontend/src/modules/HostStatus.ts @@ -285,16 +285,25 @@ export const getHostStatus = ( // The order of the status column, ascending: the hosts that need attention first const HOST_STATE_RANK: Record = { error: 0, warning: 1, disabled: 2, online: 3 }; -export const hostStatusRank = (enabled: boolean, meta?: HostMeta | null): number => - HOST_STATE_RANK[getHostStatus(enabled, meta).state]; +export const hostStatusRank = (enabled: boolean, meta?: HostMeta | null, health?: UpstreamHostHealth | null): number => + HOST_STATE_RANK[getHostStatus(enabled, meta, health).state]; interface HostRow { - original: { enabled: boolean; meta?: HostMeta | null }; + original: { id?: number; enabled: boolean; meta?: HostMeta | null }; } /** sortFn of a table's status column: sorts by the status it shows, not only enabled / disabled */ export const sortByHostStatus = (a: HostRow, b: HostRow): number => hostStatusRank(a.original.enabled, a.original.meta) - hostStatusRank(b.original.enabled, b.original.meta); +/** sortByHostStatus() with the upstream health of each host (by id), which the proxy hosts' status shows too */ +export const sortByHostStatusWithHealth = + (health?: Record | null) => + (a: HostRow, b: HostRow): number => { + const rank = (row: HostRow) => + hostStatusRank(row.original.enabled, row.original.meta, health?.[`${row.original.id}`]); + return rank(a) - rank(b); + }; + /** Whether a successful save still needs the user's attention */ export const hasSaveWarnings = (meta?: HostMeta | null) => meta?.nginxOnline === false || getProblemLocations(meta).length > 0; diff --git a/frontend/src/pages/Nginx/ProxyHosts/Table.tsx b/frontend/src/pages/Nginx/ProxyHosts/Table.tsx index 6fabba14a..a12bc9a1d 100644 --- a/frontend/src/pages/Nginx/ProxyHosts/Table.tsx +++ b/frontend/src/pages/Nginx/ProxyHosts/Table.tsx @@ -14,7 +14,7 @@ import { import { type Features, features } from "src/components/Table/features"; import { TableLayout } from "src/components/Table/TableLayout"; import { intl, T } from "src/locale"; -import { getProblemLocations, sortByHostStatus } from "src/modules/HostStatus"; +import { getProblemLocations, sortByHostStatusWithHealth } from "src/modules/HostStatus"; import { MANAGE, PROXY_HOSTS } from "src/modules/Permissions"; // "+2 locations ยท 1 problem" under the main destination @@ -127,7 +127,7 @@ export default function Table({ columnHelper.accessor((row: any) => row.enabled, { id: "enabled", header: intl.formatMessage({ id: "column.status" }), - sortFn: sortByHostStatus, + sortFn: sortByHostStatusWithHealth(health), cell: (info: any) => { const row: ProxyHost = info.row.original; return (