Skip to content

Commit e419083

Browse files
authored
app/vmui: sync tenant selectors from response headers (#865)
1 parent e167e9c commit e419083

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

app/vmui/packages/vmui/src/pages/QueryPage/hooks/useFetchLogs.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useTenant } from "../../../hooks/useTenant";
77
import { useSearchParams } from "react-router-dom";
88
import { useAppState } from "../../../state/common/StateContext";
99
import { mergeSearchParams } from "../../../utils/query-string";
10+
import { TenantType } from "../../../components/Configurators/GlobalSettings/TenantsConfiguration/Tenants";
1011

1112
interface FetchLogsParams {
1213
query?: string;
@@ -27,7 +28,7 @@ export type BeforeFetch = (body: Readonly<URLSearchParams>) => Promise<BeforeFet
2728
export const useFetchLogs = (defaultQuery?: string, defaultLimit?: number) => {
2829
const { serverUrl } = useAppState();
2930
const tenant = useTenant();
30-
const [searchParams] = useSearchParams();
31+
const [searchParams, setSearchParams] = useSearchParams();
3132

3233
const [logs, setLogs] = useState<Logs[]>([]);
3334
const [queryParams, setQueryParams] = useState<Record<string, string>>({});
@@ -72,6 +73,12 @@ export const useFetchLogs = (defaultQuery?: string, defaultLimit?: number) => {
7273
};
7374
};
7475

76+
const updateTenant = ({ accountId, projectId }: Partial<TenantType>) => {
77+
if (accountId) searchParams.set("accountID", accountId);
78+
if (projectId) searchParams.set("projectID", projectId);
79+
setSearchParams(searchParams);
80+
};
81+
7582
const fetchLogs = useCallback(async ({
7683
query = defaultQuery,
7784
limit = defaultLimit,
@@ -112,6 +119,16 @@ export const useFetchLogs = (defaultQuery?: string, defaultLimit?: number) => {
112119
return response;
113120
}
114121

122+
const uiAccountId = (options.headers as Record<string, string>)?.AccountID;
123+
const vlAccountId = response.headers.get("AccountID");
124+
const changedAccountId = vlAccountId && vlAccountId !== uiAccountId;
125+
if (changedAccountId) updateTenant({ accountId: vlAccountId });
126+
127+
const uiProjectId = (options.headers as Record<string, string>)?.ProjectID;
128+
const vlProjectId = response.headers.get("ProjectID");
129+
const changedProjectId = vlProjectId && vlProjectId !== uiProjectId;
130+
if (changedProjectId) updateTenant({ projectId: vlProjectId });
131+
115132
const duration = response.headers.get("vl-request-duration-seconds");
116133
setDurationMs(duration ? Number(duration) * 1000 : undefined);
117134

docs/victorialogs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ according to the follosing docs:
2727
* FEATURE: [OpenTelemetry data ingestion](https://docs.victoriametrics.com/victorialogs/data-ingestion/opentelemetry/): parse `EventName` in the `LogRecord` and store it into [log stream fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#stream-fields). See [#826](https://github.com/VictoriaMetrics/VictoriaLogs/issues/826).
2828
* FEATURE: [HTTP querying API](https://docs.victoriametrics.com/victorialogs/querying/#http-api): automatically convert the results of [`row_any()`](https://docs.victoriametrics.com/victorialogs/logsql/#row_any-stats), [`row_min()`](https://docs.victoriametrics.com/victorialogs/logsql/#row_min-stats) and [`row_max()`](https://docs.victoriametrics.com/victorialogs/logsql/#row_max-stats) stats functions to labels in [`/select/logsql/stats_query`](https://docs.victoriametrics.com/victorialogs/querying/#querying-log-stats) and [`/select/logsql/stats_query_range`](https://docs.victoriametrics.com/victorialogs/querying/#querying-log-range-stats) functions. This allows obtaining a sample of log message in alerting rules with the following query: `... | stats count() as hits, row_any(_msg) as msg_sample`. See [#81](https://github.com/VictoriaMetrics/VictoriaLogs/issues/81).
2929
* FEATURE: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): show VictoriaLogs version in the vmui's footer. See [#116](https://github.com/VictoriaMetrics/VictoriaLogs/issues/116).
30+
* FEATURE: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): sync selected tenant with AccountID/ProjectID returned in response headers. See [#656](https://github.com/VictoriaMetrics/VictoriaLogs/issues/656).
3031
* FEATURE: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): add hotkeys to toggle series in the hit chart.
3132

3233
* BUGFIX: [web UI](https://docs.victoriametrics.com/victorialogs/querying/#web-ui): fix mobile layout styles.

0 commit comments

Comments
 (0)