renderHeadCell.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import type {Location} from 'history';
  2. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  3. import type {Alignments} from 'sentry/components/gridEditable/sortLink';
  4. import SortLink from 'sentry/components/gridEditable/sortLink';
  5. import type {Sort} from 'sentry/utils/discover/fields';
  6. import {
  7. aggregateFunctionOutputType,
  8. fieldAlignment,
  9. parseFunction,
  10. } from 'sentry/utils/discover/fields';
  11. import {
  12. SpanFunction,
  13. SpanIndexedField,
  14. SpanMetricsField,
  15. } from 'sentry/views/starfish/types';
  16. import type {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  17. type Options = {
  18. column: GridColumnHeader<string>;
  19. location?: Location;
  20. sort?: Sort;
  21. sortParameterName?: QueryParameterNames | typeof DEFAULT_SORT_PARAMETER_NAME;
  22. };
  23. const DEFAULT_SORT_PARAMETER_NAME = 'sort';
  24. const {SPAN_SELF_TIME, HTTP_RESPONSE_CONTENT_LENGTH} = SpanMetricsField;
  25. const {RESPONSE_CODE} = SpanIndexedField;
  26. const {TIME_SPENT_PERCENTAGE, SPS, SPM, HTTP_ERROR_COUNT, HTTP_RESPONSE_RATE} =
  27. SpanFunction;
  28. export const SORTABLE_FIELDS = new Set([
  29. `avg(${SPAN_SELF_TIME})`,
  30. `p95(${SPAN_SELF_TIME})`,
  31. `p75(transaction.duration)`,
  32. `transaction.duration`,
  33. 'transaction',
  34. `count()`,
  35. `${SPS}()`,
  36. `${SPM}()`,
  37. `${TIME_SPENT_PERCENTAGE}()`,
  38. `${HTTP_ERROR_COUNT}()`,
  39. `${HTTP_RESPONSE_RATE}(2)`,
  40. `${HTTP_RESPONSE_RATE}(4)`,
  41. `${HTTP_RESPONSE_RATE}(5)`,
  42. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  43. ]);
  44. const NUMERIC_FIELDS = new Set([`${RESPONSE_CODE}`]);
  45. export const renderHeadCell = ({column, location, sort, sortParameterName}: Options) => {
  46. const {key, name} = column;
  47. const alignment = getAlignment(key);
  48. let newSortDirection: Sort['kind'] = 'desc';
  49. if (sort?.field === column.key) {
  50. if (sort.kind === 'desc') {
  51. newSortDirection = 'asc';
  52. }
  53. }
  54. const newSort = `${newSortDirection === 'desc' ? '-' : ''}${key}`;
  55. return (
  56. <SortLink
  57. align={alignment}
  58. canSort={Boolean(location && sort && SORTABLE_FIELDS.has(key))}
  59. direction={sort?.field === column.key ? sort.kind : undefined}
  60. title={name}
  61. generateSortLink={() => {
  62. return {
  63. ...location,
  64. query: {
  65. ...location?.query,
  66. [sortParameterName ?? DEFAULT_SORT_PARAMETER_NAME]: newSort,
  67. },
  68. };
  69. }}
  70. />
  71. );
  72. };
  73. export const getAlignment = (key: string): Alignments => {
  74. const result = parseFunction(key);
  75. if (result) {
  76. const outputType = aggregateFunctionOutputType(result.name, result.arguments[0]);
  77. if (outputType) {
  78. return fieldAlignment(key, outputType);
  79. }
  80. } else {
  81. if (NUMERIC_FIELDS.has(key)) {
  82. return 'right';
  83. }
  84. }
  85. return 'left';
  86. };