Skip to content

Commit

Permalink
feat: allow to match any of the filters (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Dec 10, 2024
1 parent 91b121e commit fdda27e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/components/alerts/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function AlertsTemplate(props: Props) {
const [view, setView] = useState<ViewConfig | null>(null)
const [alertGroups, setAlertGroups] = useState<Group[]>([])
const [filters] = useQueryState('filters', parseAsArrayOf(parseAsFilter, ';'))
const [filterMatch] = useQueryState('match', { defaultValue: 'all' })

useHotkeys('r', () => refreshAlerts(), []);

Expand All @@ -56,13 +57,11 @@ export function AlertsTemplate(props: Props) {

const groupBy = group !== '' ? group : view.groupBy

// Merge view filters with query filters
const alertFilters = view.filters.concat(filters || [])

// Flatten, filter & sort alerts
const flatAlerts = Object.values(alerts).
reduce((acc, val) => acc.concat(val), []).
filter(alertFilter(alertFilters))
const flatAlerts = Object.values(alerts)
.reduce((acc, val) => acc.concat(val), [])
.filter(alertFilter(view.filters, view.filtersMatch === 'all'))
.filter(alertFilter(filters || [], filterMatch === 'all'))
flatAlerts.sort(alertSort)

setFlattenedAlerts(flatAlerts)
Expand Down
8 changes: 5 additions & 3 deletions src/components/alerts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export function flattenAlerts(alerts: Record<string, Alert[]>): Alert[] {
return Object.values(alerts).reduce((acc, val) => acc.concat(val), [])
}

export function alertFilter(filters: LabelFilter[]): (alert: Alert) => boolean {
export function alertFilter(filters: LabelFilter[], matchAll = true): (alert: Alert) => boolean {
return (alert: Alert) => {
return filters.map(filter => {
const matches = filters.map(filter => {
if (showOnlyActive && alert.status.state !== 'active') {
return false
}
Expand All @@ -74,7 +74,9 @@ export function alertFilter(filters: LabelFilter[]): (alert: Alert) => boolean {
}
return filter.value.includes(value)
}
}).every(Boolean)
})

return matchAll ? matches.every(Boolean) : matches.some(Boolean)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ViewsSchema = z.object({
name: z.string().optional().default(''),
groupBy: z.string(),
category: z.string().optional().default(''),
filtersMatch: z.enum(['all', 'any']).optional().default('all'),
filters: z.array(
z.object({
label: z.string(),
Expand Down

0 comments on commit fdda27e

Please sign in to comment.