renderHeadCell.tsx 2.2 KB

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