{
  "$schema": "./.schemas/page.schema.json",
  "title": "Monitor",
  "icon": "server.rack",
  "sidebar": false,
  "params": {
    "id": {
      "example": "1",
      "type": "integer"
    }
  },
  "title_from": {
    "ref": "kuma",
    "value": {
      "first": "monitor_name",
      "of": "monitors",
      "where": {
        "path": "monitor_id",
        "equals": "{param.id}"
      }
    }
  },
  "sources": {
    "samples": {
      "dataset": "kuma-samples",
      "key": "{param.id}",
      "derive": [
        {
          "name": "up",
          "value": {
            "multiply": [
              {
                "divide": [
                  {
                    "count": "kept.rows",
                    "where": {
                      "path": "row.state",
                      "in": [
                        "ok",
                        "off"
                      ]
                    }
                  },
                  {
                    "count": "kept.rows"
                  }
                ]
              },
              100
            ],
            "round": 1
          }
        }
      ]
    },
    "kuma": {
      "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": "found",
          "value": {
            "count": "monitors",
            "where": {
              "path": "monitor_id",
              "equals": "{param.id}"
            }
          }
        },
        {
          "name": "m",
          "value": {
            "each": "monitors",
            "where": {
              "path": "monitor_id",
              "equals": "{param.id}"
            },
            "limit": 1,
            "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"
                }
              },
              "status": "monitor_status",
              "type": "monitor_type",
              "url": "monitor_url",
              "host": "monitor_hostname",
              "port": "monitor_port",
              "ms": "monitor_response_time",
              "up1d": "monitor_uptime_ratio_1d",
              "up30d": "monitor_uptime_ratio_30d",
              "up365d": "monitor_uptime_ratio_365d",
              "avg30d": {
                "path": "monitor_response_time_seconds_30d",
                "scale": 1000,
                "round": 0
              },
              "cert": "monitor_cert_days_remaining",
              "cert_valid": "monitor_cert_is_valid"
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "monitor",
      "type": "status",
      "width": "full",
      "label": "Now",
      "state": "unknown",
      "source": {
        "ref": "kuma",
        "empty": {
          "text": "Uptime Kuma no longer has this monitor.",
          "when": {
            "path": "found",
            "equals": 0
          }
        },
        "map": {
          "state": "m[0].state",
          "detail": {
            "if": {
              "path": "m[0].status",
              "equals": 0
            },
            "then": {
              "template": "Down"
            },
            "else": {
              "if": {
                "path": "m[0].status",
                "equals": 2
              },
              "then": {
                "template": "Pending"
              },
              "else": {
                "if": {
                  "path": "m[0].status",
                  "equals": 3
                },
                "then": {
                  "template": "In maintenance"
                },
                "else": {
                  "if": {
                    "path": "m[0].type",
                    "equals": "push"
                  },
                  "then": {
                    "template": "Up"
                  },
                  "else": {
                    "template": "Up · {m[0].ms} ms"
                  }
                }
              }
            }
          },
          "url": {
            "if": {
              "path": "m[0].url",
              "contains": "https://"
            },
            "then": {
              "template": "{m[0].url|origin}"
            }
          }
        }
      }
    },
    {
      "id": "history",
      "type": "strip",
      "label": "Last 24 hours",
      "start_label": "24 hours ago",
      "end_label": "Now",
      "segments": [],
      "source": {
        "ref": "samples",
        "map": {
          "segments": {
            "each": "kept.rows",
            "by_time": {
              "at": "at",
              "span": "kept.span"
            },
            "buckets": 96,
            "fields": {
              "state": {
                "worst": "row.state",
                "of": "rows"
              },
              "label": {
                "template": "{from|time}"
              }
            }
          },
          "detail": {
            "template": "{up}% up"
          }
        }
      }
    },
    {
      "id": "response",
      "type": "sparkline",
      "label": "Response",
      "unit": "ms",
      "change": false,
      "points": [],
      "start_label": "24 hours ago",
      "end_label": "Now",
      "source": {
        "ref": "samples",
        "map": {
          "points": {
            "each": "kept.rows",
            "by_time": {
              "at": "at",
              "span": "kept.span"
            },
            "buckets": 48,
            "value": "row.ms"
          }
        }
      }
    },
    {
      "id": "today",
      "type": "stat",
      "label": "Uptime, 24 hours",
      "value": "—",
      "source": {
        "ref": "kuma",
        "map": {
          "value": {
            "template": "{m[0].up1d|percent:2}"
          }
        }
      }
    },
    {
      "id": "speed",
      "type": "stat",
      "label": "Response",
      "value": "—",
      "unit": "ms",
      "source": {
        "ref": "kuma",
        "map": {
          "value": {
            "if": {
              "path": "m[0].ms",
              "gte": 0
            },
            "then": "m[0].ms",
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "more",
      "type": "facts",
      "label": "Details",
      "items": [
        {
          "key": "30 days",
          "value": "—"
        },
        {
          "key": "365 days",
          "value": "—"
        },
        {
          "key": "Certificate",
          "value": "—"
        },
        {
          "key": "Average, 30 days",
          "value": "—"
        },
        {
          "key": "Checks",
          "value": "—"
        },
        {
          "key": "Type",
          "value": "—"
        }
      ],
      "source": {
        "ref": "kuma",
        "map": {
          "items[0].value": {
            "template": "{m[0].up30d|percent:2}"
          },
          "items[1].value": {
            "template": "{m[0].up365d|percent:2}"
          },
          "items[2].value": {
            "if": {
              "path": "m[0].cert",
              "lt": 0
            },
            "then": {
              "template": "Expired"
            },
            "else": {
              "if": {
                "path": "m[0].cert_valid",
                "equals": 0
              },
              "then": {
                "template": "Not valid"
              },
              "else": {
                "if": {
                  "path": "m[0].cert",
                  "gte": 0
                },
                "then": {
                  "template": "Valid · {m[0].cert} days left"
                },
                "else": {
                  "template": "None"
                }
              }
            }
          },
          "items[3].value": {
            "if": {
              "path": "m[0].avg30d",
              "gte": 0
            },
            "then": {
              "template": "{m[0].avg30d} ms"
            },
            "else": {
              "template": "—"
            }
          },
          "items[4].value": {
            "if": {
              "path": "m[0].type",
              "equals": "push"
            },
            "then": {
              "template": "Waits for a push"
            },
            "else": {
              "if": {
                "path": "m[0].type",
                "equals": "dns"
              },
              "then": {
                "template": "A lookup of {m[0].host}"
              },
              "else": {
                "if": {
                  "path": "m[0].type",
                  "equals": "port"
                },
                "then": {
                  "template": "{m[0].host}:{m[0].port|raw}"
                },
                "else": {
                  "if": {
                    "path": "m[0].type",
                    "equals": "ping"
                  },
                  "then": {
                    "template": "{m[0].host}"
                  },
                  "else": {
                    "template": "{m[0].url|origin}"
                  }
                }
              }
            }
          },
          "items[5].value": {
            "path": "m[0].type",
            "values": {
              "http": "Web service",
              "push": "Job",
              "dns": "DNS",
              "keyword": "Web service",
              "port": "Port",
              "ping": "Ping"
            },
            "default": {
              "template": "{m[0].type|title}"
            }
          }
        }
      }
    }
  ]
}
