renderHeadCell.tsx 2.0 KB

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