responseRateChart.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type {Series} from 'sentry/types/echarts';
  2. import {formatPercentage} from 'sentry/utils/formatters';
  3. import {CHART_HEIGHT} from 'sentry/views/performance/database/settings';
  4. import {
  5. HTTP_RESPONSE_3XX_COLOR,
  6. HTTP_RESPONSE_4XX_COLOR,
  7. HTTP_RESPONSE_5XX_COLOR,
  8. } from 'sentry/views/starfish/colours';
  9. import Chart, {ChartType} from 'sentry/views/starfish/components/chart';
  10. import ChartPanel from 'sentry/views/starfish/components/chartPanel';
  11. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  12. interface Props {
  13. isLoading: boolean;
  14. series: [Series, Series, Series];
  15. error?: Error | null;
  16. }
  17. export function ResponseRateChart({series, isLoading, error}: Props) {
  18. return (
  19. <ChartPanel title={DataTitles.unsuccessfulHTTPCodes}>
  20. <Chart
  21. height={CHART_HEIGHT}
  22. grid={{
  23. left: '4px',
  24. right: '0',
  25. top: '8px',
  26. bottom: '0',
  27. }}
  28. data={series}
  29. loading={isLoading}
  30. error={error}
  31. chartColors={[
  32. HTTP_RESPONSE_3XX_COLOR,
  33. HTTP_RESPONSE_4XX_COLOR,
  34. HTTP_RESPONSE_5XX_COLOR,
  35. ]}
  36. type={ChartType.LINE}
  37. aggregateOutputFormat="percentage"
  38. dataMax={getAxisMaxForPercentageSeries(series)}
  39. tooltipFormatterOptions={{
  40. valueFormatter: value => formatPercentage(value),
  41. }}
  42. />
  43. </ChartPanel>
  44. );
  45. }
  46. /**
  47. * Given a set of `Series` objects that contain percentage data (i.e., every item in `data` has a `value` between 0 and 1) return an appropriate max value.
  48. *
  49. * e.g., for series with very low values (like 5xx rates), it rounds to the nearest significant digit. For other cases, it limits it to 100
  50. */
  51. export function getAxisMaxForPercentageSeries(series: Series[]): number {
  52. const maxValue = Math.max(
  53. ...series.map(serie => Math.max(...serie.data.map(datum => datum.value)))
  54. );
  55. const maxNumberOfDecimalPlaces = Math.ceil(Math.min(0, Math.log10(maxValue)));
  56. return Math.pow(10, maxNumberOfDecimalPlaces);
  57. }