renderHeadCell.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. `${SPS}()`,
  28. `${SPM}()`,
  29. `${TIME_SPENT_PERCENTAGE}()`,
  30. `${TIME_SPENT_PERCENTAGE}(local)`,
  31. `${HTTP_ERROR_COUNT}()`,
  32. ]);
  33. export const renderHeadCell = ({column, location, sort, sortParameterName}: Options) => {
  34. const {key, name} = column;
  35. const alignment = getAlignment(key);
  36. let newSortDirection: Sort['kind'] = 'desc';
  37. if (sort?.field === column.key) {
  38. if (sort.kind === 'desc') {
  39. newSortDirection = 'asc';
  40. }
  41. }
  42. const newSort = `${newSortDirection === 'desc' ? '-' : ''}${key}`;
  43. return (
  44. <SortLink
  45. align={alignment}
  46. canSort={Boolean(location && sort && SORTABLE_FIELDS.has(key))}
  47. direction={sort?.field === column.key ? sort.kind : undefined}
  48. title={name}
  49. generateSortLink={() => {
  50. return {
  51. ...location,
  52. query: {
  53. ...location?.query,
  54. [sortParameterName ?? DEFAULT_SORT_PARAMETER_NAME]: newSort,
  55. },
  56. };
  57. }}
  58. />
  59. );
  60. };
  61. export const getAlignment = (key: string): Alignments => {
  62. const result = parseFunction(key);
  63. if (result) {
  64. const outputType = aggregateFunctionOutputType(result.name, result.arguments[0]);
  65. if (outputType) {
  66. return fieldAlignment(key, outputType);
  67. }
  68. }
  69. return 'left';
  70. };