utils.tsx 6.8 KB

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