import React, { FC, useState, Fragment } from 'react'; import { ButtonGroup, Button, Row, Badge } from 'reactstrap'; import CollapsibleAlertPanel from './CollapsibleAlertPanel'; import Checkbox from '../../Checkbox'; import { isPresent } from '../../utils/func'; export type RuleState = keyof RuleStatus; export interface Rule { alerts: Alert[]; annotations: Record; duration: number; health: string; labels: Record; name: string; query: string; state: RuleState; type: string; } export interface RuleStatus { firing: T; pending: T; inactive: T; } export interface AlertsProps { groups?: RuleGroup[]; statsCount: RuleStatus; } interface Alert { labels: Record; state: RuleState; value: string; annotations: Record; activeAt: string; } interface RuleGroup { name: string; file: string; rules: Rule[]; interval: number; } const AlertsContent: FC = ({ groups = [], statsCount }) => { const [state, setState] = useState>({ firing: true, pending: true, inactive: true, }); const [showAnnotations, setShowAnnotations] = useState(false); const toggle = (ruleState: RuleState) => () => { setState({ ...state, [ruleState]: !state[ruleState], }); }; return ( <> setShowAnnotations(!showAnnotations)} > Show annotations {groups.map((group, i) => { return ( {group.file} > {group.name} {group.rules.map((rule, j) => { return ( state[rule.state] && ( ) ); })} ); })} ); }; interface StatusBadgesProps { rules: Rule[]; } const StatusBadges: 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;