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
13 changes: 13 additions & 0 deletions static/app/utils/analytics/monitorsAnalyticsEvents.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import type {Organization} from 'sentry/types/organization';
import {getAutomationAnalyticsPayload} from 'sentry/views/automations/components/forms/common/getAutomationAnalyticsPayload';

type AutomationAnalyticsEventPayload = ReturnType<typeof getAutomationAnalyticsPayload>;

export type MonitorsEventParameters = {
'automation.created': AutomationAnalyticsEventPayload & {
organization: Organization;
};
'automation.updated': AutomationAnalyticsEventPayload & {
organization: Organization;
};
'landing_page.platform_guide.viewed': {
guide: string;
platform: string;
Expand All @@ -9,4 +20,6 @@ type MonitorsAnalyticsKey = keyof MonitorsEventParameters;

export const monitorsEventMap: Record<MonitorsAnalyticsKey, string> = {
'landing_page.platform_guide.viewed': 'Crons Landing Page: Viewed Platform Guide',
'automation.created': 'Automations: Created',
'automation.updated': 'Automations: Updated',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {Automation} from 'sentry/types/workflowEngine/automations';

export function getAutomationAnalyticsPayload(automation: Automation): {
actions_count: number;
detectors_count: number;
environment: string | null;
frequency_minutes: number | null;
trigger_conditions_count: number;
} {
const frequency_minutes = automation.config.frequency ?? null;
const environment = automation.environment;
const detectors_count = automation.detectorIds.length;
const trigger_conditions_count = automation.triggers?.conditions?.length ?? 0;
const actions_count = automation.actionFilters.reduce(
(total, filter) => total + (filter.actions?.length ?? 0),
0
);

return {
frequency_minutes,
environment,
detectors_count,
trigger_conditions_count,
actions_count,
};
}
59 changes: 59 additions & 0 deletions static/app/views/automations/edit.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
within,
} from 'sentry-test/reactTestingLibrary';

import {trackAnalytics} from 'sentry/utils/analytics';
import {useParams} from 'sentry/utils/useParams';
import AutomationEdit from 'sentry/views/automations/edit';

jest.mock('sentry/utils/useParams');
jest.mock('sentry/utils/analytics');

describe('EditAutomation', () => {
const automation = AutomationFixture();
Expand Down Expand Up @@ -132,4 +134,61 @@ describe('EditAutomation', () => {
// Verify the button text has changed to "Enable"
expect(await screen.findByRole('button', {name: 'Enable'})).toBeInTheDocument();
});

it('updates automation', async () => {
const mockUpdateAutomation = MockApiClient.addMockResponse({
url: `/organizations/${organization.slug}/workflows/${automation.id}/`,
method: 'PUT',
body: automation,
});

const {router} = render(<AutomationEdit />, {
organization,
});

expect(await screen.findAllByText(/Automation 1/i)).toHaveLength(2);

// Update an existing filter value field
const valueInput = screen.getByRole('textbox', {name: 'Value'});
await userEvent.clear(valueInput);
await userEvent.type(valueInput, 'updated value');

await userEvent.click(screen.getByRole('button', {name: 'Save'}));

await waitFor(() => {
expect(mockUpdateAutomation).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
data: expect.objectContaining({
actionFilters: expect.arrayContaining([
expect.objectContaining({
conditions: expect.arrayContaining([
expect.objectContaining({
comparison: expect.objectContaining({
value: 'updated value',
}),
}),
]),
}),
]),
}),
})
);
});

expect(trackAnalytics).toHaveBeenCalledWith('automation.updated', {
organization,
frequency_minutes: 1440,
environment: 'production',
detectors_count: 1,
trigger_conditions_count: 0,
actions_count: 1,
});

await waitFor(() =>
expect(router.location.pathname).toBe(
`/organizations/${organization.slug}/monitors/alerts/${automation.id}/`
)
);
});
});
8 changes: 7 additions & 1 deletion static/app/views/automations/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {StickyFooter} from 'sentry/components/workflowEngine/ui/footer';
import {t} from 'sentry/locale';
import type {Automation, NewAutomation} from 'sentry/types/workflowEngine/automations';
import {DataConditionGroupLogicType} from 'sentry/types/workflowEngine/dataConditions';
import {trackAnalytics} from 'sentry/utils/analytics';
import {useNavigate} from 'sentry/utils/useNavigate';
import useOrganization from 'sentry/utils/useOrganization';
import {useParams} from 'sentry/utils/useParams';
Expand All @@ -36,6 +37,7 @@ import {
} from 'sentry/views/automations/components/automationFormData';
import {EditableAutomationName} from 'sentry/views/automations/components/editableAutomationName';
import {EditAutomationActions} from 'sentry/views/automations/components/editAutomationActions';
import {getAutomationAnalyticsPayload} from 'sentry/views/automations/components/forms/common/getAutomationAnalyticsPayload';
import {AutomationFormProvider} from 'sentry/views/automations/components/forms/context';
import {useAutomationQuery, useUpdateAutomation} from 'sentry/views/automations/hooks';
import {
Expand Down Expand Up @@ -150,10 +152,14 @@ function AutomationEditForm({automation}: {automation: Automation}) {
...formData,
};
const updatedAutomation = await updateAutomation(updatedData);
trackAnalytics('automation.updated', {
organization,
...getAutomationAnalyticsPayload(updatedAutomation),
});
navigate(makeAutomationDetailsPathname(organization.slug, updatedAutomation.id));
}
},
[automation.id, organization.slug, navigate, updateAutomation, state]
[automation.id, organization, navigate, updateAutomation, state]
);

