zhou zhou
2 天以前 5454bbe86b1a22e9f05b6bc43f7ed7e9d6c4dc14
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
124
125
126
127
128
129
130
131
132
const OPERATION_RESULT_META = {
  1: { textKey: 'pages.system.operationRecord.result.success', fallback: 'Success', type: 'success' },
  0: { textKey: 'pages.system.operationRecord.result.failed', fallback: 'Failed', type: 'danger' }
}
 
export function getOperationRecordReportTitle(t) {
  return typeof t === 'function' ? t('pages.system.operationRecord.reportTitle') : 'Operation Record Report'
}
 
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)] || {
      textKey: 'common.placeholder.empty',
      fallback: '--',
      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 || '',
    resultTextKey: resultMeta.textKey,
    resultText: resultMeta.fallback,
    resultType: resultMeta.type
  }
}
 
export function mergeOperationRecordDetail(detail = {}, fallback = {}) {
  return normalizeOperationRecordRow({
    ...fallback,
    ...detail
  })
}
 
export function getOperationRecordReportColumns(t) {
  return [
    { prop: 'namespace', label: typeof t === 'function' ? t('pages.system.operationRecord.table.namespace') : 'Namespace' },
    { prop: 'url', label: typeof t === 'function' ? t('pages.system.operationRecord.table.url') : 'API URL' },
    { prop: 'userLabel', label: typeof t === 'function' ? t('pages.system.operationRecord.table.user') : 'User' },
    { prop: 'clientIp', label: typeof t === 'function' ? t('pages.system.operationRecord.table.clientIp') : 'Client IP' },
    { prop: 'spendTimeText', label: typeof t === 'function' ? t('pages.system.operationRecord.table.spendTime') : 'Latency (ms)' },
    { prop: 'resultText', label: typeof t === 'function' ? t('pages.system.operationRecord.table.result') : 'Result' },
    { prop: 'timestampText', label: typeof t === 'function' ? t('pages.system.operationRecord.table.timestamp') : 'Operation Time' }
  ]
}
 
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 || '--'
    }
  })
}