aiAnalyticsCharts.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import styled from '@emotion/styled';
  2. import {t} from 'sentry/locale';
  3. import {space} from 'sentry/styles/space';
  4. import {MetricDisplayType} from 'sentry/utils/metrics/types';
  5. import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import {MetricChartContainer} from 'sentry/views/dashboards/metrics/chart';
  8. export function TotalTokensUsedChart() {
  9. const {selection, isReady: isGlobalSelectionReady} = usePageFilters();
  10. const {
  11. data: timeseriesData,
  12. isLoading,
  13. isError,
  14. error,
  15. } = useMetricsQuery(
  16. [
  17. {
  18. name: 'total',
  19. mri: `c:spans/ai.total_tokens.used@none`,
  20. op: 'sum',
  21. },
  22. ],
  23. selection,
  24. {
  25. intervalLadder: 'dashboard',
  26. }
  27. );
  28. if (!isGlobalSelectionReady) {
  29. return null;
  30. }
  31. if (isError) {
  32. return <div>{'' + error}</div>;
  33. }
  34. return (
  35. <TokenChartContainer>
  36. <PanelTitle>{t('Total tokens used')}</PanelTitle>
  37. <MetricChartContainer
  38. timeseriesData={timeseriesData}
  39. isLoading={isLoading}
  40. metricQueries={[
  41. {
  42. name: 'mql',
  43. formula: '$total',
  44. },
  45. ]}
  46. displayType={MetricDisplayType.AREA}
  47. chartHeight={200}
  48. />
  49. </TokenChartContainer>
  50. );
  51. }
  52. export function NumberOfPipelinesChart() {
  53. const {selection, isReady: isGlobalSelectionReady} = usePageFilters();
  54. const {
  55. data: timeseriesData,
  56. isLoading,
  57. isError,
  58. error,
  59. } = useMetricsQuery(
  60. [
  61. {
  62. name: 'number',
  63. mri: `d:spans/exclusive_time@millisecond`,
  64. op: 'count',
  65. query: 'span.op:"ai.pipeline.langchain"', // TODO: for now this is the only AI "pipeline" supported
  66. },
  67. ],
  68. selection,
  69. {
  70. intervalLadder: 'dashboard',
  71. }
  72. );
  73. if (!isGlobalSelectionReady) {
  74. return null;
  75. }
  76. if (isError) {
  77. return <div>{'' + error}</div>;
  78. }
  79. return (
  80. <TokenChartContainer>
  81. <PanelTitle>{t('Number of AI pipelines')}</PanelTitle>
  82. <MetricChartContainer
  83. timeseriesData={timeseriesData}
  84. isLoading={isLoading}
  85. metricQueries={[
  86. {
  87. name: 'mql',
  88. formula: '$number',
  89. },
  90. ]}
  91. displayType={MetricDisplayType.AREA}
  92. chartHeight={200}
  93. />
  94. </TokenChartContainer>
  95. );
  96. }
  97. export function PipelineDurationChart() {
  98. const {selection, isReady: isGlobalSelectionReady} = usePageFilters();
  99. const {
  100. data: timeseriesData,
  101. isLoading,
  102. isError,
  103. error,
  104. } = useMetricsQuery(
  105. [
  106. {
  107. name: 'number',
  108. mri: `d:spans/exclusive_time@millisecond`,
  109. op: 'avg',
  110. query: 'span.op:"ai.pipeline.langchain"', // TODO: for now this is the only AI "pipeline" supported
  111. },
  112. ],
  113. selection,
  114. {
  115. intervalLadder: 'dashboard',
  116. }
  117. );
  118. if (!isGlobalSelectionReady) {
  119. return null;
  120. }
  121. if (isError) {
  122. return <div>{'' + error}</div>;
  123. }
  124. return (
  125. <TokenChartContainer>
  126. <PanelTitle>{t('AI pipeline duration')}</PanelTitle>
  127. <MetricChartContainer
  128. timeseriesData={timeseriesData}
  129. isLoading={isLoading}
  130. metricQueries={[
  131. {
  132. name: 'mql',
  133. formula: '$number',
  134. },
  135. ]}
  136. displayType={MetricDisplayType.AREA}
  137. chartHeight={200}
  138. />
  139. </TokenChartContainer>
  140. );
  141. }
  142. const PanelTitle = styled('h5')`
  143. padding: ${space(3)} ${space(3)} 0;
  144. margin: 0;
  145. `;
  146. const TokenChartContainer = styled('div')`
  147. overflow: hidden;
  148. border: 1px solid ${p => p.theme.border};
  149. border-radius: ${p => p.theme.borderRadius};
  150. height: 100%;
  151. display: flex;
  152. flex-direction: column;
  153. `;