{
  "$schema": "./.schemas/page.schema.json",
  "title": "Grafana alerts",
  "icon": "si:grafana",
  "sources": {
    "grafana": {
      "url": "http://grafana.local:3000",
      "secret": "grafana",
      "auth": {
        "type": "bearer"
      },
      "every": "1m",
      "requests": {
        "rules": "/api/prometheus/grafana/api/v1/rules?state=firing&state=pending&state=recovering&limit_alerts=60",
        "errors": "/api/prometheus/grafana/api/v1/rules?health=error&health=nodata&limit_alerts=1",
        "alerts": "/api/alertmanager/grafana/api/v2/alerts?active=true&silenced=true&inhibited=true",
        "silences": "/api/alertmanager/grafana/api/v2/silences",
        "changes": "/api/annotations?type=alert&limit=40",
        "health": "/api/health"
      },
      "derive": [
        {
          "name": "rl",
          "value": {
            "each": "rules.data.groups",
            "flatten": "rules",
            "fields": {
              "uid": "uid",
              "name": "name",
              "folder": "parent.file",
              "group": "parent.name",
              "health": "health",
              "error": {
                "if": {
                  "all": [
                    {
                      "path": "lastError",
                      "exists": true
                    },
                    {
                      "path": "lastError",
                      "not": ""
                    }
                  ]
                },
                "then": "lastError",
                "else": {
                  "template": ""
                }
              },
              "labels": "labels",
              "annotations": "annotations",
              "alerts": "alerts",
              "n_al": {
                "add": [
                  {
                    "path": "totals.alerting",
                    "default": 0
                  },
                  {
                    "path": "totals.recovering",
                    "default": 0
                  }
                ]
              },
              "n_pe": {
                "path": "totals.pending",
                "default": 0
              }
            }
          }
        },
        {
          "name": "el",
          "value": {
            "each": "errors.data.groups",
            "flatten": "rules",
            "fields": {
              "uid": "uid",
              "name": "name",
              "folder": "parent.file",
              "group": "parent.name",
              "health": "health",
              "error": {
                "if": {
                  "all": [
                    {
                      "path": "lastError",
                      "exists": true
                    },
                    {
                      "path": "lastError",
                      "not": ""
                    }
                  ]
                },
                "then": "lastError",
                "else": {
                  "template": ""
                }
              },
              "labels": "labels",
              "annotations": "annotations",
              "alerts": "alerts",
              "n_al": {
                "add": [
                  {
                    "path": "totals.alerting",
                    "default": 0
                  },
                  {
                    "path": "totals.recovering",
                    "default": 0
                  }
                ]
              },
              "n_pe": {
                "path": "totals.pending",
                "default": 0
              }
            }
          }
        },
        {
          "name": "raw",
          "value": {
            "each": "rl",
            "flatten": "alerts",
            "fields": {
              "uid": "parent.uid",
              "rule": "parent.name",
              "word": {
                "path": "state",
                "match": "^([A-Za-z]+)",
                "default": "Normal"
              },
              "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": ""
                    }
                  }
                }
              }
            }
          }
        },
        {
          "name": "broken",
          "value": {
            "each": "el",
            "where": {
              "all": [
                {
                  "path": "health",
                  "in": [
                    "error",
                    "nodata"
                  ]
                },
                {
                  "path": "n_al",
                  "equals": 0
                }
              ]
            },
            "fields": {
              "uid": "uid",
              "rule": "name",
              "word": {
                "template": "Alerting"
              },
              "since": {
                "if": {
                  "path": "alerts[0].activeAt",
                  "exists": true
                },
                "then": "alerts[0].activeAt",
                "else": {
                  "template": ""
                }
              },
              "where": {
                "template": ""
              },
              "severity": {
                "if": {
                  "all": [
                    {
                      "path": "labels.severity",
                      "exists": true
                    },
                    {
                      "path": "labels.severity",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "template": "{labels.severity|lower}"
                },
                "else": {
                  "template": ""
                }
              },
              "summary": {
                "if": {
                  "path": "health",
                  "equals": "nodata"
                },
                "then": {
                  "template": "No data"
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "error",
                        "exists": true
                      },
                      {
                        "path": "error",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Can't be evaluated: {error}"
                  },
                  "else": {
                    "template": "Can't be evaluated"
                  }
                }
              }
            }
          }
        },
        {
          "name": "am_rows",
          "value": {
            "each": "alerts",
            "fields": {
              "uid": "labels.__alert_rule_uid__",
              "rule": {
                "if": {
                  "all": [
                    {
                      "path": "labels.rulename",
                      "exists": true
                    },
                    {
                      "path": "labels.rulename",
                      "not": ""
                    }
                  ]
                },
                "then": "labels.rulename",
                "else": "labels.alertname"
              },
              "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": {
                  "path": "labels.alertname",
                  "in": [
                    "DatasourceError",
                    "DatasourceNoData"
                  ]
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "annotations.Error",
                        "exists": true
                      },
                      {
                        "path": "annotations.Error",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Can't be evaluated: {annotations.Error}"
                  },
                  "else": {
                    "if": {
                      "path": "labels.alertname",
                      "equals": "DatasourceNoData"
                    },
                    "then": {
                      "template": "No data"
                    },
                    "else": {
                      "template": "Can't be evaluated"
                    }
                  }
                },
                "else": {
                  "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_rows"
          }
        },
        {
          "name": "am_firing",
          "value": {
            "each": "am_rows",
            "where": {
              "path": "held",
              "equals": 0
            },
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": {
                "if": {
                  "path": "severity",
                  "in": [
                    "critical",
                    "crit",
                    "high",
                    "page",
                    "error",
                    "emergency",
                    "alert",
                    "p1",
                    "sev1",
                    "fatal"
                  ]
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              }
            }
          }
        },
        {
          "name": "rule_firing",
          "value": {
            "each": [
              "raw",
              "broken"
            ],
            "where": {
              "path": "word",
              "in": [
                "Alerting",
                "Recovering"
              ]
            },
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": {
                "if": {
                  "path": "severity",
                  "in": [
                    "critical",
                    "crit",
                    "high",
                    "page",
                    "error",
                    "emergency",
                    "alert",
                    "p1",
                    "sev1",
                    "fatal"
                  ]
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              }
            }
          }
        },
        {
          "name": "rule_alone",
          "value": {
            "each": "rule_firing",
            "lookup": {
              "from": "am_rows",
              "by": "uid",
              "on": "uid",
              "as": "in_am"
            },
            "where": {
              "path": "in_am",
              "exists": false
            },
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": "red"
            }
          }
        },
        {
          "name": "am_and_broken",
          "value": {
            "each": [
              "am_firing",
              "rule_alone"
            ],
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": "red"
            }
          }
        },
        {
          "name": "firing_all",
          "value": {
            "if": {
              "path": "n_am",
              "gt": 0
            },
            "then": "am_and_broken",
            "else": "rule_firing"
          }
        },
        {
          "name": "firing_red",
          "value": {
            "each": "firing_all",
            "where": {
              "path": "red",
              "equals": 1
            },
            "sort": {
              "by": "since",
              "order": "desc"
            },
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": "red"
            }
          }
        },
        {
          "name": "firing_rest",
          "value": {
            "each": "firing_all",
            "where": {
              "path": "red",
              "equals": 0
            },
            "sort": {
              "by": "since",
              "order": "desc"
            },
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": "red"
            }
          }
        },
        {
          "name": "firing",
          "value": {
            "each": [
              "firing_red",
              "firing_rest"
            ],
            "limit": 60,
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "severity": "severity",
              "summary": "summary",
              "red": "red"
            }
          }
        },
        {
          "name": "pending",
          "value": {
            "each": "raw",
            "where": {
              "path": "word",
              "equals": "Pending"
            },
            "sort": {
              "by": "since",
              "order": "desc"
            },
            "limit": 60,
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "summary": "summary"
            }
          }
        },
        {
          "name": "held",
          "value": {
            "each": "am_rows",
            "where": {
              "path": "held",
              "equals": 1
            },
            "sort": {
              "by": "since",
              "order": "desc"
            },
            "limit": 60,
            "fields": {
              "uid": "uid",
              "rule": "rule",
              "since": "since",
              "where": "where",
              "summary": "summary"
            }
          }
        },
        {
          "name": "rl_alone",
          "value": {
            "each": "rl",
            "lookup": {
              "from": "am_rows",
              "by": "uid",
              "on": "uid",
              "as": "in_am"
            },
            "where": {
              "path": "in_am",
              "exists": false
            },
            "fields": {
              "uid": "uid",
              "n_al": "n_al"
            }
          }
        },
        {
          "name": "broken_alone",
          "value": {
            "each": "broken",
            "lookup": {
              "from": "am_rows",
              "by": "uid",
              "on": "uid",
              "as": "in_am"
            },
            "where": {
              "path": "in_am",
              "exists": false
            },
            "fields": {
              "uid": "uid"
            }
          }
        },
        {
          "name": "n_firing",
          "value": {
            "if": {
              "path": "n_am",
              "gt": 0
            },
            "then": {
              "add": [
                {
                  "count": "am_firing"
                },
                {
                  "sum": "n_al",
                  "of": "rl_alone"
                },
                {
                  "count": "broken_alone"
                }
              ]
            },
            "else": {
              "add": [
                {
                  "sum": "n_al",
                  "of": "rl"
                },
                {
                  "count": "broken"
                }
              ]
            }
          }
        },
        {
          "name": "n_firing_shown",
          "value": {
            "count": "firing"
          }
        },
        {
          "name": "n_red",
          "value": {
            "count": "firing_all",
            "where": {
              "path": "red",
              "equals": 1
            }
          }
        },
        {
          "name": "n_pending",
          "value": {
            "sum": "n_pe",
            "of": "rl"
          }
        },
        {
          "name": "n_pending_shown",
          "value": {
            "count": "pending"
          }
        },
        {
          "name": "n_held",
          "value": {
            "count": "am_rows",
            "where": {
              "path": "held",
              "equals": 1
            }
          }
        },
        {
          "name": "n_held_shown",
          "value": {
            "count": "held"
          }
        },
        {
          "name": "n_broken",
          "value": {
            "count": "el",
            "where": {
              "path": "health",
              "in": [
                "error",
                "nodata"
              ]
            }
          }
        },
        {
          "name": "n_rules",
          "value": {
            "add": [
              {
                "path": "rules.data.totals.firing",
                "default": 0
              },
              {
                "path": "rules.data.totals.pending",
                "default": 0
              },
              {
                "path": "rules.data.totals.inactive",
                "default": 0
              },
              {
                "path": "rules.data.totals.recovering",
                "default": 0
              }
            ]
          }
        },
        {
          "name": "active_silences",
          "value": {
            "each": "silences",
            "where": {
              "path": "status.state",
              "equals": "active"
            },
            "sort": "endsAt",
            "fields": {
              "id": "id",
              "ends": "endsAt"
            }
          }
        },
        {
          "name": "n_silences",
          "value": {
            "count": "active_silences"
          }
        },
        {
          "name": "next_end",
          "value": {
            "first": "ends",
            "of": "active_silences"
          }
        },
        {
          "name": "moves",
          "value": {
            "each": "changes",
            "where": {
              "any": [
                {
                  "path": "newState",
                  "contains": "Alerting"
                },
                {
                  "all": [
                    {
                      "path": "newState",
                      "contains": "Normal"
                    },
                    {
                      "path": "prevState",
                      "contains": "Alerting"
                    }
                  ]
                }
              ]
            },
            "limit": 4,
            "fields": {
              "id": "id",
              "rule": "alertName",
              "time": "time",
              "fired": {
                "if": {
                  "path": "newState",
                  "contains": "Alerting"
                },
                "then": {
                  "template": "1"
                },
                "else": {
                  "template": "0"
                }
              },
              "where": {
                "path": "text",
                "match": "instance=([^,}]+)",
                "default": ""
              }
            }
          }
        },
        {
          "name": "n_moves",
          "value": {
            "count": "moves"
          }
        },
        {
          "name": "line_firing",
          "value": {
            "if": {
              "path": "n_firing",
              "gt": 0
            },
            "then": {
              "template": "{n_firing} firing"
            },
            "else": {
              "template": "Nothing firing"
            }
          }
        },
        {
          "name": "line_pending",
          "value": {
            "if": {
              "path": "n_pending",
              "gt": 0
            },
            "then": {
              "template": " · {n_pending} pending"
            },
            "else": {
              "template": ""
            }
          }
        },
        {
          "name": "line_held",
          "value": {
            "if": {
              "path": "n_held",
              "gt": 0
            },
            "then": {
              "template": " · {n_held} silenced"
            },
            "else": {
              "template": ""
            }
          }
        },
        {
          "name": "site",
          "value": {
            "template": "http://grafana.local:3000"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "health",
      "type": "status",
      "placement": "page_info",
      "width": "full",
      "label": "Alerts",
      "state": "unknown",
      "source": {
        "ref": "grafana",
        "failed": {
          "state": "critical",
          "detail": "Grafana isn't answering",
          "after": 3
        },
        "map": {
          "state": {
            "if": {
              "path": "n_red",
              "gt": 0
            },
            "then": {
              "template": "critical"
            },
            "else": {
              "if": {
                "path": "n_firing",
                "gt": 0
              },
              "then": {
                "template": "warning"
              },
              "else": {
                "template": "ok"
              }
            }
          },
          "detail": {
            "template": "{line_firing}{line_pending}{line_held}"
          },
          "url": {
            "if": {
              "path": "site",
              "contains": "https://"
            },
            "then": {
              "template": "{site}/alerting/list"
            }
          }
        }
      }
    },
    {
      "id": "alerts",
      "type": "tabs",
      "same_height": true,
      "tabs": [
        {
          "label": "Firing",
          "cards": [
            {
              "id": "firing",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "grafana",
                "empty": {
                  "text": "Nothing firing.",
                  "when": {
                    "path": "n_firing",
                    "equals": 0
                  }
                },
                "map": {
                  "label": {
                    "if": {
                      "path": "n_firing",
                      "gt": {
                        "path": "n_firing_shown"
                      }
                    },
                    "then": {
                      "template": "{n_firing_shown} of {n_firing}"
                    }
                  },
                  "items": {
                    "each": "firing",
                    "fields": {
                      "key": {
                        "template": "{uid}|{where}|{since}"
                      },
                      "icon": {
                        "if": {
                          "path": "red",
                          "equals": 1
                        },
                        "then": {
                          "template": "xmark.octagon.fill"
                        },
                        "else": {
                          "template": "exclamationmark.triangle.fill"
                        }
                      },
                      "color": {
                        "if": {
                          "path": "red",
                          "equals": 1
                        },
                        "then": {
                          "template": "red"
                        },
                        "else": {
                          "template": "orange"
                        }
                      },
                      "text": {
                        "template": "{rule|clip:60}"
                      },
                      "subtitle": {
                        "if": {
                          "all": [
                            {
                              "path": "where",
                              "exists": true
                            },
                            {
                              "path": "where",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "if": {
                            "all": [
                              {
                                "path": "summary",
                                "exists": true
                              },
                              {
                                "path": "summary",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "if": {
                              "path": "summary",
                              "contains": {
                                "path": "where"
                              }
                            },
                            "then": {
                              "template": "{summary|clip:80}"
                            },
                            "else": {
                              "template": "{where|clip:30} · {summary|clip:80}"
                            }
                          },
                          "else": {
                            "template": "{where|clip:80}"
                          }
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "summary",
                                "exists": true
                              },
                              {
                                "path": "summary",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "{summary|clip:80}"
                          },
                          "else": {
                            "template": ""
                          }
                        }
                      },
                      "detail": {
                        "if": {
                          "all": [
                            {
                              "path": "since",
                              "exists": true
                            },
                            {
                              "path": "since",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "{since|ago}"
                        },
                        "else": {
                          "template": ""
                        }
                      },
                      "url": {
                        "template": "legato://page/grafana-rule?uid={uid}"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Pending",
          "cards": [
            {
              "id": "pending",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "grafana",
                "empty": {
                  "text": "Nothing pending.",
                  "when": {
                    "path": "n_pending",
                    "equals": 0
                  }
                },
                "map": {
                  "label": {
                    "if": {
                      "path": "n_pending",
                      "gt": {
                        "path": "n_pending_shown"
                      }
                    },
                    "then": {
                      "template": "{n_pending_shown} of {n_pending}"
                    }
                  },
                  "items": {
                    "each": "pending",
                    "fields": {
                      "key": {
                        "template": "{uid}|{where}|{since}"
                      },
                      "icon": {
                        "template": "clock"
                      },
                      "text": {
                        "template": "{rule|clip:60}"
                      },
                      "subtitle": {
                        "if": {
                          "all": [
                            {
                              "path": "where",
                              "exists": true
                            },
                            {
                              "path": "where",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "if": {
                            "all": [
                              {
                                "path": "summary",
                                "exists": true
                              },
                              {
                                "path": "summary",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "if": {
                              "path": "summary",
                              "contains": {
                                "path": "where"
                              }
                            },
                            "then": {
                              "template": "{summary|clip:80}"
                            },
                            "else": {
                              "template": "{where|clip:30} · {summary|clip:80}"
                            }
                          },
                          "else": {
                            "template": "{where|clip:80}"
                          }
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "summary",
                                "exists": true
                              },
                              {
                                "path": "summary",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "{summary|clip:80}"
                          },
                          "else": {
                            "template": ""
                          }
                        }
                      },
                      "detail": {
                        "if": {
                          "all": [
                            {
                              "path": "since",
                              "exists": true
                            },
                            {
                              "path": "since",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "{since|ago}"
                        },
                        "else": {
                          "template": ""
                        }
                      },
                      "url": {
                        "template": "legato://page/grafana-rule?uid={uid}"
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Silenced",
          "cards": [
            {
              "id": "silenced",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "grafana",
                "empty": {
                  "text": "Nothing silenced.",
                  "when": {
                    "path": "n_held",
                    "equals": 0
                  }
                },
                "map": {
                  "label": {
                    "if": {
                      "path": "n_held",
                      "gt": {
                        "path": "n_held_shown"
                      }
                    },
                    "then": {
                      "template": "{n_held_shown} of {n_held}"
                    }
                  },
                  "items": {
                    "each": "held",
                    "fields": {
                      "key": {
                        "template": "{uid}|{where}|{since}"
                      },
                      "icon": {
                        "template": "bell.slash"
                      },
                      "text": {
                        "template": "{rule|clip:60}"
                      },
                      "subtitle": {
                        "if": {
                          "all": [
                            {
                              "path": "where",
                              "exists": true
                            },
                            {
                              "path": "where",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "if": {
                            "all": [
                              {
                                "path": "summary",
                                "exists": true
                              },
                              {
                                "path": "summary",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "if": {
                              "path": "summary",
                              "contains": {
                                "path": "where"
                              }
                            },
                            "then": {
                              "template": "{summary|clip:80}"
                            },
                            "else": {
                              "template": "{where|clip:30} · {summary|clip:80}"
                            }
                          },
                          "else": {
                            "template": "{where|clip:80}"
                          }
                        },
                        "else": {
                          "if": {
                            "all": [
                              {
                                "path": "summary",
                                "exists": true
                              },
                              {
                                "path": "summary",
                                "not": ""
                              }
                            ]
                          },
                          "then": {
                            "template": "{summary|clip:80}"
                          },
                          "else": {
                            "template": ""
                          }
                        }
                      },
                      "detail": {
                        "if": {
                          "all": [
                            {
                              "path": "since",
                              "exists": true
                            },
                            {
                              "path": "since",
                              "not": ""
                            }
                          ]
                        },
                        "then": {
                          "template": "{since|ago}"
                        },
                        "else": {
                          "template": ""
                        }
                      },
                      "url": {
                        "template": "legato://page/grafana-rule?uid={uid}"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "grafana",
      "type": "facts",
      "label": "Grafana",
      "items": [
        {
          "key": "Rules",
          "value": "—"
        },
        {
          "key": "Silences",
          "value": "—"
        },
        {
          "key": "Version",
          "value": "—"
        }
      ],
      "source": {
        "ref": "grafana",
        "map": {
          "items[0].value": {
            "if": {
              "path": "n_broken",
              "gt": 0
            },
            "then": {
              "template": "{n_rules} · {n_broken} can't be evaluated"
            },
            "else": {
              "template": "{n_rules}"
            }
          },
          "items[1].value": {
            "if": {
              "path": "n_silences",
              "equals": 0
            },
            "then": {
              "template": "None"
            },
            "else": {
              "template": "{n_silences} · next ends {next_end|in}"
            }
          },
          "items[2].value": {
            "if": {
              "path": "health.version",
              "exists": true
            },
            "then": {
              "template": "{health.version}"
            },
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "changes",
      "type": "list",
      "label": "Recent changes",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "grafana",
        "when": {
          "path": "n_moves",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "moves",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "icon": {
                "if": {
                  "path": "fired",
                  "equals": 1
                },
                "then": {
                  "template": "bell"
                },
                "else": {
                  "template": "checkmark.circle"
                }
              },
              "text": {
                "template": "{rule|clip:60}"
              },
              "subtitle": {
                "if": {
                  "path": "fired",
                  "equals": 1
                },
                "then": {
                  "if": {
                    "all": [
                      {
                        "path": "where",
                        "exists": true
                      },
                      {
                        "path": "where",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Firing · {where}"
                  },
                  "else": {
                    "template": "Firing"
                  }
                },
                "else": {
                  "if": {
                    "all": [
                      {
                        "path": "where",
                        "exists": true
                      },
                      {
                        "path": "where",
                        "not": ""
                      }
                    ]
                  },
                  "then": {
                    "template": "Resolved · {where}"
                  },
                  "else": {
                    "template": "Resolved"
                  }
                }
              },
              "detail": {
                "template": "{time|ago}"
              }
            }
          }
        }
      }
    }
  ]
}
