releaseSeries.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import {Component} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import {withTheme} from '@emotion/react';
  5. import {Query} from 'history';
  6. import isEqual from 'lodash/isEqual';
  7. import memoize from 'lodash/memoize';
  8. import partition from 'lodash/partition';
  9. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  10. import {Client, ResponseMeta} from 'sentry/api';
  11. import MarkLine from 'sentry/components/charts/components/markLine';
  12. import {t} from 'sentry/locale';
  13. import {DateString, Organization} from 'sentry/types';
  14. import {Series} from 'sentry/types/echarts';
  15. import {escape} from 'sentry/utils';
  16. import {getFormattedDate, getUtcDateString} from 'sentry/utils/dates';
  17. import {formatVersion} from 'sentry/utils/formatters';
  18. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  19. import {Theme} from 'sentry/utils/theme';
  20. import withApi from 'sentry/utils/withApi';
  21. import withOrganization from 'sentry/utils/withOrganization';
  22. type ReleaseMetaBasic = {
  23. date: string;
  24. version: string;
  25. };
  26. type ReleaseConditions = {
  27. end: DateString;
  28. environment: Readonly<string[]>;
  29. project: Readonly<number[]>;
  30. start: DateString;
  31. cursor?: string;
  32. query?: string;
  33. statsPeriod?: string | null;
  34. };
  35. // This is not an exported action/function because releases list uses AsyncComponent
  36. // and this is not re-used anywhere else afaict
  37. function getOrganizationReleases(
  38. api: Client,
  39. organization: Organization,
  40. conditions: ReleaseConditions
  41. ) {
  42. const query = {};
  43. Object.keys(conditions).forEach(key => {
  44. let value = conditions[key];
  45. if (value && (key === 'start' || key === 'end')) {
  46. value = getUtcDateString(value);
  47. }
  48. if (value) {
  49. query[key] = value;
  50. }
  51. });
  52. api.clear();
  53. return api.requestPromise(`/organizations/${organization.slug}/releases/stats/`, {
  54. includeAllArgs: true,
  55. method: 'GET',
  56. query,
  57. }) as Promise<[ReleaseMetaBasic[], any, ResponseMeta]>;
  58. }
  59. type Props = WithRouterProps & {
  60. api: Client;
  61. children: (s: State) => React.ReactNode;
  62. end: DateString;
  63. environments: Readonly<string[]>;
  64. organization: Organization;
  65. projects: Readonly<number[]>;
  66. start: DateString;
  67. theme: Theme;
  68. emphasizeReleases?: string[];
  69. memoized?: boolean;
  70. period?: string | null;
  71. preserveQueryParams?: boolean;
  72. query?: string;
  73. queryExtra?: Query;
  74. releases?: ReleaseMetaBasic[] | null;
  75. tooltip?: Exclude<Parameters<typeof MarkLine>[0], undefined>['tooltip'];
  76. utc?: boolean | null;
  77. };
  78. type State = {
  79. releaseSeries: Series[];
  80. releases: ReleaseMetaBasic[] | null;
  81. };
  82. class ReleaseSeries extends Component<Props, State> {
  83. state: State = {
  84. releases: null,
  85. releaseSeries: [],
  86. };
  87. componentDidMount() {
  88. this._isMounted = true;
  89. const {releases} = this.props;
  90. if (releases) {
  91. // No need to fetch releases if passed in from props
  92. this.setReleasesWithSeries(releases);
  93. return;
  94. }
  95. this.fetchData();
  96. }
  97. componentDidUpdate(prevProps) {
  98. if (
  99. !isEqual(prevProps.projects, this.props.projects) ||
  100. !isEqual(prevProps.environments, this.props.environments) ||
  101. !isEqual(prevProps.start, this.props.start) ||
  102. !isEqual(prevProps.end, this.props.end) ||
  103. !isEqual(prevProps.period, this.props.period) ||
  104. !isEqual(prevProps.query, this.props.query)
  105. ) {
  106. this.fetchData();
  107. } else if (!isEqual(prevProps.emphasizeReleases, this.props.emphasizeReleases)) {
  108. this.setReleasesWithSeries(this.state.releases);
  109. }
  110. }
  111. componentWillUnmount() {
  112. this._isMounted = false;
  113. this.props.api.clear();
  114. }
  115. _isMounted: boolean = false;
  116. getOrganizationReleasesMemoized = memoize(
  117. (api: Client, organization: Organization, conditions: ReleaseConditions) =>
  118. getOrganizationReleases(api, organization, conditions),
  119. (_, __, conditions) =>
  120. Object.values(conditions)
  121. .map(val => JSON.stringify(val))
  122. .join('-')
  123. );
  124. async fetchData() {
  125. const {
  126. api,
  127. organization,
  128. projects,
  129. environments,
  130. period,
  131. start,
  132. end,
  133. memoized,
  134. query,
  135. } = this.props;
  136. const conditions: ReleaseConditions = {
  137. start,
  138. end,
  139. project: projects,
  140. environment: environments,
  141. statsPeriod: period,
  142. query,
  143. };
  144. let hasMore = true;
  145. const releases: ReleaseMetaBasic[] = [];
  146. while (hasMore) {
  147. try {
  148. const getReleases = memoized
  149. ? this.getOrganizationReleasesMemoized
  150. : getOrganizationReleases;
  151. const [newReleases, , resp] = await getReleases(api, organization, conditions);
  152. releases.push(...newReleases);
  153. if (this._isMounted) {
  154. this.setReleasesWithSeries(releases);
  155. }
  156. const pageLinks = resp?.getResponseHeader('Link');
  157. if (pageLinks) {
  158. const paginationObject = parseLinkHeader(pageLinks);
  159. hasMore = paginationObject?.next?.results ?? false;
  160. conditions.cursor = paginationObject.next.cursor;
  161. } else {
  162. hasMore = false;
  163. }
  164. } catch {
  165. addErrorMessage(t('Error fetching releases'));
  166. hasMore = false;
  167. }
  168. }
  169. }
  170. setReleasesWithSeries(releases) {
  171. const {emphasizeReleases = []} = this.props;
  172. const releaseSeries: Series[] = [];
  173. if (emphasizeReleases.length) {
  174. const [unemphasizedReleases, emphasizedReleases] = partition(
  175. releases,
  176. release => !emphasizeReleases.includes(release.version)
  177. );
  178. if (unemphasizedReleases.length) {
  179. releaseSeries.push(this.getReleaseSeries(unemphasizedReleases, {type: 'dotted'}));
  180. }
  181. if (emphasizedReleases.length) {
  182. releaseSeries.push(
  183. this.getReleaseSeries(emphasizedReleases, {
  184. opacity: 0.8,
  185. })
  186. );
  187. }
  188. } else {
  189. releaseSeries.push(this.getReleaseSeries(releases));
  190. }
  191. this.setState({
  192. releases,
  193. releaseSeries,
  194. });
  195. }
  196. getReleaseSeries = (releases, lineStyle = {}) => {
  197. const {
  198. organization,
  199. router,
  200. tooltip,
  201. environments,
  202. start,
  203. end,
  204. period,
  205. preserveQueryParams,
  206. queryExtra,
  207. theme,
  208. } = this.props;
  209. const query = {...queryExtra};
  210. if (organization.features.includes('global-views')) {
  211. query.project = router.location.query.project;
  212. }
  213. if (preserveQueryParams) {
  214. query.environment = [...environments];
  215. query.start = start ? getUtcDateString(start) : undefined;
  216. query.end = end ? getUtcDateString(end) : undefined;
  217. query.statsPeriod = period || undefined;
  218. }
  219. const markLine = MarkLine({
  220. animation: false,
  221. lineStyle: {
  222. color: theme.purple300,
  223. opacity: 0.3,
  224. type: 'solid',
  225. ...lineStyle,
  226. },
  227. label: {
  228. show: false,
  229. },
  230. data: releases.map(release => ({
  231. xAxis: +new Date(release.date),
  232. name: formatVersion(release.version, true),
  233. value: formatVersion(release.version, true),
  234. onClick: () => {
  235. router.push({
  236. pathname: `/organizations/${organization.slug}/releases/${release.version}/`,
  237. query,
  238. });
  239. },
  240. label: {
  241. formatter: () => formatVersion(release.version, true),
  242. },
  243. })),
  244. tooltip: tooltip || {
  245. trigger: 'item',
  246. formatter: ({data}: any) => {
  247. // XXX using this.props here as this function does not get re-run
  248. // unless projects are changed. Using a closure variable would result
  249. // in stale values.
  250. const time = getFormattedDate(data.value, 'MMM D, YYYY LT', {
  251. local: !this.props.utc,
  252. });
  253. const version = escape(formatVersion(data.name, true));
  254. return [
  255. '<div class="tooltip-series">',
  256. `<div><span class="tooltip-label"><strong>${t(
  257. 'Release'
  258. )}</strong></span> ${version}</div>`,
  259. '</div>',
  260. '<div class="tooltip-date">',
  261. time,
  262. '</div>',
  263. '</div>',
  264. '<div class="tooltip-arrow"></div>',
  265. ].join('');
  266. },
  267. },
  268. });
  269. return {
  270. seriesName: 'Releases',
  271. color: theme.purple200,
  272. data: [],
  273. markLine,
  274. };
  275. };
  276. render() {
  277. const {children} = this.props;
  278. return children({
  279. releases: this.state.releases,
  280. releaseSeries: this.state.releaseSeries,
  281. });
  282. }
  283. }
  284. export default withRouter(withOrganization(withApi(withTheme(ReleaseSeries))));