import React, { FC } from 'react'; import { RouteComponentProps } from '@reach/router'; import { APIResponse } from '../../hooks/useFetch'; import { Alert, Table, Badge } from 'reactstrap'; import { Link } from '@reach/router'; import { formatRelative, createExpressionLink, humanizeDuration, formatDuration } from '../../utils'; import { Rule } from '../../types/types'; import { now } from 'moment'; interface RulesContentProps { response: APIResponse; } interface RuleGroup { name: string; file: string; rules: Rule[]; evaluationTime: string; lastEvaluation: string; } export interface RulesMap { groups: RuleGroup[]; } const GraphExpressionLink: FC<{ expr: string; text: string; title: string }> = props => { return ( <> {props.title}: {props.text}
); }; export const RulesContent: FC = ({ response }) => { const getBadgeColor = (state: string) => { switch (state) { case 'ok': return 'success'; case 'err': return 'danger'; case 'unknown': return 'warning'; } }; if (response.data) { const groups: RuleGroup[] = response.data.groups; return ( <>

Rules

{groups.map((g, i) => { return ( {g.rules.map((r, i) => { return ( ); })}

{g.name}

{formatRelative(g.lastEvaluation, now())}

{humanizeDuration(parseFloat(g.evaluationTime) * 1000)}

Rule State Error Last Evaluation Evaluation Time
{r.alerts ? ( ) : ( )} {r.duration > 0 && (
for: {formatDuration(r.duration * 1000)}
)} {r.labels && Object.keys(r.labels).length > 0 && (
labels: {Object.entries(r.labels).map(([key, value]) => (
{key}: {value}
))}
)} {r.alerts && r.annotations && Object.keys(r.annotations).length > 0 && (
annotations: {Object.entries(r.annotations).map(([key, value]) => (
{key}: {value}
))}
)}
{r.health.toUpperCase()} {r.lastError ? {r.lastError} : null} {formatRelative(r.lastEvaluation, now())} {humanizeDuration(parseFloat(r.evaluationTime) * 1000)}
); })} ); } return null; };