metric-utils.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { includes } from 'lodash';
  2. import { scaleLog } from 'd3-scale';
  3. import React from 'react';
  4. import { formatMetricSvg } from './string-utils';
  5. import { colors } from './color-utils';
  6. export function getClipPathDefinition(clipId, height, radius) {
  7. const barHeight = 1 - (2 * height); // in the interval [-1, 1]
  8. return (
  9. <defs>
  10. <clipPath id={clipId} transform={`scale(${2 * radius})`}>
  11. <rect width={2} height={2} x={-1} y={barHeight} />
  12. </clipPath>
  13. </defs>
  14. );
  15. }
  16. //
  17. // loadScale(1) == 0.5; E.g. a nicely balanced system :).
  18. const loadScale = scaleLog().domain([0.01, 100]).range([0, 1]);
  19. export function getMetricValue(metric) {
  20. if (!metric) {
  21. return { formattedValue: 'n/a', height: 0, value: null };
  22. }
  23. const m = metric.toJS();
  24. const { value } = m;
  25. let valuePercentage = value === 0 ? 0 : value / m.max;
  26. let { max } = m;
  27. if (includes(['load1', 'load5', 'load15'], m.id)) {
  28. valuePercentage = loadScale(value);
  29. max = null;
  30. }
  31. let displayedValue = Number(value);
  32. if (displayedValue > 0 && (!max || displayedValue < max)) {
  33. const baseline = 0.1;
  34. displayedValue = (valuePercentage * (1 - (baseline * 2))) + baseline;
  35. } else if (displayedValue >= m.max && displayedValue > 0) {
  36. displayedValue = 1;
  37. }
  38. return {
  39. formattedValue: formatMetricSvg(value, m),
  40. hasMetric: value !== null,
  41. height: displayedValue
  42. };
  43. }
  44. export function getMetricColor(metric) {
  45. const metricId = typeof metric === 'string'
  46. ? metric
  47. : metric && metric.get('id');
  48. if (/mem/.test(metricId)) {
  49. return 'steelBlue';
  50. } if (/cpu/.test(metricId)) {
  51. return colors('cpu').toString();
  52. } if (/files/.test(metricId)) {
  53. // purple
  54. // return '#9467bd';
  55. return '#ff4949'
  56. } if (/load/.test(metricId)) {
  57. return colors('load').toString();
  58. }if (/error/.test(metricId)||/failed/.test(metricId)) {
  59. //错误是红色
  60. return '#ff4949';
  61. }
  62. //原先颜色默认颜色
  63. // return 'steelBlue';
  64. return '#33CCA2'
  65. }