utils.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {defined} from 'sentry/utils';
  2. import type {NetworkSpan} from 'sentry/views/replays/types';
  3. export interface ISortConfig {
  4. asc: boolean;
  5. by: keyof NetworkSpan | string;
  6. getValue: (row: NetworkSpan) => any;
  7. }
  8. export const UNKNOWN_STATUS = 'unknown';
  9. export function sortNetwork(
  10. network: NetworkSpan[],
  11. sortConfig: ISortConfig
  12. ): NetworkSpan[] {
  13. return [...network].sort((a, b) => {
  14. let valueA = sortConfig.getValue(a);
  15. let valueB = sortConfig.getValue(b);
  16. valueA = typeof valueA === 'string' ? valueA.toUpperCase() : valueA;
  17. valueB = typeof valueB === 'string' ? valueB.toUpperCase() : valueB;
  18. // if the values are not defined, we want to push them to the bottom of the list
  19. if (!defined(valueA)) {
  20. return 1;
  21. }
  22. if (!defined(valueB)) {
  23. return -1;
  24. }
  25. if (valueA === valueB) {
  26. return 0;
  27. }
  28. if (sortConfig.asc) {
  29. return valueA > valueB ? 1 : -1;
  30. }
  31. return valueB > valueA ? 1 : -1;
  32. });
  33. }
  34. export const getResourceTypes = (networkSpans: NetworkSpan[]) =>
  35. Array.from(
  36. new Set(networkSpans.map(networkSpan => networkSpan.op.replace('resource.', '')))
  37. ).sort();
  38. export const getStatusTypes = (networkSpans: NetworkSpan[]) =>
  39. Array.from(
  40. new Set(
  41. networkSpans
  42. .map(networkSpan => networkSpan.data.statusCode ?? UNKNOWN_STATUS)
  43. .map(String)
  44. )
  45. ).sort();