widgetQueries.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {useContext} from 'react';
  2. import omit from 'lodash/omit';
  3. import {Client} from 'sentry/api';
  4. import {isMultiSeriesStats} from 'sentry/components/charts/utils';
  5. import {
  6. EventsStats,
  7. MultiSeriesEventsStats,
  8. Organization,
  9. PageFilters,
  10. } from 'sentry/types';
  11. import {Series} from 'sentry/types/echarts';
  12. import {EventsTableData, TableData} from 'sentry/utils/discover/discoverQuery';
  13. import {DURATION_UNITS, SIZE_UNITS} from 'sentry/utils/discover/fieldRenderers';
  14. import {getAggregateAlias} from 'sentry/utils/discover/fields';
  15. import {ErrorsAndTransactionsConfig} from '../datasetConfig/errorsAndTransactions';
  16. import {DashboardFilters, Widget} from '../types';
  17. import {DashboardsMEPContext} from './dashboardsMEPContext';
  18. import GenericWidgetQueries, {
  19. GenericWidgetQueriesChildrenProps,
  20. OnDataFetchedProps,
  21. } from './genericWidgetQueries';
  22. type SeriesResult = EventsStats | MultiSeriesEventsStats;
  23. type TableResult = TableData | EventsTableData;
  24. type SeriesWithOrdering = [order: number, series: Series];
  25. export function transformSeries(
  26. stats: EventsStats,
  27. seriesName: string,
  28. field: string
  29. ): Series {
  30. const unit = stats.meta?.units?.[getAggregateAlias(field)];
  31. // Scale series values to milliseconds or bytes depending on units from meta
  32. const scale = (unit && (DURATION_UNITS[unit] ?? SIZE_UNITS[unit])) ?? 1;
  33. return {
  34. seriesName,
  35. data:
  36. stats?.data?.map(([timestamp, counts]) => {
  37. return {
  38. name: timestamp * 1000,
  39. value: counts.reduce((acc, {count}) => acc + count, 0) * scale,
  40. };
  41. }) ?? [],
  42. };
  43. }
  44. /**
  45. * Multiseries data with a grouping needs to be "flattened" because the aggregate data
  46. * are stored under the group names. These names need to be combined with the aggregate
  47. * names to show a series.
  48. *
  49. * e.g. count() and count_unique() grouped by environment
  50. * {
  51. * "local": {
  52. * "count()": {...},
  53. * "count_unique()": {...}
  54. * },
  55. * "prod": {
  56. * "count()": {...},
  57. * "count_unique()": {...}
  58. * }
  59. * }
  60. */
  61. export function flattenMultiSeriesDataWithGrouping(
  62. result: SeriesResult,
  63. queryAlias: string
  64. ): SeriesWithOrdering[] {
  65. const seriesWithOrdering: SeriesWithOrdering[] = [];
  66. const groupNames = Object.keys(result);
  67. groupNames.forEach(groupName => {
  68. // Each group contains an order key which we should ignore
  69. const aggregateNames = Object.keys(omit(result[groupName], 'order'));
  70. aggregateNames.forEach(aggregate => {
  71. const seriesName = `${groupName} : ${aggregate}`;
  72. const prefixedName = queryAlias ? `${queryAlias} > ${seriesName}` : seriesName;
  73. const seriesData: EventsStats = result[groupName][aggregate];
  74. seriesWithOrdering.push([
  75. result[groupName].order || 0,
  76. transformSeries(seriesData, prefixedName, seriesName),
  77. ]);
  78. });
  79. });
  80. return seriesWithOrdering;
  81. }
  82. export function getIsMetricsDataFromSeriesResponse(
  83. result: SeriesResult
  84. ): boolean | undefined {
  85. const multiIsMetricsData = Object.values(result)
  86. .map(({isMetricsData}) => isMetricsData)
  87. // One non-metrics series will cause all of them to be marked as such
  88. .reduce((acc, value) => (acc === false ? false : value), undefined);
  89. return isMultiSeriesStats(result) ? multiIsMetricsData : result.isMetricsData;
  90. }
  91. type Props = {
  92. api: Client;
  93. children: (props: GenericWidgetQueriesChildrenProps) => JSX.Element;
  94. organization: Organization;
  95. selection: PageFilters;
  96. widget: Widget;
  97. cursor?: string;
  98. dashboardFilters?: DashboardFilters;
  99. limit?: number;
  100. onDataFetched?: (results: OnDataFetchedProps) => void;
  101. };
  102. function WidgetQueries({
  103. api,
  104. children,
  105. organization,
  106. selection,
  107. widget,
  108. dashboardFilters,
  109. cursor,
  110. limit,
  111. onDataFetched,
  112. }: Props) {
  113. const config = ErrorsAndTransactionsConfig;
  114. const context = useContext(DashboardsMEPContext);
  115. let setIsMetricsData: undefined | ((value?: boolean) => void);
  116. if (context) {
  117. setIsMetricsData = context.setIsMetricsData;
  118. }
  119. const isSeriesMetricsDataResults: boolean[] = [];
  120. const afterFetchSeriesData = (rawResults: SeriesResult) => {
  121. if (rawResults.data) {
  122. rawResults = rawResults as EventsStats;
  123. if (rawResults.isMetricsData !== undefined) {
  124. isSeriesMetricsDataResults.push(rawResults.isMetricsData);
  125. }
  126. } else {
  127. Object.keys(rawResults).forEach(key => {
  128. const rawResult: EventsStats = rawResults[key];
  129. if (rawResult.isMetricsData !== undefined) {
  130. isSeriesMetricsDataResults.push(rawResult.isMetricsData);
  131. }
  132. });
  133. }
  134. // If one of the queries is sampled, then mark the whole thing as sampled
  135. setIsMetricsData?.(!isSeriesMetricsDataResults.includes(false));
  136. };
  137. const isTableMetricsDataResults: boolean[] = [];
  138. const afterFetchTableData = (rawResults: TableResult) => {
  139. if (rawResults.meta?.isMetricsData !== undefined) {
  140. isTableMetricsDataResults.push(rawResults.meta.isMetricsData);
  141. }
  142. // If one of the queries is sampled, then mark the whole thing as sampled
  143. setIsMetricsData?.(!isTableMetricsDataResults.includes(false));
  144. };
  145. return (
  146. <GenericWidgetQueries<SeriesResult, TableResult>
  147. config={config}
  148. api={api}
  149. organization={organization}
  150. selection={selection}
  151. widget={widget}
  152. cursor={cursor}
  153. limit={limit}
  154. dashboardFilters={dashboardFilters}
  155. onDataFetched={onDataFetched}
  156. afterFetchSeriesData={afterFetchSeriesData}
  157. afterFetchTableData={afterFetchTableData}
  158. >
  159. {children}
  160. </GenericWidgetQueries>
  161. );
  162. }
  163. export default WidgetQueries;