:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): show that a send waits for its realtime cohort (#119400)
Harley Alexander8430357f7ac7
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex ef454ddc957c..c85ced34e656 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -8286,6 +8286,14 @@ snapshots:hash: v1.k794b7964.9e7a12c3094416ddf703121095bc7eba5189420cab88d6967618bf43139069fe.UZSvEld_DwpzTPi6QpbRfxOtDYP7S_gR0F03DJcFMpMproducts-workflows-steps-delay--fixed-duration--light:hash: v1.k794b7964.f394f1e00c55c84136a291d5acb646dc12e0b27951a99e493137f7bad05a2082.zj4h9BHlkIR9RBHiB6nkO0QjDgPC1UKa08uSHemhjSQ+ products-workflows-steps-realtime-cohort-calculating-banner--calculating--dark:+ hash: v1.k794b7964.f8d96220904631c8b3272c69f05c32ae18aaa1f6ab885595fa093914fb4b9585.vMfQ-MPIZHxlUj7bQT739aLrmXkrNzq0avGb2p0mwmE+ products-workflows-steps-realtime-cohort-calculating-banner--calculating--light:+ hash: v1.k794b7964.d5f5076185fb6ff85442cea0dd8d7a0948bf71536526d97ad868e987f8260a50.5KYTfDsEby8LKM0FGZxgD4eqPk5FbEfIi4eEre9AWSg+ products-workflows-steps-realtime-cohort-calculating-banner--needs-attention--dark:+ hash: v1.k794b7964.81dfba2ceeec9af0760080c21a7febbca95c089e51543dcb8c0b1b94a7827f65.FIY338VfKe3yLoPte4RKAtmK_JOqDxkJbo2H7fU7DDI+ products-workflows-steps-realtime-cohort-calculating-banner--needs-attention--light:+ hash: v1.k794b7964.35f34e84a0beb529c8d6f92fb8ae876196eb3040a46666704c4d03b1722d3a50.H2tDGrJ4zAZQeOMCOeulpzK7CBBrTxzDRtI2J0Pbqscproducts-workflows-steps-trigger-volume--event-trigger--dark:hash: v1.k794b7964.2650854dfdf42c9ce43499445bbe06f8bd55b8d5a032cbdd8e5dcf037ee21080.zVFSrKI3MkfgGSToHH78WhYcRVqXQUUTYcVJQFSgJ0Yproducts-workflows-steps-trigger-volume--event-trigger--light:diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex b2def25169f5..a91ab7181d68 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -244,7 +244,6 @@ import type {ExternalDataSourceTypeEnumApi,SourceConfigMapResponseApi,} from 'products/warehouse_sources/frontend/generated/api.schemas'-import type { BlastRadiusApi } from 'products/workflows/frontend/generated/api.schemas'import type { HogFlowPublishResponseApi } from 'products/workflows/frontend/generated/api.schemas'import type { MessageTemplate } from 'products/workflows/frontend/TemplateLibrary/types'import type { HogflowTestResult } from 'products/workflows/frontend/Workflows/hogflows/steps/types'@@ -6380,16 +6379,6 @@ const api = {): Promise<HogflowTestResult> {return await new ApiRequest().hogFlow(hogFlowId).withAction('invocations').create({ data })},- async getBatchTriggerBlastRadius(- filters: Extract<HogFlowAction['config'], { type: 'batch' }>['filters'],- dedupeKey?: 'email',- sendsEmail?: boolean- ): Promise<BlastRadiusApi> {- return await new ApiRequest()- .hogFlows()- .withAction('user_blast_radius')- .create({ data: { filters, dedupe_key: dedupeKey ?? null, sends_email: sendsEmail ?? true } })- },async createHogFlowBatchJob(hogFlowId: HogFlow['id'],data: {diff --git a/products/workflows/frontend/Broadcasts/BroadcastWizard.tsx b/products/workflows/frontend/Broadcasts/BroadcastWizard.tsxindex 2e7e0f100a1b..e9d79f0a7552 100644--- a/products/workflows/frontend/Broadcasts/BroadcastWizard.tsx+++ b/products/workflows/frontend/Broadcasts/BroadcastWizard.tsx@@ -7,6 +7,10 @@ import { LemonMenu } from 'lib/lemon-ui/LemonMenu'import { SceneContent } from '~/layout/scenes/components/SceneContent'+import {+ isRealtimeCohortCalculating,+ realtimeCohortSendBlockedReason,+} from '../Workflows/hogflows/steps/realtimeCohortStatus'import { manageDisabledReason } from './broadcastLifecycle'import { BroadcastSceneHeader } from './BroadcastSceneHeader'import { broadcastWizardLogic } from './broadcastWizardLogic'@@ -19,8 +23,9 @@ import { BroadcastScheduleStep } from './steps/BroadcastScheduleStep'import { useBroadcastAgentPanel } from './useBroadcastAgentPanel'export function BroadcastWizard(): JSX.Element {- const { currentStep, stepValidationErrors, currentStepHasErrors, saving, launching, scheduleMode } =+ const { currentStep, stepValidationErrors, currentStepHasErrors, saving, launching, scheduleMode, blastRadius } =useValues(broadcastWizardLogic)+ const realtimeCohort = blastRadius?.realtime_cohortconst { setStep, prevStep, continueStep, launchBroadcast, archiveBroadcast, openInWorkflowEditor } =useActions(broadcastWizardLogic)const { broadcastId, broadcast } = useValues(broadcastWizardLogic)@@ -101,12 +106,16 @@ export function BroadcastWizard(): JSX.Element {disabledReason={stepValidationErrors.review.length > 0? 'Fix the issues above before sending'- : undefined+ : realtimeCohortSendBlockedReason(realtimeCohort)}onClick={launchBroadcast}data-attr="broadcast-wizard-launch">- {scheduleMode === 'now' ? 'Send now' : 'Schedule broadcast'}+ {scheduleMode !== 'now'+ ? 'Schedule broadcast'+ : isRealtimeCohortCalculating(realtimeCohort)+ ? 'Send when calculated'+ : 'Send now'}</LemonButton>) : (<LemonButtondiff --git a/products/workflows/frontend/Broadcasts/audience/audienceList.ts b/products/workflows/frontend/Broadcasts/audience/audienceList.tsindex 063d05da034d..424841880cdf 100644--- a/products/workflows/frontend/Broadcasts/audience/audienceList.ts+++ b/products/workflows/frontend/Broadcasts/audience/audienceList.ts@@ -119,6 +119,22 @@ export function audienceCohortLaunchError(cohort: AudienceCohort): string | nullreturn null}+export interface AudienceCohortLaunchError {+ cohortId: number+ message: string+}++/**+ * The launch errors that still stop a send. A realtime cohort the send waits for doesn't, because its run+ * starts on its own once the cohort finishes calculating.+ */+export function blockingCohortLaunchErrors(+ errors: AudienceCohortLaunchError[],+ realtimeCohortId: number | null | undefined+): string[] {+ return errors.filter((error) => error.cohortId !== realtimeCohortId).map((error) => error.message)+}+/** What an upload added, and which rows it skipped and why, so a short list is never a surprise. */export function peopleImportMessage(cohortName: string, summary: PeopleImportApi): string {const parts = [`Added "${cohortName}" to the audience.`]diff --git a/products/workflows/frontend/Broadcasts/audience/broadcastAudienceCohortsLogic.ts b/products/workflows/frontend/Broadcasts/audience/broadcastAudienceCohortsLogic.tsindex 5d79cbe20f26..90311a9a0dd9 100644--- a/products/workflows/frontend/Broadcasts/audience/broadcastAudienceCohortsLogic.ts+++ b/products/workflows/frontend/Broadcasts/audience/broadcastAudienceCohortsLogic.ts@@ -8,7 +8,12 @@ import { AnyPropertyFilter, PropertyFilterType } from '~/types'import { cohortsRetrieve } from 'products/cohorts/frontend/generated/api'import { BroadcastWizardLogicProps, broadcastWizardLogic } from '../broadcastWizardLogic'-import { audienceCohortIds, audienceCohortLaunchError, toAudienceCohort } from './audienceList'+import {+ type AudienceCohortLaunchError,+ audienceCohortIds,+ audienceCohortLaunchError,+ toAudienceCohort,+} from './audienceList'export interface AudienceCohort {id: number@@ -38,8 +43,8 @@ export interface broadcastAudienceCohortsLogicValues {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface broadcastAudienceCohortsLogicActions {loadBlastRadius: () => any // broadcastWizardLogic- setAudienceCohortLaunchErrors: (errors: string[]) => {- errors: string[]+ setAudienceCohortLaunchErrors: (errors: AudienceCohortLaunchError[]) => {+ errors: AudienceCohortLaunchError[]} // broadcastWizardLogicloadAudienceCohorts: () => {value: true@@ -123,7 +128,10 @@ export const broadcastAudienceCohortsLogic = kea<broadcastAudienceCohortsLogicTyconst cohorts = Object.fromEntries(loaded)actions.setAudienceCohorts(cohorts)actions.setAudienceCohortLaunchErrors(- Object.values(cohorts).flatMap((cohort) => (cohort ? (audienceCohortLaunchError(cohort) ?? []) : []))+ ids.flatMap((id) => {+ const message = cohorts[id] ? audienceCohortLaunchError(cohorts[id]!) : null+ return message ? [{ cohortId: id, message }] : []+ }))// The audience size counts a cohort's members, so it is stale until matching finishes.if (ids.some((id) => previous[id]?.isCalculating && cohorts[id] && !cohorts[id]!.isCalculating)) {diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.tsindex 32807b0d6230..66b51f9edf84 100644--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts@@ -11,7 +11,7 @@ import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'import { AnyPropertyFilter, PropertyFilterType, PropertyOperator } from '~/types'-import type { HogFlowApi } from 'products/workflows/frontend/generated/api.schemas'+import type { BlastRadiusApi, HogFlowApi } from 'products/workflows/frontend/generated/api.schemas'import { draftMessage } from '../MessageAudience/messageDrafts'import { urlForNewBroadcastWithAudience } from './broadcastAudiencePrefill'@@ -569,17 +569,86 @@ describe('broadcastWizardLogic', () => {})- it('stops a launch while an uploaded list in the audience is still matching', async () => {+ it('refreshes the realtime cohort status until the cohort is ready, past a failed check', async () => {+ const states = ['needs_attention', 'error', 'ready']+ let calls = 0useMocks({- get: {- '/api/projects/:team_id/cohorts/:id/': {- id: 42,- name: 'Spring list',- is_static: true,- is_calculating: true,+ post: {+ '/api/projects/:team_id/hog_flows/user_blast_radius/': async ({ request }) => {+ // The recipient email check posts here too, with an extra condition.+ const { filters } = (await request.json()) as { filters: { properties: unknown[] } }+ if (filters.properties.length > 1) {+ return [200, { affected: 0, total: 0 }]+ }+ const state = states[Math.min(calls++, states.length - 1)]+ if (state === 'error') {+ return [500, { detail: 'Simulated outage' }]+ }+ return [+ 200,+ {+ affected: 0,+ total: 0,+ realtime_cohort: { id: 42, name: 'Upgraded', state, percent_complete: null },+ },+ ]},},})+ const statusLoaded =+ (state: string) =>+ (action: { type: string; payload?: { blastRadius?: BlastRadiusApi | null } }): boolean =>+ action.type === logic.actionTypes.loadBlastRadiusSuccess &&+ action.payload?.blastRadius?.realtime_cohort?.state === state+ await expectLogic(logic, () => {+ logic.actions.setAudienceProperties([+ { type: PropertyFilterType.Cohort, key: 'id', value: 42, operator: PropertyOperator.In },+ ])+ }).toDispatchActions([statusLoaded('needs_attention')])++ jest.useFakeTimers()+ try {+ await jest.advanceTimersByTimeAsync(20000)+ await expectLogic(logic).toDispatchActions([statusLoaded('ready')])+ expect(logic.values.blastRadius?.realtime_cohort?.state).toEqual('ready')++ await jest.advanceTimersByTimeAsync(30000)+ expect(calls).toEqual(3)+ } finally {+ jest.useRealTimers()+ }+ })++ const dynamicCohort = { id: 42, name: 'Upgraded', is_static: false, is_calculating: true }+ it.each([+ [+ 'an uploaded list that is still matching',+ { id: 42, name: 'Spring list', is_static: true, is_calculating: true },+ null,+ ['"Spring list" is still matching people. You can launch when it finishes.'],+ ],+ [+ 'a cohort before its first calculation',+ dynamicCohort,+ null,+ [`"Upgraded" is still calculating who's in it. You can launch when it finishes.`],+ ],+ [+ 'a realtime cohort the send waits for',+ dynamicCohort,+ { id: 42, name: 'Upgraded', state: 'building', percent_complete: null },+ [],+ ],+ ])('checks the audience cohorts at launch: %s', async (_desc, cohort, realtimeCohort, expectedErrors) => {+ useMocks({+ get: { '/api/projects/:team_id/cohorts/:id/': cohort },+ post: {+ '/api/projects/:team_id/hog_flows/user_blast_radius/': () => [+ 200,+ { affected: 0, total: 0, realtime_cohort: realtimeCohort },+ ],+ },+ })logic.actions.setEmail({...DEFAULT_BROADCAST_EMAIL,from: { ...DEFAULT_BROADCAST_EMAIL.from, integrationId: 1 },@@ -589,17 +658,16 @@ describe('broadcastWizardLogic', () => {logic.actions.setAudienceProperties([{ type: PropertyFilterType.Cohort, key: 'id', value: 42, operator: PropertyOperator.In },])+ await expectLogic(logic, () => {+ logic.actions.loadBlastRadius()+ }).toDispatchActions(['loadBlastRadiusSuccess'])logic.actions.setStep('review')await expectLogic(logic, () => {logic.actions.launchBroadcast()- })- .toDispatchActions(['setAudienceCohortLaunchErrors', 'launchBroadcastFinished'])- .toNotHaveDispatchedActions(['saveBroadcastFinished'])+ }).toDispatchActions(['setAudienceCohortLaunchErrors'])- expect(logic.values.stepValidationErrors.review).toEqual([- '"Spring list" is still matching people. You can launch when it finishes.',- ])+ expect(logic.values.stepValidationErrors.review).toEqual(expectedErrors)})it.each([diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.tsindex 278a59140364..818038813c07 100644--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts@@ -79,9 +79,19 @@ import {stateToRRule,} from '../Workflows/hogflows/steps/components/rrule-helpers'import type { UtmTagValues } from '../Workflows/hogflows/steps/components/UtmTagFields'+import {+ REALTIME_COHORT_STATUS_POLL_MS,+ isRealtimeCohortUnresolved,+} from '../Workflows/hogflows/steps/realtimeCohortStatus'import { recipientEmailProperty } from '../Workflows/hogflows/steps/recipientEmail'import { ResourceSaveQueue } from '../Workflows/resourceSaveQueue'-import { audienceCohortIds, audienceCohortLaunchError, toAudienceCohort } from './audience/audienceList'+import {+ type AudienceCohortLaunchError,+ audienceCohortIds,+ audienceCohortLaunchError,+ blockingCohortLaunchErrors,+ toAudienceCohort,+} from './audience/audienceList'import {AUDIENCE_PREFILL_PARAM,type BroadcastPrefill,@@ -252,7 +262,7 @@ export interface broadcastWizardLogicValues {aiEmailDraftApplied: booleanaiEmailDraftLoading: booleanaiEmailDraftStatus: AiEmailDraftStatusProps | null- audienceCohortLaunchErrors: string[]+ audienceCohortLaunchErrors: AudienceCohortLaunchError[]audienceProperties: AnyPropertyFilter[]batchJobs: HogFlowBatchJobApi[]batchJobsLoading: boolean@@ -487,8 +497,8 @@ export interface broadcastWizardLogicActions {sendToEveryoneAfterRejectedLink: () => {value: true}- setAudienceCohortLaunchErrors: (errors: string[]) => {- errors: string[]+ setAudienceCohortLaunchErrors: (errors: AudienceCohortLaunchError[]) => {+ errors: AudienceCohortLaunchError[]}setAudienceProperties: (properties: AnyPropertyFilter[]) => {properties: AnyPropertyFilter[]@@ -609,7 +619,8 @@ export interface broadcastWizardLogicMeta {integrationsLoading: boolean,linkAudienceRejected: boolean,audienceProperties: AnyPropertyFilter[],- audienceCohortLaunchErrors: string[]+ audienceCohortLaunchErrors: AudienceCohortLaunchError[],+ blastRadius: BlastRadiusApi | null) => Record<BroadcastWizardStep, string[]>currentStepHasErrors: (stepValidationErrors: Record<BroadcastWizardStep, string[]>,@@ -670,7 +681,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([prefillFromLink: (prefill: BroadcastPrefill) => ({ prefill }),saveName: true,setAudienceProperties: (properties: AnyPropertyFilter[]) => ({ properties }),- setAudienceCohortLaunchErrors: (errors: string[]) => ({ errors }),+ setAudienceCohortLaunchErrors: (errors: AudienceCohortLaunchError[]) => ({ errors }),rejectLinkAudience: true,sendToEveryoneAfterRejectedLink: true,setGoalEnabled: (enabled: boolean) => ({ enabled }),@@ -866,7 +877,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([},],audienceCohortLaunchErrors: [- [] as string[],+ [] as AudienceCohortLaunchError[],{setAudienceCohortLaunchErrors: (_, { errors }) => errors,},@@ -1196,6 +1207,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([s.linkAudienceRejected,s.audienceProperties,s.audienceCohortLaunchErrors,+ s.blastRadius,],(goalEnabled: boolean,@@ -1208,7 +1220,8 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([integrationsLoading: boolean,linkAudienceRejected: boolean,audienceProperties: AnyPropertyFilter[],- audienceCohortLaunchErrors: string[]+ audienceCohortLaunchErrors: AudienceCohortLaunchError[],+ blastRadius: BlastRadiusApi | null): Record<BroadcastWizardStep, string[]> => {const errors: Record<BroadcastWizardStep, string[]> = {recipients: [],@@ -1260,7 +1273,9 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([errors.review.push(senderError)}// Only launch waits for a list to match. Editing the other steps can go on meanwhile.- errors.review.push(...audienceCohortLaunchErrors)+ errors.review.push(+ ...blockingCohortLaunchErrors(audienceCohortLaunchErrors, blastRadius?.realtime_cohort?.id)+ )return errors},@@ -1775,8 +1790,9 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([const cohortErrors = await loadAudienceCohortLaunchErrors(projectId, values.audienceProperties)breakpoint()actions.setAudienceCohortLaunchErrors(cohortErrors)- if (cohortErrors.length > 0) {- lemonToast.error(cohortErrors[0])+ const blockingErrors = blockingCohortLaunchErrors(cohortErrors, values.blastRadius?.realtime_cohort?.id)+ if (blockingErrors.length > 0) {+ lemonToast.error(blockingErrors[0])actions.launchBroadcastFinished()return}@@ -1852,12 +1868,16 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([await hogFlowsSchedulesDestroy(projectId, broadcastId, id)}activated = await activate()- await hogFlowsBatchJobsCreate(projectId, broadcastId, {+ const batchJob = await hogFlowsBatchJobsCreate(projectId, broadcastId, {hog_flow: broadcastId,variables: {},confirm_token: blastRadius.confirm_token,} as any)- lemonToast.success('Broadcast is sending')+ lemonToast.success(+ batchJob.status === 'waiting'+ ? 'Broadcast will send once its cohort finishes calculating'+ : 'Broadcast is sending'+ )} else {await hogFlowsSchedulesCreate(projectId, broadcastId, wizardSchedule(values) as any)for (const id of oldScheduleIds) {@@ -2105,6 +2125,30 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([loadBlastRadius: () => {actions.loadRecipientsWithoutEmail(null)},+ loadBlastRadiusSuccess: async ({ blastRadius }, breakpoint) => {+ // The send button reads the realtime cohort's status from the audience size, and the cohort can+ // finish or recover while the page stays open. Only the size is refreshed, not the recipient check.+ if (!values.currentProjectId || !isRealtimeCohortUnresolved(blastRadius?.realtime_cohort)) {+ return+ }+ for (;;) {+ await breakpoint(REALTIME_COHORT_STATUS_POLL_MS)+ let refreshed: BlastRadiusApi | null = null+ try {+ refreshed = await hogFlowsUserBlastRadiusCreate(String(values.currentProjectId), {+ filters: { properties: values.audienceProperties },+ dedupe_key: 'email',+ })+ } catch {+ // A failed check tries again on the next interval.+ }+ breakpoint()+ if (refreshed) {+ actions.loadBlastRadiusSuccess(refreshed)+ return+ }+ }+ },setEmail: () => {if (values.currentStep === 'review' &&@@ -2223,15 +2267,15 @@ async function createDraft(projectId: string, values: broadcastWizardLogicType['async function loadAudienceCohortLaunchErrors(projectId: string,audienceProperties: AnyPropertyFilter[]-): Promise<string[]> {+): Promise<AudienceCohortLaunchError[]> {const ids = audienceCohortIds(audienceProperties)const results = await Promise.allSettled(ids.map((id) => cohortsRetrieve(projectId, id)))// A cohort that fails to load doesn't block launch, so a lookup error can't stop every send.- return results.flatMap((result, index) =>- result.status === 'fulfilled'- ? (audienceCohortLaunchError(toAudienceCohort(ids[index], result.value)) ?? [])- : []- )+ return results.flatMap((result, index) => {+ const message =+ result.status === 'fulfilled' ? audienceCohortLaunchError(toAudienceCohort(ids[index], result.value)) : null+ return message ? [{ cohortId: ids[index], message }] : []+ })}function captureLaunchFailed(diff --git a/products/workflows/frontend/Broadcasts/steps/BroadcastReviewStep.tsx b/products/workflows/frontend/Broadcasts/steps/BroadcastReviewStep.tsxindex 135a0f942084..c486b4d60c57 100644--- a/products/workflows/frontend/Broadcasts/steps/BroadcastReviewStep.tsx+++ b/products/workflows/frontend/Broadcasts/steps/BroadcastReviewStep.tsx@@ -11,6 +11,8 @@ import { humanFriendlyNumber } from 'lib/utils/numbers'import { AiEmailDraftStatus } from '../../MessageAudience/AiEmailDraftStatus'import { optOutCategoriesLogic } from '../../OptOuts/optOutCategoriesLogic'import { MissingRecipientEmailBanner } from '../../Workflows/hogflows/steps/components/MissingRecipientEmailBanner'+import { RealtimeCohortCalculatingBanner } from '../../Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner'+import { isRealtimeCohortCalculating } from '../../Workflows/hogflows/steps/realtimeCohortStatus'import { BroadcastAudienceCohorts } from '../audience/BroadcastAudienceCohorts'import { broadcastAudienceCohortsLogic } from '../audience/broadcastAudienceCohortsLogic'import { BroadcastEmailPreview } from '../BroadcastEmailPreview'@@ -110,7 +112,7 @@ export function BroadcastReviewStep(): JSX.Element {<ReviewRow label="Recipients" step="recipients">{blastRadiusLoading ? (<Spinner />- ) : blastRadius ? (+ ) : isRealtimeCohortCalculating(blastRadius?.realtime_cohort) ? null : blastRadius ? (<span>Approximately {humanFriendlyNumber(blastRadius.affected)} of{' '}{humanFriendlyNumber(blastRadius.total)} people@@ -118,6 +120,7 @@ export function BroadcastReviewStep(): JSX.Element {) : (<span className="text-warning">Couldn't estimate the audience size</span>)}+ <RealtimeCohortCalculatingBanner status={blastRadius?.realtime_cohort} />{audienceProperties.length > 0 ? (<div className="flex flex-col gap-2">{nonCohortAudience.length > 0 ? (diff --git a/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx b/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsxindex 50b6a4b73c43..b1725219fb12 100644--- a/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx+++ b/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx@@ -13,6 +13,8 @@ import { WorkflowLogicProps, workflowLogic } from '../workflowLogic'import { hogFlowManualTriggerButtonLogic } from './HogFlowManualTriggerButtonLogic'import { batchTriggerLogic, getAudienceDedupeKey, hogFlowSendsEmail } from './steps/batchTriggerLogic'import { MissingRecipientEmailBanner } from './steps/components/MissingRecipientEmailBanner'+import { RealtimeCohortCalculatingBanner } from './steps/components/RealtimeCohortCalculatingBanner'+import { isRealtimeCohortCalculating, realtimeCohortSendBlockedReason } from './steps/realtimeCohortStatus'import { recipientEmailProperties } from './steps/recipientEmail'const TriggerPopover = ({@@ -54,7 +56,10 @@ const TriggerPopover = ({return ''}- const getButtonText = (): string => `Run workflow${blastRadiusSuffix()}`+ const realtimeCohort = workflow?.trigger?.type === 'batch' ? blastRadius?.realtime_cohort : undefined+ // The cohort has no size to show until it finishes calculating.+ const getButtonText = (): string =>+ isRealtimeCohortCalculating(realtimeCohort) ? 'Run when calculated' : `Run workflow${blastRadiusSuffix()}`const variablesSection =!workflow?.variables || workflow.variables.length === 0 ? (@@ -119,6 +124,7 @@ const TriggerPopover = ({audienceSize={blastRadius?.affected}/>))}+ <RealtimeCohortCalculatingBanner status={realtimeCohort} /><div className="flex justify-end border-t pt-3"><LemonButtontype="primary"@@ -127,7 +133,7 @@ const TriggerPopover = ({disabledReason={blastRadiusExceeded && blastRadius?.limit != null? `Your audience is above this project's batch limit of ${humanFriendlyNumber(blastRadius.limit)} ${isAccountAudience ? 'accounts' : 'users'}. Add filters to narrow it.${hogFlowSendsEmail(workflow) ? ' The limit rises as the project builds a clean sending history.' : ''}`- : undefined+ : realtimeCohortSendBlockedReason(realtimeCohort)}onClick={() => {if (workflow?.trigger?.type === 'batch') {diff --git a/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.test.ts b/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.test.tsindex c2c427de92ae..a5fc3615a849 100644--- a/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.test.ts+++ b/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.test.ts@@ -73,6 +73,57 @@ describe('batchTriggerLogic', () => {})})+ describe('realtime cohort status', () => {+ it('refreshes until the cohort is ready, past a failed check', async () => {+ const states = ['needs_attention', 'error', 'ready']+ let calls = 0+ useMocks({+ post: {+ '/api/projects/:team_id/hog_flows/user_blast_radius/': async ({ request }) => {+ // The recipient email check posts here too, with an extra condition.+ const { filters } = (await request.json()) as { filters: { properties: unknown[] } }+ if (filters.properties.length > 1) {+ return [200, { affected: 0, total: 0 }]+ }+ const state = states[Math.min(calls++, states.length - 1)]+ if (state === 'error') {+ return [500, { detail: 'Simulated outage' }]+ }+ return [+ 200,+ {+ affected: 0,+ total: 0,+ realtime_cohort: { id: 42, name: 'Upgraded', state, percent_complete: null },+ },+ ]+ },+ },+ })+ initKeaTests()+ const logic = batchTriggerLogic({+ id: 'realtime-status',+ filters: { properties: [{ key: 'id', type: 'cohort', value: 42, operator: 'in' }] as any },+ })+ logic.mount()+ await expectLogic(logic).toDispatchActions(['loadBlastRadiusSuccess'])+ expect(logic.values.blastRadius?.realtime_cohort?.state).toEqual('needs_attention')++ jest.useFakeTimers()+ try {+ await jest.advanceTimersByTimeAsync(20000)+ await expectLogic(logic).toDispatchActions(['loadBlastRadiusSuccess'])+ expect(logic.values.blastRadius?.realtime_cohort?.state).toEqual('ready')++ await jest.advanceTimersByTimeAsync(30000)+ expect(calls).toEqual(3)+ } finally {+ jest.useRealTimers()+ logic.unmount()+ }+ })+ })+describe('recipientsWithoutEmail', () => {const audience = { properties: [{ key: 'plan', value: 'pro', operator: 'exact', type: 'person' }] }diff --git a/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts b/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.tsindex 2282d023e522..5919d573ad63 100644--- a/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts+++ b/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts@@ -13,7 +13,7 @@ import {} from 'kea'import { loaders } from 'kea-loaders'-import api, { ApiError } from 'lib/api'+import { ApiError } from 'lib/api'import { objectsEqual } from 'lib/utils/objects'import { projectLogic } from 'scenes/projectLogic'@@ -21,6 +21,7 @@ import { hogFlowsUserBlastRadiusCreate } from 'products/workflows/frontend/generimport { BlastRadiusApi } from 'products/workflows/frontend/generated/api.schemas'import { HogFlowAction } from '../types'+import { REALTIME_COHORT_STATUS_POLL_MS, isRealtimeCohortUnresolved } from './realtimeCohortStatus'import { recipientEmailProperty } from './recipientEmail'export function getAudienceDedupeKey(workflow?: { actions?: HogFlowAction[] } | null): 'email' | undefined {@@ -129,6 +130,20 @@ export type batchTriggerLogicType = MakeLogicType<batchTriggerLogicMeta>+async function requestBlastRadius(+ projectId: number | null,+ props: BatchTriggerLogicProps+): Promise<BlastRadiusApi | null> {+ if (!projectId || !props.filters) {+ return null+ }+ return await hogFlowsUserBlastRadiusCreate(String(projectId), {+ filters: props.filters,+ dedupe_key: props.dedupeKey ?? null,+ sends_email: props.sendsEmail ?? true,+ })+}+export const batchTriggerLogic = kea<batchTriggerLogicType>([path(['products', 'workflows', 'frontend', 'Workflows', 'hogflows', 'steps', 'batchTriggerLogic']),props({} as BatchTriggerLogicProps),@@ -198,22 +213,36 @@ export const batchTriggerLogic = kea<batchTriggerLogicType>([null as BlastRadiusApi | null,{loadBlastRadius: async () => {- if (!props.filters) {- return null- }- return await api.hogFlows.getBatchTriggerBlastRadius(- props.filters,- props.dedupeKey,- props.sendsEmail- )+ return await requestBlastRadius(values.currentProjectId, props)},},],})),- listeners(({ actions }) => ({+ listeners(({ actions, props, values }) => ({loadBlastRadius: () => {actions.loadRecipientsWithoutEmail(null)},+ loadBlastRadiusSuccess: async ({ blastRadius }, breakpoint) => {+ // Run reads the realtime cohort's status from the audience size, and the cohort can finish or recover+ // while the popover stays open. Only the size is refreshed, not the recipient check.+ if (!props.filters || !isRealtimeCohortUnresolved(blastRadius?.realtime_cohort)) {+ return+ }+ for (;;) {+ await breakpoint(REALTIME_COHORT_STATUS_POLL_MS)+ let refreshed: BlastRadiusApi | null = null+ try {+ refreshed = await requestBlastRadius(values.currentProjectId, props)+ } catch {+ // A failed check tries again on the next interval.+ }+ breakpoint()+ if (refreshed) {+ actions.loadBlastRadiusSuccess(refreshed)+ return+ }+ }+ },loadBlastRadiusFailure: ({ errorObject }) => {const apiError = errorObject as ApiError | undefinedconst message =diff --git a/products/workflows/frontend/Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner.stories.tsx b/products/workflows/frontend/Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner.stories.tsxnew file mode 100644index 000000000000..1f86f7f07e2a--- /dev/null+++ b/products/workflows/frontend/Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner.stories.tsx@@ -0,0 +1,23 @@+import { Meta, StoryFn } from '@storybook/react'++import { RealtimeCohortStatusApi } from 'products/workflows/frontend/generated/api.schemas'++import { RealtimeCohortCalculatingBanner } from './RealtimeCohortCalculatingBanner'++const meta: Meta<typeof RealtimeCohortCalculatingBanner> = {+ title: 'Products/Workflows/Steps/Realtime cohort calculating banner',+ component: RealtimeCohortCalculatingBanner,+}+export default meta++const Template: StoryFn<{ status: RealtimeCohortStatusApi }> = ({ status }) => (+ <div className="w-120">+ <RealtimeCohortCalculatingBanner status={status} />+ </div>+)++export const Calculating = Template.bind({})+Calculating.args = { status: { id: 2, name: 'Viewed pricing', state: 'building', percent_complete: 40 } }++export const NeedsAttention = Template.bind({})+NeedsAttention.args = { status: { id: 2, name: 'Viewed pricing', state: 'needs_attention', percent_complete: null } }diff --git a/products/workflows/frontend/Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner.tsx b/products/workflows/frontend/Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner.tsxnew file mode 100644index 000000000000..3eff859dbd26--- /dev/null+++ b/products/workflows/frontend/Workflows/hogflows/steps/components/RealtimeCohortCalculatingBanner.tsx@@ -0,0 +1,37 @@+import { LemonBanner, Link } from '@posthog/lemon-ui'++import { urls } from 'scenes/urls'++import { RealtimeCohortStatusApi } from 'products/workflows/frontend/generated/api.schemas'++import { isRealtimeCohortCalculating } from '../realtimeCohortStatus'++export interface RealtimeCohortCalculatingBannerProps {+ /** The audience's realtime cohort, when the audience is one. */+ status: RealtimeCohortStatusApi | null | undefined+}++export function RealtimeCohortCalculatingBanner({ status }: RealtimeCohortCalculatingBannerProps): JSX.Element | null {+ if (!status) {+ return null+ }+ const cohort = <Link to={urls.cohort(status.id)}>{status.name}</Link>+ if (isRealtimeCohortCalculating(status)) {+ return (+ <LemonBanner type="info">+ Cohort {cohort} is still calculating+ {/* Its own element, so page translation can't detach the text that each status refresh changes. */}+ {status.percent_complete != null ? <span> ({status.percent_complete}% done)</span> : null}. This send+ waits for it and starts on its own once it's ready.+ </LemonBanner>+ )+ }+ if (status.state === 'needs_attention') {+ return (+ <LemonBanner type="warning">+ Cohort {cohort} has a problem calculating, so this send can't start. Open the cohort to check it.+ </LemonBanner>+ )+ }+ return null+}diff --git a/products/workflows/frontend/Workflows/hogflows/steps/realtimeCohortStatus.ts b/products/workflows/frontend/Workflows/hogflows/steps/realtimeCohortStatus.tsnew file mode 100644index 000000000000..c79a3a0535d4--- /dev/null+++ b/products/workflows/frontend/Workflows/hogflows/steps/realtimeCohortStatus.ts@@ -0,0 +1,23 @@+import { RealtimeCohortStatusApi } from 'products/workflows/frontend/generated/api.schemas'++/** How often an open send screen refreshes a realtime cohort's status until the cohort is ready. */+export const REALTIME_COHORT_STATUS_POLL_MS = 10000++/** Whether the audience's realtime cohort can still change state, so a send screen keeps checking it. */+export function isRealtimeCohortUnresolved(status: RealtimeCohortStatusApi | null | undefined): boolean {+ return !!status && status.state !== 'ready'+}++/** Whether the audience's realtime cohort is still calculating, so a send waits for it. */+export function isRealtimeCohortCalculating(status: RealtimeCohortStatusApi | null | undefined): boolean {+ return status?.state === 'building' || status?.state === 'rebuilding'+}++/** Why a send can't start, when the audience's realtime cohort can't finish calculating. */+export function realtimeCohortSendBlockedReason(+ status: RealtimeCohortStatusApi | null | undefined+): string | undefined {+ return status?.state === 'needs_attention'+ ? `Cohort '${status.name}' has a problem calculating. Open the cohort to check it.`+ : undefined+}