{
  "$schema": "./.schemas/page.schema.json",
  "title": "Alert rule",
  "icon": "si:grafana",
  "sidebar": false,
  "params": {
    "uid": {
      "example": "disk-full",
      "pattern": "[A-Za-z0-9_-]{1,40}"
    }
  },
  "title_from": {
    "ref": "rule",
    "value": {
      "first": "name",
      "of": "rl"
    }
  },
  "sources": {
    "rule": {
      "url": "http://grafana.local:3000",
      "secret": "grafana",
      "auth": {
        "type": "bearer"
      },
      "every": "1m",
      "requests": {
        "rule": "/api/prometheus/grafana/api/v1/rules?rule_uid={param.uid}&limit_alerts=200",
        "alerts": "/api/alertmanager/grafana/api/v2/alerts?active=true&silenced=true&inhibited=true&filter=__alert_rule_uid__%3D%22{param.uid}%22",
        "silences": "/api/alertmanager/grafana/api/v2/silences",
        "changes": "/api/annotations?type=alert&alertUID={param.uid}&limit=20",
        "perms": "/api/access-control/user/permissions"
      },
      "derive": [
        {
          "name": "all_rules",
          "value": {
            "each": "rule.data.groups",
            "flatten": "rules",
            "fields": {
              "uid": "uid",
              "name": "name",
              "folder": "parent.file",
              "group": "parent.name",
              "every": "parent.interval",
              "health": "health",
              "error": {
                "if": {
                  "all": [
                    {
                      "path": "lastError",
                      "exists": true
                    },
                    {
                      "path": "lastError",
                      "not": ""
                    }
                  ]
                },
                "then": "lastError",
                "else": {
                  "template": ""
                }
              },
              "paused": "isPaused",
              "pending_for": "duration",
              "evaluated": "lastEvaluation",
              "labels": "labels",
              "annotations": "annotations",
              "alerts": "alerts",
              "totals": "totals"
            }
          }
        },
        {
          "name": "rl",
          "value": {
            "each": "all_rules",
            "where": {
              "path": "uid",
              "equals": "{param.uid}"
            },
            "limit": 1,
            "fields": {
              "uid": "uid",
              "name": "name",
              "folder": "folder",
              "group": "group",
              "every": "every",
              "health": "health",
              "error": "error",
              "paused": "paused",
              "pending_for": "pending_for",
              "evaluated": "evaluated",
              "labels": "labels",
              "annotations": "annotations",
              "alerts": "alerts",
              "totals": "totals"
            }
          }
        },
        {
          "name": "found",
          "value": {
            "count": "rl"
          }
        },
        {
          "name": "inst",
          "value": {
            "each": "rl",
            "flatten": "alerts",
            "fields": {
              "word": {
                "path": "state",
                "match": "^([A-Za-z]+)",
                "default": "Normal"
              },
              "reason": {
                "path": "state",
                "match": "\\(([^)]+)\\)",
                "default": ""
              },
              "since": "activeAt",
              "where": {
                "if": {
                  "all": [
                    {
                      "path": "labels.instance",
                      "exists": true
                    },
                    {
                      "path": "labels.instance",
                      "not": ""
                    }
                  ]
                },
                "then": "labels.instance",
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "labels.service",
                        "exists": true
                      },
                      {
                        "path": "labels.service",
                        "not": ""
                      }
                    ]
                  },
                  "then": "labels.service",
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "labels.job",
                          "exists": true
                        },
                        {
                          "path": "labels.job",
                          "not": ""
                        }
                      ]
                    },
                    "then": "labels.job",
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "labels.host",
                            "exists": true
                          },
                          {
                            "path": "labels.host",
                            "not": ""
                          }
                        ]
                      },
                      "then": "labels.host",
                      "else": {
                        "if": {
                          "all": [
                            {
                              "path": "labels.pod",
                              "exists": true
                            },
                            {
                              "path": "labels.pod",
                              "not": ""
                            }
                          ]
                        },
                        "then": "labels.pod",
                        "else": {
                          "template": ""
                        }
                      }
                    }
                  }
                }
              },
              "severity": {
                "if": {
                  "all": [
                    {
                      "path": "labels.severity",
                      "exists": true
                    },
                    {
                      "path": "labels.severity",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{labels.severity|lower}"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "parent.labels.severity",
                        "exists": true
                      },
                      {
                        "path": "parent.labels.severity",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "{parent.labels.severity|lower}"
                  },
                  "else": {
                    "template": ""
                  }
                }
              },
              "summary": {
                "if": {
                  "all": [
                    {
                      "path": "annotations.summary",
                      "exists": true
                    },
                    {
                      "path": "annotations.summary",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "if": {
                    "path": "annotations.summary",
                    "contains": "{{"
                  },
                  "then": {
                    "if": {
                      "all": [
                        {
                          "path": "annotations.description",
                          "exists": true
                        },
                        {
                          "path": "annotations.description",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "if": {
                        "path": "annotations.description",
                        "contains": "{{"
                      },
                      "then": {
                        "if": {
                          "all": [
                            {
                              "path": "parent.annotations.summary",
                              "exists": true
                            },
                            {
                              "path": "parent.annotations.summary",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "if": {
                            "path": "parent.annotations.summary",
                            "contains": "{{"
                          },
                          "then": {
                            "template": ""
                          },
                          "else": "parent.annotations.summary"
                        },
                        "else": {
                          "template": ""
                        }
                      },
                      "else": "annotations.description"
                    },
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "parent.annotations.summary",
                            "exists": true
                          },
                          {
                            "path": "parent.annotations.summary",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "if": {
                          "path": "parent.annotations.summary",
                          "contains": "{{"
                        },
                        "then": {
                          "template": ""
                        },
                        "else": "parent.annotations.summary"
                      },
                      "else": {
                        "template": ""
                      }
                    }
                  },
                  "else": "annotations.summary"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "annotations.description",
                        "exists": true
                      },
                      {
                        "path": "annotations.description",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "if": {
                      "path": "annotations.description",
                      "contains": "{{"
                    },
                    "then": {
                      "if": {
                        "all": [
                          {
                            "path": "parent.annotations.summary",
                            "exists": true
                          },
                          {
                            "path": "parent.annotations.summary",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "if": {
                          "path": "parent.annotations.summary",
                          "contains": "{{"
                        },
                        "then": {
                          "template": ""
                        },
                        "else": "parent.annotations.summary"
                      },
                      "else": {
                        "template": ""
                      }
                    },
                    "else": "annotations.description"
                  },
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "parent.annotations.summary",
                          "exists": true
                        },
                        {
                          "path": "parent.annotations.summary",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "if": {
                        "path": "parent.annotations.summary",
                        "contains": "{{"
                      },
                      "then": {
                        "template": ""
                      },
                      "else": "parent.annotations.summary"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "held": {
                "template": "0"
              }
            }
          }
        },
        {
          "name": "am_inst",
          "value": {
            "each": "alerts",
            "fields": {
              "word": {
                "template": "Alerting"
              },
              "reason": {
                "if": {
                  "path": "labels.alertname",
                  "in": [
                    "DatasourceError",
                    "DatasourceNoData"
                  ]
                },
                "then": {
                  "if": {
                    "path": "labels.alertname",
                    "equals": "DatasourceError"
                  },
                  "then": {
                    "template": "Error"
                  },
                  "else": {
                    "template": "NoData"
                  }
                },
                "else": {
                  "template": ""
                }
              },
              "since": "startsAt",
              "where": {
                "if": {
                  "all": [
                    {
                      "path": "labels.instance",
                      "exists": true
                    },
                    {
                      "path": "labels.instance",
                      "not": ""
                    }
                  ]
                },
                "then": "labels.instance",
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "labels.service",
                        "exists": true
                      },
                      {
                        "path": "labels.service",
                        "not": ""
                      }
                    ]
                  },
                  "then": "labels.service",
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "labels.job",
                          "exists": true
                        },
                        {
                          "path": "labels.job",
                          "not": ""
                        }
                      ]
                    },
                    "then": "labels.job",
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "labels.host",
                            "exists": true
                          },
                          {
                            "path": "labels.host",
                            "not": ""
                          }
                        ]
                      },
                      "then": "labels.host",
                      "else": {
                        "if": {
                          "all": [
                            {
                              "path": "labels.pod",
                              "exists": true
                            },
                            {
                              "path": "labels.pod",
                              "not": ""
                            }
                          ]
                        },
                        "then": "labels.pod",
                        "else": {
                          "template": ""
                        }
                      }
                    }
                  }
                }
              },
              "severity": {
                "if": {
                  "all": [
                    {
                      "path": "labels.severity",
                      "exists": true
                    },
                    {
                      "path": "labels.severity",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{labels.severity|lower}"
                },
                "else": {
                  "template": ""
                }
              },
              "summary": {
                "if": {
                  "all": [
                    {
                      "path": "annotations.summary",
                      "exists": true
                    },
                    {
                      "path": "annotations.summary",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "if": {
                    "path": "annotations.summary",
                    "contains": "{{"
                  },
                  "then": {
                    "if": {
                      "all": [
                        {
                          "path": "annotations.description",
                          "exists": true
                        },
                        {
                          "path": "annotations.description",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "if": {
                        "path": "annotations.description",
                        "contains": "{{"
                      },
                      "then": {
                        "if": {
                          "all": [
                            {
                              "path": "annotations.summary",
                              "exists": true
                            },
                            {
                              "path": "annotations.summary",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "if": {
                            "path": "annotations.summary",
                            "contains": "{{"
                          },
                          "then": {
                            "template": ""
                          },
                          "else": "annotations.summary"
                        },
                        "else": {
                          "template": ""
                        }
                      },
                      "else": "annotations.description"
                    },
                    "else": {
                      "if": {
                        "all": [
                          {
                            "path": "annotations.summary",
                            "exists": true
                          },
                          {
                            "path": "annotations.summary",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "if": {
                          "path": "annotations.summary",
                          "contains": "{{"
                        },
                        "then": {
                          "template": ""
                        },
                        "else": "annotations.summary"
                      },
                      "else": {
                        "template": ""
                      }
                    }
                  },
                  "else": "annotations.summary"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "annotations.description",
                        "exists": true
                      },
                      {
                        "path": "annotations.description",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "if": {
                      "path": "annotations.description",
                      "contains": "{{"
                    },
                    "then": {
                      "if": {
                        "all": [
                          {
                            "path": "annotations.summary",
                            "exists": true
                          },
                          {
                            "path": "annotations.summary",
                            "not": ""
                          }
                        ]
                      },
                      "then": {
                        "if": {
                          "path": "annotations.summary",
                          "contains": "{{"
                        },
                        "then": {
                          "template": ""
                        },
                        "else": "annotations.summary"
                      },
                      "else": {
                        "template": ""
                      }
                    },
                    "else": "annotations.description"
                  },
                  "else": {
                    "if": {
                      "all": [
                        {
                          "path": "annotations.summary",
                          "exists": true
                        },
                        {
                          "path": "annotations.summary",
                          "not": ""
                        }
                      ]
                    },
                    "then": {
                      "if": {
                        "path": "annotations.summary",
                        "contains": "{{"
                      },
                      "then": {
                        "template": ""
                      },
                      "else": "annotations.summary"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "held": {
                "if": {
                  "path": "status.silencedBy[0]",
                  "exists": true
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              }
            }
          }
        },
        {
          "name": "n_am",
          "value": {
            "count": "am_inst"
          }
        },
        {
          "name": "quiet_inst",
          "value": {
            "each": "inst",
            "where": {
              "path": "word",
              "in": [
                "Pending",
                "Normal"
              ]
            },
            "fields": {
              "word": "word",
              "reason": "reason",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "held": "held"
            }
          }
        },
        {
          "name": "from_am",
          "value": {
            "each": [
              "am_inst",
              "quiet_inst"
            ],
            "fields": {
              "word": "word",
              "reason": "reason",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "held": "held"
            }
          }
        },
        {
          "name": "picked",
          "value": {
            "if": {
              "path": "n_am",
              "gt": 0
            },
            "then": "from_am",
            "else": "inst"
          }
        },
        {
          "name": "rows",
          "value": {
            "each": "picked",
            "fields": {
              "word": "word",
              "reason": "reason",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "held": "held",
              "rank": {
                "path": "word",
                "values": {
                  "Alerting": 0,
                  "Error": 1,
                  "NoData": 1,
                  "Pending": 2,
                  "Recovering": 3
                },
                "default": 4
              },
              "red": {
                "if": {
                  "path": "severity",
                  "in": [
                    "critical",
                    "crit",
                    "high",
                    "page",
                    "error",
                    "emergency",
                    "alert",
                    "p1",
                    "sev1",
                    "fatal"
                  ]
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              }
            }
          }
        },
        {
          "name": "shown",
          "value": {
            "each": "rows",
            "sort": {
              "by": [
                "held",
                "rank",
                "where"
              ]
            },
            "limit": 60,
            "fields": {
              "word": "word",
              "reason": "reason",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "held": "held",
              "red": "red"
            }
          }
        },
        {
          "name": "n_rows",
          "value": {
            "count": "rows"
          }
        },
        {
          "name": "n_shown",
          "value": {
            "count": "shown"
          }
        },
        {
          "name": "n_inst",
          "value": {
            "add": [
              {
                "path": "rl[0].totals.alerting",
                "default": 0
              },
              {
                "path": "rl[0].totals.pending",
                "default": 0
              },
              {
                "path": "rl[0].totals.normal",
                "default": 0
              },
              {
                "path": "rl[0].totals.error",
                "default": 0
              },
              {
                "path": "rl[0].totals.nodata",
                "default": 0
              },
              {
                "path": "rl[0].totals.recovering",
                "default": 0
              }
            ]
          }
        },
        {
          "name": "n_alerting",
          "value": {
            "if": {
              "path": "n_am",
              "gt": 0
            },
            "then": {
              "count": "am_inst",
              "where": {
                "path": "held",
                "equals": 0
              }
            },
            "else": {
              "add": [
                {
                  "path": "rl[0].totals.alerting",
                  "default": 0
                },
                {
                  "path": "rl[0].totals.recovering",
                  "default": 0
                },
                {
                  "path": "rl[0].totals.error",
                  "default": 0
                },
                {
                  "path": "rl[0].totals.nodata",
                  "default": 0
                }
              ]
            }
          }
        },
        {
          "name": "n_red",
          "value": {
            "count": "rows",
            "where": {
              "all": [
                {
                  "path": "word",
                  "in": [
                    "Alerting",
                    "Recovering",
                    "Error",
                    "NoData"
                  ]
                },
                {
                  "path": "held",
                  "equals": 0
                },
                {
                  "path": "red",
                  "equals": 1
                }
              ]
            }
          }
        },
        {
          "name": "n_pending",
          "value": {
            "path": "rl[0].totals.pending",
            "default": 0
          }
        },
        {
          "name": "n_held",
          "value": {
            "count": "am_inst",
            "where": {
              "path": "held",
              "equals": 1
            }
          }
        },
        {
          "name": "first_since",
          "value": {
            "first": "since",
            "of": "rows",
            "where": {
              "path": "word",
              "in": [
                "Alerting",
                "Recovering",
                "Error",
                "NoData"
              ]
            },
            "sort": "since"
          }
        },
        {
          "name": "summary",
          "value": {
            "first": "summary",
            "of": "shown",
            "where": {
              "all": [
                {
                  "path": "summary",
                  "exists": true
                },
                {
                  "path": "summary",
                  "not": ""
                }
              ]
            }
          }
        },
        {
          "name": "matchers",
          "value": {
            "each": "silences",
            "where": {
              "all": [
                {
                  "path": "status.state",
                  "equals": "active"
                },
                {
                  "path": "matchers[1]",
                  "exists": false
                }
              ]
            },
            "flatten": "matchers",
            "fields": {
              "name": "name",
              "value": "value",
              "equal": "isEqual",
              "regex": "isRegex",
              "id": "parent.id",
              "ends": "parent.endsAt"
            }
          }
        },
        {
          "name": "mine",
          "value": {
            "each": "matchers",
            "where": {
              "all": [
                {
                  "path": "name",
                  "equals": "__alert_rule_uid__"
                },
                {
                  "path": "value",
                  "equals": "{param.uid}"
                },
                {
                  "path": "equal",
                  "equals": true
                },
                {
                  "path": "regex",
                  "equals": false
                }
              ]
            },
            "sort": "ends",
            "fields": {
              "id": "id",
              "ends": "ends"
            }
          }
        },
        {
          "name": "silence_id",
          "value": {
            "first": "id",
            "of": "mine"
          }
        },
        {
          "name": "silence_ends",
          "value": {
            "first": "ends",
            "of": "mine"
          }
        },
        {
          "name": "can_silence",
          "value": {
            "if": {
              "any": [
                {
                  "path": "perms[\"alert.instances:create\"]",
                  "exists": true
                },
                {
                  "path": "perms[\"alert.silences:create\"]",
                  "exists": true
                }
              ]
            },
            "then": {
              "template": "1"
            },
            "else": {
              "template": "0"
            }
          }
        },
        {
          "name": "site",
          "value": {
            "template": "http://grafana.local:3000"
          }
        },
        {
          "name": "moves",
          "value": {
            "each": "changes",
            "where": {
              "any": [
                {
                  "path": "newState",
                  "contains": "Alerting"
                },
                {
                  "path": "newState",
                  "contains": "Pending"
                },
                {
                  "all": [
                    {
                      "path": "newState",
                      "contains": "Normal"
                    },
                    {
                      "path": "prevState",
                      "not": {
                        "path": "newState"
                      }
                    }
                  ]
                }
              ]
            },
            "limit": 4,
            "fields": {
              "id": "id",
              "time": "time",
              "new": {
                "path": "newState",
                "match": "^([A-Za-z]+)",
                "default": ""
              },
              "where": {
                "path": "text",
                "match": "instance=([^,}]+)",
                "default": ""
              }
            }
          }
        },
        {
          "name": "n_moves",
          "value": {
            "count": "moves"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "now",
      "type": "status",
      "width": "full",
      "label": "Now",
      "state": "unknown",
      "actions_placement": "corner",
      "source": {
        "ref": "rule",
        "empty": {
          "text": "Grafana no longer has this rule, or the token can't see its folder.",
          "when": {
            "path": "found",
            "equals": 0
          }
        },
        "map": {
          "state": {
            "if": {
              "path": "rl[0].paused",
              "equals": true
            },
            "then": {
              "template": "off"
            },
            "else": {
              "if": {
                "path": "n_red",
                "gt": 0
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "if": {
                  "path": "n_alerting",
                  "gt": 0
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "if": {
                    "path": "rl[0].health",
                    "in": [
                      "error",
                      "nodata"
                    ]
                  },
                  "then": {
                    "template": "warning"
                  },
                  "else": {
                    "template": "ok"
                  }
                }
              }
            }
          },
          "detail": {
            "if": {
              "path": "rl[0].paused",
              "equals": true
            },
            "then": {
              "template": "Paused"
            },
            "else": {
              "if": {
                "path": "rl[0].health",
                "equals": "error"
              },
              "then": {
                "if": {
                  "all": [
                    {
                      "path": "rl[0].error",
                      "exists": true
                    },
                    {
                      "path": "rl[0].error",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "Can't be evaluated: {rl[0].error|clip:100}"
                },
                "else": {
                  "template": "Can't be evaluated"
                }
              },
              "else": {
                "if": {
                  "path": "n_alerting",
                  "gt": 0
                },
                "then": {
                  "if": {
                    "path": "n_inst",
                    "gt": 1
                  },
                  "then": {
                    "template": "Firing on {n_alerting} of {n_inst} · since {first_since|ago}"
                  },
                  "else": {
                    "template": "Firing · since {first_since|ago}"
                  }
                },
                "else": {
                  "if": {
                    "path": "n_held",
                    "gt": 0
                  },
                  "then": {
                    "if": {
                      "path": "silence_ends",
                      "exists": true
                    },
                    "then": {
                      "template": "Silenced · ends {silence_ends|in}"
                    },
                    "else": {
                      "template": "Silenced"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "n_pending",
                      "gt": 0
                    },
                    "then": {
                      "template": "Pending · {n_pending} of {n_inst}"
                    },
                    "else": {
                      "if": {
                        "path": "rl[0].health",
                        "equals": "nodata"
                      },
                      "then": {
                        "template": "No data"
                      },
                      "else": {
                        "template": "Normal"
                      }
                    }
                  }
                }
              }
            }
          },
          "url": {
            "if": {
              "path": "site",
              "contains": "https://"
            },
            "then": {
              "template": "{site}/alerting/grafana/{param.uid}/view"
            }
          }
        }
      },
      "actions": [
        {
          "label": "Silence",
          "icon": "bell.slash",
          "method": "POST",
          "path": "/api/alertmanager/grafana/api/v2/silences",
          "body": {
            "matchers": [
              {
                "name": "__alert_rule_uid__",
                "value": "{param.uid}",
                "isEqual": true,
                "isRegex": false
              }
            ],
            "startsAt": "{action.at}",
            "endsAt": "{choice|from_now}",
            "createdBy": "Legato",
            "comment": "Silenced from Legato"
          },
          "choices": [
            {
              "label": "1 hour",
              "value": 3600
            },
            {
              "label": "4 hours",
              "value": 14400
            },
            {
              "label": "1 day",
              "value": 86400
            },
            {
              "label": "1 week",
              "value": 604800
            }
          ],
          "when": {
            "all": [
              {
                "path": "can_silence",
                "equals": 1
              },
              {
                "path": "found",
                "gt": 0
              },
              {
                "path": "silence_id",
                "exists": false
              }
            ]
          }
        },
        {
          "label": "End silence",
          "icon": "bell",
          "method": "DELETE",
          "path": "/api/alertmanager/grafana/api/v2/silence/{answer.silence_id}",
          "when": {
            "all": [
              {
                "path": "can_silence",
                "equals": 1
              },
              {
                "path": "silence_id",
                "exists": true
              }
            ]
          }
        }
      ]
    },
    {
      "id": "instances",
      "type": "list",
      "label": "Instances",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "rule",
        "when": {
          "path": "found",
          "gt": 0
        },
        "empty": {
          "text": "No instances yet: Grafana hasn't evaluated it.",
          "when": {
            "path": "n_rows",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "n_inst",
              "gt": {
                "path": "n_shown"
              }
            },
            "then": {
              "template": "Instances, {n_shown} of {n_inst}"
            },
            "else": {
              "template": "Instances"
            }
          },
          "items": {
            "each": "shown",
            "fields": {
              "key": {
                "template": "{where}|{word}"
              },
              "icon": {
                "if": {
                  "path": "held",
                  "equals": 1
                },
                "then": {
                  "template": "bell.slash"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "word",
                        "equals": "Alerting"
                      },
                      {
                        "path": "red",
                        "equals": 1
                      }
                    ]
                  },
                  "then": {
                    "template": "xmark.octagon.fill"
                  },
                  "else": {
                    "path": "word",
                    "values": {
                      "Alerting": "exclamationmark.triangle.fill",
                      "Error": "exclamationmark.triangle.fill",
                      "NoData": "exclamationmark.triangle.fill",
                      "Pending": "clock"
                    },
                    "default": "checkmark.circle"
                  }
                }
              },
              "color": {
                "if": {
                  "path": "held",
                  "equals": 0
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "word",
                        "equals": "Alerting"
                      },
                      {
                        "path": "red",
                        "equals": 1
                      }
                    ]
                  },
                  "then": {
                    "template": "red"
                  },
                  "else": {
                    "path": "word",
                    "values": {
                      "Alerting": "orange",
                      "Error": "orange",
                      "NoData": "orange"
                    }
                  }
                }
              },
              "text": {
                "if": {
                  "all": [
                    {
                      "path": "where",
                      "exists": true
                    },
                    {
                      "path": "where",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{where|clip:60}"
                },
                "else": {
                  "template": "This rule"
                }
              },
              "subtitle": {
                "if": {
                  "all": [
                    {
                      "path": "reason",
                      "exists": true
                    },
                    {
                      "path": "reason",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{reason}"
                },
                "else": {
                  "template": ""
                }
              },
              "detail": {
                "if": {
                  "path": "held",
                  "equals": 1
                },
                "then": {
                  "template": "Silenced"
                },
                "else": {
                  "path": "word",
                  "values": {
                    "Alerting": {
                      "template": "Firing · {since|ago}"
                    },
                    "Pending": {
                      "template": "Pending · {since|ago}"
                    },
                    "Error": {
                      "template": "Error · {since|ago}"
                    },
                    "NoData": {
                      "template": "No data · {since|ago}"
                    },
                    "Recovering": {
                      "template": "Recovering"
                    }
                  },
                  "default": {
                    "template": "Normal"
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "id": "rule",
      "type": "facts",
      "label": "Rule",
      "items": [
        {
          "key": "Summary",
          "value": "—"
        },
        {
          "key": "Folder",
          "value": "—"
        },
        {
          "key": "Evaluated",
          "value": "—"
        },
        {
          "key": "Severity",
          "value": "—"
        },
        {
          "key": "Last checked",
          "value": "—"
        }
      ],
      "source": {
        "ref": "rule",
        "when": {
          "path": "found",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "if": {
              "all": [
                {
                  "path": "summary",
                  "exists": true
                },
                {
                  "path": "summary",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{summary|clip:120}"
            },
            "else": {
              "template": "—"
            }
          },
          "items[1].value": {
            "template": "{rl[0].folder|clip:40} · {rl[0].group|clip:40}"
          },
          "items[2].value": {
            "if": {
              "path": "rl[0].pending_for",
              "gt": 0
            },
            "then": {
              "template": "Every {rl[0].every|duration} · pending for {rl[0].pending_for|duration}"
            },
            "else": {
              "template": "Every {rl[0].every|duration}"
            }
          },
          "items[3].value": {
            "if": {
              "all": [
                {
                  "path": "rl[0].labels.severity",
                  "exists": true
                },
                {
                  "path": "rl[0].labels.severity",
                  "not": ""
                }
              ]
            },
            "then": {
              "template": "{rl[0].labels.severity}"
            },
            "else": {
              "template": "Not labelled"
            }
          },
          "items[4].value": {
            "if": {
              "path": "rl[0].evaluated",
              "contains": "0001-01-01"
            },
            "then": {
              "template": "Not yet"
            },
            "else": {
              "template": "{rl[0].evaluated|ago}"
            }
          }
        }
      }
    },
    {
      "id": "changes",
      "type": "list",
      "label": "State changes",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "rule",
        "when": {
          "path": "n_moves",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "moves",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "icon": {
                "path": "new",
                "values": {
                  "Alerting": {
                    "template": "bell"
                  },
                  "Pending": {
                    "template": "clock"
                  }
                },
                "default": {
                  "template": "checkmark.circle"
                }
              },
              "text": {
                "path": "new",
                "values": {
                  "Alerting": {
                    "template": "Firing"
                  },
                  "Pending": {
                    "template": "Pending"
                  },
                  "Error": {
                    "template": "Error"
                  },
                  "NoData": {
                    "template": "No data"
                  }
                },
                "default": {
                  "template": "Resolved"
                }
              },
              "subtitle": {
                "if": {
                  "all": [
                    {
                      "path": "where",
                      "exists": true
                    },
                    {
                      "path": "where",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{where|clip:60}"
                },
                "else": {
                  "template": ""
                }
              },
              "detail": {
                "template": "{time|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
