userStats.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. const hasDomainViewFlag = organization.features.includes('insights-domain-view');
  52. let userMisery = error !== null ? <div>{'\u2014'}</div> : <Placeholder height="34px" />;
  53. if (!isLoading && error === null && totals) {
  54. const threshold: number | undefined = totals.project_threshold_config
  55. ? totals.project_threshold_config[1]
  56. : undefined;
  57. const miserableUsers: number | undefined = totals['count_miserable_user()'];
  58. const userMiseryScore: number = totals['user_misery()'] || 0;
  59. const totalUsers = totals['count_unique_user()'];
  60. userMisery = (
  61. <UserMisery
  62. bars={40}
  63. barHeight={30}
  64. userMisery={userMiseryScore}
  65. miseryLimit={threshold}
  66. totalUsers={totalUsers}
  67. miserableUsers={miserableUsers}
  68. />
  69. );
  70. }
  71. const orgSlug = organization.slug;
  72. let webVitalsTarget: LocationDescriptor = vitalsRouteWithQuery({
  73. orgSlug,
  74. transaction: transactionName,
  75. projectID: decodeScalar(location.query.project),
  76. query: location.query,
  77. });
  78. if (hasWebVitalsFlag && hasDomainViewFlag) {
  79. webVitalsTarget = {
  80. pathname: `${webVitalsUrl}/overview/`,
  81. query: {
  82. transaction: transactionName,
  83. },
  84. };
  85. }
  86. const showLink = !hasDomainViewFlag || (hasDomainViewFlag && hasWebVitalsFlag);
  87. const mepSetting = useMEPSettingContext();
  88. const mepCardinalityContext = useMetricsCardinalityContext();
  89. const queryExtras = getTransactionMEPParamsIfApplicable(
  90. mepSetting,
  91. mepCardinalityContext,
  92. organization
  93. );
  94. return (
  95. <Fragment>
  96. {hasWebVitals && (
  97. <Fragment>
  98. <VitalsHeading>
  99. <SectionHeading>
  100. {t('Web Vitals')}
  101. <QuestionTooltip
  102. position="top"
  103. title={t(
  104. 'Web Vitals with p75 better than the "poor" threshold, as defined by Google Web Vitals.'
  105. )}
  106. size="sm"
  107. />
  108. </SectionHeading>
  109. {showLink && (
  110. <Link to={webVitalsTarget}>
  111. <IconOpen />
  112. </Link>
  113. )}
  114. </VitalsHeading>
  115. <VitalInfo
  116. location={location}
  117. vital={[WebVital.FCP, WebVital.LCP, WebVital.FID, WebVital.CLS]}
  118. orgSlug={orgSlug}
  119. environment={eventView.environment}
  120. start={eventView.start}
  121. end={eventView.end}
  122. statsPeriod={eventView.statsPeriod}
  123. project={eventView.project}
  124. hideVitalThresholds
  125. hideDurationDetail
  126. queryExtras={queryExtras}
  127. />
  128. <SidebarSpacer />
  129. </Fragment>
  130. )}
  131. {!hasTransactionSummaryCleanupFlag && (
  132. <Fragment>
  133. <GuideAnchor target="user_misery" position="left">
  134. <SectionHeading>
  135. {t('User Misery')}
  136. <QuestionTooltip
  137. position="top"
  138. title={getTermHelp(organization, PerformanceTerm.USER_MISERY)}
  139. size="sm"
  140. />
  141. </SectionHeading>
  142. </GuideAnchor>
  143. {userMisery}
  144. </Fragment>
  145. )}
  146. <SidebarSpacer />
  147. </Fragment>
  148. );
  149. }
  150. const VitalsHeading = styled('div')`
  151. display: flex;
  152. justify-content: space-between;
  153. align-items: center;
  154. `;
  155. export default UserStats;