index.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // import { tableColumnMaps } from '@Monitor/constants'
  2. export const getChartTooltipLabel = (serieItem, gorupBy, measurement) => {
  3. const { raw_name = '', tags = {} } = serieItem
  4. if (raw_name && raw_name !== 'unknown-0-value') {
  5. // 从raw_name解析
  6. if (raw_name.startsWith('{') && raw_name.endsWith('}')) {
  7. try {
  8. const str = raw_name.substring(1, raw_name.length - 1)
  9. const list = str.split(',')
  10. const ret = []
  11. list.forEach(item => {
  12. const [key, value] = item.split('=')
  13. ret.push({ key: key, label: `${key}=${value}` })
  14. })
  15. return ret.map(item => item.label).join(',')
  16. } catch (err) { }
  17. } else {
  18. return raw_name
  19. }
  20. }
  21. const keys = Object.keys(tags)
  22. if (keys.length) {
  23. const ret = keys.map(key => ({ key, label: `${key}=${tags[key]}` }))
  24. return ret.map(item => item.label).join(',')
  25. }
  26. return '-'
  27. }
  28. // 添加缺失的时间点
  29. export const addMissingSeries = (series = [], chartQueryData, moment) => {
  30. if (series.length && series.some(item => item.points && item.points.length)) {
  31. return series.map(item => {
  32. const idx = (series[0].columns || []).findIndex(c => c === 'time')
  33. if (idx === -1) return item
  34. const points = item.points || []
  35. if (points.length === 0) return item
  36. const point = points[0]
  37. const pointFrom = point[idx]
  38. const pointTo = item.points[item.points.length - 1][idx]
  39. let { from, to, interval } = chartQueryData
  40. if (interval.includes('h')) {
  41. interval = parseInt(interval.replace('h', '')) * 60 * 60 * 1000
  42. } else if (interval.includes('m')) {
  43. interval = parseInt(interval.replace('m', '')) * 60 * 1000
  44. } else if (interval.includes('s')) {
  45. interval = parseInt(interval.replace('s', '')) * 1000
  46. } else {
  47. interval = parseInt(interval)
  48. }
  49. if (from && to && /^\d+$/.test(from + '') && /^\d+$/.test(to + '')) {
  50. } else if (from && (!to || to === 'now') && /^\d+[h]$/.test(from + '')) {
  51. to = moment.valueOf()
  52. from = moment.clone().subtract(parseInt(from.replace('h', '')), 'hours').valueOf()
  53. } else if (from && to && /^\d+[h]$/.test(from + '') && /^\d+[h]$/.test(to + '')) {
  54. // 使用 clone() 创建副本,避免修改原始 moment 对象
  55. // 先计算 from,再计算 to(因为 to 应该比 from 更晚)
  56. from = moment.clone().subtract(parseInt(from.replace('h', '')), 'hours').valueOf()
  57. to = moment.clone().subtract(parseInt(to.replace('h', '')), 'hours').valueOf()
  58. } else {
  59. // 未正确解析,不处理
  60. return item
  61. }
  62. let lastFrom = pointFrom
  63. let lastTo = pointTo
  64. if (points[1] && points[1][idx] - points[0][idx] === interval * 2) {
  65. interval = interval * 2
  66. }
  67. for (let i = pointFrom; i > from; i -= interval) {
  68. lastFrom = i
  69. if (lastFrom < from) {
  70. break
  71. }
  72. }
  73. for (let i = pointTo; i < to; i += interval) {
  74. lastTo = i
  75. if (lastTo > to) {
  76. break
  77. }
  78. }
  79. const list = []
  80. for (let i = lastFrom; i <= lastTo; i += interval) {
  81. const target = points.findIndex(p => p[idx] === i)
  82. if (target === -1) {
  83. const item = Array.from({ length: point.length }, () => null)
  84. item[idx] = i
  85. list.push(item)
  86. } else {
  87. list.push(points[target])
  88. }
  89. }
  90. list.sort((a, b) => a[idx] - b[idx])
  91. item.points = list
  92. return item
  93. })
  94. }
  95. return series
  96. }