Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: fix link for index from insights #92953

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/ui/workspaces/cluster-ui/src/api/schemaInsightsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type ClusterIndexUsageStatistic = {
database_id: number;
database_name: string;
unused_threshold: string;
schema_name: string;
};

type CreateIndexRecommendationsResponse = {
Expand Down Expand Up @@ -68,12 +69,13 @@ function clusterIndexUsageStatsToSchemaInsight(
results[key] = {
type: "DropIndex",
database: row.database_name,
query: `DROP INDEX ${row.table_name}@${row.index_name};`,
query: `DROP INDEX ${row.schema_name}.${row.table_name}@${row.index_name};`,
indexDetails: {
table: row.table_name,
indexID: row.index_id,
indexName: row.index_name,
lastUsed: result.reason,
schema: row.schema_name,
},
};
}
Expand Down Expand Up @@ -136,6 +138,7 @@ const dropUnusedIndexQuery: SchemaInsightQuery<ClusterIndexUsageStatistic> = {
t.name as table_name,
t.parent_id as database_id,
t.database_name,
t.schema_name,
(SELECT value FROM crdb_internal.cluster_settings WHERE variable = 'sql.index_recommendation.drop_unused_duration') AS unused_threshold
FROM "".crdb_internal.index_usage_statistics AS us
JOIN "".crdb_internal.table_indexes as ti ON us.index_id = ti.index_id AND us.table_id = ti.descriptor_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("recommendDropUnusedIndex", () => {
table_name: "test_table",
database_id: 1,
database_name: "test_db",
schema_name: "public",
unused_threshold: "10h0m0s",
};
it("should not recommend index to be dropped", () => {
Expand All @@ -49,6 +50,7 @@ describe("recommendDropUnusedIndex", () => {
table_name: "test_table",
database_id: 1,
database_name: "test_db",
schema_name: "public",
unused_threshold: "10h0m0s",
};
it("should recommend index to be dropped with the reason that the index is never used", () => {
Expand All @@ -68,6 +70,7 @@ describe("recommendDropUnusedIndex", () => {
table_name: "test_table",
database_id: 1,
database_name: "test_db",
schema_name: "public",
unused_threshold: "0h30m0s",
};
it("should recommend index to be dropped with the reason that it has exceeded the configured index unuse duration", () => {
Expand All @@ -92,6 +95,7 @@ describe("recommendDropUnusedIndex", () => {
table_name: "test_table",
database_id: 1,
database_name: "test_db",
schema_name: "public",
unused_threshold: "10h0m0s",
};
it("should not recommend index to be dropped", () => {
Expand All @@ -113,6 +117,7 @@ describe("recommendDropUnusedIndex", () => {
table_name: "test_table",
database_id: 1,
database_name: "test_db",
schema_name: "public",
unused_threshold: "0h30m0s",
};
it("should recommend index to be dropped with the reason that it has exceeded the configured index unuse duration", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const SchemaInsightsPropsFixture: SchemaInsightsViewProps = {
table: "table_name",
indexID: 1,
indexName: "index_name",
schema: "public",
lastUsed:
"This index has not been used and can be removed for better write performance.",
},
Expand All @@ -30,6 +31,7 @@ export const SchemaInsightsPropsFixture: SchemaInsightsViewProps = {
table: "table_name2",
indexID: 2,
indexName: "index_name2",
schema: "public",
lastUsed:
"This index has not been used in over 9 days, 5 hours, and 3 minutes and can be removed for better write performance.",
},
Expand Down
1 change: 1 addition & 0 deletions pkg/ui/workspaces/cluster-ui/src/insights/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export interface InsightRecommendation {

export interface indexDetails {
table: string;
schema: string;
indexID: number;
indexName: string;
lastUsed?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function descriptionCell(
insightRec: InsightRecommendation,
showQuery: boolean,
disableStmtLink: boolean,
isCockroachCloud: boolean,
): React.ReactElement {
const stmtLink =
showQuery || isIndexRec(insightRec) ? (
Expand All @@ -125,6 +126,11 @@ function descriptionCell(
{"."}
</>
);

const indexLink = isCockroachCloud
? `databases/${insightRec.database}/${insightRec.indexDetails?.schema}/${insightRec.indexDetails?.table}/${insightRec.indexDetails?.indexName}`
: `database/${insightRec.database}/table/${insightRec.indexDetails?.table}/index/${insightRec.indexDetails?.indexName}`;

switch (insightRec.type) {
case "CreateIndex":
case "ReplaceIndex":
Expand All @@ -143,10 +149,7 @@ function descriptionCell(
<>
<div className={cx("description-item")}>
<span className={cx("label-bold")}>Index: </span>{" "}
<Link
to={`database/${insightRec.database}/table/${insightRec.indexDetails.table}/index/${insightRec.indexDetails.indexName}`}
className={cx("table-link")}
>
<Link to={indexLink} className={cx("table-link")}>
{insightRec.indexDetails.indexName}
</Link>
</div>
Expand Down Expand Up @@ -312,7 +315,7 @@ const isIndexRec = (rec: InsightRecommendation) => {
};

export function makeInsightsColumns(
hideAction: boolean,
isCockroachCloud: boolean,
showQuery?: boolean,
disableStmtLink?: boolean,
): ColumnDescriptor<InsightRecommendation>[] {
Expand All @@ -327,13 +330,13 @@ export function makeInsightsColumns(
name: "details",
title: insightsTableTitles.details(),
cell: (item: InsightRecommendation) =>
descriptionCell(item, showQuery, disableStmtLink),
descriptionCell(item, showQuery, disableStmtLink, isCockroachCloud),
sort: (item: InsightRecommendation) => item.type,
},
{
name: "action",
title: insightsTableTitles.actions(),
cell: (item: InsightRecommendation) => actionCell(item, hideAction),
cell: (item: InsightRecommendation) => actionCell(item, isCockroachCloud),
},
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe("SchemaInsights sagas", () => {
table: "test_table",
indexName: "test_idx",
indexID: 1,
schema: "public",
lastUsed: "2022-08-22T22:30:02Z",
},
},
Expand Down