renderHeadCell.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, CACHE_ITEM_SIZE} = SpanIndexedField;
  26. const {
  27. TIME_SPENT_PERCENTAGE,
  28. SPS,
  29. SPM,
  30. HTTP_ERROR_COUNT,
  31. HTTP_RESPONSE_RATE,
  32. CACHE_HIT_RATE,
  33. CACHE_MISS_RATE,
  34. } = SpanFunction;
  35. export const SORTABLE_FIELDS = new Set([
  36. `avg(${SPAN_SELF_TIME})`,
  37. `p95(${SPAN_SELF_TIME})`,
  38. `p75(transaction.duration)`,
  39. `transaction.duration`,
  40. 'transaction',
  41. `count()`,
  42. `${SPS}()`,
  43. `${SPM}()`,
  44. `${TIME_SPENT_PERCENTAGE}()`,
  45. `${HTTP_ERROR_COUNT}()`,
  46. `${HTTP_RESPONSE_RATE}(2)`,
  47. `${HTTP_RESPONSE_RATE}(4)`,
  48. `${HTTP_RESPONSE_RATE}(5)`,
  49. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  50. `${CACHE_HIT_RATE}()`,
  51. `${CACHE_MISS_RATE}()`,
  52. ]);
  53. const NUMERIC_FIELDS = new Set([
  54. `${RESPONSE_CODE}`,
  55. CACHE_ITEM_SIZE,
  56. 'transaction.duration',
  57. ]);
  58. export const renderHeadCell = ({column, location, sort, sortParameterName}: Options) => {
  59. const {key, name} = column;
  60. const alignment = getAlignment(key);
  61. let newSortDirection: Sort['kind'] = 'desc';
  62. if (sort?.field === column.key) {
  63. if (sort.kind === 'desc') {
  64. newSortDirection = 'asc';
  65. }
  66. }
  67. const newSort = `${newSortDirection === 'desc' ? '-' : ''}${key}`;
  68. return (
  69. <SortLink
  70. align={alignment}
  71. canSort={Boolean(location && sort && SORTABLE_FIELDS.has(key))}
  72. direction={sort?.field === column.key ? sort.kind : undefined}
  73. title={name}
  74. generateSortLink={() => {
  75. return {
  76. ...location,
  77. query: {
  78. ...location?.query,
  79. [sortParameterName ?? DEFAULT_SORT_PARAMETER_NAME]: newSort,
  80. },
  81. };
  82. }}
  83. />
  84. );
  85. };
  86. export const getAlignment = (key: string): Alignments => {
  87. const result = parseFunction(key);
  88. if (result) {
  89. const outputType = aggregateFunctionOutputType(result.name, result.arguments[0]);
  90. if (outputType) {
  91. return fieldAlignment(key, outputType);
  92. }
  93. } else {
  94. if (NUMERIC_FIELDS.has(key)) {
  95. return 'right';
  96. }
  97. }
  98. return 'left';
  99. };