aiMonitoringDetailsPage.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import styled from '@emotion/styled';
  2. import Feature from 'sentry/components/acl/feature';
  3. import {Alert} from 'sentry/components/alert';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import NoProjectMessage from 'sentry/components/noProjectMessage';
  6. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  7. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  8. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  9. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  10. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  11. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  12. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {DurationUnit, RateUnit} from 'sentry/utils/discover/fields';
  16. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {
  19. NumberOfPipelinesChart,
  20. PipelineDurationChart,
  21. TotalTokensUsedChart,
  22. } from 'sentry/views/aiMonitoring/aiMonitoringCharts';
  23. import {PipelineSpansTable} from 'sentry/views/aiMonitoring/pipelineSpansTable';
  24. import {MetricReadout} from 'sentry/views/performance/metricReadout';
  25. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  26. import {useSpanMetrics} from 'sentry/views/starfish/queries/useSpanMetrics';
  27. import {
  28. SpanFunction,
  29. SpanMetricsField,
  30. type SpanMetricsQueryFilters,
  31. } from 'sentry/views/starfish/types';
  32. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  33. function NoAccessComponent() {
  34. return (
  35. <Layout.Page withPadding>
  36. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  37. </Layout.Page>
  38. );
  39. }
  40. interface Props {
  41. params: {
  42. groupId: string;
  43. };
  44. }
  45. export default function AiMonitoringPage({params}: Props) {
  46. const organization = useOrganization();
  47. const {groupId} = params;
  48. const filters: SpanMetricsQueryFilters = {
  49. 'span.group': groupId,
  50. 'span.category': 'ai.pipeline',
  51. };
  52. const {data, isLoading: areSpanMetricsLoading} = useSpanMetrics({
  53. search: MutableSearch.fromQueryObject(filters),
  54. fields: [
  55. SpanMetricsField.SPAN_OP,
  56. SpanMetricsField.SPAN_DESCRIPTION,
  57. 'count()',
  58. `${SpanFunction.SPM}()`,
  59. `avg(${SpanMetricsField.SPAN_DURATION})`,
  60. ],
  61. enabled: Boolean(groupId),
  62. referrer: 'api.ai-pipelines.view',
  63. });
  64. const spanMetrics = data[0] ?? {};
  65. const {data: totalTokenData, isLoading: isTotalTokenDataLoading} = useSpanMetrics({
  66. search: MutableSearch.fromQueryObject({
  67. 'span.category': 'ai',
  68. 'span.ai.pipeline.group': groupId,
  69. }),
  70. fields: ['ai_total_tokens_used()'],
  71. enabled: Boolean(groupId),
  72. referrer: 'api.ai-pipelines.view',
  73. });
  74. const tokenUsedMetric = totalTokenData[0] ?? {};
  75. return (
  76. <PageFiltersContainer>
  77. <SentryDocumentTitle
  78. title={`AI Monitoring — ${spanMetrics['span.description'] ?? t('(no name)')}`}
  79. >
  80. <Layout.Page>
  81. <Feature
  82. features="ai-analytics"
  83. organization={organization}
  84. renderDisabled={NoAccessComponent}
  85. >
  86. <NoProjectMessage organization={organization}>
  87. <Layout.Header>
  88. <Layout.HeaderContent>
  89. <Layout.Title>
  90. {`${t('AI Monitoring')} - ${spanMetrics['span.description'] ?? t('(no name)')}`}
  91. <PageHeadingQuestionTooltip
  92. title={t(
  93. 'If this name is too generic, read the docs to learn how to change it.'
  94. )}
  95. docsUrl="https://docs.sentry.io/product/ai-monitoring/"
  96. />
  97. </Layout.Title>
  98. </Layout.HeaderContent>
  99. </Layout.Header>
  100. <Layout.Body>
  101. <Layout.Main fullWidth>
  102. <ModuleLayout.Layout>
  103. <ModuleLayout.Full>
  104. <SpaceBetweenWrap>
  105. <PageFilterBar condensed>
  106. <ProjectPageFilter />
  107. <EnvironmentPageFilter />
  108. <DatePageFilter />
  109. </PageFilterBar>
  110. <MetricsRibbon>
  111. <MetricReadout
  112. title={t('Total Runs')}
  113. value={spanMetrics['count()']}
  114. unit={'count'}
  115. isLoading={areSpanMetricsLoading}
  116. />
  117. <MetricReadout
  118. title={t('Total Tokens Used')}
  119. value={tokenUsedMetric['ai_total_tokens_used()']}
  120. unit={'count'}
  121. isLoading={isTotalTokenDataLoading}
  122. />
  123. <MetricReadout
  124. title={t('Runs Per Minute')}
  125. value={spanMetrics?.[`${SpanFunction.SPM}()`]}
  126. unit={RateUnit.PER_MINUTE}
  127. isLoading={areSpanMetricsLoading}
  128. />
  129. <MetricReadout
  130. title={DataTitles.avg}
  131. value={
  132. spanMetrics?.[`avg(${SpanMetricsField.SPAN_DURATION})`]
  133. }
  134. unit={DurationUnit.MILLISECOND}
  135. isLoading={areSpanMetricsLoading}
  136. />
  137. </MetricsRibbon>
  138. </SpaceBetweenWrap>
  139. </ModuleLayout.Full>
  140. <ModuleLayout.Third>
  141. <TotalTokensUsedChart groupId={groupId} />
  142. </ModuleLayout.Third>
  143. <ModuleLayout.Third>
  144. <NumberOfPipelinesChart groupId={groupId} />
  145. </ModuleLayout.Third>
  146. <ModuleLayout.Third>
  147. <PipelineDurationChart groupId={groupId} />
  148. </ModuleLayout.Third>
  149. <ModuleLayout.Full>
  150. <PipelineSpansTable groupId={groupId} />
  151. </ModuleLayout.Full>
  152. </ModuleLayout.Layout>
  153. </Layout.Main>
  154. </Layout.Body>
  155. </NoProjectMessage>
  156. </Feature>
  157. </Layout.Page>
  158. </SentryDocumentTitle>
  159. </PageFiltersContainer>
  160. );
  161. }
  162. const SpaceBetweenWrap = styled('div')`
  163. display: flex;
  164. justify-content: space-between;
  165. flex-wrap: wrap;
  166. `;
  167. const MetricsRibbon = styled('div')`
  168. display: flex;
  169. flex-wrap: wrap;
  170. gap: ${space(4)};
  171. `;