-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster-ui: add getTableMetadataApi and connect tables page
This commit connects the v2 db details - tables page to the new `api/v2/table_metadata` api route. The following components are now connected to the api: - listing tables for a db - sorting by column - searching by table name - filtering by node id Epic: CRDB-37558 Fixes: #131122 Release note: None
- Loading branch information
Showing
9 changed files
with
393 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
pkg/ui/workspaces/cluster-ui/src/api/databases/getTableMetadataApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
import useSWR from "swr"; | ||
|
||
import { StoreID } from "../../types/clusterTypes"; | ||
import { fetchDataJSON } from "../fetchData"; | ||
import { | ||
APIV2ResponseWithPaginationState, | ||
SimplePaginationState, | ||
} from "../types"; | ||
|
||
const TABLE_METADATA_API_PATH = "api/v2/table_metadata/"; | ||
|
||
export enum TableSortOption { | ||
NAME = "name", | ||
REPLICATION_SIZE = "replicationSize", | ||
RANGES = "ranges", | ||
LIVE_DATA = "liveData", | ||
COLUMNS = "columns", | ||
INDEXES = "indexes", | ||
LAST_UPDATED = "lastUpdated", | ||
} | ||
|
||
export type TableMetadata = { | ||
db_id: number; | ||
db_name: string; | ||
table_id: number; | ||
schema_name: string; | ||
table_name: string; | ||
replication_size_bytes: number; | ||
range_count: number; | ||
column_count: number; | ||
index_count: number; | ||
percent_live_data: number; | ||
total_live_data_bytes: number; | ||
total_data_bytes: number; | ||
store_ids: number[]; | ||
last_updated: string; | ||
}; | ||
|
||
type TableMetadataResponse = APIV2ResponseWithPaginationState<TableMetadata[]>; | ||
|
||
export type TableMetadataRequest = { | ||
dbId?: number; | ||
sortBy?: string; | ||
sortOrder?: "asc" | "desc"; | ||
storeIds?: StoreID[]; | ||
pagination: SimplePaginationState; | ||
name?: string; | ||
}; | ||
|
||
export async function getTableMetadata( | ||
req: TableMetadataRequest, | ||
): Promise<TableMetadataResponse> { | ||
const urlParams = new URLSearchParams(); | ||
if (req.dbId) { | ||
urlParams.append("dbId", req.dbId.toString()); | ||
} | ||
if (req.sortBy) { | ||
urlParams.append("sortBy", req.sortBy); | ||
} | ||
if (req.sortOrder) { | ||
urlParams.append("sortOrder", req.sortOrder); | ||
} | ||
if (req.pagination.pageSize) { | ||
urlParams.append("pageSize", req.pagination.pageSize.toString()); | ||
} | ||
if (req.pagination.pageNum) { | ||
urlParams.append("pageNum", req.pagination.pageNum.toString()); | ||
} | ||
if (req.storeIds) { | ||
req.storeIds.forEach(storeID => { | ||
urlParams.append("storeId", storeID.toString()); | ||
}); | ||
} | ||
if (req.name) { | ||
urlParams.append("name", req.name); | ||
} | ||
return fetchDataJSON(TABLE_METADATA_API_PATH + "?" + urlParams.toString()); | ||
} | ||
|
||
const createKey = (req: TableMetadataRequest) => { | ||
const { dbId, sortBy, sortOrder, pagination, storeIds, name } = req; | ||
return [ | ||
"tableMetadata", | ||
dbId, | ||
sortBy, | ||
sortOrder, | ||
pagination.pageSize, | ||
pagination.pageNum, | ||
storeIds, | ||
name, | ||
].join("|"); | ||
}; | ||
|
||
export const useTableMetadata = (req: TableMetadataRequest) => { | ||
const key = createKey(req); | ||
const { data, error, isLoading } = useSWR<TableMetadataResponse>(key, () => | ||
getTableMetadata(req), | ||
); | ||
|
||
return { data, error, isLoading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.