Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions static/app/types/workflowEngine/automations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ export interface AutomationFireHistory {
detector?: Detector;
}

export type AutomationStats = {
count: number;
date: string;
};

/**
* Warning information about the status of actions in an automation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,4 @@ describe('AutomationStatsChart time range', () => {
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();
});
});
54 changes: 10 additions & 44 deletions static/app/views/automations/components/automationStatsChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {PanelBody} from 'sentry/components/panels/panelBody';
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 type {Automation} from 'sentry/types/workflowEngine/automations';
import {apiOptions} from 'sentry/utils/api/apiOptions';
import {getUtcDateString, getUtcToLocalDateObject} from 'sentry/utils/dates';
import {useOrganization} from 'sentry/utils/useOrganization';
Expand All @@ -30,38 +30,9 @@ type WorkflowStatsResponse = {
end: number;
start: number;
};
timeSeries: TimeSeries[];
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,
Expand All @@ -73,11 +44,11 @@ export function AutomationStatsChart({
const organization = useOrganization();
const chartZoomProps = useChartZoom({saveOnZoom: true});
const {
data: statsResponse,
data: stats,
isPending,
isError,
} = useQuery(
apiOptions.as<WorkflowStatsApiResponse>()(
apiOptions.as<WorkflowStatsResponse>()(
'/organizations/$organizationIdOrSlug/workflows/$workflowId/stats/',
{
path: {organizationIdOrSlug: organization.slug, workflowId: automationId},
Expand All @@ -92,9 +63,8 @@ export function AutomationStatsChart({
)
);

const stats = normalizeWorkflowStatsResponse(statsResponse);
const totalAlertsTriggered =
stats?.values.reduce((acc, curr) => acc + (curr.value ?? 0), 0) ?? 0;
stats?.timeSeries[0].values.reduce((acc, curr) => acc + (curr.value ?? 0), 0) ?? 0;

return (
<Panel>
Expand All @@ -121,18 +91,14 @@ export function AutomationStatsChart({
yAxis={{
minInterval: 1,
}}
xAxis={
stats.timeframe
? {
min: stats.timeframe.start,
max: stats.timeframe.end,
}
: undefined
}
xAxis={{
min: stats.meta.start,
max: stats.meta.end,
}}
series={[
{
seriesName: t('Alerts Triggered'),
data: stats.values.map(({timestamp, value}) => ({
data: stats.timeSeries[0].values.map(({timestamp, value}) => ({
name: timestamp,
value: value ?? 0,
})),
Expand Down
15 changes: 14 additions & 1 deletion static/app/views/automations/detail.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ describe('AutomationDetail', () => {

MockApiClient.addMockResponse({
url: '/organizations/org-slug/workflows/123/stats/',
body: [],
body: {
meta: {dataset: 'workflow', start: 0, end: 60 * 60 * 1000},
timeSeries: [
{
yAxis: 'count()',
values: [],
meta: {
interval: 60 * 60 * 1000,
valueType: 'integer',
valueUnit: null,
},
},
],
},
});

MockApiClient.addMockResponse({
Expand Down
Loading