diff --git a/apps/daas/src/i18n/langs/en.js b/apps/daas/src/i18n/langs/en.js index 850d56421..49f12b636 100644 --- a/apps/daas/src/i18n/langs/en.js +++ b/apps/daas/src/i18n/langs/en.js @@ -261,7 +261,6 @@ export default { dashboard_odh_start_time: 'Start time', dashboard_odh_end_time: 'End time', dashboard_odh_time_range_to: 'to', - dashboard_odh_timezone: 'Time zone: {timezone}', dashboard_odh_node_online: 'Online', dashboard_odh_node_offline: 'Offline', dashboard_odh_node_list_refresh_failed: @@ -3126,19 +3125,42 @@ export default { audit_pending_short: 'awaiting final result', audit_detail_title: 'Audit details', audit_detail_not_found: 'Audit record not found', - audit_filter_user: 'Operator / User ID', - audit_filter_source_ip: 'Source IP', - audit_filter_action: 'Action, e.g. task.start', - audit_filter_object: 'Object name or ID', - audit_filter_event_id: 'Event ID', - audit_filter_operation_id: 'Operation ID', - audit_filter_keyword: 'Keyword: IP, action, object, event ID', - audit_reset_filters: 'Clear filters', + audit_query_quick_range: 'Quick range', + audit_query_time_range: 'Time range', + audit_query_range_last7d: 'Last 7 days', + audit_query_range_today: 'Today', + audit_query_range_last24h: 'Last 24 hours', + audit_query_range_custom: 'Custom', + audit_query_keyword: 'Keyword', + audit_query_keyword_placeholder: 'Search operator, object or action', + audit_query_event_type: 'Event type', + audit_query_outcome: 'Result', + audit_query_all: 'All', + audit_query_more: 'More filters', + audit_query_more_count: 'More filters ({count})', + audit_query_search: 'Search', + audit_query_reset: 'Reset', + audit_query_source: 'Source', + audit_query_user: 'Operator', + audit_query_user_placeholder: 'Enter operator or user ID', + audit_query_source_ip: 'Source IP', + audit_query_source_ip_placeholder: 'e.g. 192.168.1.37', + audit_query_action: 'Action', + audit_query_action_placeholder: 'Select or search action', + audit_query_object: 'Object', + audit_query_object_placeholder: 'Enter object name or ID', + audit_query_applied: 'Filters', + audit_query_pending: 'Changes not applied', + audit_query_refresh: 'Refresh', + audit_query_remove_tag: 'Remove filter {name}', + audit_query_tag: '{label}: {value}', + audit_query_empty: 'No matching logs', + audit_query_invalid_range: + 'Enter a valid time range. Start time must not be later than end time.', audit_export: 'Export CSV', audit_export_confirm: 'Raw audit events matching the current filters will be exported (up to {0} records). The export itself is audited. Continue?', audit_export_failed: 'Export failed', - audit_time_zone: 'Time zone: {0}', audit_write_failure_alert: '{0} audit records failed to be written on this service instance since startup; check the database and service logs', audit_value_empty: '(empty)', diff --git a/apps/daas/src/i18n/langs/zh-CN.js b/apps/daas/src/i18n/langs/zh-CN.js index 7534cbb1e..e8266dd6a 100644 --- a/apps/daas/src/i18n/langs/zh-CN.js +++ b/apps/daas/src/i18n/langs/zh-CN.js @@ -254,7 +254,6 @@ export default { dashboard_odh_start_time: '开始时间', dashboard_odh_end_time: '结束时间', dashboard_odh_time_range_to: '至', - dashboard_odh_timezone: '时区:{timezone}', dashboard_odh_node_online: '在线', dashboard_odh_node_offline: '离线', dashboard_odh_node_list_refresh_failed: '节点列表刷新失败,已保留当前选择。', @@ -2961,19 +2960,42 @@ export default { audit_pending_short: '等待最终结果', audit_detail_title: '审计详情', audit_detail_not_found: '未找到该审计记录', - audit_filter_user: '操作人 / User ID', - audit_filter_source_ip: '来源 IP', - audit_filter_action: '操作,如 task.start', - audit_filter_object: '对象名称或 ID', - audit_filter_event_id: '事件 ID', - audit_filter_operation_id: '操作 ID', - audit_filter_keyword: '关键词:IP、操作、对象、事件 ID', - audit_reset_filters: '清除筛选', + audit_query_quick_range: '快捷范围', + audit_query_time_range: '时间范围', + audit_query_range_last7d: '最近 7 天', + audit_query_range_today: '今天', + audit_query_range_last24h: '最近 24 小时', + audit_query_range_custom: '自定义', + audit_query_keyword: '关键词', + audit_query_keyword_placeholder: '搜索操作人、对象或操作内容', + audit_query_event_type: '事件类型', + audit_query_outcome: '操作结果', + audit_query_all: '全部', + audit_query_more: '更多筛选', + audit_query_more_count: '更多筛选({count})', + audit_query_search: '查询', + audit_query_reset: '重置', + audit_query_source: '来源', + audit_query_user: '操作人', + audit_query_user_placeholder: '输入操作人或用户 ID', + audit_query_source_ip: '来源 IP', + audit_query_source_ip_placeholder: '例如 192.168.1.37', + audit_query_action: '操作内容', + audit_query_action_placeholder: '选择或搜索操作内容', + audit_query_object: '操作对象', + audit_query_object_placeholder: '输入对象名称或 ID', + audit_query_applied: '已选条件', + audit_query_pending: '条件已修改,待查询', + audit_query_refresh: '刷新', + audit_query_remove_tag: '移除条件 {name}', + audit_query_tag: '{label}:{value}', + audit_query_empty: '暂无匹配的日志', + audit_query_invalid_range: + '请填写有效的起止时间,且开始时间不能晚于结束时间。', audit_export: '导出 CSV', audit_export_confirm: '将按当前筛选条件导出原始审计事件(单次最多 {0} 条),导出行为本身会被审计。是否继续?', audit_export_failed: '导出失败', - audit_time_zone: '时区:{0}', audit_write_failure_alert: '本服务实例自启动以来有 {0} 次审计记录写入失败,请检查数据库状态和服务日志', audit_value_empty: '(空)', diff --git a/apps/daas/src/i18n/langs/zh-TW.js b/apps/daas/src/i18n/langs/zh-TW.js index 2b0c43a67..73bce556b 100644 --- a/apps/daas/src/i18n/langs/zh-TW.js +++ b/apps/daas/src/i18n/langs/zh-TW.js @@ -254,7 +254,6 @@ export default { dashboard_odh_start_time: '開始時間', dashboard_odh_end_time: '結束時間', dashboard_odh_time_range_to: '至', - dashboard_odh_timezone: '時區:{timezone}', dashboard_odh_node_online: '線上', dashboard_odh_node_offline: '離線', dashboard_odh_node_list_refresh_failed: @@ -2948,19 +2947,42 @@ export default { audit_pending_short: '等待最終結果', audit_detail_title: '稽核詳情', audit_detail_not_found: '未找到該稽核記錄', - audit_filter_user: '操作人 / User ID', - audit_filter_source_ip: '來源 IP', - audit_filter_action: '操作,如 task.start', - audit_filter_object: '對象名稱或 ID', - audit_filter_event_id: '事件 ID', - audit_filter_operation_id: '操作 ID', - audit_filter_keyword: '關鍵字:IP、操作、對象、事件 ID', - audit_reset_filters: '清除篩選', + audit_query_quick_range: '快捷範圍', + audit_query_time_range: '時間範圍', + audit_query_range_last7d: '最近 7 天', + audit_query_range_today: '今天', + audit_query_range_last24h: '最近 24 小時', + audit_query_range_custom: '自訂', + audit_query_keyword: '關鍵詞', + audit_query_keyword_placeholder: '搜尋操作人、對象或操作內容', + audit_query_event_type: '事件類型', + audit_query_outcome: '操作結果', + audit_query_all: '全部', + audit_query_more: '更多篩選', + audit_query_more_count: '更多篩選({count})', + audit_query_search: '查詢', + audit_query_reset: '重置', + audit_query_source: '來源', + audit_query_user: '操作人', + audit_query_user_placeholder: '輸入操作人或使用者 ID', + audit_query_source_ip: '來源 IP', + audit_query_source_ip_placeholder: '例如 192.168.1.37', + audit_query_action: '操作內容', + audit_query_action_placeholder: '選擇或搜尋操作內容', + audit_query_object: '操作對象', + audit_query_object_placeholder: '輸入對象名稱或 ID', + audit_query_applied: '已選條件', + audit_query_pending: '條件已修改,待查詢', + audit_query_refresh: '重新整理', + audit_query_remove_tag: '移除條件 {name}', + audit_query_tag: '{label}:{value}', + audit_query_empty: '暫無符合的日誌', + audit_query_invalid_range: + '請填寫有效的起訖時間,且開始時間不能晚於結束時間。', audit_export: '匯出 CSV', audit_export_confirm: '將按目前篩選條件匯出原始稽核事件(單次最多 {0} 筆),匯出行為本身會被稽核。是否繼續?', audit_export_failed: '匯出失敗', - audit_time_zone: '時區:{0}', audit_write_failure_alert: '本服務執行個體自啟動以來有 {0} 次稽核記錄寫入失敗,請檢查資料庫狀態和服務日誌', audit_value_empty: '(空)', diff --git a/apps/daas/src/views/audit/AuditLog.vue b/apps/daas/src/views/audit/AuditLog.vue index 55d445ddc..541f524be 100644 --- a/apps/daas/src/views/audit/AuditLog.vue +++ b/apps/daas/src/views/audit/AuditLog.vue @@ -6,53 +6,92 @@ import { } from '@tap/api/src/core/audit-events' import PageContainer from '@tap/business/src/components/PageContainer.vue' import TablePage from '@tap/business/src/components/TablePage.vue' -import { FilterBar } from '@tap/component/src/filter-bar' import dayjs from 'dayjs' import i18n from '@/i18n' import AuditEventDrawer from './AuditEventDrawer.vue' import UserOperation from '@/views/notification/UserOperation.vue' import { + actionMatchesEventType, + advancedFilterCount, + appliedFilterTags, + AUDIT_DEFAULT_RANGE, AUDIT_EVENT_TYPES, AUDIT_FILTER_KEYS, AUDIT_OUTCOMES, + AUDIT_QUERY_LABEL_KEYS, + AUDIT_QUICK_RANGES, AUDIT_SOURCES, + auditActionGroups, + auditActionLabelKey, batchSummary, buildAuditQuery, displayValue, + isQueryDirty, + isValidTimeRange, operatorText, outcomeView, + parseQuickRange, + quickRangeTimes, } from './audit-log' const tr = (key, args) => i18n.t(key, args) -function buildSearchParams(query = {}) { - const base = Object.fromEntries(AUDIT_FILTER_KEYS.map((key) => [key, ''])) - return { ...base, ...query } +function emptyParams() { + return Object.fromEntries(AUDIT_FILTER_KEYS.map((key) => [key, ''])) +} + +/** 路由查询 → 已生效条件;起止时间转为毫秒数值。 */ +function parseApplied(query = {}) { + const params = { ...emptyParams(), ...buildAuditQuery(query) } + for (const key of ['start', 'end']) { + if (params[key] !== '') params[key] = Number(params[key]) + } + return params +} + +/** 路由查询的稳定比较键:值统一为字符串、键排序。 */ +function routeKey(query = {}) { + return JSON.stringify( + Object.entries(query) + .map(([key, value]) => [key, String(value)]) + .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)), + ) +} + +function defaultParams() { + const [start, end] = quickRangeTimes(AUDIT_DEFAULT_RANGE) + return { ...emptyParams(), start, end } } export default { components: { TablePage, - FilterBar, PageContainer, AuditEventDrawer, UserOperation, }, inject: ['hasPermissionByCode'], data() { - const query = { ...this.$route.query } - if (!query.start && !query.end && !query.eventId && !query.operationId) { - query.start = dayjs().subtract(7, 'day').startOf('day').valueOf() - query.end = dayjs().endOf('day').valueOf() - } + const hasTime = this.$route.query.start || this.$route.query.end + const applied = hasTime ? parseApplied(this.$route.query) : defaultParams() return { - filterItems: [], - searchParams: buildSearchParams(query), + applied, + draft: { ...applied }, + appliedRange: hasTime + ? parseQuickRange(this.$route.query) + : AUDIT_DEFAULT_RANGE, + range: hasTime ? parseQuickRange(this.$route.query) : AUDIT_DEFAULT_RANGE, + advancedVisible: false, + rangeError: false, + pendingRoute: '', drawerVisible: false, drawerId: '', exporting: false, status: null, - timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, + eventTypes: AUDIT_EVENT_TYPES, + outcomes: AUDIT_OUTCOMES, + sources: AUDIT_SOURCES, + quickRanges: AUDIT_QUICK_RANGES, } }, computed: { @@ -62,36 +101,122 @@ export default { canExport() { return this.hasPermissionByCode?.('v2_audit_log_export') ?? false }, + timeRange: { + get() { + const { start, end } = this.draft + return start === '' || end === '' ? [] : [start, end] + }, + set(value) { + const [start = '', end = ''] = value || [] + this.draft.start = start === '' ? '' : Number(start) + this.draft.end = end === '' ? '' : Number(end) + this.range = 'custom' + this.rangeError = false + }, + }, + actionGroups() { + return auditActionGroups(this.draft.eventType) + }, + advancedCount() { + return advancedFilterCount(this.applied) + }, + tags() { + return appliedFilterTags(this.applied).map((tag) => ({ + ...tag, + label: tr(AUDIT_QUERY_LABEL_KEYS[tag.key]), + text: this.valueText(tag.key, tag.value), + })) + }, + dirty() { + return ( + isQueryDirty(this.draft, this.applied) || + this.range !== this.appliedRange + ) + }, }, watch: { '$route.query': function (query) { - this.searchParams = buildSearchParams(query) + const external = routeKey(query) !== this.pendingRoute + this.pendingRoute = '' + this.applied = parseApplied(query) + this.appliedRange = parseQuickRange(query) + if (external) { + this.draft = { ...this.applied } + this.range = this.appliedRange + } this.table?.fetch?.(1) }, + 'draft.eventType': function (eventType) { + if (!actionMatchesEventType(this.draft.action, eventType)) { + this.draft.action = '' + } + }, }, created() { - this.getFilterItems() fetchAuditStatus() .then((status) => (this.status = status)) .catch(() => {}) - if (!this.$route.query.start && !this.$route.query.end) this.syncQuery() + if (!this.$route.query.start && !this.$route.query.end) this.applyQuery() }, methods: { - syncQuery() { - const query = buildAuditQuery(this.searchParams) - if ( - JSON.stringify(query) === - JSON.stringify(buildAuditQuery(this.$route.query)) - ) { + valueText(key, value) { + if (key === 'eventType') + return tr(`audit_event_type_${value.toLowerCase()}`) + if (key === 'outcome') return tr(`audit_outcome_${value.toLowerCase()}`) + if (key === 'source') return tr(`audit_source_${value.toLowerCase()}`) + if (key === 'action') return this.actionLabel(value) + return value + }, + actionLabel(action) { + return tr(auditActionLabelKey(action)) + }, + onQuickRangeChange(range) { + const times = quickRangeTimes(range) + if (times) [this.draft.start, this.draft.end] = times + this.rangeError = false + }, + /** 按给定条件写入路由;条件未变化时直接重新拉取第一页。 */ + applyQuery(params = this.draft, range = this.range) { + const query = Object.fromEntries( + Object.entries({ ...buildAuditQuery(params), range }).map( + ([key, value]) => [key, String(value)], + ), + ) + if (routeKey(query) === routeKey(this.$route.query)) { + this.applied = parseApplied(query) + this.appliedRange = range this.table?.fetch?.(1) return } + this.pendingRoute = routeKey(query) this.$router.replace({ name: this.$route.name, query }) }, + search() { + if (!isValidTimeRange(this.draft.start, this.draft.end)) { + this.rangeError = true + return + } + this.rangeError = false + this.applyQuery() + }, + refresh() { + this.table?.fetch?.() + }, + resetFilters() { + this.draft = defaultParams() + this.range = AUDIT_DEFAULT_RANGE + this.rangeError = false + this.applyQuery() + }, + removeTag(key) { + const applied = { ...this.applied, [key]: '' } + this.draft[key] = '' + this.applyQuery(applied, this.appliedRange) + }, getData({ page }) { const { current, size } = page return listAuditOperations({ - ...buildAuditQuery(this.searchParams), + ...buildAuditQuery(this.applied), page: current, size, }).then((data) => ({ @@ -137,83 +262,6 @@ export default { outcomeType: outcome.type, } }, - getFilterItems() { - const options = (prefix, values) => - values.map((value) => ({ - label: tr(`${prefix}${value.toLowerCase()}`), - value, - })) - this.filterItems = [ - { - title: tr('audit_field_time'), - type: 'datetimerange', - key: 'start,end', - }, - { - label: tr('audit_field_event_type'), - key: 'eventType', - type: 'select-inner', - items: options('audit_event_type_', AUDIT_EVENT_TYPES), - }, - { - label: tr('audit_field_source'), - key: 'source', - type: 'select-inner', - items: options('audit_source_', AUDIT_SOURCES), - }, - { - label: tr('audit_field_outcome'), - key: 'outcome', - type: 'select-inner', - items: options('audit_outcome_', AUDIT_OUTCOMES), - }, - { - placeholder: tr('audit_filter_user'), - key: 'user', - type: 'input', - width: '160px', - }, - { - placeholder: tr('audit_filter_source_ip'), - key: 'sourceIp', - type: 'input', - width: '140px', - }, - { - placeholder: tr('audit_filter_action'), - key: 'action', - type: 'input', - width: '150px', - }, - { - placeholder: tr('audit_filter_object'), - key: 'object', - type: 'input', - width: '160px', - }, - { - placeholder: tr('audit_filter_event_id'), - key: 'eventId', - type: 'input', - width: '160px', - }, - { - placeholder: tr('audit_filter_operation_id'), - key: 'operationId', - type: 'input', - width: '160px', - }, - { - placeholder: tr('audit_filter_keyword'), - key: 'keyword', - type: 'input', - }, - ] - }, - resetFilters() { - this.searchParams = buildSearchParams() - this.$router.replace({ name: this.$route.name, query: {} }) - }, openDetail(row) { this.drawerId = row.operationId || row.id this.drawerVisible = true @@ -225,7 +273,7 @@ export default { if (!confirmed) return this.exporting = true try { - await exportAuditEvents(buildAuditQuery(this.searchParams)) + await exportAuditEvents(buildAuditQuery(this.applied)) } catch (error) { this.$message.error(error?.message || tr('audit_export_failed')) } finally { @@ -239,9 +287,6 @@ export default {