1
import React, { FC } from 'react';
2
import { APIResponse } from '../../hooks/useFetch';
3
import { Alert, Table, Badge } from 'reactstrap';
4
import { Link } from 'react-router-dom';
5
import { formatRelative, createExpressionLink, humanizeDuration, formatDuration } from '../../utils';
6
import { Rule } from '../../types/types';
7
import { now } from 'moment';
9
interface RulesContentProps {
10
response: APIResponse<RulesMap>;
18
evaluationTime: string;
19
lastEvaluation: string;
22
export interface RulesMap {
26
const GraphExpressionLink: FC<{ expr: string; text: string; title: string }> = (props) => {
29
<strong>{props.title}:</strong>
30
<Link className="ml-4" to={createExpressionLink(props.expr)}>
38
export const RulesContent: FC<RulesContentProps> = ({ response }) => {
39
const getBadgeColor = (state: string) => {
53
const groups: RuleGroup[] = response.data.groups;
57
{groups.map((g, i) => {
59
<Table bordered key={i}>
63
<a href={'#' + g.name}>
64
<h4 id={g.name} className="text-break">
70
<h4>Interval: {humanizeDuration(parseFloat(g.interval) * 1000)}</h4>
73
<h4>{formatRelative(g.lastEvaluation, now())}</h4>
76
<h4>{humanizeDuration(parseFloat(g.evaluationTime) * 1000)}</h4>
81
<tr className="font-weight-bold">
85
<td>Last Evaluation</td>
86
<td>Evaluation Time</td>
88
{g.rules.map((r, i) => {
91
<td className="rule-cell">
93
<GraphExpressionLink title="alert" text={r.name} expr={`ALERTS{alertname="${r.name}"}`} />
95
<GraphExpressionLink title="record" text={r.name} expr={r.name} />
97
<GraphExpressionLink title="expr" text={r.query} expr={r.query} />
100
<strong>for:</strong> {formatDuration(r.duration * 1000)}
103
{r.keepFiringFor > 0 && (
105
<strong>keep_firing_for:</strong> {formatDuration(r.keepFiringFor * 1000)}
108
{r.labels && Object.keys(r.labels).length > 0 && (
110
<strong>labels:</strong>
111
{Object.entries(r.labels).map(([key, value]) => (
112
<div className="ml-4" key={key}>
118
{r.alerts && r.annotations && Object.keys(r.annotations).length > 0 && (
120
<strong>annotations:</strong>
121
{Object.entries(r.annotations).map(([key, value]) => (
122
<div className="ml-4" key={key}>
130
<Badge color={getBadgeColor(r.health)}>{r.health.toUpperCase()}</Badge>
132
<td>{r.lastError ? <Alert color="danger">{r.lastError}</Alert> : null}</td>
133
<td>{formatRelative(r.lastEvaluation, now())}</td>
134
<td>{humanizeDuration(parseFloat(r.evaluationTime) * 1000)}</td>