Skip to content

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

chore: support export audit log as CSV or JSON (#7617)

ecmadaoc8cb80eb8dfa
diff --git a/frontend/src/components/DataExportButton.vue b/frontend/src/components/DataExportButton.vue
index f41105dacd..5de2d5c142 100644
--- a/frontend/src/components/DataExportButton.vue
+++ b/frontend/src/components/DataExportButton.vue
@@ -36,7 +36,7 @@ interface LocalState {
const props = withDefaults(
defineProps<{
- size?: "small" | "tiny";
+ size?: "small" | "tiny" | "medium" | "large";
disabled?: boolean;
supportFormats: ExportFormat[];
}>(),
diff --git a/frontend/src/components/PagedActivityTable.vue b/frontend/src/components/PagedActivityTable.vue
index c62a4084ae..8951a3deef 100644
--- a/frontend/src/components/PagedActivityTable.vue
+++ b/frontend/src/components/PagedActivityTable.vue
@@ -78,6 +78,10 @@ const props = defineProps({
},
});
+const emit = defineEmits<{
+ (event: "list:update", list: LogEntity[]): void;
+}>();
+
const state = reactive<LocalState>({
loading: false,
auditLogList: [],
@@ -177,4 +181,9 @@ watch(isLoggedIn, () => {
resetSession();
}
});
+
+watch(
+ () => state.auditLogList,
+ (list) => emit("list:update", list)
+);
</script>
diff --git a/frontend/src/views/SettingWorkspaceAuditLog.vue b/frontend/src/views/SettingWorkspaceAuditLog.vue
index 2ae84a4d2b..75109f793d 100644
--- a/frontend/src/views/SettingWorkspaceAuditLog.vue
+++ b/frontend/src/views/SettingWorkspaceAuditLog.vue
@@ -1,7 +1,7 @@
<template>
<div class="w-full mt-4 space-y-4">
<FeatureAttention feature="bb.feature.audit-log" />
- <div class="flex justify-end items-center mt-1">
+ <div class="flex justify-end items-center mt-1 space-x-2">
<MemberSelect
class="w-52"
:disabled="!hasAuditLogFeature"
@@ -10,14 +10,14 @@
:selected-id="selectedUserUID"
@select-user-id="selectUser"
/>
- <div class="w-52 ml-2">
+ <div class="w-52">
<TypeSelect
:disabled="!hasAuditLogFeature"
:selected-type-list="selectedAuditTypeList"
@update-selected-type-list="selectAuditType"
/>
</div>
- <div class="w-112 ml-2">
+ <div class="w-112">
<NDatePicker
v-model:value="selectedTimeRange"
type="datetimerange"
@@ -28,6 +28,12 @@
>
</NDatePicker>
</div>
+ <DataExportButton
+ size="large"
+ :support-formats="['CSV', 'JSON']"
+ :disabled="auditLogList.length === 0"
+ @export="handleExport"
+ />
</div>
<PagedActivityTable
v-if="hasAuditLogFeature"
@@ -43,6 +49,7 @@
}"
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" />
@@ -95,10 +102,12 @@
<script lang="ts" setup>
import dayjs from "dayjs";
import { NGrid, NGi, NDatePicker } from "naive-ui";
+import { BinaryLike } from "node:crypto";
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 {
AuditActivityTypeList,
@@ -108,6 +117,7 @@ import {
import {
LogEntity,
LogEntity_Action,
+ logEntity_ActionToJSON,
logEntity_LevelToJSON,
} from "@/types/proto/v1/logging_service";
@@ -117,6 +127,8 @@ const state = reactive({
modalContent: {},
});
+const auditLogList = ref<LogEntity[]>([]);
+
const { t } = useI18n();
const router = useRouter();
const route = useRoute();
@@ -254,4 +266,62 @@ const clearDatePicker = () => {
},
});
};
+
+const handleExport = (
+ format: ExportFormat,
+ callback: (content: BinaryLike | Blob, format: ExportFormat) => void
+) => {
+ const content = formatExport(format);
+ callback(content, format);
+};
+
+const formatExport = (format: ExportFormat): BinaryLike => {
+ switch (format) {
+ case "CSV":
+ return auditLogList.value
+ .reduce(
+ (list, auditLog) => {
+ list.push(
+ [
+ auditLog.name,
+ auditLog.creator,
+ dayjs(auditLog.createTime).format("YYYY-MM-DD HH:mm:ss Z"),
+ dayjs(auditLog.updateTime).format("YYYY-MM-DD HH:mm:ss Z"),
+ logEntity_ActionToJSON(auditLog.action),
+ logEntity_LevelToJSON(auditLog.level),
+ auditLog.resource,
+ auditLog.comment,
+ auditLog.payload.replaceAll(",", '","'),
+ ].join(",")
+ );
+ return list;
+ },
+ [
+ "name,creator,createTime,updateTime,action,level,resource,comment,payload",
+ ]
+ )
+ .join("\n");
+ case "JSON":
+ return JSON.stringify(
+ auditLogList.value.map((auditLog) => {
+ return {
+ ...auditLog,
+ createTime: dayjs(auditLog.createTime).format(
+ "YYYY-MM-DD HH:mm:ss Z"
+ ),
+ updateTime: dayjs(auditLog.updateTime).format(
+ "YYYY-MM-DD HH:mm:ss Z"
+ ),
+ action: logEntity_ActionToJSON(auditLog.action),
+ level: logEntity_LevelToJSON(auditLog.level),
+ };
+ }),
+ null,
+ 2
+ );
+ default:
+ // Should never reach this line.
+ return "";
+ }
+};
</script>