import React, { FC, useState, Fragment } from 'react'; import { Badge } from 'reactstrap'; import CollapsibleAlertPanel from './CollapsibleAlertPanel'; import Checkbox from '../../components/Checkbox'; import { isPresent } from '../../utils'; import { Rule } from '../../types/types'; export type RuleState = keyof RuleStatus; export interface RuleStatus { firing: T; pending: T; inactive: T; } export interface AlertsProps { groups?: RuleGroup[]; statsCount: RuleStatus; } export interface Alert { labels: Record; state: RuleState; value: string; annotations: Record; activeAt: string; } interface RuleGroup { name: string; file: string; rules: Rule[]; interval: number; } const stateColorTuples: Array<[RuleState, 'success' | 'warning' | 'danger']> = [ ['inactive', 'success'], ['pending', 'warning'], ['firing', 'danger'], ]; const AlertsContent: FC = ({ groups = [], statsCount }) => { const [filter, setFilter] = useState>({ firing: true, pending: true, inactive: true, }); const [showAnnotations, setShowAnnotations] = useState(false); const toggleFilter = (ruleState: RuleState) => () => { setFilter({ ...filter, [ruleState]: !filter[ruleState], }); }; return ( <>
{stateColorTuples.map(([state, color]) => { return ( {state} ({statsCount[state]}) ); })} setShowAnnotations(!showAnnotations)} > Show annotations
{groups.map((group, i) => { const hasFilterOn = group.rules.some(rule => filter[rule.state]); return hasFilterOn ? ( {group.file} > {group.name} {group.rules.map((rule, j) => { return ( filter[rule.state] && ( ) ); })} ) : null; })} ); }; interface GroupInfoProps { rules: Rule[]; } export const GroupInfo: FC = ({ rules, children }) => { const statesCounter = rules.reduce( (acc, r) => { return { ...acc, [r.state]: acc[r.state] + r.alerts.length, }; }, { firing: 0, pending: 0, } ); return (
{children}
{isPresent(statesCounter.inactive) && inactive} {statesCounter.pending > 0 && pending ({statesCounter.pending})} {statesCounter.firing > 0 && firing ({statesCounter.firing})}
); }; AlertsContent.displayName = 'Alerts'; export default AlertsContent;