userStats.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location, LocationDescriptor} from 'history';
  4. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  5. import {SectionHeading} from 'sentry/components/charts/styles';
  6. import Link from 'sentry/components/links/link';
  7. import Placeholder from 'sentry/components/placeholder';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import UserMisery from 'sentry/components/userMisery';
  10. import {IconOpen} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import type {Organization} from 'sentry/types/organization';
  13. import type EventView from 'sentry/utils/discover/eventView';
  14. import type {QueryError} from 'sentry/utils/discover/genericDiscoverQuery';
  15. import {WebVital} from 'sentry/utils/fields';
  16. import {useMetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  17. import {useMEPSettingContext} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  18. import {decodeScalar} from 'sentry/utils/queryString';
  19. import {useModuleURL} from 'sentry/views/insights/common/utils/useModuleURL';
  20. import {ModuleName} from 'sentry/views/insights/types';
  21. import {getTermHelp, PerformanceTerm} from 'sentry/views/performance/data';
  22. import {getTransactionMEPParamsIfApplicable} from 'sentry/views/performance/transactionSummary/transactionOverview/utils';
  23. import {vitalsRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionVitals/utils';
  24. import {SidebarSpacer} from 'sentry/views/performance/transactionSummary/utils';
  25. import VitalInfo from 'sentry/views/performance/vitalDetail/vitalInfo';
  26. type Props = {
  27. error: QueryError | null;
  28. eventView: EventView;
  29. hasWebVitals: boolean;
  30. isLoading: boolean;
  31. location: Location;
  32. organization: Organization;
  33. totals: Record<string, number> | null;
  34. transactionName: string;
  35. };
  36. function UserStats({
  37. isLoading,
  38. hasWebVitals,
  39. error,
  40. totals,
  41. location,
  42. organization,
  43. transactionName,
  44. eventView,
  45. }: Props) {
  46. const hasTransactionSummaryCleanupFlag = organization.features.includes(
  47. 'performance-transaction-summary-cleanup'
  48. );
  49. const webVitalsUrl = useModuleURL(ModuleName.VITAL, false, 'frontend');
  50. const hasWebVitalsFlag = organization.features.includes('insights-initial-modules');
  51. let userMisery = error !== null ? <div>{'\u2014'}</div> : <Placeholder height="34px" />;
  52. if (!isLoading && error === null && totals) {
  53. const threshold: number | undefined = totals.project_threshold_config
  54. ? totals.project_threshold_config[1]
  55. : undefined;
  56. const miserableUsers: number | undefined = totals['count_miserable_user()'];
  57. const userMiseryScore: number = totals['user_misery()'] || 0;
  58. const totalUsers = totals['count_unique_user()'];
  59. userMisery = (
  60. <UserMisery
  61. bars={40}
  62. barHeight={30}
  63. userMisery={userMiseryScore}
  64. miseryLimit={threshold}
  65. totalUsers={totalUsers}
  66. miserableUsers={miserableUsers}
  67. />
  68. );
  69. }
  70. const orgSlug = organization.slug;
  71. let webVitalsTarget: LocationDescriptor = vitalsRouteWithQuery({
  72. orgSlug,
  73. transaction: transactionName,
  74. projectID: decodeScalar(location.query.project),
  75. query: location.query,
  76. });
  77. if (hasWebVitalsFlag) {
  78. webVitalsTarget = {
  79. pathname: `${webVitalsUrl}/overview/`,
  80. query: {
  81. transaction: transactionName,
  82. },
  83. };
  84. }
  85. const showLink = hasWebVitalsFlag;
  86. const mepSetting = useMEPSettingContext();
  87. const mepCardinalityContext = useMetricsCardinalityContext();
  88. const queryExtras = getTransactionMEPParamsIfApplicable(
  89. mepSetting,
  90. mepCardinalityContext,
  91. organization
  92. );
  93. return (
  94. <Fragment>
  95. {hasWebVitals && (
  96. <Fragment>
  97. <VitalsHeading>
  98. <SectionHeading>
  99. {t('Web Vitals')}
  100. <QuestionTooltip
  101. position="top"
  102. title={t(
  103. 'Web Vitals with p75 better than the "poor" threshold, as defined by Google Web Vitals.'
  104. )}
  105. size="sm"
  106. />
  107. </SectionHeading>
  108. {showLink && (
  109. <Link to={webVitalsTarget}>
  110. <IconOpen />
  111. </Link>
  112. )}
  113. </VitalsHeading>
  114. <VitalInfo
  115. location={location}
  116. vital={[WebVital.FCP, WebVital.LCP, WebVital.FID, WebVital.CLS]}
  117. orgSlug={orgSlug}
  118. environment={eventView.environment}
  119. start={eventView.start}
  120. end={eventView.end}
  121. statsPeriod={eventView.statsPeriod}
  122. project={eventView.project}
  123. hideVitalThresholds
  124. hideDurationDetail
  125. queryExtras={queryExtras}
  126. />
  127. <SidebarSpacer />
  128. </Fragment>
  129. )}
  130. {!hasTransactionSummaryCleanupFlag && (
  131. <Fragment>
  132. <GuideAnchor target="user_misery" position="left">
  133. <SectionHeading>
  134. {t('User Misery')}
  135. <QuestionTooltip
  136. position="top"
  137. title={getTermHelp(organization, PerformanceTerm.USER_MISERY)}
  138. size="sm"
  139. />
  140. </SectionHeading>
  141. </GuideAnchor>
  142. {userMisery}
  143. </Fragment>
  144. )}
  145. <SidebarSpacer />
  146. </Fragment>
  147. );
  148. }
  149. const VitalsHeading = styled('div')`
  150. display: flex;
  151. justify-content: space-between;
  152. align-items: center;
  153. `;
  154. export default UserStats;