mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-22 20:53:52 +09:00
fix(dashboard): keep placeholder results within one observability scope
The provider keeps a query's previous result while its key changes, so switching tenants showed the first tenant's log lines and pod names under the second tenant's heading until its request returned. Every observability query now keeps the previous result only while the proxy root is the same, so a new filter or range still shows the old lines, but a new tenant or application starts empty. Tests render with the provider's own query defaults. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
311cb1c90b
commit
9c111b3a95
5 files changed
+78
-5
No files matched your search
+47
-2
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { screen, waitFor, within } from "@testing-library/react"
|
||||
import { render, screen, waitFor, within } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
import { K8sClient, K8sProvider } from "@cozystack/k8s-client"
|
||||
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
|
||||
import { LogsView } from "./LogsView.tsx"
|
||||
|
||||
@@ -30,6 +30,51 @@ function defaultHandler(endpoint: string, params?: URLSearchParams): Response |
|
||||
return new Promise(() => {})
|
||||
}
|
||||
|
||||
// The provider's own QueryClient keeps the previous result while a key changes; it is
|
||||
// shared across tests, so each test below uses namespaces no other test reads.
|
||||
function renderWithProductionDefaults(client: K8sClient, namespace: string) {
|
||||
const view = (ns: string) => (
|
||||
<K8sProvider client={client}>
|
||||
<LogsView scope={{ namespace: ns }} />
|
||||
</K8sProvider>
|
||||
)
|
||||
const result = render(view(namespace))
|
||||
return { switchTo: (ns: string) => result.rerender(view(ns)) }
|
||||
}
|
||||
|
||||
describe("LogsView with the production query defaults", () => {
|
||||
it("drops the previous tenant's lines and pods while the next tenant loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.includes("/tenant-switch-b/") ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
const { switchTo } = renderWithProductionDefaults(client, "tenant-switch-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await screen.findByRole("option", { name: "db-1" })
|
||||
await userEvent.keyboard("{Escape}")
|
||||
|
||||
switchTo("tenant-switch-b")
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Loading log lines…")).toBeInTheDocument()
|
||||
expect(screen.queryByText("ready to accept connections")).toBeNull()
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
expect((await screen.findAllByRole("option")).map((o) => o.textContent)).toEqual(["All"])
|
||||
})
|
||||
|
||||
it("keeps the current lines while a new filter in the same tenant loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && params.get("query") === "slow" ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-filter-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "slow{Enter}")
|
||||
|
||||
expect(screen.getByText("ready to accept connections")).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe("LogsView", () => {
|
||||
it("shows recent lines from the application root", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
|
||||
+3
@@ -14,6 +14,7 @@ import {
|
||||
import {
|
||||
fieldValues,
|
||||
isScopeWide,
|
||||
keepWithinScope,
|
||||
logsRoot,
|
||||
queryLogs,
|
||||
type ObservabilityScope,
|
||||
@@ -101,6 +102,7 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) {
|
||||
query: buildLogsQuery({ filter: "", pod: narrowTo }),
|
||||
...windowFor(range),
|
||||
}, signal),
|
||||
placeholderData: keepWithinScope(root),
|
||||
retry: false,
|
||||
})
|
||||
const pods = useQuery(pickerQuery(POD_FIELD))
|
||||
@@ -110,6 +112,7 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) {
|
||||
queryKey: ["observability", "logs", root, query, range],
|
||||
queryFn: ({ signal }) => queryLogs(client, root, { query, limit: HISTORY_LIMIT, ...windowFor(range) }, signal),
|
||||
enabled: !follow,
|
||||
placeholderData: keepWithinScope(root),
|
||||
retry: false,
|
||||
})
|
||||
const tail = useLogTail(root, query, followRun, { cap: FOLLOW_CAP })
|
||||
|
||||
+18
-2
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { screen, waitFor, within } from "@testing-library/react"
|
||||
import { render, screen, waitFor, within } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
import { K8sClient, K8sProvider } from "@cozystack/k8s-client"
|
||||
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
|
||||
import type { ChartData } from "../../lib/observability/metrics.ts"
|
||||
|
||||
@@ -138,6 +138,22 @@ describe("MetricsView", () => {
|
||||
}
|
||||
})
|
||||
|
||||
it("drops the previous application's charts while the next one loads, with the production query defaults", async () => {
|
||||
const { client } = clientWith((url) => (url.pathname.includes("/switch-b/") ? new Promise<Response>(() => {}) : matrix("db-0")))
|
||||
const view = (name: string) => (
|
||||
<K8sProvider client={client}>
|
||||
<MetricsView scope={{ ...scope, name }} />
|
||||
</K8sProvider>
|
||||
)
|
||||
const { rerender } = render(view("switch-a"))
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")).toHaveLength(3))
|
||||
|
||||
rerender(view("switch-b"))
|
||||
|
||||
expect(screen.queryAllByTestId("chart")).toHaveLength(0)
|
||||
expect(screen.getAllByText("Loading…")).toHaveLength(3)
|
||||
})
|
||||
|
||||
it("holds each chart's place while it loads", () => {
|
||||
const { client } = clientWith(() => new Promise<Response>(() => {}))
|
||||
|
||||
|
||||
+2
-1
@@ -9,7 +9,7 @@ import {
|
||||
toChartData,
|
||||
type RangeKey,
|
||||
} from "../../lib/observability/metrics.ts"
|
||||
import { isScopeWide, metricsRoot, queryRange, type ObservabilityScope } from "../../lib/observability/proxy.ts"
|
||||
import { isScopeWide, keepWithinScope, metricsRoot, queryRange, type ObservabilityScope } from "../../lib/observability/proxy.ts"
|
||||
import { ProxyNotice } from "./ProxyNotice.tsx"
|
||||
import { TimeSeriesChart } from "./TimeSeriesChart.tsx"
|
||||
import { RangePicker } from "./RangePicker.tsx"
|
||||
@@ -42,6 +42,7 @@ export function MetricsView({ scope }: { scope: ObservabilityScope }) {
|
||||
)
|
||||
return toChartData(series)
|
||||
},
|
||||
placeholderData: keepWithinScope(root),
|
||||
retry: false,
|
||||
refetchInterval: REFRESH_MS,
|
||||
})),
|
||||
|
||||
@@ -20,6 +20,14 @@ export function logsRoot(scope: ObservabilityScope): string {
|
||||
return app ? `${app}/log/` : `/apis/cozystack.io/v1alpha1/namespaces/${scope.namespace}/logs/`
|
||||
}
|
||||
|
||||
// The provider keeps a query's previous result while its key changes; across
|
||||
// scopes that would show one tenant's or application's data under another.
|
||||
// Every observability query key carries its root at index 2.
|
||||
export function keepWithinScope(root: string) {
|
||||
return <T,>(previous: T | undefined, previousQuery?: { queryKey: readonly unknown[] }): T | undefined =>
|
||||
previousQuery?.queryKey[2] === root ? previous : undefined
|
||||
}
|
||||
|
||||
export type ProxyErrorKind =
|
||||
| "bad-request"
|
||||
| "unauthorized"
|
||||
|
||||
Reference in new issue
Block a user