return (
Expand Down
15 changes: 15 additions & 0 deletions static/app/views/automations/new.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {
DataConditionHandlerSubgroupType,
DataConditionType,
} from 'sentry/types/workflowEngine/dataConditions';
import {trackAnalytics} from 'sentry/utils/analytics';
import AutomationNewSettings from 'sentry/views/automations/new';

jest.mock('sentry/utils/analytics');

describe('AutomationNewSettings', () => {
const organization = OrganizationFixture({features: ['workflow-engine-ui']});

Expand Down Expand Up @@ -165,5 +168,17 @@ describe('AutomationNewSettings', () => {
`/organizations/${organization.slug}/monitors/alerts/${created.id}/`
)
);

// Verify analytics was called with correct event and payload structure
await waitFor(() => {
expect(trackAnalytics).toHaveBeenCalledWith('automation.created', {
organization,
frequency_minutes: expect.any(Number),
environment: expect.anything(),
detectors_count: expect.any(Number),
trigger_conditions_count: expect.any(Number),
actions_count: expect.any(Number),
});
});
});
});
8 changes: 7 additions & 1 deletion static/app/views/automations/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {FullHeightForm} from 'sentry/components/workflowEngine/form/fullHeightFo
import {useFormField} from 'sentry/components/workflowEngine/form/useFormField';
import {StickyFooter} from 'sentry/components/workflowEngine/ui/footer';
import {t} from 'sentry/locale';
import {trackAnalytics} from 'sentry/utils/analytics';
import {useLocation} from 'sentry/utils/useLocation';
import {useNavigate} from 'sentry/utils/useNavigate';
import useOrganization from 'sentry/utils/useOrganization';
Expand All @@ -29,6 +30,7 @@ import {
validateAutomationBuilderState,
} from 'sentry/views/automations/components/automationFormData';
import {EditableAutomationName} from 'sentry/views/automations/components/editableAutomationName';
import {getAutomationAnalyticsPayload} from 'sentry/views/automations/components/forms/common/getAutomationAnalyticsPayload';
import {AutomationFormProvider} from 'sentry/views/automations/components/forms/context';
import {useCreateAutomation} from 'sentry/views/automations/hooks';
import {
Expand Down Expand Up @@ -110,10 +112,14 @@ export default function AutomationNewSettings() {
const automation = await createAutomation(
getNewAutomationData(data as AutomationFormData, state)
);
trackAnalytics('automation.created', {
organization,
...getAutomationAnalyticsPayload(automation),
});
navigate(makeAutomationDetailsPathname(organization.slug, automation.id));
}
},
[createAutomation, state, navigate, organization.slug]
[createAutomation, state, navigate, organization]
);

return (
Expand Down
2 changes: 2 additions & 0 deletions tests/js/fixtures/automations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export function ActionFixture(params: Partial<Action> = {}): Action {
type: ActionType.SLACK,
config: {
targetType: ActionTarget.SPECIFIC,
targetIdentifier: 'C123456',
},
integrationId: 'integration-1',
data: {},
status: 'active',
...params,
Expand Down
Loading