Skip to content

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

feat: implement export center (#6502)

boojack13502c890b04
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index f4ca9bfc98..b7d2814037 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2543,5 +2543,8 @@
"condition": {
"self": "Condition"
}
+ },
+ "export-center": {
+ "self": "Export Center"
}
}
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 80bc79a9b3..c50c734c41 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2521,5 +2521,8 @@
"condition": {
"self": "Condición"
}
+ },
+ "export-center": {
+ "self": "centro de exportacion"
}
}
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index b82e883e7c..08a9779e6d 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2535,5 +2535,8 @@
"condition": {
"self": "条件"
}
+ },
+ "export-center": {
+ "self": "导出中心"
}
}
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index c0cb1f1169..06ace2c90e 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -256,6 +256,19 @@ const routes: Array<RouteRecordRaw> = [
leftSidebar: true,
},
},
+ {
+ path: "export-center",
+ name: "workspace.export-center",
+ meta: { title: () => startCase(t("export-center.self")) },
+ components: {
+ content: () => import("../views/ExportCenter/index.vue"),
+ leftSidebar: DashboardSidebar,
+ },
+ props: {
+ content: true,
+ leftSidebar: true,
+ },
+ },
{
path: "anomaly-center",
name: "workspace.anomaly-center",
@@ -1300,6 +1313,7 @@ router.beforeEach((to, from, next) => {
to.name === "workspace.inbox" ||
to.name === "workspace.slow-query" ||
to.name === "workspace.sync-schema" ||
+ to.name === "workspace.export-center" ||
to.name === "workspace.anomaly-center" ||
to.name === "workspace.project" ||
to.name === "workspace.instance" ||
diff --git a/frontend/src/store/modules/v1/sql.ts b/frontend/src/store/modules/v1/sql.ts
index df73d4c9a6..8a7bf546db 100644
--- a/frontend/src/store/modules/v1/sql.ts
+++ b/frontend/src/store/modules/v1/sql.ts
@@ -1,6 +1,10 @@
import { sqlServiceClient } from "@/grpcweb";
import { SQLResultSetV1 } from "@/types";
-import { QueryRequest } from "@/types/proto/v1/sql_service";
+import {
+ ExportRequest,
+ ExportRequest_Format,
+ QueryRequest,
+} from "@/types/proto/v1/sql_service";
import { extractGrpcErrorMessage } from "@/utils/grpcweb";
import { defineStore } from "pinia";
@@ -24,5 +28,26 @@ export const useSQLStore = defineStore("sql", () => {
};
}
};
- return { queryReadonly };
+
+ const exportData = async (params: ExportRequest) => {
+ return await sqlServiceClient.export(params);
+ };
+
+ return {
+ queryReadonly,
+ exportData,
+ };
});
+
+export const getExportRequestFormat = (
+ format: "CSV" | "JSON"
+): ExportRequest_Format => {
+ switch (format) {
+ case "CSV":
+ return ExportRequest_Format.CSV;
+ case "JSON":
+ return ExportRequest_Format.JSON;
+ default:
+ return ExportRequest_Format.FORMAT_UNSPECIFIED;
+ }
+};
diff --git a/frontend/src/views/DashboardSidebar.vue b/frontend/src/views/DashboardSidebar.vue
index 4014c41779..a8511017e1 100644
--- a/frontend/src/views/DashboardSidebar.vue
+++ b/frontend/src/views/DashboardSidebar.vue
@@ -47,6 +47,13 @@
<heroicons-outline:refresh class="w-5 h-5 mr-2" />
{{ $t("database.sync-schema.title") }}
</router-link>
+ <router-link
+ to="/export-center"
+ class="outline-item group flex items-center px-2 py-2 capitalize"
+ >
+ <heroicons-outline:download class="w-5 h-5 mr-2" />
+ {{ $t("export-center.self") }}
+ </router-link>
<router-link
to="/slow-query"
class="outline-item group flex items-center px-2 py-2 capitalize"
diff --git a/frontend/src/views/ExportCenter/ExportDataButton.vue b/frontend/src/views/ExportCenter/ExportDataButton.vue
new file mode 100644
index 0000000000..9e59a364f2
--- /dev/null
+++ b/frontend/src/views/ExportCenter/ExportDataButton.vue
@@ -0,0 +1,108 @@
+<template>
+ <NButton quaternary size="tiny" @click="state.showConfirmModal = true">
+ <heroicons-outline:document-download class="w-5 h-auto" />
+ </NButton>
+
+ <BBModal
+ v-if="state.showConfirmModal"
+ :title="$t('common.export')"
+ header-class="overflow-hidden"
+ @close="state.showConfirmModal = false"
+ >
+ <div class="w-128 mb-6">
+ {{ $t("issue.grant-request.data-export-attention") }}
+ </div>
+ <div class="w-full flex items-center justify-end mt-2 space-x-3 pr-1 pb-1">
+ <button
+ type="button"
+ class="btn-cancel"
+ @click="state.showConfirmModal = false"
+ >
+ {{ $t("common.cancel") }}
+ </button>
+ <button
+ class="btn-primary"
+ :disabled="state.isRequesting"
+ @click="handleExportData"
+ >
+ <BBSpin v-if="state.isRequesting" class="mr-2" color="text-white" />
+ {{ $t("common.confirm") }}
+ </button>
+ </div>
+ </BBModal>
+</template>
+
+<script lang="ts" setup>
+import { NButton } from "naive-ui";
+import { ExportRecord } from "./types";
+import { reactive } from "vue";
+import {
+ getExportRequestFormat,
+ pushNotification,
+ useProjectIamPolicyStore,
+ useSQLStore,
+} from "@/store";
+import dayjs from "dayjs";
+
+interface LocalState {
+ showConfirmModal: boolean;
+ isRequesting: boolean;
+}
+
+const props = defineProps<{
+ exportRecord: ExportRecord;
+}>();
+
+const sqlStore = useSQLStore();
+const projectIamPolicyStore = useProjectIamPolicyStore();
+const state = reactive<LocalState>({
+ showConfirmModal: false,
+ isRequesting: false,
+});
+
+const handleExportData = async () => {
+ if (state.isRequesting) {
+ return;
+ }
+
+ state.isRequesting = true;
+
+ const exportRecord = props.exportRecord;
+ const database = exportRecord.database;
+
+ try {
+ const { content } = await sqlStore.exportData({
+ name: database.instance,
+ connectionDatabase: database.databaseName,
+ statement: exportRecord.statement,
+ limit: exportRecord.maxRowCount,
+ format: getExportRequestFormat(exportRecord.exportFormat),
+ });
+
+ const blob = new Blob([content], {
+ type:
+ exportRecord.exportFormat === "CSV" ? "text/csv" : "application/json",
+ });
+ const url = window.URL.createObjectURL(blob);
+
+ const fileFormat = exportRecord.exportFormat.toLowerCase();
+ const formattedDateString = dayjs(new Date()).format("YYYY-MM-DDTHH-mm-ss");
+ const filename = `export-data-${formattedDateString}`;
+ const link = document.createElement("a");
+ link.download = `${filename}.${fileFormat}`;
+ link.href = url;
+ link.click();
+ // Fetch the latest iam policy.
+ await projectIamPolicyStore.fetchProjectIamPolicy(database.project, true);
+ state.isRequesting = false;
+ state.showConfirmModal = false;
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: `Failed to export data`,
+ description: JSON.stringify(error),
+ });
+ }
+};
+</script>
diff --git a/frontend/src/views/ExportCenter/ExportRecordTable.vue b/frontend/src/views/ExportCenter/ExportRecordTable.vue
new file mode 100644
index 0000000000..fdf4780f16
--- /dev/null
+++ b/frontend/src/views/ExportCenter/ExportRecordTable.vue
@@ -0,0 +1,133 @@
+<template>
+ <BBGrid
+ :column-list="COLUMN_LIST"
+ :data-source="exportRecords"
+ :row-clickable="true"
+ :show-placeholder="true"
+ :is-row-expanded="isSelectedRow"
+ class="border w-auto overflow-x-auto"
+ header-class="capitalize"
+ >
+ <template #item="{ item }: { item: ExportRecord }">
+ <div class="bb-grid-cell">
+ <NButton quaternary size="tiny" @click.stop="toggleExpandRow(item)">
+ <heroicons:chevron-right
+ class="w-4 h-auto transition-transform duration-150 cursor-pointer"
+ :class="[isSelectedRow(item) && 'rotate-90']"
+ />
+ </NButton>
+ </div>
+ <div
+ class="bb-grid-cell text-blue-600 hover:underline"
+ @click="gotoIssuePage(item)"
+ >
+ #{{ item.issueId }}
+ </div>
+ <div class="bb-grid-cell">
+ {{ item.database.databaseName }}
+ </div>
+ <div class="bb-grid-cell">
+ {{ item.database.instanceEntity.environmentEntity.title }}
+ </div>
+ <div class="bb-grid-cell">
+ {{ item.database.instanceEntity.title }}
+ </div>
+ <div class="bb-grid-cell">
+ {{ item.database.projectEntity.title }}
+ </div>
+ <div class="bb-grid-cell">
+ {{ item.expiration ? humanizeDate(new Date(item.expiration)) : "*" }}
+ </div>
+ <div class="bb-grid-cell">
+ <ExportDataButton :export-record="item" />
+ </div>
+ </template>
+ <template #expanded-item="{ item }: { item: ExportRecord }">
+ <div class="w-full max-h-[20rem] overflow-auto text-xs pl-2">
+ <HighlightCodeBlock
+ :code="item.statement"
+ class="whitespace-pre-wrap"
+ />
+ </div>
+ </template>
+ </BBGrid>
+</template>
+
+<script lang="ts" setup>
+import { computed, shallowRef } from "vue";
+import { useI18n } from "vue-i18n";
+import { ExportRecord } from "./types";
+import { BBGridColumn } from "@/bbkit";
+import ExportDataButton from "./ExportDataButton.vue";
+import { pushNotification, useIssueStore } from "@/store";
+import { UNKNOWN_ID } from "@/types";
+import { issueSlug } from "@/utils";
+
+defineProps<{
+ exportRecords: ExportRecord[];
+}>();
+
+defineEmits<{
+ (event: "export", exportRecord: ExportRecord): void;
+}>();
+
+const { t } = useI18n();
+
+const selectedExportRecord = shallowRef<ExportRecord>();
+
+const COLUMN_LIST = computed(() => {
+ const columns: BBGridColumn[] = [
+ { title: "", width: "4rem" },
+ { title: "Issue", width: "1fr" },
+ {
+ title: t("common.database"),
+ width: "1fr",
+ },
+ {
+ title: t("common.environment"),
+ width: "1fr",
+ },
+ {
+ title: t("common.instance"),
+ width: "1fr",
+ },
+ {
+ title: t("common.project"),
+ width: "1fr",
+ },
+ {
+ title: t("common.expiration"),
+ width: "1fr",
+ },
+ { title: "", width: "1fr" },
+ ];
+
+ return columns;
+});
+
+const isSelectedRow = (item: ExportRecord) => {
+ return selectedExportRecord.value === item;
+};
+
+const toggleExpandRow = (item: ExportRecord) => {
+ if (selectedExportRecord.value === item) {
+ selectedExportRecord.value = undefined;
+ } else {
+ selectedExportRecord.value = item;
+ }
+};
+
+const gotoIssuePage = async (item: ExportRecord) => {
+ const issue = await useIssueStore().getOrFetchIssueById(item.issueId);
+ if (issue.id === UNKNOWN_ID) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: `Issue #${issue.id} not found`,
+ });
+ return;
+ }
+
+ window.open(`/issue/${issueSlug(issue.name, issue.id)}`, "_blank");
+};
+</script>
diff --git a/frontend/src/views/ExportCenter/index.vue b/frontend/src/views/ExportCenter/index.vue
new file mode 100644
index 0000000000..c4ba856de4
--- /dev/null
+++ b/frontend/src/views/ExportCenter/index.vue
@@ -0,0 +1,180 @@
+<template>
+ <div class="w-full px-4 py-1 pt-2">
+ <div class="w-full flex flex-row justify-between items-center">
+ <div>
+ <NInputGroup>
+ <ProjectSelect
+ :project="state.filterParams.project?.uid ?? String(UNKNOWN_ID)"
+ :include-all="true"
+ @update:project="changeProjectId"
+ />
+ <InstanceSelect
+ :instance="state.filterParams.instance?.uid ?? String(UNKNOWN_ID)"
+ :include-all="true"
+ @update:instance="changeInstanceId"
+ />
+ <DatabaseSelect
+ :database="state.filterParams.database?.uid ?? String(UNKNOWN_ID)"
+ :instance="state.filterParams.instance?.uid"
+ :project="state.filterParams.project?.uid"
+ :include-all="true"
+ @update:database="changeDatabaseId"
+ />
+ </NInputGroup>
+ </div>
+ <div>
+ <NButton @click="createExportDataIssue">
+ {{ $t("quick-action.request-export") }}
+ </NButton>
+ </div>
+ </div>
+ <div class="w-full mt-4">
+ <ExportRecordTable :export-records="filterExportRecords" />
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { head } from "lodash-es";
+import { NButton, NInputGroup } from "naive-ui";
+import { computed, reactive, watchEffect } from "vue";
+import { useRouter } from "vue-router";
+import { UNKNOWN_ID } from "@/types";
+import { FilterParams, ExportRecord } from "./types";
+import {
+ useCurrentUserV1,
+ useDatabaseV1Store,
+ useInstanceV1Store,
+ useProjectIamPolicyStore,
+ useProjectV1ListByCurrentUser,
+ useProjectV1Store,
+} from "@/store";
+import { ProjectSelect, InstanceSelect, DatabaseSelect } from "@/components/v2";
+import { convertFromSimpleExpr } from "@/utils/issue/cel";
+import ExportRecordTable from "./ExportRecordTable.vue";
+
+interface LocalState {
+ filterParams: FilterParams;
+ exportRecords: ExportRecord[];
+}
+
+const issueDescriptionRegexp = /^#(\d+)$/;
+
+const router = useRouter();
+const currentUser = useCurrentUserV1();
+const projectIamPolicyStore = useProjectIamPolicyStore();
+const databaseStore = useDatabaseV1Store();
+const { projectList } = useProjectV1ListByCurrentUser();
+const state = reactive<LocalState>({
+ filterParams: {
+ project: undefined,
+ instance: undefined,
+ database: undefined,
+ },
+ exportRecords: [],
+});
+
+const filterExportRecords = computed(() => {
+ return state.exportRecords.filter((record) => {
+ if (state.filterParams.project) {
+ if (record.database.project !== state.filterParams.project.name) {
+ return false;
+ }
+ }
+ if (state.filterParams.instance) {
+ if (record.database.instance !== state.filterParams.instance.name) {
+ return false;
+ }
+ }
+ if (state.filterParams.database) {
+ if (record.database.name !== state.filterParams.database.name) {
+ return false;
+ }
+ }
+ return true;
+ });
+});
+
+watchEffect(async () => {
+ const projectNameList = projectList.value.map((project) => project.name);
+ const iamPolicyList =
+ await projectIamPolicyStore.batchGetOrFetchProjectIamPolicy(
+ projectNameList
+ );
+
+ const tempExportRecords: ExportRecord[] = [];
+ for (const iamPolicy of iamPolicyList) {
+ const bindings = iamPolicy.bindings.filter(
+ (binding) =>
+ binding.role === "roles/EXPORTER" &&
+ binding.members.includes(`user:${currentUser.value.email}`)
+ );
+ for (const binding of bindings) {
+ if (!binding.parsedExpr?.expr) {
+ continue;
+ }
+
+ const conditionExpr = convertFromSimpleExpr(binding.parsedExpr.expr);
+ const databaseResource = head(conditionExpr.databaseResources);
+ if (databaseResource) {
+ const description = binding.condition?.description || "";
+ const issueId = description.match(issueDescriptionRegexp)?.[1];
+ const database = await databaseStore.getOrFetchDatabaseByName(
+ databaseResource.databaseName
+ );
+ tempExportRecords.push({
+ database,
+ expiration: conditionExpr.expiredTime || "",
+ statement: conditionExpr.statement || "",
+ maxRowCount: conditionExpr.rowLimit || 0,
+ exportFormat: (conditionExpr.exportFormat as any) || "JSON",
+ issueId: issueId || String(UNKNOWN_ID),
+ });
+ }
+ }
+ }
+ state.exportRecords = tempExportRecords;
+});
+
+const changeProjectId = (id: string | undefined) => {
+ if (id && id !== String(UNKNOWN_ID)) {
+ const project = useProjectV1Store().getProjectByUID(id);
+ state.filterParams.project = project;
+ } else {
+ state.filterParams.project = undefined;
+ }
+};
+
+const changeInstanceId = (uid: string | undefined) => {
+ if (uid && uid !== String(UNKNOWN_ID)) {
+ const instance = useInstanceV1Store().getInstanceByUID(uid);
+ state.filterParams.instance = instance;
+ } else {
+ state.filterParams.instance = undefined;
+ }
+};
+
+const changeDatabaseId = (uid: string | undefined) => {
+ if (uid && uid !== String(UNKNOWN_ID)) {
+ const database = useDatabaseV1Store().getDatabaseByUID(uid);
+ state.filterParams.database = database;
+ } else {
+ state.filterParams.database = undefined;
+ }
+};
+
+const createExportDataIssue = () => {
+ const routeInfo = {
+ name: "workspace.issue.detail",
+ params: {
+ issueSlug: "new",
+ },
+ query: {
+ template: "bb.issue.grant.request",
+ role: "EXPORTER",
+ name: "New grant exporter request",
+ },
+ };
+ router.push(routeInfo);
+};
+</script>
diff --git a/frontend/src/views/ExportCenter/types.ts b/frontend/src/views/ExportCenter/types.ts
new file mode 100644
index 0000000000..2a30fea6ed
--- /dev/null
+++ b/frontend/src/views/ExportCenter/types.ts
@@ -0,0 +1,20 @@
+import { ComposedDatabase } from "@/types";
+import { Database } from "@/types/proto/v1/database_service";
+import { Instance } from "@/types/proto/v1/instance_service";
+import { Project } from "@/types/proto/v1/project_service";
+
+export interface FilterParams {
+ project: Project | undefined; // undefined to "All"
+ instance: Instance | undefined; // undefined to "All"
+ database: Database | undefined; // undefined to "All"
+}
+
+export interface ExportRecord {
+ database: ComposedDatabase;
+ expiration: string;
+ statement: string;
+ maxRowCount: number;
+ exportFormat: "CSV" | "JSON";
+ // issueId is the uid of an issue.
+ issueId: string;
+}