Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add advise index view (#5843)

boojack2d7d6faa156a
diff --git a/frontend/src/components/SlowQuery/Panel/AdviseIndex.vue b/frontend/src/components/SlowQuery/Panel/AdviseIndex.vue
new file mode 100644
index 0000000000..a6f643f614
--- /dev/null
+++ b/frontend/src/components/SlowQuery/Panel/AdviseIndex.vue
@@ -0,0 +1,101 @@
+<template>
+ <div
+ class="w-full min-w-[128px] flex flex-row justify-start items-start gap-4"
+ >
+ <div class="w-1/4 flex flex-col justify-start items-start gap-2">
+ <span class="font-medium capitalize whitespace-nowrap">{{
+ $t("slow-query.advise-index.current-index")
+ }}</span>
+ <span class="font-mono">{{ state.currentIndex }}</span>
+ </div>
+ <div class="w-3/4 flex flex-col justify-start items-start gap-2">
+ <div>
+ <span class="font-medium capitalize whitespace-nowrap">{{
+ $t("slow-query.advise-index.suggestion")
+ }}</span>
+ <button class="ml-2 normal-link underline" @click="handleCreateIndex">
+ {{ $t("slow-query.advise-index.create-index") }}
+ </button>
+ </div>
+ <span class="w-full font-mono">{{ state.suggestion }}</span>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+import { ComposedSlowQueryLog } from "@/types";
+import { reactive } from "vue";
+import { watch } from "vue";
+import dayjs from "dayjs";
+import { useRouter } from "vue-router";
+
+const props = defineProps<{
+ slowQueryLog: ComposedSlowQueryLog;
+}>();
+
+interface LocalState {
+ currentIndex: string;
+ suggestion: string;
+ createIndexStatement: string;
+}
+
+const router = useRouter();
+const state = reactive<LocalState>({
+ currentIndex: "",
+ suggestion: "",
+ createIndexStatement: "",
+});
+const log = computed(() => props.slowQueryLog.log);
+const database = computed(() => props.slowQueryLog.database);
+const sqlFingerprint = computed(
+ () => log.value.statistics?.sqlFingerprint || ""
+);
+
+const handleCreateIndex = () => {
+ const query: Record<string, any> = {
+ template: "bb.issue.database.schema.update",
+ project: database.value.projectId,
+ mode: "normal",
+ ghost: undefined,
+ };
+ query.databaseList = database.value.id;
+ query.sql = state.createIndexStatement;
+ query.name = generateIssueName();
+
+ const routeInfo = {
+ name: "workspace.issue.detail",
+ params: {
+ issueSlug: "new",
+ },
+ query,
+ };
+ router.push(routeInfo);
+};
+
+const generateIssueName = () => {
+ const issueNameParts: string[] = [];
+ issueNameParts.push(`[${database.value.name}]`);
+ issueNameParts.push(`Create index`);
+ const datetime = dayjs().format("@MM-DD HH:mm");
+ const tz = "UTC" + dayjs().format("ZZ");
+ issueNameParts.push(`${datetime} ${tz}`);
+ return issueNameParts.join(" ");
+};
+
+watch(
+ () => props.slowQueryLog,
+ async () => {
+ // TODO(junyi): Do data fetching with database and sqlFingerprint.
+ // Prevent eslint error.
+ console.log(sqlFingerprint);
+
+ state.currentIndex = "test index";
+ state.suggestion = "Your suggestion";
+ state.createIndexStatement = "CREATE INDEX balabala;";
+ },
+ {
+ immediate: true,
+ }
+);
+</script>
diff --git a/frontend/src/components/SlowQuery/Panel/DetailPanel.vue b/frontend/src/components/SlowQuery/Panel/DetailPanel.vue
index 136dde826f..767bf659a8 100644
--- a/frontend/src/components/SlowQuery/Panel/DetailPanel.vue
+++ b/frontend/src/components/SlowQuery/Panel/DetailPanel.vue
@@ -61,6 +61,11 @@
</div>
</div>
</div>
+ <!-- TODO(junyi): remove the isDev guard before releasing -->
+ <AdviseIndex
+ v-if="isDev() && slowQueryLog"
+ :slow-query-log="slowQueryLog"
+ />
<div class="flex-1 overflow-auto border">
<BBGrid
:column-list="columns"
@@ -124,6 +129,7 @@ import { NButton, NDrawer, NDrawerContent } from "naive-ui";
import { type BBGridColumn, type BBGridRow, BBGrid } from "@/bbkit";
import type { ComposedSlowQueryLog } from "@/types";
import type { SlowQueryDetails } from "@/types/proto/v1/database_service";
+import { isDev } from "@/utils";
import {
DatabaseName,
InstanceName,
@@ -131,6 +137,7 @@ import {
ProjectName,
} from "@/components/v2";
import HighlightCodeBlock from "@/components/HighlightCodeBlock";
+import AdviseIndex from "./AdviseIndex.vue";
export type SlowQueryDetailsRow = BBGridRow<SlowQueryDetails>;
diff --git a/frontend/src/components/v2/Model/DatabaseName.vue b/frontend/src/components/v2/Model/DatabaseName.vue
index 34235ff59c..94a383e862 100644
--- a/frontend/src/components/v2/Model/DatabaseName.vue
+++ b/frontend/src/components/v2/Model/DatabaseName.vue
@@ -3,6 +3,7 @@
:is="link ? 'router-link' : tag"
v-bind="bindings"
class="inline-flex items-center gap-x-1"
+ :class="link && 'normal-link'"
>
<span v-if="prefix" class="ml-1 text-gray-400">{{ prefix }}</span>
<span>{{ database.name }}</span>
diff --git a/frontend/src/components/v2/Model/EnvironmentName.vue b/frontend/src/components/v2/Model/EnvironmentName.vue
index fbf9840edc..c408bddf66 100644
--- a/frontend/src/components/v2/Model/EnvironmentName.vue
+++ b/frontend/src/components/v2/Model/EnvironmentName.vue
@@ -3,6 +3,7 @@
:is="link ? 'router-link' : tag"
v-bind="bindings"
class="inline-flex items-center gap-x-1"
+ :class="link && 'normal-link'"
>
<span>{{ environmentName(environment) }}</span>
<ProductionEnvironmentIcon
diff --git a/frontend/src/components/v2/Model/InstanceName.vue b/frontend/src/components/v2/Model/InstanceName.vue
index b4a3a13b52..6c0ef57097 100644
--- a/frontend/src/components/v2/Model/InstanceName.vue
+++ b/frontend/src/components/v2/Model/InstanceName.vue
@@ -4,6 +4,7 @@
v-bind="bindings"
:to="link && `/instance/${instance.id}`"
class="inline-flex items-center gap-x-1"
+ :class="link && 'normal-link'"
@click.stop=""
>
<InstanceEngineIcon
diff --git a/frontend/src/components/v2/Model/ProjectName.vue b/frontend/src/components/v2/Model/ProjectName.vue
index f22edba6fb..50ddfa56c2 100644
--- a/frontend/src/components/v2/Model/ProjectName.vue
+++ b/frontend/src/components/v2/Model/ProjectName.vue
@@ -3,6 +3,7 @@
:is="link ? 'router-link' : tag"
v-bind="bindings"
class="inline-flex items-center gap-x-1"
+ :class="link && 'normal-link'"
>
<span>{{ projectName(project) }}</span>
</component>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 830d6b2a7b..fdf723be36 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2388,6 +2388,11 @@
"filter": {
"from-date": "From date",
"to-date": "To date"
+ },
+ "advise-index": {
+ "current-index": "Current Index",
+ "suggestion": "Suggestion",
+ "create-index": "Create Index"
}
},
"principal": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 42dd38afc2..a2e178ce77 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2383,6 +2383,11 @@
"filter": {
"from-date": "开始日期",
"to-date": "结束日期"
+ },
+ "advise-index": {
+ "current-index": "当前索引",
+ "suggestion": "建议",
+ "create-index": "创建索引"
}
},
"principal": {