tokenUsageChart.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import styled from '@emotion/styled';
  2. import type {PageFilters} from 'sentry/types';
  3. import {MetricDisplayType} from 'sentry/utils/metrics/types';
  4. import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
  5. import withPageFilters from 'sentry/utils/withPageFilters';
  6. import {MetricChartContainer} from 'sentry/views/dashboards/metrics/chart';
  7. interface TokenUsageChartProps {
  8. metric: 'ai.total_tokens.used' | 'ai.prompt_tokens.used' | 'ai.completion_tokens.used';
  9. selection: PageFilters;
  10. isGlobalSelectionReady?: boolean;
  11. }
  12. function TokenUsageChart({selection, metric}: TokenUsageChartProps) {
  13. const {
  14. data: timeseriesData,
  15. isLoading,
  16. isError,
  17. error,
  18. } = useMetricsQuery(
  19. [
  20. {
  21. name: 'a',
  22. mri: `c:custom/${metric}@tokens`,
  23. op: 'sum',
  24. },
  25. ],
  26. selection,
  27. {
  28. intervalLadder: 'dashboard',
  29. }
  30. );
  31. if (isError) {
  32. return <div>{'' + error}</div>;
  33. }
  34. return (
  35. <TokenChartContainer>
  36. <MetricChartContainer
  37. timeseriesData={timeseriesData}
  38. isLoading={isLoading}
  39. metricQueries={[
  40. {
  41. name: 'mql',
  42. formula: '$a',
  43. },
  44. ]}
  45. displayType={MetricDisplayType.AREA}
  46. chartHeight={200}
  47. />
  48. </TokenChartContainer>
  49. );
  50. }
  51. const TokenChartContainer = styled('div')`
  52. overflow: hidden;
  53. border: 1px solid ${p => p.theme.border};
  54. border-radius: ${p => p.theme.borderRadius};
  55. flex: 1 1 content;
  56. `;
  57. export default withPageFilters(TokenUsageChart);