utils.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Color from 'color';
  2. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  3. import type {Series, SeriesDataUnit} from 'sentry/types/echarts';
  4. import type {Project} from 'sentry/types/project';
  5. import {defined} from 'sentry/utils';
  6. import type {TableData} from 'sentry/utils/discover/discoverQuery';
  7. import type {YAxis} from 'sentry/views/insights/mobile/screenload/constants';
  8. import {YAXIS_COLUMNS} from 'sentry/views/insights/mobile/screenload/constants';
  9. export function isCrossPlatform(project: Project) {
  10. return project.platform && ['react-native', 'flutter'].includes(project.platform);
  11. }
  12. export function transformReleaseEvents({
  13. yAxes,
  14. primaryRelease,
  15. secondaryRelease,
  16. topTransactions,
  17. colorPalette,
  18. releaseEvents,
  19. }: {
  20. colorPalette: string[];
  21. releaseEvents: any;
  22. topTransactions: any;
  23. yAxes: YAxis[];
  24. primaryRelease?: string;
  25. secondaryRelease?: string;
  26. }): {
  27. [yAxisName: string]: {
  28. [releaseVersion: string]: Series;
  29. };
  30. } {
  31. const topTransactionsIndex = Object.fromEntries(topTransactions.map((e, i) => [e, i]));
  32. const transformedReleaseEvents = yAxes.reduce(
  33. (acc, yAxis) => ({...acc, [YAXIS_COLUMNS[yAxis]]: {}}),
  34. {}
  35. );
  36. yAxes.forEach(val => {
  37. [primaryRelease, secondaryRelease].filter(defined).forEach(release => {
  38. transformedReleaseEvents[YAXIS_COLUMNS[val]][release] = {
  39. seriesName: release,
  40. data: Array(topTransactions.length).fill(0),
  41. };
  42. });
  43. });
  44. if (defined(releaseEvents) && defined(primaryRelease)) {
  45. releaseEvents.data?.forEach(row => {
  46. const release = row.release;
  47. const isPrimary = release === primaryRelease;
  48. const transaction = row.transaction;
  49. const index = topTransactionsIndex[transaction];
  50. yAxes.forEach(val => {
  51. if (transformedReleaseEvents[YAXIS_COLUMNS[val]][release]) {
  52. transformedReleaseEvents[YAXIS_COLUMNS[val]][release].data[index] = {
  53. name: row.transaction,
  54. value: row[YAXIS_COLUMNS[val]],
  55. itemStyle: {
  56. color: isPrimary
  57. ? colorPalette[index]
  58. : Color(colorPalette[index]).lighten(0.3).string(),
  59. },
  60. } as SeriesDataUnit;
  61. }
  62. });
  63. });
  64. }
  65. return transformedReleaseEvents;
  66. }
  67. export function transformDeviceClassEvents({
  68. yAxes,
  69. primaryRelease,
  70. secondaryRelease,
  71. data,
  72. }: {
  73. yAxes: YAxis[];
  74. data?: TableData;
  75. primaryRelease?: string;
  76. secondaryRelease?: string;
  77. }): {
  78. [yAxisName: string]: {
  79. [releaseVersion: string]: Series;
  80. };
  81. } {
  82. const transformedData = yAxes.reduce(
  83. (acc, yAxis) => ({...acc, [YAXIS_COLUMNS[yAxis]]: {}}),
  84. {}
  85. );
  86. yAxes.forEach(val => {
  87. transformedData[YAXIS_COLUMNS[val]] = {};
  88. if (primaryRelease) {
  89. transformedData[YAXIS_COLUMNS[val]][primaryRelease] = {
  90. seriesName: primaryRelease,
  91. data: Array(['high', 'medium', 'low', 'Unknown'].length).fill(0),
  92. };
  93. }
  94. if (secondaryRelease) {
  95. transformedData[YAXIS_COLUMNS[val]][secondaryRelease] = {
  96. seriesName: secondaryRelease,
  97. data: Array(['high', 'medium', 'low', 'Unknown'].length).fill(0),
  98. };
  99. }
  100. });
  101. const deviceClassIndex = Object.fromEntries(
  102. ['high', 'medium', 'low', 'Unknown'].map((e, i) => [e, i])
  103. );
  104. if (defined(data)) {
  105. data.data?.forEach(row => {
  106. const deviceClass = row['device.class'];
  107. const index = deviceClassIndex[deviceClass];
  108. const release = row.release;
  109. const isPrimary = release === primaryRelease;
  110. yAxes.forEach(val => {
  111. if (transformedData[YAXIS_COLUMNS[val]][release]) {
  112. transformedData[YAXIS_COLUMNS[val]][release].data[index] = {
  113. name: deviceClass,
  114. value: row[YAXIS_COLUMNS[val]],
  115. itemStyle: {
  116. color: isPrimary ? CHART_PALETTE[3][0] : CHART_PALETTE[3][1],
  117. },
  118. } as SeriesDataUnit;
  119. }
  120. });
  121. });
  122. }
  123. return transformedData;
  124. }