utils.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import type {Location} from 'history';
  2. import {
  3. deleteHomepageQuery,
  4. updateHomepageQuery,
  5. } from 'sentry/actionCreators/discoverHomepageQueries';
  6. import {
  7. createSavedQuery,
  8. deleteSavedQuery,
  9. updateSavedQuery,
  10. } from 'sentry/actionCreators/discoverSavedQueries';
  11. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  12. import type {Client} from 'sentry/api';
  13. import {t} from 'sentry/locale';
  14. import type {NewQuery, Organization, SavedQuery} from 'sentry/types';
  15. import {trackAnalytics} from 'sentry/utils/analytics';
  16. import type {SaveQueryEventParameters} from 'sentry/utils/analytics/discoverAnalyticsEvents';
  17. import type EventView from 'sentry/utils/discover/eventView';
  18. import {
  19. DiscoverDatasets,
  20. DisplayModes,
  21. SavedQueryDatasets,
  22. } from 'sentry/utils/discover/types';
  23. import {decodeScalar} from 'sentry/utils/queryString';
  24. import {DisplayType} from 'sentry/views/dashboards/types';
  25. import {DATASET_PARAM} from 'sentry/views/discover/savedQuery/datasetSelector';
  26. export function handleCreateQuery(
  27. api: Client,
  28. organization: Organization,
  29. eventView: EventView,
  30. yAxis: string[],
  31. // True if this is a brand new query being saved
  32. // False if this is a modification from a saved query
  33. isNewQuery: boolean = true
  34. ): Promise<SavedQuery> {
  35. const payload = eventView.toNewQuery();
  36. payload.yAxis = yAxis;
  37. trackAnalytics(getAnalyticsCreateEventKeyName(isNewQuery, 'request'), {
  38. organization,
  39. ...extractAnalyticsQueryFields(payload),
  40. });
  41. const promise = createSavedQuery(api, organization.slug, payload);
  42. promise
  43. .then((savedQuery: SavedQuery) => {
  44. addSuccessMessage(t('Query saved'));
  45. trackAnalytics(getAnalyticsCreateEventKeyName(isNewQuery, 'success'), {
  46. organization,
  47. ...extractAnalyticsQueryFields(payload),
  48. });
  49. return savedQuery;
  50. })
  51. .catch((err: Error) => {
  52. addErrorMessage(t('Query not saved'));
  53. trackAnalytics(getAnalyticsCreateEventKeyName(isNewQuery, 'failed'), {
  54. organization,
  55. ...extractAnalyticsQueryFields(payload),
  56. error:
  57. err?.message || `Could not save a ${isNewQuery ? 'new' : 'existing'} query`,
  58. });
  59. });
  60. return promise;
  61. }
  62. export function handleUpdateQuery(
  63. api: Client,
  64. organization: Organization,
  65. eventView: EventView,
  66. yAxis: string[]
  67. ): Promise<SavedQuery> {
  68. const payload = eventView.toNewQuery();
  69. payload.yAxis = yAxis;
  70. if (!eventView.name) {
  71. addErrorMessage(t('Please name your query'));
  72. return Promise.reject();
  73. }
  74. trackAnalytics('discover_v2.update_query_request', {
  75. organization,
  76. ...extractAnalyticsQueryFields(payload),
  77. });
  78. const promise = updateSavedQuery(api, organization.slug, payload);
  79. promise
  80. .then((savedQuery: SavedQuery) => {
  81. addSuccessMessage(t('Query updated'));
  82. trackAnalytics('discover_v2.update_query_success', {
  83. organization,
  84. ...extractAnalyticsQueryFields(payload),
  85. });
  86. // NOTE: there is no need to convert _saved into an EventView and push it
  87. // to the browser history, since this.props.eventView already
  88. // derives from location.
  89. return savedQuery;
  90. })
  91. .catch((err: Error) => {
  92. addErrorMessage(t('Query not updated'));
  93. trackAnalytics('discover_v2.update_query_failed', {
  94. organization,
  95. ...extractAnalyticsQueryFields(payload),
  96. error: err?.message || 'Failed to update a query',
  97. });
  98. });
  99. return promise;
  100. }
  101. /**
  102. * Essentially the same as handleUpdateQuery, but specifically for changing the
  103. * name of the query
  104. */
  105. export function handleUpdateQueryName(
  106. api: Client,
  107. organization: Organization,
  108. eventView: EventView
  109. ) {
  110. const payload = eventView.toNewQuery();
  111. trackAnalytics('discover_v2.update_query_name_request', {
  112. organization,
  113. ...extractAnalyticsQueryFields(payload),
  114. });
  115. const promise = updateSavedQuery(api, organization.slug, payload);
  116. promise
  117. .then(_saved => {
  118. addSuccessMessage(t('Query name saved'));
  119. trackAnalytics('discover_v2.update_query_name_successs', {
  120. organization,
  121. ...extractAnalyticsQueryFields(payload),
  122. });
  123. })
  124. .catch((err: Error) => {
  125. addErrorMessage(t('Query name not saved'));
  126. trackAnalytics('discover_v2.update_query_failed', {
  127. organization,
  128. ...extractAnalyticsQueryFields(payload),
  129. error: err?.message || 'Failed to update a query name',
  130. });
  131. });
  132. return promise;
  133. }
  134. export function handleDeleteQuery(
  135. api: Client,
  136. organization: Organization,
  137. eventView: EventView
  138. ): Promise<void> {
  139. trackAnalytics('discover_v2.delete_query_request', {
  140. organization,
  141. ...extractAnalyticsQueryFields(eventView.toNewQuery()),
  142. });
  143. const promise = deleteSavedQuery(api, organization.slug, eventView.id!);
  144. promise
  145. .then(() => {
  146. addSuccessMessage(t('Query deleted'));
  147. trackAnalytics('discover_v2.delete_query_success', {
  148. organization,
  149. ...extractAnalyticsQueryFields(eventView.toNewQuery()),
  150. });
  151. })
  152. .catch((err: Error) => {
  153. addErrorMessage(t('Query not deleted'));
  154. trackAnalytics('discover_v2.delete_query_failed', {
  155. organization,
  156. ...extractAnalyticsQueryFields(eventView.toNewQuery()),
  157. error: err?.message || 'Failed to delete query',
  158. });
  159. });
  160. return promise;
  161. }
  162. export function handleUpdateHomepageQuery(
  163. api: Client,
  164. organization: Organization,
  165. query: NewQuery
  166. ) {
  167. const promise = updateHomepageQuery(api, organization.slug, query);
  168. return promise
  169. .then(savedQuery => {
  170. addSuccessMessage(t('Saved as Discover default'));
  171. return savedQuery;
  172. })
  173. .catch(() => {
  174. addErrorMessage(t('Unable to set query as Discover default'));
  175. });
  176. }
  177. export function handleResetHomepageQuery(api: Client, organization: Organization) {
  178. const promise = deleteHomepageQuery(api, organization.slug);
  179. return promise
  180. .then(() => {
  181. addSuccessMessage(t('Successfully removed Discover default'));
  182. })
  183. .catch(() => {
  184. addErrorMessage(t('Unable to remove Discover default'));
  185. });
  186. }
  187. export function getAnalyticsCreateEventKeyName(
  188. // True if this is a brand new query being saved
  189. // False if this is a modification from a saved query
  190. isNewQuery: boolean,
  191. type: 'request' | 'success' | 'failed'
  192. ): keyof SaveQueryEventParameters {
  193. return (
  194. isNewQuery
  195. ? 'discover_v2.save_new_query_' + type
  196. : 'discover_v2.save_existing_query_' + type
  197. ) as keyof SaveQueryEventParameters;
  198. }
  199. /**
  200. * Takes in a DiscoverV2 NewQuery object and returns a Partial containing
  201. * the desired fields to populate into reload analytics
  202. */
  203. export function extractAnalyticsQueryFields(payload: NewQuery): Partial<NewQuery> {
  204. const {projects, fields, query} = payload;
  205. return {
  206. projects,
  207. fields,
  208. query,
  209. };
  210. }
  211. export function displayModeToDisplayType(displayMode: DisplayModes): DisplayType {
  212. switch (displayMode) {
  213. case DisplayModes.BAR:
  214. return DisplayType.BAR;
  215. case DisplayModes.TOP5:
  216. return DisplayType.TOP_N;
  217. default:
  218. return DisplayType.LINE;
  219. }
  220. }
  221. export function getSavedQueryDataset(
  222. location: Location,
  223. savedQuery: SavedQuery | undefined,
  224. splitDecision?: SavedQueryDatasets
  225. ): SavedQueryDatasets {
  226. const dataset = decodeScalar(location.query[DATASET_PARAM]);
  227. if (dataset) {
  228. return dataset as SavedQueryDatasets;
  229. }
  230. if (savedQuery?.queryDataset === SavedQueryDatasets.DISCOVER && splitDecision) {
  231. return splitDecision;
  232. }
  233. return (savedQuery?.queryDataset ?? SavedQueryDatasets.ERRORS) as SavedQueryDatasets;
  234. }
  235. export function getDatasetFromSavedQueryDataset(
  236. queryDataset: SavedQueryDatasets
  237. ): DiscoverDatasets {
  238. switch (queryDataset) {
  239. case SavedQueryDatasets.ERRORS:
  240. return DiscoverDatasets.ERRORS;
  241. case SavedQueryDatasets.TRANSACTIONS:
  242. return DiscoverDatasets.TRANSACTIONS;
  243. default:
  244. return DiscoverDatasets.DISCOVER;
  245. }
  246. }