resourceSummaryCharts.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {t} from 'sentry/locale';
  2. import type {Series} from 'sentry/types/echarts';
  3. import {formatBytesBase2} from 'sentry/utils';
  4. import {formatRate} from 'sentry/utils/formatters';
  5. import getDynamicText from 'sentry/utils/getDynamicText';
  6. import {RESOURCE_THROUGHPUT_UNIT} from 'sentry/views/performance/browser/resources';
  7. import {useResourceModuleFilters} from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  8. import {AVG_COLOR, THROUGHPUT_COLOR} from 'sentry/views/starfish/colours';
  9. import Chart from 'sentry/views/starfish/components/chart';
  10. import ChartPanel from 'sentry/views/starfish/components/chartPanel';
  11. import {useSpanMetricsSeries} from 'sentry/views/starfish/queries/useSpanMetricsSeries';
  12. import {SpanMetricsField} from 'sentry/views/starfish/types';
  13. import {
  14. DataTitles,
  15. getDurationChartTitle,
  16. getThroughputChartTitle,
  17. } from 'sentry/views/starfish/views/spans/types';
  18. import {Block, BlockContainer} from 'sentry/views/starfish/views/spanSummaryPage/block';
  19. const {
  20. SPAN_SELF_TIME,
  21. HTTP_RESPONSE_CONTENT_LENGTH,
  22. HTTP_DECODED_RESPONSE_CONTENT_LENGTH,
  23. HTTP_RESPONSE_TRANSFER_SIZE,
  24. RESOURCE_RENDER_BLOCKING_STATUS,
  25. } = SpanMetricsField;
  26. function ResourceSummaryCharts(props: {groupId: string}) {
  27. const filters = useResourceModuleFilters();
  28. // console.log({
  29. // ...(filters[RESOURCE_RENDER_BLOCKING_STATUS]
  30. // ? {[RESOURCE_RENDER_BLOCKING_STATUS]: filters[RESOURCE_RENDER_BLOCKING_STATUS]}
  31. // : {}),
  32. // });
  33. const {data: spanMetricsSeriesData, isLoading: areSpanMetricsSeriesLoading} =
  34. useSpanMetricsSeries({
  35. filters: {
  36. 'span.group': props.groupId,
  37. ...(filters[RESOURCE_RENDER_BLOCKING_STATUS]
  38. ? {[RESOURCE_RENDER_BLOCKING_STATUS]: filters[RESOURCE_RENDER_BLOCKING_STATUS]}
  39. : {}),
  40. },
  41. yAxis: [
  42. `spm()`,
  43. `avg(${SPAN_SELF_TIME})`,
  44. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  45. `avg(${HTTP_DECODED_RESPONSE_CONTENT_LENGTH})`,
  46. `avg(${HTTP_RESPONSE_TRANSFER_SIZE})`,
  47. ],
  48. });
  49. if (spanMetricsSeriesData) {
  50. spanMetricsSeriesData[`avg(${HTTP_RESPONSE_TRANSFER_SIZE})`].lineStyle = {
  51. type: 'dashed',
  52. };
  53. spanMetricsSeriesData[`avg(${HTTP_DECODED_RESPONSE_CONTENT_LENGTH})`].lineStyle = {
  54. type: 'dashed',
  55. };
  56. }
  57. return (
  58. <BlockContainer>
  59. <Block>
  60. <ChartPanel title={getThroughputChartTitle('http', RESOURCE_THROUGHPUT_UNIT)}>
  61. <Chart
  62. height={160}
  63. data={[spanMetricsSeriesData?.[`spm()`]]}
  64. loading={areSpanMetricsSeriesLoading}
  65. isLineChart
  66. definedAxisTicks={4}
  67. aggregateOutputFormat="rate"
  68. rateUnit={RESOURCE_THROUGHPUT_UNIT}
  69. stacked
  70. chartColors={[THROUGHPUT_COLOR]}
  71. tooltipFormatterOptions={{
  72. valueFormatter: value => formatRate(value, RESOURCE_THROUGHPUT_UNIT),
  73. nameFormatter: () => t('Requests'),
  74. }}
  75. />
  76. </ChartPanel>
  77. </Block>
  78. <Block>
  79. <ChartPanel title={getDurationChartTitle('http')}>
  80. <Chart
  81. height={160}
  82. data={[spanMetricsSeriesData?.[`avg(${SPAN_SELF_TIME})`]]}
  83. loading={areSpanMetricsSeriesLoading}
  84. chartColors={[AVG_COLOR]}
  85. isLineChart
  86. definedAxisTicks={4}
  87. />
  88. </ChartPanel>
  89. </Block>
  90. <Block>
  91. <ChartPanel title={t('Average Resource Size')}>
  92. <Chart
  93. height={160}
  94. aggregateOutputFormat="size"
  95. data={[
  96. spanMetricsSeriesData?.[`avg(${HTTP_DECODED_RESPONSE_CONTENT_LENGTH})`],
  97. spanMetricsSeriesData?.[`avg(${HTTP_RESPONSE_TRANSFER_SIZE})`],
  98. spanMetricsSeriesData?.[`avg(${HTTP_RESPONSE_CONTENT_LENGTH})`],
  99. ]}
  100. loading={areSpanMetricsSeriesLoading}
  101. chartColors={[AVG_COLOR]}
  102. isLineChart
  103. definedAxisTicks={4}
  104. tooltipFormatterOptions={{
  105. valueFormatter: bytes =>
  106. getDynamicText({
  107. value: formatBytesBase2(bytes),
  108. fixed: 'xx KiB',
  109. }),
  110. nameFormatter: name => DataTitles[name],
  111. }}
  112. />
  113. </ChartPanel>
  114. </Block>
  115. </BlockContainer>
  116. );
  117. }
  118. /**
  119. * Ensures a series has no zeros between two non-zero datapoints. This is useful in
  120. * @param series the series to fill
  121. * @returns a reference to the initial series filled
  122. */
  123. export const fillSeries = (series: Series): Series => {
  124. if (!series.data.length) {
  125. return series;
  126. }
  127. let lastSeenValue = series.data[0].value;
  128. return {
  129. ...series,
  130. data: series.data.map(dataPoint => {
  131. const value = dataPoint.value;
  132. if (value !== lastSeenValue && value !== 0) {
  133. lastSeenValue = value;
  134. return {...dataPoint};
  135. }
  136. return {...dataPoint, value: lastSeenValue};
  137. }),
  138. };
  139. };
  140. export default ResourceSummaryCharts;