-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix(automations): Use workflow stats time range #125141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import type {EChartsOption} from 'echarts'; | ||
| import {OrganizationFixture} from 'sentry-fixture/organization'; | ||
|
|
||
| import {render, screen} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| import {AutomationStatsChart} from 'sentry/views/automations/components/automationStatsChart'; | ||
|
|
||
| let mockChartOptions: EChartsOption | undefined; | ||
|
|
||
| jest.mock('echarts-for-react/lib/core', () => { | ||
| return function MockEcharts({option}: {option: EChartsOption}) { | ||
| mockChartOptions = option; | ||
| return null; | ||
| }; | ||
| }); | ||
|
|
||
| describe('AutomationStatsChart time range', () => { | ||
| const organization = OrganizationFixture(); | ||
|
|
||
| beforeEach(() => { | ||
| MockApiClient.clearMockResponses(); | ||
| mockChartOptions = undefined; | ||
| }); | ||
|
|
||
| it('uses the time range returned by the workflow stats endpoint on a time axis', async () => { | ||
| const start = Date.parse('2026-01-13T19:00:00Z'); | ||
| const end = Date.parse('2026-01-13T20:00:00Z'); | ||
| MockApiClient.addMockResponse({ | ||
| url: '/organizations/org-slug/workflows/123/stats/', | ||
| body: { | ||
| meta: {dataset: 'workflow', start, end}, | ||
| timeSeries: [ | ||
| { | ||
| yAxis: 'count()', | ||
| values: [{timestamp: end, value: 1, incomplete: false}], | ||
| meta: {interval: 60 * 60 * 1000, valueType: 'integer', valueUnit: null}, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| render( | ||
| <AutomationStatsChart | ||
| automationId="123" | ||
| period="1h" | ||
| start={null} | ||
| end={null} | ||
| utc={null} | ||
| />, | ||
| {organization} | ||
| ); | ||
|
|
||
| expect(await screen.findByText('1')).toBeInTheDocument(); | ||
| expect(mockChartOptions).toMatchObject({ | ||
| xAxis: {type: 'time', min: start, max: end}, | ||
| }); | ||
| }); | ||
|
|
||
| it('continues to accept the legacy response during deployment', async () => { | ||
| MockApiClient.addMockResponse({ | ||
| url: '/organizations/org-slug/workflows/123/stats/', | ||
| body: [{date: '2026-01-13T20:00:00Z', count: 1}], | ||
| }); | ||
|
|
||
| render( | ||
| <AutomationStatsChart | ||
| automationId="123" | ||
| period="1h" | ||
| start={null} | ||
| end={null} | ||
| utc={null} | ||
| />, | ||
| {organization} | ||
| ); | ||
|
|
||
| expect(await screen.findByText('1')).toBeInTheDocument(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import {useTheme} from '@emotion/react'; | ||
| import styled from '@emotion/styled'; | ||
| import {useQuery} from '@tanstack/react-query'; | ||
|
|
||
| import {Container, Flex} from '@sentry/scraps/layout'; | ||
|
|
||
|
|
@@ -14,15 +15,53 @@ import {PanelFooter} from 'sentry/components/panels/panelFooter'; | |
| import {Placeholder} from 'sentry/components/placeholder'; | ||
| import {t} from 'sentry/locale'; | ||
| import type {Automation, AutomationStats} from 'sentry/types/workflowEngine/automations'; | ||
| import {getApiUrl} from 'sentry/utils/api/getApiUrl'; | ||
| import {getUtcDateString} from 'sentry/utils/dates'; | ||
| import {useApiQuery} from 'sentry/utils/queryClient'; | ||
| import {apiOptions} from 'sentry/utils/api/apiOptions'; | ||
| import {getUtcDateString, getUtcToLocalDateObject} from 'sentry/utils/dates'; | ||
| import {useOrganization} from 'sentry/utils/useOrganization'; | ||
| import type {TimeSeries} from 'sentry/views/dashboards/widgets/common/types'; | ||
|
|
||
| interface IssueAlertDetailsProps extends DateTimeObject { | ||
| automationId: Automation['id']; | ||
| } | ||
|
|
||
| type WorkflowStatsResponse = { | ||
| meta: { | ||
| dataset: string; | ||
| end: number; | ||
| start: number; | ||
| }; | ||
| timeSeries: TimeSeries[]; | ||
| }; | ||
|
|
||
| type WorkflowStatsApiResponse = AutomationStats[] | WorkflowStatsResponse; | ||
|
|
||
| type WorkflowStats = { | ||
| values: TimeSeries['values']; | ||
| timeframe?: WorkflowStatsResponse['meta']; | ||
| }; | ||
|
|
||
| function normalizeWorkflowStatsResponse( | ||
| response: WorkflowStatsApiResponse | undefined | ||
| ): WorkflowStats | undefined { | ||
| if (!response) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (Array.isArray(response)) { | ||
| return { | ||
| values: response.map(({date, count}) => ({ | ||
| timestamp: Date.parse(date), | ||
| value: count, | ||
| })), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| timeframe: response.meta, | ||
| values: response.timeSeries[0]?.values ?? [], | ||
| }; | ||
| } | ||
|
|
||
| export function AutomationStatsChart({ | ||
| automationId, | ||
| period, | ||
|
|
@@ -34,28 +73,28 @@ export function AutomationStatsChart({ | |
| const organization = useOrganization(); | ||
| const chartZoomProps = useChartZoom({saveOnZoom: true}); | ||
| const { | ||
| data: fireHistory, | ||
| data: statsResponse, | ||
| isPending, | ||
| isError, | ||
| } = useApiQuery<AutomationStats[]>( | ||
| [ | ||
| getApiUrl('/organizations/$organizationIdOrSlug/workflows/$workflowId/stats/', { | ||
| path: {organizationIdOrSlug: organization.slug, workflowId: automationId}, | ||
| }), | ||
| } = useQuery( | ||
| apiOptions.as<WorkflowStatsApiResponse>()( | ||
| '/organizations/$organizationIdOrSlug/workflows/$workflowId/stats/', | ||
| { | ||
| path: {organizationIdOrSlug: organization.slug, workflowId: automationId}, | ||
| query: { | ||
| ...(period && {statsPeriod: period}), | ||
| start: start ? getUtcDateString(start) : undefined, | ||
| end: end ? getUtcDateString(end) : undefined, | ||
| utc: utc ? 'true' : undefined, | ||
| }, | ||
| }, | ||
| ], | ||
| {staleTime: 30000} | ||
| staleTime: 30_000, | ||
| } | ||
| ) | ||
| ); | ||
|
|
||
| const stats = normalizeWorkflowStatsResponse(statsResponse); | ||
| const totalAlertsTriggered = | ||
| fireHistory?.reduce((acc, curr) => acc + curr.count, 0) ?? 0; | ||
| stats?.values.reduce((acc, curr) => acc + (curr.value ?? 0), 0) ?? 0; | ||
|
|
||
| return ( | ||
| <Panel> | ||
|
|
@@ -65,10 +104,14 @@ export function AutomationStatsChart({ | |
| </Container> | ||
| {isPending && <Placeholder height="200px" />} | ||
| {isError && <LoadingError />} | ||
| {fireHistory && ( | ||
| {stats && ( | ||
| <BarChart | ||
| {...chartZoomProps} | ||
| period={period} | ||
| showTimeInTooltip | ||
| start={start ? getUtcToLocalDateObject(start) : undefined} | ||
| end={end ? getUtcToLocalDateObject(end) : undefined} | ||
| utc={utc ?? undefined} | ||
| grid={{ | ||
| left: theme.space['2xs'], | ||
| right: theme.space.xl, | ||
|
|
@@ -78,12 +121,20 @@ export function AutomationStatsChart({ | |
| yAxis={{ | ||
| minInterval: 1, | ||
| }} | ||
| xAxis={ | ||
| stats.timeframe | ||
| ? { | ||
| min: stats.timeframe.start, | ||
| max: stats.timeframe.end, | ||
| } | ||
| : undefined | ||
| } | ||
| series={[ | ||
| { | ||
| seriesName: t('Alerts Triggered'), | ||
| data: fireHistory.map(automation => ({ | ||
| name: automation.date, | ||
| value: automation.count, | ||
| data: stats.values.map(({timestamp, value}) => ({ | ||
| name: timestamp, | ||
| value: value ?? 0, | ||
|
Comment on lines
+135
to
+137
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The chart renders raw numeric timestamps on its x-axis for legacy data because Suggested FixIn Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| })), | ||
| emphasis: { | ||
| disabled: true, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function is here temporarily while we move the endpoint to the new format. follow up to remove this normalization