Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: query history ui (#406)
STEVEN701d1a769229
diff --git a/frontend/package.json b/frontend/package.jsonindex 02c1d0270a..ab8856bbe4 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -16,6 +16,7 @@"@bytebase/vue-kbar": "^0.1.5","@soerenmartius/vue3-clipboard": "^0.1.2","@tailwindcss/forms": "^0.2.1",+ "@tailwindcss/line-clamp": "^0.3.1","@tailwindcss/typography": "^0.4.0","@vueuse/core": "^7.0.1","autoprefixer": "^10.2.6",diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vueindex 99e35b9777..1f8dc9dfe2 100644--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue@@ -13,11 +13,22 @@ import {defineProps,defineEmits,onUnmounted,+ watch,} from "vue";import type { editor as Editor } from "monaco-editor";import { useMonaco } from "./useMonaco";-import { SqlDialect } from "../../types";+import {+ EditorSelectorGetters,+ SqlDialect,+ SqlEditorActions,+ SqlEditorState,+} from "../../types";+import {+ useNamespacedActions,+ useNamespacedGetters,+ useNamespacedState,+} from "vuex-composition-helpers";const props = defineProps({value: {@@ -41,12 +52,25 @@ const editorRef = ref();const sqlCode = toRef(props, "value");const language = toRef(props, "language");+const { shouldSetContent } = useNamespacedState<SqlEditorState>("sqlEditor", [+ "shouldSetContent",+]);+const { currentTab } = useNamespacedGetters<EditorSelectorGetters>(+ "editorSelector",+ ["currentTab"]+);+const { setShouldSetContent } = useNamespacedActions<SqlEditorActions>(+ "sqlEditor",+ ["setShouldSetContent"]+);+let editorInstance: Editor.IStandaloneCodeEditor;const {monaco,setPositionAtEndOfLine,formatContent,+ setContent,completionItemProvider,} = await useMonaco(language.value);@@ -129,4 +153,14 @@ onUnmounted(() => {completionItemProvider.dispose();editorInstance.dispose();});++watch(+ () => shouldSetContent.value,+ () => {+ if (shouldSetContent.value) {+ setShouldSetContent(false);+ setContent(editorInstance, currentTab.value.queryStatement);+ }+ }+);</script>diff --git a/frontend/src/components/ProvideSqlEditorContext.vue b/frontend/src/components/ProvideSqlEditorContext.vueindex cbe6ebe3a6..6a19ba8c13 100644--- a/frontend/src/components/ProvideSqlEditorContext.vue+++ b/frontend/src/components/ProvideSqlEditorContext.vue@@ -115,6 +115,7 @@ const prepareSqlEdtiorContext = async () => {store.dispatch("sqlEditor/setConnectionContext", {isLoadingTree: false,});+ store.dispatch("sqlEditor/fetchQueryHistory");};onMounted(async () => {diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.ymlindex bdd4bed307..21f41e4296 100644--- a/frontend/src/locales/en.yml+++ b/frontend/src/locales/en.yml@@ -163,6 +163,9 @@ common:or: orexport: Exporttips: Tips+ delete: Delete+ history: History+ loading: Loadingerror-page:go-back-home: Go back homeanomaly-center: Anomaly Center@@ -1089,6 +1092,10 @@ sql-editor:goto-alter-schema-hint: Please select a database connection from the top of the editorcan-not-execute-query: Can not execute query when loading data.rows: row | rows+ no-history-found: No history found+ search-history: Search History+ hint-tips:+ confirm-to-delete-this-history: Confirm to delete this history?label:empty-label-value: <Empty Value>no-label: No labelsdiff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.ymlindex 880649073c..6a3335ff04 100644--- a/frontend/src/locales/zh-CN.yml+++ b/frontend/src/locales/zh-CN.yml@@ -163,6 +163,9 @@ common:or: 或者export: 导出tips: 提示信息+ delete: 删除+ history: 历史记录+ loading: 加载中error-page:go-back-home: 回到主页anomaly-center: 异常中心@@ -956,6 +959,10 @@ sql-editor:goto-alter-schema-hint: 请从编辑器顶部选择一个数据库连接can-not-execute-query: 加载数据时无法执行查询rows: 条记录+ no-history-found: 无历史记录+ search-history: 搜索历史记录+ hint-tips:+ confirm-to-delete-this-history: 确定删除这条记录?label:empty-label-value: <空值>no-label: 没有标签diff --git a/frontend/src/store/modules/activity.ts b/frontend/src/store/modules/activity.tsindex 48966f9cec..01826c2866 100644--- a/frontend/src/store/modules/activity.ts+++ b/frontend/src/store/modules/activity.ts@@ -101,6 +101,16 @@ const actions = {return activityList;},+ async fetchActivityListForQueryHistory({ rootGetters }: any, userId: number) {+ const queryList = ["type=bb.sql-editor.query", `user=${userId}`];+ const data = (await axios.get(`/api/activity?${queryList.join("&")}`)).data;+ const activityList = data.data.map((activity: ResourceObject) => {+ return convert(activity, data.included, rootGetters);+ });++ return activityList;+ },+async createActivity({ dispatch, rootGetters }: any,newActivity: ActivityCreate@@ -162,6 +172,10 @@ const actions = {dispatch("fetchActivityListForIssue", activity.containerId);}},++ async deleteActivityById(_: any, id: number) {+ await axios.delete(`/api/activity/${id}`);+ },};const mutations = {diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.tsindex 1b53bfa2ad..6293e4c900 100644--- a/frontend/src/store/modules/sqlEditor.ts+++ b/frontend/src/store/modules/sqlEditor.ts@@ -8,9 +8,11 @@ import {Database,DatabaseId,ProjectId,+ QueryHistory,} from "../../types";import * as types from "../mutation-types";import { makeActions } from "../actions";+import dayjs from "dayjs";const state: () => SqlEditorState = () => ({connectionTree: [],@@ -26,6 +28,9 @@ const state: () => SqlEditorState = () => ({selectedDatabaseId: 0,selectedTableName: "",},+ shouldSetContent: false,+ queryHistoryList: [],+ isFetchingQueryHistory: false,isExecuting: false,isShowExecutingHint: false,});@@ -123,6 +128,21 @@ const mutations = {) {Object.assign(state.connectionContext, payload);},+ [types.SET_SHOULD_SET_CONTENT](state: SqlEditorState, payload: boolean) {+ state.shouldSetContent = payload;+ },+ [types.SET_QUERY_HISTORY_LIST](+ state: SqlEditorState,+ payload: QueryHistory[]+ ) {+ state.queryHistoryList = payload;+ },+ [types.SET_IS_FETCHING_QUERY_HISTORY](+ state: SqlEditorState,+ payload: boolean+ ) {+ state.isFetchingQueryHistory = payload;+ },[types.SET_IS_EXECUTING](state: SqlEditorState, payload: boolean) {state.isExecuting = payload;},@@ -132,6 +152,9 @@ type SqlEditorActionsMap = {setSqlEditorState: typeof mutations.SET_SQL_EDITOR_STATE;setConnectionTree: typeof mutations.SET_CONNECTION_TREE;setConnectionContext: typeof mutations.SET_CONNECTION_CONTEXT;+ setShouldSetContent: typeof mutations.SET_SHOULD_SET_CONTENT;+ setQueryHistoryList: typeof mutations.SET_QUERY_HISTORY_LIST;+ setIsFetchingQueryHistory: typeof mutations.SET_IS_FETCHING_QUERY_HISTORY;setIsExecuting: typeof mutations.SET_IS_EXECUTING;};@@ -140,6 +163,9 @@ const actions = {setSqlEditorState: types.SET_SQL_EDITOR_STATE,setConnectionTree: types.SET_CONNECTION_TREE,setConnectionContext: types.SET_CONNECTION_CONTEXT,+ setShouldSetContent: types.SET_SHOULD_SET_CONTENT,+ setQueryHistory: types.SET_QUERY_HISTORY_LIST,+ setIsFetchingQueryHistory: types.SET_IS_FETCHING_QUERY_HISTORY,setIsExecuting: types.SET_IS_EXECUTING,}),async executeQuery(@@ -157,6 +183,7 @@ const actions = {},{ root: true });+dispatch("editorSelector/updateActiveTab",{@@ -164,6 +191,7 @@ const actions = {},{ root: true });+ dispatch("sqlEditor/fetchQueryHistory", {}, { root: true });return res;},async fetchConnectionByInstanceIdAndDatabaseId(@@ -188,6 +216,47 @@ const actions = {databaseName: databaseInfo.name,});},+ async fetchQueryHistory({ commit, dispatch, rootGetters }: any) {+ commit(types.SET_IS_FETCHING_QUERY_HISTORY, true);+ const currentUser = rootGetters["auth/currentUser"]();+ const activityList = await dispatch(+ "activity/fetchActivityListForQueryHistory",+ currentUser.id,+ {+ root: true,+ }+ );+ const queryHistoryList: QueryHistory[] = activityList.map(+ (history: any) => {+ return {+ id: history.id,+ creator: history.creator,+ createdTs: history.createdTs,+ updatedTs: history.updatedTs,+ statement: history.payload.statement,+ durationNs: history.payload.durationNs,+ instanceName: history.payload.instanceName,+ databaseName: history.payload.databaseName,+ error: history.payload.error,+ createdAt: dayjs(history.createdTs * 1000).format(+ "YYYY-MM-DD HH:mm:ss"+ ),+ };+ }+ );++ commit(+ types.SET_QUERY_HISTORY_LIST,+ queryHistoryList.sort((a, b) => b.createdTs - a.createdTs)+ );+ commit(types.SET_IS_FETCHING_QUERY_HISTORY, false);+ },+ async deleteQueryHistory({ dispatch }: any, id: number) {+ await dispatch("activity/deleteActivityById", id, {+ root: true,+ });+ dispatch("sqlEditor/fetchQueryHistory", {}, { root: true });+ },};export default {diff --git a/frontend/src/store/mutation-types.ts b/frontend/src/store/mutation-types.tsindex d0da0f1e50..4ef2826259 100644--- a/frontend/src/store/mutation-types.ts+++ b/frontend/src/store/mutation-types.ts@@ -2,7 +2,10 @@export const SET_SQL_EDITOR_STATE = "SET_SQL_EDITOR_STATE";export const SET_CONNECTION_TREE = "SET_CONNECTION_TREE";export const SET_CONNECTION_CONTEXT = "SET_CONNECTION_CONTEXT";+export const SET_SHOULD_SET_CONTENT = "SET_SHOULD_SET_CONTENT";export const SET_IS_EXECUTING = "SET_IS_EXECUTING";+export const SET_QUERY_HISTORY_LIST = "SET_QUERY_HISTORY_LIST";+export const SET_IS_FETCHING_QUERY_HISTORY = "SET_IS_FETCHING_QUERY_HISTORY";// Editor Selector mutation typesexport const SET_EDITOR_SELECTOR_STATE = "SET_EDITOR_SELECTOR_STATE";diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.tsindex 2203720a93..6abe89e988 100644--- a/frontend/src/types/sqlEditor.ts+++ b/frontend/src/types/sqlEditor.ts@@ -1,6 +1,7 @@import * as monaco from "monaco-editor";-import { InstanceId, DatabaseId, TableId, ViewId } from "../types";+import { InstanceId, DatabaseId, TableId, ViewId, ActivityId } from "../types";+import { Principal } from "./principal";export type EditorModel = monaco.editor.ITextModel;export type EditorPosition = monaco.Position;@@ -38,3 +39,23 @@ export type ConnectionContext = {selectedDatabaseId: number;selectedTableName: string;};++export interface QueryHistory {+ id: ActivityId;++ // Standard fields+ creator: Principal;+ createdTs: number;+ updatedTs: number;+ instanceId: number;++ // Domain fields+ statement: string;+ durationNs: number;+ instanceName: string;+ databaseName: string;+ error: string;++ // Customerize fields+ createdAt: string;+}diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.tsindex aae2815fa6..9c4ceb3916 100644--- a/frontend/src/types/store.ts+++ b/frontend/src/types/store.ts@@ -5,6 +5,7 @@ import {MigrationHistoryId,Policy,PolicyType,+ QueryHistory,View,} from ".";import { Activity } from "./activity";@@ -191,6 +192,9 @@ export interface LabelState {export interface SqlEditorState {connectionTree: ConnectionAtom[];connectionContext: ConnectionContext;+ shouldSetContent: boolean;+ queryHistoryList: QueryHistory[];+ isFetchingQueryHistory: boolean;isExecuting: boolean;isShowExecutingHint: boolean;}diff --git a/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue b/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vueindex 745dbfd5aa..beede37f6f 100644--- a/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue+++ b/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue@@ -1,7 +1,7 @@<template><div class="aside-panel h-full">- <n-tabs type="segment" default-value="databases" class="h-full">- <n-tab-pane name="databases" :tab="$t('common.databases')">+ <NTabs type="segment" default-value="databases" class="h-full">+ <NTabPane name="databases" :tab="$t('common.databases')"><Splitpaneshorizontalclass="default-theme"@@ -16,11 +16,14 @@<TableSchema @close-pane="handleCloseTableSchemaPane" /></Pane></Splitpanes>- </n-tab-pane>- <n-tab-pane v-if="isDev()" name="queries" tab="Queries">+ </NTabPane>+ <NTabPane v-if="isDev()" name="queries" tab="Queries">Queries Page- </n-tab-pane>- </n-tabs>+ </NTabPane>+ <NTabPane name="history" :tab="$t('common.history')">+ <QueryHistoryView />+ </NTabPane>+ </NTabs></div></template>@@ -32,6 +35,7 @@ import type { SqlEditorState } from "../../../types";import { isDev } from "../../../utils";import DatabaseTree from "./DatabaseTree.vue";import TableSchema from "./TableSchema.vue";+import QueryHistoryView from "./QueryHistoryView.vue";const FULL_HEIGHT = 100;const DATABASE_PANE_SIZE = 60;diff --git a/frontend/src/views/SqlEditor/AsidePanel/DeleteHint.vue b/frontend/src/views/SqlEditor/AsidePanel/DeleteHint.vuenew file mode 100644index 0000000000..1229674da1--- /dev/null+++ b/frontend/src/views/SqlEditor/AsidePanel/DeleteHint.vue@@ -0,0 +1,39 @@+<template>+ <div class="w-80 flex flex-col justify-start items-start">+ <div class="w-full leading-8">+ <p>{{ props.content }}</p>+ </div>+ <div class="w-full mt-4 flex justify-end space-x-2">+ <NButton @click="handleColse">{{ $t("common.close") }}</NButton>+ <NButton type="primary" @click="handleConfirm">{{+ $t("common.confirm")+ }}</NButton>+ </div>+ </div>+</template>++<script lang="ts" setup>+import { defineEmits, defineProps } from "vue";++const props = defineProps({+ content: {+ type: String,+ default: "",+ },+});++const emit = defineEmits<{+ (e: "confirm"): void;+ (e: "close"): void;+}>();++const handleConfirm = () => {+ emit("confirm");+};++const handleColse = () => {+ emit("close");+};+</script>++<style scoped></style>diff --git a/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryView.vue b/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryView.vuenew file mode 100644index 0000000000..6f5f3c7e51--- /dev/null+++ b/frontend/src/views/SqlEditor/AsidePanel/QueryHistoryView.vue@@ -0,0 +1,184 @@+<template>+ <div+ class="relative p-2 space-y-2 w-full h-full flex flex-col justify-start items-start"+ >+ <div class="w-full">+ <NInput+ v-model:value="state.search"+ :placeholder="$t('sql-editor.search-history')"+ >+ <template #prefix>+ <heroicons-outline:search class="h-5 w-5 text-gray-300" />+ </template>+ </NInput>+ </div>+ <div class="w-full flex flex-col justify-start items-start overflow-y-auto">+ <div+ v-for="history in data"+ :key="history.id"+ class="w-full px-1 pr-2 py-2 border-b flex flex-col justify-start items-start"+ >+ <div class="w-full flex flex-row justify-between items-center">+ <span class="text-xs text-gray-500">{{ history.createdAt }}</span>+ <NDropdown+ trigger="click"+ :options="historyDropdownOptions"+ @select="(key: string) => handleActionBtnClick(key, history)"+ @clickoutside="handleActionBtnOutsideClick"+ >+ <NButton text>+ <template #icon>+ <heroicons-outline:dots-horizontal+ class="h-4 w-4 text-gray-500"+ />+ </template>+ </NButton>+ </NDropdown>+ </div>+ <p+ class="max-w-full mt-2 mb-1 text-sm break-words font-mono cursor-pointer line-clamp-3 hover:bg-gray-300"+ @click="handleQueryHistoryClick(history)"+ >+ {{ history.statement }}+ </p>+ </div>+ </div>++ <div+ v-show="notifyMessage"+ class="absolute w-full h-full flex justify-center items-center transition-all bg-transparent"+ >+ {{ notifyMessage }}+ </div>++ <div+ v-show="isLoading && queryHistoryList.length === 0"+ class="absolute w-full h-full flex justify-center items-center"+ >+ <BBSpin :title="$t('common.loading')" />+ </div>+ </div>++ <BBModal+ v-if="state.isShowDeletingHint"+ :title="$t('common.tips')"+ @close="handleHintClose"+ >+ <DeleteHint+ :content="$t('sql-editor.hint-tips.confirm-to-delete-this-history')"+ @close="handleHintClose"+ @confirm="handleDeleteHistory"+ />+ </BBModal>+</template>++<script lang="ts" setup>+import { computed, reactive } from "vue";+import { useI18n } from "vue-i18n";+import {+ useNamespacedActions,+ useNamespacedState,+} from "vuex-composition-helpers";+import {+ EditorSelectorActions,+ QueryHistory,+ SqlEditorActions,+ SqlEditorState,+} from "../../../types";+import DeleteHint from "./DeleteHint.vue";++interface State {+ search: string;+ isShowDeletingHint: boolean;+ currentActionHistory: QueryHistory | null;+}++const { t } = useI18n();++const { queryHistoryList, isFetchingQueryHistory: isLoading } =+ useNamespacedState<SqlEditorState>("sqlEditor", [+ "queryHistoryList",+ "isFetchingQueryHistory",+ ]);+const { deleteQueryHistory, setShouldSetContent } =+ useNamespacedActions<SqlEditorActions>("sqlEditor", [+ "deleteQueryHistory",+ "setShouldSetContent",+ ]);+const { updateActiveTab } = useNamespacedActions<EditorSelectorActions>(+ "editorSelector",+ ["updateActiveTab"]+);++const state = reactive<State>({+ search: "",+ isShowDeletingHint: false,+ currentActionHistory: null,+});++const data = computed(() => {+ const temp =+ queryHistoryList.value && queryHistoryList.value.length > 0+ ? queryHistoryList.value.filter((history) => {+ let t = false;++ if (history.statement.includes(state.search)) {+ t = true;+ }++ return t;+ })+ : [];+ return temp;+});++const notifyMessage = computed(() => {+ if (isLoading.value) {+ return "";+ }+ if (queryHistoryList.value.length === null) {+ return t("sql-editor.no-history-found");+ }++ return "";+});++const historyDropdownOptions = computed(() => [+ {+ label: t("common.delete"),+ key: "delete",+ },+]);++const handleActionBtnClick = (key: string, history: QueryHistory) => {+ state.currentActionHistory = history;++ if (key === "delete") {+ state.isShowDeletingHint = true;+ }+};++const handleActionBtnOutsideClick = () => {+ state.currentActionHistory = null;+};++const handleHintClose = () => {+ state.currentActionHistory = null;+ state.isShowDeletingHint = false;+};++const handleDeleteHistory = () => {+ if (state.currentActionHistory) {+ deleteQueryHistory(state.currentActionHistory.id);+ }+ state.isShowDeletingHint = false;+};++const handleQueryHistoryClick = async (queryHistory: QueryHistory) => {+ updateActiveTab({+ queryStatement: queryHistory.statement,+ selectedStatement: "",+ });+ setShouldSetContent(true);+};+</script>diff --git a/frontend/src/views/SqlEditor/TablePanel/HistoryView.vue b/frontend/src/views/SqlEditor/TablePanel/HistoryView.vuedeleted file mode 100644index 86fa7e9428..0000000000--- a/frontend/src/views/SqlEditor/TablePanel/HistoryView.vue+++ /dev/null@@ -1 +0,0 @@-<template>History View</template>diff --git a/frontend/src/views/SqlEditor/TablePanel/TablePanel.vue b/frontend/src/views/SqlEditor/TablePanel/TablePanel.vueindex dd9e3d797e..1c1db9c3b1 100644--- a/frontend/src/views/SqlEditor/TablePanel/TablePanel.vue+++ b/frontend/src/views/SqlEditor/TablePanel/TablePanel.vue@@ -3,15 +3,9 @@<Pane size="100"><TableView /></Pane>- <Pane v-if="isDev()" size="0">- <HistroyView />- </Pane></Splitpanes></template><script lang="ts" setup>import TableView from "./TableView.vue";-import HistroyView from "./HistoryView.vue";--import { isDev } from "../../../utils";</script>diff --git a/frontend/tailwind.config.js b/frontend/tailwind.config.jsindex 037d4f090c..79d889a7b0 100644--- a/frontend/tailwind.config.js+++ b/frontend/tailwind.config.js@@ -95,5 +95,9 @@ module.exports = {textColor: ["disabled"],},},- plugins: [require("@tailwindcss/forms"), require("@tailwindcss/typography")],+ plugins: [+ require("@tailwindcss/forms"),+ require("@tailwindcss/line-clamp"),+ require("@tailwindcss/typography"),+ ],};diff --git a/frontend/yarn.lock b/frontend/yarn.lockindex f7e32052fa..040483d902 100644--- a/frontend/yarn.lock+++ b/frontend/yarn.lock@@ -278,6 +278,11 @@dependencies:mini-svg-data-uri "^1.2.3"+"@tailwindcss/line-clamp@^0.3.1":+ version "0.3.1"+ resolved "https://registry.yarnpkg.com/@tailwindcss/line-clamp/-/line-clamp-0.3.1.tgz#4d8441b509b87ece84e94f28a4aa9998413ab849"+ integrity sha512-pNr0T8LAc3TUx/gxCfQZRe9NB2dPEo/cedPHzUGIPxqDMhgjwNm6jYxww4W5l0zAsAddxr+XfZcqttGiFDgrGg==+"@tailwindcss/typography@^0.4.0":version "0.4.1"resolved "https://registry.yarnpkg.com/@tailwindcss/typography/-/typography-0.4.1.tgz#51ddbceea6a0ee9902c649dbe58871c81a831212"