: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
import { useValues } from 'kea'
import { LemonSelect, Tooltip } from '@posthog/lemon-ui'
import { NextScheduledRun } from 'lib/components/ScheduledRunStatus'
import { TZLabel } from 'lib/components/TZLabel'
import { upgradeModalLogic } from 'lib/components/UpgradeModal/upgradeModalLogic'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
import { AlertCalculationInterval } from '~/queries/schema/schema-general'
import { AvailableFeature } from '~/types'
import { AlertDefinitionRow } from 'products/alerts/frontend/components/AlertDefinition'
import { AlertFormType } from 'products/alerts/frontend/logic/alertFormLogic'
import {
canSetAlertScheduleStartTime,
cadenceFinerThanInsightInterval,
getAlertScheduleStartMinute,
selectAlertCalculationInterval,
scheduleStartTimeForInterval,
scheduleStartTimeForMinute,
} from 'products/alerts/frontend/logic/alertIntervalHelpers'
import { approximateNextAlertRun } from 'products/alerts/frontend/logic/alertSchedulingStale'
import {
AlertType,
isHogQLAlertConfig,
isTrendsAlertConfig,
supportsOngoingInterval,
supportsTimeWindow,
} from 'products/alerts/frontend/types'
import { getAlertIntervalOptions } from './editAlertModalUtils'
export interface AlertIntervalRowProps {
alertForm: AlertFormType
creatingNewAlert: boolean
alert: AlertType | null | undefined
trendInterval: string | null | undefined
nextPlannedEvaluationStale: boolean
canCheckOngoingInterval: boolean
onSetAlertFormValue: <K extends keyof AlertFormType>(key: K, value: AlertFormType[K]) => void
}
function getHogQLEvaluatedText(alertForm: AlertFormType): string {
if (!isHogQLAlertConfig(alertForm.config)) {
return ''
}
if (alertForm.config.evaluation === 'any_row') {
return 'and check every row of the result'
}
if (alertForm.config.evaluation === 'first_row') {
return "and evaluate the query's first (newest) row"
}
return "and evaluate the query's last (newest) row"
}
export function AlertIntervalRow({
alertForm,
creatingNewAlert,
alert,
trendInterval,
nextPlannedEvaluationStale,
canCheckOngoingInterval,
onSetAlertFormValue,
}: AlertIntervalRowProps): JSX.Element {
const { hasAvailableFeature } = useValues(userLogic)
const { guardAvailableFeature } = useValues(upgradeModalLogic)
const { currentTeam } = useValues(teamLogic)
const hasHighFrequencyAlertsEntitlement = hasAvailableFeature(AvailableFeature.HIGH_FREQUENCY_ALERTS)
const hasRealTimeAlertsEntitlement = hasAvailableFeature(AvailableFeature.REAL_TIME_ALERTS)
let evaluatedWindow: JSX.Element
if (!supportsTimeWindow(alertForm.config)) {
evaluatedWindow = <div>{getHogQLEvaluatedText(alertForm)}</div>
} else {
const period =
isTrendsAlertConfig(alertForm.config) && alertForm.config.check_ongoing_interval ? 'current' : 'last'
evaluatedWindow = (
<div data-attr="alertForm-trend-interval">
and check {period}{' '}
<Tooltip
title={
<>
Set by the insight's <b>grouped by</b> interval. Edit the insight to change it.
</>
}
>
<span className="font-semibold underline decoration-dotted cursor-help">
{trendInterval ?? 'day'}
</span>
</Tooltip>
</div>
)
}
let nextEvaluation: JSX.Element | null
if (alertForm.calculation_interval === AlertCalculationInterval.REAL_TIME) {
nextEvaluation = null
} else if (creatingNewAlert || nextPlannedEvaluationStale) {
const { earliest, latest } = approximateNextAlertRun(
alertForm.calculation_interval,
currentTeam?.timezone ?? 'UTC',
alertForm.schedule_start_time
)
nextEvaluation = (
<NextScheduledRun label="Next planned evaluation:">
<span className="flex flex-wrap items-center gap-x-1">
<span>{earliest.isSame(latest) ? 'Approximately' : 'Approximately between'}</span>
<TZLabel time={earliest} displayTimezone={currentTeam?.timezone ?? 'UTC'} />
{!earliest.isSame(latest) && (
<>
<span>and</span>
<TZLabel time={latest} displayTimezone={currentTeam?.timezone ?? 'UTC'} />
</>
)}
</span>
</NextScheduledRun>
)
} else if (alert) {
let status: JSX.Element
if (alert.next_check_at) {
status = <TZLabel time={alert.next_check_at} />
} else {
status = <span>We're calculating this. This can take a few minutes.</span>
}
nextEvaluation = (
<NextScheduledRun
label="Next planned evaluation:"
loading={!nextPlannedEvaluationStale && !alert.next_check_at}
>
{status}
</NextScheduledRun>
)
} else {
nextEvaluation = null
}
const scheduleLabel =
alertForm.calculation_interval === AlertCalculationInterval.REAL_TIME ? 'Run alert' : 'Run alert every'
return (
<div className="space-y-2">
<AlertDefinitionRow label={scheduleLabel}>
<LemonField name="calculation_interval">
{({ value, onChange }) => (
<LemonSelect
fullWidth
className="w-36 shrink-0 whitespace-nowrap"
data-attr="alertForm-calculation-interval"
value={value}
options={getAlertIntervalOptions(
hasHighFrequencyAlertsEntitlement,
hasRealTimeAlertsEntitlement,
alertForm.detector_config?.type === 'llm'
)}
onChange={(interval) => {
selectAlertCalculationInterval(interval, {
guardAvailableFeature,
onSelect: (selected) => {
onChange(selected)
onSetAlertFormValue(
'schedule_start_time',
scheduleStartTimeForInterval(selected, alertForm.schedule_start_time)
)
if (
cadenceFinerThanInsightInterval(selected, trendInterval) &&
canCheckOngoingInterval &&
supportsOngoingInterval(alertForm.config) &&
alertForm.config.check_ongoing_interval === undefined
) {
onSetAlertFormValue('config', {
...alertForm.config,
check_ongoing_interval: true,
})
}
},
hasHighFrequencyAlertsEntitlement,
hasRealTimeAlertsEntitlement,
})
}}
/>
)}
</LemonField>
{canSetAlertScheduleStartTime(alertForm.calculation_interval) && (
<>
<span>at minute</span>
<LemonInput
className="w-20 shrink-0"
type="number"
min={0}
max={59}
step={1}
value={getAlertScheduleStartMinute(alertForm.schedule_start_time)}
onFocus={(event) => event.currentTarget.select()}
onChange={(minute) =>
onSetAlertFormValue('schedule_start_time', scheduleStartTimeForMinute(minute))
}
aria-label="Alert evaluation minute"
data-attr="alertForm-schedule-start-time"
/>
<span>of the hour</span>
</>
)}
{evaluatedWindow}
</AlertDefinitionRow>
{nextEvaluation}
{alertForm.calculation_interval !== AlertCalculationInterval.REAL_TIME &&
!alertForm.schedule_start_time && (
<p className="text-sm text-muted m-0">
Automatic checks use a consistent minute for each alert so evaluations are spread out.
</p>
)}
</div>
)
}