index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import React from 'react'
  2. import ReactECharts from 'echarts-for-react'
  3. import * as echarts from 'echarts'
  4. import { get } from 'lodash'
  5. import s from './index.less'
  6. import { Descriptions, Divider } from 'antd'
  7. const Page = props => {
  8. const [data, setData] = React.useState({})
  9. React.useEffect(() => {
  10. const { data = {} } = props
  11. setData(data)
  12. }, [])
  13. const {
  14. // saveAsImage = {
  15. // icon:
  16. // 'image://https://cdn.weipaitang.com/static/public/20210824acf87486-499f-7486499f-1d32-40d793b1fd05.svg'
  17. // }
  18. saveAsImage = false,
  19. showTitle = true,
  20. showLine = false
  21. } = props
  22. function getOptions (title, xData = [], yData = [], {
  23. yFormat = '{value}',
  24. tooltipFormat,
  25. yMax,
  26. showAreaStyle
  27. } = {}) {
  28. return {
  29. tooltip: {
  30. trigger: 'axis',
  31. position: function (pt) {
  32. return [pt[0], '10%']
  33. },
  34. formatter: function (params) {
  35. let rez = `<div>${params[0].axisValue}</div>`
  36. params.forEach(item => {
  37. const strItem = `<div style="display: flex; justify-content: space-between;">
  38. <div style="padding-right: 10px">
  39. ${item.marker} ${item.seriesName}
  40. </div>
  41. <div style="color: #666;">
  42. ${tooltipFormat ? tooltipFormat(item.value) : item.value}
  43. </div>
  44. </div>`
  45. rez += strItem
  46. })
  47. return rez
  48. }
  49. },
  50. grid: {
  51. left: '60px',
  52. right: '28px',
  53. containLabel: true
  54. },
  55. title: {
  56. left: 'center',
  57. text: title,
  58. textStyle: {
  59. fontSize: 16,
  60. fontWeight: 'normal',
  61. color: '#666'
  62. }
  63. },
  64. toolbox: {
  65. feature: {
  66. saveAsImage: saveAsImage
  67. }
  68. },
  69. xAxis: {
  70. type: 'category',
  71. boundaryGap: false,
  72. data: xData
  73. },
  74. yAxis: {
  75. type: 'value',
  76. boundaryGap: [0, '100%'],
  77. axisLabel: {
  78. show: true,
  79. interval: 'auto',
  80. formatter: yFormat
  81. },
  82. max: yMax
  83. },
  84. dataZoom: [
  85. {
  86. type: 'inside',
  87. start: 0,
  88. end: 100
  89. },
  90. {
  91. start: 0,
  92. end: 100
  93. }
  94. ],
  95. series: yData.map((yItem, index) => {
  96. const h = ((yData.length - index) * (255 / yData.length)) >> 0
  97. return {
  98. name: yItem.name,
  99. type: 'line',
  100. symbol: 'none',
  101. sampling: 'lttb',
  102. itemStyle: {
  103. color: yData.length === 1 ? 'rgb(255, 70, 131)' : `hsl(${h}, 100%, 64%)`
  104. },
  105. ...(
  106. showAreaStyle ? {
  107. areaStyle: {
  108. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  109. {
  110. offset: 0,
  111. color: 'rgb(255, 158, 68)'
  112. },
  113. {
  114. offset: 1,
  115. color: 'rgb(255, 70, 131)'
  116. }
  117. ])
  118. }
  119. } : {}
  120. ),
  121. data: yItem.data
  122. }
  123. })
  124. }
  125. }
  126. function getSeriesYData (data, format) {
  127. const _data = data
  128. return Object.keys(_data).map(key => {
  129. return {
  130. name: key,
  131. data: _data[key].vertical.map(item => {
  132. if (format) {
  133. return format(item)
  134. } else {
  135. return item
  136. }
  137. })
  138. }
  139. })
  140. }
  141. function getSeriesXData (data) {
  142. return Object.keys(data).reduce((acc, key) => {
  143. return data[key].cross
  144. }, {})
  145. }
  146. return (
  147. <div>
  148. {
  149. showLine && (
  150. <Divider />
  151. )
  152. }
  153. {
  154. showTitle && (
  155. <div className={s.title}>
  156. {data.taskName}-压测报告
  157. </div>
  158. )
  159. }
  160. <div className={s.descriptionWrap}>
  161. <Descriptions bordered>
  162. <Descriptions.Item label="压测参数" span={24}>
  163. -t {data.threads} -c {data.connects} -d {data.duration} -timeout {data.timeout}</Descriptions.Item>
  164. <Descriptions.Item label="服务器配置" span={8}>{get(data, 'monitor.infoResult')}</Descriptions.Item>
  165. <Descriptions.Item label="部署类型" span={8}>{get(data, 'monitor.deployType')}</Descriptions.Item>
  166. <Descriptions.Item label="QPS">{get(data, 'report.summary_req_sec')}</Descriptions.Item>
  167. <Descriptions.Item label="平均延时">{get(data, 'report.latency_mean')}</Descriptions.Item>
  168. <Descriptions.Item label="99% 延时">{get(data, 'report.latency_p99')}</Descriptions.Item>
  169. </Descriptions>
  170. </div>
  171. <ReactECharts
  172. option={getOptions('压力持续图', get(data, 'pressure_data.cross'), [
  173. { name: '数据', data: get(data, 'pressure_data.vertical') }
  174. ], { showAreaStyle: true })}
  175. />
  176. <div className={s.chartWrap}>
  177. <div className={s.chartItem}>
  178. <ReactECharts
  179. option={getOptions(
  180. 'CPU 使用率',
  181. getSeriesXData(get(data, 'monitor.metrics.node_cpu_seconds_total') || {}),
  182. getSeriesYData(get(data, 'monitor.metrics.node_cpu_seconds_total') || {}),
  183. {
  184. yFormat: function (value) {
  185. return value + '%'
  186. },
  187. tooltipFormat: function (value) {
  188. return value + '%'
  189. },
  190. yMax: 100
  191. }
  192. )}
  193. />
  194. </div>
  195. <div className={s.chartItem}>
  196. <ReactECharts
  197. option={getOptions(
  198. '内存使用率',
  199. getSeriesXData(get(data, 'monitor.metrics.memUsage') || {}),
  200. getSeriesYData(get(data, 'monitor.metrics.memUsage') || {}),
  201. {
  202. yFormat: function (value) {
  203. return value + '%'
  204. },
  205. tooltipFormat: function (value) {
  206. return value + '%'
  207. },
  208. yMax: 100
  209. }
  210. )}
  211. />
  212. </div>
  213. </div>
  214. <div className={s.chartWrap}>
  215. <div className={s.chartItem}>
  216. <ReactECharts
  217. option={getOptions(
  218. '磁盘读取',
  219. getSeriesXData(get(data, 'monitor.metrics.node_disk_read_bytes_total') || {}),
  220. getSeriesYData(get(data, 'monitor.metrics.node_disk_read_bytes_total') || {}, value => (value / 1024).toFixed(2)),
  221. {
  222. yFormat: function (value) {
  223. return value + 'kb/s'
  224. },
  225. tooltipFormat: function (value) {
  226. return value + 'kb/s'
  227. }
  228. }
  229. )}
  230. />
  231. </div>
  232. <div className={s.chartItem}>
  233. <ReactECharts
  234. option={getOptions(
  235. '磁盘写入',
  236. getSeriesXData(get(data, 'monitor.metrics.node_disk_written_bytes_total') || {}),
  237. getSeriesYData(get(data, 'monitor.metrics.node_disk_written_bytes_total') || {}, value => (value / 1024).toFixed(2)),
  238. {
  239. yFormat: function (value) {
  240. return value + 'kb/s'
  241. },
  242. tooltipFormat: function (value) {
  243. return value + 'kb/s'
  244. }
  245. }
  246. )}
  247. />
  248. </div>
  249. </div>
  250. <div className={s.chartWrap}>
  251. <div className={s.chartItem}>
  252. <ReactECharts
  253. option={getOptions(
  254. 'FPM',
  255. getSeriesXData(get(data, 'monitor.metrics.phpfpm_processes_total') || {}),
  256. getSeriesYData(get(data, 'monitor.metrics.phpfpm_processes_total') || {})
  257. )}
  258. />
  259. </div>
  260. </div>
  261. </div>
  262. )
  263. }
  264. export default Page