utils.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type {Location, LocationDescriptor} from 'history';
  2. import type {Organization} from 'sentry/types/organization';
  3. import type {SpanResult, TraceResult} from './content';
  4. import type {Field} from './data';
  5. export function normalizeTraces(traces: TraceResult<string>[] | undefined) {
  6. if (!traces) {
  7. return traces;
  8. }
  9. return traces.sort(
  10. // Only sort name == null to the end, the rest leave in the original order.
  11. (t1, t2) => (t1.name ? '0' : '1').localeCompare(t2.name ? '0' : '1')
  12. );
  13. }
  14. export function getStylingSliceName(
  15. sliceName: string | null,
  16. sliceSecondaryName: string | null
  17. ) {
  18. if (sliceSecondaryName) {
  19. // Our color picking relies on the first 4 letters. Since we want to differentiate sdknames and project names we have to include part of the sdk name.
  20. return (sliceName ?? '').slice(0, 1) + sliceSecondaryName.slice(-4);
  21. }
  22. return sliceName;
  23. }
  24. export function areQueriesEmpty(queries: string[]): boolean {
  25. if (queries.length > 1) {
  26. return false;
  27. }
  28. if (queries.length === 0) {
  29. return true;
  30. }
  31. if (queries.length === 1) {
  32. return queries[0].length === 0;
  33. }
  34. return false;
  35. }
  36. export function getSecondaryNameFromSpan(span: SpanResult<Field>) {
  37. return span['sdk.name'];
  38. }
  39. export function generateTracesRoute({orgSlug}: {orgSlug: Organization['slug']}): string {
  40. return `/organizations/${orgSlug}/traces/`;
  41. }
  42. export function generateTracesRouteWithQuery({
  43. orgSlug,
  44. metric,
  45. query,
  46. }: {
  47. orgSlug: Organization['slug'];
  48. metric?: {
  49. mri: string;
  50. op: string;
  51. max?: number;
  52. min?: number;
  53. query?: string;
  54. };
  55. query?: Location['query'];
  56. }): LocationDescriptor {
  57. const {
  58. mri,
  59. op: metricsOp,
  60. query: metricsQuery,
  61. max: metricsMax,
  62. min: metricsMin,
  63. } = metric || {};
  64. const pathname = generateTracesRoute({orgSlug});
  65. return {
  66. pathname,
  67. query: {
  68. ...query,
  69. metricsMax,
  70. metricsMin,
  71. metricsOp,
  72. metricsQuery,
  73. mri,
  74. },
  75. };
  76. }
  77. export function getShortenedSdkName(sdkName: string | null) {
  78. if (!sdkName) {
  79. return '';
  80. }
  81. const sdkNameParts = sdkName.split('.');
  82. if (sdkNameParts.length <= 1) {
  83. return sdkName;
  84. }
  85. return sdkNameParts[sdkNameParts.length - 1];
  86. }