zhou zhou
2 天以前 aaf8a50511d77dbc209ca93bbba308c21179a8bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const OPERATION_RESULT_META = {
  1: { text: '成功', type: 'success' },
  0: { text: '失败', type: 'danger' }
}
 
export const OPERATION_RECORD_REPORT_TITLE = '操作日志报表'
 
export function createOperationRecordSearchState() {
  return {
    condition: '',
    namespace: '',
    url: '',
    clientIp: '',
    result: '',
    timeStart: '',
    timeEnd: ''
  }
}
 
export function getOperationRecordPaginationKey() {
  return {
    current: 'current',
    size: 'pageSize'
  }
}
 
export function buildOperationRecordSearchParams(params = {}) {
  return {
    condition: String(params.condition || '').trim(),
    namespace: String(params.namespace || '').trim(),
    url: String(params.url || '').trim(),
    clientIp: String(params.clientIp || '').trim(),
    ...(params.result !== '' && params.result !== null && params.result !== undefined
      ? { result: Number(params.result) }
      : {}),
    ...(params.timeStart ? { timeStart: params.timeStart } : {}),
    ...(params.timeEnd ? { timeEnd: params.timeEnd } : {})
  }
}
 
export function buildOperationRecordPageQueryParams(params = {}) {
  return {
    current: params.current || 1,
    pageSize: params.pageSize || params.size || 20,
    ...buildOperationRecordSearchParams(params)
  }
}
 
export function getOperationResultMeta(value) {
  return OPERATION_RESULT_META[Number(value)] || { text: '-', type: 'info' }
}
 
export function formatOperationTimestamp(value) {
  if (value === '' || value === null || value === undefined) {
    return ''
  }
 
  const timestamp = Number(value)
  const date = Number.isNaN(timestamp) ? new Date(value) : new Date(timestamp)
  if (Number.isNaN(date.getTime())) {
    return String(value)
  }
 
  const pad = (segment) => String(segment).padStart(2, '0')
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(
    date.getMinutes()
  )}:${pad(date.getSeconds())}`
}
 
export function normalizeOperationRecordRow(record = {}) {
  const resultMeta = getOperationResultMeta(record.result)
  return {
    ...record,
    namespace: record.namespace || '',
    url: record.url || '',
    appkey: record.appkey || '',
    request: record.request || '',
    response: record.response || '',
    err: record.err || '',
    memo: record.memo || '',
    clientIp: record.clientIp || '',
    spendTime: record.spendTime ?? 0,
    userLabel: record['userId$'] || record.userLabel || (record.userId ? String(record.userId) : ''),
    timestampText: formatOperationTimestamp(record.timestamp),
    createTimeText: record['createTime$'] || record.createTime || '',
    resultText: resultMeta.text,
    resultType: resultMeta.type
  }
}
 
export function mergeOperationRecordDetail(detail = {}, fallback = {}) {
  return normalizeOperationRecordRow({
    ...fallback,
    ...detail
  })
}
 
export function getOperationRecordReportColumns() {
  return [
    { prop: 'namespace', label: '名称空间' },
    { prop: 'url', label: '接口地址' },
    { prop: 'userLabel', label: '操作用户' },
    { prop: 'clientIp', label: '客户端IP' },
    { prop: 'spendTimeText', label: '耗时(ms)' },
    { prop: 'resultText', label: '结果' },
    { prop: 'timestampText', label: '操作时间' }
  ]
}
 
export function buildOperationRecordPrintRows(records = []) {
  return records.map((record) => {
    const row = normalizeOperationRecordRow(record)
    return {
      namespace: row.namespace || '--',
      url: row.url || '--',
      userLabel: row.userLabel || '--',
      clientIp: row.clientIp || '--',
      spendTimeText: row.spendTime ?? '--',
      resultText: row.resultText || '--',
      timestampText: row.timestampText || '--'
    }
  })
}