utils.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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[] | readonly 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(
  32. topTransactions.map((e: any, i: any) => [e, i])
  33. );
  34. const transformedReleaseEvents = yAxes.reduce(
  35. (acc, yAxis) => ({...acc, [YAXIS_COLUMNS[yAxis]]: {}}),
  36. {}
  37. );
  38. yAxes.forEach(val => {
  39. [primaryRelease, secondaryRelease].filter(defined).forEach(release => {
  40. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  41. transformedReleaseEvents[YAXIS_COLUMNS[val]][release] = {
  42. seriesName: release,
  43. data: Array(topTransactions.length).fill(0),
  44. };
  45. });
  46. });
  47. if (defined(releaseEvents) && defined(primaryRelease)) {
  48. releaseEvents.data?.forEach((row: any) => {
  49. const release = row.release;
  50. const isPrimary = release === primaryRelease;
  51. const transaction = row.transaction;
  52. const index = topTransactionsIndex[transaction];
  53. yAxes.forEach(val => {
  54. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  55. if (transformedReleaseEvents[YAXIS_COLUMNS[val]][release]) {
  56. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  57. transformedReleaseEvents[YAXIS_COLUMNS[val]][release].data[index] = {
  58. name: row.transaction,
  59. value: row[YAXIS_COLUMNS[val]],
  60. itemStyle: {
  61. color: isPrimary
  62. ? colorPalette[index]
  63. : Color(colorPalette[index]).lighten(0.3).string(),
  64. },
  65. } as SeriesDataUnit;
  66. }
  67. });
  68. });
  69. }
  70. return transformedReleaseEvents;
  71. }
  72. export function transformDeviceClassEvents({
  73. yAxes,
  74. primaryRelease,
  75. secondaryRelease,
  76. data,
  77. }: {
  78. yAxes: YAxis[];
  79. data?: TableData;
  80. primaryRelease?: string;
  81. secondaryRelease?: string;
  82. }): {
  83. [yAxisName: string]: {
  84. [releaseVersion: string]: Series;
  85. };
  86. } {
  87. const transformedData = yAxes.reduce(
  88. (acc, yAxis) => ({...acc, [YAXIS_COLUMNS[yAxis]]: {}}),
  89. {}
  90. );
  91. yAxes.forEach(val => {
  92. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  93. transformedData[YAXIS_COLUMNS[val]] = {};
  94. if (primaryRelease) {
  95. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  96. transformedData[YAXIS_COLUMNS[val]][primaryRelease] = {
  97. seriesName: primaryRelease,
  98. data: Array(['high', 'medium', 'low', 'Unknown'].length).fill(0),
  99. };
  100. }
  101. if (secondaryRelease) {
  102. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  103. transformedData[YAXIS_COLUMNS[val]][secondaryRelease] = {
  104. seriesName: secondaryRelease,
  105. data: Array(['high', 'medium', 'low', 'Unknown'].length).fill(0),
  106. };
  107. }
  108. });
  109. const deviceClassIndex = Object.fromEntries(
  110. ['high', 'medium', 'low', 'Unknown'].map((e, i) => [e, i])
  111. );
  112. if (defined(data)) {
  113. data.data?.forEach(row => {
  114. const deviceClass = row['device.class']!;
  115. const index = deviceClassIndex[deviceClass];
  116. const release = row.release;
  117. const isPrimary = release === primaryRelease;
  118. yAxes.forEach(val => {
  119. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  120. if (transformedData[YAXIS_COLUMNS[val]][release]) {
  121. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  122. transformedData[YAXIS_COLUMNS[val]][release].data[index] = {
  123. name: deviceClass,
  124. value: row[YAXIS_COLUMNS[val]],
  125. itemStyle: {
  126. color: isPrimary ? CHART_PALETTE[3][0] : CHART_PALETTE[3][1],
  127. },
  128. } as SeriesDataUnit;
  129. }
  130. });
  131. });
  132. }
  133. return transformedData;
  134. }