{
  "$schema": "./.schemas/page.schema.json",
  "title": "Service status",
  "icon": "waveform.path.ecg",
  "sources": {
    "status": {
      "hosts": {
        "list": "status-pages",
        "max": 30
      },
      "every": "2m",
      "requests": {
        "summary": "/api/v2/summary.json"
      },
      "derive": [
        {
          "name": "name",
          "value": "summary.page.name"
        },
        {
          "name": "url",
          "value": "summary.page.url"
        },
        {
          "name": "rating",
          "value": {
            "path": "summary.status.indicator",
            "values": {
              "none": "ok",
              "minor": "warning",
              "major": "critical",
              "critical": "critical",
              "maintenance": "off"
            },
            "default": "unknown"
          }
        },
        {
          "name": "words",
          "value": "summary.status.description"
        },
        {
          "name": "open",
          "value": {
            "each": "summary.incidents",
            "limit": 20,
            "fields": {
              "id": "id",
              "name": "name",
              "status": "status",
              "rank": {
                "path": "impact",
                "values": {
                  "critical": 0,
                  "major": 1,
                  "minor": 2,
                  "none": 3
                },
                "default": 3
              },
              "impact": {
                "path": "impact",
                "values": {
                  "critical": "Critical",
                  "major": "Major",
                  "minor": "Minor",
                  "none": "No impact"
                },
                "default": {
                  "template": "{impact|title}"
                }
              },
              "latest": "incident_updates[0].body",
              "link": {
                "if": {
                  "path": "shortlink",
                  "exists": true
                },
                "then": "shortlink"
              }
            }
          }
        },
        {
          "name": "newest",
          "value": "summary.incidents[0].name"
        }
      ],
      "fail": {
        "when": {
          "path": "summary.page.id",
          "exists": false
        },
        "message": {
          "template": "not a status page"
        }
      }
    },
    "history": {
      "hosts": {
        "list": "status-pages",
        "max": 30
      },
      "every": "15m",
      "requests": {
        "incidents": "/api/v2/incidents.json"
      },
      "derive": [
        {
          "name": "name",
          "value": "incidents.page.name"
        },
        {
          "name": "url",
          "value": "incidents.page.url"
        },
        {
          "name": "past",
          "value": {
            "each": "incidents.incidents",
            "where": {
              "path": "status",
              "in": [
                "resolved",
                "postmortem"
              ]
            },
            "limit": 5,
            "fields": {
              "id": "id",
              "name": "name",
              "impact": {
                "path": "impact",
                "values": {
                  "critical": "Critical",
                  "major": "Major",
                  "minor": "Minor",
                  "none": "No impact"
                },
                "default": {
                  "template": "{impact|title}"
                }
              },
              "resolved_at": "resolved_at",
              "link": {
                "if": {
                  "path": "shortlink",
                  "exists": true
                },
                "then": "shortlink"
              },
              "mins": {
                "subtract": [
                  {
                    "if": {
                      "path": "started_at",
                      "exists": true
                    },
                    "then": {
                      "since": "started_at"
                    },
                    "else": {
                      "since": "created_at"
                    }
                  },
                  {
                    "since": "resolved_at"
                  }
                ],
                "scale": 0.016666666666666666,
                "round": 0
              }
            }
          }
        }
      ],
      "fail": {
        "when": {
          "path": "incidents.page.id",
          "exists": false
        },
        "message": {
          "template": "not a status page"
        }
      }
    }
  },
  "cards": [
    {
      "id": "vendors",
      "type": "fleet",
      "label": "Services",
      "max_items": 24,
      "items": [],
      "source": {
        "ref": "status",
        "map": {
          "items": {
            "each": "hosts",
            "fields": {
              "name": {
                "if": {
                  "path": "answer.name",
                  "exists": true
                },
                "then": {
                  "template": "{answer.name|clip:24}"
                },
                "else": {
                  "template": "{entry|clip:24}"
                }
              },
              "state": {
                "if": {
                  "any": [
                    {
                      "path": "state",
                      "equals": "answered"
                    },
                    {
                      "all": [
                        {
                          "path": "answer.name",
                          "exists": true
                        },
                        {
                          "path": "fails",
                          "lt": 3
                        }
                      ]
                    }
                  ]
                },
                "then": "answer.rating",
                "else": {
                  "template": "unknown"
                }
              },
              "detail": {
                "if": {
                  "path": "state",
                  "equals": "answered"
                },
                "then": {
                  "if": {
                    "path": "answer.newest",
                    "exists": true
                  },
                  "then": {
                    "template": "{answer.newest|clip:80}"
                  },
                  "else": {
                    "if": {
                      "path": "answer.rating",
                      "not": "ok"
                    },
                    "then": {
                      "template": "{answer.words|clip:80}"
                    }
                  }
                },
                "else": {
                  "template": "{error|clip:80}"
                }
              },
              "url": "origin"
            }
          }
        }
      }
    },
    {
      "id": "incidents",
      "type": "list",
      "label": "Open incidents",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "status",
        "when": {
          "value": {
            "count": "hosts",
            "where": {
              "all": [
                {
                  "any": [
                    {
                      "path": "state",
                      "equals": "answered"
                    },
                    {
                      "all": [
                        {
                          "path": "answer.name",
                          "exists": true
                        },
                        {
                          "path": "fails",
                          "lt": 3
                        }
                      ]
                    }
                  ]
                },
                {
                  "path": "answer.open[0].id",
                  "exists": true
                }
              ]
            }
          },
          "gt": 0
        },
        "map": {
          "items": {
            "each": "hosts",
            "where": {
              "any": [
                {
                  "path": "state",
                  "equals": "answered"
                },
                {
                  "all": [
                    {
                      "path": "answer.name",
                      "exists": true
                    },
                    {
                      "path": "fails",
                      "lt": 3
                    }
                  ]
                }
              ]
            },
            "flatten": "answer.open",
            "sort": "rank",
            "limit": 60,
            "fields": {
              "key": {
                "template": "{parent.key}-{id}"
              },
              "text": {
                "template": "{name|clip:160}"
              },
              "subtitle": {
                "template": "{parent.answer.name|clip:24} · {latest|clip:50}"
              },
              "detail": {
                "template": "{impact} · {status|title}"
              },
              "url": {
                "if": {
                  "path": "link",
                  "exists": true
                },
                "then": "link",
                "else": "parent.answer.url"
              }
            }
          }
        }
      }
    },
    {
      "id": "recent-heading",
      "type": "heading",
      "text": "Recent incidents",
      "collapsed": true,
      "cards": [
        "recent"
      ],
      "source": {
        "ref": "history",
        "when": {
          "value": {
            "count": "hosts",
            "where": {
              "all": [
                {
                  "any": [
                    {
                      "path": "state",
                      "equals": "answered"
                    },
                    {
                      "all": [
                        {
                          "path": "answer.name",
                          "exists": true
                        },
                        {
                          "path": "fails",
                          "lt": 3
                        }
                      ]
                    }
                  ]
                },
                {
                  "path": "answer.past[0].id",
                  "exists": true
                }
              ]
            }
          },
          "gt": 0
        }
      }
    },
    {
      "id": "recent",
      "type": "list",
      "label": "Recent incidents",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "history",
        "when": {
          "value": {
            "count": "hosts",
            "where": {
              "all": [
                {
                  "any": [
                    {
                      "path": "state",
                      "equals": "answered"
                    },
                    {
                      "all": [
                        {
                          "path": "answer.name",
                          "exists": true
                        },
                        {
                          "path": "fails",
                          "lt": 3
                        }
                      ]
                    }
                  ]
                },
                {
                  "path": "answer.past[0].id",
                  "exists": true
                }
              ]
            }
          },
          "gt": 0
        },
        "map": {
          "items": {
            "each": "hosts",
            "where": {
              "any": [
                {
                  "path": "state",
                  "equals": "answered"
                },
                {
                  "all": [
                    {
                      "path": "answer.name",
                      "exists": true
                    },
                    {
                      "path": "fails",
                      "lt": 3
                    }
                  ]
                }
              ]
            },
            "flatten": "answer.past",
            "sort": {
              "by": "resolved_at",
              "order": "desc",
              "as": "date"
            },
            "limit": 20,
            "fields": {
              "key": {
                "template": "{parent.key}-{id}"
              },
              "text": {
                "template": "{name|clip:160}"
              },
              "subtitle": {
                "if": {
                  "path": "mins",
                  "gte": 1
                },
                "then": {
                  "template": "{parent.answer.name|clip:24} · {impact} · {mins|duration:min}"
                },
                "else": {
                  "template": "{parent.answer.name|clip:24} · {impact}"
                }
              },
              "detail": {
                "template": "{resolved_at|ago}"
              },
              "url": {
                "if": {
                  "path": "link",
                  "exists": true
                },
                "then": "link",
                "else": "parent.answer.url"
              }
            }
          }
        }
      }
    }
  ]
}
