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

343 lines
11 KiB
TypeScript

import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { PaginationManual } from '@hanzo/elements'
import api from 'lib/api'
import { toast } from 'lib/elements/Toast/Toast'
import { objectClean } from 'lib/utils/objects'
import { AlertState } from '~/queries/schema/schema-general'
import { AlertType } from '../types'
import { AlertLogicProps } from './alertLogic'
import { buildAlertFilterConfig } from './alertNotifications'
export interface AlertsLogicProps extends AlertLogicProps {}
export const ALERTS_PER_PAGE = 30
export interface AlertsFilters {
search: string
createdBy: string
}
export const DEFAULT_ALERTS_FILTERS: AlertsFilters = {
search: '',
createdBy: 'All users',
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface alertsLogicValues {
alertDestinationCounts: Record<string, number>
alertDestinationCountsLoading: boolean
alerts: AlertType[]
alertsCount: number
alertsResponse: {
count: number
results: AlertType[]
}
alertsResponseLoading: boolean
alertsSortedByState: AlertType[]
deletingAlertIds: Set<string>
filters: AlertsFilters
isFiltering: boolean
page: number
pagination: PaginationManual
togglingAlertIds: Set<string>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface alertsLogicActions {
deleteAlert: (alert: AlertType) => {
alert: AlertType
}
loadAlertDestinationCounts: (alerts: AlertType[]) => AlertType[]
loadAlertDestinationCountsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadAlertDestinationCountsSuccess: (
alertDestinationCounts: Record<string, number>,
payload?: AlertType[]
) => {
alertDestinationCounts: Record<string, number>
payload?: AlertType[]
}
loadAlerts: () => any
loadAlertsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadAlertsSuccess: (
alertsResponse: {
count: number
results: AlertType[]
},
payload?: any
) => {
alertsResponse: {
count: number
results: AlertType[]
}
payload?: any
}
setAlertDeleting: (
alertId: string,
isDeleting: boolean
) => {
alertId: string
isDeleting: boolean
}
setAlertToggling: (
alertId: string,
isToggling: boolean
) => {
alertId: string
isToggling: boolean
}
setFilters: (filters: Partial<AlertsFilters>) => {
filters: Partial<AlertsFilters>
}
setPage: (page: number) => {
page: number
}
toggleAlertEnabled: (alert: AlertType) => {
alert: AlertType
}
updateAlertInList: (alert: AlertType) => {
alert: AlertType
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface alertsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
alerts: (alertsResponse: { count: number; results: AlertType[] }) => AlertType[]
alertsCount: (alertsResponse: { count: number; results: AlertType[] }) => number
isFiltering: (filters: AlertsFilters) => boolean
alertsSortedByState: (alerts: AlertType[]) => AlertType[]
pagination: (page: number, alertsCount: number) => PaginationManual
}
}
export type alertsLogicType = MakeLogicType<alertsLogicValues, alertsLogicActions, Record<string, any>, alertsLogicMeta>
export const alertsLogic = kea<alertsLogicType>([
path(['lib', 'components', 'Alerts', 'alertsLogic']),
actions({
setPage: (page: number) => ({ page }),
setFilters: (filters: Partial<AlertsFilters>) => ({ filters }),
setAlertToggling: (alertId: string, isToggling: boolean) => ({ alertId, isToggling }),
deleteAlert: (alert: AlertType) => ({ alert }),
setAlertDeleting: (alertId: string, isDeleting: boolean) => ({ alertId, isDeleting }),
toggleAlertEnabled: (alert: AlertType) => ({ alert }),
updateAlertInList: (alert: AlertType) => ({ alert }),
}),
reducers({
alertsResponse: [
{ results: [], count: 0 } as { results: AlertType[]; count: number },
{
updateAlertInList: (state, { alert }) => ({
...state,
results: state.results.map((currentAlert) => (currentAlert.id === alert.id ? alert : currentAlert)),
}),
},
],
page: [
1,
{
setPage: (_, { page }) => page,
},
],
deletingAlertIds: [
new Set<string>(),
{
setAlertDeleting: (state, { alertId, isDeleting }) => {
const nextState = new Set(state)
if (isDeleting) {
nextState.add(alertId)
} else {
nextState.delete(alertId)
}
return nextState
},
},
],
togglingAlertIds: [
new Set<string>(),
{
setAlertToggling: (state, { alertId, isToggling }) => {
const nextState = new Set(state)
if (isToggling) {
nextState.add(alertId)
} else {
nextState.delete(alertId)
}
return nextState
},
},
],
filters: [
DEFAULT_ALERTS_FILTERS,
{
setFilters: (state, { filters }) =>
objectClean({
...state,
...filters,
}),
},
],
}),
loaders(({ values }) => ({
alertsResponse: [
{ results: [], count: 0 } as { results: AlertType[]; count: number },
{
loadAlerts: async () => {
const search = values.filters.search.trim()
const response = await api.alerts.list(undefined, {
limit: ALERTS_PER_PAGE,
offset: (values.page - 1) * ALERTS_PER_PAGE,
...(search ? { search } : {}),
...(values.filters.createdBy !== 'All users' ? { created_by: values.filters.createdBy } : {}),
})
return { results: response.results, count: response.count ?? response.results.length }
},
},
],
alertDestinationCounts: [
{} as Record<string, number>,
{
loadAlertDestinationCounts: async (alerts: AlertType[]) => {
if (alerts.length === 0) {
return {}
}
const alertIds = new Set(alerts.map((alert) => alert.id))
const response = await api.insightsFunctions.list({
types: ['internal_destination'],
filter_groups: alerts.map((alert) => buildAlertFilterConfig(alert.id)),
full: true,
limit: 500,
})
return response.results.reduce<Record<string, number>>((counts, insightsFunction) => {
if (!insightsFunction.enabled) {
return counts
}
const alertIdProperty = insightsFunction.filters?.properties?.find(
(property) => property.key === 'alert_id'
)
const alertId = alertIdProperty?.value
if (typeof alertId === 'string' && alertIds.has(alertId)) {
counts[alertId] = (counts[alertId] ?? 0) + 1
}
return counts
}, {})
},
},
],
})),
selectors(({ actions }) => ({
alerts: [
(s) => [s.alertsResponse],
(response: { count: number; results: AlertType[] }): AlertType[] => response.results,
],
alertsCount: [
(s) => [s.alertsResponse],
(response: { count: number; results: AlertType[] }): number => response.count,
],
isFiltering: [
(s) => [s.filters],
(filters: AlertsFilters): boolean =>
filters.search !== DEFAULT_ALERTS_FILTERS.search ||
filters.createdBy !== DEFAULT_ALERTS_FILTERS.createdBy,
],
alertsSortedByState: [
(s) => [s.alerts],
(alerts: AlertType[]): AlertType[] =>
[...alerts].sort((a, b) => alertComparatorKey(a) - alertComparatorKey(b)),
],
pagination: [
(s) => [s.page, s.alertsCount],
(page: number, count: number): PaginationManual => ({
controlled: true,
pageSize: ALERTS_PER_PAGE,
currentPage: page,
entryCount: count,
onBackward: () => actions.setPage(page - 1),
onForward: () => actions.setPage(page + 1),
}),
],
})),
listeners(({ actions, values }) => ({
deleteAlert: async ({ alert }) => {
actions.setAlertDeleting(alert.id, true)
try {
await api.alerts.delete(alert.id)
actions.loadAlerts()
toast.success(`${alert.name || 'Unnamed alert'} has been deleted`)
} catch {
toast.error("Couldn't delete the alert. Try again.")
} finally {
actions.setAlertDeleting(alert.id, false)
}
},
setPage: () => {
actions.loadAlerts()
},
setFilters: async ({ filters }, breakpoint) => {
if ('search' in filters && filters.search?.trim()) {
await breakpoint(300)
}
if (values.page !== 1) {
actions.setPage(1)
} else {
actions.loadAlerts()
}
},
toggleAlertEnabled: async ({ alert }) => {
actions.setAlertToggling(alert.id, true)
try {
const updatedAlert = await api.alerts.update(alert.id, { enabled: !alert.enabled })
actions.updateAlertInList(updatedAlert)
} catch {
toast.error('Failed to update alert')
} finally {
actions.setAlertToggling(alert.id, false)
}
},
loadAlertsSuccess: ({ alertsResponse }) => {
actions.loadAlertDestinationCounts(alertsResponse.results)
},
})),
afterMount(({ actions }) => actions.loadAlerts()),
])
const alertComparatorKey = (alert: AlertType): number => {
if (!alert.enabled) {
return 100
}
switch (alert.state) {
case AlertState.FIRING:
return 1
case AlertState.ERRORED:
return 2
case AlertState.SNOOZED:
return 3
case AlertState.NOT_FIRING:
return 4
}
}