parseMetricWidgetsQueryParam.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import {getDefaultMetricOp} from 'sentry/utils/metrics';
  2. import {
  3. DEFAULT_SORT_STATE,
  4. emptyMetricsQueryWidget,
  5. NO_QUERY_ID,
  6. } from 'sentry/utils/metrics/constants';
  7. import {isMRI} from 'sentry/utils/metrics/mri';
  8. import {
  9. type BaseWidgetParams,
  10. type FocusedMetricsSeries,
  11. MetricDisplayType,
  12. type MetricFormulaWidgetParams,
  13. MetricQueryType,
  14. type MetricQueryWidgetParams,
  15. type MetricWidgetQueryParams,
  16. type SortState,
  17. } from 'sentry/utils/metrics/types';
  18. import {getUniqueQueryIdGenerator} from 'sentry/views/ddm/utils/uniqueQueryId';
  19. function isRecord(value: unknown): value is Record<string, unknown> {
  20. return typeof value === 'object' && value !== null && !Array.isArray(value);
  21. }
  22. function isMetricDisplayType(value: unknown): value is MetricDisplayType {
  23. return Object.values(MetricDisplayType).includes(value as MetricDisplayType);
  24. }
  25. function getMRIParam(widget: Record<string, unknown>) {
  26. return 'mri' in widget && isMRI(widget.mri) ? widget.mri : undefined;
  27. }
  28. function parseStringParam(
  29. widget: Record<string, unknown>,
  30. key: string
  31. ): string | undefined {
  32. const value = widget[key];
  33. return typeof value === 'string' ? value : undefined;
  34. }
  35. function parseNumberParam(
  36. widget: Record<string, unknown>,
  37. key: string
  38. ): number | undefined {
  39. const value = widget[key];
  40. return typeof value === 'number' && !Number.isNaN(value) ? value : undefined;
  41. }
  42. function parseBooleanParam(
  43. widget: Record<string, unknown>,
  44. key: string
  45. ): boolean | undefined {
  46. const value = widget[key];
  47. return typeof value === 'boolean' ? value : undefined;
  48. }
  49. function parseArrayParam<T extends Exclude<any, undefined>>(
  50. widget: object,
  51. key: string,
  52. entryParser: (entry: unknown) => T | undefined
  53. ): T[] {
  54. if (!(key in widget)) {
  55. return [];
  56. }
  57. // allow single values instead of arrays
  58. if (!Array.isArray(widget[key])) {
  59. const entry = entryParser(widget[key]);
  60. return entry === undefined ? [] : [entry];
  61. }
  62. return widget[key].map(entryParser).filter((entry): entry is T => entry !== undefined);
  63. }
  64. function parseFocusedSeries(series: any): FocusedMetricsSeries | undefined {
  65. if (!isRecord(series)) {
  66. return undefined;
  67. }
  68. const id = parseStringParam(series, 'id');
  69. const groupBy =
  70. 'groupBy' in series && isRecord(series.groupBy)
  71. ? (series.groupBy as Record<string, string>)
  72. : undefined;
  73. if (!id) {
  74. return undefined;
  75. }
  76. return {id, groupBy};
  77. }
  78. function parseSortParam(widget: Record<string, unknown>, key: string): SortState {
  79. const sort = widget[key];
  80. if (!isRecord(sort)) {
  81. return DEFAULT_SORT_STATE;
  82. }
  83. const name = parseStringParam(sort, 'name');
  84. const order =
  85. 'order' in sort && (sort.order === 'desc' || sort.order === 'asc')
  86. ? sort.order
  87. : DEFAULT_SORT_STATE.order;
  88. if (
  89. name === 'name' ||
  90. name === 'avg' ||
  91. name === 'min' ||
  92. name === 'max' ||
  93. name === 'sum'
  94. ) {
  95. return {name, order};
  96. }
  97. return {name: undefined, order};
  98. }
  99. function isValidId(n: number | undefined): n is number {
  100. return n !== undefined && Number.isInteger(n) && n >= 0;
  101. }
  102. function parseQueryType(
  103. widget: Record<string, unknown>,
  104. key: string
  105. ): MetricQueryType | undefined {
  106. const value = widget[key];
  107. return typeof value === 'number' && Object.values(MetricQueryType).includes(value)
  108. ? value
  109. : undefined;
  110. }
  111. function parseQueryWidget(
  112. widget: Record<string, unknown>,
  113. baseWidgetParams: BaseWidgetParams
  114. ): MetricQueryWidgetParams | null {
  115. const mri = getMRIParam(widget);
  116. // If we cannot retrieve an MRI, there is nothing to display
  117. if (!mri) {
  118. return null;
  119. }
  120. return {
  121. mri,
  122. op: parseStringParam(widget, 'op') ?? getDefaultMetricOp(mri),
  123. query: parseStringParam(widget, 'query') ?? '',
  124. groupBy: parseArrayParam(widget, 'groupBy', entry =>
  125. typeof entry === 'string' ? entry : undefined
  126. ),
  127. powerUserMode: parseBooleanParam(widget, 'powerUserMode') ?? false,
  128. ...baseWidgetParams,
  129. type: MetricQueryType.QUERY,
  130. };
  131. }
  132. function parseFormulaWidget(
  133. widget: Record<string, unknown>,
  134. baseWidgetParams: BaseWidgetParams
  135. ): MetricFormulaWidgetParams | null {
  136. const formula = parseStringParam(widget, 'formula');
  137. // If we cannot retrieve a formula, there is nothing to display
  138. if (formula === undefined) {
  139. return null;
  140. }
  141. return {
  142. formula,
  143. ...baseWidgetParams,
  144. type: MetricQueryType.FORMULA,
  145. };
  146. }
  147. export function parseMetricWidgetsQueryParam(
  148. queryParam?: string
  149. ): MetricWidgetQueryParams[] {
  150. let currentWidgets: unknown = undefined;
  151. try {
  152. currentWidgets = JSON.parse(queryParam || '');
  153. } catch (_) {
  154. currentWidgets = [];
  155. }
  156. // It has to be an array and non-empty
  157. if (!Array.isArray(currentWidgets)) {
  158. currentWidgets = [];
  159. }
  160. const usedIds = new Set<number>();
  161. const indezesWithoutId = new Set<number>();
  162. const parsedWidgets = (currentWidgets as unknown[]).map(
  163. (widget: unknown, index): MetricWidgetQueryParams | null => {
  164. if (!isRecord(widget)) {
  165. return null;
  166. }
  167. const type = parseQueryType(widget, 'type') ?? MetricQueryType.QUERY;
  168. const id = parseNumberParam(widget, 'id');
  169. if (!isValidId(id)) {
  170. indezesWithoutId.add(index);
  171. } else if (usedIds.has(id)) {
  172. // We drop qidgets with duplicate ids
  173. return null;
  174. } else {
  175. usedIds.add(id);
  176. }
  177. const displayType = parseStringParam(widget, 'displayType');
  178. const baseWidgetParams: BaseWidgetParams = {
  179. type,
  180. id: !isValidId(id) ? NO_QUERY_ID : id,
  181. displayType: isMetricDisplayType(displayType)
  182. ? displayType
  183. : MetricDisplayType.LINE,
  184. focusedSeries: parseArrayParam(widget, 'focusedSeries', parseFocusedSeries),
  185. sort: parseSortParam(widget, 'sort'),
  186. };
  187. switch (type) {
  188. case MetricQueryType.QUERY:
  189. return parseQueryWidget(widget, baseWidgetParams);
  190. case MetricQueryType.FORMULA:
  191. return parseFormulaWidget(widget, baseWidgetParams);
  192. default:
  193. return null;
  194. }
  195. }
  196. );
  197. // Iterate over the widgets without an id and assign them a unique one
  198. if (indezesWithoutId.size > 0) {
  199. const generateId = getUniqueQueryIdGenerator(usedIds);
  200. for (const index of indezesWithoutId) {
  201. const widget = parsedWidgets[index];
  202. if (!widget) {
  203. continue;
  204. }
  205. widget.id = generateId.next().value;
  206. }
  207. }
  208. const filteredWidgets = parsedWidgets.filter(
  209. (widget): widget is MetricWidgetQueryParams => widget !== null
  210. );
  211. if (filteredWidgets.length === 0) {
  212. filteredWidgets.push(emptyMetricsQueryWidget);
  213. }
  214. // We can reset the id if there is only one widget
  215. if (filteredWidgets.length === 1) {
  216. filteredWidgets[0].id = 0;
  217. }
  218. return filteredWidgets;
  219. }