Skip to content

Commit

Permalink
Graph logic for AI analytics page (getsentry#68296)
Browse files Browse the repository at this point in the history
Adds time series graphs for prompt tokens and completion tokens
  • Loading branch information
colin-sentry authored Apr 4, 2024
1 parent 5fe3456 commit 2124b36
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
4 changes: 2 additions & 2 deletions static/app/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1493,10 +1493,10 @@ function buildRoutes() {
const aiAnalyticsRoutes = (
<Route
path="/ai-analytics/"
component={make(() => import('sentry/views/ai-analytics'))}
component={make(() => import('sentry/views/aiAnalytics'))}
withOrgPath
>
<IndexRoute component={make(() => import('sentry/views/ai-analytics/landing'))} />
<IndexRoute component={make(() => import('sentry/views/aiAnalytics/landing'))} />
</Route>
);

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import PageFiltersContainer from 'sentry/components/organizations/pageFilters/co
import {t} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import useOrganization from 'sentry/utils/useOrganization';
import AiAnalyticsFilters from 'sentry/views/ai-analytics/filters';
import AiAnalyticsFilters from 'sentry/views/aiAnalytics/filters';
import TokenUsageChart from 'sentry/views/aiAnalytics/tokenUsageChart';

function NoAccessComponent() {
return (
Expand All @@ -35,6 +36,10 @@ function AiAnalyticsContainer() {
<PageFiltersContainer>
<AiAnalyticsFilters onSearch={x => setSearch(x)} query={search} />
</PageFiltersContainer>
<TwoColumns>
<TokenUsageChart metric="ai.prompt_tokens.used" />
<TokenUsageChart metric="ai.completion_tokens.used" />
</TwoColumns>
</StyledMain>
</StyledBody>
</Layout.Page>
Expand All @@ -56,6 +61,11 @@ const StyledBody = styled('div')`
grid-template-areas: 'content saved-searches';
`;

const TwoColumns = styled('div')`
display: flex;
gap: ${space(2)};
`;

const StyledMain = styled('section')`
grid-area: content;
padding: ${space(2)};
Expand Down
File renamed without changes.
64 changes: 64 additions & 0 deletions static/app/views/aiAnalytics/tokenUsageChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import styled from '@emotion/styled';

import type {PageFilters} from 'sentry/types';
import {MetricDisplayType} from 'sentry/utils/metrics/types';
import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
import withPageFilters from 'sentry/utils/withPageFilters';
import {MetricChartContainer} from 'sentry/views/dashboards/metrics/chart';

interface TokenUsageChartProps {
metric: 'ai.total_tokens.used' | 'ai.prompt_tokens.used' | 'ai.completion_tokens.used';
selection: PageFilters;
isGlobalSelectionReady?: boolean;
}

function TokenUsageChart({selection, metric}: TokenUsageChartProps) {
const {
data: timeseriesData,
isLoading,
isError,
error,
} = useMetricsQuery(
[
{
name: 'a',
mri: `c:custom/${metric}@tokens`,
op: 'sum',
},
],
selection,
{
intervalLadder: 'dashboard',
}
);

if (isError) {
return <div>{'' + error}</div>;
}

return (
<TokenChartContainer>
<MetricChartContainer
timeseriesData={timeseriesData}
isLoading={isLoading}
metricQueries={[
{
name: 'mql',
formula: '$a',
},
]}
displayType={MetricDisplayType.AREA}
chartHeight={200}
/>
</TokenChartContainer>
);
}

const TokenChartContainer = styled('div')`
overflow: hidden;
border: 1px solid ${p => p.theme.border};
border-radius: ${p => p.theme.borderRadius};
flex: 1 1 content;
`;

export default withPageFilters(TokenUsageChart);

0 comments on commit 2124b36

Please sign in to comment.