renderHeadCell.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 {SpanMetricsFields} 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. };
  17. const {SPAN_SELF_TIME} = SpanMetricsFields;
  18. export const SORTABLE_FIELDS = new Set([
  19. `avg(${SPAN_SELF_TIME})`,
  20. `p95(${SPAN_SELF_TIME})`,
  21. 'sps()',
  22. 'time_spent_percentage()',
  23. 'http_error_count()',
  24. ]);
  25. export const renderHeadCell = ({column, location, sort}: Options) => {
  26. const {key, name} = column;
  27. const alignment = getAlignment(key);
  28. let newSortDirection: Sort['kind'] = 'desc';
  29. if (sort?.field === column.key) {
  30. if (sort.kind === 'desc') {
  31. newSortDirection = 'asc';
  32. }
  33. }
  34. const newSort = `${newSortDirection === 'desc' ? '-' : ''}${key}`;
  35. return (
  36. <SortLink
  37. align={alignment}
  38. canSort={Boolean(location && sort && SORTABLE_FIELDS.has(key))}
  39. direction={sort?.field === column.key ? sort.kind : undefined}
  40. title={name}
  41. generateSortLink={() => {
  42. return {
  43. ...location,
  44. query: {
  45. ...location?.query,
  46. [QueryParameterNames.SORT]: newSort,
  47. },
  48. };
  49. }}
  50. />
  51. );
  52. };
  53. export const getAlignment = (key: string): Alignments => {
  54. const result = parseFunction(key);
  55. if (result) {
  56. const outputType = aggregateFunctionOutputType(result.name, result.arguments[0]);
  57. if (outputType) {
  58. return fieldAlignment(key, outputType);
  59. }
  60. }
  61. return 'left';
  62. };