Skip to content

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

chore: export logs (#8178)

ecmadao4d603c52014b
diff --git a/frontend/src/components/DataExportButton.vue b/frontend/src/components/DataExportButton.vue
index 5de2d5c142..57b048ef64 100644
--- a/frontend/src/components/DataExportButton.vue
+++ b/frontend/src/components/DataExportButton.vue
@@ -26,9 +26,8 @@ import { NButton, NDropdown } from "naive-ui";
import { BinaryLike } from "node:crypto";
import { computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
-import { getExportFileType, pushNotification } from "@/store";
-
-export type ExportFormat = "CSV" | "JSON" | "SQL" | "XLSX";
+import { pushNotification } from "@/store";
+import { ExportFormat, exportFormatToJSON } from "@/types/proto/v1/common";
interface LocalState {
isRequesting: boolean;
@@ -61,7 +60,9 @@ const state = reactive<LocalState>({
const exportDropdownOptions = computed(() => {
return props.supportFormats.map((format) => ({
- label: t("sql-editor.download-as-file", { file: format }),
+ label: t("sql-editor.download-as-file", {
+ file: exportFormatToJSON(format),
+ }),
key: format,
}));
});
@@ -87,13 +88,26 @@ const doExport = async (format: ExportFormat) => {
}
};
+const getExportFileType = (format: ExportFormat) => {
+ switch (format) {
+ case ExportFormat.CSV:
+ return "text/csv";
+ case ExportFormat.JSON:
+ return "application/json";
+ case ExportFormat.SQL:
+ return "application/sql";
+ case ExportFormat.XLSX:
+ return "application/vnd.ms-excel";
+ }
+};
+
const doDownload = (content: BinaryLike | Blob, format: ExportFormat) => {
const blob = new Blob([content], {
type: getExportFileType(format),
});
const url = window.URL.createObjectURL(blob);
- const fileFormat = format.toLowerCase();
+ const fileFormat = exportFormatToJSON(format).toLowerCase();
const formattedDateString = dayjs(new Date()).format("YYYY-MM-DDTHH-mm-ss");
const filename = `export-data-${formattedDateString}`;
const link = document.createElement("a");
diff --git a/frontend/src/store/modules/export.ts b/frontend/src/store/modules/export.ts
index dd09b6dea1..e2931eb61d 100644
--- a/frontend/src/store/modules/export.ts
+++ b/frontend/src/store/modules/export.ts
@@ -1,8 +1,9 @@
-import { getExportRequestFormat, useSQLStore } from "@/store";
+import { useSQLStore } from "@/store";
+import { ExportFormat } from "@/types/proto/v1/common";
import { extractDatabaseResourceName } from "@/utils";
export type ExportDataParams = {
- format: "CSV" | "JSON" | "SQL" | "XLSX";
+ format: ExportFormat;
statement: string;
limit: number;
database: string; // instances/{instance}/databases/{database}
@@ -23,7 +24,7 @@ export const useExportData = () => {
connectionDatabase,
statement: params.statement,
limit: params.limit,
- format: getExportRequestFormat(params.format),
+ format: params.format,
admin: params.admin ?? false,
});
diff --git a/frontend/src/store/modules/v1/activity.ts b/frontend/src/store/modules/v1/activity.ts
index 7eefe1bf1e..ea4a4a5b09 100644
--- a/frontend/src/store/modules/v1/activity.ts
+++ b/frontend/src/store/modules/v1/activity.ts
@@ -11,6 +11,7 @@ import {
EMPTY_ID,
ComposedIssue,
} from "@/types";
+import { ExportFormat } from "@/types/proto/v1/common";
import {
LogEntity,
LogEntity_Action,
@@ -184,6 +185,21 @@ export const useActivityV1Store = defineStore("activity_v1", () => {
return activity.resource.split("/").slice(-1)[0];
};
+ const exportData = async ({
+ find,
+ format,
+ }: {
+ find: FindActivityMessage;
+ format: ExportFormat;
+ }) => {
+ const resp = await loggingServiceClient.exportLogs({
+ orderBy: find.order ? `create_time ${find.order}` : "",
+ filter: buildFilter(find),
+ format,
+ });
+ return resp.content;
+ };
+
return {
fetchActivityList,
fetchActivityListForIssue,
@@ -194,5 +210,6 @@ export const useActivityV1Store = defineStore("activity_v1", () => {
getActivityListByIssue,
getActivityListByIssueV1,
getResourceId,
+ exportData,
};
});
diff --git a/frontend/src/store/modules/v1/sql.ts b/frontend/src/store/modules/v1/sql.ts
index 4a06bc5622..7fdb4eb491 100644
--- a/frontend/src/store/modules/v1/sql.ts
+++ b/frontend/src/store/modules/v1/sql.ts
@@ -2,7 +2,6 @@ import { ClientError, Status } from "nice-grpc-common";
import { defineStore } from "pinia";
import { sqlServiceClient } from "@/grpcweb";
import { SQLResultSetV1 } from "@/types";
-import { ExportFormat } from "@/types/proto/v1/common";
import { ExportRequest, QueryRequest } from "@/types/proto/v1/sql_service";
import { extractGrpcErrorMessage } from "@/utils/grpcweb";
@@ -47,33 +46,3 @@ export const useSQLStore = defineStore("sql", () => {
exportData,
};
});
-
-export const getExportRequestFormat = (
- format: "CSV" | "JSON" | "SQL" | "XLSX"
-): ExportFormat => {
- switch (format) {
- case "CSV":
- return ExportFormat.CSV;
- case "JSON":
- return ExportFormat.JSON;
- case "SQL":
- return ExportFormat.SQL;
- case "XLSX":
- return ExportFormat.XLSX;
- default:
- return ExportFormat.FORMAT_UNSPECIFIED;
- }
-};
-
-export const getExportFileType = (format: "CSV" | "JSON" | "SQL" | "XLSX") => {
- switch (format) {
- case "CSV":
- return "text/csv";
- case "JSON":
- return "application/json";
- case "SQL":
- return "application/sql";
- case "XLSX":
- return "application/vnd.ms-excel";
- }
-};
diff --git a/frontend/src/views/ExportCenter/ExportDataButton.vue b/frontend/src/views/ExportCenter/ExportDataButton.vue
index bfd4d64553..85063fcfd2 100644
--- a/frontend/src/views/ExportCenter/ExportDataButton.vue
+++ b/frontend/src/views/ExportCenter/ExportDataButton.vue
@@ -1,16 +1,21 @@
<template>
<DataExportButton
size="tiny"
- :support-formats="['CSV', 'JSON', 'SQL', 'XLSX']"
+ :support-formats="[
+ ExportFormat.CSV,
+ ExportFormat.JSON,
+ ExportFormat.SQL,
+ ExportFormat.XLSX,
+ ]"
@export="handleExportData"
/>
</template>
<script lang="ts" setup>
import { BinaryLike } from "node:crypto";
-import { ExportFormat } from "@/components/DataExportButton.vue";
import { useProjectIamPolicyStore } from "@/store";
import { useExportData } from "@/store/modules/export";
+import { ExportFormat } from "@/types/proto/v1/common";
import { ExportRecord } from "./types";
const props = defineProps<{
diff --git a/frontend/src/views/SettingWorkspaceAuditLog.vue b/frontend/src/views/SettingWorkspaceAuditLog.vue
index cec808c688..6c79476caa 100644
--- a/frontend/src/views/SettingWorkspaceAuditLog.vue
+++ b/frontend/src/views/SettingWorkspaceAuditLog.vue
@@ -31,26 +31,20 @@
</div>
<DataExportButton
size="large"
- :support-formats="['CSV', 'JSON']"
- :disabled="auditLogList.length === 0"
+ :support-formats="[
+ ExportFormat.CSV,
+ ExportFormat.JSON,
+ ExportFormat.XLSX,
+ ]"
+ :disabled="!hasAuditLogFeature"
@export="handleExport"
/>
</div>
<PagedActivityTable
v-if="hasAuditLogFeature"
- :activity-find="{
- action:
- selectedAuditTypeList.length > 0
- ? selectedAuditTypeList
- : AuditActivityTypeList,
- creatorEmail: selectedUserEmail,
- order: 'desc',
- createdTsAfter: selectedTimeRange ? selectedTimeRange[0] : undefined,
- createdTsBefore: selectedTimeRange ? selectedTimeRange[1] : undefined,
- }"
+ :activity-find="activityFind"
session-key="bb.page-audit-log-table.settings-audit-log-table"
:page-size="10"
- @list:update="(list: LogEntity[]) => auditLogList = list"
>
<template #table="{ list }">
<AuditLogTable :audit-log-list="list" @view-detail="handleViewDetail" />
@@ -108,18 +102,17 @@ import { reactive, ref, computed } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import { BBDialog } from "@/bbkit";
-import { ExportFormat } from "@/components/DataExportButton.vue";
-import { featureToRef, useUserStore } from "@/store";
+import { featureToRef, useUserStore, useActivityV1Store } from "@/store";
import {
AuditActivityTypeList,
UNKNOWN_ID,
AuditActivityTypeI18nNameMap,
+ FindActivityMessage,
} from "@/types";
+import { ExportFormat } from "@/types/proto/v1/common";
import {
LogEntity,
LogEntity_Action,
- logEntity_ActionToJSON,
- LogEntity_Level,
logEntity_LevelToJSON,
} from "@/types/proto/v1/logging_service";
@@ -129,12 +122,11 @@ const state = reactive({
modalContent: {},
});
-const auditLogList = ref<LogEntity[]>([]);
-
const { t } = useI18n();
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
+const activityV1Store = useActivityV1Store();
const hasAuditLogFeature = featureToRef("bb.feature.audit-log");
@@ -269,79 +261,31 @@ const clearDatePicker = () => {
});
};
-const handleExport = (
+const activityFind = computed((): FindActivityMessage => {
+ return {
+ action:
+ selectedAuditTypeList.value.length > 0
+ ? selectedAuditTypeList.value
+ : AuditActivityTypeList,
+ creatorEmail: selectedUserEmail.value,
+ order: "desc",
+ createdTsAfter: selectedTimeRange.value
+ ? selectedTimeRange.value[0]
+ : undefined,
+ createdTsBefore: selectedTimeRange.value
+ ? selectedTimeRange.value[1]
+ : undefined,
+ };
+});
+
+const handleExport = async (
format: ExportFormat,
callback: (content: BinaryLike | Blob, format: ExportFormat) => void
) => {
- const content = formatExport(format);
+ const content = await activityV1Store.exportData({
+ find: activityFind.value,
+ format,
+ });
callback(content, format);
};
-
-function escapeCSVString(s: string) {
- // Escape double quotes
- s = s.replace(/"/g, '""');
- // Escape commas
- s = s.replace(/,/g, "\\,");
- // Escape new lines
- s = s.replace(/\n/g, "\\n");
- // Escape carriage returns
- s = s.replace(/\r/g, "\\r");
- return s;
-}
-
-function formatLevel(level: LogEntity_Level) {
- switch (level) {
- case LogEntity_Level.LEVEL_INFO:
- return "INFO";
- case LogEntity_Level.LEVEL_WARNING:
- return "WARNING";
- case LogEntity_Level.LEVEL_ERROR:
- return "ERROR";
- }
- return "UNKNOWN_LEVEL";
-}
-
-const formatExport = (format: ExportFormat): BinaryLike => {
- switch (format) {
- case "CSV":
- return auditLogList.value
- .reduce(
- (list, auditLog) => {
- list.push(
- [
- dayjs(auditLog.createTime).format("YYYY-MM-DD HH:mm:ss Z"),
- formatLevel(auditLog.level),
- logEntity_ActionToJSON(auditLog.action),
- auditLog.creator,
- auditLog.resource,
- auditLog.comment,
- escapeCSVString(auditLog.payload),
- ].join(",")
- );
- return list;
- },
- ["time,level,action,actor,resource,comment,payload"]
- )
- .join("\n");
- case "JSON":
- return JSON.stringify(
- auditLogList.value.map((auditLog) => {
- return {
- time: dayjs(auditLog.createTime).format("YYYY-MM-DD HH:mm:ss Z"),
- level: formatLevel(auditLog.level),
- action: logEntity_ActionToJSON(auditLog.action),
- actor: auditLog.creator,
- resource: auditLog.resource,
- comment: auditLog.comment,
- payload: auditLog.payload,
- };
- }),
- null,
- 2
- );
- default:
- // Should never reach this line.
- return "";
- }
-};
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index 9e41631d3e..d519de9066 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -65,7 +65,12 @@
v-if="showExportButton"
size="small"
:disabled="props.result === null || isEmpty(props.result)"
- :support-formats="['CSV', 'JSON', 'SQL', 'XLSX']"
+ :support-formats="[
+ ExportFormat.CSV,
+ ExportFormat.JSON,
+ ExportFormat.SQL,
+ ExportFormat.XLSX,
+ ]"
@export="handleExportBtnClick"
/>
<NButton v-else @click="state.showRequestExportPanel = true">
@@ -139,7 +144,6 @@ import { NInput, NPagination, NTooltip } from "naive-ui";
import { BinaryLike } from "node:crypto";
import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
-import { ExportFormat } from "@/components/DataExportButton.vue";
import RequestExportPanel from "@/components/Issue/panel/RequestExportPanel/index.vue";
import { DISMISS_PLACEHOLDER } from "@/plugins/ai/components/state";
import {
@@ -159,6 +163,7 @@ import {
TabMode,
UNKNOWN_ID,
} from "@/types";
+import { ExportFormat } from "@/types/proto/v1/common";
import { Engine } from "@/types/proto/v1/common";
import { QueryResult } from "@/types/proto/v1/sql_service";
import {