Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

147 lines
5.3 KiB
TypeScript

import insights from 'insights-js'
import type { GuardAvailableFeatureFn } from 'lib/components/UpgradeModal/upgradeModalLogic'
import { AlertCalculationInterval } from '~/queries/schema/schema-general'
import { AvailableFeature, IntervalType } from '~/types'
export function getDefaultSimulationRange(interval: AlertCalculationInterval): string {
switch (interval) {
case AlertCalculationInterval.REAL_TIME:
return '-1h'
case AlertCalculationInterval.EVERY_15_MINUTES:
return '-12h'
case AlertCalculationInterval.HOURLY:
return '-48h'
case AlertCalculationInterval.DAILY:
return '-30d'
case AlertCalculationInterval.WEEKLY:
return '-12w'
case AlertCalculationInterval.MONTHLY:
return '-12m'
}
}
export const HIGH_FREQUENCY_ALERTS_REQUIRED_MESSAGE =
'15-minute alert intervals require a Boost, Scale, or Enterprise platform add-on.'
const SUB_DAILY_INTERVALS = [
AlertCalculationInterval.HOURLY,
AlertCalculationInterval.EVERY_15_MINUTES,
AlertCalculationInterval.REAL_TIME,
]
export function isSubDailyAlertInterval(interval: AlertCalculationInterval): boolean {
return SUB_DAILY_INTERVALS.includes(interval)
}
const INTERVAL_DISPLAY_LABELS: Record<AlertCalculationInterval, string> = {
[AlertCalculationInterval.REAL_TIME]: 'Real time',
[AlertCalculationInterval.EVERY_15_MINUTES]: 'Every 15 minutes',
[AlertCalculationInterval.HOURLY]: 'Hourly',
[AlertCalculationInterval.DAILY]: 'Daily',
[AlertCalculationInterval.WEEKLY]: 'Weekly',
[AlertCalculationInterval.MONTHLY]: 'Monthly',
}
export function alertIntervalDisplayLabel(interval: AlertCalculationInterval): string {
return INTERVAL_DISPLAY_LABELS[interval]
}
// Twin of _CADENCE_DURATION_MINUTES / _INTERVAL_DURATION_MINUTES in
// products/alerts/backend/evaluation/validation.py — keep the two in sync.
const CADENCE_DURATION_MINUTES: Record<AlertCalculationInterval, number> = {
[AlertCalculationInterval.REAL_TIME]: 2,
[AlertCalculationInterval.EVERY_15_MINUTES]: 15,
[AlertCalculationInterval.HOURLY]: 60,
[AlertCalculationInterval.DAILY]: 60 * 24,
[AlertCalculationInterval.WEEKLY]: 60 * 24 * 7,
[AlertCalculationInterval.MONTHLY]: 60 * 24 * 30,
}
const INSIGHT_INTERVAL_DURATION_MINUTES: Record<IntervalType, number> = {
second: 1 / 60,
minute: 1,
hour: 60,
day: 60 * 24,
week: 60 * 24 * 7,
month: 60 * 24 * 30,
quarter: 60 * 24 * 30 * 3,
year: 60 * 24 * 365,
}
/** An alert re-checks the insight's last completed bucket, whose size is the insight's grouping
* interval. A cadence finer than that bucket re-reads the same frozen value until the bucket closes,
* so evaluating the ongoing (incomplete) bucket is what makes the faster cadence meaningful. */
export function cadenceFinerThanInsightInterval(
cadence: AlertCalculationInterval,
insightInterval: string | null | undefined
): boolean {
const insightMinutes =
INSIGHT_INTERVAL_DURATION_MINUTES[(insightInterval as IntervalType | null) ?? 'day'] ??
INSIGHT_INTERVAL_DURATION_MINUTES.day
return CADENCE_DURATION_MINUTES[cadence] < insightMinutes
}
type EntitlementResult =
| { blocked: true; message: string; feature: AvailableFeature }
| { blocked: false; message: null; feature: null }
export function blockSubmitWithoutEntitlement(
interval: AlertCalculationInterval,
{
hasHighFrequencyAlertsEntitlement,
hasRealTimeAlertsEntitlement,
}: { hasHighFrequencyAlertsEntitlement: boolean; hasRealTimeAlertsEntitlement: boolean }
): EntitlementResult {
if (interval === AlertCalculationInterval.EVERY_15_MINUTES && !hasHighFrequencyAlertsEntitlement) {
return {
blocked: true,
message: HIGH_FREQUENCY_ALERTS_REQUIRED_MESSAGE,
feature: AvailableFeature.HIGH_FREQUENCY_ALERTS,
}
}
if (interval === AlertCalculationInterval.REAL_TIME && !hasRealTimeAlertsEntitlement) {
return {
blocked: true,
message: 'Real-time alert intervals require a Scale or Enterprise plan.',
feature: AvailableFeature.REAL_TIME_ALERTS,
}
}
return { blocked: false, message: null, feature: null }
}
export function selectAlertCalculationInterval(
value: AlertCalculationInterval,
{
guardAvailableFeature,
onSelect,
hasHighFrequencyAlertsEntitlement,
hasRealTimeAlertsEntitlement,
}: {
guardAvailableFeature: GuardAvailableFeatureFn
onSelect: (interval: AlertCalculationInterval) => void
hasHighFrequencyAlertsEntitlement: boolean
hasRealTimeAlertsEntitlement: boolean
}
): boolean {
if (value === AlertCalculationInterval.REAL_TIME) {
insights.capture('alert real time interval selected', {
has_entitlement: hasRealTimeAlertsEntitlement,
})
return guardAvailableFeature(AvailableFeature.REAL_TIME_ALERTS, () => {
onSelect(value)
})
}
if (value === AlertCalculationInterval.EVERY_15_MINUTES) {
insights.capture('alert 15 min interval selected', {
has_entitlement: hasHighFrequencyAlertsEntitlement,
})
return guardAvailableFeature(AvailableFeature.HIGH_FREQUENCY_ALERTS, () => {
onSelect(value)
})
}
onSelect(value)
return true
}