renderHeadCell.tsx 1.9 KB

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