{
  "$schema": "./.schemas/page.schema.json",
  "title": "Uptime",
  "icon": "server.rack",
  "sources": {
    "kuma": {
      "collect": {
        "dataset": "kuma-samples",
        "each": "monitors",
        "key": "{monitor_id}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "state": {
            "if": {
              "path": "monitor_status",
              "equals": 1
            },
            "then": {
              "if": {
                "path": "monitor_cert_is_valid",
                "equals": 0
              },
              "then": {
                "template": "critical"
              },
              "else": {
                "expires": {
                  "days": "monitor_cert_days_remaining"
                },
                "warning": 14,
                "as": "state",
                "default": "ok"
              }
            },
            "else": {
              "path": "monitor_status",
              "values": {
                "0": "critical",
                "2": "warning",
                "3": "off"
              },
              "default": "unknown"
            }
          },
          "ms": "monitor_response_time"
        },
        "readers": [
          "uptime-monitor"
        ]
      },
      "url": "https://kuma.example.com",
      "secret": "kuma",
      "every": "1m",
      "auth": {
        "type": "basic"
      },
      "requests": {
        "monitors": {
          "path": "/metrics",
          "format": "prometheus",
          "group_by": "monitor_id",
          "split_by": [
            "window"
          ]
        }
      },
      "derive": [
        {
          "name": "cert_next",
          "value": {
            "first": "monitor_name",
            "of": "monitors",
            "where": {
              "path": "monitor_cert_days_remaining",
              "gte": -36500
            },
            "sort": "monitor_cert_days_remaining"
          }
        },
        {
          "name": "cert_next_days",
          "value": {
            "first": "monitor_cert_days_remaining",
            "of": "monitors",
            "where": {
              "path": "monitor_cert_days_remaining",
              "gte": -36500
            },
            "sort": "monitor_cert_days_remaining"
          }
        },
        {
          "name": "cert_next_id",
          "value": {
            "first": "monitor_id",
            "of": "monitors",
            "where": {
              "path": "monitor_cert_days_remaining",
              "gte": -36500
            },
            "sort": "monitor_cert_days_remaining"
          }
        },
        {
          "name": "slow_name",
          "value": {
            "first": "monitor_name",
            "of": "monitors",
            "where": {
              "all": [
                {
                  "path": "monitor_type",
                  "equals": "http"
                },
                {
                  "path": "monitor_status",
                  "equals": 1
                },
                {
                  "path": "monitor_response_time",
                  "gte": 0
                }
              ]
            },
            "sort": {
              "by": "monitor_response_time",
              "order": "desc"
            }
          }
        },
        {
          "name": "slow_ms",
          "value": {
            "first": "monitor_response_time",
            "of": "monitors",
            "where": {
              "all": [
                {
                  "path": "monitor_type",
                  "equals": "http"
                },
                {
                  "path": "monitor_status",
                  "equals": 1
                },
                {
                  "path": "monitor_response_time",
                  "gte": 0
                }
              ]
            },
            "sort": {
              "by": "monitor_response_time",
              "order": "desc"
            }
          }
        },
        {
          "name": "web_avg",
          "value": {
            "avg": "monitor_response_time",
            "of": "monitors",
            "where": {
              "all": [
                {
                  "path": "monitor_type",
                  "equals": "http"
                },
                {
                  "path": "monitor_status",
                  "equals": 1
                },
                {
                  "path": "monitor_response_time",
                  "gte": 0
                }
              ]
            },
            "round": 0
          }
        },
        {
          "name": "up_1d",
          "value": {
            "avg": "monitor_uptime_ratio_1d",
            "of": "monitors",
            "where": {
              "all": [
                {
                  "path": "monitor_type",
                  "not": "group"
                },
                {
                  "path": "monitor_status",
                  "not": 3
                }
              ]
            }
          }
        },
        {
          "name": "up_30d",
          "value": {
            "avg": "monitor_uptime_ratio_30d",
            "of": "monitors",
            "where": {
              "path": "monitor_type",
              "not": "group"
            }
          }
        },
        {
          "name": "up_365d",
          "value": {
            "avg": "monitor_uptime_ratio_365d",
            "of": "monitors",
            "where": {
              "path": "monitor_type",
              "not": "group"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "monitors",
      "type": "fleet",
      "label": "Monitors",
      "max_items": 40,
      "items": [],
      "source": {
        "ref": "kuma",
        "map": {
          "items": {
            "each": "monitors",
            "where": {
              "path": "monitor_type",
              "not": "group"
            },
            "sort": "monitor_name",
            "fields": {
              "name": "monitor_name",
              "state": {
                "if": {
                  "path": "monitor_status",
                  "equals": 1
                },
                "then": {
                  "if": {
                    "path": "monitor_cert_is_valid",
                    "equals": 0
                  },
                  "then": {
                    "template": "critical"
                  },
                  "else": {
                    "expires": {
                      "days": "monitor_cert_days_remaining"
                    },
                    "warning": 14,
                    "as": "state",
                    "default": "ok"
                  }
                },
                "else": {
                  "path": "monitor_status",
                  "values": {
                    "0": "critical",
                    "2": "warning",
                    "3": "off"
                  },
                  "default": "unknown"
                }
              },
              "detail": {
                "if": {
                  "path": "monitor_status",
                  "equals": 0
                },
                "then": {
                  "template": "Down"
                },
                "else": {
                  "if": {
                    "path": "monitor_status",
                    "equals": 2
                  },
                  "then": {
                    "template": "Pending"
                  },
                  "else": {
                    "if": {
                      "path": "monitor_status",
                      "equals": 3
                    },
                    "then": {
                      "template": "In maintenance"
                    },
                    "else": {
                      "if": {
                        "path": "monitor_cert_is_valid",
                        "equals": 0
                      },
                      "then": {
                        "template": "Certificate not valid"
                      },
                      "else": {
                        "if": {
                          "path": "monitor_cert_days_remaining",
                          "lt": 0
                        },
                        "then": {
                          "template": "Certificate expired"
                        },
                        "else": {
                          "if": {
                            "path": "monitor_cert_days_remaining",
                            "equals": 0
                          },
                          "then": {
                            "template": "Certificate expires today"
                          },
                          "else": {
                            "if": {
                              "path": "monitor_cert_days_remaining",
                              "equals": 1
                            },
                            "then": {
                              "template": "Certificate expires tomorrow"
                            },
                            "else": {
                              "if": {
                                "all": [
                                  {
                                    "path": "monitor_cert_days_remaining",
                                    "gte": 0
                                  },
                                  {
                                    "path": "monitor_cert_days_remaining",
                                    "lte": 14
                                  }
                                ]
                              },
                              "then": {
                                "template": "Certificate expires in {monitor_cert_days_remaining} days"
                              },
                              "else": {
                                "if": {
                                  "path": "monitor_type",
                                  "equals": "push"
                                },
                                "then": {
                                  "template": "{monitor_uptime_ratio_1d|percent:2} in 24 hours"
                                },
                                "else": {
                                  "template": "{monitor_response_time} ms · {monitor_uptime_ratio_1d|percent:2} in 24 hours"
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              },
              "group": {
                "path": "monitor_type",
                "values": {
                  "http": "Services",
                  "keyword": "Services",
                  "push": "Jobs and backups",
                  "dns": "DNS"
                },
                "default": "Other"
              },
              "url": {
                "template": "legato://page/uptime-monitor?id={monitor_id|raw}"
              }
            }
          }
        }
      }
    },
    {
      "id": "response",
      "type": "sparkline",
      "label": "Web response, average",
      "unit": "ms",
      "change": false,
      "points": [],
      "source": {
        "ref": "kuma",
        "map": {
          "points": {
            "history": "web_avg",
            "every": "1h",
            "keep": 24
          }
        }
      }
    },
    {
      "id": "uptime",
      "type": "facts",
      "label": "Uptime",
      "items": [
        {
          "key": "24 hours",
          "value": "—"
        },
        {
          "key": "Next certificate",
          "value": "—"
        },
        {
          "key": "30 days",
          "value": "—"
        },
        {
          "key": "365 days",
          "value": "—"
        }
      ],
      "source": {
        "ref": "kuma",
        "map": {
          "items[0].value": {
            "template": "{up_1d|percent:2}"
          },
          "items[1].value": {
            "if": {
              "path": "cert_next_days",
              "lt": 0
            },
            "then": {
              "template": "{cert_next} · expired"
            },
            "else": {
              "template": "{cert_next} · {cert_next_days} days"
            }
          },
          "items[1].url": {
            "template": "legato://page/uptime-monitor?id={cert_next_id|raw}"
          },
          "items[2].value": {
            "template": "{up_30d|percent:2}"
          },
          "items[3].value": {
            "template": "{up_365d|percent:2}"
          }
        }
      }
    },
    {
      "id": "explore",
      "type": "list",
      "label": "Explore",
      "items": [
        {
          "text": "Certificates",
          "url": "legato://page/uptime-certificates"
        },
        {
          "text": "Response times",
          "url": "legato://page/uptime-response"
        },
        {
          "text": "Uptime Kuma",
          "detail": "kuma.example.com",
          "url": "https://kuma.example.com"
        }
      ],
      "source": {
        "ref": "kuma",
        "map": {
          "items[0].detail": {
            "if": {
              "path": "cert_next_days",
              "lt": 0
            },
            "then": {
              "template": "{cert_next} has expired"
            },
            "else": {
              "template": "{cert_next} first, {cert_next_days} days left"
            }
          },
          "items[1].detail": {
            "template": "Slowest: {slow_name}, {slow_ms} ms"
          }
        }
      }
    }
  ]
}
